This repository was archived by the owner on Feb 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskeletor.util.componentLoader.js
More file actions
90 lines (79 loc) · 2.71 KB
/
Copy pathskeletor.util.componentLoader.js
File metadata and controls
90 lines (79 loc) · 2.71 KB
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
/**
* @copyright 2016, The Skeletor Project
* @license http://opensource.org/licenses/BSD-3-Clause
*/
define(['jquery', 'skeletor.core', 'onMediaQuery'],function($, Skeletor, MQ){
function ComponentLoader(element, options) {
ComponentLoader.__super__.call(this, element, options, ComponentLoader.DEFAULTS);
}
ComponentLoader.VERSION = '0.2.1';
ComponentLoader.DEFAULTS = {}
Skeletor.Plugin.create(ComponentLoader, {
_init: function(element) {
var _this = this;
var components = $.map($('[data-component]'), function(el){
return {context: $(el).data('component-context') || null, name: 'components/'+$(el).data('component')};
});
var contexts = this._mergeByContext(components);
//Init onMediaQuery if it isn't already
if(!MQ.callbacks){
MQ.init();
}
$.each(contexts, function(i, component){
if(component.context != 'null'){
_this._addContextQuery(component.context, component.name)
}else{
//component does not need a context. i.e, it's initilized on page load no matter the context;
_this._loadComponent(component.name)
}
})
},
_addContextQuery: function(context, components){
var contextArr = context.split(','),
componentsArr = components.split(',');
MQ.addQuery({
context: contextArr,
call_for_each_context: false,
match: function(){
require(componentsArr, function(){
$.each(arguments, function(i, component){
component.init();
})
},function (err) {
var failedId = err.requireModules && err.requireModules[0];
console.error(failedId + ' couldn\'t be found! Does the file exist?')
})
},
unmatch: function(){
$.each(componentsArr, function(i, component){
var component = require(component);
if (typeof component.destroy === 'function'){ component.destroy() };
})
}
});
},
_loadComponent: function(components){
var componentsArr = components.split(',');
require(componentsArr, function(){
$.each(arguments, function(i, component){
component.init();
})
},function (err) {
var failedId = err.requireModules && err.requireModules[0];
console.error(failedId + ' couldn\'t be found! Does the file exist?')
})
},
_mergeByContext: function(arr){ //http://stackoverflow.com/questions/19118016/merge-values-of-array-by-duplicate-keys
var temp = {};
for (var i=0; i<arr.length; i++) {
temp[arr[i].context] = temp[arr[i].context] === undefined ? arr[i].name : temp[arr[i].context]+','+arr[i].name;
}
arr = [];
for (var key in temp) {
arr.push({context: key, name:temp[key]})
}
return arr;
}
});
new ComponentLoader()
});