This repository has been archived by the owner on Dec 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
base.js
129 lines (91 loc) · 2.88 KB
/
base.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
/*
elements
*/"use strict"
var prime = require("prime")
var forEach = require("mout/array/forEach"),
map = require("mout/array/map"),
filter = require("mout/array/filter"),
every = require("mout/array/every"),
some = require("mout/array/some")
// uniqueID
var index = 0,
__dc = document.__counter,
counter = document.__counter = (__dc ? parseInt(__dc, 36) + 1 : 0).toString(36),
key = "uid:" + counter
var uniqueID = function(n){
if (n === window) return "window"
if (n === document) return "document"
if (n === document.documentElement) return "html"
return n[key] || (n[key] = (index++).toString(36))
}
var instances = {}
// elements prime
var $ = prime({constructor: function $(n, context){
if (n == null) return (this && this.constructor === $) ? new Elements : null
var self, uid
if (n.constructor !== Elements){
self = new Elements
if (typeof n === "string"){
if (!self.search) return null
self[self.length++] = context || document
return self.search(n)
}
if (n.nodeType || n === window){
self[self.length++] = n
} else if (n.length){
// this could be an array, or any object with a length attribute,
// including another instance of elements from another interface.
var uniques = {}
for (var i = 0, l = n.length; i < l; i++){ // perform elements flattening
var nodes = $(n[i], context)
if (nodes && nodes.length) for (var j = 0, k = nodes.length; j < k; j++){
var node = nodes[j]
uid = uniqueID(node)
if (!uniques[uid]){
self[self.length++] = node
uniques[uid] = true
}
}
}
}
} else {
self = n
}
if (!self.length) return null
// when length is 1 always use the same elements instance
if (self.length === 1){
uid = uniqueID(self[0])
return instances[uid] || (instances[uid] = self)
}
return self
}})
var Elements = prime({
inherits: $,
constructor: function Elements(){
this.length = 0
},
unlink: function(){
return this.map(function(node){
delete instances[uniqueID(node)]
return node
})
},
// methods
forEach: function(method, context){
forEach(this, method, context)
return this
},
map: function(method, context){
return map(this, method, context)
},
filter: function(method, context){
return filter(this, method, context)
},
every: function(method, context){
return every(this, method, context)
},
some: function(method, context){
return some(this, method, context)
}
})
module.exports = $