[483] | 1 | define(["./_base/kernel", "./on", "./has", "./dom", "./_base/window"], function(dojo, on, has, dom, win){ |
---|
| 2 | |
---|
| 3 | // module: |
---|
| 4 | // dojo/mouse |
---|
| 5 | |
---|
| 6 | has.add("dom-quirks", win.doc && win.doc.compatMode == "BackCompat"); |
---|
| 7 | has.add("events-mouseenter", win.doc && "onmouseenter" in win.doc.createElement("div")); |
---|
| 8 | has.add("events-mousewheel", win.doc && 'onmousewheel' in win.doc); |
---|
| 9 | |
---|
| 10 | var mouseButtons; |
---|
| 11 | if((has("dom-quirks") && has("ie")) || !has("dom-addeventlistener")){ |
---|
| 12 | mouseButtons = { |
---|
| 13 | LEFT: 1, |
---|
| 14 | MIDDLE: 4, |
---|
| 15 | RIGHT: 2, |
---|
| 16 | // helper functions |
---|
| 17 | isButton: function(e, button){ return e.button & button; }, |
---|
| 18 | isLeft: function(e){ return e.button & 1; }, |
---|
| 19 | isMiddle: function(e){ return e.button & 4; }, |
---|
| 20 | isRight: function(e){ return e.button & 2; } |
---|
| 21 | }; |
---|
| 22 | }else{ |
---|
| 23 | mouseButtons = { |
---|
| 24 | LEFT: 0, |
---|
| 25 | MIDDLE: 1, |
---|
| 26 | RIGHT: 2, |
---|
| 27 | // helper functions |
---|
| 28 | isButton: function(e, button){ return e.button == button; }, |
---|
| 29 | isLeft: function(e){ return e.button == 0; }, |
---|
| 30 | isMiddle: function(e){ return e.button == 1; }, |
---|
| 31 | isRight: function(e){ return e.button == 2; } |
---|
| 32 | }; |
---|
| 33 | } |
---|
| 34 | dojo.mouseButtons = mouseButtons; |
---|
| 35 | |
---|
| 36 | /*===== |
---|
| 37 | dojo.mouseButtons = { |
---|
| 38 | // LEFT: Number |
---|
| 39 | // Numeric value of the left mouse button for the platform. |
---|
| 40 | LEFT: 0, |
---|
| 41 | // MIDDLE: Number |
---|
| 42 | // Numeric value of the middle mouse button for the platform. |
---|
| 43 | MIDDLE: 1, |
---|
| 44 | // RIGHT: Number |
---|
| 45 | // Numeric value of the right mouse button for the platform. |
---|
| 46 | RIGHT: 2, |
---|
| 47 | |
---|
| 48 | isButton: function(e, button){ |
---|
| 49 | // summary: |
---|
| 50 | // Checks an event object for a pressed button |
---|
| 51 | // e: Event |
---|
| 52 | // Event object to examine |
---|
| 53 | // button: Number |
---|
| 54 | // The button value (example: dojo.mouseButton.LEFT) |
---|
| 55 | return e.button == button; // Boolean |
---|
| 56 | }, |
---|
| 57 | isLeft: function(e){ |
---|
| 58 | // summary: |
---|
| 59 | // Checks an event object for the pressed left button |
---|
| 60 | // e: Event |
---|
| 61 | // Event object to examine |
---|
| 62 | return e.button == 0; // Boolean |
---|
| 63 | }, |
---|
| 64 | isMiddle: function(e){ |
---|
| 65 | // summary: |
---|
| 66 | // Checks an event object for the pressed middle button |
---|
| 67 | // e: Event |
---|
| 68 | // Event object to examine |
---|
| 69 | return e.button == 1; // Boolean |
---|
| 70 | }, |
---|
| 71 | isRight: function(e){ |
---|
| 72 | // summary: |
---|
| 73 | // Checks an event object for the pressed right button |
---|
| 74 | // e: Event |
---|
| 75 | // Event object to examine |
---|
| 76 | return e.button == 2; // Boolean |
---|
| 77 | } |
---|
| 78 | }; |
---|
| 79 | =====*/ |
---|
| 80 | |
---|
| 81 | function eventHandler(type, selectHandler){ |
---|
| 82 | // emulation of mouseenter/leave with mouseover/out using descendant checking |
---|
| 83 | var handler = function(node, listener){ |
---|
| 84 | return on(node, type, function(evt){ |
---|
| 85 | if(selectHandler){ |
---|
| 86 | return selectHandler(evt, listener); |
---|
| 87 | } |
---|
| 88 | if(!dom.isDescendant(evt.relatedTarget, node)){ |
---|
| 89 | return listener.call(this, evt); |
---|
| 90 | } |
---|
| 91 | }); |
---|
| 92 | }; |
---|
| 93 | handler.bubble = function(select){ |
---|
| 94 | return eventHandler(type, function(evt, listener){ |
---|
| 95 | // using a selector, use the select function to determine if the mouse moved inside the selector and was previously outside the selector |
---|
| 96 | var target = select(evt.target); |
---|
| 97 | var relatedTarget = evt.relatedTarget; |
---|
| 98 | if(target && (target != (relatedTarget && relatedTarget.nodeType == 1 && select(relatedTarget)))){ |
---|
| 99 | return listener.call(target, evt); |
---|
| 100 | } |
---|
| 101 | }); |
---|
| 102 | }; |
---|
| 103 | return handler; |
---|
| 104 | } |
---|
| 105 | var wheel; |
---|
| 106 | if(has("events-mousewheel")){ |
---|
| 107 | wheel = 'mousewheel'; |
---|
| 108 | }else{ //firefox |
---|
| 109 | wheel = function(node, listener){ |
---|
| 110 | return on(node, 'DOMMouseScroll', function(evt){ |
---|
| 111 | evt.wheelDelta = -evt.detail; |
---|
| 112 | listener.call(this, evt); |
---|
| 113 | }); |
---|
| 114 | }; |
---|
| 115 | } |
---|
| 116 | return { |
---|
| 117 | // summary: |
---|
| 118 | // This module provide mouse event handling utility functions and exports |
---|
| 119 | // mouseenter and mouseleave event emulation. |
---|
| 120 | // example: |
---|
| 121 | // To use these events, you register a mouseenter like this: |
---|
| 122 | // | define(["dojo/on", dojo/mouse"], function(on, mouse){ |
---|
| 123 | // | on(targetNode, mouse.enter, function(event){ |
---|
| 124 | // | dojo.addClass(targetNode, "highlighted"); |
---|
| 125 | // | }); |
---|
| 126 | // | on(targetNode, mouse.leave, function(event){ |
---|
| 127 | // | dojo.removeClass(targetNode, "highlighted"); |
---|
| 128 | // | }); |
---|
| 129 | |
---|
| 130 | _eventHandler: eventHandler, // for dojo/touch |
---|
| 131 | |
---|
| 132 | // enter: Synthetic Event |
---|
| 133 | // This is an extension event for the mouseenter that IE provides, emulating the |
---|
| 134 | // behavior on other browsers. |
---|
| 135 | enter: eventHandler("mouseover"), |
---|
| 136 | |
---|
| 137 | // leave: Synthetic Event |
---|
| 138 | // This is an extension event for the mouseleave that IE provides, emulating the |
---|
| 139 | // behavior on other browsers. |
---|
| 140 | leave: eventHandler("mouseout"), |
---|
| 141 | |
---|
| 142 | // wheel: Normalized Mouse Wheel Event |
---|
| 143 | // This is an extension event for the mousewheel that non-Mozilla browsers provide, |
---|
| 144 | // emulating the behavior on Mozilla based browsers. |
---|
| 145 | wheel: wheel, |
---|
| 146 | |
---|
| 147 | isLeft: mouseButtons.isLeft, |
---|
| 148 | /*===== |
---|
| 149 | isLeft: function(){ |
---|
| 150 | // summary: |
---|
| 151 | // Test an event object (from a mousedown event) to see if the left button was pressed. |
---|
| 152 | }, |
---|
| 153 | =====*/ |
---|
| 154 | |
---|
| 155 | isMiddle: mouseButtons.isMiddle, |
---|
| 156 | /*===== |
---|
| 157 | isMiddle: function(){ |
---|
| 158 | // summary: |
---|
| 159 | // Test an event object (from a mousedown event) to see if the middle button was pressed. |
---|
| 160 | }, |
---|
| 161 | =====*/ |
---|
| 162 | |
---|
| 163 | isRight: mouseButtons.isRight |
---|
| 164 | /*===== |
---|
| 165 | , isRight: function(){ |
---|
| 166 | // summary: |
---|
| 167 | // Test an event object (from a mousedown event) to see if the right button was pressed. |
---|
| 168 | } |
---|
| 169 | =====*/ |
---|
| 170 | }; |
---|
| 171 | }); |
---|