forked from c9/cloud9
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatcher.js
More file actions
260 lines (225 loc) · 9.15 KB
/
Copy pathwatcher.js
File metadata and controls
260 lines (225 loc) · 9.15 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
/**
* Watcher Module for the Cloud9 IDE
*
* @copyright 2010, Ajax.org B.V.
* @license GPLv3 <http://www.gnu.org/licenses/gpl.txt>
*/
require.def("ext/watcher/watcher",
["core/ext", "core/ide", "core/util", "ext/tree/tree"],
function(ext, ide, util, tree) {
return ext.register("ext/watcher/watcher", {
name : "Watcher",
dev : "Ajax.org",
alone : true,
offline : false,
type : ext.GENERAL,
markup : null,
visible : true,
init : function() {
// console.log("Initializing watcher");
var removedPaths = {},
removedPathCount = 0,
changedPaths = {},
changedPathCount = 0,
expandedPaths = {},
_self = this;
function sendWatchFile(path) {
ide.socket.send(JSON.stringify({
"command" : "watcher",
"type" : "watchFile",
"path" : ide.workspaceDir + path.slice(ide.davPrefix.length)
}));
}
function sendUnwatchFile(path) {
ide.socket.send(JSON.stringify({
"command" : "watcher",
"type" : "unwatchFile",
"path" : ide.workspaceDir + path.slice(ide.davPrefix.length)
}));
}
function checkPage() {
var page = tabEditors.getPage(),
data = page.$model.data;
if (!data || !data.getAttribute)
return;
var path = data.getAttribute("path");
if (removedPaths[path]) {
util.question(
"File removed, keep tab open?",
path + " has been deleted, or is no longer available.",
"Do you wish to keep the file open in the editor?",
function() { // Yes
apf.xmldb.setAttribute(data, "changed", "1");
delete removedPaths[path];
--removedPathCount;
winQuestion.hide();
},
function() { // Yes to all
var pages = tabEditors.getPages();
pages.forEach(function(page) {
apf.xmldb.setAttribute(page.$model.data, "changed", "1");
});
removedPaths = {};
removedPathCount = 0;
winQuestion.hide();
},
function() { // No
tabEditors.remove(page);
delete removedPaths[path];
--removedPathCount;
winQuestion.hide();
},
function() { // No to all
var pages = tabEditors.getPages();
pages.forEach(function(page) {
if (removedPaths[page.$model.data.getAttribute("path")])
tabEditors.remove(page);
});
removedPaths = {};
removedPathCount = 0;
winQuestion.hide();
}
);
btnQuestionYesToAll.setAttribute("visible", removedPathCount > 1);
btnQuestionNoToAll.setAttribute("visible", removedPathCount > 1);
} else if (changedPaths[path]) {
util.question(
"File changed, reload tab?",
path + " has been changed by another application.",
"Do you want to reload it?",
function() { // Yes
ide.dispatchEvent("reload", {doc : page.$doc});
delete changedPaths[path];
--changedPathCount;
winQuestion.hide();
},
function() { // Yes to all
var pages = tabEditors.getPages();
pages.forEach(function (page) {
if (changedPaths[page.$model.data.getAttribute("path")])
ide.dispatchEvent("reload", {doc : page.$doc});
});
changedPaths = {};
changedPathCount = 0;
winQuestion.hide();
},
function() { // No
delete changedPaths[path];
--changedPathCount;
winQuestion.hide();
},
function() { // No to all
changedPaths = {};
changedPathCount = 0;
winQuestion.hide();
}
);
btnQuestionYesToAll.setAttribute("visible", changedPathCount > 1);
btnQuestionNoToAll.setAttribute("visible", changedPathCount > 1);
}
}
stServerConnected.addEventListener("activate", function() {
if (_self.disabled) return;
var pages = tabEditors.getPages();
pages.forEach(function (page) {
sendWatchFile(page.$model.data.getAttribute("path"));
});
for (var path in expandedPaths)
sendWatchFile(path);
});
ide.addEventListener("openfile", function(e) {
var path = e.doc.getNode().getAttribute("path");
// console.log("Opened file " + path);
if (ide.socket)
sendWatchFile(path);
else
stServerConnected.addEventListener("activate", function () {
sendWatchFile(path);
stServerConnected.removeEventListener("activate", arguments.callee);
});
});
ide.addEventListener("closefile", function(e) {
if (_self.disabled) return;
var path = e.xmlNode.getAttribute("path");
if (ide.socket)
sendUnwatchFile(path);
else
stServerConnected.addEventListener("activate", function () {
sendUnwatchFile(path);
stServerConnected.removeEventListener("activate", arguments.callee);
});
});
ide.addEventListener("socketMessage", function(e) {
if (_self.disabled) return;
var pages = tabEditors.getPages();
var message = e.message;
if ((message.type && message.type != "watcher") || !message.path)
return;
var path = ide.davPrefix + message.path.slice(ide.workspaceDir.length);
if (expandedPaths[path])
return ide.dispatchEvent("treechange", {
path : path,
files : message.files
});
if (!pages.some(function (page) {
return page.$model.data.getAttribute("path") == path;
}))
return;
switch (message.subtype) {
case "create":
break;
case "remove":
if (!removedPaths[path]) {
removedPaths[path] = path;
++removedPathCount;
checkPage();
/*
ide.dispatchEvent("treeremove", {
path : path
});
*/
}
break;
case "change":
if (!changedPaths[path]) {
changedPaths[path] = path;
++changedPathCount;
checkPage();
}
break;
}
});
tabEditors.addEventListener("afterswitch", function(e) {
if (_self.disabled) return;
checkPage();
});
trFiles.addEventListener("expand", function(e) {
if (_self.disabled) return;
var node = e.xmlNode;
if (node && node.getAttribute("type") == "folder") {
var path = node.getAttribute("path");
expandedPaths[path] = path;
sendWatchFile(path);
}
});
trFiles.addEventListener("collapse", function (e) {
if (_self.disabled) return;
var node = e.xmlNode;
if (node && node.getAttribute("type") == "folder") {
var path = node.getAttribute("path");
delete expandedPaths[path];
sendUnwatchFile(path);
}
});
},
enable : function(){
this.disabled = false;
//@todo add code here to set watchers again based on the current state
},
disable : function(){
this.disabled = true;
},
destroy : function(){
}
});
});