Commit 8ad3e2dd authored by Sindre Sorhus's avatar Sindre Sorhus

Code formatting improvements

parent fd14f902
// Array Remove - By John Resig (MIT Licensed) // Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) { Array.prototype.remove = function( from, to ) {
var rest = this.slice((to || from) + 1 || this.length); var rest = this.slice( ( to || from ) + 1 || this.length );
this.length = from < 0 ? this.length + from : from; this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest); return this.push.apply( this, rest );
}; };
/* /*
...@@ -13,24 +13,28 @@ sindresorhus.com ...@@ -13,24 +13,28 @@ sindresorhus.com
*/ */
jQuery(function($){ jQuery(function($){
jQuery(function($) {
"use strict";
var Utils = { var Utils = {
// https://gist.github.com/823878 // https://gist.github.com/823878
uuid: function() { uuid: function() {
var uuid = "", i, random; var uuid = "", i, random;
for ( i = 0; i < 32; i++ ) { for ( i = 0; i < 32; i++ ) {
random = Math.random() * 16 | 0; random = Math.random() * 16 | 0;
if ( i == 8 || i == 12 || i == 16 || i == 20 ) { if ( i === 8 || i === 12 || i === 16 || i === 20 ) {
uuid += "-"; uuid += "-";
} }
uuid += (i == 12 ? 4 : (i == 16 ? (random & 3 | 8) : random)).toString(16); uuid += ( i === 12 ? 4 : ( i === 16 ? ( random & 3 | 8 ) : random ) ).toString(16);
} }
return uuid; return uuid;
}, },
pluralize: function(count, word) { pluralize: function( count, word ) {
return count === 1 ? word : word + 's'; return count === 1 ? word : word + 's';
} }
}; };
var App = { var App = {
init: function() { init: function() {
this.$template = $('#todo-template'); this.$template = $('#todo-template');
...@@ -49,22 +53,22 @@ jQuery(function($){ ...@@ -49,22 +53,22 @@ jQuery(function($){
var html = this.$template.tmpl( this.todos ); var html = this.$template.tmpl( this.todos );
this.$todoList.html( html ); this.$todoList.html( html );
this.renderFooter(); this.renderFooter();
this.store.set('todos', this.todos); this.store.set( 'todos', this.todos );
}, },
bindEvents: function() { bindEvents: function() {
var elem = this.$todoApp, var app = this.$todoApp,
list = this.$todoList; list = this.$todoList;
elem.on('click', '.clear', this.destroyDone); app.on( 'click', '.clear', this.destroyDone );
elem.on('submit', 'form', this.create); app.on( 'submit', 'form', this.create );
list.on('change', 'input[type="checkbox"]', this.toggle); list.on( 'change', 'input[type="checkbox"]', this.toggle );
list.on('dblclick', '.view', this.edit); list.on( 'dblclick', '.view', this.edit );
list.on('keypress', 'input[type="text"]', this.blurOnEnter); list.on( 'keypress', 'input[type="text"]', this.blurOnEnter );
list.on('blur', 'input[type="text"]', this.update); list.on( 'blur', 'input[type="text"]', this.update );
list.on('click', '.destroy', this.destroy); list.on( 'click', '.destroy', this.destroy );
}, },
activeTodoCount: function() { activeTodoCount: function() {
var count = 0; var count = 0;
$.each(this.todos, function(i, val) { $.each( this.todos, function( i, val ) {
if ( !val.done ) { if ( !val.done ) {
count++; count++;
} }
...@@ -75,7 +79,7 @@ jQuery(function($){ ...@@ -75,7 +79,7 @@ jQuery(function($){
var todoCount = this.todos.length, var todoCount = this.todos.length,
activeTodos = this.activeTodoCount(), activeTodos = this.activeTodoCount(),
completedTodos = todoCount - activeTodos, completedTodos = todoCount - activeTodos,
countTitle = '<b>' + activeTodos + '</b> ' + Utils.pluralize( activeTodos, 'item' ) + ' left'; countTitle = '<b>' + activeTodos + '</b> ' + Utils.pluralize( activeTodos, 'item' ) + ' left',
clearTitle = 'Clear ' + completedTodos + ' completed ' + Utils.pluralize( completedTodos, 'item' ); clearTitle = 'Clear ' + completedTodos + ' completed ' + Utils.pluralize( completedTodos, 'item' );
// Only show the footer when there are at least one todo. // Only show the footer when there are at least one todo.
this.$footer.toggle( !!todoCount ); this.$footer.toggle( !!todoCount );
...@@ -93,12 +97,12 @@ jQuery(function($){ ...@@ -93,12 +97,12 @@ jQuery(function($){
} }
App.render(); App.render();
}, },
// Accepts an element from inside the ".item" div, and returns the corresponding todo in the todos array. // Accepts an element from inside the ".item" div and returns the corresponding todo in the todos array.
getTodo: function(elem, callback) { getTodo: function( elem, callback ) {
var id = $(elem).closest('.item').data('id'); var id = $( elem ).closest('.item').data('id');
$.each(this.todos, function(i, val) { $.each( this.todos, function( i, val ) {
if ( val.id === id ) { if ( val.id === id ) {
callback.apply(App, arguments); callback.apply( App, arguments );
return false; return false;
} }
}); });
...@@ -119,7 +123,7 @@ jQuery(function($){ ...@@ -119,7 +123,7 @@ jQuery(function($){
App.render(); App.render();
}, },
toggle: function() { toggle: function() {
App.getTodo(this, function(i, val) { App.getTodo( this, function( i, val ) {
val.done = !val.done; val.done = !val.done;
}); });
App.render(); App.render();
...@@ -134,13 +138,13 @@ jQuery(function($){ ...@@ -134,13 +138,13 @@ jQuery(function($){
}, },
update: function() { update: function() {
var newVal = $(this).removeClass('editing').val(); var newVal = $(this).removeClass('editing').val();
App.getTodo(this, function(i) { App.getTodo( this, function(i) {
this.todos[i].title = newVal; this.todos[i].title = newVal;
}); });
App.render(); App.render();
}, },
destroy: function() { destroy: function() {
App.getTodo(this, function(i) { App.getTodo( this, function(i) {
this.todos.remove(i); this.todos.remove(i);
this.render(); this.render();
}); });
...@@ -148,5 +152,5 @@ jQuery(function($){ ...@@ -148,5 +152,5 @@ jQuery(function($){
}; };
window.TodoApp = App.init(); window.TodoApp = App.init();
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment