1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | !function(bindings) {
|
7 | var hasOwn = Object.prototype.hasOwnProperty
|
8 | , slice = Array.prototype.slice
|
9 |
|
10 | bindingEvery.once =
|
11 | emitForm.once =
|
12 | bindingFn.once =
|
13 | bindingOn.once =
|
14 | bindingsEach.raw = bindingsEach.once =
|
15 | true
|
16 |
|
17 | bindings.every = bindingEvery
|
18 | function bindingEvery(el, list, attrName) {
|
19 | var len = 0
|
20 | , data = this
|
21 | , parent = el.parentNode
|
22 | , comm = document.createComment("every " + (list.name || list.length))
|
23 |
|
24 | parent.replaceChild(comm, el)
|
25 |
|
26 | if (list) {
|
27 | if (typeof list === "string") {
|
28 | data.model.on("change:" + list, render)
|
29 | render()
|
30 | } else if (list.eachLive) {
|
31 | list.eachLive(add, remove)
|
32 | } else {
|
33 | comm.render = render
|
34 | render()
|
35 | }
|
36 | }
|
37 | return true
|
38 |
|
39 | function render() {
|
40 | for (; len; len--) {
|
41 | El.kill(comm.previousSibling)
|
42 | }
|
43 | Object.each(typeof list === "string" ? data.model.get(list) : list, add)
|
44 | }
|
45 |
|
46 | function add(item, i) {
|
47 | len++
|
48 | var up
|
49 | , clone = el.cloneNode(true)
|
50 | , scope = El.scope(clone, data)
|
51 | scope.i = i
|
52 | scope[attrName || "item"] = item
|
53 | El.append(parent, clone, comm)
|
54 | El.render(clone, scope)
|
55 | if (typeof item.on === "function") {
|
56 | item.on("change", up = El.render.bind(clone, clone))
|
57 | El.on(clone, "kill", function() {
|
58 | item.off("change", up)
|
59 | })
|
60 | }
|
61 | }
|
62 |
|
63 | function remove(pos) {
|
64 | for (var el = comm, i = pos + 1; i--; ) {
|
65 | el = el.previousSibling
|
66 | }
|
67 | El.kill(el)
|
68 | }
|
69 | }
|
70 |
|
71 | bindings.fn = bindingFn
|
72 | function bindingFn(el, fn) {
|
73 | return fn.apply(el, slice.call(arguments, 3))
|
74 | }
|
75 |
|
76 | bindings["if"] = bindingsIf
|
77 | function bindingsIf(el, enabled) {
|
78 | var parent = el.parentNode
|
79 | , scope = this
|
80 | if (enabled) {
|
81 | parent || el._ifComm && el._ifComm.parentNode.replaceChild(el, el._ifComm)
|
82 | } else {
|
83 | if (parent) {
|
84 | if (!el._ifComm) {
|
85 | El.on(el, "kill", El.kill.bind(el, el._ifComm = document.createComment("if")))
|
86 | el._ifComm.render = function() {
|
87 | El.render(el, scope)
|
88 | }
|
89 | }
|
90 | parent.replaceChild(el._ifComm, el)
|
91 | }
|
92 | return true
|
93 | }
|
94 | }
|
95 |
|
96 | bindings.is = function bindingIs(node, model, path, list, state) {
|
97 | var i, match, val
|
98 | , scope = this
|
99 | if (typeof model === "string") {
|
100 | state = list
|
101 | list = path
|
102 | path = model
|
103 | model = scope.model
|
104 | }
|
105 | if (model && path) {
|
106 | match = val = state || model.get(path)
|
107 | if (list) {
|
108 | if (!Array.isArray(list)) {
|
109 | list = list.split(",")
|
110 | }
|
111 | i = list.length & -2
|
112 |
|
113 | for (; i > -1; i -= 2) {
|
114 | if (i == 0 || list[i - 1] == "" + val || +list[i - 1] <= val) {
|
115 | match = list[i]
|
116 | break
|
117 | }
|
118 | }
|
119 | }
|
120 | El.rmClass(node, scope["_is-" + path])
|
121 | El.addClass(node, scope["_is-" + path] = match && "is-" + match)
|
122 | }
|
123 | }
|
124 |
|
125 | bindings.on = bindingOn
|
126 | function bindingOn(el, ev, fn, a1, a2, a3, a4, a5) {
|
127 | El.on(el, ev, typeof fn == "string" ? function(e) {
|
128 | View.emit(fn, e, el, a1, a2, a3, a4, a5)
|
129 | } : fn)
|
130 | }
|
131 |
|
132 | bindings.emitForm = emitForm
|
133 | function emitForm(el, ev, a1, a2, a3, a4) {
|
134 | El.on(el, "submit", function(e) {
|
135 | var data = El.val(el)
|
136 | View.emit(ev, e, data, a1, a2, a3, a4)
|
137 | return Event.stop(e)
|
138 | })
|
139 | }
|
140 |
|
141 | function getChilds(node) {
|
142 | var child
|
143 | , childs = node._childs
|
144 | if (!childs) {
|
145 | for (node._childs = childs = []; child = node.firstChild;) {
|
146 | childs.push(child);
|
147 | node.removeChild(child)
|
148 | }
|
149 | }
|
150 | return childs
|
151 | }
|
152 |
|
153 | bindings.each = bindingsEach
|
154 |
|
155 | function bindingsEach(el, expr) {
|
156 | var node = el
|
157 | , child = getChilds(node)[0]
|
158 | , match = /^\s*(\w+) in (\w*)(.*)/.exec(expr)
|
159 | , fn = "with(data){var out=[],loop={i:0,offset:0},_1,_2=" + match[2]
|
160 | + match[3].replace(/ (limit|offset):\s*(\d+)/ig, ";loop.$1=$2")
|
161 | + ";if(_2)for(_1 in _2)if(hasOwn.call(_2,_1)&&!(loop.offset&&loop.offset--)){"
|
162 | + "loop.i++;"
|
163 | + "if(loop.limit&&loop.i-loop.offset>loop.limit)break;"
|
164 | + "var clone=el.cloneNode(true)"
|
165 | + ",scope=El.scope(clone,data);"
|
166 | + "scope.loopKey=loop.key=_1;"
|
167 | + "scope.loop=loop;"
|
168 | + "scope." + match[1] + "=_2[_1];"
|
169 | + "out.push(clone);"
|
170 | + "};return out}"
|
171 |
|
172 | var childs = Function("hasOwn,el,data", fn)(hasOwn, child, this)
|
173 |
|
174 | El.append(El.empty(node), childs)
|
175 | El.render(node)
|
176 | return node
|
177 | }
|
178 |
|
179 | bindings.focus = function(el) {
|
180 | el.focus()
|
181 | }
|
182 |
|
183 | bindings.href = function(el, url) {
|
184 | if (url) {
|
185 | var chr = url.charAt(0)
|
186 | el.href = chr === "+" || chr === "%" ? "#" + View.url(url) : url
|
187 | }
|
188 | }
|
189 | }(El.bindings)
|
190 |
|