From d4cab20408bcfa394ca978a16e09e6fd84539350 Mon Sep 17 00:00:00 2001 From: cash Date: Sat, 12 Mar 2011 20:08:44 +0000 Subject: Refs #3091 rewrote the js and css register functions to require a name git-svn-id: http://code.elgg.org/elgg/trunk@8668 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/admin.php | 6 +-- engine/lib/elgglib.php | 103 +++++++++++++++++++++---------------------- engine/lib/views.php | 16 +++---- engine/tests/api/helpers.php | 39 +++++----------- 4 files changed, 74 insertions(+), 90 deletions(-) (limited to 'engine') diff --git a/engine/lib/admin.php b/engine/lib/admin.php index e788e3056..4e0c4f681 100644 --- a/engine/lib/admin.php +++ b/engine/lib/admin.php @@ -282,7 +282,7 @@ function elgg_admin_add_plugin_settings_menu() { function admin_pagesetup() { if (elgg_in_context('admin')) { $url = elgg_get_simplecache_url('css', 'admin'); - elgg_register_css($url, 'admin'); + elgg_register_css('elgg.admin', $url); elgg_unregister_css('elgg'); // setup footer menu @@ -327,9 +327,9 @@ function admin_settings_page_handler($page) { elgg_unregister_css('elgg'); $url = elgg_get_simplecache_url('js', 'admin'); - elgg_register_js($url, 'admin'); + elgg_register_js('elgg.admin', $url); - elgg_register_js('vendors/jquery/jquery.jeditable.mini.js', 'jquery.jeditable'); + elgg_register_js('jquery.jeditable', 'vendors/jquery/jquery.jeditable.mini.js'); // default to dashboard if (!isset($page[0]) || empty($page[0])) { diff --git a/engine/lib/elgglib.php b/engine/lib/elgglib.php index bc272fbd7..7b21f7dfc 100644 --- a/engine/lib/elgglib.php +++ b/engine/lib/elgglib.php @@ -163,47 +163,55 @@ function forward($location = "", $reason = 'system') { * JavaScript from a view that may be called more than once. It also handles * more than one plugin adding the same JavaScript. * - * Plugin authors are encouraged to use the $id variable. jQuery plugins - * often have filenames such as jquery.rating.js. In that case, the id - * would be "jquery.rating". It is recommended to not use version numbers - * in the id. + * jQuery plugins often have filenames such as jquery.rating.js. A best practice + * is to base $name on the filename: "jquery.rating". It is recommended to not + * use version numbers in the name. * * The JavaScript files can be local to the server or remote (such as * Google's CDN). * + * @param string $name An identifier for the JavaScript library * @param string $url URL of the JavaScript file - * @param string $id An identifier of the JavaScript library * @param string $location Page location: head or footer. (default: head) + * @param int $priority Priority of the CSS file (lower numbers load earlier) + * * @return bool + * @since 1.8.0 */ -function elgg_register_js($url, $id = '', $location = 'head') { - return elgg_register_external_file('javascript', $url, $id, $location); +function elgg_register_js($name, $url, $location = 'head', $priority = 500) { + return elgg_register_external_file('js', $name, $url, $location, $priority); } /** * Register a CSS file for inclusion in the HTML head * - * @param string $url URL of the CSS file - * @param string $id An identifier for the CSS file + * @param string $name An identifier for the CSS file + * @param string $url URL of the CSS file + * @param int $priority Priority of the CSS file (lower numbers load earlier) + * * @return bool + * @since 1.8.0 */ -function elgg_register_css($url, $id = '') { - return elgg_register_external_file('css', $url, $id, 'head'); +function elgg_register_css($name, $url, $priority = 500) { + return elgg_register_external_file('css', $name, $url, 'head', $priority); } /** * Core registration function for external files * * @param string $type Type of external resource + * @param string $name Identifier used as key * @param string $url URL - * @param string $id Identifier used as key * @param string $location Location in the page to include the file + * @param int $priority Loading priority of the file + * * @return bool + * @since 1.8.0 */ -function elgg_register_external_file($type, $url, $id, $location) { +function elgg_register_external_file($type, $name, $url, $location, $priority) { global $CONFIG; - if (empty($url)) { + if (empty($name) || empty($url)) { return false; } @@ -221,13 +229,8 @@ function elgg_register_external_file($type, $url, $id, $location) { $CONFIG->externals[$type][$location] = array(); } - if (!$id) { - $id = count($CONFIG->externals[$type][$location]); - } else { - $id = trim(strtolower($id)); - } - - $CONFIG->externals[$type][$location][$id] = elgg_normalize_url($url); + $name = trim(strtolower($name)); + $CONFIG->externals[$type][$location][$name] = elgg_normalize_url($url); return true; } @@ -235,36 +238,37 @@ function elgg_register_external_file($type, $url, $id, $location) { /** * Unregister a JavaScript file * - * @param string $id The identifier for the JavaScript library - * @param string $url Optional URL to search for if id is not specified - * @param string $location Location in the page + * @param string $name The identifier for the JavaScript library + * * @return bool + * @since 1.8.0 */ -function elgg_unregister_js($id = '', $url = '', $location = 'head') { - return elgg_unregister_external_file('javascript', $id, $url, $location); +function elgg_unregister_js($name) { + return elgg_unregister_external_file('js', $name); } /** - * Unregister an external file + * Unregister a CSS file + * + * @param string $name The identifier for the CSS file * - * @param string $id The identifier of the CSS file - * @param string $url Optional URL to search for if id is not specified * @return bool + * @since 1.8.0 */ -function elgg_unregister_css($id = '', $url = '') { - return elgg_unregister_external_file('css', $id, $url, 'head'); +function elgg_unregister_css($name) { + return elgg_unregister_external_file('css', $name); } /** * Unregister an external file * - * @param string $type Type of file: javascript or css - * @param string $id The identifier of the file - * @param string $url Optional URL to search for if the id is not specified - * @param string $location Location in the page + * @param string $type Type of file: javascript or css + * @param string $name The identifier of the file + * * @return bool + * @since 1.8.0 */ -function elgg_unregister_external_file($type, $id = '', $url = '', $location = 'head') { +function elgg_unregister_external_file($type, $name) { global $CONFIG; if (!isset($CONFIG->externals)) { @@ -275,20 +279,11 @@ function elgg_unregister_external_file($type, $id = '', $url = '', $location = ' return false; } - if (!isset($CONFIG->externals[$type][$location])) { - return false; - } - - if (array_key_exists($id, $CONFIG->externals[$type][$location])) { - unset($CONFIG->externals[$type][$location][$id]); - return true; - } - - // was not registered with an id so do a search for the url - $key = array_search($url, $CONFIG->externals[$type][$location]); - if ($key) { - unset($CONFIG->externals[$type][$location][$key]); - return true; + foreach ($CONFIG->externals[$type] as $location => $files) { + if (array_key_exists($name, $CONFIG->externals[$type][$location])) { + unset($CONFIG->externals[$type][$location][$name]); + return true; + } } return false; @@ -300,15 +295,17 @@ function elgg_unregister_external_file($type, $id = '', $url = '', $location = ' * @param string $location 'head' or 'footer' * * @return array + * @since 1.8.0 */ function elgg_get_js($location = 'head') { - return elgg_get_external_file('javascript', $location); + return elgg_get_external_file('js', $location); } /** * Get the CSS URLs * * @return array + * @since 1.8.0 */ function elgg_get_css() { return elgg_get_external_file('css', 'head'); @@ -319,7 +316,9 @@ function elgg_get_css() { * * @param string $type Type of resource * @param string $location Page location + * * @return array + * @since 1.8.0 */ function elgg_get_external_file($type, $location) { global $CONFIG; @@ -1761,7 +1760,7 @@ function elgg_is_valid_options_for_batch_operation($options, $type) { * @return boolean */ function elgg_walled_garden_index() { - elgg_register_css('/css/walled_garden.css'); + elgg_register_css('elgg.walled_garden', '/css/walled_garden.css'); $login = elgg_view('core/account/login_walled_garden'); echo elgg_view_page('', $login, 'walled_garden'); diff --git a/engine/lib/views.php b/engine/lib/views.php index 15e8b179b..3d126332a 100644 --- a/engine/lib/views.php +++ b/engine/lib/views.php @@ -926,14 +926,14 @@ function elgg_view_annotation(ElggAnnotation $annotation, $full = true, $bypass * @return string The rendered list of entities * @access private */ -function elgg_view_entity_list($entities, $vars, $offset = 0, $limit = 10, $full_view = true, +function elgg_view_entity_list($entities, $vars = array(), $offset = 0, $limit = 10, $full_view = true, $list_type_toggle = true, $pagination = true) { if (!is_int($offset)) { $offset = (int)get_input('offset', 0); } - if (func_num_args() == 2) { + if (is_array($vars)) { // new function $defaults = array( 'items' => $entities, @@ -1256,7 +1256,7 @@ function elgg_view_form($action, $form_vars = array(), $body_vars = array()) { * @since 1.8.0 * @access private */ -function elgg_view_list_item($item, $full_view, array $vars = array()) { +function elgg_view_list_item($item, array $vars = array()) { $full_view = elgg_extract('full_view', $vars, false); @@ -1470,10 +1470,10 @@ function autoregister_views($view_base, $folder, $base_location_path, $viewtype) */ function elgg_views_register_core_head_elements() { $url = elgg_get_simplecache_url('js', 'elgg'); - elgg_register_js($url, 'elgg'); + elgg_register_js('elgg', $url, 'head', 10); $url = elgg_get_simplecache_url('css', 'elgg'); - elgg_register_css($url, 'elgg'); + elgg_register_css('elgg', $url, 10); } /** @@ -1492,9 +1492,9 @@ function elgg_views_boot() { elgg_register_simplecache_view('css/ie6'); elgg_register_simplecache_view('js/elgg'); - elgg_register_js("/vendors/jquery/jquery-1.5.min.js", 'jquery'); - elgg_register_js("/vendors/jquery/jquery-ui-1.8.9.min.js", 'jquery-ui'); - elgg_register_js("/vendors/jquery/jquery.form.js", 'jquery.form'); + elgg_register_js('jquery', '/vendors/jquery/jquery-1.5.min.js', 'head', 1); + elgg_register_js('jquery-ui', '/vendors/jquery/jquery-ui-1.8.9.min.js', 'head', 2); + elgg_register_js('jquery.form', '/vendors/jquery/jquery.form.js'); elgg_register_event_handler('ready', 'system', 'elgg_views_register_core_head_elements'); diff --git a/engine/tests/api/helpers.php b/engine/tests/api/helpers.php index 488fd8390..b8cf96900 100644 --- a/engine/tests/api/helpers.php +++ b/engine/tests/api/helpers.php @@ -103,15 +103,10 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest { public function testElggRegisterJS() { global $CONFIG; - // specify id - $result = elgg_register_js('//test1.com', 'key', 'footer'); + // specify name + $result = elgg_register_js('key', '//test1.com', 'footer'); $this->assertTrue($result); - $this->assertIdentical('//test1.com', $CONFIG->externals['javascript']['footer']['key']); - - // let Elgg pick id - $result = elgg_register_js('//test2.com'); - $this->assertTrue($result); - $this->assertIdentical('//test2.com', $CONFIG->externals['javascript']['head'][0]); + $this->assertIdentical('//test1.com', $CONFIG->externals['js']['footer']['key']); // send a bad url $result = elgg_register_js(); @@ -124,19 +119,10 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest { public function testElggRegisterCSS() { global $CONFIG; - // specify id - $result = elgg_register_css('//test1.com', 'key'); + // specify name + $result = elgg_register_css('key', '//test1.com'); $this->assertTrue($result); $this->assertIdentical('//test1.com', $CONFIG->externals['css']['head']['key']); - - // let Elgg pick id - $result = elgg_register_css('//test2.com'); - $this->assertTrue($result); - $this->assertIdentical('//test2.com', $CONFIG->externals['css']['head'][1]); - - // send a bad url - $result = elgg_register_js(); - $this->assertFalse($result); } /** @@ -147,23 +133,20 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest { $urls = array('id1' => '//url1.com', 'id2' => '//url2.com', 'id3' => '//url3.com'); foreach ($urls as $id => $url) { - elgg_register_js($url, $id); + elgg_register_js($id, $url); } $result = elgg_unregister_js('id1'); $this->assertTrue($result); - $this->assertNULL($CONFIG->externals['javascript']['head']['id1']); - - $result = elgg_unregister_js('', '//url2.com'); - $this->assertTrue($result); - $this->assertNULL($CONFIG->externals['javascript']['head']['id2']); + $this->assertNULL($CONFIG->externals['js']['head']['id1']); $result = elgg_unregister_js('id1'); $this->assertFalse($result); $result = elgg_unregister_js('', '//url2.com'); $this->assertFalse($result); - $this->assertIdentical($urls['id3'], $CONFIG->externals['javascript']['head']['id3']); + $result = elgg_unregister_js('id2'); + $this->assertIdentical($urls['id3'], $CONFIG->externals['js']['head']['id3']); } /** @@ -172,7 +155,9 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest { public function testElggGetJS() { global $CONFIG; - $urls = array('id1' => '//url1.com', 'id2' => '//url2.com', 'id3' => '//url3.com'); + $base = trim(elgg_get_site_url(), "/"); + + $urls = array('id1' => "$base/id1", 'id2' => "$base/id2", 'id3' => "$base/id3"); foreach ($urls as $id => $url) { elgg_register_js($url, $id); } -- cgit v1.2.3