-
Notifications
You must be signed in to change notification settings - Fork 1
/
e3_js.js
250 lines (216 loc) · 7 KB
/
e3_js.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* @file e3_js.js
*
* @author rgkimball
* @see https://github.com/rgkimball/e3_js for documentation and usage
*
* JavaScript should be made compatible with libraries other than jQuery by
* wrapping it with an "anonymous closure". See:
* - http://drupal.org/node/1446420
* - http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
*
* Precede IIFEs with a ! to prevent file concatenation errors;
* this simplifies ;(function(){})(); to !function(){}()
*/
!function ($, Drupal, window, document, undefined) {
window.e3 = {
version: '0.3.0',
win: $(window),
doc: $(document),
// These should match whatever is defined in _base.scss:
breakpoints: [460,768,960,1280],
// Leave each of these objects empty. They are called sequentially below
// You can queue a function using e3.{object}.myFunc()
// This prevents bogging down the page with multiple function calls
// and other mistakes, like having multiple resize handlers
load: {},
behaviors: {},
click: {},
resize: {},
scroll: {},
delay: {},
// Helper functions
url: function(int) {
// Works like Drupal's arg(), but gets path arguments instead
// used to check if an argument is part of the URL, eg. arg(0) == 'front'
var args = window.location.href.split('?')[0].split('#')[0].split('/');
var check = int + 3; // assumes :// is in url, so first 3 items irrelevant
if (typeof args[check] == 'undefined') {
return false;
} else {
return args[check];
}
},
getUrl: function() {
// Turns items of the URL into an array
var vars = [],
args = window.location.href.split('?')[0].split('/');
for (var i = 0; i < args.length; i++) {
if(i > 2) {
vars.push(args[i]);
}
}
return vars;
},
getQuery: function() {
var vars = [], hash;
if(window.location.href.indexOf('?') > -1) {
var hashes = window.location.href.split('?')[1].split('&');
for(var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
} else {
return false;
}
},
// Call bundle's own functions.
callEach: function(bundle) {
for (func in bundle) {
if (bundle.hasOwnProperty(func) && e3.isFuncOf(func, bundle)) {
var args = [].slice.call(arguments,1);
bundle[func].apply(bundle, args);
}
}
},
// Checks if object is a function.
isFuncOf: function(func, parent) {
return Object.prototype.toString.call(parent[func]) === '[object Function]';
}
};
// Shortcut vars
e3.bpmobile = e3.breakpoints[0];
e3.bptablet = e3.breakpoints[1];
e3.bpdesktop = e3.breakpoints[2];
e3.bphuge = e3.breakpoints[3];
e3.load.timerInit = function() {
e3.startTime = (new Date).getTime();
e3.timer = true;
e3.getTime = function() {
if(e3.timer == true) {
e3.time = (((new Date).getTime() - e3.startTime)/1000).toFixed(20);
e3.callEach(e3.delay, e3.time);
}
}
setInterval(e3.getTime,1000);
};
// Acquire contextual objects from Drupal.settings.e3
e3.load.init = function() {
if (!!Drupal.settings.e3) {
$.extend(e3, Drupal.settings.e3);
}
}
e3.doc.ready(function() {
// Nothing here should need to be changed; add functionality in new files
// prefixed with the Drupal object
!function() {
once(e3.callEach(e3.load));
var width = e3.win.width(), height = e3.win.height();
once(e3.callEach(e3.resize, width, height));
}(); // Runs all load, resize and scroll functions once
e3.win.click(
throttle((function(e) {
e3.callEach(e3.click, e.toElement, e);
}), 100)
);
e3.win.resize(
throttle((function() {
e3.callEach(e3.resize, e3.win.width(), e3.win.height());
}), 250)
);
e3.win.scroll(
throttle((function() {
e3.callEach(e3.scroll, e3.win.scrollTop());
}), 200)
);
});
Drupal.behaviors.e3 = {
attach: function(settings, context) {
!function() {
e3.callEach(e3.behaviors, settings, context);
}();
}
};
// The following functions, throttle, once, and dependencies are borrowed from
// underscore.js: @see https://github.com/jashkenas/underscore
// Reusable constructor function for prototype setting.
var Ctor = function(){};
// Determines whether to execute a function as a constructor
// or a normal function with the provided arguments
var executeBound = function(sourceFunc, boundFunc, context, callingContext, args) {
if (!(callingContext instanceof boundFunc)) return sourceFunc.apply(context, args);
var self = baseCreate(sourceFunc.prototype);
var result = sourceFunc.apply(self, args);
if (isObject(result)) return result;
return self;
};
var isObject = function(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
}
// For creating a new object that inherits from another.
var baseCreate = function(prototype) {
if (!isObject(prototype)) return {};
if (Object.create) return Object.create(prototype);
Ctor.prototype = prototype;
var result = new Ctor;
Ctor.prototype = null;
return result;
};
var partial = function(func) {
var boundArgs = Array.prototype.slice.call(arguments, 1);
return function bound() {
var position = 0;
var args = boundArgs.slice();
for (var i = 0, length = args.length; i < length; i++) {
args[i] = arguments[position++]; // assume valid.
}
while (position < arguments.length) args.push(arguments[position++]);
return executeBound(func, bound, this, this, args);
};
};
var throttle = function(func, wait, options) {
var context, args, result;
var timeout = null;
var previous = 0;
var now = e3.time;
if (!options) options = {};
var later = function() {
previous = options.leading === false ? 0 : now;
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};
return function() {
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
}
var before = function(times, func) {
var memo;
return function() {
if (--times > 0) {
memo = func.apply(this, arguments);
}
if (times <= 1) func = null;
return memo;
};
};
var once = partial(before, 2);
}(jQuery, Drupal, this, this.document);