mirror of
https://bitbucket.org/bellord13/kit.git
synced 2026-11-13 19:06:11 +03:00
v2
This commit is contained in:
+820
-615
File diff suppressed because it is too large
Load Diff
Vendored
+1
File diff suppressed because one or more lines are too long
@@ -0,0 +1,425 @@
|
||||
var __PluginManagerConfigHandler__ = function() {
|
||||
var root = __pmConfig__.assetsPath;
|
||||
return {
|
||||
menu: {
|
||||
path: root + '/menu',
|
||||
module: '/menu.js',
|
||||
css: [
|
||||
'/menu.css'
|
||||
],
|
||||
type: 'HMD',
|
||||
deps: ['lib', 'align', 'dataStructures', 'animations', 'userAction', 'Collection', 'support', 'ActionCollection', 'Action', 'Viewer']
|
||||
},
|
||||
lib: {
|
||||
path: root + '/lib',
|
||||
module: '/lib.js',
|
||||
type: 'HMD',
|
||||
},
|
||||
align: {
|
||||
path: root + '/align',
|
||||
module: '/align.js',
|
||||
type: 'HMD',
|
||||
deps: ['support']
|
||||
},
|
||||
flexKit: {
|
||||
path: root + '/flex-kit',
|
||||
css: '/flex-kit.css'
|
||||
},
|
||||
support: {
|
||||
path: root + '/support',
|
||||
module: '/support.js',
|
||||
type: 'HMD',
|
||||
},
|
||||
jqueryIniter: {
|
||||
type: 'HMD',
|
||||
handler: function(require, exports, module, global){
|
||||
var J = function (){
|
||||
var c = [];
|
||||
J.queue.push(c);
|
||||
c.push(['$', arguments]);
|
||||
var J2 = function(){}
|
||||
J2.ready = function (){
|
||||
c.push(['ready', arguments]);
|
||||
}
|
||||
return J2
|
||||
}
|
||||
J.queue = [];
|
||||
|
||||
function JQueryIniter(){}
|
||||
JQueryIniter.prototype.J = J;
|
||||
JQueryIniter.prototype.hasJQuery = function(){
|
||||
return window.hasOwnProperty('jQuery') && window.jQuery.hasOwnProperty('fn');
|
||||
};
|
||||
JQueryIniter.prototype.hasJQueryIniter = function(){
|
||||
return window.hasOwnProperty('jQuery') && Object.is(window.jQuery, J);
|
||||
};
|
||||
JQueryIniter.prototype.onRegister = function(plugin){
|
||||
if(!this.hasJQuery()){
|
||||
(function(w){
|
||||
w.$ = w.jQuery = J;
|
||||
plugin.module.initer = J;
|
||||
})(window);
|
||||
}
|
||||
};
|
||||
JQueryIniter.prototype.onInstall = function(plugin){
|
||||
var self = this;
|
||||
(function(w){
|
||||
var q = plugin.module.initer.queue;
|
||||
if(self.hasJQueryIniter()){
|
||||
w.$ = w.jQuery = plugin.module.exports;
|
||||
for (var ch of q){
|
||||
var root = w;
|
||||
for (var c of ch){
|
||||
root = root[c[0]].apply(c[0], c[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
})(window);
|
||||
};
|
||||
|
||||
module.exports = new JQueryIniter();
|
||||
|
||||
},
|
||||
},
|
||||
jquery: {
|
||||
path: root + '/jquery',
|
||||
module: '/jquery.min.js',
|
||||
type: 'UMD',
|
||||
alias: ['$', 'jQuery'],
|
||||
onRegister: function (plugin) {
|
||||
console.log('reg jq');
|
||||
var initer = kit.require('jqueryIniter');
|
||||
initer.onRegister(plugin);
|
||||
},
|
||||
onInstall: function(plugin){
|
||||
var initer = kit.require('jqueryIniter');
|
||||
initer.onInstall(plugin);
|
||||
},
|
||||
},
|
||||
popper: {
|
||||
path: root + '/popper',
|
||||
alias: 'popper.js',
|
||||
module: '/popper.js',
|
||||
type: 'HMD',
|
||||
css: '/popper.css',
|
||||
},
|
||||
bsCore: {
|
||||
path: root + '/bsCore',
|
||||
module: '/bsCore.js',
|
||||
type: 'HMD',
|
||||
css: [
|
||||
'/bsCore.css'
|
||||
]
|
||||
},
|
||||
bsToast: {
|
||||
path: root + '/bsToast',
|
||||
module: '/bsToast.js',
|
||||
type: 'HMD',
|
||||
deps: ['bsCore'],
|
||||
},
|
||||
bsToastService: {
|
||||
path: root + '/bsToastService',
|
||||
module: '/bsToastService.js',
|
||||
type: 'HMD',
|
||||
css: [
|
||||
'/bsToastService.css'
|
||||
],
|
||||
deps: ['bsToast', 'support', 'Collection'],
|
||||
},
|
||||
Tooltip: {
|
||||
path: root + '/tooltip',
|
||||
module: '/tooltip.js',
|
||||
type: 'HMD',
|
||||
css: '/tooltip.css',
|
||||
onInstall: function (plugin) {
|
||||
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
|
||||
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
|
||||
return __pmGlobal__.Tooltip.getOrCreateInstance(tooltipTriggerEl, {sanitize: false});
|
||||
});
|
||||
},
|
||||
deps: ['popper', 'bsCore'],
|
||||
},
|
||||
Tooltip2: {
|
||||
path: root + '/tooltip2',
|
||||
module: '/tooltip2.js',
|
||||
type: 'HMD',
|
||||
css: '/tooltip2.css',
|
||||
onInstall: function (plugin) {
|
||||
console.log('tt2 install');
|
||||
document.addEventListener('mouseover', function (event) {
|
||||
// console.log(event);
|
||||
// console.log(event.target);
|
||||
if (!(event.target && event.target.dataset.kitTooltip2 === 'autoload')) {
|
||||
return;
|
||||
}
|
||||
// console.log(1);
|
||||
if (kit.support.getNodeData(event.target, 'tooltip2')) {
|
||||
return;
|
||||
}
|
||||
// console.log(2);
|
||||
var tooltip = new __pmGlobal__.Tooltip2(event.target);
|
||||
tooltip.run();
|
||||
tooltip.show();
|
||||
});
|
||||
// var elements = [].slice.call(document.querySelectorAll('[data-kit-tooltip2="autoload"]'));
|
||||
// elements.map(function (element) {
|
||||
// var tooltip = new __pmGlobal__.Tooltip2(element);
|
||||
// tooltip.run();
|
||||
// console.log(tooltip);
|
||||
// });
|
||||
},
|
||||
deps: ['Viewer', 'support', 'ActionCollection', 'Action', 'userAction', 'align', 'Collection', 'animations'],
|
||||
},
|
||||
Popover: {
|
||||
path: root + '/popover',
|
||||
module: '/popover.js',
|
||||
type: 'HMD',
|
||||
css: '/popover.css',
|
||||
onInstall: function () {
|
||||
var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]'));
|
||||
var popoverList = popoverTriggerList.map(function (popoverTriggerEl) {
|
||||
return new __pmGlobal__.Popover(popoverTriggerEl);
|
||||
});
|
||||
},
|
||||
deps: ['popper', 'bsCore', 'Tooltip'],
|
||||
},
|
||||
mask: {
|
||||
path: root + '/mask',
|
||||
module: '/mask.js',
|
||||
type: 'HMD',
|
||||
deps: ['jQuery'],
|
||||
},
|
||||
toolkit: {
|
||||
path: root + '/toolkit',
|
||||
module: '/toolkit.js',
|
||||
type: 'HMD',
|
||||
css: ['/toolkit.css'],
|
||||
deps: ['jQuery', 'mask', 'support'],
|
||||
},
|
||||
Modal: {
|
||||
path: root + '/modal',
|
||||
module: '/modal.js',
|
||||
type: 'HMD',
|
||||
css: [
|
||||
'/modal.css',
|
||||
],
|
||||
deps: ['bsCore'],
|
||||
onRegister: function (plugin) {
|
||||
plugin.onLoadConditionHandler = function (event) {
|
||||
if (event.target && event.target.dataset && event.target.dataset.bsTarget && event.target.dataset.bsToggle === 'modal') {
|
||||
kit.exec(plugin.id, function () {
|
||||
var newEvent = new Event("click");
|
||||
event.target.dispatchEvent(newEvent);
|
||||
});
|
||||
}
|
||||
};
|
||||
window.addEventListener('click', plugin.onLoadConditionHandler, false);
|
||||
},
|
||||
onInstall: function (plugin) {
|
||||
if (plugin.onLoadConditionHandler) {
|
||||
window.removeEventListener('click', plugin.onLoadConditionHandler, false);
|
||||
}
|
||||
},
|
||||
},
|
||||
collapse: {
|
||||
path: root + '/collapse',
|
||||
module: '/collapse.js',
|
||||
type: 'HMD',
|
||||
css: [
|
||||
'/collapse.css',
|
||||
],
|
||||
deps: ['bsCore'],
|
||||
},
|
||||
datepicker: {
|
||||
path: root + '/datepicker',
|
||||
module: '/js/datepicker.js',
|
||||
type: 'HMD',
|
||||
css: [
|
||||
'/css/datepicker.css',
|
||||
],
|
||||
deps: ['jQuery'],
|
||||
},
|
||||
cookie: {
|
||||
path: root + '/cookie',
|
||||
module: '/cookie.js',
|
||||
type: 'HMD',
|
||||
},
|
||||
animations: {
|
||||
path: root + '/animations',
|
||||
css: '/animations.css',
|
||||
module: '/animations.js',
|
||||
type: 'HMD',
|
||||
},
|
||||
Collection: {
|
||||
path: root + '/collection',
|
||||
module: '/collection.js',
|
||||
type: 'HMD',
|
||||
deps: ['support'],
|
||||
},
|
||||
dataStructures: {
|
||||
path: root + '/dataStructures',
|
||||
module: '/dataStructures.js',
|
||||
type: 'HMD',
|
||||
},
|
||||
userAction: {
|
||||
path: root + '/userAction',
|
||||
module: '/userAction.js',
|
||||
type: 'HMD',
|
||||
deps: ['support', 'ActionCollection', 'Action', 'Viewer'],
|
||||
},
|
||||
Viewer: {
|
||||
path: root + '/Viewer',
|
||||
module: '/Viewer.js',
|
||||
type: 'HMD',
|
||||
deps: ['Collection', 'support', 'ActionCollection', 'Action'],
|
||||
},
|
||||
ActionCollection: {
|
||||
path: root + '/ActionCollection',
|
||||
module: '/ActionCollection.js',
|
||||
type: 'HMD',
|
||||
deps: ['Collection', 'support', 'Action'],
|
||||
},
|
||||
Action: {
|
||||
path: root + '/Action',
|
||||
module: '/Action.js',
|
||||
type: 'HMD',
|
||||
deps: ['support'],
|
||||
},
|
||||
date: {
|
||||
path: root + '/date',
|
||||
module: '/date.js',
|
||||
type: 'HMD',
|
||||
deps: [],
|
||||
},
|
||||
testing: {
|
||||
path: root + '/testing',
|
||||
module: '/testing.js',
|
||||
type: 'module',
|
||||
deps: ['log'],
|
||||
},
|
||||
log: {
|
||||
path: root + '/log',
|
||||
module: '/log.js',
|
||||
css: '/log.css',
|
||||
type: 'HMD',
|
||||
deps: ['support', 'Collection', 'lib']
|
||||
},
|
||||
fancybox: {
|
||||
path: root + '/fancybox',
|
||||
css: '/fancybox.css',
|
||||
js: '/fancybox.umd.js',
|
||||
},
|
||||
lightGallery: {
|
||||
path: root + '/lightGallery',
|
||||
css: '/lightGallery.css',
|
||||
js: '/lightGallery.js',
|
||||
deps: ['jQuery'],
|
||||
},
|
||||
owlCarousel: {
|
||||
path: root + '/owlCarousel',
|
||||
css: '/owlCarousel.css',
|
||||
js: '/owlCarousel.js',
|
||||
deps: ['jQuery'],
|
||||
},
|
||||
Chosen: {
|
||||
path: root + '/chosen',
|
||||
css: '/chosen.css',
|
||||
js: '/chosen.jquery.js',
|
||||
deps: ['jQuery'],
|
||||
},
|
||||
makeCSSAnimations: {
|
||||
path: root + '/makeCSSAnimations',
|
||||
js: '/makeCSSAnimations.js',
|
||||
deps: ['jQuery', 'support'],
|
||||
},
|
||||
mCustomScrollbar: {
|
||||
path: root + '/mCustomScrollbar',
|
||||
alias: 'malihu-custom-scrollbar-plugin',
|
||||
js: '/mCustomScrollbar.js',
|
||||
css: '/jquery.mCustomScrollbar.css',
|
||||
deps: ['jQuery', 'jqueryMousewheel'],
|
||||
onInstall: function (plugin) {
|
||||
plugin.module.exports(kit.require('jQuery'));
|
||||
}
|
||||
},
|
||||
jqueryMousewheel: {
|
||||
path: root + '/jqueryMousewheel',
|
||||
alias: 'jquery-mousewheel',
|
||||
js: '/jquery.mousewheel.js',
|
||||
deps: ['jQuery'],
|
||||
onInstall: function (plugin) {
|
||||
plugin.module.exports(kit.require('jQuery'));
|
||||
}
|
||||
},
|
||||
jqueryLazy: {
|
||||
path: root + '/jquery-lazy',
|
||||
alias: 'jquery-lazy',
|
||||
js: [
|
||||
'/jquery.lazy.min.js',
|
||||
'/jquery.lazy.plugins.min.js',
|
||||
],
|
||||
deps: ['jQuery'],
|
||||
},
|
||||
slickCarousel: {
|
||||
path: root + '/slickCarousel',
|
||||
alias: ['slick', 'slick-carousel'],
|
||||
js: '/slick.min.js',
|
||||
css: [
|
||||
'/slick.css',
|
||||
'/slick-theme.css',
|
||||
],
|
||||
deps: ['jQuery'],
|
||||
},
|
||||
mustache: {
|
||||
path: root + '/mustache',
|
||||
alias: ['mustache.js'],
|
||||
js: '/mustache.js',
|
||||
deps: [],
|
||||
},
|
||||
bxSlider: {
|
||||
path: root + '/bxSlider',
|
||||
module: '/jquery.bxslider.min.js',
|
||||
css: [
|
||||
'/jquery.bxslider.min.css',
|
||||
],
|
||||
type: 'import',
|
||||
deps: ['jQuery'],
|
||||
},
|
||||
selectric: {
|
||||
path: root + '/selectric',
|
||||
module: '/jquery.selectric.min.js',
|
||||
css: [
|
||||
'/selectric.css',
|
||||
],
|
||||
type: 'import',
|
||||
alias: ['jquerySelectric'],
|
||||
deps: ['jQuery'],
|
||||
},
|
||||
jBox: {
|
||||
path: root + '/jBox',
|
||||
module: '/jBox.min.js',
|
||||
css: [
|
||||
'/jBox.min.css',
|
||||
],
|
||||
type: 'import',
|
||||
deps: ['jQuery'],
|
||||
},
|
||||
intlTelInput: {
|
||||
path: root + '/intl-tel-input',
|
||||
module: '/js/intlTelInput.min.js',
|
||||
css: [
|
||||
'/css/intlTelInput.min.css',
|
||||
],
|
||||
type: 'import',
|
||||
alias: ['intl-tel-input'],
|
||||
},
|
||||
autoComplete: {
|
||||
path: root + '/autoComplete',
|
||||
module: '/autoComplete.min.js',
|
||||
css: [
|
||||
'/css/autoComplete.css',
|
||||
],
|
||||
type: 'HMD',
|
||||
},
|
||||
};
|
||||
};
|
||||
+1
File diff suppressed because one or more lines are too long
@@ -0,0 +1,730 @@
|
||||
|
||||
var kit;
|
||||
kit = window.kit = window.kit || {};
|
||||
kit.service = kit.service || {};
|
||||
kit.config = kit.config || {};
|
||||
(function(global){
|
||||
var stateList = {
|
||||
undefined: 0,
|
||||
registered: 1,
|
||||
loaded: 3,
|
||||
installed: 4,
|
||||
};
|
||||
var moduleTypeList = {
|
||||
loader: 'loader',
|
||||
import: 'import',
|
||||
};
|
||||
|
||||
/**
|
||||
* @class PluginManager
|
||||
* @param global
|
||||
* @param plugins
|
||||
* @memberOf kit
|
||||
*/
|
||||
var PluginManager = function(global, plugins = {}){
|
||||
this.plugins = {};
|
||||
this.global = global;
|
||||
this.registerPlugins(plugins);
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @type {kit.PluginManager|PluginManager}
|
||||
* @private
|
||||
*/
|
||||
var __proto = PluginManager.prototype;
|
||||
|
||||
__proto.__checkState = function(pluginId, state){
|
||||
return this.plugins.hasOwnProperty(pluginId) && this.plugins[pluginId].state >= state;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param plugins
|
||||
* @return {PluginManager}
|
||||
*/
|
||||
__proto.registerPlugins = function (plugins) {
|
||||
if(Array.isArray(plugins)) {
|
||||
plugins = Object.assign({}, plugins);
|
||||
}
|
||||
for (var pluginId in plugins) {
|
||||
if(plugins.hasOwnProperty(pluginId)) {
|
||||
this.register(pluginId, plugins[pluginId]);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* @return {boolean}
|
||||
*/
|
||||
__proto.register = function (pluginId, config = {}) {
|
||||
//console.warn('PluginManager.register: plugin ' + pluginId + ' ('+(this.plugins[pluginId]?this.plugins[pluginId].state:'0')+')');
|
||||
var defaultPlugin = {
|
||||
module: {
|
||||
exports: {},
|
||||
},
|
||||
config: {
|
||||
},
|
||||
promise: new Promise(function(resolve, reject){resolve()}),
|
||||
id: pluginId,
|
||||
state: stateList.undefined,
|
||||
stateInit: {
|
||||
loading: false,
|
||||
installing: false,
|
||||
},
|
||||
};
|
||||
var plugin = this.__getPlugin(pluginId);
|
||||
if(plugin) {
|
||||
pluginId = plugin.id;
|
||||
} else {
|
||||
plugin = this.plugins[pluginId] = defaultPlugin;
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo !!!
|
||||
*/
|
||||
if(
|
||||
(this.__checkState(pluginId, stateList.installed) && typeof config.loader === 'function') ||
|
||||
(
|
||||
(plugin.stateInit.loading || this.__checkState(pluginId, stateList.loaded)) && typeof config === 'object'
|
||||
)
|
||||
) {
|
||||
console.warn('PluginManager.register: plugin ' + pluginId + ' уже невозможно конфигурировать ('+plugin.state+')');
|
||||
return false;
|
||||
}
|
||||
|
||||
var loader;
|
||||
if(typeof config === 'function') {
|
||||
loader = config;
|
||||
config = plugin.config;
|
||||
config.loader = loader;
|
||||
config.type = 'loader';
|
||||
if(this.__checkState(pluginId, stateList.registered)) {
|
||||
if(this.__checkState(pluginId, stateList.loaded)){
|
||||
plugin.stateInit.installing = false;
|
||||
}
|
||||
//console.log(`PluginManager.register(${pluginId}): (${plugin.state} > ${stateList.registered})`);
|
||||
plugin.state = stateList.registered;
|
||||
if(plugin.config.autoRequire) {
|
||||
this.require(pluginId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(typeof config === 'object') {
|
||||
if(typeof config.onLoad !== 'function') {
|
||||
config.onLoad = function(){};
|
||||
}
|
||||
if(typeof config.onInstall !== 'function') {
|
||||
config.onInstall = function(){};
|
||||
}
|
||||
if(typeof config.onRegister !== 'function') {
|
||||
config.onRegister = function(){};
|
||||
}
|
||||
if(typeof config.path !== 'string') {
|
||||
config.path = '';
|
||||
}
|
||||
if(!config.hasOwnProperty('modulePath')) {
|
||||
config.modulePath = config.path;
|
||||
} else if(typeof config.modulePath !== 'string') {
|
||||
config.modulePath = config.path;
|
||||
}
|
||||
if(typeof config.js === 'string') {
|
||||
config.js = [config.js];
|
||||
} else if(!Array.isArray(config.js)) {
|
||||
config.js = [];
|
||||
}
|
||||
if(typeof config.css === 'string') {
|
||||
config.css = [config.css];
|
||||
} else if(!Array.isArray(config.css)) {
|
||||
config.css = [];
|
||||
}
|
||||
|
||||
if(typeof config.alias === 'string') {
|
||||
config.alias = [config.alias];
|
||||
} else if(!Array.isArray(config.alias)) {
|
||||
config.alias = [];
|
||||
}
|
||||
|
||||
config.loadJsAfterModule = !!config.loadJsAfterModule;
|
||||
config.isSetTimestamp = config.hasOwnProperty('isSetTimestamp')?!!config.isSetTimestamp:true;
|
||||
config.type = config.type || 'import'; //loader
|
||||
|
||||
if(typeof config.deps === 'string') {
|
||||
config.deps = [config.deps];
|
||||
} else if(!Array.isArray(config.deps)) {
|
||||
config.deps = [];
|
||||
}
|
||||
|
||||
} else {
|
||||
console.warn('PluginManager.register: plugin ' + pluginId + ' is not a valid format');
|
||||
return false;
|
||||
}
|
||||
|
||||
Object.assign(plugin.config, config);
|
||||
|
||||
if(!this.__checkState(pluginId, stateList.registered)) {
|
||||
plugin.state = stateList.registered;
|
||||
plugin.config.onRegister(plugin);
|
||||
var event = new CustomEvent("onRegister" + plugin.id + "Plugin", {
|
||||
"detail": {plugin: plugin}
|
||||
});
|
||||
document.dispatchEvent(event);
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param pluginId
|
||||
* @param callback
|
||||
* @return {PluginManager}
|
||||
*/
|
||||
__proto.load = function (pluginId, callback) {
|
||||
var self = this;
|
||||
//console.log('PluginManager.load: plugin ' + pluginId + ' ('+(this.plugins[pluginId]?this.plugins[pluginId].state:'0')+')');
|
||||
if(typeof callback !== 'function') {
|
||||
callback = function(){};
|
||||
}
|
||||
|
||||
var plugin = this.__getPlugin(pluginId);
|
||||
if(plugin) {
|
||||
pluginId = plugin.id;
|
||||
}
|
||||
|
||||
if(!this.__checkState(pluginId, stateList.registered)) {
|
||||
console.warn('kit.load: plugin ' + pluginId + ' is not register');
|
||||
return this;
|
||||
}
|
||||
if(this.__checkState(pluginId, stateList.loaded)) {
|
||||
//console.warn('PluginManager.load: plugin ' + pluginId + ' уже загружен');
|
||||
callback();
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
if(plugin.stateInit.loading) {
|
||||
//console.log('PluginManager.install: plugin ' + pluginId + ' уже грузится');
|
||||
this.__exec(pluginId, callback);
|
||||
return this;
|
||||
}
|
||||
plugin.stateInit.loading = true;
|
||||
|
||||
var configModule = plugin.config;
|
||||
|
||||
var stackPush = function (callbackPromise, argArr = []){
|
||||
if(typeof plugin.promise === 'undefined') {
|
||||
plugin.promise = callbackPromise.apply(this, argArr);
|
||||
} else {
|
||||
plugin.promise = plugin.promise.then(function(m){
|
||||
return callbackPromise.apply(this, argArr);
|
||||
});
|
||||
}
|
||||
}.bind(this);
|
||||
|
||||
var getModulePromise = (function () {
|
||||
|
||||
function getFileLoadNode(src) {
|
||||
src = src.match(/^[^#?]*/)[0];
|
||||
return document.querySelector('script[src^="'+src+'"]') ||
|
||||
document.querySelector('link[href^="'+src+'"]') ||
|
||||
document.querySelector('meta[property="kit:file-load"][content="'+src+'"]');
|
||||
}
|
||||
|
||||
function loadScript(src, resolveCallback, rejectCallback) {
|
||||
var originSrc = src;
|
||||
src = String(src).split('?')[0];
|
||||
if(typeof resolveCallback !== 'function'){
|
||||
resolveCallback = function(){};
|
||||
}
|
||||
if(typeof rejectCallback !== 'function'){
|
||||
rejectCallback = function(res){
|
||||
console.warn(res.message, originSrc);
|
||||
};
|
||||
}
|
||||
return new Promise(function(resolve, reject) {
|
||||
var loadNode = getFileLoadNode(src);
|
||||
if (loadNode) {
|
||||
global.config.debug && console.warn(`Скрипт ${src} уже загружен`);
|
||||
// self.__ready(resolve, [loadNode]);
|
||||
resolve();
|
||||
} else {
|
||||
var script = document.createElement('script');
|
||||
|
||||
try {
|
||||
var url = new URL(originSrc);
|
||||
if(plugin.config.isSetTimestamp){
|
||||
url.searchParams.append('__pm', (new Date()).getTime());
|
||||
}
|
||||
script.src = url.toString();
|
||||
} catch (e) {
|
||||
script.src = '' + src + '?__pm' + ((new Date()).getTime());
|
||||
}
|
||||
|
||||
script.onload = function() {resolve(script);};
|
||||
script.onerror = function() {reject(new Error(`Ошибка загрузки скрипта ${src}`));};
|
||||
document.head.append(script);
|
||||
}
|
||||
}).then(resolveCallback, rejectCallback);
|
||||
}
|
||||
function syncLoadScriptList(list) {
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
var i = 0;
|
||||
var status = true;
|
||||
var count = list.length;
|
||||
var checkResolve = function(src, cond){
|
||||
if(!cond) {
|
||||
console.error(`Ошибка загрузки скрипта ${src}`)
|
||||
}
|
||||
status = status && cond;
|
||||
i++;
|
||||
if(i === count) {
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
var syncPromise = new Promise(function(_resolve, _reject){_resolve();});
|
||||
list.forEach(function(item, i, arr){
|
||||
syncPromise.then(function(){
|
||||
var src = configModule.path + item;
|
||||
var loadNode = getFileLoadNode(src);
|
||||
if(loadNode){
|
||||
self.__ready(resolve, [src, true]);
|
||||
// resolve(src, true);
|
||||
} else {
|
||||
var script = document.createElement('script');
|
||||
script.src = '' + src + '?__pm' + (new Date()).getTime();
|
||||
script.onload = () => checkResolve(src, true);
|
||||
script.onerror = () => checkResolve(src, false);
|
||||
document.head.append(script);
|
||||
}
|
||||
});
|
||||
});
|
||||
})
|
||||
}
|
||||
function loadStyle(src) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
var loadNode = getFileLoadNode(src);
|
||||
if(loadNode) {
|
||||
global.config.debug && console.warn(`Стиль ${src} уже загружен`);
|
||||
// self.__ready(resolve, [src]);
|
||||
resolve();
|
||||
} else {
|
||||
var link = document.createElement('link');
|
||||
link.rel = 'stylesheet';
|
||||
link.type = 'text/css';
|
||||
link.href = src + '?__pm' + (new Date()).getTime();
|
||||
link.media = 'all';
|
||||
document.head.append(link);
|
||||
resolve(link);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var getImportPromise = function (){
|
||||
if(configModule.module && configModule.type === 'import') {
|
||||
return import(configModule.modulePath + configModule.module).then(function(m) {
|
||||
m.__esModule = true;
|
||||
plugin.module.exports = m;
|
||||
}.bind(this), function(r){
|
||||
console.error(r);
|
||||
});
|
||||
} else if(configModule.module && configModule.type === 'loader' && !(configModule.hasOwnProperty('loader') && typeof configModule.loader === 'function')) {
|
||||
return loadScript(configModule.modulePath + configModule.module).then(function() {
|
||||
|
||||
}.bind(this));
|
||||
}
|
||||
return new Promise(function(resolve, reject){resolve();});
|
||||
}.bind(this);
|
||||
var getResPromise = function(){
|
||||
var p = new Promise(function(resolve, reject){resolve();});
|
||||
configModule.js.forEach(function(item, i, array){
|
||||
if(Array.isArray(item)) {
|
||||
p = p.then(function () {
|
||||
return syncLoadScriptList(item);
|
||||
});
|
||||
} else {
|
||||
p = p.then(function () {
|
||||
return loadScript(configModule.path + item);
|
||||
});
|
||||
}
|
||||
//loadScript(configModule.path + item);
|
||||
});
|
||||
return p;
|
||||
}.bind(this);
|
||||
var setSkipLoadPromise = function (depsPromise) {
|
||||
|
||||
var jsPromiseExists = true, importPromiseExists = true;
|
||||
if(configModule.js.length === 0) {
|
||||
jsPromiseExists = false;
|
||||
}
|
||||
if((!configModule.module && configModule.type === 'import') || (configModule.type === 'loader' && typeof configModule.loader === 'function')) {
|
||||
importPromiseExists = false;
|
||||
}
|
||||
plugin.skipLoadPromise = !importPromiseExists && !jsPromiseExists && !depsPromise;
|
||||
|
||||
return plugin.skipLoadPromise;
|
||||
}.bind(this);
|
||||
|
||||
configModule.css.forEach(function(item, i, array){
|
||||
loadStyle(configModule.path + item);
|
||||
});
|
||||
|
||||
var depsPromise = this.__getDepsPromise(this.depsCorrection(configModule.deps));
|
||||
setSkipLoadPromise(depsPromise);
|
||||
|
||||
if(depsPromise) {
|
||||
stackPush(function () {
|
||||
return depsPromise;
|
||||
});
|
||||
}
|
||||
|
||||
if(configModule.loadJsAfterModule) {
|
||||
stackPush(getImportPromise);
|
||||
stackPush(getResPromise);
|
||||
} else {
|
||||
stackPush(getResPromise);
|
||||
stackPush(getImportPromise);
|
||||
}
|
||||
|
||||
this.__exec(pluginId, function(){
|
||||
plugin.state = stateList.loaded;
|
||||
plugin.skipLoadPromise = true;
|
||||
configModule.onLoad.call(plugin.module.exports);
|
||||
var event = new CustomEvent("onLoad" + plugin.id + "Plugin", {
|
||||
"detail": {plugin: plugin}
|
||||
});
|
||||
document.dispatchEvent(event);
|
||||
callback();
|
||||
}.bind(this));
|
||||
|
||||
return plugin.promise;
|
||||
|
||||
}).bind(this);
|
||||
|
||||
getModulePromise();
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param pluginId
|
||||
* @param callback
|
||||
* @return {PluginManager}
|
||||
*/
|
||||
__proto.install = function(pluginId, callback){
|
||||
//console.warn('PluginManager.install: plugin ' + pluginId + ' ('+(this.plugins[pluginId]?this.plugins[pluginId].state:'0')+')');
|
||||
if(typeof callback !== 'function') {
|
||||
callback = function(){};
|
||||
}
|
||||
|
||||
var plugin = this.__getPlugin(pluginId);
|
||||
if(plugin) {
|
||||
pluginId = plugin.id;
|
||||
}
|
||||
var module = plugin.module;
|
||||
|
||||
if(!this.__checkState(pluginId, stateList.registered)) {
|
||||
console.warn('PluginManager.install: plugin ' + pluginId + ' is not register');
|
||||
return this;
|
||||
}
|
||||
if(this.__checkState(pluginId, stateList.installed)) {
|
||||
callback();
|
||||
return this;
|
||||
}
|
||||
|
||||
if(plugin.stateInit.installing) {
|
||||
//console.warn(`PluginManager.install(${pluginId}): уже устанавливается (${plugin.state}).`);
|
||||
this.__exec(pluginId, callback);
|
||||
return this;
|
||||
}
|
||||
plugin.stateInit.installing = true;
|
||||
|
||||
this.load(pluginId);
|
||||
|
||||
var installer = function(){
|
||||
var setGlobalExtension = function (key, aliasList, data) {
|
||||
if(data === undefined) {
|
||||
return;
|
||||
}
|
||||
if(!this.global.hasOwnProperty(key)) {
|
||||
this.global[key] = data;
|
||||
}
|
||||
if(Array.isArray(aliasList) && aliasList.length) {
|
||||
aliasList.forEach(function (alias) {
|
||||
setGlobalExtension(alias, [], data)
|
||||
}.bind(this))
|
||||
}
|
||||
}.bind(this);
|
||||
|
||||
/**
|
||||
* @todo !!!
|
||||
*/
|
||||
var returnData;
|
||||
if(plugin.config.type === moduleTypeList.loader && typeof plugin.config.loader === 'function') {
|
||||
// var returnData = plugin.config.loader.bind(module.exports).call(module.exports, module, module.exports, this.require.bind(this), global);
|
||||
returnData = plugin.config.loader.bind(this.global).call(this.global, module, module.exports, this.require.bind(this), this.global);
|
||||
if(returnData === undefined && module.exports){
|
||||
returnData = module.exports;
|
||||
}
|
||||
setGlobalExtension(pluginId, plugin.config.alias, returnData);
|
||||
} else if(plugin.config.type === moduleTypeList.import && typeof plugin.config.module === 'string') {
|
||||
returnData = (typeof module.exports === 'object' && module.exports.default !== undefined)?module.exports.default:module.exports;
|
||||
setGlobalExtension(pluginId, plugin.config.alias, returnData);
|
||||
}
|
||||
|
||||
console.log(`%cPluginManager.install(${pluginId}): installed (${plugin.state} > ${stateList.installed}).`, "color: green;");
|
||||
plugin.state = stateList.installed;
|
||||
|
||||
|
||||
return true;
|
||||
}.bind(this);
|
||||
|
||||
this.__exec(pluginId, function(){
|
||||
if(!this.__checkState(pluginId, stateList.installed)) {
|
||||
if(installer()){
|
||||
plugin.config.onInstall.call(module.exports, plugin);
|
||||
var event = new CustomEvent("onInstall" + plugin.id + "Plugin", {
|
||||
"detail": {plugin: plugin}
|
||||
});
|
||||
document.dispatchEvent(event);
|
||||
}
|
||||
}
|
||||
callback();
|
||||
}.bind(this));
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* @name kitPluginManagerRequire
|
||||
* @param {string} pluginId
|
||||
* @param {callback} callback
|
||||
* @return {boolean}
|
||||
* @memberOf {kit}
|
||||
*/
|
||||
__proto.require = function (pluginId, callback) {
|
||||
|
||||
var plugin = this.__getPlugin(pluginId);
|
||||
if(!plugin) {
|
||||
console.warn('PluginManager.require: plugin ' + pluginId + ' not found ('+(this.plugins[pluginId]?this.plugins[pluginId].state:'0')+')');
|
||||
return undefined;
|
||||
}
|
||||
pluginId = plugin.id;
|
||||
var module = plugin.module;
|
||||
|
||||
if(!this.__checkState(pluginId, stateList.registered)) {
|
||||
console.warn('PluginManager.require: plugin ' + pluginId + ' is not register');
|
||||
return undefined;
|
||||
}
|
||||
if(typeof callback !== 'function') {
|
||||
callback = function(){};
|
||||
}
|
||||
|
||||
this.install(pluginId, callback);
|
||||
|
||||
return module.exports;
|
||||
};
|
||||
|
||||
|
||||
|
||||
__proto.depsCorrection = function(deps){
|
||||
if(!Array.isArray(deps) ) {
|
||||
deps = [deps];
|
||||
}
|
||||
var correct = [];
|
||||
deps.forEach(function(pluginId, i, arr) {
|
||||
var realPlugin = this.__getPlugin(pluginId);
|
||||
if(realPlugin === undefined) {
|
||||
console.warn(`Plugin "${pluginId}" not registered!`);
|
||||
return false;
|
||||
}
|
||||
correct.push(realPlugin.id);
|
||||
}.bind(this));
|
||||
return correct;
|
||||
};
|
||||
|
||||
/**
|
||||
* @memberOf kit
|
||||
*/
|
||||
__proto.exec = function (deps, callback, argArr = [], context = undefined) {
|
||||
|
||||
if(typeof deps === 'function') {
|
||||
context = argArr;
|
||||
argArr = callback;
|
||||
callback = deps;
|
||||
deps = [];
|
||||
}
|
||||
if(context === undefined) {
|
||||
context = this;
|
||||
}
|
||||
|
||||
deps = this.depsCorrection(deps);
|
||||
|
||||
if(typeof argArr === 'undefined') {
|
||||
argArr = [];
|
||||
}
|
||||
|
||||
deps = Array.from(new Set(deps.concat(global.config.requiredDependencies)));
|
||||
|
||||
if(typeof callback !== 'function') {
|
||||
return undefined;
|
||||
}
|
||||
var depsPromise = this.__getDepsPromise(deps);
|
||||
|
||||
var getArgArr = function () {
|
||||
if(argArr.length === 0) {
|
||||
deps.forEach(function (pluginId) {
|
||||
argArr.push(this.require(pluginId));
|
||||
}.bind(this));
|
||||
return argArr;
|
||||
}
|
||||
return argArr;
|
||||
}.bind(this);
|
||||
|
||||
if(depsPromise) {
|
||||
depsPromise.then(function(){
|
||||
callback.apply(context, getArgArr());
|
||||
});
|
||||
} else {
|
||||
callback.apply(context, getArgArr())
|
||||
}
|
||||
};
|
||||
|
||||
__proto.__exec = function (pluginId, callback, argArr = []) {
|
||||
|
||||
var plugin = this.plugins[pluginId];
|
||||
|
||||
var exec = function(_callback, argArr){
|
||||
if(plugin.skipLoadPromise) {
|
||||
_callback.apply(this, argArr);
|
||||
} else {
|
||||
plugin.promise = plugin.promise.then(function(){
|
||||
_callback.apply(this, argArr);
|
||||
}.bind(this));
|
||||
}
|
||||
}.bind(this);
|
||||
|
||||
exec(callback, argArr);
|
||||
};
|
||||
|
||||
__proto.__getDepsPromise = function (pluginList) {
|
||||
var skipPromise = true;
|
||||
var promise = new Promise((resolve, reject)=>{
|
||||
var i = 0;
|
||||
var checkModule = function(){
|
||||
if(++i === pluginList.length + 1) {
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
checkModule();
|
||||
pluginList.forEach(function(pluginId, i, array){
|
||||
if(!this.__checkState(pluginId, stateList.registered)) {
|
||||
checkModule();
|
||||
return false;
|
||||
}
|
||||
this.install(pluginId, checkModule);
|
||||
skipPromise = skipPromise && this.plugins[pluginId].skipLoadPromise;
|
||||
}.bind(this));
|
||||
});
|
||||
|
||||
return !skipPromise?promise:undefined;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {(string|string[])} deps
|
||||
* @param {function} callback
|
||||
* @memberOf kit
|
||||
*/
|
||||
__proto.ready = function(deps, callback = undefined) {
|
||||
if(typeof deps === 'function') {
|
||||
callback = deps;
|
||||
deps = [];
|
||||
}
|
||||
this.__ready(this.exec, [deps, callback], this);
|
||||
};
|
||||
__proto.__ready = function(fn, args = [], context = window) {
|
||||
context = context || this;
|
||||
if (/complete|interactive|loaded/.test(document.readyState)) {
|
||||
fn.apply(context, args);
|
||||
} else {
|
||||
document.addEventListener("DOMContentLoaded", function () {
|
||||
fn.apply(context, args);
|
||||
}.bind(this));
|
||||
}
|
||||
};
|
||||
|
||||
__proto.__getPlugin = function (pluginId) {
|
||||
if(this.plugins.hasOwnProperty(pluginId)) {
|
||||
return this.plugins[pluginId];
|
||||
}
|
||||
var _pluginId, alias;
|
||||
for (_pluginId in this.plugins) {
|
||||
if(this.plugins.hasOwnProperty(_pluginId) && this.plugins[_pluginId].config.alias.indexOf(pluginId) !== -1) {
|
||||
return this.plugins[_pluginId];
|
||||
}
|
||||
}
|
||||
var pluginIdLowerCase = pluginId.toLowerCase();
|
||||
for (_pluginId in this.plugins) {
|
||||
if(!this.plugins.hasOwnProperty(_pluginId)) {
|
||||
continue;
|
||||
}
|
||||
if(pluginIdLowerCase === _pluginId.toLowerCase()) {
|
||||
return this.plugins[_pluginId];
|
||||
}
|
||||
for(alias of this.plugins[_pluginId].config.alias) {
|
||||
if(pluginIdLowerCase === alias.toLowerCase()) {
|
||||
return this.plugins[_pluginId];
|
||||
}
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
|
||||
/**
|
||||
* @class kitConfig
|
||||
*/
|
||||
var defaultConfig = {
|
||||
version: '1.10',
|
||||
assetsPath: '',
|
||||
requiredDependencies: [],
|
||||
autoloadPluginList: [],
|
||||
debug: false,
|
||||
defaultPhoneMask: '+7 (999) 999-99-99',
|
||||
};
|
||||
|
||||
var script = document.currentScript;
|
||||
if(script){
|
||||
var path = script.src.split('?')[0];
|
||||
defaultConfig.assetsPath = path.split('/').slice(0, -2).join('/');
|
||||
}
|
||||
|
||||
var manager = new PluginManager(global);
|
||||
manager.register('PluginManager', {
|
||||
alias: 'pm',
|
||||
type: moduleTypeList.loader,
|
||||
loader: function(module, exports, require) {
|
||||
return module.exports = manager;
|
||||
}
|
||||
});
|
||||
manager.install('PluginManager');
|
||||
manager.register('config', {
|
||||
type: moduleTypeList.loader,
|
||||
loader: function(module, exports, require, global) {
|
||||
var config = global.config || {};
|
||||
config = Object.assign({}, defaultConfig, config);
|
||||
window.__pmConfig__ = global.config = config;
|
||||
}
|
||||
});
|
||||
manager.install('config');
|
||||
|
||||
window.__PluginManager__ = manager;
|
||||
global.ready = manager.ready.bind(manager);
|
||||
global.exec = manager.exec.bind(manager);
|
||||
global.require = manager.require.bind(manager);
|
||||
/**
|
||||
* @var __pmGlobal__
|
||||
*/
|
||||
window.__pmGlobal__ = global;
|
||||
})(kit);
|
||||
|
||||
Reference in New Issue
Block a user