forked from c9/cloud9
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave.js
More file actions
357 lines (306 loc) · 12.2 KB
/
Copy pathsave.js
File metadata and controls
357 lines (306 loc) · 12.2 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/**
* Refactor Module for the Cloud9 IDE
*
* @copyright 2010, Ajax.org B.V.
* @license GPLv3 <http://www.gnu.org/licenses/gpl.txt>
*/
require.def("ext/save/save",
["core/ide", "core/ext", "core/util", "ext/filesystem/filesystem",
"text!ext/save/save.xml"],
function(ide, ext, util, fs, markup) {
return ext.register("ext/save/save", {
dev : "Ajax.org",
name : "Save",
alone : true,
type : ext.GENERAL,
markup : markup,
deps : [fs],
offline : false,
commands : {
"quicksave": {hint: "save the currently active file to disk"},
"saveas": {hint: "save the file to disk with a different filename"}
},
hotitems : {},
nodes : [],
saveBuffer : {},
hook : function(){
if (!self.tabEditors) return;
var _self = this;
tabEditors.addEventListener("close", this.$close = function(e) {
var at = e.page.$at;
if (!at.undo_ptr)
at.undo_ptr = at.$undostack[0];
if (at.undo_ptr && at.$undostack[at.$undostack.length-1] !== at.undo_ptr) {
ext.initExtension(_self);
winCloseConfirm.page = e.page;
winCloseConfirm.all = 0;
winCloseConfirm.show();
winCloseConfirm.addEventListener("hide", function(){
if (winCloseConfirm.all != -100) {
tabEditors.remove(winCloseConfirm.page, true);
winCloseConfirm.page.$at.undo(-1);
delete winCloseConfirm.page;
}
winCloseConfirm.removeEventListener("hide", arguments.callee);
});
btnYesAll.hide();
btnNoAll.hide();
e.preventDefault();
}
});
this.nodes.push(ide.barTools.appendChild(new apf.button({
id : "btnSave",
icon : "save.png",
tooltip : "Save",
skin : "c9-toolbarbutton",
disabled : "{!!!tabEditors.activepage}",
onclick : this.quicksave.bind(this)
})));
var saveItem, saveAsItem;
this.nodes.push(
ide.mnuFile.insertBefore(new apf.divider(), ide.mnuFile.firstChild),
ide.mnuFile.insertBefore(new apf.item({
caption : "Save All",
onclick : function(){
_self.saveall();
},
disabled : "{!!!tabEditors.activepage}"
}), ide.mnuFile.firstChild),
saveAsItem = ide.mnuFile.insertBefore(new apf.item({
caption : "Save As...",
onclick : function () {
_self.saveas();
},
disabled : "{!!!tabEditors.activepage}"
}), ide.mnuFile.firstChild),
saveItem = ide.mnuFile.insertBefore(new apf.item({
caption : "Save",
onclick : this.quicksave.bind(this),
disabled : "{!!!tabEditors.activepage}"
}), ide.mnuFile.firstChild)
);
this.hotitems["quicksave"] = [saveItem];
this.hotitems["saveas"] = [saveAsItem];
},
init : function(amlNode){
var _self = this;
winCloseConfirm.onafterrender = function(){
btnYesAll.addEventListener("click", function(){
winCloseConfirm.all = 1;
winCloseConfirm.hide();
});
btnNoAll.addEventListener("click", function(){
winCloseConfirm.all = -1;
winCloseConfirm.hide();
});
btnSaveYes.addEventListener("click", function(){
_self.quicksave(winCloseConfirm.page);
winCloseConfirm.hide()
});
btnSaveNo.addEventListener("click", function(){
winCloseConfirm.hide();
});
btnSaveCancel.addEventListener("click", function(){
winCloseConfirm.all = -100;
winCloseConfirm.hide();
});
}
},
saveall : function(){
var pages = tabEditors.getPages();
for (var i = 0; i < pages.length; i++) {
var at = pages[i].$at;
// if (at.undo_ptr && at.$undostack[at.$undostack.length-1] !== at.undo_ptr)
this.quicksave(pages[i]);
}
},
saveAllInteractive : function(pages, callback){
ext.initExtension(this);
winCloseConfirm.all = 0;
var _self = this;
apf.asyncForEach(pages, function(item, next) {
var at = item.$at;
if (at.undo_ptr && at.$undostack[at.$undostack.length-1] !== at.undo_ptr) {
if (winCloseConfirm.all == 1)
_self.quicksave(item);
//else if (winCloseConfirm.all == -1)
//item.$at.undo(-1);
if (winCloseConfirm.all)
return next();
tabEditors.set(item);
winCloseConfirm.page = item;
winCloseConfirm.show();
winCloseConfirm.addEventListener("hide", function(){
if (winCloseConfirm.all == 1)
_self.quicksave(item);
//else if (winCloseConfirm.all == -1)
//item.$at.undo(-1);
winCloseConfirm.removeEventListener("hide", arguments.callee);
next();
});
btnYesAll.setProperty("visible", pages.length > 1);
btnNoAll.setProperty("visible", pages.length > 1);
}
else
next();
},
function() {
callback(winCloseConfirm.all);
});
},
quicksave : function(page) {
if (!page || !page.$at)
page = tabEditors.getPage();
if (!page)
return;
var doc = page.$doc;
var node = doc.getNode();
if (node.getAttribute("debug"))
return;
var path = node.getAttribute("path");
var value = doc.getValue();
// check if we're already saving!
var saving = parseInt(node.getAttribute("saving"));
if (saving) {
this.saveBuffer[path] = page;
return;
}
apf.b(node).attr("saving", "1");
var _self = this, panel = sbMain.firstChild;
panel.setAttribute("caption", "Saving file " + path);
ide.dispatchEvent("beforefilesave", {node: node, doc: doc, value: value});
fs.saveFile(path, value, function(data, state, extra){
if (state != apf.SUCCESS) {
util.alert(
"Could not save document",
"An error occurred while saving this document",
"Please see if your internet connection is available and try again. "
+ (state == apf.TIMEOUT
? "The connection timed out."
: "The error reported was " + extra.message));
}
panel.setAttribute("caption", "Saved file " + path);
ide.dispatchEvent("afterfilesave", {node: node, doc: doc, value: value});
apf.b(node).attr("saving", "0");
if (_self.saveBuffer[path]) {
delete _self.saveBuffer[path];
_self.quicksave(page);
}
//setTimeout(function(){
// if (panel.caption == "Saved file " + path)
// panel.removeAttribute("caption");
//}, 2500);
});
var at = page.$at
at.undo_ptr = at.$undostack[at.$undostack.length-1];
page.$at.dispatchEvent("afterchange");
return false;
},
choosePath : function(path, select) {
var _self = this;
fs.list(path.match(/(.*)\/[^/]*/)[1], function (data, state, extra) {
if (new RegExp("<folder.*" + path + ".*>").test(data)) {
path = path.replace(/\/([^/]*)/g, "/node()[@name=\"$1\"]")
.replace(/\[@name="workspace"\]/, "")
.replace(/\//, "");
// console.log(path);
trSaveAs.expandList([path], function() {
var node = trSaveAs.getModel().data.selectSingleNode(path);
trSaveAs.select(node);
});
} else
_self.saveFileAs();
});
},
saveas : function(){
var path = tabEditors.getPage().$model.data.getAttribute("path");
ext.initExtension(this);
txtSaveAs.setValue(path);
this.choosePath(path.match(/(.*)\/[^/]/)[1]);
winSaveAs.show();
},
saveFileAs : function(page) {
var page = page || tabEditors.getPage(),
file = page.$model.data,
path = file.getAttribute("path"),
newPath = txtSaveAs.getValue();
// check if we're already saving!
var saving = parseInt(file.getAttribute("saving"));
if (saving) {
this.saveBuffer[path] = page;
return;
}
apf.b(file).attr("saving", "1");
function onconfirm() {
var panel = sbMain.firstChild,
value = page.$doc.getValue();
// console.log(value);
winConfirm.hide();
winSaveAs.hide();
panel.setAttribute("caption", "Saving file " + newPath);
fs.saveFile(newPath, value, function(value, state, extra) {
if (state != apf.SUCCESS) {
util.alert("Could not save document",
"An error occurred while saving this document",
"Please see if your internet connection is available and try again.");
}
panel.setAttribute("caption", "Saved file " + newPath);
if (path != newPath) {
var model = page.$model,
node = model.getXml();
model.load(node);
file = model.data;
fs.beforeRename(file, null, newPath);
page.$doc.setNode(file);
}
apf.b(node).attr("saving", "0");
if (_self.saveBuffer[path]) {
delete _self.saveBuffer[path];
_self.saveFileAs(page);
}
//setTimeout(function () {
// if (panel.caption == "Saved file " + newPath)
// panel.removeAttribute("caption");
//}, 2500);
});
};
if (path != newPath) {
fs.exists(newPath, function (exists) {
if (exists) {
var name = newPath.match(/\/([^/]*)$/)[1],
folder = newPath.match(/\/([^/]*)\/[^/]*$/)[1];
util.confirm(
"Are you sure?",
"\"" + name + "\" already exists, do you want to replace it?",
"A file or folder with the same name already exists in the folder "
+ folder + ". "
+ "Replacing it will overwrite it's current contents.",
onconfirm);
}
else
onconfirm();
});
}
else
onconfirm();
},
enable : function(){
this.nodes.each(function(item){
item.enable();
});
},
disable : function(){
this.nodes.each(function(item){
item.disable();
});
},
destroy : function(){
this.nodes.each(function(item){
item.destroy(true, true);
});
this.nodes = [];
tabEditors.removeEventListener("close", this.$close);
}
});
}
);