Debian Codegraph @

dijit/Tooltip.js

raw symbols SCIP
1 define([
2 "dojo/_base/array", // array.forEach array.indexOf array.map
3 "dojo/_base/declare", // declare
4 "dojo/_base/fx", // fx.fadeIn fx.fadeOut
5 "dojo/dom", // dom.byId
6 "dojo/dom-class", // domClass.add
7 "dojo/dom-geometry", // domGeometry.position
8 "dojo/dom-style", // domStyle.set, domStyle.get
9 "dojo/_base/lang", // lang.hitch lang.isArrayLike
10 "dojo/mouse",
11 "dojo/on",
12 "dojo/sniff", // has("ie"), has("trident")
13 "./_base/manager", // manager.defaultDuration
14 "./place",
15 "./_Widget",
16 "./_TemplatedMixin",
17 "./BackgroundIframe",
18 "dojo/text!./templates/Tooltip.html",
19 "./main" // sets dijit.showTooltip etc. for back-compat
20 ], function(array, declare, fx, dom, domClass, domGeometry, domStyle, lang, mouse, on, has,
21 manager, place, _Widget, _TemplatedMixin, BackgroundIframe, template, dijit){
22
23 // module:
24 // dijit/Tooltip
25
26
27 // TODO: Tooltip should really share more positioning code with TooltipDialog, like:
28 // - the orient() method
29 // - the connector positioning code in show()
30 // - the dijitTooltip[Dialog] class
31 //
32 // The problem is that Tooltip's implementation supplies it's own <iframe> and interacts directly
33 // with dijit/place, rather than going through dijit/popup like TooltipDialog and other popups (ex: Menu).
34
35 var MasterTooltip = declare("dijit._MasterTooltip", [_Widget, _TemplatedMixin], {
36 // summary:
37 // Internal widget that holds the actual tooltip markup,
38 // which occurs once per page.
39 // Called by Tooltip widgets which are just containers to hold
40 // the markup
41 // tags:
42 // protected
43
44 // duration: Integer
45 // Milliseconds to fade in/fade out
46 duration: manager.defaultDuration,
47
48 templateString: template,
49
50 postCreate: function(){
51 this.ownerDocumentBody.appendChild(this.domNode);
52
53 this.bgIframe = new BackgroundIframe(this.domNode);
54
55 // Setup fade-in and fade-out functions.
56 this.fadeIn = fx.fadeIn({ node: this.domNode, duration: this.duration, onEnd: lang.hitch(this, "_onShow") });
57 this.fadeOut = fx.fadeOut({ node: this.domNode, duration: this.duration, onEnd: lang.hitch(this, "_onHide") });
58 },
59
60 show: function(innerHTML, aroundNode, position, rtl, textDir, onMouseEnter, onMouseLeave){
61 // summary:
62 // Display tooltip w/specified contents to right of specified node
63 // (To left if there's no space on the right, or if rtl == true)
64 // innerHTML: String
65 // Contents of the tooltip
66 // aroundNode: DomNode|dijit/place.__Rectangle
67 // Specifies that tooltip should be next to this node / area
68 // position: String[]?
69 // List of positions to try to position tooltip (ex: ["right", "above"])
70 // rtl: Boolean?
71 // Corresponds to `WidgetBase.dir` attribute, where false means "ltr" and true
72 // means "rtl"; specifies GUI direction, not text direction.
73 // textDir: String?
74 // Corresponds to `WidgetBase.textdir` attribute; specifies direction of text.
75 // onMouseEnter: Function?
76 // Callback function for mouse enter on tooltip
77 // onMouseLeave: Function?
78 // Callback function for mouse leave on tooltip
79
80 if(this.aroundNode && this.aroundNode === aroundNode && this.containerNode.innerHTML == innerHTML){
81 return;
82 }
83
84 if(this.fadeOut.status() == "playing"){
85 // previous tooltip is being hidden; wait until the hide completes then show new one
86 this._onDeck=arguments;
87 return;
88 }
89 this.containerNode.innerHTML=innerHTML;
90
91 if(textDir){
92 this.set("textDir", textDir);
93 }
94
95 this.containerNode.align = rtl? "right" : "left"; //fix the text alignment
96
97 var pos = place.around(this.domNode, aroundNode,
98 position && position.length ? position : Tooltip.defaultPosition, !rtl, lang.hitch(this, "orient"));
99
100 // Position the tooltip connector for middle alignment.
101 // This could not have been done in orient() since the tooltip wasn't positioned at that time.
102 var aroundNodeCoords = pos.aroundNodePos;
103 if(pos.corner.charAt(0) == 'M' && pos.aroundCorner.charAt(0) == 'M'){
104 this.connectorNode.style.top = aroundNodeCoords.y + ((aroundNodeCoords.h - this.connectorNode.offsetHeight) >> 1) - pos.y + "px";
105 this.connectorNode.style.left = "";
106 }else if(pos.corner.charAt(1) == 'M' && pos.aroundCorner.charAt(1) == 'M'){
107 this.connectorNode.style.left = aroundNodeCoords.x + ((aroundNodeCoords.w - this.connectorNode.offsetWidth) >> 1) - pos.x + "px";
108 }else{
109 // Not *-centered, but just above/below/after/before
110 this.connectorNode.style.left = "";
111 this.connectorNode.style.top = "";
112 }
113
114 // show it
115 domStyle.set(this.domNode, "opacity", 0);
116 this.fadeIn.play();
117 this.isShowingNow = true;
118 this.aroundNode = aroundNode;
119
120 this.onMouseEnter = onMouseEnter || noop;
121 this.onMouseLeave = onMouseLeave || noop;
122 },
123
124 orient: function(/*DomNode*/ node, /*String*/ aroundCorner, /*String*/ tooltipCorner, /*Object*/ spaceAvailable, /*Object*/ aroundNodeCoords){
125 // summary:
126 // Private function to set CSS for tooltip node based on which position it's in.
127 // This is called by the dijit popup code. It will also reduce the tooltip's
128 // width to whatever width is available
129 // tags:
130 // protected
131
132 this.connectorNode.style.top = ""; //reset to default
133
134 var heightAvailable = spaceAvailable.h,
135 widthAvailable = spaceAvailable.w;
136
137 node.className = "dijitTooltip " +
138 {
139 "MR-ML": "dijitTooltipRight",
140 "ML-MR": "dijitTooltipLeft",
141 "TM-BM": "dijitTooltipAbove",
142 "BM-TM": "dijitTooltipBelow",
143 "BL-TL": "dijitTooltipBelow dijitTooltipABLeft",
144 "TL-BL": "dijitTooltipAbove dijitTooltipABLeft",
145 "BR-TR": "dijitTooltipBelow dijitTooltipABRight",
146 "TR-BR": "dijitTooltipAbove dijitTooltipABRight",
147 "BR-BL": "dijitTooltipRight",
148 "BL-BR": "dijitTooltipLeft"
149 }[aroundCorner + "-" + tooltipCorner];
150
151 // reset width; it may have been set by orient() on a previous tooltip show()
152 this.domNode.style.width = "auto";
153
154 // Reduce tooltip's width to the amount of width available, so that it doesn't overflow screen.
155 // Note that sometimes widthAvailable is negative, but we guard against setting style.width to a
156 // negative number since that causes an exception on IE.
157 var size = domGeometry.position(this.domNode);
158 if(has("ie") || has("trident")){
159 // workaround strange IE bug where setting width to offsetWidth causes words to wrap
160 size.w += 2;
161 }
162
163 var width = Math.min((Math.max(widthAvailable,1)), size.w);
164
165 domGeometry.setMarginBox(this.domNode, {w: width});
166
167 // Reposition the tooltip connector.
168 if(tooltipCorner.charAt(0) == 'B' && aroundCorner.charAt(0) == 'B'){
169 var bb = domGeometry.position(node);
170 var tooltipConnectorHeight = this.connectorNode.offsetHeight;
171 if(bb.h > heightAvailable){
172 // The tooltip starts at the top of the page and will extend past the aroundNode
173 var aroundNodePlacement = heightAvailable - ((aroundNodeCoords.h + tooltipConnectorHeight) >> 1);
174 this.connectorNode.style.top = aroundNodePlacement + "px";
175 this.connectorNode.style.bottom = "";
176 }else{
177 // Align center of connector with center of aroundNode, except don't let bottom
178 // of connector extend below bottom of tooltip content, or top of connector
179 // extend past top of tooltip content
180 this.connectorNode.style.bottom = Math.min(
181 Math.max(aroundNodeCoords.h/2 - tooltipConnectorHeight/2, 0),
182 bb.h - tooltipConnectorHeight) + "px";
183 this.connectorNode.style.top = "";
184 }
185 }else{
186 // reset the tooltip back to the defaults
187 this.connectorNode.style.top = "";
188 this.connectorNode.style.bottom = "";
189 }
190
191 return Math.max(0, size.w - widthAvailable);
192 },
193
194 _onShow: function(){
195 // summary:
196 // Called at end of fade-in operation
197 // tags:
198 // protected
199 if(has("ie")){
200 // the arrow won't show up on a node w/an opacity filter
201 this.domNode.style.filter="";
202 }
203 },
204
205 hide: function(aroundNode){
206 // summary:
207 // Hide the tooltip
208
209 if(this._onDeck && this._onDeck[1] == aroundNode){
210 // this hide request is for a show() that hasn't even started yet;
211 // just cancel the pending show()
212 this._onDeck=null;
213 }else if(this.aroundNode === aroundNode){
214 // this hide request is for the currently displayed tooltip
215 this.fadeIn.stop();
216 this.isShowingNow = false;
217 this.aroundNode = null;
218 this.fadeOut.play();
219 }else{
220 // just ignore the call, it's for a tooltip that has already been erased
221 }
222
223 this.onMouseEnter = this.onMouseLeave = noop;
224 },
225
226 _onHide: function(){
227 // summary:
228 // Called at end of fade-out operation
229 // tags:
230 // protected
231
232 this.domNode.style.cssText=""; // to position offscreen again
233 this.containerNode.innerHTML="";
234 if(this._onDeck){
235 // a show request has been queued up; do it now
236 this.show.apply(this, this._onDeck);
237 this._onDeck=null;
238 }
239 }
240 });
241
242 if(has("dojo-bidi")){
243 MasterTooltip.extend({
244 _setAutoTextDir: function(/*Object*/node){
245 // summary:
246 // Resolve "auto" text direction for children nodes
247 // tags:
248 // private
249
250 this.applyTextDir(node);
251 array.forEach(node.children, function(child){ this._setAutoTextDir(child); }, this);
252 },
253
254 _setTextDirAttr: function(/*String*/ textDir){
255 // summary:
256 // Setter for textDir.
257 // description:
258 // Users shouldn't call this function; they should be calling
259 // set('textDir', value)
260 // tags:
261 // private
262
263 this._set("textDir", textDir);
264
265 if (textDir == "auto"){
266 this._setAutoTextDir(this.containerNode);
267 }else{
268 this.containerNode.dir = this.textDir;
269 }
270 }
271 });
272 }
273
274 dijit.showTooltip = function(innerHTML, aroundNode, position, rtl, textDir, onMouseEnter, onMouseLeave){
275 // summary:
276 // Static method to display tooltip w/specified contents in specified position.
277 // See description of dijit/Tooltip.defaultPosition for details on position parameter.
278 // If position is not specified then dijit/Tooltip.defaultPosition is used.
279 // innerHTML: String
280 // Contents of the tooltip
281 // aroundNode: place.__Rectangle
282 // Specifies that tooltip should be next to this node / area
283 // position: String[]?
284 // List of positions to try to position tooltip (ex: ["right", "above"])
285 // rtl: Boolean?
286 // Corresponds to `WidgetBase.dir` attribute, where false means "ltr" and true
287 // means "rtl"; specifies GUI direction, not text direction.
288 // textDir: String?
289 // Corresponds to `WidgetBase.textdir` attribute; specifies direction of text.
290 // onMouseEnter: Function?
291 // Callback function for mouse over on tooltip
292 // onMouseLeave: Function?
293 // Callback function for mouse leave on tooltip
294
295 // After/before don't work, but for back-compat convert them to the working after-centered, before-centered.
296 // Possibly remove this in 2.0. Alternately, get before/after to work.
297 if(position){
298 position = array.map(position, function(val){
299 return {after: "after-centered", before: "before-centered"}[val] || val;
300 });
301 }
302
303 if(!Tooltip._masterTT){ dijit._masterTT = Tooltip._masterTT = new MasterTooltip(); }
304 return Tooltip._masterTT.show(innerHTML, aroundNode, position, rtl, textDir, onMouseEnter, onMouseLeave);
305 };
306
307 dijit.hideTooltip = function(aroundNode){
308 // summary:
309 // Static method to hide the tooltip displayed via showTooltip()
310 return Tooltip._masterTT && Tooltip._masterTT.hide(aroundNode);
311 };
312
313 // Possible states for a tooltip, see Tooltip.state property for definitions
314 var DORMANT = "DORMANT",
315 SHOW_TIMER = "SHOW TIMER",
316 SHOWING = "SHOWING",
317 HIDE_TIMER = "HIDE TIMER";
318
319 function noop(){}
320
321 var Tooltip = declare("dijit.Tooltip", _Widget, {
322 // summary:
323 // Pops up a tooltip (a help message) when you hover over a node.
324 // Also provides static show() and hide() methods that can be used without instantiating a dijit/Tooltip.
325
326 // label: String
327 // HTML to display in the tooltip.
328 // Specified as innerHTML when creating the widget from markup.
329 label: "",
330
331 // showDelay: Integer
332 // Number of milliseconds to wait after hovering over/focusing on the object, before
333 // the tooltip is displayed.
334 showDelay: 400,
335
336 // hideDelay: Integer
337 // Number of milliseconds to wait after unhovering the object, before
338 // the tooltip is hidden. Note that blurring an object hides the tooltip immediately.
339 hideDelay: 400,
340
341 // connectId: String|String[]|DomNode|DomNode[]
342 // Id of domNode(s) to attach the tooltip to.
343 // When user hovers over specified dom node(s), the tooltip will appear.
344 connectId: [],
345
346 // position: String[]
347 // See description of `dijit/Tooltip.defaultPosition` for details on position parameter.
348 position: [],
349
350 // selector: String?
351 // CSS expression to apply this Tooltip to descendants of connectIds, rather than to
352 // the nodes specified by connectIds themselves. Useful for applying a Tooltip to
353 // a range of rows in a table, tree, etc. Use in conjunction with getContent() parameter.
354 // Ex: connectId: myTable, selector: "tr", getContent: function(node){ return ...; }
355 //
356 // The application must require() an appropriate level of dojo/query to handle the selector.
357 selector: "",
358
359 // TODO: in 2.0 remove support for multiple connectIds. selector gives the same effect.
360 // So, change connectId to a "", remove addTarget()/removeTarget(), etc.
361
362 _setConnectIdAttr: function(/*String|String[]|DomNode|DomNode[]*/ newId){
363 // summary:
364 // Connect to specified node(s)
365
366 // Remove connections to old nodes (if there are any)
367 array.forEach(this._connections || [], function(nested){
368 array.forEach(nested, function(handle){ handle.remove(); });
369 }, this);
370
371 // Make array of id's to connect to, excluding entries for nodes that don't exist yet, see startup()
372 this._connectIds = array.filter(lang.isArrayLike(newId) ? newId : (newId ? [newId] : []),
373 function(id){ return dom.byId(id, this.ownerDocument); }, this);
374
375 // Make connections
376 this._connections = array.map(this._connectIds, function(id){
377 var node = dom.byId(id, this.ownerDocument),
378 selector = this.selector,
379 delegatedEvent = selector ?
380 function(eventType){ return on.selector(selector, eventType); } :
381 function(eventType){ return eventType; },
382 self = this;
383 return [
384 on(node, delegatedEvent(mouse.enter), function(){
385 self._onHover(this);
386 }),
387 on(node, delegatedEvent("focusin"), function(){
388 self._onHover(this);
389 }),
390 on(node, delegatedEvent(mouse.leave), lang.hitch(self, "_onUnHover")),
391 on(node, delegatedEvent("focusout"), lang.hitch(self, "set", "state", DORMANT))
392 ];
393 }, this);
394
395 this._set("connectId", newId);
396 },
397
398 addTarget: function(/*DomNode|String*/ node){
399 // summary:
400 // Attach tooltip to specified node if it's not already connected
401
402 // TODO: remove in 2.0 and just use set("connectId", ...) interface
403
404 var id = node.id || node;
405 if(array.indexOf(this._connectIds, id) == -1){
406 this.set("connectId", this._connectIds.concat(id));
407 }
408 },
409
410 removeTarget: function(/*DomNode|String*/ node){
411 // summary:
412 // Detach tooltip from specified node
413
414 // TODO: remove in 2.0 and just use set("connectId", ...) interface
415
416 var id = node.id || node, // map from DOMNode back to plain id string
417 idx = array.indexOf(this._connectIds, id);
418 if(idx >= 0){
419 // remove id (modifies original this._connectIds but that's OK in this case)
420 this._connectIds.splice(idx, 1);
421 this.set("connectId", this._connectIds);
422 }
423 },
424
425 buildRendering: function(){
426 this.inherited(arguments);
427 domClass.add(this.domNode,"dijitTooltipData");
428 },
429
430 startup: function(){
431 this.inherited(arguments);
432
433 // If this tooltip was created in a template, or for some other reason the specified connectId[s]
434 // didn't exist during the widget's initialization, then connect now.
435 var ids = this.connectId;
436 array.forEach(lang.isArrayLike(ids) ? ids : [ids], this.addTarget, this);
437 },
438
439 getContent: function(/*DomNode*/ node){
440 // summary:
441 // User overridable function that return the text to display in the tooltip.
442 // tags:
443 // extension
444 return this.label || this.domNode.innerHTML;
445 },
446
447 // state: [private readonly] String
448 // One of:
449 //
450 // - DORMANT: tooltip not SHOWING
451 // - SHOW TIMER: tooltip not SHOWING but timer set to show it
452 // - SHOWING: tooltip displayed
453 // - HIDE TIMER: tooltip displayed, but timer set to hide it
454 state: DORMANT,
455 _setStateAttr: function(val){
456 if(this.state == val ||
457 (val == SHOW_TIMER && this.state == SHOWING) ||
458 (val == HIDE_TIMER && this.state == DORMANT)){
459 return;
460 }
461
462 if(this._hideTimer){
463 this._hideTimer.remove();
464 delete this._hideTimer;
465 }
466 if(this._showTimer){
467 this._showTimer.remove();
468 delete this._showTimer;
469 }
470
471 switch(val){
472 case DORMANT:
473 if(this._connectNode){
474 Tooltip.hide(this._connectNode);
475 delete this._connectNode;
476 this.onHide();
477 }
478 break;
479 case SHOW_TIMER: // set timer to show tooltip
480 // should only get here from a DORMANT state, i.e. tooltip can't be already SHOWING
481 if(this.state != SHOWING){
482 this._showTimer = this.defer(function(){ this.set("state", SHOWING); }, this.showDelay);
483 }
484 break;
485 case SHOWING: // show tooltip and clear timers
486 var content = this.getContent(this._connectNode);
487 if(!content){
488 this.set("state", DORMANT);
489 return;
490 }
491
492 // Show tooltip and setup callbacks for mouseenter/mouseleave of tooltip itself
493 Tooltip.show(content, this._connectNode, this.position, !this.isLeftToRight(), this.textDir,
494 lang.hitch(this, "set", "state", SHOWING), lang.hitch(this, "set", "state", HIDE_TIMER));
495
496 this.onShow(this._connectNode, this.position);
497 break;
498 case HIDE_TIMER: // set timer set to hide tooltip
499 this._hideTimer = this.defer(function(){ this.set("state", DORMANT); }, this.hideDelay);
500 break;
501 }
502
503 this._set("state", val);
504 },
505
506 _onHover: function(/*DomNode*/ target){
507 // summary:
508 // Despite the name of this method, it actually handles both hover and focus
509 // events on the target node, setting a timer to show the tooltip.
510 // tags:
511 // private
512
513 if(this._connectNode && target != this._connectNode){
514 // Tooltip is displaying for another node
515 this.set("state", DORMANT);
516 }
517 this._connectNode = target; // _connectNode means "tooltip currently displayed for this node"
518
519 this.set("state", SHOW_TIMER); // no-op if show-timer already set, or if already showing
520 },
521
522 _onUnHover: function(/*DomNode*/ target){
523 // summary:
524 // Handles mouseleave event on the target node, hiding the tooltip.
525 // tags:
526 // private
527
528 this.set("state", HIDE_TIMER); // no-op if already dormant, or if hide-timer already set
529 },
530
531 // open() and close() aren't used anymore, except from the _BidiSupport/misc/Tooltip test.
532 // Should probably remove for 2.0, but leaving for now.
533 open: function(/*DomNode*/ target){
534 // summary:
535 // Display the tooltip; usually not called directly.
536 // tags:
537 // private
538
539 this.set("state", DORMANT);
540 this._connectNode = target; // _connectNode means "tooltip currently displayed for this node"
541 this.set("state", SHOWING);
542 },
543
544 close: function(){
545 // summary:
546 // Hide the tooltip or cancel timer for show of tooltip
547 // tags:
548 // private
549
550 this.set("state", DORMANT);
551 },
552
553 onShow: function(/*===== target, position =====*/){
554 // summary:
555 // Called when the tooltip is shown
556 // tags:
557 // callback
558 },
559
560 onHide: function(){
561 // summary:
562 // Called when the tooltip is hidden
563 // tags:
564 // callback
565 },
566
567 destroy: function(){
568 this.set("state", DORMANT);
569
570 // Remove connections manually since they aren't registered to be removed by _WidgetBase
571 array.forEach(this._connections || [], function(nested){
572 array.forEach(nested, function(handle){ handle.remove(); });
573 }, this);
574
575 this.inherited(arguments);
576 }
577 });
578
579 Tooltip._MasterTooltip = MasterTooltip; // for monkey patching
580 Tooltip.show = dijit.showTooltip; // export function through module return value
581 Tooltip.hide = dijit.hideTooltip; // export function through module return value
582
583 Tooltip.defaultPosition = ["after-centered", "before-centered"];
584
585 /*=====
586 lang.mixin(Tooltip, {
587 // defaultPosition: String[]
588 // This variable controls the position of tooltips, if the position is not specified to
589 // the Tooltip widget or *TextBox widget itself. It's an array of strings with the values
590 // possible for `dijit/place.around()`. The recommended values are:
591 //
592 // - before-centered: centers tooltip to the left of the anchor node/widget, or to the right
593 // in the case of RTL scripts like Hebrew and Arabic
594 // - after-centered: centers tooltip to the right of the anchor node/widget, or to the left
595 // in the case of RTL scripts like Hebrew and Arabic
596 // - above-centered: tooltip is centered above anchor node
597 // - below-centered: tooltip is centered above anchor node
598 //
599 // The list is positions is tried, in order, until a position is found where the tooltip fits
600 // within the viewport.
601 //
602 // Be careful setting this parameter. A value of "above-centered" may work fine until the user scrolls
603 // the screen so that there's no room above the target node. Nodes with drop downs, like
604 // DropDownButton or FilteringSelect, are especially problematic, in that you need to be sure
605 // that the drop down and tooltip don't overlap, even when the viewport is scrolled so that there
606 // is only room below (or above) the target node, but not both.
607 });
608 =====*/
609 return Tooltip;
610 });