Commit d494efd3 authored by Sven Franck's avatar Sven Franck

jshint gadgets

parent 41fbc7c5
/*global window, RSVP, FileReader */ /*global window, RSVP, FileReader */
/*jslint indent: 2, maxerr: 3, unparam: true */ /*jslint indent: 2, maxerr: 3, unparam: true */
(function (window, RSVP, FileReader) { (function(window, RSVP, FileReader) {
"use strict"; "use strict";
window.loopEventListener = function (target, type, useCapture, callback, window.loopEventListener = function(target, type, useCapture, callback,
allowDefault) { allowDefault) {
////////////////////////// //////////////////////////
// Infinite event listener (promise is never resolved) // Infinite event listener (promise is never resolved)
// eventListener is removed when promise is cancelled/rejected // eventListener is removed when promise is cancelled/rejected
////////////////////////// //////////////////////////
var handle_event_callback, var handle_event_callback,
callback_promise; callback_promise;
function cancelResolver() { function cancelResolver() {
if ((callback_promise !== undefined) && if ((callback_promise !== undefined) &&
(typeof callback_promise.cancel === "function")) { (typeof callback_promise.cancel === "function")) {
callback_promise.cancel(); callback_promise.cancel();
} }
} }
function canceller() { function canceller() {
if (handle_event_callback !== undefined) { if (handle_event_callback !== undefined) {
target.removeEventListener(type, handle_event_callback, useCapture); target.removeEventListener(type, handle_event_callback, useCapture);
} }
cancelResolver(); cancelResolver();
} }
function itsANonResolvableTrap(resolve, reject) {
handle_event_callback = function (evt) { function itsANonResolvableTrap(resolve, reject) {
evt.stopPropagation();
if (allowDefault !== true) {
evt.preventDefault();
}
cancelResolver();
callback_promise = new RSVP.Queue()
.push(function () {
return callback(evt);
})
.push(undefined, function (error) {
if (!(error instanceof RSVP.CancellationError)) {
canceller();
reject(error);
}
});
};
target.addEventListener(type, handle_event_callback, useCapture); handle_event_callback = function(evt) {
} evt.stopPropagation();
return new RSVP.Promise(itsANonResolvableTrap, canceller); if (allowDefault !== true) {
}; evt.preventDefault();
}
cancelResolver();
callback_promise = new RSVP.Queue()
.push(function() {
return callback(evt);
})
.push(undefined, function(error) {
if (!(error instanceof RSVP.CancellationError)) {
canceller();
reject(error);
}
});
};
window.promiseEventListener = function (target, type, useCapture) { target.addEventListener(type, handle_event_callback, useCapture);
////////////////////////// }
// Resolve the promise as soon as the event is triggered return new RSVP.Promise(itsANonResolvableTrap, canceller);
// eventListener is removed when promise is cancelled/resolved/rejected };
//////////////////////////
var handle_event_callback;
function canceller() { window.promiseEventListener = function(target, type, useCapture) {
target.removeEventListener(type, handle_event_callback, useCapture); //////////////////////////
} // Resolve the promise as soon as the event is triggered
// eventListener is removed when promise is cancelled/resolved/rejected
//////////////////////////
var handle_event_callback;
function resolver(resolve) { function canceller() {
handle_event_callback = function (evt) { target.removeEventListener(type, handle_event_callback, useCapture);
canceller(); }
evt.stopPropagation();
evt.preventDefault();
resolve(evt);
return false;
};
target.addEventListener(type, handle_event_callback, useCapture); function resolver(resolve) {
} handle_event_callback = function(evt) {
return new RSVP.Promise(resolver, canceller); canceller();
}; evt.stopPropagation();
evt.preventDefault();
resolve(evt);
return false;
};
window.promiseReadAsText = function (file) { target.addEventListener(type, handle_event_callback, useCapture);
return new RSVP.Promise(function (resolve, reject) { }
var reader = new FileReader(); return new RSVP.Promise(resolver, canceller);
reader.onload = function (evt) { };
resolve(evt.target.result);
}; window.promiseReadAsText = function(file) {
reader.onerror = function (evt) { return new RSVP.Promise(function(resolve, reject) {
reject(evt); var reader = new FileReader();
}; reader.onload = function(evt) {
reader.readAsText(file); resolve(evt.target.result);
}); };
}; reader.onerror = function(evt) {
}(window, RSVP, FileReader)); reject(evt);
\ No newline at end of file };
reader.readAsText(file);
});
};
}(window, RSVP, FileReader));
/*global define, App, window, RSVP, rJS, jIO */ /*global window, RSVP, rJS, jIO, Date */
/*jshint unused:false */ /*jshint unused:false */
(function (window, RSVP, rJS, jIO) { (function(window, RSVP, rJS, jIO, Date) {
'use strict'; 'use strict';
rJS(window) rJS(window)
// Initialize the gadget as soon as it is loaded in memory, // Initialize the gadget as soon as it is loaded in memory,
// blocking all other methods in itself and its ancestors. // blocking all other methods in itself and its ancestors.
.ready(function () { .ready(function() {
var gadget = this; var gadget = this;
// Create a new jIO storage that supports queries, // Create a new jIO storage that supports queries,
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
// Put a new todo into jIO storage, with an auto-generated ID. // Put a new todo into jIO storage, with an auto-generated ID.
.declareMethod('postTodo', function (title) { .declareMethod('postTodo', function(title) {
var gadget = this; var gadget = this;
var storage = gadget.state.storage; var storage = gadget.state.storage;
return storage.post({ return storage.post({
...@@ -43,50 +43,53 @@ ...@@ -43,50 +43,53 @@
}) })
// Update the properties of an existing todo in jIO storage. // Update the properties of an existing todo in jIO storage.
.declareMethod('putTodo', function (id, todo) { .declareMethod('putTodo', function(id, todo) {
var gadget = this; var gadget = this;
var storage = gadget.state.storage; var storage = gadget.state.storage;
// Get todo from storage first to get all its properties. // Get todo from storage first to get all its properties.
return storage.get(id) return storage.get(id)
.push(function (result) { .push(function(result) {
var key; var key;
// Only overwrite the given properties. // Only overwrite the given properties.
for (key in todo) { for (key in todo) {
if (todo.hasOwnProperty(key)) { if (todo.hasOwnProperty(key)) {
result[key] = todo[key]; result[key] = todo[key];
}
} }
} return result;
return result; },
},
// Reject callback if todo is not found in storage. // Reject callback if todo is not found in storage.
function () { function() {
return todo; return todo;
}) })
.push(function (todo) { .push(function(todo) {
return storage.put(id, todo); return storage.put(id, todo);
}); });
}) })
// Return a list of all todos in storage that match the given query. // Return a list of all todos in storage that match the given query.
.declareMethod('getTodos', function (query) { .declareMethod('getTodos', function(query) {
var gadget = this; var gadget = this;
var storage = gadget.state.storage; var storage = gadget.state.storage;
// Get a list of all todos in storage that match the given query, // Get a list of all todos in storage that match the given query,
// in chronological order, with 'title' and 'completed' properties. // in chronological order, with 'title' and 'completed' properties.
return storage.allDocs({ return storage.allDocs({
query: query, query: query,
sort_on: [['creation_date', 'ascending']], sort_on: [
select_list: ['title', 'completed'] ['creation_date', 'ascending']
}) ],
select_list: ['title', 'completed']
})
// Add the todo IDs into the list. // Add the todo IDs into the list.
.push(function (result_list) { .push(function(result_list) {
var todo_list = [], todo, i; var todo_list = [],
todo, i;
for (i = 0; i < result_list.data.total_rows; i += 1) { for (i = 0; i < result_list.data.total_rows; i += 1) {
todo = result_list.data.rows[i]; todo = result_list.data.rows[i];
todo_list.push({ todo_list.push({
...@@ -100,13 +103,15 @@ ...@@ -100,13 +103,15 @@
}) })
// Get the count of all total todos and all active todos. // Get the count of all total todos and all active todos.
.declareMethod('getTodoCountDict', function () { .declareMethod('getTodoCountDict', function() {
var gadget = this; var gadget = this;
var storage = gadget.state.storage; var storage = gadget.state.storage;
// Get a list of all todos in storage with the 'completed' property // Get a list of all todos in storage with the 'completed' property
return storage.allDocs({select_list: ['completed']}) return storage.allDocs({
.push(function (result_list) { select_list: ['completed']
})
.push(function(result_list) {
var todo_count_dict = { var todo_count_dict = {
total: result_list.data.total_rows, total: result_list.data.total_rows,
active: 0 active: 0
...@@ -123,25 +128,29 @@ ...@@ -123,25 +128,29 @@
}) })
// Change the title of a todo. // Change the title of a todo.
.declareMethod('changeTitle', function (id, title) { .declareMethod('changeTitle', function(id, title) {
var gadget = this; var gadget = this;
return gadget.putTodo(id, {title: title}); return gadget.putTodo(id, {
title: title
});
}) })
// Change the completion status of a todo. // Change the completion status of a todo.
.declareMethod('toggleOne', function (id, completed) { .declareMethod('toggleOne', function(id, completed) {
var gadget = this; var gadget = this;
return gadget.putTodo(id, {completed: completed}); return gadget.putTodo(id, {
completed: completed
});
}) })
// Change the completion status of all todos. // Change the completion status of all todos.
.declareMethod('toggleAll', function (completed) { .declareMethod('toggleAll', function(completed) {
var gadget = this; var gadget = this;
var storage = gadget.state.storage; var storage = gadget.state.storage;
// Get all todos, and change the completion status of each one. // Get all todos, and change the completion status of each one.
return storage.allDocs() return storage.allDocs()
.push(function (result_list) { .push(function(result_list) {
var promise_list = []; var promise_list = [];
for (var i = 0; i < result_list.data.total_rows; i += 1) { for (var i = 0; i < result_list.data.total_rows; i += 1) {
promise_list.push(gadget.toggleOne( promise_list.push(gadget.toggleOne(
...@@ -153,19 +162,19 @@ ...@@ -153,19 +162,19 @@
}) })
// Remove one todo from the storage. // Remove one todo from the storage.
.declareMethod('removeOne', function (id) { .declareMethod('removeOne', function(id) {
var gadget = this; var gadget = this;
var storage = gadget.state.storage; var storage = gadget.state.storage;
return storage.remove(id); return storage.remove(id);
}) })
// Remove all completed todos from the storage. // Remove all completed todos from the storage.
.declareMethod('removeCompleted', function () { .declareMethod('removeCompleted', function() {
var gadget = this; var gadget = this;
// Get a list of all todos, and only remove the completed ones. // Get a list of all todos, and only remove the completed ones.
return gadget.getTodos() return gadget.getTodos()
.push(function (todo_list) { .push(function(todo_list) {
var promise_list = []; var promise_list = [];
for (var i = 0; i < todo_list.length; i += 1) { for (var i = 0; i < todo_list.length; i += 1) {
if (todo_list[i].completed) { if (todo_list[i].completed) {
...@@ -176,4 +185,4 @@ ...@@ -176,4 +185,4 @@
}); });
}); });
}(window, RSVP, rJS, jIO)); }(window, RSVP, rJS, jIO, Date));
/*global define, App, window, RSVP, rJS, loopEventListener */ /*global window, RSVP, rJS, loopEventListener */
/*jshint unused:false */ /*jshint unused:false */
(function (window, RSVP, rJS, loopEventListener) { (function(window, RSVP, rJS, loopEventListener) {
'use strict'; 'use strict';
// Get the appropriate query for allDocs() based on the given hash. // Get the appropriate query for allDocs() based on the given hash.
function getQueryFromHash(hash) { function getQueryFromHash(hash) {
switch (hash) { switch (hash) {
case '#/active': case '#/active':
return 'completed: "false"'; return 'completed: "false"';
case '#/completed': case '#/completed':
return 'completed: "true"'; return 'completed: "true"';
default: default:
return ''; return '';
} }
} }
...@@ -20,17 +20,17 @@ ...@@ -20,17 +20,17 @@
// Initialize the gadget as soon as it is loaded in memory, // Initialize the gadget as soon as it is loaded in memory,
// blocking all other methods in itself and its ancestors. // blocking all other methods in itself and its ancestors.
.ready(function () { .ready(function() {
var gadget = this; var gadget = this;
return gadget.setQuery(getQueryFromHash(window.location.hash)); return gadget.setQuery(getQueryFromHash(window.location.hash));
}) })
// Initialize the gadget as soon as it is loaded in the DOM, // Initialize the gadget as soon as it is loaded in the DOM,
// but only after ready() has finished and stopped blocking. // but only after ready() has finished and stopped blocking.
.declareService(function () { .declareService(function() {
var gadget = this; var gadget = this;
return loopEventListener(window, 'hashchange', false, return loopEventListener(window, 'hashchange', false,
function () { function() {
return gadget.setQuery( return gadget.setQuery(
getQueryFromHash(window.location.hash) getQueryFromHash(window.location.hash)
); );
...@@ -41,4 +41,4 @@ ...@@ -41,4 +41,4 @@
// Declare an acquired method from the parent gadget to use it. // Declare an acquired method from the parent gadget to use it.
.declareAcquiredMethod('setQuery', 'setQuery'); .declareAcquiredMethod('setQuery', 'setQuery');
}(window, RSVP, rJS, loopEventListener)); }(window, RSVP, rJS, loopEventListener));
\ No newline at end of file
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