hovercard.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Show hovercards with a delay after the mouse moves over an
  16. * element of a specified type and with a specific attribute.
  17. *
  18. * @see ../demos/hovercard.html
  19. */
  20. goog.provide('goog.ui.HoverCard');
  21. goog.provide('goog.ui.HoverCard.EventType');
  22. goog.provide('goog.ui.HoverCard.TriggerEvent');
  23. goog.require('goog.array');
  24. goog.require('goog.dom');
  25. goog.require('goog.events');
  26. goog.require('goog.events.Event');
  27. goog.require('goog.events.EventType');
  28. goog.require('goog.ui.AdvancedTooltip');
  29. goog.require('goog.ui.PopupBase');
  30. goog.require('goog.ui.Tooltip');
  31. /**
  32. * Create a hover card object. Hover cards extend tooltips in that they don't
  33. * have to be manually attached to each element that can cause them to display.
  34. * Instead, you can create a function that gets called when the mouse goes over
  35. * any element on your page, and returns whether or not the hovercard should be
  36. * shown for that element.
  37. *
  38. * Alternatively, you can define a map of tag names to the attribute name each
  39. * tag should have for that tag to trigger the hover card. See example below.
  40. *
  41. * Hovercards can also be triggered manually by calling
  42. * {@code triggerForElement}, shown without a delay by calling
  43. * {@code showForElement}, or triggered over other elements by calling
  44. * {@code attach}. For the latter two cases, the application is responsible
  45. * for calling {@code detach} when finished.
  46. *
  47. * HoverCard objects fire a TRIGGER event when the mouse moves over an element
  48. * that can trigger a hovercard, and BEFORE_SHOW when the hovercard is
  49. * about to be shown. Clients can respond to these events and can prevent the
  50. * hovercard from being triggered or shown.
  51. *
  52. * @param {Function|Object} isAnchor Function that returns true if a given
  53. * element should trigger the hovercard. Alternatively, it can be a map of
  54. * tag names to the attribute that the tag should have in order to trigger
  55. * the hovercard, e.g., {A: 'href'} for all links. Tag names must be all
  56. * upper case; attribute names are case insensitive.
  57. * @param {boolean=} opt_checkDescendants Use false for a performance gain if
  58. * you are sure that none of your triggering elements have child elements.
  59. * Default is true.
  60. * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper to use for
  61. * creating and rendering the hovercard element.
  62. * @param {Document=} opt_triggeringDocument Optional document to use in place
  63. * of the one included in the DomHelper for finding triggering elements.
  64. * Defaults to the document included in the DomHelper.
  65. * @constructor
  66. * @extends {goog.ui.AdvancedTooltip}
  67. */
  68. goog.ui.HoverCard = function(
  69. isAnchor, opt_checkDescendants, opt_domHelper, opt_triggeringDocument) {
  70. goog.ui.AdvancedTooltip.call(this, null, null, opt_domHelper);
  71. if (goog.isFunction(isAnchor)) {
  72. // Override default implementation of {@code isAnchor_}.
  73. this.isAnchor_ = isAnchor;
  74. } else {
  75. /**
  76. * Map of tag names to attribute names that will trigger a hovercard.
  77. * @type {Object}
  78. * @private
  79. */
  80. this.anchors_ = isAnchor;
  81. }
  82. /**
  83. * Whether anchors may have child elements. If true, then we need to check
  84. * the parent chain of any mouse over event to see if any of those elements
  85. * could be anchors. Default is true.
  86. * @type {boolean}
  87. * @private
  88. */
  89. this.checkDescendants_ = opt_checkDescendants != false;
  90. /**
  91. * Array of anchor elements that should be detached when we are no longer
  92. * associated with them.
  93. * @type {!Array<Element>}
  94. * @private
  95. */
  96. this.tempAttachedAnchors_ = [];
  97. /**
  98. * Document containing the triggering elements, to which we listen for
  99. * mouseover events.
  100. * @type {Document}
  101. * @private
  102. */
  103. this.document_ = opt_triggeringDocument ||
  104. (opt_domHelper ? opt_domHelper.getDocument() : goog.dom.getDocument());
  105. goog.events.listen(
  106. this.document_, goog.events.EventType.MOUSEOVER,
  107. this.handleTriggerMouseOver_, false, this);
  108. };
  109. goog.inherits(goog.ui.HoverCard, goog.ui.AdvancedTooltip);
  110. goog.tagUnsealableClass(goog.ui.HoverCard);
  111. /**
  112. * Enum for event type fired by HoverCard.
  113. * @enum {string}
  114. */
  115. goog.ui.HoverCard.EventType = {
  116. TRIGGER: 'trigger',
  117. CANCEL_TRIGGER: 'canceltrigger',
  118. BEFORE_SHOW: goog.ui.PopupBase.EventType.BEFORE_SHOW,
  119. SHOW: goog.ui.PopupBase.EventType.SHOW,
  120. BEFORE_HIDE: goog.ui.PopupBase.EventType.BEFORE_HIDE,
  121. HIDE: goog.ui.PopupBase.EventType.HIDE
  122. };
  123. /** @override */
  124. goog.ui.HoverCard.prototype.disposeInternal = function() {
  125. goog.ui.HoverCard.superClass_.disposeInternal.call(this);
  126. goog.events.unlisten(
  127. this.document_, goog.events.EventType.MOUSEOVER,
  128. this.handleTriggerMouseOver_, false, this);
  129. };
  130. /**
  131. * Anchor of hovercard currently being shown. This may be different from
  132. * {@code anchor} property if a second hovercard is triggered, when
  133. * {@code anchor} becomes the second hovercard while {@code currentAnchor_}
  134. * is still the old (but currently displayed) anchor.
  135. * @type {Element}
  136. * @private
  137. */
  138. goog.ui.HoverCard.prototype.currentAnchor_;
  139. /**
  140. * Maximum number of levels to search up the dom when checking descendants.
  141. * @type {number}
  142. * @private
  143. */
  144. goog.ui.HoverCard.prototype.maxSearchSteps_;
  145. /**
  146. * This function can be overridden by passing a function as the first parameter
  147. * to the constructor.
  148. * @param {Node} node Node to test.
  149. * @return {boolean} Whether or not hovercard should be shown.
  150. * @private
  151. */
  152. goog.ui.HoverCard.prototype.isAnchor_ = function(node) {
  153. return node.tagName in this.anchors_ &&
  154. !!node.getAttribute(this.anchors_[node.tagName]);
  155. };
  156. /**
  157. * If the user mouses over an element with the correct tag and attribute, then
  158. * trigger the hovercard for that element. If anchors could have children, then
  159. * we also need to check the parent chain of the given element.
  160. * @param {goog.events.Event} e Mouse over event.
  161. * @private
  162. */
  163. goog.ui.HoverCard.prototype.handleTriggerMouseOver_ = function(e) {
  164. var target = /** @type {Element} */ (e.target);
  165. // Target might be null when hovering over disabled input textboxes in IE.
  166. if (!target) {
  167. return;
  168. }
  169. if (this.isAnchor_(target)) {
  170. this.setPosition(null);
  171. this.triggerForElement(target);
  172. } else if (this.checkDescendants_) {
  173. var trigger = goog.dom.getAncestor(
  174. target, goog.bind(this.isAnchor_, this), false, this.maxSearchSteps_);
  175. if (trigger) {
  176. this.setPosition(null);
  177. this.triggerForElement(/** @type {!Element} */ (trigger));
  178. }
  179. }
  180. };
  181. /**
  182. * Triggers the hovercard to show after a delay.
  183. * @param {Element} anchorElement Element that is triggering the hovercard.
  184. * @param {goog.positioning.AbstractPosition=} opt_pos Position to display
  185. * hovercard.
  186. * @param {Object=} opt_data Data to pass to the onTrigger event.
  187. */
  188. goog.ui.HoverCard.prototype.triggerForElement = function(
  189. anchorElement, opt_pos, opt_data) {
  190. if (anchorElement == this.currentAnchor_) {
  191. // Element is already showing, just make sure it doesn't hide.
  192. this.clearHideTimer();
  193. return;
  194. }
  195. if (anchorElement == this.anchor) {
  196. // Hovercard is pending, no need to retrigger.
  197. return;
  198. }
  199. // If a previous hovercard was being triggered, cancel it.
  200. this.maybeCancelTrigger_();
  201. // Create a new event for this trigger
  202. var triggerEvent = new goog.ui.HoverCard.TriggerEvent(
  203. goog.ui.HoverCard.EventType.TRIGGER, this, anchorElement, opt_data);
  204. if (!this.getElements().contains(anchorElement)) {
  205. this.attach(anchorElement);
  206. this.tempAttachedAnchors_.push(anchorElement);
  207. }
  208. this.anchor = anchorElement;
  209. if (!this.onTrigger(triggerEvent)) {
  210. this.onCancelTrigger();
  211. return;
  212. }
  213. var pos = opt_pos || this.getPosition();
  214. this.startShowTimer(
  215. anchorElement,
  216. /** @type {goog.positioning.AbstractPosition} */ (pos));
  217. };
  218. /**
  219. * Sets the current anchor element at the time that the hovercard is shown.
  220. * @param {Element} anchor New current anchor element, or null if there is
  221. * no current anchor.
  222. * @private
  223. */
  224. goog.ui.HoverCard.prototype.setCurrentAnchor_ = function(anchor) {
  225. if (anchor != this.currentAnchor_) {
  226. this.detachTempAnchor_(this.currentAnchor_);
  227. }
  228. this.currentAnchor_ = anchor;
  229. };
  230. /**
  231. * If given anchor is in the list of temporarily attached anchors, then
  232. * detach and remove from the list.
  233. * @param {Element|undefined} anchor Anchor element that we may want to detach
  234. * from.
  235. * @private
  236. */
  237. goog.ui.HoverCard.prototype.detachTempAnchor_ = function(anchor) {
  238. if (anchor) {
  239. var pos = goog.array.indexOf(this.tempAttachedAnchors_, anchor);
  240. if (pos != -1) {
  241. this.detach(anchor);
  242. this.tempAttachedAnchors_.splice(pos, 1);
  243. }
  244. }
  245. };
  246. /**
  247. * Called when an element triggers the hovercard. This will return false
  248. * if an event handler sets preventDefault to true, which will prevent
  249. * the hovercard from being shown.
  250. * @param {!goog.ui.HoverCard.TriggerEvent} triggerEvent Event object to use
  251. * for trigger event.
  252. * @return {boolean} Whether hovercard should be shown or cancelled.
  253. * @protected
  254. */
  255. goog.ui.HoverCard.prototype.onTrigger = function(triggerEvent) {
  256. return this.dispatchEvent(triggerEvent);
  257. };
  258. /**
  259. * Abort pending hovercard showing, if any.
  260. */
  261. goog.ui.HoverCard.prototype.cancelTrigger = function() {
  262. this.clearShowTimer();
  263. this.onCancelTrigger();
  264. };
  265. /**
  266. * If hovercard is in the process of being triggered, then cancel it.
  267. * @private
  268. */
  269. goog.ui.HoverCard.prototype.maybeCancelTrigger_ = function() {
  270. if (this.getState() == goog.ui.Tooltip.State.WAITING_TO_SHOW ||
  271. this.getState() == goog.ui.Tooltip.State.UPDATING) {
  272. this.cancelTrigger();
  273. }
  274. };
  275. /**
  276. * This method gets called when we detect that a trigger event will not lead
  277. * to the hovercard being shown.
  278. * @protected
  279. */
  280. goog.ui.HoverCard.prototype.onCancelTrigger = function() {
  281. var event = new goog.ui.HoverCard.TriggerEvent(
  282. goog.ui.HoverCard.EventType.CANCEL_TRIGGER, this, this.anchor || null);
  283. this.dispatchEvent(event);
  284. this.detachTempAnchor_(this.anchor);
  285. delete this.anchor;
  286. };
  287. /**
  288. * Gets the DOM element that triggered the current hovercard. Note that in
  289. * the TRIGGER or CANCEL_TRIGGER events, the current hovercard's anchor may not
  290. * be the one that caused the event, so use the event's anchor property instead.
  291. * @return {Element} Object that caused the currently displayed hovercard (or
  292. * pending hovercard if none is displayed) to be triggered.
  293. */
  294. goog.ui.HoverCard.prototype.getAnchorElement = function() {
  295. // this.currentAnchor_ is only set if the hovercard is showing. If it isn't
  296. // showing yet, then use this.anchor as the pending anchor.
  297. return /** @type {Element} */ (this.currentAnchor_ || this.anchor);
  298. };
  299. /**
  300. * Make sure we detach from temp anchor when we are done displaying hovercard.
  301. * @protected
  302. * @override
  303. */
  304. goog.ui.HoverCard.prototype.onHide = function() {
  305. goog.ui.HoverCard.superClass_.onHide.call(this);
  306. this.setCurrentAnchor_(null);
  307. };
  308. /**
  309. * This mouse over event is only received if the anchor is already attached.
  310. * If it was attached manually, then it may need to be triggered.
  311. * @param {goog.events.BrowserEvent} event Mouse over event.
  312. * @override
  313. */
  314. goog.ui.HoverCard.prototype.handleMouseOver = function(event) {
  315. // If this is a child of a triggering element, find the triggering element.
  316. var trigger = this.getAnchorFromElement(
  317. /** @type {Element} */ (event.target));
  318. // If we moused over an element different from the one currently being
  319. // triggered (if any), then trigger this new element.
  320. if (trigger && trigger != this.anchor) {
  321. this.triggerForElement(trigger);
  322. return;
  323. }
  324. goog.ui.HoverCard.superClass_.handleMouseOver.call(this, event);
  325. };
  326. /**
  327. * If the mouse moves out of the trigger while we're being triggered, then
  328. * cancel it.
  329. * @param {goog.events.BrowserEvent} event Mouse out or blur event.
  330. * @override
  331. */
  332. goog.ui.HoverCard.prototype.handleMouseOutAndBlur = function(event) {
  333. // Get ready to see if a trigger should be cancelled.
  334. var anchor = this.anchor;
  335. var state = this.getState();
  336. goog.ui.HoverCard.superClass_.handleMouseOutAndBlur.call(this, event);
  337. if (state != this.getState() &&
  338. (state == goog.ui.Tooltip.State.WAITING_TO_SHOW ||
  339. state == goog.ui.Tooltip.State.UPDATING)) {
  340. // Tooltip's handleMouseOutAndBlur method sets anchor to null. Reset
  341. // so that the cancel trigger event will have the right data, and so that
  342. // it will be properly detached.
  343. this.anchor = anchor;
  344. this.onCancelTrigger(); // This will remove and detach the anchor.
  345. }
  346. };
  347. /**
  348. * Called by timer from mouse over handler. If this is called and the hovercard
  349. * is not shown for whatever reason, then send a cancel trigger event.
  350. * @param {Element} el Element to show tooltip for.
  351. * @param {goog.positioning.AbstractPosition=} opt_pos Position to display popup
  352. * at.
  353. * @override
  354. */
  355. goog.ui.HoverCard.prototype.maybeShow = function(el, opt_pos) {
  356. goog.ui.HoverCard.superClass_.maybeShow.call(this, el, opt_pos);
  357. if (!this.isVisible()) {
  358. this.cancelTrigger();
  359. } else {
  360. this.setCurrentAnchor_(el);
  361. }
  362. };
  363. /**
  364. * Sets the max number of levels to search up the dom if checking descendants.
  365. * @param {number} maxSearchSteps Maximum number of levels to search up the
  366. * dom if checking descendants.
  367. */
  368. goog.ui.HoverCard.prototype.setMaxSearchSteps = function(maxSearchSteps) {
  369. if (!maxSearchSteps) {
  370. this.checkDescendants_ = false;
  371. } else if (this.checkDescendants_) {
  372. this.maxSearchSteps_ = maxSearchSteps;
  373. }
  374. };
  375. /**
  376. * Create a trigger event for specified anchor and optional data.
  377. * @param {goog.ui.HoverCard.EventType} type Event type.
  378. * @param {goog.ui.HoverCard} target Hovercard that is triggering the event.
  379. * @param {Element} anchor Element that triggered event.
  380. * @param {Object=} opt_data Optional data to be available in the TRIGGER event.
  381. * @constructor
  382. * @extends {goog.events.Event}
  383. * @final
  384. */
  385. goog.ui.HoverCard.TriggerEvent = function(type, target, anchor, opt_data) {
  386. goog.events.Event.call(this, type, target);
  387. /**
  388. * Element that triggered the hovercard event.
  389. * @type {Element}
  390. */
  391. this.anchor = anchor;
  392. /**
  393. * Optional data to be passed to the listener.
  394. * @type {Object|undefined}
  395. */
  396. this.data = opt_data;
  397. };
  398. goog.inherits(goog.ui.HoverCard.TriggerEvent, goog.events.Event);