forked from Pomax/nrGrammar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pocketknife.js
executable file
·566 lines (532 loc) · 16.1 KB
/
pocketknife.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
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
/**
This is a tiny "I don't need the full jQuery API"
library. It has a small API, and acts more as a
JS API enrichment than a "library". You don't call
functions on $ or Pocketknife or something, you just
call functions in global scope or on HTML elements
and arrays. If that seems bad, don't use this.
**/
(function(_w, _d) {
"use strict";
/**
* Give things a foreach.
*/
NodeList.prototype.forEach =
HTMLCollection.prototype.forEach =
Array.prototype.forEach;
/**
* Pocketknife object, for accessing the update() function
*/
var Pocketknife = {
version: "2013.05.19"
};
/**
* bind Pocketknife object
*/
_w.Pocketknife = Pocketknife;
/**
* Also set up a "does thing exist?" evaluation function
*/
_w.exists = (function(undef) {
return function(thing) {
return (thing !== undef) && (thing !== null);
};
}());
/**
* And a simplified AJAX API. If called with a callback,
* it's async. If not, it's synchronouse, returning the
* data obtained through the request. BASED ON XHR2
*/
(function setupXHR(){
var doXHR = function(method,url,data,callback) {
var xhr = new XMLHttpRequest(),
async = exists(callback);
xhr.open(method, url, async);
if(async) {
xhr.withCredentials = true;
xhr.onreadystatechange = function() {
if(xhr.readyState===4 && (xhr.status===200||xhr.status===0)) {
callback(xhr);
}
};
}
if(data) {
var fd = new FormData(), name;
for(name in Object.keys(data)) {
fd.append(name,data[name]); }
data = fd; }
xhr.send(data);
if(!async) { return xhr.responseText; }
};
_w.get = function(url, callback) { return doXHR("GET", url, null, callback); };
_w.post = function(url, data, callback) { return doXHR("POST", url, data, callback); };
}(_w));
/**
* Extend window so that there is a "create" function that we
* can use instead of the limited document.createElement().
*/
_w.create = function(tagname, attributes, content) {
var element = _d.createElement(tagname),
property;
// element attributes
if(typeof attributes == "object") {
for(property in attributes) {
if(attributes.hasOwnProperty(property)) {
element.setAttribute(property, attributes[property]);
}
}
}
if (typeof attributes === "string") { content = attributes; }
if(content) { element.innerHTML = content; }
return element;
};
/**
* First off, we replace querySelector/querySelectorAll with "find".
* In part to homogenise the API, in part because NodeList is an
* utterly useless thing to work with, compared to arrays.
*/
var find = function(context, selector) {
var nodelist = context.querySelectorAll(selector),
elements = [];
if (nodelist.length === 0) {
return [];
}
if (nodelist.length === 1) {
return nodelist[0];
}
for(var i = 0, last = nodelist.length; i < last; i++) {
elements[i] = nodelist[i];
}
return elements;
};
/**
* The global implementation of "find" uses the current document.
* find(sel) and find(string, sel) work, the latter for not needing
* to first build an element manually.
*/
_w.find = function(selector, _) {
var context = _d;
if (_) {
context = create("section",selector);
selector = _;
}
return find(context, selector);
};
/*************************************************************************
The API, callable on both HTML elements and arrays:
find, html, position,
css, show, toggle,
classes().{add, remove, contains},
parent, add, replace, remove, clear,
get, set,
listen, listenOnce,
forEach
*************************************************************************/
var hiderule = "data-pocketknife-hidden";
var classesName = "内組";
/*************************************************************************/
/**
* No browsers offers a simple way to find out which functions will
* fire on an element, and for which event. Let's change that.
*/
var EventListeners = function(owner) {
this.owner = owner;
this.events = [];
this.listeners = {};
};
EventListeners.prototype = {
record: function(evt, fn) {
this.events.pushUnique(evt);
if (!exists(this.listeners[evt])) {
this.listeners[evt] = [];
}
this.listeners[evt].push(fn);
},
ignore: function(evt, fn) {
var pos = this.listeners[evt].indexOf(fn);
this.listeners[evt].splice(pos, 1);
}
};
/**
* Not all browsers support .classList, and even those that do
* don't let us decorate them to make them chaining functions,
* so: too bad, so sad, and we implement our own class list.
*/
var ClassList = function(owner) {
this.owner = owner;
var classAttr = owner.getAttribute("class");
this.classes = (!classAttr ? [] : classAttr.split(/\s+/));
this.length = 0;
};
ClassList.prototype = {
classes: [],
__update: function() {
if(this.classes.length === 0) { this.owner.removeAttribute("class"); }
else { this.owner.setAttribute("class", this.classes.join(" ")); }
this.length = this.classes.length;
},
add: function(clstring) {
if(this.classes.indexOf(clstring)===-1) {
this.classes.push(clstring);
}
this.__update();
return this.owner;
},
remove: function(clstring) {
var pos = this.classes.indexOf(clstring);
if(pos>-1) {
this.classes.splice(pos, 1);
this.__update();
}
return this.owner;
},
contains: function(clstring) {
return (this.classes.indexOf(clstring) !== -1);
},
item: function(idx) { return this.classes[idx]; }
};
/**
* We need to make sure that from a user perspective,
* the difference between "array" and "single element"
* is irrelevant. This means homogenizing the Array
* and HTMLElement prototypes. Yes, prototype pollution,
* because we want to install our library, not "use" it.
*/
(function($){
// public helper for "add only if not already added"
$.pushUnique = function(e) { if(this.indexOf(e) === -1) { this.push(e); }};
// public helper for "do any of the elements in this array pass this test"
$.test = function(f, strict) {
if (strict !== true) strict = false;
var i, len=this.length, t;
for(i=0; i<len; i++) {
t = f(this[i]);
if(strict && !t) return false;
if(t && !strict) return true;
}
return false;
};
// helper function for containment
$.contains = function(e) {
return this.indexOf(e) > -1;
};
// make forEach() a chaining function
$.forEach = function(forEach) {
return function(fn) {
forEach.call(this,fn);
return this;
};
}($.forEach);
// API implementation
$.classes = function() {
if(!this[classesName]) {
this[classesName] = {};
var arr = this;
["add","remove"].forEach(function(fn) {
arr[classesName][fn] = function() {
var input = arguments, classes;
arr.forEach(function(e) {
classes = e.classes();
classes[fn].apply(classes,input);
});
return arr;
};
});
this[classesName].contains = function() {
var input = arguments, classes;
return arr.test(function(e) {
classes = e.classes();
return classes.contains.apply(classes,input);
});
};
}
return this[classesName];
};
// functions that will end up applying only to the first element:
["add", "replace"].forEach(function(fn){
$[fn] = function() {
var e = this[0];
if (e)
e[fn].apply(e, arguments);
return this;
};
});
// functions that get applied to all elements, returning the array:
["show", "toggle", "set", "remove", "clear", "listen", "ignore", "listenOnce"].forEach(function(fn){
$[fn] = function() {
var input = arguments;
this.map(function(e) {
e[fn].apply(e, input);
});
return this;
};
});
// aggregating functions with the same aggregation shape:
["find", "parent", "query"].forEach(function(fn) {
$[fn] = function() {
var results = [];
this.forEach(function(e) {
e[fn].apply(e,arguments).forEach(function(r) {
results.pushUnique(r);
});
});
return results;
};
});
// functions that get applied to all elements, returning the array-of-results:
["position", "html", "css", "get"].forEach(function(fn) {
$[fn] = function() {
var result = [],
input = arguments,
forEachFn = (function(result, input) {
return function(e) {
result.push(e[fn].apply(e, input));
};
}(result, input));
this.forEach(forEachFn);
return result;
};
});
}(Array.prototype));
/**
* Extend the HTMLElement prototype.
*/
(function($, find){
// Array homogenization
$.length = 1;
// This lets us call forEach irrespective of whether we're
// dealing with an HTML element or an array of HTML elements:
$.forEach = function(fn) {
fn(this);
return this;
};
$.css = function(prop, val) {
if(typeof val === "string") {
this.style[prop] = val;
if (this.get("style") === "") {
this.set("style", "");
}
return this;
}
if(!val && typeof prop === "object") {
for(var p in prop) {
if(Object.hasOwnProperty(prop,p)) continue;
this.css(p,prop[p]); }
return this;
}
return getComputedStyle(this).getPropertyValue(prop) || this.style[prop];
};
$.qs = function(full) {
var n = this,
c = n.get("class"),
qs = (n.id ? n.localName + '#' + n.id : n.localName) + (c ? '.' + c.replace(/ /g,'.') : '');
if(!full) {
return qs;
}
var parent = this.parentNode;
while(parent.localName !== "html") {
qs = parent.qs() + " > " + qs;
parent = parent.parentNode;
}
return qs;
};
$.position = function() {
return this.getBoundingClientRect();
};
$.classes = function() {
if(!this[classesName]) {
this[classesName] = new ClassList(this);
}
return this[classesName];
};
$.show = function(yes) {
if(yes) { this.set(hiderule,false); }
else { this.set(hiderule,"true"); }
return this;
};
$.toggle = function() {
this.show(_w.exists(this.get(hiderule)));
return this;
};
$.html = function(html) {
if(_w.exists(html)) {
this.innerHTML = html;
return this;
}
return this.innerHTML;
};
$.parent = function(newParent) {
if(typeof newParent === "string") {
var all = find(_d, newParent), i;
if(all.length === 1) {
if (all.contains(this)) return all;
return false;
}
var ancestor = false;
for(i = all.length-1; i>0; i--) {
if(all[i].contains(this)) {
ancestor = all[i];
break;
}
}
return ancestor;
}
if(newParent) {
newParent.add(this);
return this;
}
return this.parentNode;
};
$.add = function(arg) {
if(typeof arg === "string") {
this.innerHTML += arg;
}
else {
var e, fn = function(a) { e.add(a); };
for(var i=0, last=arguments.length; i<last; i++) {
if(_w.exists(arguments[i])) {
if(arguments[i] instanceof Array) {
e = this;
arguments[i].forEach(fn);
} else { this.appendChild(arguments[i]); }
}
}
}
return this;
};
$.replace = function(o,n) {
if(typeof o === "string") {
var re = new RegExp(o,'g');
this.innerHTML = this.innerHTML.replace(re,n);
return this;
}
else if(_w.exists(o.parentNode) && _w.exists(n)) {
o.parentNode.replaceChild(n,o);
return n;
}
this.parentNode.replaceChild(o,this);
return o;
};
$.remove = function(c) {
if(typeof arg === "string") {
return this.replace(c,"");
}
// remove self
else if(!_w.exists(c)) { this.parentNode.removeChild(this); }
// remove child by number
else if(parseInt(c,10)==c) { this.removeChild(this.children[c]); }
// remove child by reference
else if(c.parentNode && c.parentNode === this) { this.removeChild(c); }
return this;
};
$.clear = function() {
this.innerHTML = "";
return this;
};
$.get = function(a) {
if(a == parseInt(a,10)) {
return this.children[a];
}
return this.getAttribute(a);
};
$.set = function(a,b) {
if(!_w.exists(b)) {
for(var prop in a) {
if(!Object.hasOwnProperty(a, prop)) {
this.setAttribute(prop, a[prop]);
}
}
}
else if (b === false) { this.removeAttribute(a); }
else { this.setAttribute(a, b); }
return this;
};
}(HTMLElement.prototype, find));
// IE has no HTMLDocument, so we have to use Document, instead.
var docPrototype = (_w.HTMLDocument? HTMLDocument.prototype : Document.prototype);
/**
* Extend the HTMLElement and HTMLDocument prototypes.
*/
[docPrototype, HTMLElement.prototype].forEach(function($) {
$.find = function(selector) {
return find(this, selector);
};
$.eventListeners = false;
$.__addAnEventListener = function(s,f,b) {
this.addEventListener(s,f,b);
if(!this.eventListeners) {
this.eventListeners = new EventListeners(this);
}
this.eventListeners.record(s,f);
};
$.__removeAnEventListener = function(s,f,b) {
this.removeEventListener(s,f,b);
this.eventListeners.ignore(s,f);
};
// better functions
$.listen = function(s, f) {
this.__addAnEventListener(s, f, false);
return this;
};
$.ignore = function(s, f) {
if (exists(f)) {
this.__removeAnEventListener(s, f, false);
}
else {
var entity = this;
var functions = this.eventListeners.listeners[s], i;
if (exists(functions)) {
for (i = functions.length - 1; i >= 0; i--) {
entity.ignore(s, functions[i]);
}
}
}
return this;
};
$.listenOnce = function(s, f) {
var e = this, _ = function() {
e.__removeAnEventListener(s, _, false);
f.call();
};
this.__addAnEventListener(s, _, false);
return this;
};
});
/**
* In order for show() to be reliable, we don't want to intercept style.display.
* Instead, we use a special data attribute that regulates visibility. Handy!
*/
(function(dataAttr){
var rules = ["display:none!important", "visibility:hidden!important","opacity:0!important"],
rule = "*["+dataAttr+"]{" + rules.join(";") + "}",
sheet = _w.create("style", {type: "text/css"}, rule);
_d.head.add(sheet);
}(hiderule));
/**
* Final steps
*/
(function(){
/**
* top-level handling function for functions that are normally
* added using document.addEventListener("DOMContentLoaded"...)
* or jQuery's $.ready(...)
*/
_w.schedule = function(fn) {
if (_w.ready) { return fn(); }
_d.listenOnce("DOMContentLoaded",fn);
};
// relied on by the above function
var rd = function() { _w.ready = true; };
if (["complete","loaded","interactive"].indexOf(_d.readyState) !== -1) { rd(); }
else { _d.listenOnce("DOMContentLoaded", rd); }
/**
* For DOM manipulation, we really want 'head' and 'body' to just
* be global variables.
*/
_w.schedule(function() { _w.head = document.head; });
_w.schedule(function() { _w.body = document.body; });
// This is the worst thing: an IE hack. For some reason,
// IE's "p" has a .clear property, for no good reason.
delete HTMLParagraphElement.prototype.clear;
}());
}(window, document));