|
1 |
| -/*global $, $$, $parent, $live */ |
| 1 | +/*global qs, qsa, $parent, $live */ |
2 | 2 |
|
3 | 3 | (function (window) {
|
4 | 4 | 'use strict';
|
|
18 | 18 | this.ENTER_KEY = 13;
|
19 | 19 | this.ESCAPE_KEY = 27;
|
20 | 20 |
|
21 |
| - this.$todoList = $$('#todo-list'); |
22 |
| - this.$todoItemCounter = $$('#todo-count'); |
23 |
| - this.$clearCompleted = $$('#clear-completed'); |
24 |
| - this.$main = $$('#main'); |
25 |
| - this.$footer = $$('#footer'); |
26 |
| - this.$toggleAll = $$('#toggle-all'); |
27 |
| - this.$newTodo = $$('#new-todo'); |
| 21 | + this.$todoList = qs('#todo-list'); |
| 22 | + this.$todoItemCounter = qs('#todo-count'); |
| 23 | + this.$clearCompleted = qs('#clear-completed'); |
| 24 | + this.$main = qs('#main'); |
| 25 | + this.$footer = qs('#footer'); |
| 26 | + this.$toggleAll = qs('#toggle-all'); |
| 27 | + this.$newTodo = qs('#new-todo'); |
28 | 28 | }
|
29 | 29 |
|
30 | 30 | View.prototype._removeItem = function (id) {
|
31 |
| - var elem = $$('[data-id="' + id + '"]'); |
| 31 | + var elem = qs('[data-id="' + id + '"]'); |
32 | 32 |
|
33 | 33 | if (elem) {
|
34 | 34 | this.$todoList.removeChild(elem);
|
|
41 | 41 | };
|
42 | 42 |
|
43 | 43 | View.prototype._setFilter = function (currentPage) {
|
44 |
| - // Remove all other selected states. We loop through all of them in case the |
45 |
| - // UI gets in a funky state with two selected. |
46 |
| - $('#filters .selected').forEach(function (item) { |
47 |
| - item.className = ''; |
48 |
| - }); |
49 |
| - |
50 |
| - $('#filters [href="#/' + currentPage + '"]').forEach(function (item) { |
51 |
| - item.className = 'selected'; |
52 |
| - }); |
| 44 | + qs('#filters .selected').className = ''; |
| 45 | + qs('#filters [href="#/' + currentPage + '"]').className = 'selected'; |
53 | 46 | };
|
54 | 47 |
|
55 | 48 | View.prototype._elementComplete = function (id, completed) {
|
56 |
| - var listItem = $$('[data-id="' + id + '"]'); |
| 49 | + var listItem = qs('[data-id="' + id + '"]'); |
57 | 50 |
|
58 | 51 | if (!listItem) {
|
59 | 52 | return;
|
|
62 | 55 | listItem.className = completed ? 'completed' : '';
|
63 | 56 |
|
64 | 57 | // In case it was toggled from an event and not by clicking the checkbox
|
65 |
| - listItem.querySelector('input').checked = completed; |
| 58 | + qs('input', listItem).checked = completed; |
66 | 59 | };
|
67 | 60 |
|
68 | 61 | View.prototype._editItem = function (id, title) {
|
69 |
| - var listItem = $$('[data-id="' + id + '"]'); |
| 62 | + var listItem = qs('[data-id="' + id + '"]'); |
70 | 63 |
|
71 | 64 | if (!listItem) {
|
72 | 65 | return;
|
|
83 | 76 | };
|
84 | 77 |
|
85 | 78 | View.prototype._editItemDone = function (id, title) {
|
86 |
| - var listItem = $$('[data-id="' + id + '"]'); |
| 79 | + var listItem = qs('[data-id="' + id + '"]'); |
87 | 80 |
|
88 | 81 | if (!listItem) {
|
89 | 82 | return;
|
90 | 83 | }
|
91 | 84 |
|
92 |
| - var input = listItem.querySelector('input.edit'); |
| 85 | + var input = qs('input.edit', listItem); |
93 | 86 | listItem.removeChild(input);
|
94 | 87 |
|
95 | 88 | listItem.className = listItem.className.replace('editing', '');
|
96 | 89 |
|
97 |
| - listItem.querySelectorAll('label').forEach(function (label) { |
| 90 | + qsa('label', listItem).forEach(function (label) { |
98 | 91 | label.textContent = title;
|
99 | 92 | });
|
100 | 93 | };
|
101 | 94 |
|
102 | 95 | View.prototype.render = function (viewCmd, parameter) {
|
103 | 96 | var that = this;
|
104 | 97 | var viewCommands = {
|
105 |
| - showEntries: function () { |
106 |
| - that.$todoList.innerHTML = that.template.show(parameter); |
107 |
| - }, |
108 |
| - removeItem: function () { |
109 |
| - that._removeItem(parameter); |
110 |
| - }, |
111 |
| - updateElementCount: function () { |
112 |
| - that.$todoItemCounter.innerHTML = that.template.itemCounter(parameter); |
113 |
| - }, |
114 |
| - clearCompletedButton: function () { |
115 |
| - that._clearCompletedButton(parameter.completed, parameter.visible); |
116 |
| - }, |
117 |
| - contentBlockVisibility: function () { |
118 |
| - that.$main.style.display = that.$footer.style.display = parameter.visible ? 'block' : 'none'; |
119 |
| - }, |
120 |
| - toggleAll: function () { |
121 |
| - that.$toggleAll.checked = parameter.checked; |
122 |
| - }, |
123 |
| - setFilter: function () { |
124 |
| - that._setFilter(parameter); |
125 |
| - }, |
126 |
| - clearNewTodo: function () { |
127 |
| - that.$newTodo.value = ''; |
128 |
| - }, |
129 |
| - elementComplete: function () { |
130 |
| - that._elementComplete(parameter.id, parameter.completed); |
131 |
| - }, |
132 |
| - editItem: function () { |
133 |
| - that._editItem(parameter.id, parameter.title); |
134 |
| - }, |
135 |
| - editItemDone: function () { |
136 |
| - that._editItemDone(parameter.id, parameter.title); |
137 |
| - } |
138 |
| - }; |
| 98 | + showEntries: function () { |
| 99 | + that.$todoList.innerHTML = that.template.show(parameter); |
| 100 | + }, |
| 101 | + removeItem: function () { |
| 102 | + that._removeItem(parameter); |
| 103 | + }, |
| 104 | + updateElementCount: function () { |
| 105 | + that.$todoItemCounter.innerHTML = that.template.itemCounter(parameter); |
| 106 | + }, |
| 107 | + clearCompletedButton: function () { |
| 108 | + that._clearCompletedButton(parameter.completed, parameter.visible); |
| 109 | + }, |
| 110 | + contentBlockVisibility: function () { |
| 111 | + that.$main.style.display = that.$footer.style.display = parameter.visible ? 'block' : 'none'; |
| 112 | + }, |
| 113 | + toggleAll: function () { |
| 114 | + that.$toggleAll.checked = parameter.checked; |
| 115 | + }, |
| 116 | + setFilter: function () { |
| 117 | + that._setFilter(parameter); |
| 118 | + }, |
| 119 | + clearNewTodo: function () { |
| 120 | + that.$newTodo.value = ''; |
| 121 | + }, |
| 122 | + elementComplete: function () { |
| 123 | + that._elementComplete(parameter.id, parameter.completed); |
| 124 | + }, |
| 125 | + editItem: function () { |
| 126 | + that._editItem(parameter.id, parameter.title); |
| 127 | + }, |
| 128 | + editItemDone: function () { |
| 129 | + that._editItemDone(parameter.id, parameter.title); |
| 130 | + } |
| 131 | + }; |
139 | 132 |
|
140 | 133 | viewCommands[viewCmd]();
|
141 | 134 | };
|
142 | 135 |
|
143 |
| - View.prototype._itemIdForEvent = function (e) { |
144 |
| - var element = e.target; |
| 136 | + View.prototype._itemId = function (element) { |
145 | 137 | var li = $parent(element, 'li');
|
146 |
| - var id = li.dataset.id; |
147 |
| - |
148 |
| - return id; |
| 138 | + return li.dataset.id; |
149 | 139 | };
|
150 | 140 |
|
151 | 141 | View.prototype._bindItemEditDone = function (handler) {
|
152 |
| - $live('#todo-list li .edit', 'blur', function (e) { |
153 |
| - var input = e.target; |
154 |
| - var id = this._itemIdForEvent(e); |
155 |
| - |
156 |
| - if (!input.dataset.iscanceled) { |
| 142 | + var that = this; |
| 143 | + $live('#todo-list li .edit', 'blur', function () { |
| 144 | + if (!this.dataset.iscanceled) { |
157 | 145 | handler({
|
158 |
| - id: id, |
159 |
| - title: input.value |
| 146 | + id: that._itemId(this), |
| 147 | + title: this.value |
160 | 148 | });
|
161 | 149 | }
|
162 |
| - }.bind(this)); |
| 150 | + }); |
163 | 151 |
|
164 |
| - $live('#todo-list li .edit', 'keypress', function (e) { |
165 |
| - var input = e.target; |
166 |
| - if (e.keyCode === this.ENTER_KEY) { |
| 152 | + $live('#todo-list li .edit', 'keypress', function (event) { |
| 153 | + if (event.keyCode === that.ENTER_KEY) { |
167 | 154 | // Remove the cursor from the input when you hit enter just like if it
|
168 | 155 | // were a real form
|
169 |
| - input.blur(); |
| 156 | + this.blur(); |
170 | 157 | }
|
171 |
| - }.bind(this)); |
| 158 | + }); |
172 | 159 | };
|
173 | 160 |
|
174 | 161 | View.prototype._bindItemEditCancel = function (handler) {
|
175 |
| - $live('#todo-list li .edit', 'keyup', function (e) { |
176 |
| - var input = e.target; |
177 |
| - var id = this._itemIdForEvent(e); |
178 |
| - |
179 |
| - if (e.keyCode === this.ESCAPE_KEY) { |
180 |
| - |
181 |
| - input.dataset.iscanceled = true; |
182 |
| - input.blur(); |
| 162 | + var that = this; |
| 163 | + $live('#todo-list li .edit', 'keyup', function (event) { |
| 164 | + if (event.keyCode === that.ESCAPE_KEY) { |
| 165 | + this.dataset.iscanceled = true; |
| 166 | + this.blur(); |
183 | 167 |
|
184 |
| - handler({id: id}); |
| 168 | + handler({id: that._itemId(this)}); |
185 | 169 | }
|
186 |
| - }.bind(this)); |
| 170 | + }); |
187 | 171 | };
|
188 | 172 |
|
189 | 173 | View.prototype.bind = function (event, handler) {
|
| 174 | + var that = this; |
190 | 175 | if (event === 'newTodo') {
|
191 |
| - this.$newTodo.addEventListener('change', function () { |
192 |
| - handler(this.$newTodo.value); |
193 |
| - }.bind(this)); |
| 176 | + that.$newTodo.addEventListener('change', function () { |
| 177 | + handler(that.$newTodo.value); |
| 178 | + }); |
194 | 179 |
|
195 | 180 | } else if (event === 'removeCompleted') {
|
196 |
| - this.$clearCompleted.addEventListener('click', function () { |
| 181 | + that.$clearCompleted.addEventListener('click', function () { |
197 | 182 | handler();
|
198 |
| - }.bind(this)); |
| 183 | + }); |
199 | 184 |
|
200 | 185 | } else if (event === 'toggleAll') {
|
201 |
| - this.$toggleAll.addEventListener('click', function (e) { |
202 |
| - var input = e.target; |
203 |
| - |
204 |
| - handler({completed: input.checked}); |
205 |
| - }.bind(this)); |
| 186 | + that.$toggleAll.addEventListener('click', function () { |
| 187 | + handler({completed: this.checked}); |
| 188 | + }); |
206 | 189 |
|
207 | 190 | } else if (event === 'itemEdit') {
|
208 |
| - $live('#todo-list li label', 'dblclick', function (e) { |
209 |
| - var id = this._itemIdForEvent(e); |
210 |
| - |
211 |
| - handler({id: id}); |
212 |
| - }.bind(this)); |
| 191 | + $live('#todo-list li label', 'dblclick', function () { |
| 192 | + handler({id: that._itemId(this)}); |
| 193 | + }); |
213 | 194 |
|
214 | 195 | } else if (event === 'itemRemove') {
|
215 |
| - $live('#todo-list .destroy', 'click', function (e) { |
216 |
| - var id = this._itemIdForEvent(e); |
217 |
| - |
218 |
| - handler({id: id}); |
219 |
| - }.bind(this)); |
| 196 | + $live('#todo-list .destroy', 'click', function () { |
| 197 | + handler({id: that._itemId(this)}); |
| 198 | + }); |
220 | 199 |
|
221 | 200 | } else if (event === 'itemToggle') {
|
222 |
| - $live('#todo-list .toggle', 'click', function (e) { |
223 |
| - var input = e.target; |
224 |
| - var id = this._itemIdForEvent(e); |
225 |
| - |
226 |
| - handler({id: id, completed: input.checked}); |
227 |
| - }.bind(this)); |
| 201 | + $live('#todo-list .toggle', 'click', function () { |
| 202 | + handler({ |
| 203 | + id: that._itemId(this), |
| 204 | + completed: this.checked |
| 205 | + }); |
| 206 | + }); |
228 | 207 |
|
229 | 208 | } else if (event === 'itemEditDone') {
|
230 |
| - this._bindItemEditDone(handler); |
| 209 | + that._bindItemEditDone(handler); |
231 | 210 |
|
232 | 211 | } else if (event === 'itemEditCancel') {
|
233 |
| - this._bindItemEditCancel(handler); |
| 212 | + that._bindItemEditCancel(handler); |
234 | 213 | }
|
235 | 214 | };
|
236 | 215 |
|
|
0 commit comments