aboutsummaryrefslogtreecommitdiff
path: root/js/io/workflow/save-as-dialog.reel/save-as-dialog.js
blob: 52e5ab82ae49b634e29aa0d813d15bc3f5636177 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
</copyright> */

var Montage = require("montage/core/core").Montage,
    Component = require("montage/ui/component").Component,
    fileUtils = require("js/io/utils/file-utils").FileUtils;

var SaveAsDialog = exports.SaveAsDialog = Montage.create(Component, {

	hasReel: {
        value: true
    },

    fileName : {
        enumerable: true,
        writable: true,
        value: ""
    },

    folderUri:{
        enumerable: true,
        writable: true,
        value: ""
    },

    callback : {
        enumerable: true,
        writable: true,
        value: null
    },

    callbackScope : {
        enumerable: true,
        writable: true,
        value: null
    },

    willDraw: {
        enumerable: false,
        value: function() {}
    },
    draw: {
        enumerable: false,
        value: function() {}
    },
    didDraw: {
        enumerable: false,
        value: function() {
            var self = this;
            this.fileInputField.selectDirectory = true;
            this.fileInputField.pickerName = "saveAsDirectoryPicker";
            this.newFileName.value = this.fileName;
            this.fileInputField.newFileDirectory.value = this.folderUri;

            this.newFileName.addEventListener("blur", function(evt){self.handleNewFileNameOnblur(evt);}, false);
            this.eventManager.addEventListener("newFileDirectorySet", function(evt){self.handleNewFileDirectorySet(evt);}, false);

            this.enableOk();
        }
    },

    handleNewFileDirectorySet:{
         value:function(evt){
             if(!!evt._event.newFileDirectory){
                 this.folderUri = evt._event.newFileDirectory;
                 if(this.folderUri !== ""){
                     this.enableOk();
                 }
             }
         }
     },

    handleNewFileNameOnblur:{
          value:function(evt){
              this.fileName = this.newFileName.value;
              if(this.fileName !== ""){
                  if(this.fileName !== ""){
                      this.enableOk();
                  }
              }
          }
    },


    enableOk:{
        value: function(){
            if(this.isValidFileName(this.fileName) && this.isValidUri(this.folderUri) && !this.checkFileExists(this.fileName, this.folderUri)){
                this.okButton.removeAttribute("disabled");
                this.error.innerHTML="";
            }
        }
    },

    handleCancelButtonAction :{
        value:function(evt){
            //clean up memory
            //this.cleanup();

            if(this.popup){
                this.popup.hide();
            }

        }
    },

    handleOkButtonAction:{
        value: function(evt){
            var filename = this.fileName,
                newFileDirectory = this.newFileDirectory,
                success = true;
            if(this.isValidFileName(this.fileName) && this.isValidUri(this.folderUri) && !this.checkFileExists(this.fileName, this.folderUri)){
                try{
                    //validate file name and folder path
                    //check if file already exists
                    if(!!this.callback && !!this.callbackScope){//inform document-controller if save successful
                        this.callback.call(this.callbackScope, {"filename":filename, "destination": newFileDirectory});//document-controller api
                    }else{
                        //send save as event
                        var saveAsEvent = document.createEvent("Events");
                        saveAsEvent.initEvent("saveAsFile", false, false);
                        saveAsEvent.saveAsOptions = {"filename":filename, "destination": newFileDirectory};
                        this.eventManager.dispatchEvent(saveAsEvent);
                    }
                }catch(e){
                        success = false;
                        console.log("[ERROR] Failed to save:  "+ this.fileName + " at "+ this.newFileDirectory);
                        console.log(e.stack);
                }

                if(success){
                    //clean up memory
                    //this.cleanup();

                    if(this.popup){
                        this.popup.hide();
                    }
                }
            }else{
                if(this.error.innerHTML !== ""){
                    this.showError("! Name and Location should be valid.");
                }
                //disable ok
                if(!this.okButton.hasAttribute("disabled")){
                    this.okButton.setAttribute("disabled", "true");
                }
            }
        }
    },

    isValidUri:{
        value: function(uri){
            var status= fileUtils.isValidUri(uri);
            if(uri !== ""){
                if(!status){
                    this.showError("! Invalid directory.");
                }
            }
            return status;
        }
    },
    isValidFileName:{
        value: function(fileName){
            var status = fileUtils.isValidFileName(fileName);
            if(fileName !== ""){
                if(!status){
                    this.showError("! Invalid file name.");
                }
            }
            return status;
        }
    },
    checkFileExists:{
        value: function(fileUri, folderUri, fileType){
            var status= fileUtils.checkFileExists(fileUri, folderUri, fileType);
            if(status){
                this.showError("! File already exists.");
            }
            return status;
        }
    },

    showError:{
        value:function(errorString){
            this.error.innerHTML = "";
            this.error.innerHTML=errorString;
            //disable ok
            if(!this.okButton.hasAttribute("disabled")){
                this.okButton.setAttribute("disabled", "true");
            }
        }
    }

});