Unreviewed, rolling out r216677.
[WebKit-https.git] / PerformanceTests / Speedometer / resources / todomvc / dependency-examples / flight / flight / app / js / data / todos.js
1 /*global define */
2 'use strict';
3
4 define([
5     'flight/lib/component',
6     'app/store'
7 ], function (defineComponent, dataStore) {
8     function todos() {
9         var filter;
10         this.attributes({
11             dataStore: dataStore
12         });
13
14         this.add = function (e, data) {
15             var todo = this.attr.dataStore.save({
16                 title: data.title,
17                 completed: false
18             });
19
20             this.trigger('dataTodoAdded', { todo: todo, filter: filter });
21         };
22
23         this.remove = function (e, data) {
24             var todo = this.attr.dataStore.destroy(data.id);
25
26             this.trigger('dataTodoRemoved', todo);
27         };
28
29         this.load = function () {
30             var todos;
31
32             // filter = localStorage.getItem('filter');
33             filter = '';
34             todos = this.find();
35             this.trigger('dataTodosLoaded', { todos: todos });
36         };
37
38         this.update = function (e, data) {
39             this.attr.dataStore.save(data);
40         };
41
42         this.toggleCompleted = function (e, data) {
43             var eventType;
44             var todo = this.attr.dataStore.get(data.id);
45
46             todo.completed = !todo.completed;
47             this.attr.dataStore.save(todo);
48
49             eventType = filter ? 'dataTodoRemoved' : 'dataTodoToggled';
50
51             this.trigger(eventType, todo);
52         };
53
54         this.toggleAllCompleted = function (e, data) {
55             this.attr.dataStore.updateAll({ completed: data.completed });
56             this.trigger('dataTodoToggledAll', { todos: this.find(filter) });
57         };
58
59         this.filter = function (e, data) {
60             var todos;
61
62             // localStorage.setItem('filter', data.filter);
63             filter = data.filter;
64             todos = this.find();
65
66             this.trigger('dataTodosFiltered', { todos: todos });
67         };
68
69         this.find = function () {
70             var todos;
71
72             if (filter) {
73                 todos = this.attr.dataStore.find(function (each) {
74                     return (typeof each[filter] !== 'undefined') ? each.completed : !each.completed;
75                 });
76             } else {
77                 todos = this.attr.dataStore.all();
78             }
79
80             return todos;
81         };
82
83         this.clearCompleted = function () {
84             this.attr.dataStore.destroyAll({ completed: true });
85
86             this.trigger('uiFilterRequested', { filter: filter });
87             this.trigger('dataClearedCompleted');
88         };
89
90         this.after('initialize', function () {
91             this.on(document, 'uiAddRequested', this.add);
92             this.on(document, 'uiUpdateRequested', this.update);
93             this.on(document, 'uiRemoveRequested', this.remove);
94             this.on(document, 'uiLoadRequested', this.load);
95             this.on(document, 'uiToggleRequested', this.toggleCompleted);
96             this.on(document, 'uiToggleAllRequested', this.toggleAllCompleted);
97             this.on(document, 'uiClearRequested', this.clearCompleted);
98             this.on(document, 'uiFilterRequested', this.filter);
99         });
100     }
101
102     return defineComponent(todos);
103 });