_ListMouseMixin.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. define("dijit/form/_ListMouseMixin", [
  2. "dojo/_base/declare", // declare
  3. "dojo/_base/event", // event.stop
  4. "dojo/touch",
  5. "./_ListBase"
  6. ], function(declare, event, touch, _ListBase){
  7. /*=====
  8. var _ListBase = dijit.form._ListBase;
  9. =====*/
  10. // module:
  11. // dijit/form/_ListMouseMixin
  12. // summary:
  13. // a mixin to handle mouse or touch events for a focus-less menu
  14. return declare( "dijit.form._ListMouseMixin", _ListBase, {
  15. // summary:
  16. // a Mixin to handle mouse or touch events for a focus-less menu
  17. // Abstract methods that must be defined externally:
  18. // onClick: item was chosen (mousedown somewhere on the menu and mouseup somewhere on the menu)
  19. // tags:
  20. // private
  21. postCreate: function(){
  22. this.inherited(arguments);
  23. this.connect(this.domNode, "onclick", function(evt){ this._onClick(evt, this._getTarget(evt)); });
  24. this.connect(this.domNode, "onmousedown", "_onMouseDown");
  25. this.connect(this.domNode, "onmouseup", "_onMouseUp");
  26. this.connect(this.domNode, "onmouseover", "_onMouseOver");
  27. this.connect(this.domNode, "onmouseout", "_onMouseOut");
  28. },
  29. _onClick: function(/*Event*/ evt, /*DomNode*/ target){
  30. this._setSelectedAttr(target, false);
  31. if(this._deferredClick){
  32. this._deferredClick.remove();
  33. }
  34. this._deferredClick = this.defer(function(){
  35. this._deferredClick = null;
  36. this.onClick(target);
  37. });
  38. },
  39. _onMouseDown: function(/*Event*/ evt){
  40. if(this._hoveredNode){
  41. this.onUnhover(this._hoveredNode);
  42. this._hoveredNode = null;
  43. }
  44. this._isDragging = true;
  45. this._setSelectedAttr(this._getTarget(evt), false);
  46. },
  47. _onMouseUp: function(/*Event*/ evt){
  48. this._isDragging = false;
  49. var selectedNode = this._getSelectedAttr();
  50. var target = this._getTarget(evt);
  51. var hoveredNode = this._hoveredNode;
  52. if(selectedNode && target == selectedNode){
  53. this.defer(function(){ this._onClick(evt, selectedNode); });
  54. }else if(hoveredNode){ // drag to select
  55. this.defer(function(){ this._onClick(evt, hoveredNode); });
  56. }
  57. },
  58. _onMouseOut: function(/*Event*/ /*===== evt ====*/){
  59. if(this._hoveredNode){
  60. this.onUnhover(this._hoveredNode);
  61. if(this._getSelectedAttr() == this._hoveredNode){
  62. this.onSelect(this._hoveredNode);
  63. }
  64. this._hoveredNode = null;
  65. }
  66. if(this._isDragging){
  67. this._cancelDrag = (new Date()).getTime() + 1000; // cancel in 1 second if no _onMouseOver fires
  68. }
  69. },
  70. _onMouseOver: function(/*Event*/ evt){
  71. if(this._cancelDrag){
  72. var time = (new Date()).getTime();
  73. if(time > this._cancelDrag){
  74. this._isDragging = false;
  75. }
  76. this._cancelDrag = null;
  77. }
  78. var node = this._getTarget(evt);
  79. if(!node){ return; }
  80. if(this._hoveredNode != node){
  81. if(this._hoveredNode){
  82. this._onMouseOut({ target: this._hoveredNode });
  83. }
  84. if(node && node.parentNode == this.containerNode){
  85. if(this._isDragging){
  86. this._setSelectedAttr(node, false);
  87. }else{
  88. this._hoveredNode = node;
  89. this.onHover(node);
  90. }
  91. }
  92. }
  93. }
  94. });
  95. });