From e572e5f68e4ab6bcff1fb04747a7febb38cd61e7 Mon Sep 17 00:00:00 2001 From: nao-pon Date: Mon, 5 Dec 2016 17:49:37 +0900 Subject: [PATCH] [js:core] fix #1791 implement JS, CSS loader and auto load of elFinder CSS --- elfinder.html | 8 --- js/elFinder.js | 132 ++++++++++++++++++++++++++++++++--------- js/elFinder.options.js | 10 ++++ main.default.js | 13 +++- 4 files changed, 124 insertions(+), 39 deletions(-) diff --git a/elfinder.html b/elfinder.html index 350dad305..6ed40662b 100644 --- a/elfinder.html +++ b/elfinder.html @@ -5,14 +5,6 @@ elFinder 2.1.x source version with PHP connector - - - - - - - - diff --git a/js/elFinder.js b/js/elFinder.js index 5893c580d..bdaaed1cb 100644 --- a/js/elFinder.js +++ b/js/elFinder.js @@ -7,6 +7,35 @@ var elFinder = function(node, opts) { //this.time('load'); + // auto load required CSS + if (opts.cssAutoLoad) { + (function(fm) { + var myTag = $('head > script[src$="js/elfinder.min.js"],script[src$="js/elfinder.full.js"]:first'), + baseUrl, hide, fi; + if (myTag.length) { + // hide elFinder node while css loading + hide = $(''); + + $('head').append(hide); + baseUrl = myTag.attr('src').replace(/js\/[^\/]+$/, ''); + fm.loadCss([baseUrl+'css/elfinder.min.css', baseUrl+'css/theme.css']); + + // additional CSS files + if ($.isArray(opts.cssAutoLoad)) { + fm.loadCss(opts.cssAutoLoad); + } + + // check css loaded and remove hide + fi = setInterval(function() { + if ($(node).css('display') !== 'none') { + clearInterval(fi); + hide.remove(); + } + }, 10); + } + })(this); + } + var self = this, /** @@ -1267,34 +1296,6 @@ var elFinder = function(node, opts) { return this.options.url + (this.options.url.indexOf('?') === -1 ? '?' : '&') + $.param(params, true); }; - /** - * Convert from relative URL to abstract URL based on current URL - * - * @param String URL - * @return String - */ - this.convAbsUrl = function(url) { - if (url.match(/^http/i)) { - return url; - } - if (url.substr(0,2) === '//') { - return window.location.protocol + url; - } - var root = window.location.protocol + '//' + window.location.host, - reg = /[^\/]+\/\.\.\//, - ret; - if (url.substr(0, 1) === '/') { - ret = root + url; - } else { - ret = root + window.location.pathname.replace(/\/[^\/]+$/, '/') + url; - } - ret = ret.replace('/./', '/'); - while(reg.test(ret)) { - ret = ret.replace(reg, ''); - } - return ret; - }; - /** * Return file url for open in elFinder * @@ -6440,6 +6441,34 @@ elFinder.prototype = { return newItem; }, + /** + * Convert from relative URL to abstract URL based on current URL + * + * @param String URL + * @return String + */ + convAbsUrl : function(url) { + if (url.match(/^http/i)) { + return url; + } + if (url.substr(0,2) === '//') { + return window.location.protocol + url; + } + var root = window.location.protocol + '//' + window.location.host, + reg = /[^\/]+\/\.\.\//, + ret; + if (url.substr(0, 1) === '/') { + ret = root + url; + } else { + ret = root + window.location.pathname.replace(/\/[^\/]+$/, '/') + url; + } + ret = ret.replace('/./', '/'); + while(reg.test(ret)) { + ret = ret.replace(reg, ''); + } + return ret; + }, + navHash2Id : function(hash) { return this.navPrefix + hash; }, @@ -6468,6 +6497,53 @@ elFinder.prototype = { return document.elementFromPoint(rect.left, rect.top)? true : false; }, + /** + * Load JavaScript files + * + * @param Array to load JavaScript file URLs + * @param Function call back function on script loaded + * @param Object Additional options to $.ajax + * @return elFinder + */ + loadScript : function(urls, callback, opts) { + var defOpts = { + dataType : 'script', + cache : true + }, + success = null; + if ($.isFunction(callback)) { + success = function() { callback.apply(callback.caller || window); }; + } + opts = $.isPlainObject(opts)? $.extend(defOpts, opts) : defOpts; + (function appendScript() { + $.ajax($.extend(opts, { + url: urls.shift(), + success: urls.length? appendScript : success + })); + })(); + return this; + }, + + /** + * Load CSS files + * + * @param Array to load CSS file URLs + * @return elFinder + */ + loadCss : function(urls) { + var self = this; + if (typeof urls === 'string') { + urls = [ urls ]; + } + $.each(urls, function(i, url) { + url = self.convAbsUrl(url).replace(/^https?:/i, ''); + if (! $("head > link[href='+url+']").length) { + $('head').append(''); + } + }); + return this; + }, + log : function(m) { window.console && window.console.log && window.console.log(m); return this; }, debug : function(type, m) { diff --git a/js/elFinder.options.js b/js/elFinder.options.js index a6836a37a..2c397fb2b 100644 --- a/js/elFinder.options.js +++ b/js/elFinder.options.js @@ -127,6 +127,16 @@ elFinder.prototype._options = { */ lang : 'en', + /** + * Auto load required CSS + * `false` to disable this function or + * CSS URL Array to load additional CSS files + * + * @type Boolean|Array + * @default true + */ + cssAutoLoad : true, + /** * Additional css class for filemanager node. * diff --git a/main.default.js b/main.default.js index df1f69458..1ff2e7dde 100644 --- a/main.default.js +++ b/main.default.js @@ -6,7 +6,11 @@ * e.g. `` **/ (function(){ - var // Detect language (optional) + var // jQuery and jQueryUI version + jqver = '3.1.1', + uiver = '1.12.1', + + // Detect language (optional) lang = (function() { var locq = window.location.search, fullLang, locm, lang; @@ -35,6 +39,9 @@ // Start elFinder (REQUIRED) start = function(elFinder) { + // load jQueryUI CSS + elFinder.prototype.loadCss('//cdnjs.cloudflare.com/ajax/libs/jqueryui/'+uiver+'/themes/smoothness/jquery-ui.css'); + $(function() { // Optional for Japanese decoder "extras/encoding-japanese.min" if (window.Encoding && Encoding.convert) { @@ -70,8 +77,8 @@ require.config({ baseUrl : 'js', paths : { - 'jquery' : '//cdnjs.cloudflare.com/ajax/libs/jquery/'+(ie8? '1.12.4' : '3.1.1')+'/jquery.min', - 'jquery-ui': '//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min', + 'jquery' : '//cdnjs.cloudflare.com/ajax/libs/jquery/'+(ie8? '1.12.4' : jqver)+'/jquery.min', + 'jquery-ui': '//cdnjs.cloudflare.com/ajax/libs/jqueryui/'+uiver+'/jquery-ui.min', 'elfinder' : 'elfinder.min', 'elfinder.lang': [ 'i18n/elfinder.'+lang,