Skip to content

Commit

Permalink
DUEL bug fixed, code style.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Jun 13, 2013
1 parent b852875 commit f83a3ca
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<%-- could have embedded in 'tasks' for-loop, but this allows us to add single tasks --%>

<li class="<%= data.completed ? 'complete' : '' %>">
<li class="<%= data.completed ? 'completed' : '' %>">

<div class="view">
<input class="toggle" type="checkbox" checked="<%= data.completed %>"
Expand Down
138 changes: 76 additions & 62 deletions labs/architecture-examples/duel/src/main/webapp/js/todos/controller.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
/*global window */
/*jshint camelcase:false */

var todos = todos || {};

(function( todos, document ) {
(function (todos, document) {
'use strict';

/*-- private members -------------------------------*/

var ENTER_KEY = 13,
STATS_ID = 'footer',
TODOAPP_ID = 'todoapp',
TASKS_ID = 'main',
LIST_ID = 'todo-list',
EDITING_CSS = 'editing';
var ENTER_KEY = 13;
var STATS_ID = 'footer';
var TODOAPP_ID = 'todoapp';
var TASKS_ID = 'main';
var LIST_ID = 'todo-list';
var EDITING_CSS = 'editing';

function getById( id ) {
return document.getElementById( id );
function getById(id) {
return document.getElementById(id);
}

function refreshStats( stats ) {
function refreshStats(stats) {
// get the data
var data = stats || todos.model.stats();

// build the view
var view = todos.views.Stats( data ).toDOM();
var view = todos.views.Stats(data).toDOM();

// replace old stats
var old = getById( STATS_ID );
if ( old ) {
old.parentNode.replaceChild( view, old );
var old = getById(STATS_ID);
if (old) {
old.parentNode.replaceChild(view, old);
} else {
getById( TODOAPP_ID ).appendChild( view );
getById(TODOAPP_ID).appendChild(view);
}
}

Expand All @@ -39,47 +43,47 @@ var todos = todos || {};
};

// build the view
var view = todos.views.Tasks( data ).toDOM();
var view = todos.views.Tasks(data).toDOM();

// replace old task list
var old = getById( TASKS_ID );
if ( old ) {
old.parentNode.replaceChild( view, old );
var old = getById(TASKS_ID);
if (old) {
old.parentNode.replaceChild(view, old);
} else {
getById( TODOAPP_ID ).appendChild( view );
getById(TODOAPP_ID).appendChild(view);
}

refreshStats( data.stats );
refreshStats(data.stats);
}

function add( input ) {
function add(input) {
var title = (input.value || '').trim();
input.value = '';

if ( !title ) {
if (!title) {
return;
}

var task = todos.model.add( title );
var task = todos.model.add(title);

var list = getById( LIST_ID );
if ( list ) {
var list = getById(LIST_ID);
if (list) {
// add new at the top
list.appendChild( todos.views.Task( task ).toDOM() );
list.appendChild(todos.views.Task(task).toDOM());
refreshStats();
} else {
refreshAll();
}
}

function edit( input, id ) {
function edit(input, id) {
var title = (input.value || '').trim();
input.value = title;

if ( title ) {
todos.model.edit( id, title );
if (title) {
todos.model.edit(id, title);
} else {
todos.model.remove( id );
todos.model.remove(id);
}
refreshAll();
}
Expand All @@ -88,82 +92,92 @@ var todos = todos || {};

// event handlers
todos.actions = {
add_blur: function( e ) {
add( this );
addBlur: function () {
add(this);
},

add_keypress: function( e ) {
if ( e.keyCode === ENTER_KEY ) {
add( this );
add_keypress: function (e) {
if (e.keyCode === ENTER_KEY) {
add(this);
}
},

edit_blur: function( id ) {
edit_blur: function (id) {
// create a closure around the ID
return function( e ) {
edit( this, id );
return function () {
edit(this, id);
};
},

edit_keypress: function( id ) {
edit_keypress: function () {
// create a closure around the ID
return function(e) {
if ( e.keyCode === ENTER_KEY ) {
return function (e) {
if (e.keyCode === ENTER_KEY) {
// just blur so doesn't get triggered twice
this.blur();
}
};
},

remove_click: function( id ) {
remove_click: function (id) {
// create a closure around the ID
return function( e ) {
todos.model.remove( id );
return function () {
todos.model.remove(id);
refreshAll();
};
},

clear_click: function() {
clear_click: function () {
todos.model.expunge();
refreshAll();
},

content_dblclick: function( id ) {
content_dblclick: function () {
// create a closure around the ID
return function( e ) {
var li = this;
var toggleEditingMode = function (li) {
if (li.tagName !== 'LI') {
return toggleEditingMode(li.parentNode);
}

li.className = EDITING_CSS;
li.getElementsByTagName( 'input' )[1].focus();

var input = li.getElementsByTagName('input')[1];
input.focus();
input.value = input.value;
};

return function () {
toggleEditingMode(this);
};
},

completed_change: function( id ) {
completed_change: function (id) {
// create a closure around the ID
return function( e ) {
return function () {
var checkbox = this;
todos.model.toggle( id, checkbox.checked );
todos.model.toggle(id, checkbox.checked);
refreshAll();
};
},

toggle_change: function( e ) {
toggle_change: function () {
var checkbox = this;
todos.model.toggleAll( checkbox.checked );
todos.model.toggleAll(checkbox.checked);
refreshAll();
}
};

/*-- init task list -------------------------------*/

(function( body ) {
(function (body) {
// build out task list
var view = todos.views.TodoApp({
tasks: todos.model.tasks(),
stats: todos.model.stats()
}).toDOM();
tasks: todos.model.tasks(),
stats: todos.model.stats()
}).toDOM();

// insert at top
body.insertBefore( view, body.firstChild );
})( document.body );
body.insertBefore(view, body.firstChild);
})(document.body);

})( todos, document );
})(todos, window.document);
Loading

0 comments on commit f83a3ca

Please sign in to comment.