]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | /**\r |
2 | * @class SimpleTasks.view.lists.Tree\r | |
3 | * @extends Ext.tree.Panel\r | |
4 | * The task list view. A tree that displays all of the task lists.\r | |
5 | */\r | |
6 | Ext.define('SimpleTasks.view.lists.Tree', {\r | |
7 | extend: 'Ext.tree.Panel',\r | |
8 | xtype: 'listTree',\r | |
9 | requires: [\r | |
10 | 'Ext.grid.plugin.CellEditing',\r | |
11 | 'Ext.tree.plugin.TreeViewDragDrop',\r | |
12 | 'Ext.grid.column.Action'\r | |
13 | ],\r | |
14 | title: 'Lists',\r | |
15 | store: 'Lists',\r | |
16 | hideHeaders: true,\r | |
17 | \r | |
18 | dockedItems: [\r | |
19 | {\r | |
20 | xtype: 'toolbar',\r | |
21 | dock: 'bottom',\r | |
22 | items: [\r | |
23 | {\r | |
24 | iconCls: 'tasks-new-list',\r | |
25 | tooltip: 'New List'\r | |
26 | },\r | |
27 | {\r | |
28 | iconCls: 'tasks-delete-list',\r | |
29 | id: 'delete-list-btn',\r | |
30 | tooltip: 'Delete List'\r | |
31 | },\r | |
32 | {\r | |
33 | iconCls: 'tasks-new-folder',\r | |
34 | tooltip: 'New Folder'\r | |
35 | },\r | |
36 | {\r | |
37 | iconCls: 'tasks-delete-folder',\r | |
38 | id: 'delete-folder-btn',\r | |
39 | tooltip: 'Delete Folder'\r | |
40 | }\r | |
41 | ]\r | |
42 | }\r | |
43 | ],\r | |
44 | \r | |
45 | viewConfig: {\r | |
46 | plugins: {\r | |
47 | ptype: 'tasksdragdrop',\r | |
48 | dragText: 'Drag to reorder',\r | |
49 | ddGroup: 'task'\r | |
50 | }\r | |
51 | },\r | |
52 | \r | |
53 | /**\r | |
54 | * @event deleteclick\r | |
55 | * Fires when the delete icon is clicked\r | |
56 | * @param {Ext.grid.View} gridView\r | |
57 | * @param {Number} rowIndex\r | |
58 | * @param {Number} colIndex\r | |
59 | * @param {Ext.grid.column.Action} column\r | |
60 | * @param {EventObject} e\r | |
61 | */\r | |
62 | \r | |
63 | /**\r | |
64 | * @event taskdrop\r | |
65 | * Fires when a task record is dropped on this grid\r | |
66 | * @param {SimpleTasks.model.Task} task The task record\r | |
67 | * @param {SimpleTasks.model.List} list The list that the task was dropped on\r | |
68 | */\r | |
69 | \r | |
70 | /**\r | |
71 | * @event listdrop\r | |
72 | * Fires when a list record is dropped on this grid\r | |
73 | * @param {SimpleTasks.model.List} list The list that was dropped\r | |
74 | * @param {SimpleTasks.model.List} overList The list that the list was dropped on\r | |
75 | * @param {String} position `"before"` or `"after"` depending on whether the mouse is above or below the midline of the node.\r | |
76 | */\r | |
77 | \r | |
78 | initComponent: function() {\r | |
79 | var me = this;\r | |
80 | \r | |
81 | /**\r | |
82 | * This Tree Panel's cell editing plugin\r | |
83 | * @property cellEditingPlugin\r | |
84 | * @type Ext.grid.plugin.CellEditing\r | |
85 | */\r | |
86 | me.plugins = [me.cellEditingPlugin = Ext.create('Ext.grid.plugin.CellEditing')];\r | |
87 | \r | |
88 | me.columns = [\r | |
89 | {\r | |
90 | xtype: 'treecolumn',\r | |
91 | dataIndex: 'name',\r | |
92 | flex: 1,\r | |
93 | editor: {\r | |
94 | xtype: 'textfield',\r | |
95 | selectOnFocus: true,\r | |
96 | allowOnlyWhitespace: false\r | |
97 | },\r | |
98 | renderer: Ext.bind(me.renderName, me)\r | |
99 | },\r | |
100 | {\r | |
101 | xtype: 'actioncolumn',\r | |
102 | width: 24,\r | |
103 | icon: 'resources/images/delete.png',\r | |
104 | iconCls: 'x-hidden',\r | |
105 | tooltip: 'Delete',\r | |
106 | handler: Ext.bind(me.handleDeleteClick, me)\r | |
107 | }\r | |
108 | ];\r | |
109 | \r | |
110 | me.callParent(arguments);\r | |
111 | \r | |
112 | me.on('beforeedit', me.handleBeforeEdit, me);\r | |
113 | me.relayEvents(me.getView(), ['taskdrop', 'listdrop']);\r | |
114 | \r | |
115 | },\r | |
116 | \r | |
117 | /**\r | |
118 | * Handles a click on a delete icon\r | |
119 | * @private\r | |
120 | * @param {Ext.tree.View} treeView\r | |
121 | * @param {Number} rowIndex\r | |
122 | * @param {Number} colIndex\r | |
123 | * @param {Ext.grid.column.Action} column\r | |
124 | * @param {EventObject} e\r | |
125 | */\r | |
126 | handleDeleteClick: function(gridView, rowIndex, colIndex, column, e) {\r | |
127 | // Fire a "deleteclick" event with all the same args as this handler\r | |
128 | this.fireEvent('deleteclick', gridView, rowIndex, colIndex, column, e);\r | |
129 | },\r | |
130 | \r | |
131 | /**\r | |
132 | * Handles this grid's "beforeedit" event (relayed from the CellEditing plugin).\r | |
133 | * Prevents editing of "All Lists" root by returning false if the record has an id of -1\r | |
134 | * @private\r | |
135 | * @param {Ext.grid.plugin.CellEditing} editingPlugin The cell editing plugin\r | |
136 | * @param {Object} e an edit event object\r | |
137 | */\r | |
138 | handleBeforeEdit: function(editingPlugin, e) {\r | |
139 | return e.record.get('id') !== -1;\r | |
140 | },\r | |
141 | \r | |
142 | /**\r | |
143 | * Renderer for the name field.\r | |
144 | * Adds the task count after the list name.\r | |
145 | * @private\r | |
146 | * @param {String} value\r | |
147 | * @param {Object} metaData\r | |
148 | * @param {SimpleTasks.model.List} list\r | |
149 | * @param {Number} rowIndex\r | |
150 | * @param {Number} colIndex\r | |
151 | * @param {SimpleTasks.store.Lists} store\r | |
152 | * @param {Ext.grid.View} view\r | |
153 | */\r | |
154 | renderName: function(value, metaData, list, rowIndex, colIndex, store, view) {\r | |
155 | var tasksStore = Ext.StoreMgr.lookup('Tasks'),\r | |
156 | count = 0;\r | |
157 | \r | |
158 | (function countTasks(list) {\r | |
159 | count += tasksStore.queryBy(function(task, id) {\r | |
160 | // only show count for tasks that are not done\r | |
161 | return task.get('list_id') === list.get('id') && task.get('done') === false;\r | |
162 | }).getCount();\r | |
163 | \r | |
164 | list.eachChild(function(child) {\r | |
165 | countTasks(child);\r | |
166 | });\r | |
167 | }(list));\r | |
168 | \r | |
169 | return value + ' (' + count + ')';\r | |
170 | },\r | |
171 | \r | |
172 | /**\r | |
173 | * Triggers the list tree to refresh its view. This is necessary in two scenarios:\r | |
174 | * 1) Since the lists and tasks are loaded asyncrounously, The Lists store may have finished\r | |
175 | * loading before the tasks store. In this case, the tasks data would not be available so all\r | |
176 | * of the task counts would be rendered as (0).\r | |
177 | * 2) When a task is dragged and dropped onto a list, or when a list is deleted the task count won't automatially be updated\r | |
178 | * because none of the data in the lists store actually changed (the renderer gets the count\r | |
179 | * from the tasks store).\r | |
180 | * \r | |
181 | * In both situations refreshing the lists view we ensure that the task counts are accurate.\r | |
182 | */\r | |
183 | refreshView: function() {\r | |
184 | // refresh the data in the view. This will trigger the column renderers to run, making sure the task counts are up to date.\r | |
185 | this.getView().refresh();\r | |
186 | }\r | |
187 | \r | |
188 | \r | |
189 | \r | |
190 | });\r |