Debian Codegraph @

dijit/typematic.js

raw symbols SCIP
1 define([
2 "dojo/_base/array", // array.forEach
3 "dojo/_base/connect", // connect._keyPress
4 "dojo/_base/lang", // lang.mixin, lang.hitch
5 "dojo/on",
6 "dojo/sniff", // has("ie")
7 "./main" // setting dijit.typematic global
8 ], function(array, connect, lang, on, has, dijit){
9
10 // module:
11 // dijit/typematic
12
13 var typematic = (dijit.typematic = {
14 // summary:
15 // These functions are used to repetitively call a user specified callback
16 // method when a specific key or mouse click over a specific DOM node is
17 // held down for a specific amount of time.
18 // Only 1 such event is allowed to occur on the browser page at 1 time.
19
20 _fireEventAndReload: function(){
21 this._timer = null;
22 this._callback(++this._count, this._node, this._evt);
23
24 // Schedule next event, timer is at most minDelay (default 10ms) to avoid
25 // browser overload (particularly avoiding starving DOH robot so it never gets to send a mouseup)
26 this._currentTimeout = Math.max(
27 this._currentTimeout < 0 ? this._initialDelay :
28 (this._subsequentDelay > 1 ? this._subsequentDelay : Math.round(this._currentTimeout * this._subsequentDelay)),
29 this._minDelay);
30 this._timer = setTimeout(lang.hitch(this, "_fireEventAndReload"), this._currentTimeout);
31 },
32
33 trigger: function(/*Event*/ evt, /*Object*/ _this, /*DOMNode*/ node, /*Function*/ callback, /*Object*/ obj, /*Number?*/ subsequentDelay, /*Number?*/ initialDelay, /*Number?*/ minDelay){
34 // summary:
35 // Start a timed, repeating callback sequence.
36 // If already started, the function call is ignored.
37 // This method is not normally called by the user but can be
38 // when the normal listener code is insufficient.
39 // evt:
40 // key or mouse event object to pass to the user callback
41 // _this:
42 // pointer to the user's widget space.
43 // node:
44 // the DOM node object to pass the the callback function
45 // callback:
46 // function to call until the sequence is stopped called with 3 parameters:
47 // count:
48 // integer representing number of repeated calls (0..n) with -1 indicating the iteration has stopped
49 // node:
50 // the DOM node object passed in
51 // evt:
52 // key or mouse event object
53 // obj:
54 // user space object used to uniquely identify each typematic sequence
55 // subsequentDelay:
56 // if > 1, the number of milliseconds until the 3->n events occur
57 // or else the fractional time multiplier for the next event's delay, default=0.9
58 // initialDelay:
59 // the number of milliseconds until the 2nd event occurs, default=500ms
60 // minDelay:
61 // the maximum delay in milliseconds for event to fire, default=10ms
62 if(obj != this._obj){
63 this.stop();
64 this._initialDelay = initialDelay || 500;
65 this._subsequentDelay = subsequentDelay || 0.90;
66 this._minDelay = minDelay || 10;
67 this._obj = obj;
68 this._node = node;
69 this._currentTimeout = -1;
70 this._count = -1;
71 this._callback = lang.hitch(_this, callback);
72 this._evt = { faux: true };
73 for(var attr in evt){
74 if(attr != "layerX" && attr != "layerY"){ // prevent WebKit warnings
75 var v = evt[attr];
76 if(typeof v != "function" && typeof v != "undefined"){
77 this._evt[attr] = v
78 }
79 }
80 }
81 this._fireEventAndReload();
82 }
83 },
84
85 stop: function(){
86 // summary:
87 // Stop an ongoing timed, repeating callback sequence.
88 if(this._timer){
89 clearTimeout(this._timer);
90 this._timer = null;
91 }
92 if(this._obj){
93 this._callback(-1, this._node, this._evt);
94 this._obj = null;
95 }
96 },
97
98 addKeyListener: function(/*DOMNode*/ node, /*Object*/ keyObject, /*Object*/ _this, /*Function*/ callback, /*Number*/ subsequentDelay, /*Number*/ initialDelay, /*Number?*/ minDelay){
99 // summary:
100 // Start listening for a specific typematic key.
101 // See also the trigger method for other parameters.
102 // keyObject:
103 // an object defining the key to listen for:
104 //
105 // - keyCode: the keyCode (number) to listen for, used for non-printable keys
106 // - charCode: the charCode (number) to listen for, used for printable keys
107 // - charOrCode: deprecated, use keyCode or charCode
108 // - ctrlKey: desired ctrl key state to initiate the callback sequence:
109 // - pressed (true)
110 // - released (false)
111 // - either (unspecified)
112 // - altKey: same as ctrlKey but for the alt key
113 // - shiftKey: same as ctrlKey but for the shift key
114 // returns:
115 // a connection handle
116
117 // Setup keydown or keypress listener depending on whether keyCode or charCode was specified.
118 // If charOrCode is specified use deprecated connect._keypress synthetic event (remove for 2.0)
119 var type = "keyCode" in keyObject ? "keydown" : "charCode" in keyObject ? "keypress" : connect._keypress,
120 attr = "keyCode" in keyObject ? "keyCode" : "charCode" in keyObject ? "charCode" : "charOrCode";
121
122 var handles = [
123 on(node, type, lang.hitch(this, function(evt){
124 if(evt[attr] == keyObject[attr] &&
125 (keyObject.ctrlKey === undefined || keyObject.ctrlKey == evt.ctrlKey) &&
126 (keyObject.altKey === undefined || keyObject.altKey == evt.altKey) &&
127 (keyObject.metaKey === undefined || keyObject.metaKey == (evt.metaKey || false)) && // IE doesn't even set metaKey
128 (keyObject.shiftKey === undefined || keyObject.shiftKey == evt.shiftKey)){
129 evt.stopPropagation();
130 evt.preventDefault();
131 typematic.trigger(evt, _this, node, callback, keyObject, subsequentDelay, initialDelay, minDelay);
132 }else if(typematic._obj == keyObject){
133 typematic.stop();
134 }
135 })),
136 on(node, "keyup", lang.hitch(this, function(){
137 if(typematic._obj == keyObject){
138 typematic.stop();
139 }
140 }))
141 ];
142 return { remove: function(){
143 array.forEach(handles, function(h){
144 h.remove();
145 });
146 } };
147 },
148
149 addMouseListener: function(/*DOMNode*/ node, /*Object*/ _this, /*Function*/ callback, /*Number*/ subsequentDelay, /*Number*/ initialDelay, /*Number?*/ minDelay){
150 // summary:
151 // Start listening for a typematic mouse click.
152 // See the trigger method for other parameters.
153 // returns:
154 // a connection handle
155 var handles = [
156 on(node, "mousedown", lang.hitch(this, function(evt){
157 evt.preventDefault();
158 typematic.trigger(evt, _this, node, callback, node, subsequentDelay, initialDelay, minDelay);
159 })),
160 on(node, "mouseup", lang.hitch(this, function(evt){
161 if(this._obj){
162 evt.preventDefault();
163 }
164 typematic.stop();
165 })),
166 on(node, "mouseout", lang.hitch(this, function(evt){
167 if(this._obj){
168 evt.preventDefault();
169 }
170 typematic.stop();
171 })),
172 on(node, "dblclick", lang.hitch(this, function(evt){
173 evt.preventDefault();
174 if(has("ie") < 9){
175 typematic.trigger(evt, _this, node, callback, node, subsequentDelay, initialDelay, minDelay);
176 setTimeout(lang.hitch(this, typematic.stop), 50);
177 }
178 }))
179 ];
180 return { remove: function(){
181 array.forEach(handles, function(h){
182 h.remove();
183 });
184 } };
185 },
186
187 addListener: function(/*Node*/ mouseNode, /*Node*/ keyNode, /*Object*/ keyObject, /*Object*/ _this, /*Function*/ callback, /*Number*/ subsequentDelay, /*Number*/ initialDelay, /*Number?*/ minDelay){
188 // summary:
189 // Start listening for a specific typematic key and mouseclick.
190 // This is a thin wrapper to addKeyListener and addMouseListener.
191 // See the addMouseListener and addKeyListener methods for other parameters.
192 // mouseNode:
193 // the DOM node object to listen on for mouse events.
194 // keyNode:
195 // the DOM node object to listen on for key events.
196 // returns:
197 // a connection handle
198 var handles = [
199 this.addKeyListener(keyNode, keyObject, _this, callback, subsequentDelay, initialDelay, minDelay),
200 this.addMouseListener(mouseNode, _this, callback, subsequentDelay, initialDelay, minDelay)
201 ];
202 return { remove: function(){
203 array.forEach(handles, function(h){
204 h.remove();
205 });
206 } };
207 }
208 });
209
210 return typematic;
211 });