source: Dev/branches/rest-dojo-ui/client/dojo/mouse.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 3.9 KB
RevLine 
[256]1define(["./_base/kernel", "./on", "./has", "./dom", "./_base/window"], function(dojo, on, has, dom, win){
2
3        /*=====
4        dojo.mouse = {
5        // summary:
6        //              This module provide mouse event handling utility functions and exports
7        //              mouseenter and mouseleave event emulation.
8        // enter: Synthetic Event
9        //              This is an extension event for the mouseenter that IE provides, emulating the
10        //              behavior on other browsers.
11        // leave: Synthetic Event
12        //              This is an extension event for the mouseleave that IE provides, emulating the
13        //              behavior on other browsers.
14        // isLeft: Function
15        //              Test an event object (from a mousedown event) to see if the left button was pressed.
16        // isMiddle: Function
17        //              Test an event object (from a mousedown event) to see if the middle button was pressed.
18        // isRight: Function
19        //              Test an event object (from a mousedown event) to see if the right button was pressed.
20        // example:
21        //              To use these events, you register a mouseenter like this:
22        //              |       define(["dojo/on", dojo/mouse"], function(on, mouse){
23        //              |               on(targetNode, mouse.enter, function(event){
24        //              |                       dojo.addClass(targetNode, "highlighted");
25        //              |               });
26        //              |               on(targetNode, mouse.leave, function(event){
27        //              |                       dojo.removeClass(targetNode, "highlighted");
28        //              |               });
29        };
30        ======*/
31
32    has.add("dom-quirks", win.doc && win.doc.compatMode == "BackCompat");
33        has.add("events-mouseenter", win.doc && "onmouseenter" in win.doc.createElement("div"));
34
35        var mouseButtons;
36        if(has("dom-quirks") || !has("dom-addeventlistener")){
37                mouseButtons = {
38                        LEFT:   1,
39                        MIDDLE: 4,
40                        RIGHT:  2,
41                        // helper functions
42                        isButton: function(e, button){ return e.button & button; },
43                        isLeft:   function(e){ return e.button & 1; },
44                        isMiddle: function(e){ return e.button & 4; },
45                        isRight:  function(e){ return e.button & 2; }
46                };
47        }else{
48                mouseButtons = {
49                        LEFT:   0,
50                        MIDDLE: 1,
51                        RIGHT:  2,
52                        // helper functions
53                        isButton: function(e, button){ return e.button == button; },
54                        isLeft:   function(e){ return e.button == 0; },
55                        isMiddle: function(e){ return e.button == 1; },
56                        isRight:  function(e){ return e.button == 2; }
57                };
58        }
59        dojo.mouseButtons = mouseButtons;
60
61/*=====
62        dojo.mouseButtons = {
63                // LEFT: Number
64                //              Numeric value of the left mouse button for the platform.
65                LEFT:   0,
66                // MIDDLE: Number
67                //              Numeric value of the middle mouse button for the platform.
68                MIDDLE: 1,
69                // RIGHT: Number
70                //              Numeric value of the right mouse button for the platform.
71                RIGHT:  2,
72
73                isButton: function(e, button){
74                        // summary:
75                        //              Checks an event object for a pressed button
76                        // e: Event
77                        //              Event object to examine
78                        // button: Number
79                        //              The button value (example: dojo.mouseButton.LEFT)
80                        return e.button == button; // Boolean
81                },
82                isLeft: function(e){
83                        // summary:
84                        //              Checks an event object for the pressed left button
85                        // e: Event
86                        //              Event object to examine
87                        return e.button == 0; // Boolean
88                },
89                isMiddle: function(e){
90                        // summary:
91                        //              Checks an event object for the pressed middle button
92                        // e: Event
93                        //              Event object to examine
94                        return e.button == 1; // Boolean
95                },
96                isRight: function(e){
97                        // summary:
98                        //              Checks an event object for the pressed right button
99                        // e: Event
100                        //              Event object to examine
101                        return e.button == 2; // Boolean
102                }
103        };
104=====*/
105
106        function eventHandler(type, mustBubble){
107                // emulation of mouseenter/leave with mouseover/out using descendant checking
108                var handler = function(node, listener){
109                        return on(node, type, function(evt){
110                                if(!dom.isDescendant(evt.relatedTarget, mustBubble ? evt.target : node)){
111                                        return listener.call(this, evt);
112                                }
113                        });
114                };
115                if(!mustBubble){
116                        handler.bubble = eventHandler(type, true);
117                }
118                return handler;
119        }
120        return {
121                enter: eventHandler("mouseover"),
122                leave: eventHandler("mouseout"),
123                isLeft: mouseButtons.isLeft,
124                isMiddle: mouseButtons.isMiddle,
125                isRight: mouseButtons.isRight
126        };
127});
Note: See TracBrowser for help on using the repository browser.