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
|
define('plugins.setup', ['plugins'], function (plugins) {
var system = plugins.setup({
//the global attribute name prefix that hints the system
//for a given plugin in a node
attributeNamePrefix: "data-plugin",
//This is a way to configure/extend all plugin instances.
//The 'configuration' object will be available to all plugins
//using the 'this.configuration'
//You can return a promise (wrapping an xhr request or other async operations)
preconfigure: function (ctx) {
//ctx.configuration.someProperty = someValue;
},
//the time (in seconds) that the system will wait before
//it destroys a disconnected plugin. Default value is 10
idleTimeout: 10,
//the time (in seconds) that the system will wait before
//it starts processing discovered plugins. Default value is 10
processInterval: 10,
//the time (in seconds) that the system will wait before
//it triggers a cleanup cycle. Default value is 10
clearInterval: 10,
//the attribute that hints the system to lazy load a node's plugins
lazyLoadAttribute: 'plugins-lazy',
//the margin of the IntersectionObserver
//that detects when a lazy plugin should be registered
lazyLoadRootMargin: '0px 0px 200px 0px',
//the attribute that hints the system to ignore a node from
//the plugin discovery phase
ignoreAttribute: 'plugins-ignore',
//predicate function to filter out a node from
//the plugin discovery phase
filter: function (node) {
//ignore nodes with the v-plugin directive. Plugins will be initialized by the directive itself.
//return node.getAttribute("v-plugin") === null;
return true;
},
//false if the automatic discovery of plugins is not desired
observeDocument: true,
// shouldLazyLoad: function (node, pluginOptions, lazyLoadAttributeName) {
// return true;
// }
}),
$ = window.jQuery,
$$plugins = {};
system.use(function (context) {
//system.moduleLoader.basePath = '/src/plugins/';
context.onCreate(function (instance) {
if (instance.application.debug) {
console.log(instance.definition.type, 'plugin instance created', instance.definition);
}
if (instance.application.debug) {
instance.onDisconnect(logger("Disconnected"));
instance.onConnect(logger("Connected"));
instance.onDestroy(logger("Destroyed"));
}
function logger (msg){
return function (instance) {
console.log(msg, instance.definition);
}
}
});
context.moduleLoader = {
load: function (pluginType, element) {
return new Promise(function (resolve) {
window.require(pluginType, resolve);
});
}
}
$$plugins.execute = context.processor.initialize;
$$plugins.parse = context.processor.process;
$$plugins.define = function (pluginName, dependencies, plugin, invoke) {
invoke = invoke || function (fn, args, context) {
return fn.apply(context, args);
}
if (typeof (plugin) == 'undefined') {
plugin = dependencies;
dependencies = [];
}
define(pluginName, dependencies, function () {
var dependencyValues = arguments;
return function (element, options) {
var args = [element, options], i;
for (i = 0; i < dependencyValues.length; i++) {
args.push(dependencyValues[i]);
}
return invoke(plugin, args, this);
};
});
};
});
if ($) {
$ = window.jQuery;
$.defineAttributePlugin = function (pluginName, dependencies, plugin) {
return $$plugins.define(pluginName, dependencies, plugin, function (fn, args, context){
args[0] = $(args[0]);
return fn.apply(context, args);
});
}
$.fn.parseAttributePlugins = function () {
return $(this).each(function () {
$$plugins.parse([this]);
});
};
$.fn.executePlugin = function (pluginName, pluginConfiguration, callback) {
return $(this).each(function () {
var $element = $(this);
return $$plugins.execute(this, pluginName, pluginName, pluginConfiguration).then(function (resolved) {
if (typeof (callback) !== 'undefined') {
return callback.apply($element, [resolved]);
}
});
});
};
$.getOnce = function (options) {
if (!window.getOnceCallbacks) {
window.getOnceCallbacks = {};
}
if (options.data) {
var s = $.param(options.data, options.traditional);
if (options.url.indexOf('?') == -1) {
options.url += '?' + s;
}
else {
options.url += '&' + s;
}
delete options.data;
}
if (window.getOnceCallbacks[options.url]) {
window.getOnceCallbacks[options.url].push({
success: options.success,
error: options.error
});
return;
}
window.getOnceCallbacks[options.url] = [{
success: options.success,
error: options.error
}];
function done(m, a) {
var callbacks = window.getOnceCallbacks[options.url];
for (var i = 0; i < callbacks.length; i++) {
callbacks[i][m].apply(this, a);
}
delete window.getOnceCallbacks[options.url];
}
options.success = function () {
done.apply(this, ['success', arguments]);
};
options.error = function () {
done.apply(this, ['error', arguments]);
};
$.ajax(options);
}
window.define.amd["jQuery"] = window.define.amd["jquery"] = { loaded: true, ready: true, object: $ };
}
if (window.Vue) {
window.define.amd["Vue"] = { loaded: true, ready: true, object: window.Vue };
}
window.$$plugins = $$plugins;
return system.initialize;
});
require(['plugins.setup'], function (initialize) {
var pluginsSystem = initialize();
pluginsSystem.application.debug = false;
window.define.amd["plugins.system"] = { loaded: true, ready: true, object: pluginsSystem };
window.define.amd["application"] = { loaded: true, ready: true, object: pluginsSystem.application };
});
|