_ListMouseMixin.js.uncompressed.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. define("dijit/form/_ListMouseMixin", [
  2. "dojo/_base/declare", // declare
  3. "dojo/mouse",
  4. "dojo/on",
  5. "dojo/touch",
  6. "./_ListBase"
  7. ], function(declare, mouse, on, touch, _ListBase){
  8. // module:
  9. // dijit/form/_ListMouseMixin
  10. return declare( "dijit.form._ListMouseMixin", _ListBase, {
  11. // summary:
  12. // a Mixin to handle mouse or touch events for a focus-less menu
  13. // Abstract methods that must be defined externally:
  14. //
  15. // - onClick: item was chosen (mousedown somewhere on the menu and mouseup somewhere on the menu)
  16. // tags:
  17. // private
  18. postCreate: function(){
  19. this.inherited(arguments);
  20. this.own(on(this.domNode, touch.press, function(evt){ evt.preventDefault(); })); // prevent focus shift on list scrollbar press
  21. this._listConnect(touch.press, "_onMouseDown");
  22. this._listConnect(touch.release, "_onMouseUp");
  23. this._listConnect(mouse.enter, "_onMouseOver");
  24. this._listConnect(mouse.leave, "_onMouseOut");
  25. },
  26. _onMouseDown: function(/*Event*/ evt, /*DomNode*/ target){
  27. if(this._hoveredNode){
  28. this.onUnhover(this._hoveredNode);
  29. this._hoveredNode = null;
  30. }
  31. this._isDragging = true;
  32. this._setSelectedAttr(target);
  33. },
  34. _onMouseUp: function(/*Event*/ evt, /*DomNode*/ target){
  35. this._isDragging = false;
  36. var selectedNode = this.selected;
  37. var hoveredNode = this._hoveredNode;
  38. if(selectedNode && target == selectedNode){
  39. this.onClick(selectedNode);
  40. }else if(hoveredNode && target == hoveredNode){ // drag to select
  41. this._setSelectedAttr(hoveredNode);
  42. this.onClick(hoveredNode);
  43. }
  44. },
  45. _onMouseOut: function(/*Event*/ evt, /*DomNode*/ target){
  46. if(this._hoveredNode){
  47. this.onUnhover(this._hoveredNode);
  48. this._hoveredNode = null;
  49. }
  50. if(this._isDragging){
  51. this._cancelDrag = (new Date()).getTime() + 1000; // cancel in 1 second if no _onMouseOver fires
  52. }
  53. },
  54. _onMouseOver: function(/*Event*/ evt, /*DomNode*/ target){
  55. if(this._cancelDrag){
  56. var time = (new Date()).getTime();
  57. if(time > this._cancelDrag){
  58. this._isDragging = false;
  59. }
  60. this._cancelDrag = null;
  61. }
  62. this._hoveredNode = target;
  63. this.onHover(target);
  64. if(this._isDragging){
  65. this._setSelectedAttr(target);
  66. }
  67. }
  68. });
  69. });