-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clover.js
132 lines (113 loc) · 3.66 KB
/
Clover.js
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
/* Clover */
var Clover = {
version: 1,
path: (function () {
var scripts = document.getElementsByTagName('script');
var i = scripts.length;
while (i--) {
var script = scripts[i];
if (/Clover/.test(script.src)) {
return script.src.replace(/Clover.js/, '');
}
}
})(),
browser: (function () {
if (window.opera) {
return "Opera";
} else if (window.ActiveXObject) {
return "IE" + (!!(window.XMLHttpRequest) ? "7" : "6");
} else if (!navigator.taintEnabled) {
return "Safari";
} else if (document.getBoxObjectFor !== null) {
return "Firefox";
}
})()
};
Clover.enrich = function Clover_enrich (thing, extras) {
for (var k in extras) thing[k] = extras[k];
return thing;
};
Clover.bindFunction = function Clover_bindFunction (method, object) {
return function () {
return method.apply(object,arguments);
};
};
/* Loadtime initialization */
Clover.addLoadCallback = function Clover_addLoadCallback (func) {
document.addEventListener ?
document.addEventListener('DOMContentLoaded', func, false) :
window.attachEvent('onload', func);
};
/* Event listener wrapper */
Clover.listen = function (element, eventType, func) {
document.addEventListener ?
element.addEventListener(eventType, func, false) :
element.attachEvent('on' + eventType, func);
};
/* Class Primitive */
Clover.Primitive = function Clover_Primitive (options) { };
Clover.Primitive.prototype.initialize =
function Clover_Primitive_prototype_initialize () {};
Clover.Primitive.beget = function Clover_Primitive_beget (dna) {
var classDef = function () {
if (arguments[0] !== Clover.Primitive) {
this.initialize.apply(this, arguments);
}
};
var proto = new this(Clover.Primitive);
var superClass = this.prototype;
Clover.enrich(proto, dna);
classDef.prototype = proto;
classDef.beget = this.beget;
classDef.prototype.superClass = superClass;
return classDef;
};
/* Dynamic module loading */
Clover.Requirement = Clover.Primitive.beget({
initialize: function Clover_Requirement_initialize (options) {
this.dependencies = (typeof options.require == 'string') ? [options.require] : options.require;
this.thenDo = options.thenDo || function () {};
for (var i = 0; i < this.dependencies.length; i++) {
var dep = this.dependencies[i];
var path = Clover.path + 'modules/' + dep.replace(/\./g, '/');
path += path.substring(path.lastIndexOf('/')) + '.js';
if (! (Clover.Requirement.moduleStatus[dep] && Clover.Requirement.moduleStatus[dep] == 'ready')) {
var s = document.createElement('script');
s.src = path;
document.getElementsByTagName('head')[0].appendChild(s);
Clover.Requirement.moduleStatus[dep] = 'loading';
}
}
this.dependencyCheck();
},
dependencyCheck: function Clover_Requirement_dependencyCheck () {
var dependenciesLeft = false;
for (var i = 0; i < this.dependencies.length && !dependenciesLeft; i++) {
if (Clover.Requirement.moduleStatus[this.dependencies[i]] != 'ready') {
dependenciesLeft = true;
}
}
if (dependenciesLeft) {
window.setTimeout(Clover.bindFunction(this.dependencyCheck, this), 10);
} else {
this.thenDo();
}
}
});
Clover.Requirement.moduleStatus = {};
Clover.Requirement.met = function Clover_Requirement_met (req) {
Clover.Requirement.moduleStatus[req] = 'ready';
};
/* Utilities */
Clover.addJS = function (file, id) {
var s = document.createElement('script');
s.setAttribute('src', file);
id && s.setAttribute('id', id);
document.getElementsByTagName('head')[0].appendChild(s);
};
Clover.addCSS = function (file) {
var s = document.createElement('link');
s.setAttribute('rel', 'stylesheet');
s.setAttribute('href', file);
document.getElementsByTagName('head')[0].appendChild(s);
};