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 +++----- mod/blog/lib/blog.php | 4 +- mod/notifications/index.php | 2 +- .../notifications/subscriptions/forminternals.php | 4 +- mod/pages/start.php | 4 +- mod/tinymce/views/default/tinymce/init.php | 4 +- mod/zaudio/views/default/zaudio/audioplayer.php | 2 +- views/default/admin/plugins/advanced.php | 5 +- views/default/input/autocomplete.php | 2 +- views/default/input/friendspicker.php | 4 +- views/default/input/userpicker.php | 2 +- views/default/page/components/list.php | 5 + 15 files changed, 97 insertions(+), 105 deletions(-) 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); } diff --git a/mod/blog/lib/blog.php b/mod/blog/lib/blog.php index 81d06ca0a..94ab65da1 100644 --- a/mod/blog/lib/blog.php +++ b/mod/blog/lib/blog.php @@ -299,7 +299,7 @@ function blog_get_page_content_edit($page, $guid = 0, $revision = NULL) { elgg_push_breadcrumb(elgg_echo('edit')); $blog_js = elgg_get_simplecache_url('js', 'blog/save_draft'); - elgg_register_js($blog_js, 'blog'); + elgg_register_js('elgg.blog', $blog_js); $content = elgg_view_form('blog/save', $vars, $body_vars); $sidebar = elgg_view('blog/sidebar/revisions', $vars); @@ -320,7 +320,7 @@ function blog_get_page_content_edit($page, $guid = 0, $revision = NULL) { $content = elgg_view_form('blog/save', $vars, $body_vars); $blog_js = elgg_get_simplecache_url('js', 'blog/save_draft'); - elgg_register_js($blog_js, 'blog'); + elgg_register_js('elgg.blog', $blog_js); } $return['title'] = $title; diff --git a/mod/notifications/index.php b/mod/notifications/index.php index f970dfc32..30ee1b74b 100644 --- a/mod/notifications/index.php +++ b/mod/notifications/index.php @@ -14,7 +14,7 @@ gatekeeper(); set_page_owner(elgg_get_logged_in_user_guid()); $js_url = elgg_get_simplecache_url('js', 'friendsPickerv1'); -elgg_register_js($js_url, 'friendsPicker'); +elgg_register_js('friendsPicker', $js_url); // Set the context to settings elgg_set_context('settings'); diff --git a/mod/notifications/views/default/notifications/subscriptions/forminternals.php b/mod/notifications/views/default/notifications/subscriptions/forminternals.php index 936194842..fac8c083c 100644 --- a/mod/notifications/views/default/notifications/subscriptions/forminternals.php +++ b/mod/notifications/views/default/notifications/subscriptions/forminternals.php @@ -3,8 +3,8 @@ * Hacked up friends picker that needs to be replaced */ -elgg_register_js('js/lib/friends_picker.js', 'friendspicker', 'head'); -elgg_register_js('vendors/jquery/jquery.easing.1.3.packed.js', 'jquery.easing'); +elgg_register_js('elgg.friendspicker', 'js/lib/friends_picker.js'); +elgg_register_js('jquery.easing', 'vendors/jquery/jquery.easing.1.3.packed.js'); ?>
diff --git a/mod/pages/start.php b/mod/pages/start.php index 1c655bd07..23c564da5 100644 --- a/mod/pages/start.php +++ b/mod/pages/start.php @@ -101,9 +101,9 @@ function pages_page_handler($page) { // add the jquery treeview files for navigation $js_url = elgg_get_site_url() . 'mod/pages/vendors/jquery-treeview/jquery.treeview.min.js'; - elgg_register_js($js_url, 'jquery-treeview'); + elgg_register_js('jquery-treeview', $js_url); $css_url = elgg_get_site_url() . 'mod/pages/vendors/jquery-treeview/jquery.treeview.css'; - elgg_register_css($css_url, 'jquery-treeview'); + elgg_register_css('jquery-treeview', $css_url); if (!isset($page[0])) { $page[0] = 'all'; diff --git a/mod/tinymce/views/default/tinymce/init.php b/mod/tinymce/views/default/tinymce/init.php index 408aba094..3fe08771c 100644 --- a/mod/tinymce/views/default/tinymce/init.php +++ b/mod/tinymce/views/default/tinymce/init.php @@ -3,5 +3,5 @@ * Initialize the TinyMCE script */ -elgg_register_js('mod/tinymce/vendor/tinymce/jscripts/tiny_mce/tiny_mce.js', 'tinymce'); -elgg_register_js(elgg_get_simplecache_url('js', 'tinymce'), 'elgg.tinymce'); \ No newline at end of file +elgg_register_js('tinymce', 'mod/tinymce/vendor/tinymce/jscripts/tiny_mce/tiny_mce.js'); +elgg_register_js('elgg.tinymce', elgg_get_simplecache_url('js', 'tinymce')); \ No newline at end of file diff --git a/mod/zaudio/views/default/zaudio/audioplayer.php b/mod/zaudio/views/default/zaudio/audioplayer.php index 2708f4cec..6898506a6 100644 --- a/mod/zaudio/views/default/zaudio/audioplayer.php +++ b/mod/zaudio/views/default/zaudio/audioplayer.php @@ -5,7 +5,7 @@ */ $js_url = elgg_get_site_url() . 'mod/zaudio/audioplayer/audio-player.js'; -elgg_register_js($js_url, 'zaudio'); +elgg_register_js('elgg.zaudio', $js_url); $swf_url = elgg_get_site_url() . 'mod/zaudio/audioplayer/player.swf'; $mp3_url = elgg_get_site_url() . "mod/file/download.php?file_guid={$vars['file_guid']}"; diff --git a/views/default/admin/plugins/advanced.php b/views/default/admin/plugins/advanced.php index 49e31d9c1..e78dbe2f1 100644 --- a/views/default/admin/plugins/advanced.php +++ b/views/default/admin/plugins/advanced.php @@ -85,7 +85,10 @@ $buttons .= $category_form;
\ No newline at end of file diff --git a/views/default/input/autocomplete.php b/views/default/input/autocomplete.php index 61f338611..212333141 100644 --- a/views/default/input/autocomplete.php +++ b/views/default/input/autocomplete.php @@ -29,7 +29,7 @@ $ac_url_params = http_build_query(array( unset($vars['match_on']); unset($vars['match_owner']); -elgg_register_js('js/lib/autocomplete.js', 'autocomplete', 'head'); +elgg_register_js('elgg.autocomplete', 'js/lib/autocomplete.js'); ?> diff --git a/views/default/input/friendspicker.php b/views/default/input/friendspicker.php index 82ef3e3d7..b5f347e0c 100644 --- a/views/default/input/friendspicker.php +++ b/views/default/input/friendspicker.php @@ -11,8 +11,8 @@ * @uses $vars['entities'] The array of ElggUser objects */ -elgg_register_js('js/lib/friends_picker.js', 'friendspicker', 'head'); -elgg_register_js('vendors/jquery/jquery.easing.1.3.packed.js', 'jquery.easing'); +elgg_register_js('elgg.friendspicker', 'js/lib/friends_picker.js'); +elgg_register_js('jquery.easing', 'vendors/jquery/jquery.easing.1.3.packed.js'); $chararray = elgg_echo('friendspicker:chararray'); diff --git a/views/default/input/userpicker.php b/views/default/input/userpicker.php index 77a51f37c..8280305b0 100644 --- a/views/default/input/userpicker.php +++ b/views/default/input/userpicker.php @@ -18,7 +18,7 @@ * */ -elgg_register_js('js/lib/userpicker.js', 'userpicker', 'head'); +elgg_register_js('elgg.userpicker', 'js/lib/userpicker.js'); function user_picker_add_user($user_id) { $user = get_entity($user_id); diff --git a/views/default/page/components/list.php b/views/default/page/components/list.php index ae951c89f..5e8c8432d 100644 --- a/views/default/page/components/list.php +++ b/views/default/page/components/list.php @@ -25,6 +25,11 @@ $pagination = elgg_extract('pagination', $vars, true); $offset_key = elgg_extract('offset_key', $vars, 'offset'); $position = elgg_extract('position', $vars, 'after'); +// @todo standardize on full - will require backward compatible code +if (isset($vars['full_view'])) { + $vars['full'] = $vars['full_view']; +} + $list_class = 'elgg-list'; if (isset($vars['list_class'])) { $list_class = "{$vars['list_class']} $list_class"; -- cgit v1.2.3