I was trying to get popovers working in a custom views template and nothing I did was working. I was trying to follow the examples on the bootstrap website using the data attributes, ie, data-toggle etc. This didn't seem to work, so I got it working using the .popover() function.

Is it possible to use this module without writing javascripti using just the data-* attributes api, or are we required to write javascript to use components like popovers and tooltips?

thanks,
Cliff

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

markhalliwell’s picture

Component: Miscellaneous » Code
Category: support » feature

From http://getbootstrap.com/2.3.2/javascript.html#tooltips

For performance reasons, the tooltip and popover data-apis are opt in, meaning you must initialize them yourself.

The equivalent for BS3: http://getbootstrap.com/javascript/#popovers-usage under the "Markup" section:

For performance reasons, the Tooltip and Popover data-apis are opt in. If you would like to use them just specify a selector option.

Tabs are activated automatically via the "data-api":
https://github.com/twbs/bootstrap/blob/master/js/tab.js#L127-L133

And you can see that popovers and tooltips are not:
https://github.com/twbs/bootstrap/blob/master/js/popover.js#L116
https://github.com/twbs/bootstrap/blob/master/js/tooltip.js#L385

I agree this is rather confusing. Perhaps we should throw a theme setting in, enabled by default, to auto load these plugins:
http://drupalcode.org/project/bootstrap.git/blob/refs/heads/7.x-3.x:/js/...

markhalliwell’s picture

Title: Data attributes without JS » Create theme setting for Popover/Tooltip plugins for "data-api"
valkum’s picture

Assigned: Unassigned » valkum

Will look tomorrow afternoon into it (GMT+2). If anyone got time before, np.

valkum’s picture

Status: Active » Needs review
FileSize
3.54 KB

What about this?
When you add elements using data-toggle via ajax you have to call .popover or .tooltip again.
Maybe we can came up with an .on() solution? Or is there no need to handle this in this base theme? Mark?

markhalliwell’s picture

Status: Needs review » Needs work
valkum’s picture

Status: Needs work » Needs review
FileSize
2.66 KB
3.65 KB

The problem with this is,
with popover('show') the popover doesn't close, it opens on every click.
with popover() the first click doesn't open the popover.
We could use one() instead of on(), But i'm not sure if this applies to new element as well.
Here is a patch with one()

markhalliwell’s picture

Status: Needs review » Needs work

Not exactly like tabs, popovers have their own methods and events: http://getbootstrap.com/javascript/#popovers, specifically $('#element').popover('toggle')

  1. +++ b/theme/settings.inc
    @@ -208,6 +208,26 @@ function _bootstrap_settings_form(&$form, $form_state) {
    +    '#title' => t('Javascript'),
    

    "JavaScript"

  2. +++ b/theme/settings.inc
    @@ -208,6 +208,26 @@ function _bootstrap_settings_form(&$form, $form_state) {
    +  $form['javascript']['bootstrap_javascript_popover'] = array(
    ...
    +    '#default_value' => theme_get_setting('bootstrap_javascript_popover'),
    ...
    +  $form['javascript']['bootstrap_javascript_tooltip'] = array(
    ...
    +    '#default_value' => theme_get_setting('bootstrap_javascript_tooltip'),
    

    bootstrap_popover and bootstrap_tooltip is fine.

Also, perhaps we should give the users which "event" they would like this triggered on (ie: click, mouseup, mousemove, etc).

valkum’s picture

I think, in base theme, we should stick with the default events that are bound to the element on .popover() .tooltip()
I think we should stay with .one on tooltip.
I'm not sure if there is need to use on with toggle on popovers.
The only thing we need to do is init. the popover for this element and show it.

valkum’s picture

Status: Needs work » Needs review
FileSize
3.65 KB
3.47 KB

Here is a patch with fixed #7.1 and #7.2
I'd like to stick with default events and not to bind 2 event listener to the same element.

Data-api tabs are different, as there is no listener defined in the plugin itself.
popover and tooltip bind event listeners on first call of .popover() or .popover(option) / .tooltip() or tooltip(option)

I think the $.one() solution is the best way.

markhalliwell’s picture

Status: Needs review » Needs work

The first form of this method is identical to .bind(), except that the handler is unbound after its first invocation. The second two forms, introduced in jQuery 1.7, are identical to .on() except that the handler is removed after the first time the event occurs at the delegated element, whether the selector matched anything or not.

One isn't the best way, it will only fire once (per click or hover) instead of continuously. It should be using .on.

valkum’s picture

i came up with this solution. I'm not sure if there is a better way.

This way the .popover(option) function is only called once on the specific element ([data-toggle="popover"])
and the clicks after this initial click are handled by popovers own event listener.
The same for tooltips.

// Init popovers on pageload when settings is set.
      if (settings.bootstrap.popover) {
        $(context).on('click.drupal.popover.data-api', '[data-toggle="popover"]', function (e) {
          if (!$(this).data('popover-initiated')) {
            $(this).data('popover-initiated', true);
            e.preventDefault();
            $(this).popover('show');
          }
        });
      }
      // Init tooltips on pageload when settings is set.
      if (settings.bootstrap.tooltip) {
        $(context).on('mouseenter.drupal.tooltip.data-api focus.drupal.tooltip.data-api', '[data-toggle="tooltip"]', function (e) {
          if (!$(this).data('tooltip-initiated')) {
            $(this).data('tooltip-initiated', true);
            e.preventDefault();
            $(this).tooltip('show');
          }
        });
valkum’s picture

Status: Needs work » Needs review
FileSize
1.28 KB
45.84 KB
markhalliwell’s picture

Status: Needs review » Needs work

The patch in #12 looks to contain several other patches, please reroll with just the changes for this issue (FWIW, I typically create separate branches from 7.x-3.x per issue so I don't x-patch issues).

valkum’s picture

sry, don't know from where this came. I have an branch per issue. Think messed somethin up with unstashed/uncommited changes.
here is the rerolled patch

valkum’s picture

Status: Needs work » Needs review
markhalliwell’s picture

Issue summary: View changes
Status: Needs review » Fixed

Sorry @valkum, the more I thought about this, this is more of what I had in mind:
Committed a5223b6 to 7.x-3.x:

Issue #2114663 by Mark Carver, valkum: Create theme setting for Popover/Tooltip plugins for "data-api".

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.