TreeSelector.js
上传用户:kimgenplus
上传日期:2016-06-05
资源大小:20877k
文件大小:3k
源码类别:

OA系统

开发平台:

Java

  1. /*
  2. Copyright (c) 2004-2006, The Dojo Foundation
  3. All Rights Reserved.
  4. Licensed under the Academic Free License version 2.1 or above OR the
  5. modified BSD license. For more information on Dojo licensing, see:
  6. http://dojotoolkit.org/community/licensing.shtml
  7. */
  8. dojo.provide("dojo.widget.TreeSelector");
  9. dojo.require("dojo.widget.HtmlWidget");
  10. dojo.widget.defineWidget("dojo.widget.TreeSelector", dojo.widget.HtmlWidget, function () {
  11. this.eventNames = {};
  12. this.listenedTrees = [];
  13. }, {widgetType:"TreeSelector", selectedNode:null, dieWithTree:false, eventNamesDefault:{select:"select", destroy:"destroy", deselect:"deselect", dblselect:"dblselect"}, initialize:function () {
  14. for (var name in this.eventNamesDefault) {
  15. if (dojo.lang.isUndefined(this.eventNames[name])) {
  16. this.eventNames[name] = this.widgetId + "/" + this.eventNamesDefault[name];
  17. }
  18. }
  19. }, destroy:function () {
  20. dojo.event.topic.publish(this.eventNames.destroy, {source:this});
  21. return dojo.widget.HtmlWidget.prototype.destroy.apply(this, arguments);
  22. }, listenTree:function (tree) {
  23. dojo.event.topic.subscribe(tree.eventNames.titleClick, this, "select");
  24. dojo.event.topic.subscribe(tree.eventNames.iconClick, this, "select");
  25. dojo.event.topic.subscribe(tree.eventNames.collapse, this, "onCollapse");
  26. dojo.event.topic.subscribe(tree.eventNames.moveFrom, this, "onMoveFrom");
  27. dojo.event.topic.subscribe(tree.eventNames.removeNode, this, "onRemoveNode");
  28. dojo.event.topic.subscribe(tree.eventNames.treeDestroy, this, "onTreeDestroy");
  29. this.listenedTrees.push(tree);
  30. }, unlistenTree:function (tree) {
  31. dojo.event.topic.unsubscribe(tree.eventNames.titleClick, this, "select");
  32. dojo.event.topic.unsubscribe(tree.eventNames.iconClick, this, "select");
  33. dojo.event.topic.unsubscribe(tree.eventNames.collapse, this, "onCollapse");
  34. dojo.event.topic.unsubscribe(tree.eventNames.moveFrom, this, "onMoveFrom");
  35. dojo.event.topic.unsubscribe(tree.eventNames.removeNode, this, "onRemoveNode");
  36. dojo.event.topic.unsubscribe(tree.eventNames.treeDestroy, this, "onTreeDestroy");
  37. for (var i = 0; i < this.listenedTrees.length; i++) {
  38. if (this.listenedTrees[i] === tree) {
  39. this.listenedTrees.splice(i, 1);
  40. break;
  41. }
  42. }
  43. }, onTreeDestroy:function (message) {
  44. this.unlistenTree(message.source);
  45. if (this.dieWithTree) {
  46. this.destroy();
  47. }
  48. }, onCollapse:function (message) {
  49. if (!this.selectedNode) {
  50. return;
  51. }
  52. var node = message.source;
  53. var parent = this.selectedNode.parent;
  54. while (parent !== node && parent.isTreeNode) {
  55. parent = parent.parent;
  56. }
  57. if (parent.isTreeNode) {
  58. this.deselect();
  59. }
  60. }, select:function (message) {
  61. var node = message.source;
  62. var e = message.event;
  63. if (this.selectedNode === node) {
  64. if (e.ctrlKey || e.shiftKey || e.metaKey) {
  65. this.deselect();
  66. return;
  67. }
  68. dojo.event.topic.publish(this.eventNames.dblselect, {node:node});
  69. return;
  70. }
  71. if (this.selectedNode) {
  72. this.deselect();
  73. }
  74. this.doSelect(node);
  75. dojo.event.topic.publish(this.eventNames.select, {node:node});
  76. }, onMoveFrom:function (message) {
  77. if (message.child !== this.selectedNode) {
  78. return;
  79. }
  80. if (!dojo.lang.inArray(this.listenedTrees, message.newTree)) {
  81. this.deselect();
  82. }
  83. }, onRemoveNode:function (message) {
  84. if (message.child !== this.selectedNode) {
  85. return;
  86. }
  87. this.deselect();
  88. }, doSelect:function (node) {
  89. node.markSelected();
  90. this.selectedNode = node;
  91. }, deselect:function () {
  92. var node = this.selectedNode;
  93. this.selectedNode = null;
  94. node.unMarkSelected();
  95. dojo.event.topic.publish(this.eventNames.deselect, {node:node});
  96. }});