PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/static/lib/inputex/examples/TaskManager/js/TaskManager.js

https://github.com/andrewtracer/dataflow
JavaScript | 248 lines | 145 code | 43 blank | 60 comment | 16 complexity | 7aaaea695800f1b63cabc85140948aa2 MD5 | raw file
  1. // Needed for inputEx
  2. YAHOO.inputEx.spacerUrl = "../images/space.gif";
  3. /**
  4. * Main TaskManager object
  5. */
  6. var TaskManager = {
  7. // Map of YAHOO.widget.TextNode instances in the TreeView instance.
  8. oTextNodeMap: {},
  9. // Remember the last clicked task
  10. oEditingTaskNode: null,
  11. // The YAHOO.widget.TextNode instance whose "contextmenu"
  12. // DOM event triggered the display of the ContextMenu instance.
  13. oCurrentTextNode: null,
  14. // Treeview instance
  15. oTreeView: null,
  16. // Task Details Form
  17. oTaskDetailsForm: null,
  18. /**
  19. * Init the taskManager
  20. */
  21. init: function() {
  22. this.buildTree();
  23. this.initContextMenu();
  24. this.buildForm();
  25. this.initEvents();
  26. this.queryData();
  27. },
  28. /**
  29. * Create the Task Details Form
  30. */
  31. buildForm: function() {
  32. this.oTaskDetailsForm = new YAHOO.inputEx.Group({parentEl: 'taskInformation', fields: [
  33. {inputParams: {label: 'Description', name: 'description'} },
  34. {type: 'date', inputParams: {label: 'Due date', name: 'duedate' } },
  35. {inputParams: {label: 'Tags', name: 'tags' } },
  36. {type:'text', inputParams: {label: 'Comments', name:'comments'}}
  37. ] });
  38. },
  39. /**
  40. * Init the events
  41. */
  42. initEvents: function() {
  43. // Save button
  44. YAHOO.util.Event.addListener('saveTreeButton', 'click', this.onSaveClick, this, true);
  45. // "Add Root Node" button
  46. YAHOO.util.Event.addListener('addRootNode', 'click', function(e) {
  47. YAHOO.util.Event.stopEvent(e);
  48. this.addNode(this.oTreeView.getRoot());
  49. }, this, true);
  50. // Updated event sent by the form
  51. this.oTaskDetailsForm.updatedEvt.subscribe(this.onTaskDetailsUpdated, this, true);
  52. },
  53. /**
  54. * Update the task details value
  55. */
  56. onTaskDetailsUpdated: function(e) {
  57. this.oEditingTaskNode._taskDetails = this.oTaskDetailsForm.getValue();
  58. var sLabel = this.oEditingTaskNode._taskDetails.description;
  59. if (sLabel && sLabel.length > 0) {
  60. this.oEditingTaskNode.getLabelEl().innerHTML = sLabel;
  61. this.oEditingTaskNode.label = sLabel;
  62. }
  63. },
  64. /**
  65. * Save button click
  66. */
  67. onSaveClick: function() {
  68. var value = [];
  69. var rootNode = this.oTreeView.getRoot();
  70. for(var i = 0 ; i < rootNode.children.length ; i++) {
  71. var childNode = rootNode.children[i];
  72. value.push( childNode.toJsObject() );
  73. }
  74. var json = YAHOO.lang.JSON.stringify(value);
  75. YAHOO.util.Dom.get('saveStatus').innerHTML = "saving...";
  76. YAHOO.util.Connect.asyncRequest('POST', 'store.php', {
  77. success: function(o) {
  78. var d = new Date();
  79. YAHOO.util.Dom.get('saveStatus').innerHTML = "saved at "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
  80. },
  81. failure: function(o) {
  82. YAHOO.util.Dom.get('saveStatus').innerHTML = "error !";
  83. }
  84. }, "data="+json);
  85. },
  86. /**
  87. * Ajax request to retreive the data
  88. */
  89. queryData: function() {
  90. YAHOO.util.Connect.asyncRequest('GET', 'store.php', {
  91. success: function(o) {
  92. var childNodes = YAHOO.lang.JSON.parse(o.responseText);
  93. this.populateTree(childNodes);
  94. },
  95. failure: function(o) {
  96. //console.log(o.responseText);
  97. },
  98. scope: this
  99. }, null);
  100. },
  101. /**
  102. * Populate the tree with the tasks
  103. */
  104. populateTree: function(childNodes) {
  105. // Function that render the branch
  106. var buildBranch = function(nodeInfos, parentNode) {
  107. var oTextNode = new YAHOO.widget.TaskNode(nodeInfos.label, parentNode, false, nodeInfos.checked);
  108. this.oTextNodeMap[oTextNode.labelElId] = oTextNode;
  109. oTextNode._taskDetails = nodeInfos._taskDetails;
  110. for(var i = 0 ; i < nodeInfos.children.length ; i++) {
  111. var n = nodeInfos.children[i];
  112. buildBranch.call(this, n, oTextNode);
  113. }
  114. };
  115. for(var i = 0 ; i < childNodes.length ; i++) {
  116. var nodeInfos = childNodes[i];
  117. buildBranch.call(this, nodeInfos, this.oTreeView.getRoot() );
  118. }
  119. this.oTreeView.draw();
  120. },
  121. /**
  122. * Render the treeview
  123. */
  124. buildTree: function() {
  125. // Create a TreeView instance
  126. this.oTreeView = new YAHOO.widget.TreeView("taskTreeView");
  127. },
  128. /**
  129. * Add a "Unamed" node to the parentEl and focus on the description field
  130. */
  131. addNode: function(parentNode) {
  132. var oChildNode = new YAHOO.widget.TaskNode("Unamed", parentNode, false);
  133. this.oTextNodeMap[oChildNode.labelElId] = oChildNode;
  134. parentNode.refresh();
  135. parentNode.expand();
  136. this.setEditingTaskNode(oChildNode);
  137. this.oTaskDetailsForm.inputsNames["description"].el.focus();
  138. this.oTaskDetailsForm.inputsNames["description"].el.setSelectionRange(0,6);
  139. },
  140. /**
  141. * Create the context menu
  142. */
  143. initContextMenu: function() {
  144. var that = this;
  145. var oContextMenu = new YAHOO.widget.ContextMenu("mytreecontextmenu", {
  146. trigger: "taskTreeView",
  147. lazyload: true,
  148. itemdata: [
  149. { text: "Add Subtask", onclick: { fn: function() { that.addNode(that.oCurrentTextNode); } } },
  150. { text: "Delete Task", onclick: { fn: function() {
  151. delete that.oTextNodeMap[that.oCurrentTextNode.labelElId];
  152. that.oTreeView.removeNode(that.oCurrentTextNode);
  153. that.oTreeView.draw();
  154. } } }
  155. ] });
  156. // Subscribe to the "contextmenu" event for the element(s) specified as the "trigger" for the ContextMenu instance.
  157. oContextMenu.subscribe("triggerContextMenu", this.onTriggerContextMenu);
  158. },
  159. /**
  160. * Get the TaskNode instance that that triggered the display of the ContextMenu instance.
  161. * and keep it in TaskManager.oCurrentTextNode
  162. */
  163. onTriggerContextMenu: function(p_oEvent) {
  164. function getTextNodeFromEventTarget(p_oTarget) {
  165. if (p_oTarget.tagName.toUpperCase() == "A" && YAHOO.util.Dom.hasClass(p_oTarget, "ygtvlabel") ) {
  166. return TaskManager.oTextNodeMap[p_oTarget.id];
  167. }
  168. else {
  169. if (p_oTarget.parentNode && p_oTarget.parentNode.nodeType == 1) {
  170. return getTextNodeFromEventTarget(p_oTarget.parentNode);
  171. }
  172. }
  173. }
  174. //
  175. var oTextNode = getTextNodeFromEventTarget(this.contextEventTarget);
  176. if (oTextNode) {
  177. TaskManager.oCurrentTextNode = oTextNode;
  178. }
  179. else {
  180. // Cancel the display of the ContextMenu instance.
  181. this.cancel();
  182. }
  183. },
  184. /**
  185. * Set the CSS class for the newly selected class and update the form
  186. */
  187. setEditingTaskNode: function(node) {
  188. // Update CSS class
  189. if(this.oEditingTaskNode) {
  190. YAHOO.util.Dom.removeClass( this.oEditingTaskNode.getLabelEl(), "selectedTask" );
  191. }
  192. YAHOO.util.Dom.addClass( node.getLabelEl(), "selectedTask" );
  193. // Keep a reference to the last clicked node
  194. this.oEditingTaskNode = node;
  195. // Set the form values
  196. this.oTaskDetailsForm.setValue(this.oEditingTaskNode._taskDetails || {
  197. description: this.oEditingTaskNode.label,
  198. duedate: "",
  199. tags: "",
  200. comments: ""
  201. });
  202. }
  203. };
  204. // Init
  205. YAHOO.util.Event.addListener(window, 'load', TaskManager.init, TaskManager, true);