Commit 48e183af authored by Pascal Hartig's avatar Pascal Hartig

olives: jshint style

parent ed62c2fa
(function( window ) { /*jshint newcap:false*/
/*global require*/
(function () {
'use strict'; 'use strict';
// These are the UIs that compose the Todo application // These are the UIs that compose the Todo application
require([ 'Todos/Input', 'Todos/List', 'Todos/Controls', 'Olives/LocalStore', 'Store' ], require(['Todos/Input', 'Todos/List', 'Todos/Controls', 'Olives/LocalStore', 'Store'],
// The application // The application
function Todos( Input, List, Controls, LocalStore, Store ) { function Todos(Input, List, Controls, LocalStore, Store) {
// The tasks Store is told to init on an array // The tasks Store is told to init on an array
// so tasks are indexed by a number // so tasks are indexed by a number
// This store is shared among several UIs of this application // This store is shared among several UIs of this application
...@@ -24,14 +27,14 @@ ...@@ -24,14 +27,14 @@
tasks.sync('todos-olives'); tasks.sync('todos-olives');
// Initialize Input UI by giving it a view and a model. // Initialize Input UI by giving it a view and a model.
Input(document.querySelector('#header input'), tasks ); Input(document.querySelector('#header input'), tasks);
// Init the List UI the same way, pass it the stats store too // Init the List UI the same way, pass it the stats store too
List(document.querySelector('#main'), tasks, stats ); List(document.querySelector('#main'), tasks, stats);
// Same goes for the control UI // Same goes for the control UI
Controls(document.querySelector('#footer'), tasks, stats ); Controls(document.querySelector('#footer'), tasks, stats);
}); });
})( window ); }());
define( 'Todos/Controls', /*global define*/
[ 'Olives/OObject', 'Olives/Event-plugin', 'Olives/Model-plugin', 'Olives/LocalStore', 'Todos/Tools' ], define('Todos/Controls',
['Olives/OObject', 'Olives/Event-plugin', 'Olives/Model-plugin', 'Olives/LocalStore', 'Todos/Tools'],
// The Controls UI // The Controls UI
function Controls( OObject, EventPlugin, ModelPlugin, Store, Tools ) { function Controls(OObject, EventPlugin, ModelPlugin, Store, Tools) {
'use strict';
return function ControlsInit( view, model, stats ) { return function ControlsInit(view, model, stats) {
// The OObject (the controller) inits with a default model which is a simple store // The OObject (the controller) inits with a default model which is a simple store
// But it can be init'ed with any other store, like the LocalStore // But it can be init'ed with any other store, like the LocalStore
var controls = new OObject(model), var controls = new OObject(model),
...@@ -13,8 +16,8 @@ function Controls( OObject, EventPlugin, ModelPlugin, Store, Tools ) { ...@@ -13,8 +16,8 @@ function Controls( OObject, EventPlugin, ModelPlugin, Store, Tools ) {
// A function to get the completed tasks // A function to get the completed tasks
getCompleted = function () { getCompleted = function () {
var completed = []; var completed = [];
model.loop(function ( value, id ) { model.loop(function (value, id) {
if ( value.completed ) { if (value.completed) {
completed.push(id); completed.push(id);
} }
}); });
...@@ -25,32 +28,32 @@ function Controls( OObject, EventPlugin, ModelPlugin, Store, Tools ) { ...@@ -25,32 +28,32 @@ function Controls( OObject, EventPlugin, ModelPlugin, Store, Tools ) {
updateStats = function () { updateStats = function () {
var nbCompleted = getCompleted().length; var nbCompleted = getCompleted().length;
stats.set( 'nbItems', model.getNbItems() ); stats.set('nbItems', model.getNbItems());
stats.set( 'nbLeft', stats.get('nbItems') - nbCompleted ); stats.set('nbLeft', stats.get('nbItems') - nbCompleted);
stats.set( 'nbCompleted', nbCompleted ); stats.set('nbCompleted', nbCompleted);
stats.set( 'plural', stats.get('nbLeft') === 1 ? 'item' : 'items' ); stats.set('plural', stats.get('nbLeft') === 1 ? 'item' : 'items');
}; };
// Add plugins to the UI. // Add plugins to the UI.
controls.plugins.addAll({ controls.plugins.addAll({
'event': new EventPlugin( controls ), 'event': new EventPlugin(controls),
'stats': new ModelPlugin( stats, { 'stats': new ModelPlugin(stats, {
'toggleClass': Tools.toggleClass 'toggleClass': Tools.toggleClass
}) })
}); });
// Alive applies the plugins to the HTML view // Alive applies the plugins to the HTML view
controls.alive( view ); controls.alive(view);
// Delete all tasks // Delete all tasks
controls.delAll = function () { controls.delAll = function () {
model.delAll( getCompleted() ); model.delAll(getCompleted());
}; };
// Update stats when the tasks list is modified // Update stats when the tasks list is modified
model.watch( 'added', updateStats ); model.watch('added', updateStats);
model.watch( 'deleted', updateStats ); model.watch('deleted', updateStats);
model.watch( 'updated', updateStats ); model.watch('updated', updateStats);
// I could either update stats at init or save them in a localStore // I could either update stats at init or save them in a localStore
updateStats(); updateStats();
......
/*global define*/
// It's going to be called Input // It's going to be called Input
define( 'Todos/Input', define('Todos/Input',
// It uses the Olives' OObject and the Event Plugin to listen to dom events and connect them to methods // It uses the Olives' OObject and the Event Plugin to listen to dom events and connect them to methods
[ 'Olives/OObject', 'Olives/Event-plugin' ], ['Olives/OObject', 'Olives/Event-plugin'],
// The Input UI // The Input UI
function Input( OObject, EventPlugin ) { function Input(OObject, EventPlugin) {
'use strict';
// It returns an init function // It returns an init function
return function InputInit( view, model ) { return function InputInit(view, model) {
// The OObject (the controller) inits with a default model which is a simple store // The OObject (the controller) inits with a default model which is a simple store
// But it can be init'ed with any other store, like the LocalStore // But it can be init'ed with any other store, like the LocalStore
var input = new OObject( model ), var input = new OObject(model),
ENTER_KEY = 13; ENTER_KEY = 13;
// The event plugin that is added to the OObject // The event plugin that is added to the OObject
// We have to tell it where to find the methods // We have to tell it where to find the methods
input.plugins.add( 'event', new EventPlugin(input) ); input.plugins.add('event', new EventPlugin(input));
// The method to add a new taks // The method to add a new taks
input.addTask = function addTask( event, node ) { input.addTask = function addTask(event, node) {
if ( event.keyCode === ENTER_KEY && node.value.trim() ) { if (event.keyCode === ENTER_KEY && node.value.trim()) {
model.alter( 'push', { model.alter('push', {
title: node.value.trim(), title: node.value.trim(),
completed: false completed: false
}); });
...@@ -31,6 +34,6 @@ function Input( OObject, EventPlugin ) { ...@@ -31,6 +34,6 @@ function Input( OObject, EventPlugin ) {
}; };
// Alive applies the plugins to the HTML view // Alive applies the plugins to the HTML view
input.alive( view ); input.alive(view);
}; };
}); });
define( 'Todos/List', /*global define*/
define('Todos/List',
[ 'Olives/OObject', 'Olives/Event-plugin', 'Olives/Model-plugin', 'Todos/Tools' ], ['Olives/OObject', 'Olives/Event-plugin', 'Olives/Model-plugin', 'Todos/Tools'],
// The List UI // The List UI
function List( OObject, EventPlugin, ModelPlugin, Tools ) { function List(OObject, EventPlugin, ModelPlugin, Tools) {
'use strict';
return function ListInit( view, model, stats ) { return function ListInit(view, model, stats) {
// The OObject (the controller) inits with a default model which is a simple store // The OObject (the controller) inits with a default model which is a simple store
// But it can be init'ed with any other store, like the LocalStore // But it can be init'ed with any other store, like the LocalStore
var list = new OObject( model ), var list = new OObject(model),
ENTER_KEY = 13; ENTER_KEY = 13;
// The plugins // The plugins
list.plugins.addAll({ list.plugins.addAll({
'event': new EventPlugin( list ), 'event': new EventPlugin(list),
'model': new ModelPlugin( model, { 'model': new ModelPlugin(model, {
'toggleClass': Tools.toggleClass 'toggleClass': Tools.toggleClass
}), }),
'stats': new ModelPlugin( stats, { 'stats': new ModelPlugin(stats, {
'toggleClass': Tools.toggleClass, 'toggleClass': Tools.toggleClass,
'toggleCheck': function ( value ) { 'toggleCheck': function (value) {
this.checked = model.getNbItems() === value ? 'on' : ''; this.checked = model.getNbItems() === value ? 'on' : '';
} }
}) })
}); });
// Remove the completed task // Remove the completed task
list.remove = function remove( event, node ) { list.remove = function remove(event, node) {
model.del( node.getAttribute('data-model_id') ); model.del(node.getAttribute('data-model_id'));
}; };
// Un/check all tasks // Un/check all tasks
list.toggleAll = function toggleAll( event, node ) { list.toggleAll = function toggleAll(event, node) {
var checked = !!node.checked; var checked = !!node.checked;
model.loop( function ( value, idx ) { model.loop(function (value, idx) {
this.update( idx, 'completed', checked ); this.update(idx, 'completed', checked);
}, model); }, model);
}; };
// Enter edit mode // Enter edit mode
list.startEdit = function ( event, node ) { list.startEdit = function (event, node) {
var taskId = node.getAttribute('data-model_id'); var taskId = node.getAttribute('data-model_id');
Tools.toggleClass.call( view.querySelector('li[data-model_id="' + taskId + '"]'), true, 'editing' ); Tools.toggleClass.call(view.querySelector('li[data-model_id="' + taskId + '"]'), true, 'editing');
view.querySelector('input.edit[data-model_id="' + taskId + '"]').focus(); view.querySelector('input.edit[data-model_id="' + taskId + '"]').focus();
}; };
// Leave edit mode // Leave edit mode
list.stopEdit = function ( event, node ) { list.stopEdit = function (event, node) {
var taskId = node.getAttribute('data-model_id'), var taskId = node.getAttribute('data-model_id'),
value; value;
if ( event.keyCode === ENTER_KEY ) { if (event.keyCode === ENTER_KEY) {
value = node.value.trim(); value = node.value.trim();
if ( value ) { if (value) {
model.update( taskId, 'title', value ); model.update(taskId, 'title', value);
} else { } else {
model.del( taskId ); model.del(taskId);
} }
// When task #n is removed, #n+1 becomes #n, the dom node is updated to the new value, so editing mode should exit anyway // When task #n is removed, #n+1 becomes #n, the dom node is updated to the new value, so editing mode should exit anyway
if ( model.has( taskId ) ) { if (model.has(taskId)) {
Tools.toggleClass.call( view.querySelector('li[data-model_id="' + taskId + '"]'), false, 'editing' ); Tools.toggleClass.call(view.querySelector('li[data-model_id="' + taskId + '"]'), false, 'editing');
} }
} else if ( event.type === 'blur' ) { } else if (event.type === 'blur') {
Tools.toggleClass.call( view.querySelector('li[data-model_id="' + taskId + '"]'), false, 'editing' ); Tools.toggleClass.call(view.querySelector('li[data-model_id="' + taskId + '"]'), false, 'editing');
} }
}; };
// Alive applies the plugins to the HTML view // Alive applies the plugins to the HTML view
list.alive( view ); list.alive(view);
}; };
......
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