2012-03-05 14:07:38 +01:00
|
|
|
/**
|
|
|
|
* EGroupware clientside API object
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Andreas Stöckel (as AT stylite.de)
|
|
|
|
* @author Ralf Becker <RalfBecker@outdoor-training.de>
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*egw:uses
|
|
|
|
egw_core;
|
2012-03-09 16:32:29 +01:00
|
|
|
egw_ready;
|
2012-03-05 14:07:38 +01:00
|
|
|
egw_debug;
|
|
|
|
*/
|
2021-06-05 20:39:39 +02:00
|
|
|
import './egw_core.js';
|
2012-03-05 14:07:38 +01:00
|
|
|
|
2013-10-08 10:55:15 +02:00
|
|
|
/**
|
|
|
|
* @augments Class
|
2016-02-29 16:50:24 +01:00
|
|
|
* @param {string} _app application name object is instanciated for
|
|
|
|
* @param {object} _wnd window object is instanciated for
|
2013-10-08 10:55:15 +02:00
|
|
|
*/
|
2014-01-11 13:10:31 +01:00
|
|
|
egw.extend('files', egw.MODULE_WND_LOCAL, function(_app, _wnd)
|
2013-10-08 10:55:15 +02:00
|
|
|
{
|
2016-02-29 16:50:24 +01:00
|
|
|
"use strict";
|
|
|
|
|
2012-03-09 16:32:29 +01:00
|
|
|
var egw = this;
|
2012-03-05 14:07:38 +01:00
|
|
|
|
2013-02-13 17:26:42 +01:00
|
|
|
/**
|
2016-03-03 10:23:45 +01:00
|
|
|
* Remove optional timestamp attached as query parameter, eg. /path/name.js?12345678[&other=val]
|
2014-01-11 13:10:31 +01:00
|
|
|
*
|
2013-02-13 17:26:42 +01:00
|
|
|
* Examples:
|
|
|
|
* /path/file.js --> /path/file.js
|
|
|
|
* /path/file.js?123456 --> /path/file.js
|
|
|
|
* /path/file.php?123456¶m=value --> /path/file.php?param=value
|
2016-03-03 10:23:45 +01:00
|
|
|
* /path/file.php?param=value&123456 --> /path/file.php?param=value
|
2013-02-13 17:26:42 +01:00
|
|
|
*
|
|
|
|
* @param _src url
|
|
|
|
* @return url with timestamp stripped off
|
|
|
|
*/
|
|
|
|
function removeTS(_src)
|
|
|
|
{
|
2016-03-03 10:23:45 +01:00
|
|
|
return _src.replace(/[?&][0-9]+&?/, '?').replace(/\?$/, '');
|
2013-02-13 17:26:42 +01:00
|
|
|
}
|
2012-03-05 14:07:38 +01:00
|
|
|
|
2014-01-11 13:10:31 +01:00
|
|
|
/**
|
|
|
|
* RegExp to extract string with comma-separated files from a bundle-url
|
|
|
|
*
|
|
|
|
* @type RegExp
|
|
|
|
*/
|
|
|
|
var bundle2files_regexp = /phpgwapi\/inc\/min\/\?b=[^&]+&f=([^&]+)/;
|
|
|
|
|
2016-03-01 21:45:31 +01:00
|
|
|
/**
|
|
|
|
* Regexp to detect and remove .min.js extension
|
|
|
|
*
|
|
|
|
* @type RegExp
|
|
|
|
*/
|
|
|
|
var min_js_regexp = /\.min\.js$/;
|
|
|
|
|
2014-01-11 13:10:31 +01:00
|
|
|
/**
|
|
|
|
* Return array of files-sources from bundle(s) incl. bundle-src itself
|
|
|
|
*
|
|
|
|
* @param {string|Array} _srcs all url's have to be egw releativ!
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
|
|
|
function files_from_bundles(_srcs)
|
|
|
|
{
|
|
|
|
var files = [];
|
|
|
|
|
|
|
|
if (typeof _srcs == 'string') _srcs = [_srcs];
|
|
|
|
|
|
|
|
for(var n=0; n < _srcs.length; ++n)
|
|
|
|
{
|
|
|
|
var file = _srcs[n];
|
2016-03-01 21:45:31 +01:00
|
|
|
files.push(file.replace(min_js_regexp, '.js'));
|
2014-01-11 13:10:31 +01:00
|
|
|
var contains = file.match(bundle2files_regexp);
|
|
|
|
|
|
|
|
if (contains && contains.length > 1)
|
|
|
|
{
|
2016-03-01 21:45:31 +01:00
|
|
|
var bundle = contains[1].split(',');
|
2016-03-03 10:23:45 +01:00
|
|
|
for(var i=0; i < bundle.length; ++i)
|
2016-03-01 21:45:31 +01:00
|
|
|
{
|
|
|
|
files.push(bundle[i].replace(min_js_regexp, '.js'));
|
|
|
|
}
|
2014-01-11 13:10:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Strip of egw_url from given urls (if containing it)
|
|
|
|
*
|
|
|
|
* @param {array} _urls absolute urls
|
|
|
|
* @returns {array} relativ urls
|
|
|
|
*/
|
|
|
|
function strip_egw_url(_urls)
|
|
|
|
{
|
|
|
|
var egw_url = egw.webserverUrl;
|
|
|
|
if (egw_url.charAt(egw_url.length-1) != '/') egw_url += '/';
|
|
|
|
|
|
|
|
for(var i=0; i < _urls.length; ++i)
|
|
|
|
{
|
|
|
|
var file = _urls[i];
|
|
|
|
// check if egw_url is only path and urls contains full url incl. protocol
|
|
|
|
// --> prefix it with our protocol and host, as eg. splitting by just '/' will fail!
|
2015-03-17 15:47:24 +01:00
|
|
|
var need_full_url = egw_url[0] == '/' && file.substr(0,4) == 'http' ? window.location.protocol+'//'+window.location.host : '';
|
|
|
|
var parts = file.split(need_full_url+egw_url);
|
|
|
|
if (parts.length > 1)
|
|
|
|
{
|
|
|
|
// discard protocol and host
|
|
|
|
parts.shift();
|
|
|
|
_urls[i] = parts.join(need_full_url+egw_url);
|
|
|
|
}
|
2014-01-11 13:10:31 +01:00
|
|
|
}
|
|
|
|
return _urls;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array which contains all currently bound in javascript and css files.
|
|
|
|
*/
|
|
|
|
var files = [];
|
|
|
|
// add already included scripts
|
2014-03-13 16:28:42 +01:00
|
|
|
var tags = jQuery('script', _wnd.document);
|
2014-01-11 13:10:31 +01:00
|
|
|
for(var i=0; i < tags.length; ++i)
|
|
|
|
{
|
|
|
|
files.push(removeTS(tags[i].src));
|
|
|
|
}
|
|
|
|
// add already included css
|
2014-03-13 16:28:42 +01:00
|
|
|
tags = jQuery('link[type="text/css"]', _wnd.document);
|
2014-01-11 13:10:31 +01:00
|
|
|
for(var i=0; i < tags.length; ++i)
|
|
|
|
{
|
|
|
|
files.push(removeTS(tags[i].href));
|
|
|
|
}
|
|
|
|
// make urls egw-relative
|
|
|
|
files = strip_egw_url(files);
|
2016-03-01 21:45:31 +01:00
|
|
|
// resolve bundles and replace .min.js with .js
|
2014-01-11 13:10:31 +01:00
|
|
|
files = files_from_bundles(files);
|
|
|
|
|
2012-03-05 14:07:38 +01:00
|
|
|
return {
|
2013-10-08 10:55:15 +02:00
|
|
|
/**
|
|
|
|
* Load and execute javascript file(s) in order
|
2014-01-11 13:10:31 +01:00
|
|
|
*
|
2013-10-08 10:55:15 +02:00
|
|
|
* @memberOf egw
|
2016-02-29 16:50:24 +01:00
|
|
|
* @param {string|array} _jsFiles (array of) urls to include
|
|
|
|
* @param {function} _callback called after JS files are loaded and executed
|
|
|
|
* @param {object} _context
|
|
|
|
* @param {string} _prefix prefix for _jsFiles
|
2021-06-05 20:39:39 +02:00
|
|
|
* @return Promise
|
2013-10-08 10:55:15 +02:00
|
|
|
*/
|
2014-01-11 13:10:31 +01:00
|
|
|
includeJS: function(_jsFiles, _callback, _context, _prefix)
|
|
|
|
{
|
|
|
|
// Also allow including a single javascript file
|
|
|
|
if (typeof _jsFiles === 'string')
|
2013-02-13 17:26:42 +01:00
|
|
|
{
|
2014-01-11 13:10:31 +01:00
|
|
|
_jsFiles = [_jsFiles];
|
2013-02-13 17:26:42 +01:00
|
|
|
}
|
2021-06-10 11:38:54 +02:00
|
|
|
// filter out files included by script-tag via egw.js
|
|
|
|
_jsFiles = _jsFiles.filter((src) => src.match(egw.legacy_js_regexp) === null);
|
2021-06-08 17:13:30 +02:00
|
|
|
let promise;
|
|
|
|
if (_jsFiles.length === 1) // running this in below case fails when loading app.js from etemplate.load()
|
2012-03-05 14:07:38 +01:00
|
|
|
{
|
2021-06-08 17:13:30 +02:00
|
|
|
const src = _jsFiles[0];
|
2021-06-09 11:11:34 +02:00
|
|
|
promise = import(_prefix ? _prefix+src : src)
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(src+":\n\n"+err.message);
|
|
|
|
});
|
2014-01-11 13:10:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-08 17:13:30 +02:00
|
|
|
promise = Promise.all(_jsFiles.map((src) => {
|
|
|
|
import(_prefix ? _prefix+src : src)
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(src+":\n\n"+err.message);
|
|
|
|
})
|
|
|
|
}));
|
2014-01-11 13:10:31 +01:00
|
|
|
}
|
2021-06-08 17:13:30 +02:00
|
|
|
return typeof _callback === 'undefined' ? promise : promise.then(_callback.call(_context));
|
2012-03-05 14:07:38 +01:00
|
|
|
},
|
|
|
|
|
2014-01-11 13:10:31 +01:00
|
|
|
/**
|
|
|
|
* Check if file is already included and optional mark it as included if not yet included
|
|
|
|
*
|
2016-03-01 21:45:31 +01:00
|
|
|
* Check does NOT differenciate between file.min.js and file.js.
|
|
|
|
* Only .js get's recored in files for further checking, if _add_if_not set.
|
|
|
|
*
|
2014-01-11 13:10:31 +01:00
|
|
|
* @param {string} _file
|
|
|
|
* @param {boolean} _add_if_not if true mark file as included
|
|
|
|
* @return boolean true if file already included, false if not
|
|
|
|
*/
|
|
|
|
included: function(_file, _add_if_not)
|
|
|
|
{
|
2016-03-01 21:45:31 +01:00
|
|
|
var file = removeTS(_file).replace(min_js_regexp, '.js');
|
2014-01-11 13:10:31 +01:00
|
|
|
var not_inc = files.indexOf(file) == -1;
|
|
|
|
|
|
|
|
if (not_inc && _add_if_not)
|
|
|
|
{
|
|
|
|
files = files.concat(files_from_bundles(file));
|
|
|
|
}
|
|
|
|
return !not_inc;
|
|
|
|
},
|
|
|
|
|
2013-10-08 10:55:15 +02:00
|
|
|
/**
|
|
|
|
* Include a CSS file
|
2014-01-11 13:10:31 +01:00
|
|
|
*
|
2014-01-15 16:48:22 +01:00
|
|
|
* @param {string|array} _cssFiles full url of file to include
|
2013-10-08 10:55:15 +02:00
|
|
|
*/
|
2014-01-15 16:48:22 +01:00
|
|
|
includeCSS: function(_cssFiles)
|
2014-01-11 13:10:31 +01:00
|
|
|
{
|
2014-01-15 16:48:22 +01:00
|
|
|
if (typeof _cssFiles == 'string') _cssFiles = [_cssFiles];
|
|
|
|
_cssFiles = strip_egw_url(_cssFiles);
|
|
|
|
|
|
|
|
for(var n=0; n < _cssFiles.length; ++n)
|
2012-03-05 14:07:38 +01:00
|
|
|
{
|
2014-01-15 16:48:22 +01:00
|
|
|
var file = _cssFiles[n];
|
|
|
|
if (!this.included(file, true)) // check if included and marks as such if not
|
2013-11-20 00:22:33 +01:00
|
|
|
{
|
2014-01-15 16:48:22 +01:00
|
|
|
// Create the node which is used to include the css file
|
|
|
|
var cssnode = _wnd.document.createElement('link');
|
|
|
|
cssnode.type = "text/css";
|
|
|
|
cssnode.rel = "stylesheet";
|
|
|
|
cssnode.href = egw.webserverUrl+'/'+file;
|
|
|
|
|
|
|
|
// Get the head node and append the newly created "link" nod to it
|
|
|
|
var head = _wnd.document.getElementsByTagName('head')[0];
|
2013-11-20 00:22:33 +01:00
|
|
|
head.appendChild(cssnode);
|
|
|
|
}
|
2012-03-05 14:07:38 +01:00
|
|
|
}
|
|
|
|
}
|
2014-01-11 13:10:31 +01:00
|
|
|
};
|
2012-03-05 14:07:38 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|