| 1 |
/* base.js */ |
| 2 |
(function () { |
| 3 |
'use strict'; |
| 4 |
|
| 5 |
/* jshint ignore:start */ |
| 6 |
// Underscore's Template Module |
| 7 |
// Courtesy of underscorejs.org |
| 8 |
var _ = (function (_) { |
| 9 |
_.defaults = function (object) { |
| 10 |
if (!object) { |
| 11 |
return object; |
| 12 |
} |
| 13 |
for (var argsIndex = 1, argsLength = arguments.length; argsIndex < argsLength; argsIndex++) { |
| 14 |
var iterable = arguments[argsIndex]; |
| 15 |
if (iterable) { |
| 16 |
for (var key in iterable) { |
| 17 |
if (object[key] == null) { |
| 18 |
object[key] = iterable[key]; |
| 19 |
} |
| 20 |
} |
| 21 |
} |
| 22 |
} |
| 23 |
return object; |
| 24 |
} |
| 25 |
|
| 26 |
// By default, Underscore uses ERB-style template delimiters, change the |
| 27 |
// following template settings to use alternative delimiters. |
| 28 |
_.templateSettings = { |
| 29 |
evaluate : /<%([\s\S]+?)%>/g, |
| 30 |
interpolate : /<%=([\s\S]+?)%>/g, |
| 31 |
escape : /<%-([\s\S]+?)%>/g |
| 32 |
}; |
| 33 |
|
| 34 |
// When customizing `templateSettings`, if you don't want to define an |
| 35 |
// interpolation, evaluation or escaping regex, we need one that is |
| 36 |
// guaranteed not to match. |
| 37 |
var noMatch = /(.)^/; |
| 38 |
|
| 39 |
// Certain characters need to be escaped so that they can be put into a |
| 40 |
// string literal. |
| 41 |
var escapes = { |
| 42 |
"'": "'", |
| 43 |
'\\': '\\', |
| 44 |
'\r': 'r', |
| 45 |
'\n': 'n', |
| 46 |
'\t': 't', |
| 47 |
'\u2028': 'u2028', |
| 48 |
'\u2029': 'u2029' |
| 49 |
}; |
| 50 |
|
| 51 |
var escaper = /\\|'|\r|\n|\t|\u2028|\u2029/g; |
| 52 |
|
| 53 |
// JavaScript micro-templating, similar to John Resig's implementation. |
| 54 |
// Underscore templating handles arbitrary delimiters, preserves whitespace, |
| 55 |
// and correctly escapes quotes within interpolated code. |
| 56 |
_.template = function(text, data, settings) { |
| 57 |
var render; |
| 58 |
settings = _.defaults({}, settings, _.templateSettings); |
| 59 |
console.log(text, data) |
| 60 |
|
| 61 |
// Combine delimiters into one regular expression via alternation. |
| 62 |
var matcher = new RegExp([ |
| 63 |
(settings.escape || noMatch).source, |
| 64 |
(settings.interpolate || noMatch).source, |
| 65 |
(settings.evaluate || noMatch).source |
| 66 |
].join('|') + '|$', 'g'); |
| 67 |
|
| 68 |
// Compile the template source, escaping string literals appropriately. |
| 69 |
var index = 0; |
| 70 |
var source = "__p+='"; |
| 71 |
text.replace(matcher, function(match, escape, interpolate, evaluate, offset) { |
| 72 |
source += text.slice(index, offset) |
| 73 |
.replace(escaper, function(match) { return '\\' + escapes[match]; }); |
| 74 |
|
| 75 |
if (escape) { |
| 76 |
source += "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'"; |
| 77 |
} |
| 78 |
if (interpolate) { |
| 79 |
source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'"; |
| 80 |
} |
| 81 |
if (evaluate) { |
| 82 |
source += "';\n" + evaluate + "\n__p+='"; |
| 83 |
} |
| 84 |
index = offset + match.length; |
| 85 |
return match; |
| 86 |
}); |
| 87 |
source += "';\n"; |
| 88 |
|
| 89 |
// If a variable is not specified, place data values in local scope. |
| 90 |
if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n'; |
| 91 |
|
| 92 |
source = "var __t,__p='',__j=Array.prototype.join," + |
| 93 |
"print=function(){__p+=__j.call(arguments,'');};\n" + |
| 94 |
source + "return __p;\n"; |
| 95 |
|
| 96 |
try { |
| 97 |
render = new Function(settings.variable || 'obj', '_', source); |
| 98 |
} catch (e) { |
| 99 |
e.source = source; |
| 100 |
throw e; |
| 101 |
} |
| 102 |
|
| 103 |
if (data) return render(data, _); |
| 104 |
var template = function(data) { |
| 105 |
return render.call(this, data, _); |
| 106 |
}; |
| 107 |
|
| 108 |
// Provide the compiled function source as a convenience for precompilation. |
| 109 |
template.source = 'function(' + (settings.variable || 'obj') + '){\n' + source + '}'; |
| 110 |
|
| 111 |
return template; |
| 112 |
}; |
| 113 |
|
| 114 |
return _; |
| 115 |
})({}); |
| 116 |
})(); |
| 117 |
|
| 118 |
|
| 119 |
/** helpers.js **/ |
| 120 |
(function (window) { |
| 121 |
'use strict'; |
| 122 |
|
| 123 |
// Get element(s) by CSS selector: |
| 124 |
window.qs = function (selector, scope) { |
| 125 |
return (scope || document).querySelector(selector); |
| 126 |
}; |
| 127 |
window.qsa = function (selector, scope) { |
| 128 |
return (scope || document).querySelectorAll(selector); |
| 129 |
}; |
| 130 |
|
| 131 |
// addEventListener wrapper: |
| 132 |
window.$on = function (target, type, callback, useCapture) { |
| 133 |
target.addEventListener(type, callback, !!useCapture); |
| 134 |
}; |
| 135 |
|
| 136 |
// Attach a handler to event for all elements that match the selector, |
| 137 |
// now or in the future, based on a root element |
| 138 |
window.$delegate = function (target, selector, type, handler) { |
| 139 |
function dispatchEvent(event) { |
| 140 |
var targetElement = event.target; |
| 141 |
var potentialElements = window.qsa(selector, target); |
| 142 |
var hasMatch = Array.prototype.indexOf.call(potentialElements, targetElement) >= 0; |
| 143 |
|
| 144 |
if (hasMatch) { |
| 145 |
handler.call(targetElement, event); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
// https://developer.mozilla.org/en-US/docs/Web/Events/blur |
| 150 |
var useCapture = type === 'blur' || type === 'focus'; |
| 151 |
|
| 152 |
window.$on(target, type, dispatchEvent, useCapture); |
| 153 |
}; |
| 154 |
|
| 155 |
// Find the element's parent with the given tag name: |
| 156 |
// $parent(qs('a'), 'div'); |
| 157 |
window.$parent = function (element, tagName) { |
| 158 |
if (!element.parentNode) { |
| 159 |
return; |
| 160 |
} |
| 161 |
if (element.parentNode.tagName.toLowerCase() === tagName.toLowerCase()) { |
| 162 |
return element.parentNode; |
| 163 |
} |
| 164 |
return window.$parent(element.parentNode, tagName); |
| 165 |
}; |
| 166 |
|
| 167 |
// Allow for looping on nodes by chaining: |
| 168 |
// qsa('.foo').forEach(function () {}) |
| 169 |
NodeList.prototype.forEach = Array.prototype.forEach; |
| 170 |
})(window); |
| 171 |
|
| 172 |
|
| 173 |
/* store.js */ |
| 174 |
(function (window) { |
| 175 |
'use strict'; |
| 176 |
|
| 177 |
/** |
| 178 |
* Creates a new client side storage object and will create an empty |
| 179 |
* collection if no collection already exists. |
| 180 |
* |
| 181 |
* @param {string} name The name of our DB we want to use |
| 182 |
* @param {function} callback Our fake DB uses callbacks because in |
| 183 |
* real life you probably would be making AJAX calls |
| 184 |
*/ |
| 185 |
function Store(name, callback) { |
| 186 |
callback = callback || function () {}; |
| 187 |
|
| 188 |
this._dbName = name; |
| 189 |
window.todoStorage = {}; |
| 190 |
|
| 191 |
if (!window.todoStorage[name]) { |
| 192 |
var data = { |
| 193 |
todos: [] |
| 194 |
}; |
| 195 |
|
| 196 |
window.todoStorage[name] = JSON.stringify(data); |
| 197 |
} |
| 198 |
|
| 199 |
callback.call(this, JSON.parse(window.todoStorage[name])); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Finds items based on a query given as a JS object |
| 204 |
* |
| 205 |
* @param {object} query The query to match against (i.e. {foo: 'bar'}) |
| 206 |
* @param {function} callback The callback to fire when the query has |
| 207 |
* completed running |
| 208 |
* |
| 209 |
* @example |
| 210 |
* db.find({foo: 'bar', hello: 'world'}, function (data) { |
| 211 |
* // data will return any items that have foo: bar and |
| 212 |
* // hello: world in their properties |
| 213 |
* }); |
| 214 |
*/ |
| 215 |
Store.prototype.find = function (query, callback) { |
| 216 |
if (!callback) { |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
var todos = JSON.parse(window.todoStorage[this._dbName]).todos; |
| 221 |
|
| 222 |
callback.call(this, todos.filter(function (todo) { |
| 223 |
for (var q in query) { |
| 224 |
if (query[q] !== todo[q]) { |
| 225 |
return false; |
| 226 |
} |
| 227 |
} |
| 228 |
return true; |
| 229 |
})); |
| 230 |
}; |
| 231 |
|
| 232 |
/** |
| 233 |
* Will retrieve all data from the collection |
| 234 |
* |
| 235 |
* @param {function} callback The callback to fire upon retrieving data |
| 236 |
*/ |
| 237 |
Store.prototype.findAll = function (callback) { |
| 238 |
callback = callback || function () {}; |
| 239 |
callback.call(this, JSON.parse(window.todoStorage[this._dbName]).todos); |
| 240 |
}; |
| 241 |
|
| 242 |
/** |
| 243 |
* Will save the given data to the DB. If no item exists it will create a new |
| 244 |
* item, otherwise it'll simply update an existing item's properties |
| 245 |
* |
| 246 |
* @param {object} updateData The data to save back into the DB |
| 247 |
* @param {function} callback The callback to fire after saving |
| 248 |
* @param {number} id An optional param to enter an ID of an item to update |
| 249 |
*/ |
| 250 |
Store.prototype.save = function (updateData, callback, id) { |
| 251 |
var data = JSON.parse(window.todoStorage[this._dbName]); |
| 252 |
var todos = data.todos; |
| 253 |
|
| 254 |
callback = callback || function () {}; |
| 255 |
|
| 256 |
// If an ID was actually given, find the item and update each property |
| 257 |
if (id) { |
| 258 |
for (var i = 0; i < todos.length; i++) { |
| 259 |
if (todos[i].id === id) { |
| 260 |
for (var key in updateData) { |
| 261 |
todos[i][key] = updateData[key]; |
| 262 |
} |
| 263 |
break; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
window.todoStorage[this._dbName] = JSON.stringify(data); |
| 268 |
callback.call(this, todos); |
| 269 |
} else { |
| 270 |
// Generate an ID |
| 271 |
updateData.id = new Date().getTime(); |
| 272 |
|
| 273 |
todos.push(updateData); |
| 274 |
window.todoStorage[this._dbName] = JSON.stringify(data); |
| 275 |
callback.call(this, [updateData]); |
| 276 |
} |
| 277 |
}; |
| 278 |
|
| 279 |
/** |
| 280 |
* Will remove an item from the Store based on its ID |
| 281 |
* |
| 282 |
* @param {number} id The ID of the item you want to remove |
| 283 |
* @param {function} callback The callback to fire after saving |
| 284 |
*/ |
| 285 |
Store.prototype.remove = function (id, callback) { |
| 286 |
var data = JSON.parse(window.todoStorage[this._dbName]); |
| 287 |
var todos = data.todos; |
| 288 |
|
| 289 |
for (var i = 0; i < todos.length; i++) { |
| 290 |
if (todos[i].id == id) { |
| 291 |
todos.splice(i, 1); |
| 292 |
break; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
window.todoStorage[this._dbName] = JSON.stringify(data); |
| 297 |
callback.call(this, todos); |
| 298 |
}; |
| 299 |
|
| 300 |
/** |
| 301 |
* Will drop all storage and start fresh |
| 302 |
* |
| 303 |
* @param {function} callback The callback to fire after dropping the data |
| 304 |
*/ |
| 305 |
Store.prototype.drop = function (callback) { |
| 306 |
var data = {todos: []}; |
| 307 |
window.todoStorage[this._dbName] = JSON.stringify(data); |
| 308 |
callback.call(this, data.todos); |
| 309 |
}; |
| 310 |
|
| 311 |
// Export to window |
| 312 |
window.app = window.app || {}; |
| 313 |
window.app.Store = Store; |
| 314 |
})(window); |
| 315 |
|
| 316 |
|
| 317 |
/* model.js */ |
| 318 |
(function (window) { |
| 319 |
'use strict'; |
| 320 |
|
| 321 |
/** |
| 322 |
* Creates a new Model instance and hooks up the storage. |
| 323 |
* |
| 324 |
* @constructor |
| 325 |
* @param {object} storage A reference to the client side storage class |
| 326 |
*/ |
| 327 |
function Model(storage) { |
| 328 |
this.storage = storage; |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Creates a new todo model |
| 333 |
* |
| 334 |
* @param {string} [title] The title of the task |
| 335 |
* @param {function} [callback] The callback to fire after the model is created |
| 336 |
*/ |
| 337 |
Model.prototype.create = function (title, callback) { |
| 338 |
title = title || ''; |
| 339 |
callback = callback || function () {}; |
| 340 |
|
| 341 |
var newItem = { |
| 342 |
title: title.trim(), |
| 343 |
completed: false |
| 344 |
}; |
| 345 |
|
| 346 |
this.storage.save(newItem, callback); |
| 347 |
}; |
| 348 |
|
| 349 |
/** |
| 350 |
* Finds and returns a model in storage. If no query is given it'll simply |
| 351 |
* return everything. If you pass in a string or number it'll look that up as |
| 352 |
* the ID of the model to find. Lastly, you can pass it an object to match |
| 353 |
* against. |
| 354 |
* |
| 355 |
* @param {string|number|object} [query] A query to match models against |
| 356 |
* @param {function} [callback] The callback to fire after the model is found |
| 357 |
* |
| 358 |
* @example |
| 359 |
* model.read(1, func); // Will find the model with an ID of 1 |
| 360 |
* model.read('1'); // Same as above |
| 361 |
* //Below will find a model with foo equalling bar and hello equalling world. |
| 362 |
* model.read({ foo: 'bar', hello: 'world' }); |
| 363 |
*/ |
| 364 |
Model.prototype.read = function (query, callback) { |
| 365 |
var queryType = typeof query; |
| 366 |
callback = callback || function () {}; |
| 367 |
|
| 368 |
if (queryType === 'function') { |
| 369 |
callback = query; |
| 370 |
return this.storage.findAll(callback); |
| 371 |
} else if (queryType === 'string' || queryType === 'number') { |
| 372 |
query = parseInt(query, 10); |
| 373 |
this.storage.find({ id: query }, callback); |
| 374 |
} else { |
| 375 |
this.storage.find(query, callback); |
| 376 |
} |
| 377 |
}; |
| 378 |
|
| 379 |
/** |
| 380 |
* Updates a model by giving it an ID, data to update, and a callback to fire when |
| 381 |
* the update is complete. |
| 382 |
* |
| 383 |
* @param {number} id The id of the model to update |
| 384 |
* @param {object} data The properties to update and their new value |
| 385 |
* @param {function} callback The callback to fire when the update is complete. |
| 386 |
*/ |
| 387 |
Model.prototype.update = function (id, data, callback) { |
| 388 |
this.storage.save(data, callback, id); |
| 389 |
}; |
| 390 |
|
| 391 |
/** |
| 392 |
* Removes a model from storage |
| 393 |
* |
| 394 |
* @param {number} id The ID of the model to remove |
| 395 |
* @param {function} callback The callback to fire when the removal is complete. |
| 396 |
*/ |
| 397 |
Model.prototype.remove = function (id, callback) { |
| 398 |
this.storage.remove(id, callback); |
| 399 |
}; |
| 400 |
|
| 401 |
/** |
| 402 |
* WARNING: Will remove ALL data from storage. |
| 403 |
* |
| 404 |
* @param {function} callback The callback to fire when the storage is wiped. |
| 405 |
*/ |
| 406 |
Model.prototype.removeAll = function (callback) { |
| 407 |
this.storage.drop(callback); |
| 408 |
}; |
| 409 |
|
| 410 |
/** |
| 411 |
* Returns a count of all todos |
| 412 |
*/ |
| 413 |
Model.prototype.getCount = function (callback) { |
| 414 |
var todos = { |
| 415 |
active: 0, |
| 416 |
completed: 0, |
| 417 |
total: 0 |
| 418 |
}; |
| 419 |
|
| 420 |
this.storage.findAll(function (data) { |
| 421 |
data.forEach(function (todo) { |
| 422 |
if (todo.completed) { |
| 423 |
todos.completed++; |
| 424 |
} else { |
| 425 |
todos.active++; |
| 426 |
} |
| 427 |
|
| 428 |
todos.total++; |
| 429 |
}); |
| 430 |
callback(todos); |
| 431 |
}); |
| 432 |
}; |
| 433 |
|
| 434 |
// Export to window |
| 435 |
window.app = window.app || {}; |
| 436 |
window.app.Model = Model; |
| 437 |
})(window); |
| 438 |
|
| 439 |
|
| 440 |
/* template.js */ |
| 441 |
(function (window) { |
| 442 |
'use strict'; |
| 443 |
|
| 444 |
var htmlEscapes = { |
| 445 |
'&': '&', |
| 446 |
'<': '<', |
| 447 |
'>': '>', |
| 448 |
'"': '"', |
| 449 |
'\'': ''', |
| 450 |
'`': '`' |
| 451 |
}; |
| 452 |
|
| 453 |
var escapeHtmlChar = function (chr) { |
| 454 |
return htmlEscapes[chr]; |
| 455 |
}; |
| 456 |
|
| 457 |
var reUnescapedHtml = /[&<>"'`]/g; |
| 458 |
var reHasUnescapedHtml = new RegExp(reUnescapedHtml.source); |
| 459 |
|
| 460 |
var escape = function (string) { |
| 461 |
return (string && reHasUnescapedHtml.test(string)) |
| 462 |
? string.replace(reUnescapedHtml, escapeHtmlChar) |
| 463 |
: string; |
| 464 |
}; |
| 465 |
|
| 466 |
/** |
| 467 |
* Sets up defaults for all the Template methods such as a default template |
| 468 |
* |
| 469 |
* @constructor |
| 470 |
*/ |
| 471 |
function Template() { |
| 472 |
this.defaultTemplate |
| 473 |
= '<li data-id="{{id}}" class="{{completed}}">' |
| 474 |
+ '<div class="view">' |
| 475 |
+ '<input class="toggle" type="checkbox" {{checked}}>' |
| 476 |
+ '<label>{{title}}</label>' |
| 477 |
+ '<button class="destroy"></button>' |
| 478 |
+ '</div>' |
| 479 |
+ '</li>'; |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
* Creates an <li> HTML string and returns it for placement in your app. |
| 484 |
* |
| 485 |
* NOTE: In real life you should be using a templating engine such as Mustache |
| 486 |
* or Handlebars, however, this is a vanilla JS example. |
| 487 |
* |
| 488 |
* @param {object} data The object containing keys you want to find in the |
| 489 |
* template to replace. |
| 490 |
* @returns {string} HTML String of an <li> element |
| 491 |
* |
| 492 |
* @example |
| 493 |
* view.show({ |
| 494 |
* id: 1, |
| 495 |
* title: "Hello World", |
| 496 |
* completed: 0, |
| 497 |
* }); |
| 498 |
*/ |
| 499 |
Template.prototype.show = function (data) { |
| 500 |
var i, l; |
| 501 |
var view = ''; |
| 502 |
|
| 503 |
for (i = 0, l = data.length; i < l; i++) { |
| 504 |
var template = this.defaultTemplate; |
| 505 |
var completed = ''; |
| 506 |
var checked = ''; |
| 507 |
|
| 508 |
if (data[i].completed) { |
| 509 |
completed = 'completed'; |
| 510 |
checked = 'checked'; |
| 511 |
} |
| 512 |
|
| 513 |
template = template.replace('{{id}}', data[i].id); |
| 514 |
template = template.replace('{{title}}', escape(data[i].title)); |
| 515 |
template = template.replace('{{completed}}', completed); |
| 516 |
template = template.replace('{{checked}}', checked); |
| 517 |
|
| 518 |
view = view + template; |
| 519 |
} |
| 520 |
|
| 521 |
return view; |
| 522 |
}; |
| 523 |
|
| 524 |
/** |
| 525 |
* Displays a counter of how many to dos are left to complete |
| 526 |
* |
| 527 |
* @param {number} activeTodos The number of active todos. |
| 528 |
* @returns {string} String containing the count |
| 529 |
*/ |
| 530 |
Template.prototype.itemCounter = function (activeTodos) { |
| 531 |
var plural = activeTodos === 1 ? '' : 's'; |
| 532 |
|
| 533 |
return '<strong>' + activeTodos + '</strong> item' + plural + ' left'; |
| 534 |
}; |
| 535 |
|
| 536 |
/** |
| 537 |
* Updates the text within the "Clear completed" button |
| 538 |
* |
| 539 |
* @param {[type]} completedTodos The number of completed todos. |
| 540 |
* @returns {string} String containing the count |
| 541 |
*/ |
| 542 |
Template.prototype.clearCompletedButton = function (completedTodos) { |
| 543 |
if (completedTodos > 0) { |
| 544 |
return 'Clear completed'; |
| 545 |
} else { |
| 546 |
return ''; |
| 547 |
} |
| 548 |
}; |
| 549 |
|
| 550 |
// Export to window |
| 551 |
window.app = window.app || {}; |
| 552 |
window.app.Template = Template; |
| 553 |
})(window); |
| 554 |
|
| 555 |
/* view.js */ |
| 556 |
(function (window) { |
| 557 |
'use strict'; |
| 558 |
|
| 559 |
/** |
| 560 |
* View that abstracts away the browser's DOM completely. |
| 561 |
* It has two simple entry points: |
| 562 |
* |
| 563 |
* - bind(eventName, handler) |
| 564 |
* Takes a todo application event and registers the handler |
| 565 |
* - render(command, parameterObject) |
| 566 |
* Renders the given command with the options |
| 567 |
*/ |
| 568 |
function View(template) { |
| 569 |
this.template = template; |
| 570 |
|
| 571 |
this.ENTER_KEY = 13; |
| 572 |
this.ESCAPE_KEY = 27; |
| 573 |
|
| 574 |
this.$todoList = qs('.todo-list'); |
| 575 |
this.$todoItemCounter = qs('.todo-count'); |
| 576 |
this.$clearCompleted = qs('.clear-completed'); |
| 577 |
this.$main = qs('.main'); |
| 578 |
this.$footer = qs('.footer'); |
| 579 |
this.$toggleAll = qs('.toggle-all'); |
| 580 |
this.$newTodo = qs('.new-todo'); |
| 581 |
this.$toggleFullscreen = qs('.toggle-fullscreen') |
| 582 |
} |
| 583 |
|
| 584 |
View.prototype._removeItem = function (id) { |
| 585 |
var elem = qs('[data-id="' + id + '"]'); |
| 586 |
|
| 587 |
if (elem) { |
| 588 |
this.$todoList.removeChild(elem); |
| 589 |
} |
| 590 |
}; |
| 591 |
|
| 592 |
View.prototype._clearCompletedButton = function (completedCount, visible) { |
| 593 |
this.$clearCompleted.innerHTML = this.template.clearCompletedButton(completedCount); |
| 594 |
this.$clearCompleted.style.display = visible ? 'block' : 'none'; |
| 595 |
}; |
| 596 |
|
| 597 |
View.prototype._setFilter = function (currentPage) { |
| 598 |
qs('.filters .selected').className = ''; |
| 599 |
qs('.filters [href="#/' + currentPage + '"]').className = 'selected'; |
| 600 |
}; |
| 601 |
|
| 602 |
View.prototype._elementComplete = function (id, completed) { |
| 603 |
var listItem = qs('[data-id="' + id + '"]'); |
| 604 |
|
| 605 |
if (!listItem) { |
| 606 |
return; |
| 607 |
} |
| 608 |
|
| 609 |
listItem.className = completed ? 'completed' : ''; |
| 610 |
|
| 611 |
// In case it was toggled from an event and not by clicking the checkbox |
| 612 |
qs('input', listItem).checked = completed; |
| 613 |
}; |
| 614 |
|
| 615 |
View.prototype._editItem = function (id, title) { |
| 616 |
var listItem = qs('[data-id="' + id + '"]'); |
| 617 |
|
| 618 |
if (!listItem) { |
| 619 |
return; |
| 620 |
} |
| 621 |
|
| 622 |
listItem.className = listItem.className + ' editing'; |
| 623 |
|
| 624 |
var input = document.createElement('input'); |
| 625 |
input.className = 'edit'; |
| 626 |
|
| 627 |
listItem.appendChild(input); |
| 628 |
input.focus(); |
| 629 |
input.value = title; |
| 630 |
}; |
| 631 |
|
| 632 |
View.prototype._editItemDone = function (id, title) { |
| 633 |
var listItem = qs('[data-id="' + id + '"]'); |
| 634 |
|
| 635 |
if (!listItem) { |
| 636 |
return; |
| 637 |
} |
| 638 |
|
| 639 |
var input = qs('input.edit', listItem); |
| 640 |
listItem.removeChild(input); |
| 641 |
|
| 642 |
listItem.className = listItem.className.replace('editing', ''); |
| 643 |
|
| 644 |
qsa('label', listItem).forEach(function (label) { |
| 645 |
label.textContent = title; |
| 646 |
}); |
| 647 |
}; |
| 648 |
|
| 649 |
View.prototype.render = function (viewCmd, parameter) { |
| 650 |
var self = this; |
| 651 |
var viewCommands = { |
| 652 |
showEntries: function () { |
| 653 |
self.$todoList.innerHTML = self.template.show(parameter); |
| 654 |
}, |
| 655 |
removeItem: function () { |
| 656 |
self._removeItem(parameter); |
| 657 |
}, |
| 658 |
updateElementCount: function () { |
| 659 |
self.$todoItemCounter.innerHTML = self.template.itemCounter(parameter); |
| 660 |
}, |
| 661 |
clearCompletedButton: function () { |
| 662 |
self._clearCompletedButton(parameter.completed, parameter.visible); |
| 663 |
}, |
| 664 |
contentBlockVisibility: function () { |
| 665 |
self.$main.style.display = self.$footer.style.display = parameter.visible ? 'block' : 'none'; |
| 666 |
}, |
| 667 |
toggleAll: function () { |
| 668 |
self.$toggleAll.checked = parameter.checked; |
| 669 |
}, |
| 670 |
setFilter: function () { |
| 671 |
self._setFilter(parameter); |
| 672 |
}, |
| 673 |
clearNewTodo: function () { |
| 674 |
self.$newTodo.value = ''; |
| 675 |
}, |
| 676 |
elementComplete: function () { |
| 677 |
self._elementComplete(parameter.id, parameter.completed); |
| 678 |
}, |
| 679 |
editItem: function () { |
| 680 |
self._editItem(parameter.id, parameter.title); |
| 681 |
}, |
| 682 |
editItemDone: function () { |
| 683 |
self._editItemDone(parameter.id, parameter.title); |
| 684 |
} |
| 685 |
}; |
| 686 |
|
| 687 |
viewCommands[viewCmd](); |
| 688 |
}; |
| 689 |
|
| 690 |
View.prototype._itemId = function (element) { |
| 691 |
var li = $parent(element, 'li'); |
| 692 |
return parseInt(li.dataset.id, 10); |
| 693 |
}; |
| 694 |
|
| 695 |
View.prototype._bindItemEditDone = function (handler) { |
| 696 |
var self = this; |
| 697 |
$delegate(self.$todoList, 'li .edit', 'blur', function () { |
| 698 |
if (!this.dataset.iscanceled) { |
| 699 |
handler({ |
| 700 |
id: self._itemId(this), |
| 701 |
title: this.value |
| 702 |
}); |
| 703 |
} |
| 704 |
}); |
| 705 |
|
| 706 |
$delegate(self.$todoList, 'li .edit', 'keypress', function (event) { |
| 707 |
if (event.keyCode === self.ENTER_KEY) { |
| 708 |
// Remove the cursor from the input when you hit enter just like if it |
| 709 |
// were a real form |
| 710 |
this.blur(); |
| 711 |
} |
| 712 |
}); |
| 713 |
}; |
| 714 |
|
| 715 |
View.prototype._bindItemEditCancel = function (handler) { |
| 716 |
var self = this; |
| 717 |
$delegate(self.$todoList, 'li .edit', 'keyup', function (event) { |
| 718 |
if (event.keyCode === self.ESCAPE_KEY) { |
| 719 |
this.dataset.iscanceled = true; |
| 720 |
this.blur(); |
| 721 |
|
| 722 |
handler({id: self._itemId(this)}); |
| 723 |
} |
| 724 |
}); |
| 725 |
}; |
| 726 |
|
| 727 |
View.prototype.bind = function (event, handler) { |
| 728 |
var self = this; |
| 729 |
if (event === 'newTodo') { |
| 730 |
$on(self.$newTodo, 'keyup', function (event) { |
| 731 |
if (event.keyCode === self.ENTER_KEY) { |
| 732 |
handler(self.$newTodo.value); |
| 733 |
} |
| 734 |
}); |
| 735 |
|
| 736 |
} else if (event === 'removeCompleted') { |
| 737 |
$on(self.$clearCompleted, 'click', function () { |
| 738 |
handler(); |
| 739 |
}); |
| 740 |
|
| 741 |
} else if (event === 'toggleAll') { |
| 742 |
$on(self.$toggleAll, 'click', function () { |
| 743 |
handler({completed: this.checked}); |
| 744 |
}); |
| 745 |
|
| 746 |
} else if (event === 'itemEdit') { |
| 747 |
$delegate(self.$todoList, 'li label', 'dblclick', function () { |
| 748 |
handler({id: self._itemId(this)}); |
| 749 |
}); |
| 750 |
|
| 751 |
} else if (event === 'itemRemove') { |
| 752 |
$delegate(self.$todoList, '.destroy', 'click', function () { |
| 753 |
handler({id: self._itemId(this)}); |
| 754 |
}); |
| 755 |
|
| 756 |
} else if (event === 'itemToggle') { |
| 757 |
$delegate(self.$todoList, '.toggle', 'click', function () { |
| 758 |
handler({ |
| 759 |
id: self._itemId(this), |
| 760 |
completed: this.checked |
| 761 |
}); |
| 762 |
}); |
| 763 |
|
| 764 |
} else if (event === 'itemEditDone') { |
| 765 |
self._bindItemEditDone(handler); |
| 766 |
|
| 767 |
} else if (event === 'itemEditCancel') { |
| 768 |
self._bindItemEditCancel(handler); |
| 769 |
|
| 770 |
} else if (event === 'toggleFullscreen') { |
| 771 |
$on(self.$toggleFullscreen, 'click', function (e) { |
| 772 |
e.preventDefault() |
| 773 |
handler(); |
| 774 |
}); |
| 775 |
} |
| 776 |
}; |
| 777 |
|
| 778 |
// Export to window |
| 779 |
window.app = window.app || {}; |
| 780 |
window.app.View = View; |
| 781 |
}(window)); |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
/* controller.js */ |
| 786 |
(function (window) { |
| 787 |
'use strict'; |
| 788 |
|
| 789 |
/** |
| 790 |
* Takes a model and view and acts as the controller between them |
| 791 |
* |
| 792 |
* @constructor |
| 793 |
* @param {object} model The model instance |
| 794 |
* @param {object} view The view instance |
| 795 |
*/ |
| 796 |
function Controller(model, view) { |
| 797 |
var self = this; |
| 798 |
self.model = model; |
| 799 |
self.view = view; |
| 800 |
|
| 801 |
self.view.bind('newTodo', function (title) { |
| 802 |
self.addItem(title); |
| 803 |
window.pywebview.api.addItem(title) |
| 804 |
}); |
| 805 |
|
| 806 |
self.view.bind('itemEdit', function (item) { |
| 807 |
self.editItem(item.id); |
| 808 |
}); |
| 809 |
|
| 810 |
self.view.bind('itemEditDone', function (item) { |
| 811 |
self.editItemSave(item.id, item.title); |
| 812 |
window.pywebview.api.editItem(item) |
| 813 |
}); |
| 814 |
|
| 815 |
self.view.bind('itemEditCancel', function (item) { |
| 816 |
self.editItemCancel(item.id); |
| 817 |
}); |
| 818 |
|
| 819 |
self.view.bind('itemRemove', function (item) { |
| 820 |
self.removeItem(item.id); |
| 821 |
window.pywebview.api.removeItem(item) |
| 822 |
}); |
| 823 |
|
| 824 |
self.view.bind('itemToggle', function (item) { |
| 825 |
self.toggleComplete(item.id, item.completed); |
| 826 |
window.pywebview.api.toggleItem(item) |
| 827 |
}); |
| 828 |
|
| 829 |
self.view.bind('removeCompleted', function () { |
| 830 |
self.removeCompletedItems(); |
| 831 |
}); |
| 832 |
|
| 833 |
self.view.bind('toggleAll', function (status) { |
| 834 |
self.toggleAll(status.completed); |
| 835 |
}); |
| 836 |
|
| 837 |
self.view.bind('toggleFullscreen', function () { |
| 838 |
window.pywebview.api.toggleFullscreen() |
| 839 |
}); |
| 840 |
} |
| 841 |
|
| 842 |
/** |
| 843 |
* Loads and initialises the view |
| 844 |
* |
| 845 |
* @param {string} '' | 'active' | 'completed' |
| 846 |
*/ |
| 847 |
Controller.prototype.setView = function (locationHash) { |
| 848 |
var route = locationHash.split('/')[1]; |
| 849 |
var page = route || ''; |
| 850 |
this._updateFilterState(page); |
| 851 |
}; |
| 852 |
|
| 853 |
/** |
| 854 |
* An event to fire on load. Will get all items and display them in the |
| 855 |
* todo-list |
| 856 |
*/ |
| 857 |
Controller.prototype.showAll = function () { |
| 858 |
var self = this; |
| 859 |
self.model.read(function (data) { |
| 860 |
self.view.render('showEntries', data); |
| 861 |
}); |
| 862 |
}; |
| 863 |
|
| 864 |
/** |
| 865 |
* Renders all active tasks |
| 866 |
*/ |
| 867 |
Controller.prototype.showActive = function () { |
| 868 |
var self = this; |
| 869 |
self.model.read({ completed: false }, function (data) { |
| 870 |
self.view.render('showEntries', data); |
| 871 |
}); |
| 872 |
}; |
| 873 |
|
| 874 |
/** |
| 875 |
* Renders all completed tasks |
| 876 |
*/ |
| 877 |
Controller.prototype.showCompleted = function () { |
| 878 |
var self = this; |
| 879 |
self.model.read({ completed: true }, function (data) { |
| 880 |
self.view.render('showEntries', data); |
| 881 |
}); |
| 882 |
}; |
| 883 |
|
| 884 |
/** |
| 885 |
* An event to fire whenever you want to add an item. Simply pass in the event |
| 886 |
* object and it'll handle the DOM insertion and saving of the new item. |
| 887 |
*/ |
| 888 |
Controller.prototype.addItem = function (title) { |
| 889 |
var self = this; |
| 890 |
|
| 891 |
if (title.trim() === '') { |
| 892 |
return; |
| 893 |
} |
| 894 |
|
| 895 |
self.model.create(title, function () { |
| 896 |
self.view.render('clearNewTodo'); |
| 897 |
self._filter(true); |
| 898 |
}); |
| 899 |
}; |
| 900 |
|
| 901 |
/* |
| 902 |
* Triggers the item editing mode. |
| 903 |
*/ |
| 904 |
Controller.prototype.editItem = function (id) { |
| 905 |
var self = this; |
| 906 |
self.model.read(id, function (data) { |
| 907 |
self.view.render('editItem', {id: id, title: data[0].title}); |
| 908 |
}); |
| 909 |
}; |
| 910 |
|
| 911 |
/* |
| 912 |
* Finishes the item editing mode successfully. |
| 913 |
*/ |
| 914 |
Controller.prototype.editItemSave = function (id, title) { |
| 915 |
var self = this; |
| 916 |
title = title.trim(); |
| 917 |
|
| 918 |
if (title.length !== 0) { |
| 919 |
self.model.update(id, {title: title}, function () { |
| 920 |
self.view.render('editItemDone', {id: id, title: title}); |
| 921 |
}); |
| 922 |
} else { |
| 923 |
self.removeItem(id); |
| 924 |
} |
| 925 |
}; |
| 926 |
|
| 927 |
/* |
| 928 |
* Cancels the item editing mode. |
| 929 |
*/ |
| 930 |
Controller.prototype.editItemCancel = function (id) { |
| 931 |
var self = this; |
| 932 |
self.model.read(id, function (data) { |
| 933 |
self.view.render('editItemDone', {id: id, title: data[0].title}); |
| 934 |
}); |
| 935 |
}; |
| 936 |
|
| 937 |
/** |
| 938 |
* By giving it an ID it'll find the DOM element matching that ID, |
| 939 |
* remove it from the DOM and also remove it from storage. |
| 940 |
* |
| 941 |
* @param {number} id The ID of the item to remove from the DOM and |
| 942 |
* storage |
| 943 |
*/ |
| 944 |
Controller.prototype.removeItem = function (id) { |
| 945 |
var self = this; |
| 946 |
self.model.remove(id, function () { |
| 947 |
self.view.render('removeItem', id); |
| 948 |
}); |
| 949 |
|
| 950 |
self._filter(); |
| 951 |
}; |
| 952 |
|
| 953 |
/** |
| 954 |
* Will remove all completed items from the DOM and storage. |
| 955 |
*/ |
| 956 |
Controller.prototype.removeCompletedItems = function () { |
| 957 |
var self = this; |
| 958 |
self.model.read({ completed: true }, function (data) { |
| 959 |
data.forEach(function (item) { |
| 960 |
self.removeItem(item.id); |
| 961 |
}); |
| 962 |
}); |
| 963 |
|
| 964 |
self._filter(); |
| 965 |
}; |
| 966 |
|
| 967 |
/** |
| 968 |
* Give it an ID of a model and a checkbox and it will update the item |
| 969 |
* in storage based on the checkbox's state. |
| 970 |
* |
| 971 |
* @param {number} id The ID of the element to complete or uncomplete |
| 972 |
* @param {object} checkbox The checkbox to check the state of complete |
| 973 |
* or not |
| 974 |
* @param {boolean|undefined} silent Prevent re-filtering the todo items |
| 975 |
*/ |
| 976 |
Controller.prototype.toggleComplete = function (id, completed, silent) { |
| 977 |
var self = this; |
| 978 |
self.model.update(id, { completed: completed }, function () { |
| 979 |
self.view.render('elementComplete', { |
| 980 |
id: id, |
| 981 |
completed: completed |
| 982 |
}); |
| 983 |
}); |
| 984 |
|
| 985 |
if (!silent) { |
| 986 |
self._filter(); |
| 987 |
} |
| 988 |
}; |
| 989 |
|
| 990 |
/** |
| 991 |
* Will toggle ALL checkboxes' on/off state and completeness of models. |
| 992 |
* Just pass in the event object. |
| 993 |
*/ |
| 994 |
Controller.prototype.toggleAll = function (completed) { |
| 995 |
var self = this; |
| 996 |
self.model.read({ completed: !completed }, function (data) { |
| 997 |
data.forEach(function (item) { |
| 998 |
self.toggleComplete(item.id, completed, true); |
| 999 |
}); |
| 1000 |
}); |
| 1001 |
|
| 1002 |
self._filter(); |
| 1003 |
}; |
| 1004 |
|
| 1005 |
/** |
| 1006 |
* Updates the pieces of the page which change depending on the remaining |
| 1007 |
* number of todos. |
| 1008 |
*/ |
| 1009 |
Controller.prototype._updateCount = function () { |
| 1010 |
var self = this; |
| 1011 |
self.model.getCount(function (todos) { |
| 1012 |
self.view.render('updateElementCount', todos.active); |
| 1013 |
self.view.render('clearCompletedButton', { |
| 1014 |
completed: todos.completed, |
| 1015 |
visible: todos.completed > 0 |
| 1016 |
}); |
| 1017 |
|
| 1018 |
self.view.render('toggleAll', {checked: todos.completed === todos.total}); |
| 1019 |
self.view.render('contentBlockVisibility', {visible: todos.total > 0}); |
| 1020 |
}); |
| 1021 |
}; |
| 1022 |
|
| 1023 |
/** |
| 1024 |
* Re-filters the todo items, based on the active route. |
| 1025 |
* @param {boolean|undefined} force forces a re-painting of todo items. |
| 1026 |
*/ |
| 1027 |
Controller.prototype._filter = function (force) { |
| 1028 |
var activeRoute = this._activeRoute.charAt(0).toUpperCase() + this._activeRoute.substr(1); |
| 1029 |
|
| 1030 |
// Update the elements on the page, which change with each completed todo |
| 1031 |
this._updateCount(); |
| 1032 |
|
| 1033 |
// If the last active route isn't "All", or we're switching routes, we |
| 1034 |
// re-create the todo item elements, calling: |
| 1035 |
// this.show[All|Active|Completed](); |
| 1036 |
if (force || this._lastActiveRoute !== 'All' || this._lastActiveRoute !== activeRoute) { |
| 1037 |
this['show' + activeRoute](); |
| 1038 |
} |
| 1039 |
|
| 1040 |
this._lastActiveRoute = activeRoute; |
| 1041 |
}; |
| 1042 |
|
| 1043 |
/** |
| 1044 |
* Simply updates the filter nav's selected states |
| 1045 |
*/ |
| 1046 |
Controller.prototype._updateFilterState = function (currentPage) { |
| 1047 |
// Store a reference to the active route, allowing us to re-filter todo |
| 1048 |
// items as they are marked complete or incomplete. |
| 1049 |
this._activeRoute = currentPage; |
| 1050 |
|
| 1051 |
if (currentPage === '') { |
| 1052 |
this._activeRoute = 'All'; |
| 1053 |
} |
| 1054 |
|
| 1055 |
this._filter(); |
| 1056 |
|
| 1057 |
this.view.render('setFilter', currentPage); |
| 1058 |
}; |
| 1059 |
|
| 1060 |
// Export to window |
| 1061 |
window.app = window.app || {}; |
| 1062 |
window.app.Controller = Controller; |
| 1063 |
})(window); |
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
/** app.js **/ |
| 1068 |
(function () { |
| 1069 |
'use strict'; |
| 1070 |
|
| 1071 |
/** |
| 1072 |
* Sets up a brand new Todo list. |
| 1073 |
* |
| 1074 |
* @param {string} name The name of your new to do list. |
| 1075 |
*/ |
| 1076 |
function Todo(name) { |
| 1077 |
this.storage = new app.Store(name); |
| 1078 |
this.model = new app.Model(this.storage); |
| 1079 |
this.template = new app.Template(); |
| 1080 |
this.view = new app.View(this.template); |
| 1081 |
this.controller = new app.Controller(this.model, this.view); |
| 1082 |
} |
| 1083 |
|
| 1084 |
var todo = new Todo('todos-vanillajs'); |
| 1085 |
|
| 1086 |
function setView() { |
| 1087 |
todo.controller.setView(document.location.hash); |
| 1088 |
} |
| 1089 |
|
| 1090 |
$on(window, 'load', setView); |
| 1091 |
$on(window, 'hashchange', setView); |
| 1092 |
})(); |