This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
Pydio.es6
493 lines (438 loc) · 17 KB
/
Pydio.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
class Pydio extends Observable{
/**
*
* @param parameters {Map}
*/
constructor(parameters){
super();
this.Parameters = parameters;
this._initLoadRep = parameters.get('initLoadRep') || null;
this.usersEnabled = parameters.get('usersEnabled') || null;
this.currentLanguage = parameters.get('currentLanguage') || null;
this.appTitle = "Pydio";
if(this.Parameters.has("customWording")){
this.appTitle = this.Parameters.get("customWording").title || "Pydio";
}
this.MessageHash = {};
if(window.MessageHash) this.MessageHash = window.MessageHash;
this.ApiClient = PydioApi.getClient();
this.ApiClient.setPydioObject(this);
this.Registry = new Registry(this);
this._rootNode = new AjxpNode("/", "Root");
this._dataModel = this._contextHolder = new PydioDataModel(false);
this._dataModel.setAjxpNodeProvider(new RemoteNodeProvider());
this._dataModel.setRootNode(this._rootNode);
// Must happen AFTER datamodel initization.
this.Controller = new Controller(this);
}
fire(eventName, data){
this.notify(eventName, data);
// Backward compatibility
if(document.fire){
document.fire("ajaxplorer:" + eventName, data);
}
}
/**
* Real initialisation sequence. Will Trigger the whole GUI building.
* Event ajaxplorer:loaded is fired at the end.
*/
init(){
if(!this.Parameters.has('SECURE_TOKEN')){
PydioApi.getClient().getBootConf(function(){
this.init();
}.bind(this));
return;
}
if(!window.useReactPydioUI){
this.UI = new PydioUI(this);
}
/*
if(window.PydioUI){
}else{
// FAKE CLASS
this.UI = {
guiLoaded: true,
modal: {
setLoadingStepCounts: function(){},
refreshDialogAppearance: function(){},
displayMessage: function(){},
initForms: function(){},
updateLoadingProgress:function(progress){
Logger.log(progress);
},
prepareHeader: function(){},
showModalDialog: function(){}
},
refreshTemplateParts: function(){},
initTemplates: function(){},
initObjects: function(){},
updateI18nTags: function(){},
insertForm:function(formId, formCode){},
removeForm: function(formId){},
mountComponents: function(componentsNodes){}
};
}
*/
let modal = this.UI && this.UI.modal ? this.UI.modal : (window.modal ? window.modal : null);
this.observe("registry_loaded", function(){
this.Registry.refreshExtensionsRegistry();
this.Registry.logXmlUser(false);
if(this.user){
var repId = this.user.getActiveRepository();
var repList = this.user.getRepositoriesList();
var repositoryObject = repList.get(repId);
if(repositoryObject) repositoryObject.loadResources();
}
if(this.UI.guiLoaded) {
this.UI.refreshTemplateParts();
this.Registry.refreshExtensionsRegistry();
this.Controller.loadActionsFromRegistry(this.getXmlRegistry());
} else {
this.observe("gui_loaded", function(){
this.UI.refreshTemplateParts();
this.Registry.refreshExtensionsRegistry();
this.Controller.loadActionsFromRegistry(this.getXmlRegistry());
}.bind(this));
}
this.loadActiveRepository();
if(this.Parameters.has("USER_GUI_ACTION")){
var a= this.Parameters.get("USER_GUI_ACTION");
this.Parameters.delete("USER_GUI_ACTION");
var aBar = this.Controller;
window.setTimeout(function(){
aBar.fireAction(a);
}, 2000);
}
}.bind(this));
if(modal) modal.setLoadingStepCounts(window.useReactPydioUI ? 1 : 5);
var starterFunc = function(){
if(!this.UI && window.useReactPydioUI){
ResourcesManager.loadClassesAndApply(["React", "PydioReactUI"], function(){
this.UI = new PydioReactUI.Builder(this);
this.UI.initTemplates();
this.UI.modal.initForms();
this.UI.initObjects();
if(!this.user) {
PydioApi.getClient().tryToLogUserFromRememberData();
}
this.fire("registry_loaded", this.Registry.getXML());
window.setTimeout(function(){
this.fire('loaded');
}.bind(this), 200);
this.Router = new Router(this);
}.bind(this));
}
}.bind(this);
if(this.Parameters.get("PRELOADED_REGISTRY")){
this.Registry.loadFromString(this.Parameters.get("PRELOADED_REGISTRY"));
this.Parameters.delete("PRELOADED_REGISTRY");
if(modal) modal.updateLoadingProgress('XML Registry loaded');
starterFunc();
}else{
this.loadXmlRegistry(false, null, starterFunc);
}
this.observe("server_message", function(xml){
var reload = XMLUtils.XPathSelectSingleNode(xml, "tree/require_registry_reload");
if(reload){
if(reload.getAttribute("repositoryId") != this.repositoryId){
this.loadXmlRegistry(false, null, null, reload.getAttribute("repositoryId"));
this.repositoryId = null;
}
}
}.bind(this));
}
/**
* Loads the XML Registry, an image of the application in its current state
* sent by the server.
* @param sync Boolean Whether to send synchronously or not.
* @param xPath String An XPath to load only a subpart of the registry
*/
loadXmlRegistry (sync, xPath=null, completeFunc=null, targetRepositoryId=null){
this.Registry.load(sync, xPath, completeFunc, (targetRepositoryId === null ? Math.random() : targetRepositoryId));
}
/**
* Get the XML Registry
* @returns Document
*/
getXmlRegistry (){
return this.Registry.getXML();
}
/**
* Find the current repository (from the current user) and load it.
*/
loadActiveRepository (){
var repositoryObject = new Repository(null);
if(this.user != null)
{
var repId = this.user.getActiveRepository();
var repList = this.user.getRepositoriesList();
repositoryObject = repList.get(repId);
if(!repositoryObject){
if(this.user.lock){
this.Controller.loadActionsFromRegistry(this.getXmlRegistry());
let lock = this.user.lock.split(",").shift();
window.setTimeout(function(){
this.Controller.fireAction(lock);
}.bind(this), 50);
return;
}
alert("No active repository found for user!");
}
if(this.user.getPreference("pending_folder") && this.user.getPreference("pending_folder") != "-1"){
this._initLoadRep = this.user.getPreference("pending_folder");
this.user.setPreference("pending_folder", "-1");
this.user.savePreference("pending_folder");
}else if(this.user.getPreference("ls_history", true)){
var data = this.user.getPreference("ls_history", true);
this._initLoadRep = data[repId];
}
}
this.loadRepository(repositoryObject);
if(repList && repId){
this.fire("repository_list_refreshed", {list:repList,active:repId});
}else{
this.fire("repository_list_refreshed", {list:false,active:false});
}
}
/**
* Refresh the repositories list for the current user
*/
reloadRepositoriesList (){
if(!this.user) return;
this.observeOnce("registry_part_loaded", function(data){
if(data != "user/repositories") return;
this.Registry.logXmlUser(true);
this.fire("repository_list_refreshed", {
list:this.user.getRepositoriesList(),
active:this.user.getActiveRepository()});
}.bind(this));
this.loadXmlRegistry(false, "user/repositories");
}
/**
* Load a Repository instance
* @param repository Repository
*/
loadRepository(repository){
if(this.repositoryId != null && this.repositoryId == repository.getId()){
Logger.debug('Repository already loaded, do nothing');
}
this._contextHolder.setSelectedNodes([]);
if(repository == null) return;
repository.loadResources();
var repositoryId = repository.getId();
var newIcon = repository.getIcon();
this.skipLsHistory = true;
var providerDef = repository.getNodeProviderDef();
var rootNode;
if(providerDef != null){
var provider = eval('new '+providerDef.name+'()');
if(providerDef.options){
provider.initProvider(providerDef.options);
}
this._contextHolder.setAjxpNodeProvider(provider);
rootNode = new AjxpNode("/", false, repository.getLabel(), newIcon, provider);
}else{
rootNode = new AjxpNode("/", false, repository.getLabel(), newIcon);
// Default
this._contextHolder.setAjxpNodeProvider(new RemoteNodeProvider());
}
this._contextHolder.setRootNode(rootNode);
rootNode.observeOnce('first_load', function(){
this._contextHolder.notify('context_changed', rootNode);
}.bind(this));
this.repositoryId = repositoryId;
if(this._initLoadRep){
if(this._initLoadRep != "" && this._initLoadRep != "/"){
var copy = this._initLoadRep.valueOf();
this._initLoadRep = null;
rootNode.observeOnce("first_load", function(){
setTimeout(function(){
this.goTo(copy);
this.skipLsHistory = false;
}.bind(this), 1000);
}.bind(this));
}else{
this.skipLsHistory = false;
}
}else{
this.skipLsHistory = false;
}
rootNode.load();
}
/**
* Require a context change to the given path
* @param nodeOrPath AjxpNode|String A node or a path
*/
goTo(nodeOrPath){
var path;
if(typeof(nodeOrPath) == "string"){
path = nodeOrPath;
}else{
path = nodeOrPath.getPath();
if(nodeOrPath.getMetadata().has("repository_id") && nodeOrPath.getMetadata().get("repository_id") != this.repositoryId
&& nodeOrPath.getAjxpMime() != "repository" && nodeOrPath.getAjxpMime() != "repository_editable"){
if(this.user){
this.user.setPreference("pending_folder", nodeOrPath.getPath());
this._initLoadRep = nodeOrPath.getPath();
}
this.triggerRepositoryChange(nodeOrPath.getMetadata().get("repository_id"));
return;
}
}
var current = this._contextHolder.getContextNode();
if(current && current.getPath() == path){
return;
}
var gotoNode;
if(path == "" || path == "/") {
gotoNode = new AjxpNode("/");
this._contextHolder.requireContextChange(gotoNode);
return;
}
this._contextHolder.loadPathInfoAsync(path, function(foundNode){
if(foundNode.isLeaf() && foundNode.getAjxpMime()!='ajxp_browsable_archive') {
this._contextHolder.setPendingSelection(PathUtils.getBasename(path));
gotoNode = new AjxpNode(PathUtils.getDirname(path));
}else{
gotoNode = foundNode;
}
this._contextHolder.requireContextChange(gotoNode);
}.bind(this));
}
/**
* Change the repository of the current user and reload list and current.
* @param repositoryId String Id of the new repository
*/
triggerRepositoryChange(repositoryId){
this.fire("trigger_repository_switch");
var onComplete = function(transport){
if(transport.responseXML){
this.ApiClient.parseXmlMessage(transport.responseXML);
}
this.loadXmlRegistry(false, null, null, repositoryId);
this.repositoryId = null;
}.bind(this);
var root = this._contextHolder.getRootNode();
if(root){
this.skipLsHistory = true;
root.clear();
}
this.ApiClient.switchRepository(repositoryId, onComplete);
}
getPluginConfigs (pluginQuery){
return this.Registry.getPluginConfigs(pluginQuery);
}
listLanguagesWithCallback(callback){
let langs = this.Parameters.get("availableLanguages") || {"en":"Default"};
let current = this.currentLanguage;
Object.keys(langs).sort().map(function(key){
callback(key, langs[key], (current === key));
});
}
/**
* Reload all messages from server and trigger updateI18nTags
* @param newLanguage String
*/
loadI18NMessages(newLanguage){
var onComplete = function(transport){
if(transport.responseJSON){
this.MessageHash = transport.responseJSON;
if(window && window.MessageHash) {
window.MessageHash = this.MessageHash;
}
for(var key in this.MessageHash){
if(this.MessageHash.hasOwnProperty(key)){
this.MessageHash[key] = this.MessageHash[key].replace("\\n", "\n");
}
}
this.UI.updateI18nTags();
this.Controller.refreshGuiActionsI18n();
this.loadXmlRegistry();
this.fireContextRefresh();
this.currentLanguage = newLanguage;
}
}.bind(this);
this.ApiClient.switchLanguage(newLanguage, onComplete);
}
/**
* Get the main controller
* @returns ActionManager
*/
getController(){
return this.Controller;
}
/**
* Display an information or error message to the user
* @param messageType String ERROR or SUCCESS
* @param message String the message
*/
displayMessage(messageType, message){
var urls = LangUtils.parseUrl(message);
if(urls.length && this.user && this.user.repositories){
urls.forEach(function(match){
var repo = this.user.repositories.get(match.host);
if(!repo) return;
message = message.replace(match.url, repo.label+":" + match.path + match.file);
}.bind(this));
}
if(messageType == 'ERROR') Logger.error(message);
else Logger.log(message);
if(this.UI.modal) this.UI.modal.displayMessage(messageType, message);
}
/*************************************************
*
* PROXY METHODS FOR DATAMODEL
*
************************************************/
/**
* Accessor for updating the datamodel context
* @param ajxpContextNode AjxpNode
* @param ajxpSelectedNodes AjxpNode[]
* @param selectionSource String
*/
updateContextData (ajxpContextNode, ajxpSelectedNodes, selectionSource){
if(ajxpContextNode){
this._contextHolder.requireContextChange(ajxpContextNode);
}
if(ajxpSelectedNodes){
this._contextHolder.setSelectedNodes(ajxpSelectedNodes, selectionSource);
}
}
/**
* @returns AjxpDataModel
*/
getContextHolder (){
return this._contextHolder;
}
/**
* @returns AjxpNode
*/
getContextNode (){
return this._contextHolder.getContextNode() || new AjxpNode("");
}
/**
* @returns AjxpDataModel
*/
getUserSelection (){
return this._contextHolder;
}
/**
* Accessor for datamodel.requireContextChange()
*/
fireContextRefresh (){
this.getContextHolder().requireContextChange(this.getContextNode(), true);
}
/**
* Accessor for datamodel.requireContextChange()
*/
fireNodeRefresh (nodePathOrNode, completeCallback){
this.getContextHolder().requireNodeReload(nodePathOrNode, completeCallback);
}
/**
* Accessor for datamodel.requireContextChange()
*/
fireContextUp (){
if(this.getContextNode().isRoot()) return;
this.updateContextData(this.getContextNode().getParent());
}
}