Commit d494efd3 authored by Sven Franck's avatar Sven Franck

jshint gadgets

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