_dndContainer.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dijit.tree._dndContainer"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dijit.tree._dndContainer"] = true;
  8. dojo.provide("dijit.tree._dndContainer");
  9. dojo.require("dojo.dnd.common");
  10. dojo.require("dojo.dnd.Container");
  11. dojo.getObject("tree", true, dojo);
  12. dijit.tree._compareNodes = function(n1, n2){
  13. if(n1 === n2){
  14. return 0;
  15. }
  16. if('sourceIndex' in document.documentElement){ //IE
  17. //TODO: does not yet work if n1 and/or n2 is a text node
  18. return n1.sourceIndex - n2.sourceIndex;
  19. }else if('compareDocumentPosition' in document.documentElement){ //FF, Opera
  20. return n1.compareDocumentPosition(n2) & 2 ? 1: -1;
  21. }else if(document.createRange){ //Webkit
  22. var r1 = doc.createRange();
  23. r1.setStartBefore(n1);
  24. var r2 = doc.createRange();
  25. r2.setStartBefore(n2);
  26. return r1.compareBoundaryPoints(r1.END_TO_END, r2);
  27. }else{
  28. throw Error("dijit.tree._compareNodes don't know how to compare two different nodes in this browser");
  29. }
  30. };
  31. dojo.declare("dijit.tree._dndContainer",
  32. null,
  33. {
  34. // summary:
  35. // This is a base class for `dijit.tree._dndSelector`, and isn't meant to be used directly.
  36. // It's modeled after `dojo.dnd.Container`.
  37. // tags:
  38. // protected
  39. /*=====
  40. // current: DomNode
  41. // The currently hovered TreeNode.rowNode (which is the DOM node
  42. // associated w/a given node in the tree, excluding it's descendants)
  43. current: null,
  44. =====*/
  45. constructor: function(tree, params){
  46. // summary:
  47. // A constructor of the Container
  48. // tree: Node
  49. // Node or node's id to build the container on
  50. // params: dijit.tree.__SourceArgs
  51. // A dict of parameters, which gets mixed into the object
  52. // tags:
  53. // private
  54. this.tree = tree;
  55. this.node = tree.domNode; // TODO: rename; it's not a TreeNode but the whole Tree
  56. dojo.mixin(this, params);
  57. // class-specific variables
  58. this.map = {};
  59. this.current = null; // current TreeNode's DOM node
  60. // states
  61. this.containerState = "";
  62. dojo.addClass(this.node, "dojoDndContainer");
  63. // set up events
  64. this.events = [
  65. // container level events
  66. dojo.connect(this.node, "onmouseenter", this, "onOverEvent"),
  67. dojo.connect(this.node, "onmouseleave", this, "onOutEvent"),
  68. // switching between TreeNodes
  69. dojo.connect(this.tree, "_onNodeMouseEnter", this, "onMouseOver"),
  70. dojo.connect(this.tree, "_onNodeMouseLeave", this, "onMouseOut"),
  71. // cancel text selection and text dragging
  72. dojo.connect(this.node, "ondragstart", dojo, "stopEvent"),
  73. dojo.connect(this.node, "onselectstart", dojo, "stopEvent")
  74. ];
  75. },
  76. getItem: function(/*String*/ key){
  77. // summary:
  78. // Returns the dojo.dnd.Item (representing a dragged node) by it's key (id).
  79. // Called by dojo.dnd.Source.checkAcceptance().
  80. // tags:
  81. // protected
  82. var widget = this.selection[key],
  83. ret = {
  84. data: widget,
  85. type: ["treeNode"]
  86. };
  87. return ret; // dojo.dnd.Item
  88. },
  89. destroy: function(){
  90. // summary:
  91. // Prepares this object to be garbage-collected
  92. dojo.forEach(this.events, dojo.disconnect);
  93. // this.clearItems();
  94. this.node = this.parent = null;
  95. },
  96. // mouse events
  97. onMouseOver: function(/*TreeNode*/ widget, /*Event*/ evt){
  98. // summary:
  99. // Called when mouse is moved over a TreeNode
  100. // tags:
  101. // protected
  102. this.current = widget;
  103. },
  104. onMouseOut: function(/*TreeNode*/ widget, /*Event*/ evt){
  105. // summary:
  106. // Called when mouse is moved away from a TreeNode
  107. // tags:
  108. // protected
  109. this.current = null;
  110. },
  111. _changeState: function(type, newState){
  112. // summary:
  113. // Changes a named state to new state value
  114. // type: String
  115. // A name of the state to change
  116. // newState: String
  117. // new state
  118. var prefix = "dojoDnd" + type;
  119. var state = type.toLowerCase() + "State";
  120. //dojo.replaceClass(this.node, prefix + newState, prefix + this[state]);
  121. dojo.replaceClass(this.node, prefix + newState, prefix + this[state]);
  122. this[state] = newState;
  123. },
  124. _addItemClass: function(node, type){
  125. // summary:
  126. // Adds a class with prefix "dojoDndItem"
  127. // node: Node
  128. // A node
  129. // type: String
  130. // A variable suffix for a class name
  131. dojo.addClass(node, "dojoDndItem" + type);
  132. },
  133. _removeItemClass: function(node, type){
  134. // summary:
  135. // Removes a class with prefix "dojoDndItem"
  136. // node: Node
  137. // A node
  138. // type: String
  139. // A variable suffix for a class name
  140. dojo.removeClass(node, "dojoDndItem" + type);
  141. },
  142. onOverEvent: function(){
  143. // summary:
  144. // This function is called once, when mouse is over our container
  145. // tags:
  146. // protected
  147. this._changeState("Container", "Over");
  148. },
  149. onOutEvent: function(){
  150. // summary:
  151. // This function is called once, when mouse is out of our container
  152. // tags:
  153. // protected
  154. this._changeState("Container", "");
  155. }
  156. });
  157. }