Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added behavior.load to allow for a single go-to place to register/load javascript libraries. #1192

Closed
wants to merge 1 commit into from

2 participants

@rcorral

With this I added a central place where javascript libraries can be registered along with their CSS and JS files. Which is something that other implementations don't account for.

An example of this would be doing this:
JHtmlBehavior::register('modal', array(
'script' => array('system/modal.js' => array()),
'stylesheet' => array('system/modal.css' => array(array(), true, false, true))
)
);

If more CSS or JS files need to be included, they can be added to the 'script' or 'stylesheet' array. For each element in the array the key is the path to the file, this file will get parsed by JHtml::_includeRelativeFiles, and the value for the key is an array of arguments that you would liked passed to JHtml::script or JHtml::stylesheet respectively.

I modified JHtmlBehavior::modal as an example of how this new feature can be implemented. It makes a call to self::load('modal'); to add the library to the document.

Please review and comment.

Cheers,
Rafael

@rcorral rcorral Added feature to JHtmlBehavior register javascript libraries
I have modified the modal method to use the new code I created as an example of usage.
d8ab1a6
@LouisLandry

@rcorral this pull request is no longer mergeable. While I am personally indifferent to the feature, I'm happy to merge it if others are positive towards it. I would probably want to see a couple of unit tests so that we can avoid regressions and confusion about the feature down the road though.

For now I am going to close the pull request. This isn't because I'm rejecting it, but because it really isn't in a reviewable state right now. If you still want to pursue it please rebase the branch so that it is mergeable again and re-open the pull request. If we can get some interest from other developers that will certainly help.

Thanks a bunch for the initiative and work!

@LouisLandry LouisLandry closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 8, 2012
  1. @rcorral

    Added feature to JHtmlBehavior register javascript libraries

    rcorral authored
    I have modified the modal method to use the new code I created as an example of usage.
This page is out of date. Refresh to see the latest.
Showing with 132 additions and 2 deletions.
  1. +132 −2 libraries/joomla/html/html/behavior.php
View
134 libraries/joomla/html/html/behavior.php
@@ -24,6 +24,137 @@
protected static $loaded = array();
/**
+ * @var array Array contains a list of all scripts and all the files that relate to it
+ */
+ protected static $scripts = array();
+
+ /**
+ * Method that initializes all known behaviors
+ *
+ * @return void
+ *
+ * @since 11.4
+ */
+ protected static function init()
+ {
+ // Only load once
+ if (!empty(self::$loaded[__METHOD__]))
+ {
+ return;
+ }
+
+ self::register('modal', array(
+ 'script' => array('system/modal.js' => array()),
+ 'stylesheet' => array('system/modal.css' => array(array(), true, false, true))
+ )
+ );
+
+ self::$loaded[__METHOD__] = true;
+
+ return;
+ }
+
+ /**
+ * Registers new behaviors
+ *
+ * @param string $name The name of the behavior
+ * @param array $files A multidimensional array where the key tells the types of files in the child array so that they can get added correctly via JHtml::script
+ * @param string $override Override paths to behavior if they exists
+ *
+ * @return void
+ *
+ * @since 11.4
+ */
+ public static function register($name, $files, $override = false)
+ {
+ if (isset(self::$scripts[$name]) && false === $override)
+ {
+ return;
+ }
+
+ self::$scripts[$name] = $files;
+
+ return self::$scripts;
+ }
+
+ /**
+ * Method to load any javascript file into the document head
+ *
+ * If debugging mode is on an uncompressed version of the script is included
+ *
+ * @param string $script JavaScript file to load
+ * @param mixed $dependencies An array of JavaScript files that the script depends on [optional]
+ * @param boolean $debug Is debugging mode on? [optional]
+ *
+ * @return void
+ *
+ * @since 11.4
+ */
+ public static function load($script, $dependencies = array(), $debug = null)
+ {
+ // Only load once
+ if (!empty(self::$loaded[__METHOD__][$script]))
+ {
+ return;
+ }
+
+ self::init();
+
+ // Make sure that script is registered
+ if (!isset(self::$scripts[$script]))
+ {
+ return;
+ }
+
+ // If no debugging value is set, use the configuration setting
+ if ($debug === null)
+ {
+ $debug = JFactory::getConfig()->get('debug');
+ }
+
+ // Add dependencies
+ foreach ((array) $dependencies as $_script)
+ {
+ self::load($_script, array(), $debug);
+ }
+
+ foreach (self::$scripts[$script] as $type => $_scripts)
+ {
+ foreach ($_scripts as $_file => $_args)
+ {
+ switch ($type)
+ {
+ case 'script':
+ // Merge $_args with default arguments
+ $args = array_merge(array(false, true, false, false, $debug), (array) $_args);
+ array_unshift($args, $type, $_file);
+ call_user_func_array('JHtml::_', $args);
+ break;
+
+ case 'stylesheet':
+ // Merge $_args with default arguments
+ $args = array_merge(array(array(), true, false, false, $debug), (array) $_args);
+ array_unshift($args, $type, $_file);
+ call_user_func_array('JHtml::_', $args);
+ break;
+
+ default:
+ break;
+ }
+ }
+ }
+
+ if ('jquery' == $script)
+ {
+ JFactory::getDocument()->addScriptDeclaration('jQuery.noConflict();');
+ }
+
+ self::$loaded[__METHOD__][$script] = true;
+
+ return;
+ }
+
+ /**
* Method to load the MooTools framework into the document head
*
* If debugging mode is on an uncompressed version of MooTools is included for easier debugging.
@@ -287,8 +418,7 @@ public static function modal($selector = 'a.modal', $params = array())
self::framework(true);
// Load the javascript and css
- JHtml::_('script', 'system/modal.js', true, true);
- JHtml::_('stylesheet', 'system/modal.css', array(), true);
+ self::load('modal');
}
$sig = md5(serialize(array($selector, $params)));
Something went wrong with that request. Please try again.