1 | /* |
---|
2 | * Ext JS Library 0.30 |
---|
3 | * Copyright(c) 2006-2009, Ext JS, LLC. |
---|
4 | * licensing@extjs.com |
---|
5 | * |
---|
6 | * http://extjs.com/license |
---|
7 | */ |
---|
8 | |
---|
9 | |
---|
10 | tx.data.TaskStore = Ext.extend(Ext.data.GroupingStore, { |
---|
11 | constructor: function(){ |
---|
12 | tx.data.TaskStore.superclass.constructor.call(this, { |
---|
13 | sortInfo:{field: 'dueDate', direction: "ASC"}, |
---|
14 | groupField:'dueDate', |
---|
15 | taskFilter: 'all', |
---|
16 | reader: new Ext.data.JsonReader({ |
---|
17 | id: 'taskId', |
---|
18 | fields: tx.data.Task |
---|
19 | }) |
---|
20 | }); |
---|
21 | this.conn = tx.data.conn; |
---|
22 | this.proxy = new Ext.sql.Proxy(tx.data.conn, 'task', 'taskId', this); |
---|
23 | }, |
---|
24 | |
---|
25 | applyFilter : function(filter){ |
---|
26 | if(filter !== undefined){ |
---|
27 | this.taskFilter = filter; |
---|
28 | } |
---|
29 | var value = this.taskFilter; |
---|
30 | if(value == 'all'){ |
---|
31 | return this.clearFilter(); |
---|
32 | } |
---|
33 | return this.filterBy(function(item){ |
---|
34 | return item.data.completed === value; |
---|
35 | }); |
---|
36 | }, |
---|
37 | |
---|
38 | addTask : function(data){ |
---|
39 | this.suspendEvents(); |
---|
40 | this.clearFilter(); |
---|
41 | this.resumeEvents(); |
---|
42 | this.loadData([data], true); |
---|
43 | this.suspendEvents(); |
---|
44 | this.applyFilter(); |
---|
45 | this.applyGrouping(true); |
---|
46 | this.resumeEvents(); |
---|
47 | this.fireEvent('datachanged', this); |
---|
48 | }, |
---|
49 | |
---|
50 | loadList: function(listId){ |
---|
51 | var multi = Ext.isArray(listId); |
---|
52 | this.activeList = multi ? listId[0] : listId; |
---|
53 | this.suspendEvents(); |
---|
54 | if(multi){ |
---|
55 | var ps = []; |
---|
56 | for(var i = 0, len = listId.length; i < len; i++){ |
---|
57 | ps.push('?'); |
---|
58 | } |
---|
59 | this.load({ |
---|
60 | params: { |
---|
61 | where: 'where listId in (' + ps.join(',') + ')', |
---|
62 | args: listId |
---|
63 | } |
---|
64 | }); |
---|
65 | }else{ |
---|
66 | this.load({params: { |
---|
67 | where: 'where listId = ?', |
---|
68 | args: [listId] |
---|
69 | }}); |
---|
70 | } |
---|
71 | this.applyFilter(); |
---|
72 | this.applyGrouping(true); |
---|
73 | this.resumeEvents(); |
---|
74 | this.fireEvent('datachanged', this); |
---|
75 | }, |
---|
76 | |
---|
77 | removeList: function(listId){ |
---|
78 | this.conn.execBy('delete from task where listId = ?', [listId]); |
---|
79 | this.reload(); |
---|
80 | }, |
---|
81 | |
---|
82 | prepareTable : function(){ |
---|
83 | try{ |
---|
84 | this.createTable({ |
---|
85 | name: 'task', |
---|
86 | key: 'taskId', |
---|
87 | fields: tx.data.Task.prototype.fields |
---|
88 | }); |
---|
89 | }catch(e){console.log(e);} |
---|
90 | }, |
---|
91 | |
---|
92 | createTask : function(title, listText, dueDate, description, completed){ |
---|
93 | if(!Ext.isEmpty(title)){ |
---|
94 | var listId = ''; |
---|
95 | if(!Ext.isEmpty(listText)){ |
---|
96 | listId = tx.data.lists.addList(Ext.util.Format.htmlEncode(listText)).id; |
---|
97 | }else{ |
---|
98 | listId = tx.data.lists.newList(false).id; |
---|
99 | } |
---|
100 | this.addTask({ |
---|
101 | taskId: Ext.uniqueId(), |
---|
102 | title: Ext.util.Format.htmlEncode(title), |
---|
103 | dueDate: dueDate||'', |
---|
104 | description: description||'', |
---|
105 | listId: listId, |
---|
106 | completed: completed || false |
---|
107 | }); |
---|
108 | } |
---|
109 | }, |
---|
110 | |
---|
111 | afterEdit : function(r){ |
---|
112 | if(r.isModified(this.getGroupState())){ |
---|
113 | this.applyGrouping(); |
---|
114 | } |
---|
115 | //workaround WebKit cross-frame date issue |
---|
116 | fixDateMember(r.data, 'completedDate'); |
---|
117 | fixDateMember(r.data, 'reminder'); |
---|
118 | fixDateMember(r.data, 'dueDate'); |
---|
119 | if(r.isModified('completed')){ |
---|
120 | r.editing = true; |
---|
121 | r.set('completedDate', r.data.completed ? new Date() : ''); |
---|
122 | r.editing = false; |
---|
123 | } |
---|
124 | tx.data.TaskStore.superclass.afterEdit.apply(this, arguments); |
---|
125 | }, |
---|
126 | |
---|
127 | init : function(){ |
---|
128 | tx.data.lists.load(); |
---|
129 | this.load({ |
---|
130 | callback: function(){ |
---|
131 | // first time? |
---|
132 | if(this.getCount() < 1){ |
---|
133 | Ext.Msg.confirm('Create Tasks?', 'Your database is currently empty. Would you like to insert some demo data?', |
---|
134 | function(btn){ |
---|
135 | if(btn == 'yes'){ |
---|
136 | tx.data.lists.loadDemoLists(); |
---|
137 | this.loadDemoTasks(); |
---|
138 | } |
---|
139 | }, this); |
---|
140 | } |
---|
141 | }, |
---|
142 | scope: this |
---|
143 | }); |
---|
144 | }, |
---|
145 | |
---|
146 | lookup : function(id){ |
---|
147 | var task; |
---|
148 | if(task = this.getById(id)){ |
---|
149 | return task; |
---|
150 | } |
---|
151 | var data = this.proxy.table.lookup(id); |
---|
152 | if (data) { |
---|
153 | var result = this.reader.readRecords([data]); |
---|
154 | return result.records[0]; |
---|
155 | } |
---|
156 | return null; |
---|
157 | }, |
---|
158 | |
---|
159 | /* This is used to laod some demo tasks if the task database is empty */ |
---|
160 | loadDemoTasks: function(){ |
---|
161 | var s = new Date(); |
---|
162 | // hardcoded demo tasks |
---|
163 | this.addTask({taskId: Ext.uniqueId(), title:'Update Ext 2.0 documentation', listId:'ext2', description:'', dueDate: s.add('d', 21), completed: false, reminder: ''}); |
---|
164 | this.addTask({taskId: Ext.uniqueId(), title:'Release Ext 2.l Beta 1', listId:'ext2', description:'', dueDate:s.add('d', 2), completed: false, reminder: s.add('d', 2).clearTime(true).add('h', 9)}); |
---|
165 | this.addTask({taskId: Ext.uniqueId(), title:'Take wife to see movie', listId:'family', description:'', dueDate:s.add('d', 2), completed: false, reminder: ''}); |
---|
166 | this.addTask({taskId: Ext.uniqueId(), title:'Finish Simple Tasks v2 sample app', listId:'ext2', description:'', dueDate:s.add('d', 2), completed: false, reminder: ''}); |
---|
167 | this.addTask({taskId: Ext.uniqueId(), title:'Do something other than work', listId:'fun', description:'', dueDate:s.add('d', -1), completed: false, reminder: ''}); |
---|
168 | this.addTask({taskId: Ext.uniqueId(), title:'Go to the grocery store', listId:'family', description:'', dueDate:s.add('d', -1), completed: true, reminder: '', completedDate: new Date()}); |
---|
169 | this.addTask({taskId: Ext.uniqueId(), title:'Reboot my computer', listId:'personal-misc', description:'', dueDate:s, completed: false, reminder: ''}); |
---|
170 | this.addTask({taskId: Ext.uniqueId(), title:'Respond to emails', listId:'work-misc', description:'', dueDate:s, completed: true, reminder: '', completedDate: new Date()}); |
---|
171 | } |
---|
172 | }); |
---|