11import "regenerator-runtime/runtime" ; // needed for ``await`` support
22import $ from "jquery" ;
33import Base from "../../core/base" ;
4+ import dom from "../../core/dom" ;
45import logging from "../../core/logging" ;
56import Parser from "../../core/parser" ;
67import events from "../../core/events" ;
78import registry from "../../core/registry" ;
89import utils from "../../core/utils" ;
910
11+ // Initialize close-panel functionality.
12+ import "../close-panel/close-panel" ;
13+
1014const log = logging . getLogger ( "pat-tooltip" ) ;
1115
1216export const parser = new Parser ( "tooltip" ) ;
@@ -235,36 +239,14 @@ export default Base.extend({
235239 return tippy_options ;
236240 } ,
237241
238- _initializeContent ( ) {
239- // Initialize all .close-panel elements
240- const close_els = this . tippy . popper . querySelectorAll ( ".close-panel" ) ;
241- const close_buttons = this . tippy . popper . querySelectorAll (
242- ".pat-tooltip--close-button"
243- ) ;
244- for ( let close_el of close_els ) {
245- events . add_event_listener (
246- close_el ,
247- "click" ,
248- "pat-tooltip--close-tooltip" ,
249- async ( ) => {
250- await utils . timeout ( 1 ) ; // wait a tick for event being processed by other handlers.
251- for ( let close_button of close_buttons ) {
252- // Also remove the close button
253- close_button . parentNode . removeChild ( close_button ) ;
254- }
255- this . tippy . hide ( ) ;
256- }
257- ) ;
258- }
242+ _initialize_content ( ) {
259243 // Initialize any other patterns.
260244 registry . scan ( this . tippy . popper ) ;
261245 } ,
262246
263247 async _onMount ( ) {
264248 if ( this . options . source === "ajax" ) {
265- await this . get_content ( ) ; // + _initializeContent
266- } else {
267- this . _initializeContent ( ) ;
249+ await this . get_content ( ) ; // + _initialize_content
268250 }
269251
270252 // Notify parent patterns about injected content.
@@ -275,26 +257,23 @@ export default Base.extend({
275257 this . el ,
276258 this . tippy . popper ,
277259 ] ) ;
278- } ,
279-
280- _onShow ( ) {
281- if ( this . options . closing !== "auto" && this . options . trigger === "hover" ) {
282- // no auto-close when hovering when closing mode is "sticky" or "close-button".
283- this . tippy . setProps ( { trigger : "click" } ) ;
284- }
285260
286261 if ( this . options . closing === "close-button" ) {
262+ // First, remove previously automatically added close buttons.
263+ // Otherwise we would end up adding another close button with every
264+ // click on it.
265+ for ( const close_button of this . tippy . popper . querySelectorAll (
266+ ".pat-tooltip--close-button"
267+ ) ) {
268+ close_button . parentNode . removeChild ( close_button ) ;
269+ }
270+
287271 const close_button = document . createElement ( "button" ) ;
288272 close_button . setAttribute ( "class" , "close-panel pat-tooltip--close-button" ) ;
289273 const content = this . tippy . popper . querySelector ( ".tippy-content" ) ;
290274 content . parentNode . insertBefore ( close_button , content ) ;
291275 }
292276
293- if ( this . options . markInactive ) {
294- this . el . classList . remove ( this . inactive_class ) ;
295- this . el . classList . add ( this . active_class ) ;
296- }
297-
298277 if ( this . options . class ) {
299278 for ( let class_ of this . options . class . split ( " " ) ) {
300279 this . tippy . popper . classList . add ( class_ ) ;
@@ -303,6 +282,24 @@ export default Base.extend({
303282
304283 // Add a generic non-tippy related class to identify the tooltip container
305284 this . tippy . popper . classList . add ( "tooltip-container" ) ;
285+
286+ // Store reference to method for closing panels on the tooltip element instance.
287+ this . tippy . popper . classList . add ( "has-close-panel" ) ;
288+ dom . set_data ( this . tippy . popper , "close_panel" , this . hide . bind ( this ) ) ;
289+
290+ this . _initialize_content ( ) ;
291+ } ,
292+
293+ _onShow ( ) {
294+ if ( this . options . closing !== "auto" && this . options . trigger === "hover" ) {
295+ // no auto-close when hovering when closing mode is "sticky" or "close-button".
296+ this . tippy . setProps ( { trigger : "click" } ) ;
297+ }
298+
299+ if ( this . options . markInactive ) {
300+ this . el . classList . remove ( this . inactive_class ) ;
301+ this . el . classList . add ( this . active_class ) ;
302+ }
306303 } ,
307304
308305 _onHide ( ) {
@@ -321,7 +318,7 @@ export default Base.extend({
321318 }
322319 } ,
323320
324- async get_content ( url = this . options . url ) {
321+ async _get_content ( url = this . options . url ) {
325322 let selector ;
326323 ( { url, selector } = this . get_url_parts ( url || this . el . getAttribute ( "href" ) ) ) ;
327324 let content ;
@@ -349,10 +346,15 @@ export default Base.extend({
349346 this . tippy . setContent ( content ) ;
350347 await utils . timeout ( 1 ) ; // Wait a tick before forceUpdate. Might fail due to unset popperInstance.
351348 this . tippy . popperInstance . forceUpdate ( ) ; // re-position tippy after content is known.
352- this . _initializeContent ( ) ;
353349 }
354350 } ,
355351
352+ async get_content ( url = this . options . url ) {
353+ // API method: _get_content + _initialize_content
354+ await this . _get_content ( url ) ;
355+ this . _initialize_content ( ) ;
356+ } ,
357+
356358 get_url_parts ( href ) {
357359 // Return the URL and a CSS ID selector.
358360 let url , selector , query ;
0 commit comments