Skip to content
This repository

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

Rafael Corral Louis Landry
Rafael Corral
rcorral commented May 08, 2012

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

Rafael Corral 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
Louis Landry

@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!

Louis Landry LouisLandry closed this October 09, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

May 08, 2012
Rafael Corral 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
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 132 additions and 2 deletions. Show diff stats Hide diff stats

  1. 134  libraries/joomla/html/html/behavior.php
134  libraries/joomla/html/html/behavior.php
@@ -24,6 +24,137 @@
24 24
 	protected static $loaded = array();
25 25
 
26 26
 	/**
  27
+	 * @var   array   Array contains a list of all scripts and all the files that relate to it
  28
+	 */
  29
+	protected static $scripts = array();
  30
+
  31
+	/**
  32
+	 * Method that initializes all known behaviors
  33
+	 *
  34
+	 * @return  void
  35
+	 *
  36
+	 * @since   11.4
  37
+	 */
  38
+	protected static function init()
  39
+	{
  40
+		// Only load once
  41
+		if (!empty(self::$loaded[__METHOD__]))
  42
+		{
  43
+			return;
  44
+		}
  45
+
  46
+		self::register('modal', array(
  47
+			'script' => array('system/modal.js' => array()),
  48
+			'stylesheet' => array('system/modal.css' => array(array(), true, false, true))
  49
+			)
  50
+		);
  51
+
  52
+		self::$loaded[__METHOD__] = true;
  53
+
  54
+		return;
  55
+	}
  56
+
  57
+	/**
  58
+	 * Registers new behaviors
  59
+	 *
  60
+	 * @param   string  $name      The name of the behavior
  61
+	 * @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
  62
+	 * @param   string  $override  Override paths to behavior if they exists
  63
+	 *
  64
+	 * @return  void
  65
+	 *
  66
+	 * @since   11.4
  67
+	 */
  68
+	public static function register($name, $files, $override = false)
  69
+	{
  70
+		if (isset(self::$scripts[$name]) && false === $override)
  71
+		{
  72
+			return;
  73
+		}
  74
+
  75
+		self::$scripts[$name] = $files;
  76
+
  77
+		return self::$scripts;
  78
+	}
  79
+
  80
+	/**
  81
+	 * Method to load any javascript file into the document head
  82
+	 *
  83
+	 * If debugging mode is on an uncompressed version of the script is included
  84
+	 *
  85
+	 * @param   string   $script        JavaScript file to load
  86
+	 * @param   mixed    $dependencies  An array of JavaScript files that the script depends on [optional]
  87
+	 * @param   boolean  $debug         Is debugging mode on? [optional]
  88
+	 *
  89
+	 * @return  void
  90
+	 *
  91
+	 * @since   11.4
  92
+	 */
  93
+	public static function load($script, $dependencies = array(), $debug = null)
  94
+	{
  95
+		// Only load once
  96
+		if (!empty(self::$loaded[__METHOD__][$script]))
  97
+		{
  98
+			return;
  99
+		}
  100
+
  101
+		self::init();
  102
+
  103
+		// Make sure that script is registered
  104
+		if (!isset(self::$scripts[$script]))
  105
+		{
  106
+			return;
  107
+		}
  108
+
  109
+		// If no debugging value is set, use the configuration setting
  110
+		if ($debug === null)
  111
+		{
  112
+			$debug = JFactory::getConfig()->get('debug');
  113
+		}
  114
+
  115
+		// Add dependencies
  116
+		foreach ((array) $dependencies as $_script)
  117
+		{
  118
+			self::load($_script, array(), $debug);
  119
+		}
  120
+
  121
+		foreach (self::$scripts[$script] as $type => $_scripts)
  122
+		{
  123
+			foreach ($_scripts as $_file => $_args)
  124
+			{
  125
+				switch ($type)
  126
+				{
  127
+					case 'script':
  128
+						// Merge $_args with default arguments
  129
+						$args = array_merge(array(false, true, false, false, $debug), (array) $_args);
  130
+						array_unshift($args, $type, $_file);
  131
+						call_user_func_array('JHtml::_', $args);
  132
+						break;
  133
+
  134
+					case 'stylesheet':
  135
+						// Merge $_args with default arguments
  136
+						$args = array_merge(array(array(), true, false, false, $debug), (array) $_args);
  137
+						array_unshift($args, $type, $_file);
  138
+						call_user_func_array('JHtml::_', $args);
  139
+						break;
  140
+
  141
+					default:
  142
+						break;
  143
+				}
  144
+			}
  145
+		}
  146
+
  147
+		if ('jquery' == $script)
  148
+		{
  149
+			JFactory::getDocument()->addScriptDeclaration('jQuery.noConflict();');
  150
+		}
  151
+
  152
+		self::$loaded[__METHOD__][$script] = true;
  153
+
  154
+		return;
  155
+	}
  156
+
  157
+	/**
27 158
 	 * Method to load the MooTools framework into the document head
28 159
 	 *
29 160
 	 * 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())
287 418
 			self::framework(true);
288 419
 
289 420
 			// Load the javascript and css
290  
-			JHtml::_('script', 'system/modal.js', true, true);
291  
-			JHtml::_('stylesheet', 'system/modal.css', array(), true);
  421
+			self::load('modal');
292 422
 		}
293 423
 
294 424
 		$sig = md5(serialize(array($selector, $params)));
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.