Contextual addition of JavaScript libraries
| Project: | Drupal |
| Version: | 7.x-dev |
| Component: | javascript |
| Category: | task |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
| Issue tags: | drupal_add_library, JavaScript, jQuery, jQuery UI, Libraries |
Jump to:
#315100: Allow to add JS/CSS libraries (sets of files, settings, and dependent libraries) allowed the addition of multiple JavaScript/CSS libraries at a time. Taking action during the addition of these libraries was not put in that issue though. Some examples of what we could do when the libraries are being added are give the settings so that the JavaScript settings take effect correctly. This would end up looking something like this:
<?php
echo '<div id="mydialog">Hello, I'm a dialog box!</div>';
drupal_add_library('system', 'ui.dialog', '#mydialog');
?>As you can see, while we're adding "ui.dialog" to the page, we also pass it some settings to properly attach the behaviors to the specified element. But, what this is missing is context. We need a way to specify how we want those setting to behave so you're not restricted to always passing the element that you'd like to take action on. This concept was known as the "add callbacks" in #315100: Allow to add JS/CSS libraries (sets of files, settings, and dependent libraries).

#1
I think we should look at this in a larger scope. Passing data to a callback function that turns them into JavaScript settings and loads a support script is not sufficient in many cases.
The library as well as the settings and support script also depend on a interaction design pattern:
1) Theme functions: The markup to which a behavior/effect is applied must match the markup the library expects, resp. requires.
2) Initialization: Just turning some content into a dialog/tab/whatever does not (always) show the dialog/tab/whatever; most often, a separate trigger is required.
#454382: Provide jquery_ui_elements() for easier usage of plugins in other modules provides some more clues. Small and easily overlooked, but very important amendment there regarding 2) above:
#2
I think the focus should be on what can be done with a callback. To try and find a solution for every single scenario it's not possible.
What can be done is to initialize each interaction or widget (in ui case). We can't expect to put every option/event "draggable" can handle for example in drupal_add_library.
We also can make theme functions to render the widgets, add the library and initialize the widget. Everything else is developers job to do.
Off issue: that dialog doesn't need anything because autoOpen defaults to true in ui.dialog.
#3
Tagging.
#4
#87994: Filter tips modal popup / jQuery UI modal popup helper function contains something like this, but targets jQuery UI elements as they all hold almost the same API structure.