You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

455 lines
13 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. Ext.onReady(function(){
  16. Ext.QuickTips.init();
  17. var xg = Ext.grid;
  18. // turn off default shadows which look funky in air
  19. xg.GridEditor.prototype.shadow = false;
  20. var conn = Ext.data.SqlDB.getInstance();
  21. conn.open('tasks.db');
  22. // the main grid store
  23. var taskStore = new TaskStore(conn);
  24. // Category store shared by category combos
  25. var catStore = new CategoryStore();
  26. taskStore.load({
  27. callback: function(){
  28. // first time?
  29. if(taskStore.getCount() < 1){
  30. Ext.Msg.confirm('Create Tasks?', 'Your database is currently empty. Would you like to insert some demo data?',
  31. function(btn){
  32. if(btn == 'yes'){
  33. loadDemoTasks(taskStore);
  34. }
  35. catStore.init(taskStore);
  36. });
  37. }else{
  38. catStore.init(taskStore);
  39. }
  40. }
  41. });
  42. // custom event to notify when a new category is available
  43. taskStore.on('newcategory', catStore.addCategory, catStore);
  44. // set of event handlers shared by combos to allow them to share
  45. // the same local store
  46. var comboEvents = {
  47. focus: function(){
  48. this.bindStore(catStore);
  49. },
  50. blur: function(c){
  51. catStore.purgeListeners();
  52. }
  53. }
  54. var completeColumn = new CompleteColumn();
  55. // custom template for the grid header
  56. var headerTpl = new Ext.Template(
  57. '<table border="0" cellspacing="0" cellpadding="0" style="{tstyle}">',
  58. '<thead><tr class="x-grid3-hd-row">{cells}</tr></thead>',
  59. '<tbody><tr class="new-task-row">',
  60. '<td><div id="new-task-icon"></div></td>',
  61. '<td><div class="x-small-editor" id="new-task-title"></div></td>',
  62. '<td><div class="x-small-editor" id="new-task-cat"></div></td>',
  63. '<td><div class="x-small-editor" id="new-task-due"></div></td>',
  64. '</tr></tbody>',
  65. "</table>"
  66. );
  67. var selections = new Ext.grid.RowSelectionModel();
  68. // The main grid in all its configuration option glory
  69. var grid = new xg.EditorGridPanel({
  70. id:'tasks-grid',
  71. store: taskStore,
  72. sm: selections,
  73. clicksToEdit: 'auto',
  74. enableColumnHide:false,
  75. enableColumnMove:false,
  76. border:false,
  77. title:'All Tasks',
  78. iconCls:'icon-show-all',
  79. region:'center',
  80. plugins: completeColumn,
  81. columns: [
  82. completeColumn,
  83. {
  84. header: "Task",
  85. width:400,
  86. sortable: true,
  87. dataIndex: 'title',
  88. id:'task-title',
  89. editor: new Ext.form.TextField({
  90. allowBlank: false
  91. })
  92. },
  93. {
  94. header: "Category",
  95. width:150,
  96. sortable: true,
  97. dataIndex: 'category',
  98. editor: new Ext.form.ComboBox({
  99. displayField: 'text',
  100. triggerAction: 'all',
  101. mode:'local',
  102. selectOnFocus:true,
  103. listClass:'x-combo-list-small',
  104. listeners: comboEvents
  105. })
  106. },
  107. {
  108. header: "Due Date",
  109. width: 150,
  110. sortable: true,
  111. renderer: Ext.util.Format.dateRenderer('D m/d/Y'),
  112. dataIndex: 'dueDate',
  113. groupRenderer: textDate(),
  114. groupName: 'Due',
  115. editor: new Ext.form.DateField({
  116. format : "m/d/Y"
  117. })
  118. }
  119. ],
  120. view: new Ext.grid.GroupingView({
  121. forceFit:true,
  122. ignoreAdd: true,
  123. emptyText: 'No Tasks to display',
  124. templates: {
  125. header: headerTpl
  126. },
  127. getRowClass : function(r){
  128. var d = r.data;
  129. if(d.completed){
  130. return 'task-completed';
  131. }
  132. if(d.dueDate && d.dueDate.getTime() < new Date().clearTime().getTime()){
  133. return 'task-overdue';
  134. }
  135. return '';
  136. }
  137. })
  138. });
  139. var viewPanel = new Ext.Panel({
  140. frame:true,
  141. title: 'Views',
  142. collapsible:true,
  143. contentEl:'task-views',
  144. titleCollapse: true
  145. });
  146. var taskActions = new Ext.Panel({
  147. frame:true,
  148. title: 'Task Actions',
  149. collapsible:true,
  150. contentEl:'task-actions',
  151. titleCollapse: true
  152. });
  153. var groupActions = new Ext.Panel({
  154. frame:true,
  155. title: 'Task Grouping',
  156. collapsible:true,
  157. contentEl:'task-grouping',
  158. titleCollapse: true
  159. });
  160. var actionPanel = new Ext.Panel({
  161. id:'action-panel',
  162. region:'west',
  163. split:true,
  164. collapsible: true,
  165. collapseMode: 'mini',
  166. header: false,
  167. width:200,
  168. minWidth: 150,
  169. border: false,
  170. baseCls:'x-plain',
  171. items: [taskActions, viewPanel, groupActions]
  172. });
  173. if(Ext.isAir){ // create AIR window
  174. var win = new Ext.air.MainWindow({
  175. layout:'border',
  176. items: [actionPanel, grid],
  177. title: 'Simple Tasks',
  178. iconCls: 'icon-show-all'
  179. }).render();
  180. }else{
  181. var viewport = new Ext.Viewport({
  182. layout:'border',
  183. items: [actionPanel, grid]
  184. });
  185. }
  186. var ab = actionPanel.body;
  187. ab.on('mousedown', doAction, null, {delegate:'a'});
  188. ab.on('click', Ext.emptyFn, null, {delegate:'a', preventDefault:true});
  189. grid.on('resize', syncFields);
  190. grid.on('columnresize', syncFields);
  191. grid.on('afteredit', function(e){
  192. if(e.field == 'category'){
  193. catStore.addCategory(e.value);
  194. }
  195. if(e.field == taskStore.getGroupState()){
  196. taskStore.applyGrouping();
  197. }
  198. });
  199. grid.on('keydown', function(e){
  200. if(e.getKey() == e.DELETE && !grid.editing){
  201. actions['action-delete']();
  202. }
  203. });
  204. selections.on('selectionchange', function(sm){
  205. var bd = taskActions.body, c = sm.getCount();
  206. bd.select('li:not(#new-task)').setDisplayed(c > 0);
  207. bd.select('span.s').setDisplayed(c > 1);
  208. });
  209. // The fields in the grid's header
  210. var ntTitle = new Ext.form.TextField({
  211. renderTo: 'new-task-title',
  212. emptyText: 'Add a task...'
  213. });
  214. var ntCat = new Ext.form.ComboBox({
  215. renderTo: 'new-task-cat',
  216. disabled:true,
  217. displayField: 'text',
  218. triggerAction: 'all',
  219. mode:'local',
  220. selectOnFocus:true,
  221. listClass:'x-combo-list-small',
  222. listeners: comboEvents
  223. });
  224. var ntDue = new Ext.form.DateField({
  225. renderTo: 'new-task-due',
  226. value: new Date(),
  227. disabled:true,
  228. format : "m/d/Y"
  229. });
  230. // syncs the header fields' widths with the grid column widths
  231. function syncFields(){
  232. var cm = grid.getColumnModel();
  233. ntTitle.setSize(cm.getColumnWidth(1)-2);
  234. ntCat.setSize(cm.getColumnWidth(2)-4);
  235. ntDue.setSize(cm.getColumnWidth(3)-4);
  236. }
  237. syncFields();
  238. var editing = false, focused = false, userTriggered = false;
  239. var handlers = {
  240. focus: function(){
  241. focused = true;
  242. },
  243. blur: function(){
  244. focused = false;
  245. doBlur.defer(250);
  246. },
  247. specialkey: function(f, e){
  248. if(e.getKey()==e.ENTER){
  249. userTriggered = true;
  250. e.stopEvent();
  251. f.el.blur();
  252. if(f.triggerBlur){
  253. f.triggerBlur();
  254. }
  255. }
  256. }
  257. }
  258. ntTitle.on(handlers);
  259. ntCat.on(handlers);
  260. ntDue.on(handlers);
  261. ntTitle.on('focus', function(){
  262. focused = true;
  263. if(!editing){
  264. ntCat.enable();
  265. ntDue.enable();
  266. syncFields();
  267. editing = true;
  268. }
  269. });
  270. // when a field in the add bar is blurred, this determines
  271. // whether a new task should be created
  272. function doBlur(){
  273. if(editing && !focused){
  274. var title = ntTitle.getValue();
  275. if(!Ext.isEmpty(title)){
  276. taskStore.addTask({
  277. taskId: Task.nextId(),
  278. title: title,
  279. dueDate: ntDue.getValue()||'',
  280. description: '', // ???
  281. category: ntCat.getValue(),
  282. completed: false
  283. });
  284. ntTitle.setValue('');
  285. if(userTriggered){ // if the entered to add the task, then go to a new add automatically
  286. userTriggered = false;
  287. ntTitle.focus.defer(100, ntTitle);
  288. }
  289. }
  290. ntCat.disable();
  291. ntDue.disable();
  292. editing = false;
  293. }
  294. }
  295. var actions = {
  296. 'view-all' : function(){
  297. taskStore.applyFilter('all');
  298. grid.setTitle('All Tasks', 'icon-show-all');
  299. },
  300. 'view-active' : function(){
  301. taskStore.applyFilter(false);
  302. grid.setTitle('Active Tasks', 'icon-show-active');
  303. },
  304. 'view-complete' : function(){
  305. taskStore.applyFilter(true);
  306. grid.setTitle('Completed Tasks', 'icon-show-complete');
  307. },
  308. 'action-new' : function(){
  309. ntTitle.focus();
  310. },
  311. 'action-complete' : function(){
  312. selections.each(function(s){
  313. s.set('completed', true);
  314. });
  315. taskStore.applyFilter();
  316. },
  317. 'action-active' : function(){
  318. selections.each(function(s){
  319. s.set('completed', false);
  320. });
  321. taskStore.applyFilter();
  322. },
  323. 'action-delete' : function(){
  324. Ext.Msg.confirm('Confirm', 'Are you sure you want to delete the selected task(s)?',
  325. function(btn){
  326. if(btn == 'yes'){
  327. selections.each(function(s){
  328. taskStore.remove(s);
  329. });
  330. }
  331. });
  332. },
  333. 'group-date' : function(){
  334. taskStore.groupBy('dueDate');
  335. },
  336. 'group-cat' : function(){
  337. taskStore.groupBy('category');
  338. },
  339. 'no-group' : function(){
  340. taskStore.clearGrouping();
  341. }
  342. };
  343. function doAction(e, t){
  344. e.stopEvent();
  345. actions[t.id]();
  346. }
  347. // generates a renderer function to be used for textual date groups
  348. function textDate(){
  349. // create the cache of ranges to be reused
  350. var today = new Date().clearTime(true);
  351. var year = today.getFullYear();
  352. var todayTime = today.getTime();
  353. var yesterday = today.add('d', -1).getTime();
  354. var tomorrow = today.add('d', 1).getTime();
  355. var weekDays = today.add('d', 6).getTime();
  356. var lastWeekDays = today.add('d', -6).getTime();
  357. return function(date){
  358. if(!date) {
  359. return '(No Date)';
  360. }
  361. var notime = date.clearTime(true).getTime();
  362. if (notime == todayTime) {
  363. return 'Today';
  364. }
  365. if(notime > todayTime){
  366. if (notime == tomorrow) {
  367. return 'Tomorrow';
  368. }
  369. if (notime <= weekDays) {
  370. return date.format('l');
  371. }
  372. }else {
  373. if(notime == yesterday) {
  374. return 'Yesterday';
  375. }
  376. if(notime >= lastWeekDays) {
  377. return 'Last ' + date.format('l');
  378. }
  379. }
  380. return date.getFullYear() == year ? date.format('D m/d') : date.format('D m/d/Y');
  381. }
  382. }
  383. });
  384. /* This is used to laod some demo tasks if the task database is empty */
  385. function loadDemoTasks(store){
  386. var s = new Date();
  387. // hardcoded demo tasks
  388. store.addTask({taskId: Task.nextId(), title:'Start documentation of Ext 2.0', category:'Ext', description:'', dueDate: s.add('d', 21), completed: false});
  389. store.addTask({taskId: Task.nextId(), title:'Release Ext 1.l Beta 2', category:'Ext', description:'', dueDate:s.add('d', 2), completed: false});
  390. store.addTask({taskId: Task.nextId(), title:'Take wife to see movie', category:'Family', description:'', dueDate:s.add('d', 2), completed: false});
  391. store.addTask({taskId: Task.nextId(), title:'Finish task list demo app', category:'Ext', description:'', dueDate:s.add('d', 2), completed: false});
  392. store.addTask({taskId: Task.nextId(), title:'Do something other than work', category:'Family', description:'', dueDate:s.add('d', -1), completed: false});
  393. store.addTask({taskId: Task.nextId(), title:'Go to the grocery store', category:'Family', description:'', dueDate:s.add('d', -1), completed: true});
  394. store.addTask({taskId: Task.nextId(), title:'Reboot my computer', category:'Misc', description:'', dueDate:s, completed: false});
  395. store.addTask({taskId: Task.nextId(), title:'Respond to emails', category:'Ext', description:'', dueDate:s, completed: true});
  396. }