/* * Copyright 2007-2017 Charles du Jeu - Abstrium SAS * 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 . * * The latest code can be found at . */ import XMLUtils from '../util/XMLUtils' const SystemJS = require('systemjs'); /** * A manager that can handle the loading of JS, CSS and checks dependencies */ class ResourcesManager{ /** * Constructor */ constructor(){ this.mainFormContainerId = 'all_forms'; this.resources = {}; this.loaded = false; } /** * Adds a Javascript resource * @param fileName String * @param className String */ addJSResource(fileName, className){ if(!this.resources.js){ this.resources.js = []; } this.resources.js.push({ fileName:fileName, className:className, autoload:false }); } /** * Adds a CSS resource * @param fileName String */ addCSSResource(fileName){ if(!this.resources.css){ this.resources.css = []; } this.resources.css.push(fileName); } /** * Adds a FORM from html snipper * @param formId String * @param htmlSnippet String */ addGuiForm(formId, htmlSnippet){ if(!this.resources.forms){ this.resources.forms = new Map(); } this.resources.forms.set(formId,htmlSnippet); } /** * Add a dependency to another plugin * @param data Object */ addDependency(data){ if(!this.resources.dependencies){ this.resources.dependencies = []; } this.resources.dependencies.push(data); } /** * Check if some dependencies must be loaded before * @returns Boolean */ hasDependencies(){ return (this.resources.dependencies || false); } /** * Load resources * @param resourcesRegistry Pydio resources registry */ load(resourcesRegistry, jsAutoloadOnly=false, callback = FuncUtils.Empty){ if(this.loaded) { callback(); return; } if(this.hasDependencies() && !this.dependenciesLoaded){ this.resources.dependencies.forEach(function(el){ if(resourcesRegistry[el]){ // Load dependencies and try again resourcesRegistry[el].load(resourcesRegistry, false, function(){ this.dependenciesLoaded = true; this.load(resourcesRegistry, false, callback); }.bind(this)); } }.bind(this) ); } if(this.resources.forms){ this.resources.forms.forEach(function(value,key){ // REMOVED //this.loadGuiForm(key, value); }.bind(this) ); } if(this.resources.js){ let it = this.resources.js.values(); let cb = function(){ let object = it.next(); if(object.value){ if(jsAutoloadOnly && !object.value.autoload) { cb(); return; } this.loadJSResource(object.value.fileName, object.value.className, cb, true); }else{ this.loaded = true; callback(); } }.bind(this); cb(); }else{ this.loaded = true; callback(); } if(this.resources.css){ this.resources.css.forEach(function(value){ this.loadCSSResource(value); }.bind(this)); } } /** * Load a javascript file * @param fileName String * @param className String * @param callback Function * @param aSync Boolean */ loadJSResource(fileName, className, callback, aSync = true){ if(!ResourcesManager.__configsParsed) { ResourcesManager.loadAutoLoadResources(); } SystemJS.import(className).then(callback); } /** * Load a CSS file * @param fileName String */ loadCSSResource(fileName){ if(pydio.Parameters.get('SERVER_PREFIX_URI')){ fileName = pydio.Parameters.get('SERVER_PREFIX_URI')+fileName; } fileName = fileName+"?v="+pydio.Parameters.get("ajxpVersion"); let found = false; const links = document.getElementsByTagName('link'); for(let i=0; i {return SystemJS.import(c)})).then(() => { callbackFunc(); }).catch((reason) => { console.error('Failed Loading ' + classNames.join(', ') + ' : ', reason); }); return; } static detectModuleToLoadAndApply(callbackString, callbackFunc, async = true){ if(!ResourcesManager.__configsParsed){ ResourcesManager.loadAutoLoadResources(); } const className = callbackString.split('.',1).shift(); if(async){ SystemJS.import(className).then(callbackFunc); }else{ ResourcesManager.loadScriptSync(className, callbackFunc); } return; } static async loadScriptSync(name, callback){ await SystemJS.import(name); callback(); } } ResourcesManager.__configsParsed = false; ResourcesManager.__requires = {}; export {ResourcesManager as default}