Commit f22ebe10 authored by Sindre Sorhus's avatar Sindre Sorhus

Merge pull request #456 from passy/agility-jshint

AgilityJS: Style update
parents 1058c813 2f1923dc
(function( $, $$ ) { (function ($, $$) {
'use strict'; 'use strict';
var ENTER_KEY = 13; var ENTER_KEY = 13;
// Hack of taking out html elements from DOM so that agility's view can use it. // Hack of taking out html elements from DOM so that agility's view can use it.
// We need 'outerhtml' also, as agilityjs will append DOM, so removing it. // We need 'outerhtml' also, as agilityjs will append DOM, so removing it.
var drawHtml = function( selector ) { var drawHtml = function (selector) {
return $(selector).remove().wrap('<div>').parent().html(); return $(selector).remove().wrap('<div>').parent().html();
}; };
// Simple Two layer composition: // Simple Two layer composition:
// individual 'todoitem' and 'app'lication which holds multiple todoitems. // individual 'todoitem' and 'app'lication which holds multiple todoitems.
$(function() { $(function () {
// todo item // todo item
var todoitem = $$({ var todoitem = $$({
model: { model: {
...@@ -22,39 +22,39 @@ ...@@ -22,39 +22,39 @@
style: '.hidden { display: none }' style: '.hidden { display: none }'
}, },
controller: { controller: {
'change:completed': function() { 'change:completed': function () {
this.view.$().toggleClass( 'completed', this.model.get('completed') ); this.view.$().toggleClass('completed', this.model.get('completed'));
app.updateStatus(); app.updateStatus();
}, },
'dblclick &': function() { 'dblclick &': function () {
this.view.$().addClass('editing'); this.view.$().addClass('editing');
this.view.$('.edit').focus(); this.view.$('.edit').focus();
}, },
'click .destroy': function() { 'click .destroy': function () {
this.destroy(); this.destroy();
}, },
'create': function() { 'create': function () {
this.view.$().toggleClass( 'completed', this.model.get('completed') ); this.view.$().toggleClass('completed', this.model.get('completed'));
}, },
'change': function() { 'change': function () {
this.save(); this.save();
}, },
'destroy': function() { 'destroy': function () {
this.erase(); this.erase();
}, },
'blur input': function() { 'blur input': function () {
this.updateTitle(); this.updateTitle();
}, },
'keyup input': function() { 'keyup input': function () {
if ( event.which === ENTER_KEY ) { if (event.which === ENTER_KEY) {
this.updateTitle(); this.updateTitle();
} }
} }
}, },
updateTitle: function() { updateTitle: function () {
this.view.$().removeClass('editing'); this.view.$().removeClass('editing');
var title = this.model.get('title').trim(); var title = this.model.get('title').trim();
if ( title ) { if (title) {
this.model.set({ this.model.set({
title: title title: title
}); });
...@@ -62,7 +62,7 @@ ...@@ -62,7 +62,7 @@
this.destroy(); this.destroy();
} }
} }
}).persist( $$.adapter.localStorage, { }).persist($$.adapter.localStorage, {
collection: 'todos-agilityjs' collection: 'todos-agilityjs'
}); });
...@@ -81,45 +81,45 @@ ...@@ -81,45 +81,45 @@
style: '.hidden { display: none }' style: '.hidden { display: none }'
}, },
controller: { controller: {
'remove': function() { 'remove': function () {
this.updateStatus(); this.updateStatus();
}, },
'append': function() { 'append': function () {
this.updateStatus(); this.updateStatus();
}, },
'keyup #new-todo': function( event ) { 'keyup #new-todo': function (event) {
var title = $('#new-todo').val().trim(); var title = $('#new-todo').val().trim();
if ( event.which === ENTER_KEY && title ) { if (event.which === ENTER_KEY && title) {
var item = $$( todoitem, { var item = $$(todoitem, {
title: title title: title
}).save(); }).save();
this.append( item, '#todo-list' ); this.append(item, '#todo-list');
event.target.value = ''; // clear input field event.target.value = ''; // clear input field
} }
}, },
'click #toggle-all': function() { 'click #toggle-all': function () {
var ischecked = this.view.$('#toggle-all').prop('checked'); var ischecked = this.view.$('#toggle-all').prop('checked');
this.each(function( id, item ) { this.each(function (id, item) {
item.model.set({ item.model.set({
completed: ischecked completed: ischecked
}); });
}); });
}, },
'click #clear-completed': function() { 'click #clear-completed': function () {
this.each(function( id, item ) { this.each(function (id, item) {
if ( item.model.get('completed') ) { if (item.model.get('completed')) {
item.destroy(); item.destroy();
} }
}); });
} }
}, },
// utility functions // utility functions
updateStatus: function() { updateStatus: function () {
// update counts // update counts
var count = this.size(), var count = this.size(),
completedCount = 0; completedCount = 0;
this.each(function( id, item ) { this.each(function (id, item) {
if ( item.model.get('completed') ) { if (item.model.get('completed')) {
completedCount++; completedCount++;
} }
}); });
...@@ -131,51 +131,51 @@ ...@@ -131,51 +131,51 @@
clearBtnStyle: (completedCount === 0 ? 'hidden' : '') clearBtnStyle: (completedCount === 0 ? 'hidden' : '')
}); });
// update toggle-all checked status // update toggle-all checked status
$('#toggle-all').prop( 'checked', completedCount === count ); $('#toggle-all').prop('checked', completedCount === count);
// update the view according to the current filter. // update the view according to the current filter.
this.applyFilter(); this.applyFilter();
}, },
// filter handler // filter handler
filters: { filters: {
'#/': function( item ) { '#/': function () {
return true; return true;
}, },
'#/active': function( item ) { '#/active': function (item) {
return !item.model.get('completed'); return !item.model.get('completed');
}, },
'#/completed': function( item ) { '#/completed': function (item) {
return item.model.get('completed'); return item.model.get('completed');
} }
}, },
applyFilter: function( hash ) { applyFilter: function (hash) {
var isVisible = this.filters[hash || location.hash]; var isVisible = this.filters[hash || location.hash];
if ( isVisible ) { if (isVisible) {
this.each(function( id, item ) { this.each(function (id, item) {
item.view.$().toggleClass( 'hidden', !isVisible( item ) ); item.view.$().toggleClass('hidden', !isVisible(item));
}); });
} }
} }
}).persist(); }).persist();
$$.document.prepend( app ); $$.document.prepend(app);
// load from localStorage // load from localStorage
app.gather( todoitem, 'append', '#todo-list' ).updateStatus(); app.gather(todoitem, 'append', '#todo-list').updateStatus();
// manual routing (not supported by agilityjs) // manual routing (not supported by agilityjs)
$(window).on( 'hashchange', function() { $(window).on('hashchange', function () {
var hash = location.hash; var hash = location.hash;
app.applyFilter( hash ); app.applyFilter(hash);
$('#filters a').each(function() { $('#filters a').each(function () {
if ( hash === $(this).attr('href') ) { if (hash === $(this).attr('href')) {
$(this).addClass('selected'); $(this).addClass('selected');
} else { } else {
$(this).removeClass('selected'); $(this).removeClass('selected');
} }
}); });
} ); });
if ( location.hash ) { if (location.hash) {
$(window).trigger('hashchange'); $(window).trigger('hashchange');
} }
}); });
})( window.jQuery, window.agility ); })(window.jQuery, window.agility);
// custom agilityjs adapter for localstorage // custom agilityjs adapter for localstorage
(function( $$, undefined ) { (function ($$, undefined) {
'use strict'; 'use strict';
$$.adapter.localStorage = function( _params ) { $$.adapter.localStorage = function (_params) {
var storageKey = (this._data.persist.baseUrl || '') + this._data.persist.collection, var storageKey = (this._data.persist.baseUrl || '') + this._data.persist.collection,
storageStr = localStorage[storageKey], storageStr = localStorage[storageKey],
items = (storageStr ? JSON.parse( storageStr ) : {}); items = (storageStr ? JSON.parse(storageStr) : {});
// //
if ( _params.type === 'GET' ) { if (_params.type === 'GET') {
if ( _params.id !== undefined ) { // normal get if (_params.id !== undefined) { // normal get
if ( typeof items[_params.id] === 'object' ) { if (typeof items[_params.id] === 'object') {
_params.success( items[_params.id] ); _params.success(items[_params.id]);
} else { } else {
_params.error(); _params.error();
} }
} else { // gather call } else { // gather call
_params.success( items ); _params.success(items);
} }
} else if ( _params.type === 'DELETE' ) { } else if (_params.type === 'DELETE') {
delete items[_params.id]; delete items[_params.id];
localStorage[storageKey] = JSON.stringify( items ); localStorage[storageKey] = JSON.stringify(items);
} else if ( _params.type === 'PUT' || _params.type === 'POST' ) { } else if (_params.type === 'PUT' || _params.type === 'POST') {
if ( _params.id === undefined ) { if (_params.id === undefined) {
_params.id = (new Date()).getTime(); _params.id = (new Date()).getTime();
_params.data.id = _params.id; _params.data.id = _params.id;
} }
items[_params.id] = _params.data; items[_params.id] = _params.data;
localStorage[storageKey] = JSON.stringify( items ); localStorage[storageKey] = JSON.stringify(items);
} else { } else {
_params.error(); _params.error();
} }
_params.complete(); _params.complete();
}; };
})(window.agility);
})( window.agility );
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