Grid.js
11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**
* @class SimpleTasks.view.tasks.List
* @extends Ext.grid.Panel
* The tasks list view. A grid that displays a list of tasks.
*/
Ext.define('SimpleTasks.view.tasks.Grid', {
extend: 'Ext.grid.Panel',
xtype: 'taskGrid',
requires: [
'SimpleTasks.ux.DragDrop',
'SimpleTasks.ux.CheckColumn',
'SimpleTasks.ux.ReminderColumn',
'Ext.grid.plugin.CellEditing',
'Ext.grid.column.Action',
'Ext.grid.column.Date',
'Ext.grid.feature.Grouping',
'Ext.grid.plugin.DragDrop',
'Ext.ux.TreePicker'
],
store: 'Tasks',
viewConfig: {
plugins: {
ptype: 'gridviewdragdrop',
ddGroup: 'task',
dragText: 'Drag task to change list',
enableDrop: false
},
getRowClass: function(record, rowIndex, rowParams, store){
var due = record.get('due');
if(record.get('done')) {
return 'tasks-completed-task';
} else if(due && (due < Ext.Date.clearTime(new Date()))) {
return 'tasks-overdue-task';
}
}
},
dockedItems: [
{
xtype: 'taskForm',
dock: 'top',
// the grid's column headers are a docked item with a weight of 100.
// giving this a weight of 101 causes it to be docked under the column headers
weight: 101,
bodyStyle: {
'background-color': '#E4E5E7'
}
}
],
initComponent: function() {
var me = this,
cellEditingPlugin = Ext.create('Ext.grid.plugin.CellEditing'),
groupingFeature = Ext.create('Ext.grid.feature.Grouping', {
groupHeaderTpl: [
'{groupValue:this.renderDueDate}',
{
renderDueDate: me.renderDueDate
}
],
enableGroupingMenu: false
});
me.plugins = [cellEditingPlugin];
me.features = [groupingFeature];
me.columns = {
defaults: {
draggable: false,
resizable: false,
hideable: false
},
items: [
{
xtype: 'checkcolumn',
dataIndex: 'done',
cls: 'tasks-icon-column-header tasks-done-column-header',
width: 24,
align: 'center',
menuDisabled: true,
sortable: false,
listeners: {
'checkchange': Ext.bind(me.handleCheckChange, me)
}
},
{
text: 'Title',
dataIndex: 'title',
flex: 1,
emptyCellText: '',
editor: {
xtype: 'textfield',
selectOnFocus: true
}
},
{
text: 'List',
dataIndex: 'list_id',
width: 200,
editor: {
xtype: 'treepicker',
displayField: 'name',
store: Ext.create('SimpleTasks.store.Lists', {storeId: 'Lists-TaskGrid' })
},
renderer: me.renderList
},
{
xtype: 'datecolumn',
text: 'Due Date',
dataIndex: 'due',
width: 90,
editor: 'datefield',
format: 'n/j/Y',
emptyCellText: ''
},
{
xtype: 'remindercolumn',
dataIndex: 'reminder',
cls: 'tasks-icon-column-header tasks-reminder-column-header',
width: 24,
tooltip: 'Set Reminder',
menuPosition: 'tr-br',
menuDisabled: true,
sortable: false,
emptyCellText: '',
listeners: {
select: Ext.bind(me.handleReminderSelect, me)
}
},
{
xtype: 'actioncolumn',
cls: 'tasks-icon-column-header tasks-edit-column-header',
width: 24,
icon: 'resources/images/edit_task.png',
iconCls: 'x-hidden',
tooltip: 'Edit',
menuDisabled: true,
sortable: false,
handler: Ext.bind(me.handleEditClick, me)
},
{
xtype: 'actioncolumn',
cls: 'tasks-icon-column-header tasks-delete-column-header',
width: 24,
icon: 'resources/images/delete.png',
iconCls: 'x-hidden',
tooltip: 'Delete',
menuDisabled: true,
sortable: false,
handler: Ext.bind(me.handleDeleteClick, me)
}
]
};
me.callParent(arguments);
me.addEvents(
/**
* @event editclick
* Fires when an edit icon is clicked
* @param {Ext.grid.View} view
* @param {Number} rowIndex
* @param {Number} colIndex
* @param {Ext.grid.column.Action} column
* @param {EventObject} e
*/
'editclick',
/**
* @event deleteclick
* Fires when a delete icon is clicked
* @param {Ext.grid.View} view
* @param {Number} rowIndex
* @param {Number} colIndex
* @param {Ext.grid.column.Action} column
* @param {EventObject} e
*/
'deleteclick',
/**
* @event edit
* Fires when a record is edited using the CellEditing plugin or the checkcolumn
* @param {SimpleTasks.model.Task} task The task record that was edited
*/
'recordedit',
/**
* @event reminderselect
* Fires when a reminder time is selected from the reminder column's dropdown menu
* @param {SimpleTasks.model.Task} task the underlying record of the row that was clicked to show the reminder menu
* @param {String|Number} value The value that was selected
*/
'reminderselect'
);
cellEditingPlugin.on('edit', me.handleCellEdit, this);
},
/**
* Handles a click on the edit icon
* @private
* @param {Ext.grid.View} gridView
* @param {Number} rowIndex
* @param {Number} colIndex
* @param {Ext.grid.column.Action} column
* @param {EventObject} e
*/
handleEditClick: function(gridView, rowIndex, colIndex, column, e) {
// Fire a "deleteclick" event with all the same args as this handler
this.fireEvent('editclick', gridView, rowIndex, colIndex, column, e);
},
/**
* Handles a click on a delete icon
* @private
* @param {Ext.grid.View} gridView
* @param {Number} rowIndex
* @param {Number} colIndex
* @param {Ext.grid.column.Action} column
* @param {EventObject} e
*/
handleDeleteClick: function(gridView, rowIndex, colIndex, column, e) {
// Fire a "deleteclick" event with all the same args as this handler
this.fireEvent('deleteclick', gridView, rowIndex, colIndex, column, e);
},
/**
* Handles a "checkchange" event on the "done" column
* @private
* @param {SimpleTasks.ux.CheckColumn} column
* @param {Number} rowIndex
* @param {Boolean} checked
*/
handleCheckChange: function(column, rowIndex, checked) {
this.fireEvent('recordedit', this.store.getAt(rowIndex));
},
/**
* Handles a "select" event on the reminder column
* @private
* @param {SimpleTasks.model.Task} task the underlying record of the row that was clicked to show the reminder menu
* @param {String|Number} value The value that was selected
*/
handleReminderSelect: function(task, value) {
this.fireEvent('reminderselect', task, value);
},
/**
* Handles the CellEditing plugin's "edit" event
* @private
* @param {Ext.grid.plugin.CellEditing} editor
* @param {Object} e an edit event object
*/
handleCellEdit: function(editor, e) {
this.fireEvent('recordedit', e.record);
},
/**
* Reapplies the store's current filters. This is needed because when data in the store is modified
* after filters have been applied, the filters do not automatically get applied to the new data.
*/
refreshFilters: function() {
var store = this.store,
filters = store.filters;
// save a reference to the existing task filters before clearing them
filters = filters.getRange(0, filters.getCount() - 1);
// clear the tasks store's filters and reapply them.
store.clearFilter();
store.filter(filters);
},
/**
* Renderer for the list column
* @private
* @param {Number} value
* @param {Object} metaData
* @param {SimpleTasks.model.Task} task
* @param {Number} rowIndex
* @param {Number} colIndex
* @param {SimpleTasks.store.Tasks} store
* @param {Ext.grid.View} view
*/
renderList: function(value, metaData, task, rowIndex, colIndex, store, view) {
var listsStore = Ext.getStore('Lists'),
node = value ? listsStore.getNodeById(value) : listsStore.getRootNode();
return node.get('name');
},
/**
* Renderer for the group headers
* @private
* @param {Date} date
*/
renderDueDate: function(date) {
var today = Ext.Date.clearTime(new Date()),
todayTime = today.getTime(),
dueDateTime;
if(!date) {
return '(No Date)';
}
dueDateTime = Ext.Date.clearTime(date).getTime();
if(dueDateTime === todayTime) {
return 'Today';
}
if(dueDateTime > todayTime) {
if(dueDateTime === Ext.Date.add(today, Ext.Date.DAY, 1).getTime()) {
// due date is current date + 1 day
return 'Tomorrow';
}
if(dueDateTime < Ext.Date.add(today, Ext.Date.DAY, 7).getTime()) {
// if the due date is less than one week in the future, return the day of the week.
return Ext.Date.format(date, 'l');
}
} else {
if(dueDateTime === Ext.Date.add(today, Ext.Date.DAY, -1).getTime()) {
// due date is current date - 1 day.
return 'Yesterday';
}
if(dueDateTime > Ext.Date.add(today, Ext.Date.DAY, -7).getTime()) {
// if the due date is less than one week past, return 'Last' + the day of the week.
return 'Last '+ Ext.Date.format(date, 'l');
}
}
return date.getFullYear() === today.getFullYear() ? Ext.Date.format(date, 'D m/d') : Ext.Date.format(date, 'D m/d/Y');
}
});