This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
Controller.es6
559 lines (519 loc) · 17.5 KB
/
Controller.es6
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
* Copyright 2007-2013 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <http://pyd.io/>.
*/
/**
* Singleton class that manages all actions. Can be called directly using pydio.getController().
*/
class Controller extends Observable{
/**
* Standard constructor
* @param pydioObject Pydio
* @param dataModelElementId
*/
constructor(pydioObject, dataModelElementId=null)
{
super();
this._pydioObject = pydioObject;
this._registeredKeys = new Map();
this.usersEnabled = pydioObject.Parameters.get("usersEnabled");
this.subMenus = [];
this.actions = new Map();
this.defaultActions = new Map();
this.toolbars = new Map();
this._guiActions = new Map();
this.contextChangedObs = function(event){
window.setTimeout(function(){
this.fireContextChange();
}.bind(this), 0);
}.bind(this);
this.selectionChangedObs = function(event){
window.setTimeout(function(){
this.fireSelectionChange();
}.bind(this), 0);
}.bind(this);
if(dataModelElementId){
this.localDataModel = true;
try{
this._dataModel = document.getElementById(dataModelElementId).ajxpPaneObject.getDataModel();
}catch(e){}
if(this._dataModel) {
this._connectDataModel();
}else{
this._pydioObject.observeOnce("datamodel-loaded-" + dataModelElementId, function(){
this._dataModel = document.getElementById(dataModelElementId).ajxpPaneObject.getDataModel();
this._connectDataModel();
}.bind(this));
}
}else{
this.localDataModel = false;
this._connectDataModel();
}
if(this.usersEnabled){
this._pydioObject.observe("user_logged", function(user){
this.setUser(user);
}.bind(this));
if(this._pydioObject.user) {
this.setUser(this._pydioObject.user);
}
}
}
publishActionEvent(eventName, data){
this._pydioObject.fire(eventName, data);
}
_connectDataModel(){
if(this.localDataModel){
this._dataModel.observe("context_changed", this.contextChangedObs);
this._dataModel.observe("selection_changed", this.selectionChangedObs);
this.loadActionsFromRegistry();
this._pydioObject.observe("registry_loaded", function(registry){
this.loadActionsFromRegistry(registry);
}.bind(this));
}else{
this._pydioObject.observe("context_changed", this.contextChangedObs);
this._pydioObject.observe("selection_changed", this.selectionChangedObs);
this._dataModel = this._pydioObject.getContextHolder();
}
}
updateGuiActions(actions){
actions.forEach(function(v, k){
this._guiActions.set(k, v);
}.bind(this));
}
deleteFromGuiActions(actionName){
this._guiActions.delete(actionName);
}
refreshGuiActionsI18n(){
this._guiActions.forEach(function(value, key){
value.refreshFromI18NHash();
});
}
getBackgroundTasksManager(){
if(!Controller._bgManager){
Controller._bgManager = new BackgroundTasksManager();
}
return Controller._bgManager;
}
getDataModel(){
return this._dataModel;
}
destroy(){
if(this.localDataModel && this._dataModel){
this._dataModel.stopObserving("context_changed", this.contextChangedObs);
this._dataModel.stopObserving("selection_changed", this.selectionChangedObs);
}
}
getMessage(messageId){
try{
return this._pydioObject.MessageHash[messageId];
}catch(e){
return messageId;
}
}
uiInsertForm(formId, formCode){
if(this._pydioObject.UI){
this._pydioObject.UI.insertForm(formId, formCode);
}
}
uiRemoveForm(formId){
if(this._pydioObject.UI){
this._pydioObject.UI.removeForm(formId);
}
}
uiGetModal(){
if(this._pydioObject && this._pydioObject.UI){
return this._pydioObject.UI.modal
}
return null;
}
uiMountComponents(componentsNodes){
if(this._pydioObject && this._pydioObject.UI){
return this._pydioObject.UI.mountComponents(componentsNodes);
}
}
/**
* COMPATIBILITY METHD
* @param xmlDoc
* @returns {*}
*/
parseXmlMessage(xmlDoc){
Logger.log("Controller.parseXmlMessage() is deprecated, use PydioApi instead");
if(window.console && window.console.trace){
Logger.log(console.trace());
}
return PydioApi.getClient().parseXmlMessage(xmlDoc);
}
/**
* Submits a form using Connexion class.
* @param formName String The id of the form
* @param post Boolean Whether to POST or GET
* @param completeCallback Function Callback to be called on complete
*/
submitForm(formName, post, completeCallback){
Logger.log("Controller.submitForm() is deprecated, use PydioApi instead");
return PydioApi.getClient().submitForm(formName, post, completeCallback);
}
/**
* Stores the currently logged user object
* @param oUser User User instance
*/
setUser(oUser)
{
this.oUser = oUser;
if(oUser != null && oUser.id != 'guest' && oUser.getPreference('lang') != null
&& oUser.getPreference('lang') != ""
&& oUser.getPreference('lang') != this._pydioObject.currentLanguage
&& !oUser.lock
)
{
this._pydioObject.loadI18NMessages(oUser.getPreference('lang'));
}
}
/**
* Filter the actions given the srcElement passed as arguments.
* TODO: SIGNATURE CHANGED, FROM srcElement to actionsSelectorAtt
* @param srcElement String An identifier among selectionContext, genericContext, a webfx object id
* @param ignoreGroups Array a list of groups to ignore
* @returns Array
*/
getContextActions(actionsSelectorAtt, ignoreGroups)
{
var contextActions = [];
var defaultGroup;
var contextActionsGroup = new Map();
this.actions.forEach(function(action){
if(!action.context.contextMenu) return;
if(actionsSelectorAtt == 'selectionContext' && !action.context.selection) return;
if(actionsSelectorAtt == 'directoryContext' && !action.context.dir) return;
if(actionsSelectorAtt == 'genericContext' && action.context.selection) return;
if(action.contextHidden || action.deny) return;
action.context.actionBarGroup.split(',').forEach(function(barGroup){
if(!contextActionsGroup.has(barGroup)){
contextActionsGroup.set(barGroup, []);
}
});
var isDefault = false;
if(actionsSelectorAtt == 'selectionContext'){
// set default in bold
var userSelection = this._dataModel;
if(!userSelection.isEmpty()){
var defaultAction = 'file';
if(userSelection.isUnique() && (userSelection.hasDir() || userSelection.hasMime(['ajxp_browsable_archive']))){
defaultAction = 'dir';
}
if(this.defaultActions.get(defaultAction) && action.options.name == this.defaultActions.get(defaultAction)){
isDefault = true;
}
}
}
action.context.actionBarGroup.split(',').forEach(function(barGroup){
var menuItem = {
name:action.getKeyedText(),
alt:action.options.title,
action_id:action.options.name,
image_unresolved: action.options.src,
isDefault:isDefault,
callback: function(e){this.apply();}.bind(action)
};
if(action.options.icon_class){
menuItem.icon_class = action.options.icon_class;
}
if(action.options.subMenu){
menuItem.subMenu = [];
if(action.subMenuItems.staticOptions){
menuItem.subMenu = action.subMenuItems.staticOptions;
}
if(action.subMenuItems.dynamicBuilder){
menuItem.subMenuBeforeShow = action.subMenuItems.dynamicBuilder;
}
}
contextActionsGroup.get(barGroup).push(menuItem);
if(isDefault){
defaultGroup = barGroup;
}
});
}.bind(this));
var first = true, keys = [];
if(defaultGroup && contextActionsGroup.has(defaultGroup)){
keys.push(defaultGroup);
}
for(var k of contextActionsGroup.keys()){
if(k == defaultGroup) continue;
keys.push(k);
}
keys.sort();
keys.each(function(key){
var value = contextActionsGroup.get(key);
if(!first){
contextActions.push({separator:true});
}
if(ignoreGroups && ignoreGroups.indexOf(key) != -1){
return;
}
first = false;
value.forEach(function(mItem){
contextActions.push(mItem);
});
});
return contextActions;
}
/**
* Generic method to get actions for a given component part.
* @param ajxpClassName String
* @param widgetId String
* @returns []
*/
getActionsForAjxpWidget(ajxpClassName, widgetId){
var actions = [];
this.actions.forEach(function(action){
if(action.context.ajxpWidgets && (
action.context.ajxpWidgets.indexOf(ajxpClassName+'::'+widgetId) != -1
||
action.context.ajxpWidgets.indexOf(ajxpClassName) != -1
)
&& !action.deny) actions.push(action);
});
return actions;
}
/**
* Finds a default action and fires it.
* @param defaultName String ("file", "dir", "dragndrop", "ctrldragndrop")
*/
fireDefaultAction(defaultName){
var actionName = this.defaultActions.get(defaultName);
if(actionName){
arguments[0] = actionName;
if(actionName == "ls"){
var action = this.actions.get(actionName);
if(action) action.enable(); // Force enable on default action
}
this.fireAction.apply(this, arguments);
}
}
/**
* Fire an action based on its name
* @param actionName String The name of the action
*/
fireAction (actionName) {
var action = this.actions.get(actionName);
if(action != null) {
var args = Array.from(arguments).slice(1);
action.apply(args);
}
}
/**
* Registers an accesskey for a given action.
* @param key String The access key
* @param actionName String The name of the action
* @param optionnalCommand String An optionnal argument
* that will be passed to the action when fired.
*/
registerKey(key, actionName, optionnalCommand){
if(optionnalCommand){
actionName = actionName + "::" + optionnalCommand;
}
this._registeredKeys.set(key.toLowerCase(), actionName);
}
/**
* Remove all registered keys.
*/
clearRegisteredKeys(){
this._registeredKeys = new Map();
}
/**
* Triggers an action by its access key.
* @param event Event The key event (will be stopped)
* @param keyName String A key name
*/
fireActionByKey(event, keyName)
{
if(this._registeredKeys.get(keyName))
{
if(this._registeredKeys.get(keyName).indexOf("::")!==-1){
var parts = this._registeredKeys.get(keyName).split("::");
this.fireAction(parts[0], parts[1]);
}else{
this.fireAction(this._registeredKeys.get(keyName));
}
try{
event.preventDefault();
event.stopPropagation();
}catch(e){
Logger.error("Error trying to stop event propagation");
}
}
}
/**
* Complex function called when drag'n'dropping. Basic checks of who is child of who.
* @param fileName String The dragged element
* @param destDir String The drop target node path
* @param destNodeName String The drop target node name
* @param copy Boolean Copy or Move
*/
applyDragMove(fileName, destDir, destNodeName, copy)
{
if((!copy && (!this.defaultActions.has('dragndrop') || this.getDefaultAction('dragndrop').deny)) ||
(copy && (!this.defaultActions.has('ctrldragndrop')||this.getDefaultAction('ctrldragndrop').deny))){
return;
}
var fileNames;
if(fileName == null) fileNames = this._dataModel.getFileNames();
else fileNames = [fileName];
// Check that dest is not the direct parent of source, ie current rep!
if(destDir == this._dataModel.getContextNode().getPath()){
this._pydioObject.displayMessage('ERROR', MessageHash[203]);
return;
}
// Check that dest is not child of source it self
for(var i=0; i<fileNames.length;i++){
if(destDir.lastIndexOf(fileNames[i],0) === 0){
this._pydioObject.displayMessage('ERROR', MessageHash[202]);
return;
}
}
var params = {};
params['get_action'] = this.defaultActions.get(copy?'ctrldragndrop':'dragndrop');
params['nodes[]'] = fileNames;
params['dest'] = destDir;
params['dir'] = this._dataModel.getContextNode().getPath();
PydioApi.getClient().request(params, function(transport){
this.parseXmlMessage(transport.responseXML);
}.bind(PydioApi.getClient()));
}
/**
* Get the action defined as default for a given default string
* @param defaultName String
* @returns Action
*/
getDefaultAction (defaultName){
if(this.defaultActions.has(defaultName)){
return this.actions.get(this.defaultActions.get(defaultName));
}
return null;
}
/**
* Spreads a selection change to all actions and to registered components
* by triggering ajaxplorer:actions_refreshed event.
*/
fireSelectionChange(){
var userSelection = null;
userSelection = this._dataModel;
if(userSelection.isEmpty()) userSelection = null;
this.actions.forEach(function(action){
action.fireSelectionChange(userSelection);
});
if(this.localDataModel){
this.notify("actions_refreshed");
}else{
this._pydioObject.fire("actions_refreshed");
}
}
/**
* Spreads a context change to all actions and to registered components
* by triggering ajaxplorer:actions_refreshed event.
*/
fireContextChange(){
var crtNode = this._dataModel.getContextNode();
this.actions.forEach(function(action){
action.fireContextChange(this.usersEnabled,
this.oUser,
crtNode);
}.bind(this));
if(this.localDataModel){
this.notify("actions_refreshed");
}else{
this._pydioObject.fire("actions_refreshed");
}
}
/**
* Remove all actions
*/
removeActions(){
this.actions.forEach(function(action){
action.remove();
});
this.actions = new Map();
this.clearRegisteredKeys();
}
/**
* Create actions from XML Registry
* @param registry DOMDocument
*/
loadActionsFromRegistry (registry=null){
if(!registry) {
registry = pydio.getXmlRegistry();
}
this.removeActions();
this.parseActions(registry);
this._guiActions.forEach(function(act){
this.registerAction(act);
}.bind(this));
if(this.localDataModel){
this.notify("actions_loaded");
}else{
this._pydioObject.fire("actions_loaded", this.actions);
}
this.fireContextChange();
this.fireSelectionChange();
}
/**
* Registers an action to this manager (default, accesskey).
* @param action Action
*/
registerAction (action){
var actionName = action.options.name;
this.actions.set(actionName, action);
if(action.defaults){
for(var key in action.defaults) {
if(action.defaults.hasOwnProperty(key)){
this.defaultActions.set(key, actionName);
}
}
}
if(action.options.hasAccessKey){
this.registerKey(action.options.accessKey, actionName);
}
if(action.options.specialAccessKey){
this.registerKey("key_" + action.options.specialAccessKey, actionName);
}
action.setManager(this);
}
/**
* Parse an XML action node and registers the action
* @param documentElement DOMNode The node to parse
*/
parseActions(documentElement){
var actions = XMLUtils.XPathSelectNodes(documentElement, "actions/action");
for(var i=0;i<actions.length;i++){
if(actions[i].nodeName != 'action') continue;
if(actions[i].getAttribute('enabled') == 'false') continue;
var newAction = new Action();
newAction.setManager(this);
newAction.createFromXML(actions[i]);
this.registerAction(newAction);
}
}
/**
* Find an action by its name
* @param actionName String
* @returns Action
*/
getActionByName (actionName){
return this.actions.get(actionName);
}
}