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) {
"use strict";
(function(window, RSVP, FileReader) {
"use strict";
window.loopEventListener = function (target, type, useCapture, callback,
allowDefault) {
//////////////////////////
// Infinite event listener (promise is never resolved)
// eventListener is removed when promise is cancelled/rejected
//////////////////////////
var handle_event_callback,
callback_promise;
window.loopEventListener = function(target, type, useCapture, callback,
allowDefault) {
//////////////////////////
// Infinite event listener (promise is never resolved)
// eventListener is removed when promise is cancelled/rejected
//////////////////////////
var handle_event_callback,
callback_promise;
function cancelResolver() {
if ((callback_promise !== undefined) &&
(typeof callback_promise.cancel === "function")) {
callback_promise.cancel();
}
}
function cancelResolver() {
if ((callback_promise !== undefined) &&
(typeof callback_promise.cancel === "function")) {
callback_promise.cancel();
}
}
function canceller() {
if (handle_event_callback !== undefined) {
target.removeEventListener(type, handle_event_callback, useCapture);
}
cancelResolver();
}
function itsANonResolvableTrap(resolve, reject) {
function canceller() {
if (handle_event_callback !== undefined) {
target.removeEventListener(type, handle_event_callback, useCapture);
}
cancelResolver();
}
handle_event_callback = function (evt) {
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);
}
});
};
function itsANonResolvableTrap(resolve, reject) {
target.addEventListener(type, handle_event_callback, useCapture);
}
return new RSVP.Promise(itsANonResolvableTrap, canceller);
};
handle_event_callback = function(evt) {
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);
}
});
};
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
//////////////////////////
var handle_event_callback;
target.addEventListener(type, handle_event_callback, useCapture);
}
return new RSVP.Promise(itsANonResolvableTrap, canceller);
};
function canceller() {
target.removeEventListener(type, handle_event_callback, 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
//////////////////////////
var handle_event_callback;
function resolver(resolve) {
handle_event_callback = function (evt) {
canceller();
evt.stopPropagation();
evt.preventDefault();
resolve(evt);
return false;
};
function canceller() {
target.removeEventListener(type, handle_event_callback, useCapture);
}
target.addEventListener(type, handle_event_callback, useCapture);
}
return new RSVP.Promise(resolver, canceller);
};
function resolver(resolve) {
handle_event_callback = function(evt) {
canceller();
evt.stopPropagation();
evt.preventDefault();
resolve(evt);
return false;
};
window.promiseReadAsText = function (file) {
return new RSVP.Promise(function (resolve, reject) {
var reader = new FileReader();
reader.onload = function (evt) {
resolve(evt.target.result);
};
reader.onerror = function (evt) {
reject(evt);
};
reader.readAsText(file);
});
};
}(window, RSVP, FileReader));
\ No newline at end of file
target.addEventListener(type, handle_event_callback, useCapture);
}
return new RSVP.Promise(resolver, canceller);
};
window.promiseReadAsText = function(file) {
return new RSVP.Promise(function(resolve, reject) {
var reader = new FileReader();
reader.onload = function(evt) {
resolve(evt.target.result);
};
reader.onerror = function(evt) {
reject(evt);
};
reader.readAsText(file);
});
};
}(window, RSVP, FileReader));
/*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';
rJS(window)
// Initialize the gadget as soon as it is loaded in memory,
// blocking all other methods in itself and its ancestors.
.ready(function () {
// blocking all other methods in itself and its ancestors.
.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,50 +43,53 @@
})
// 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) {
var key;
.push(function(result) {
var key;
// Only overwrite the given properties.
for (key in todo) {
if (todo.hasOwnProperty(key)) {
result[key] = todo[key];
// Only overwrite the given properties.
for (key in todo) {
if (todo.hasOwnProperty(key)) {
result[key] = todo[key];
}
}
}
return result;
},
return result;
},
// Reject callback if todo is not found in storage.
function () {
return todo;
})
// Reject callback if todo is not found in storage.
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;
// Get a list of all todos in storage that match the given query,
// in chronological order, with 'title' and 'completed' properties.
return storage.allDocs({
query: query,
sort_on: [['creation_date', 'ascending']],
select_list: ['title', 'completed']
})
query: query,
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';
// Get the appropriate query for allDocs() based on the given hash.
function getQueryFromHash(hash) {
switch (hash) {
case '#/active':
return 'completed: "false"';
case '#/completed':
return 'completed: "true"';
default:
return '';
case '#/active':
return 'completed: "false"';
case '#/completed':
return 'completed: "true"';
default:
return '';
}
}
......@@ -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)
);
......@@ -41,4 +41,4 @@
// Declare an acquired method from the parent gadget to use it.
.declareAcquiredMethod('setQuery', 'setQuery');
}(window, RSVP, rJS, loopEventListener));
\ No newline at end of file
}(window, RSVP, rJS, loopEventListener));
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