-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexplain.html
673 lines (623 loc) · 23.2 KB
/
explain.html
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
<!DOCTYPE html>
<html>
<head>
<title> | ExplainJS by grunt-explainjs</title>
<link rel="stylesheet" type="text/css" href="css/unsemantic.css"/>
<link rel="stylesheet" type="text/css" href="css/explain.css"/>
<link href='http://fonts.googleapis.com/css?family=Inconsolata|Open+Sans:300italic,300,700' rel='stylesheet' type='text/css'>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1" />
</head>
<body>
<div class='page-menu'>
<div class='grid-container'>
<div class='grid-60 mobile-grid-100'>
<a class='logo' href='http://www.explainjs.com'>ExplainJS</a> by <a class='logo' href='https://github.com/collinforrester/grunt-explainjs'>grunt-explainjs</a>
</div>
</div>
</div>
<div class='sections grid-container'>
<div class='code-bg hide-on-mobile'>
</div>
<br />
<br />
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>global _</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre>(function () {
'use strict';</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>takes function and calls it for every item in context object, then cleans up falsy entries and returns return</p> <p>@param {Function} fn</p> <p>@returns {Array}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> function mapCompact(fn) {</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>jshint validthis: true</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> return _.chain(this).map(fn).compact().value();
}
</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>updates subset based on results of fn execution</p> <p>@param {Function} fn</p> <p>@returns {Array|$.Init}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> function updateOverFn(fn) {</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>jshint validthis: true</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> return this._update(mapCompact.call(this, fn));
}</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>check if element is even or odd, based on its location in a dom, related to its parent</p> <p>@param {Number} order 0 or 1 - even or odd</p> <p>@returns {Number} either 0 or something else not falsy</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> function evenOdd(order) {</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>jshint validthis: true</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> return (_.indexOf(this.parentNode.children, this) - (order ? 1 : 0)) % 2;
}
</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>check if element is even or odd, based on its order</p> <p>@param {Number} order 0 or 1 - even or odd</p> <p>@param {Number} i</p> <p>@returns {Number} either 0 or something else not falsy</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> function simpleEvenOdd(order, i) {
return (i - order) % 2;
}
</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>detects support for next/previousElementSibling and returns next/previous siblings</p> <p>@param {String} direction next or previous</p> <p>@returns {$.Init}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> var nextPrev = (function nextPrev() {
if (document.body.previousElementSibling) {
return function (direction) {
return updateOverFn.call(this, function (el) {
return el[direction + 'ElementSibling'];
});
};
}
return function (direction) {
return updateOverFn.call(this, function (el) {
do { //ie
el = el[direction + 'Sibling'];
} while (el && el.nodeType !== 1);
return el;
});
};
}());</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>returns function to find all next/previous siblings</p> <p>@param {String} direction next or previous</p> <p>@returns {$.Init}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> function nextPrevAll(direction) {</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>jshint validthis: true</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> return this._update(_.chain(this).map(function (el) {
var results = [],
result;
while ((result = _.$(el)[direction]()) && (el = result[0])) {
results.push(result);
}
return results;
}).flatten().value());
}
var next = _.partial(nextPrev, 'next'),
prev = _.partial(nextPrev, 'previous'),
nextAll = _.partial(nextPrevAll, 'next'),
prevAll = _.partial(nextPrevAll, 'prev');
</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>returns function which will be called as method of given object (_.$)</p> <p>@param {Function} fn</p> <p>@returns {Function}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> function callAsSelf(fn) {
return function () {
return fn.call(this);
};
}
</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>get style value for given property of the given name (found on github)</p> <p>@param {HTMLElement} el</p> <p>@param {String} styleProp</p> <p>@returns {String}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> var getStyle = (function () {</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>W3C standard way:</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> if (window.getComputedStyle) {
return function getStyle(el, styleProp) {</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>sanitize property name to css notation (hyphen separated words eg. font-size)</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> styleProp = styleProp.replace(/([A-Z])/g, '-$1').toLowerCase();
return getComputedStyle(el, null).getPropertyValue(styleProp);
};
} else { // IE
return function (el, styleProp) {</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>sanitize property name to camelCase</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> styleProp = styleProp.replace(/\-(\w)/g, function (str, letter) {
return letter.toUpperCase();
});
var value = el.currentStyle[styleProp];</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>convert other units to pixels on IE</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> if (/^\d+(em|pt|%|ex)?$/i.test(value)) {
return (function (value) {
var oldLeft = el.style.left, oldRsLeft = el.runtimeStyle.left;
el.runtimeStyle.left = el.currentStyle.left;
el.style.left = value || 0;
value = el.style['pixelLeft'] + 'px';
el.style.left = oldLeft;
el.runtimeStyle.left = oldRsLeft;
return value;
})(value);
}
return value;
};
}
}());
</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>convert css string into object</p> <p>@param {String} cssText</p> <p>@returns {Object}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> function rulesToObj(cssText) {
if (!trim(cssText)) {
return {};
}
return _.chain(cssText.split(';'))
.map(function (rules) {
rules = rules.split(':');
return {
prop: trim(rules[0]),
value: trim(rules[1])
};
})
.indexBy('prop')
.mapValues('value')
.value();
}
</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>convert object into css string</p> <p>@param {Object} obj</p> <p>@returns {String}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> function rulesFromObj(obj) {
return _.chain(obj)
.pairs()
.invoke('join', ':')
.value()
.join(';');
}
</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>cross-browser string trim</p> <p>@param {String} str</p> <p>@returns {String}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> function trim(str) {
return str.replace(/^\s+|\s+$/, '');
}
</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<hr />
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre></pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>get elements by selector in a given context</p> <p>@static</p> <p>@memberOf _.</p> <p>@param {String} selector</p> <p>@param {HTMLElement} [context]</p> <p>@returns {$.Init}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> function $(selector, context) {
return new $.prototype.Init(selector, context);
}</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>get elements by selector in a given context</p> <p>@constructor</p> <p>@param {String|HTMLElement} selector</p> <p>@param {HTMLElement} [context]</p> <p>@memberOf _.$.prototype</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> function Init(selector, context) {
this.length = 0;
if (typeof selector === 'string') {
context = context || document;
_.each(context.querySelectorAll(selector), _.bind(this._push, this));
} else {
this._push(selector);
}
return this;
}
$.prototype = {
constructor: $,</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>@param {*} mixed</p> <p>@protected</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> _push: function (mixed) {
if (mixed) {
[].push.call(this, mixed);
}
},
_clean: function () {
[].splice.call(this, 0, this.length);
if ('0' in this) { //ie8
for (var i in this) {
if (this.hasOwnProperty(i)) {
delete this[i];
}
}
}
},
_update: function (updates) {
this._clean();
_.each(updates, _.bind(this._push, this));
return this;
},
Init: Init,</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>find elements within base element by selector</p> <p>@param {String} selector</p> <p>@memberOf _.$.prototype</p> <p>@returns {$.Init}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> find: function find(selector) {
return this._update(_.chain(this).map(function (element) {
return _.chain(selector).$(element).toArray().value();
}).flatten().value());
},</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>filter elements by selector or function</p> <p>@param {String|Function} selector</p> <p>@memberOf _.$.prototype</p> <p>@returns {$.Init}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> filter: function filter(selector) {
return this._update(_.filter(this, function (el, i) {
if (_.isString(selector)) {
return _.$(el).is(selector);
}
return _.bind(selector, el)(i);
}));
},</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>get siblings for given elements</p> <p>@memberOf _.$.prototype</p> <p>@returns {$.Init}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> siblings: function siblings() {
var results = _.chain(this).map(function (el) {
return _.reject(el.parentNode.children, function (child) {
return child === el;
});
}).flatten().uniq().value();
return this._update(results);
},</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>@type {Function}</p> <p>@memberOf _.$.prototype</p> <p>@returns {$.Init}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> next: callAsSelf(next),</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>@type {Function}</p> <p>@memberOf _.$.prototype</p> <p>@returns {$.Init}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> prev: callAsSelf(prev),</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>@type {Function}</p> <p>@memberOf _.$.prototype</p> <p>@returns {$.Init}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> nextAll: callAsSelf(nextAll),</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>@type {Function}</p> <p>@memberOf _.$.prototype</p> <p>@returns {$.Init}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> prevAll: callAsSelf(prevAll),</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>set or get text for elements</p> <p>@param {String} str</p> <p>@returns {$.Init|Array} strings or elements</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> text: function text(str) {
var getText = 'textContent' in this[0] ? 'textContent' : 'innerText';
return updateOverFn.call(this, function (el) {
if (str) {
el[getText] = str;
return el;
} else {
return el[getText];
}
});
},</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>check if element(s) match the selector</p> <p>@param {String} selector</p> <p>@returns {Boolean}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> is: function is(selector) {</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>jshint -W052</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> var f = this[0],
matches = f.matches || f.matchesSelector || f['msMatchesMatchesSelector'] || f['mozMatchesMatchesSelector'] || f['webkitMatchesSelector'] || f['oMatchesSelector'];
return _.every(this, function (el) {
if (matches) {
return matches.call(el, selector);
} else {</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>jshint bitwise: false</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> return ~_.chain(selector).$(el.parentNode).indexOf(el).value();
}
});
},</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>get closest parent with given selector</p> <p>@param {String} selector</p> <p>@memberOf _.$.prototype</p> <p>@returns {$.Init}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> closest: function closest(selector) {
return updateOverFn.call(this, function (el) {
var parent;
while ((parent = el.parentNode) && !_.$(parent).is(selector)) {
el = parent;
}
return parent;
});
},</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>set or get css value</p> <p>@param {Object|String} rules</p> <p>@this {$.Init}</p> <p>@returns {$.Init|String}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> css: function css(rules) {
if (_.isString(rules)) {
return getStyle(this[0], rules);
}
_.each(this, function (el) {
el.style.cssText = rulesFromObj(
_.merge(
rulesToObj(el.style.cssText.replace(/;$/, '')),
rules
)
);
});
return this;
}
};
Init.prototype = $.prototype;</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>helper function to filter even elements from collection based on elements location within its parent node</p> <p>@memberOf _.$</p> <p>@static</p> <p>@type {Function}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> $.even = _.partial(evenOdd, 0);</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>helper function to filter odd elements from collection based on elements location within its parent node</p> <p>@memberOf _.$</p> <p>@static</p> <p>@type {Function}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> $.odd = _.partial(evenOdd, 1);</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>helper function to filter even elements from collection based on collection's order</p> <p>@memberOf _.$</p> <p>@static</p> <p>@type {Function}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> $.simpleEven = _.partial(simpleEvenOdd, 0);</pre>
</div>
</div>
<div class='clear'>
</div>
<div class='section'>
<div class='comment grid-40 mobile-grid-100 not-empty'>
<p>helper function to filter odd elements from collection based on collection's order</p> <p>@memberOf _.$</p> <p>@static</p> <p>@type {Function}</p>
</div>
<div class='code grid-60 mobile-grid-100 not-empty'>
<pre> $.simpleOdd = _.partial(simpleEvenOdd, 1);
_.mixin({
$: $
});
})();.</pre>
</div>
</div>
<div class='clear'>
</div>
</div>
</body>
</html>