diff options
author | cash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2011-03-12 22:25:02 +0000 |
---|---|---|
committer | cash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2011-03-12 22:25:02 +0000 |
commit | bfb0a3e9b289e4036bfeb3e694586387fc4f904e (patch) | |
tree | 6beec05b6424702e036d67622db37ac6f8852a54 | |
parent | 1bd696b3d8eb8175cc2b6583716fe3e89110aab0 (diff) | |
download | elgg-bfb0a3e9b289e4036bfeb3e694586387fc4f904e.tar.gz elgg-bfb0a3e9b289e4036bfeb3e694586387fc4f904e.tar.bz2 |
Fixes #3091 adds load functions for js and css
git-svn-id: http://code.elgg.org/elgg/trunk@8675 36083f99-b078-4883-b0ff-0f9b5a30f544
-rw-r--r-- | engine/lib/admin.php | 3 | ||||
-rw-r--r-- | engine/lib/elgglib.php | 202 | ||||
-rw-r--r-- | engine/lib/views.php | 5 | ||||
-rw-r--r-- | engine/tests/api/helpers.php | 27 | ||||
-rw-r--r-- | mod/blog/lib/blog.php | 3 | ||||
-rw-r--r-- | mod/blog/start.php | 4 | ||||
-rw-r--r-- | mod/developers/views/default/page/theme_preview.php | 2 | ||||
-rw-r--r-- | mod/notifications/index.php | 3 | ||||
-rw-r--r-- | mod/notifications/views/default/notifications/subscriptions/forminternals.php | 4 | ||||
-rw-r--r-- | mod/pages/start.php | 14 | ||||
-rw-r--r-- | mod/tinymce/start.php | 3 | ||||
-rw-r--r-- | mod/tinymce/views/default/tinymce/init.php | 4 | ||||
-rw-r--r-- | mod/zaudio/start.php | 3 | ||||
-rw-r--r-- | mod/zaudio/views/default/zaudio/audioplayer.php | 3 | ||||
-rw-r--r-- | views/default/input/autocomplete.php | 2 | ||||
-rw-r--r-- | views/default/input/friendspicker.php | 4 | ||||
-rw-r--r-- | views/default/input/userpicker.php | 2 | ||||
-rw-r--r-- | views/default/page/elements/head.php | 4 |
18 files changed, 203 insertions, 89 deletions
diff --git a/engine/lib/admin.php b/engine/lib/admin.php index 104b362a9..8ef01e7cc 100644 --- a/engine/lib/admin.php +++ b/engine/lib/admin.php @@ -285,6 +285,7 @@ function admin_pagesetup() { if (elgg_in_context('admin')) { $url = elgg_get_simplecache_url('css', 'admin'); elgg_register_css('elgg.admin', $url); + elgg_load_css('elgg.admin'); elgg_unregister_css('elgg'); // setup footer menu @@ -330,8 +331,10 @@ function admin_settings_page_handler($page) { elgg_unregister_css('elgg'); $url = elgg_get_simplecache_url('js', 'admin'); elgg_register_js('elgg.admin', $url); + elgg_load_js('elgg.admin'); elgg_register_js('jquery.jeditable', 'vendors/jquery/jquery.jeditable.mini.js'); + elgg_load_js('jquery.jeditable'); // default to dashboard if (!isset($page[0]) || empty($page[0])) { diff --git a/engine/lib/elgglib.php b/engine/lib/elgglib.php index 93be114f2..45546a3f0 100644 --- a/engine/lib/elgglib.php +++ b/engine/lib/elgglib.php @@ -183,6 +183,44 @@ function elgg_register_js($name, $url, $location = 'head', $priority = 500) { } /** + * Unregister a JavaScript file + * + * @param string $name The identifier for the JavaScript library + * + * @return bool + * @since 1.8.0 + */ +function elgg_unregister_js($name) { + return elgg_unregister_external_file('js', $name); +} + +/** + * Load a JavaScript resource on this page + * + * This must be called before elgg_view_page(). It can be called before the + * script is registered. If you do not want a script loaded, unregister it. + * + * @param string $name Identifier of the JavaScript resource + * + * @since 1.8.0 + */ +function elgg_load_js($name) { + elgg_load_external_file('js', $name); +} + +/** + * Get the JavaScript URLs that are loaded + * + * @param string $location 'head' or 'footer' + * + * @return array + * @since 1.8.0 + */ +function elgg_get_loaded_js($location = 'head') { + return elgg_get_loaded_external_files('js', $location); +} + +/** * Register a CSS file for inclusion in the HTML head * * @param string $name An identifier for the CSS file @@ -197,9 +235,45 @@ function elgg_register_css($name, $url, $priority = 500) { } /** + * Unregister a CSS file + * + * @param string $name The identifier for the CSS file + * + * @return bool + * @since 1.8.0 + */ +function elgg_unregister_css($name) { + return elgg_unregister_external_file('css', $name); +} + +/** + * Load a CSS file for this page + * + * This must be called before elgg_view_page(). It can be called before the + * CSS file is registered. If you do not want a CSS file loaded, unregister it. + * + * @param string $name Identifier of the CSS file + * + * @since 1.8.0 + */ +function elgg_load_css($name) { + elgg_load_external_file('css', $name); +} + +/** + * Get the loaded CSS URLs + * + * @return array + * @since 1.8.0 + */ +function elgg_get_loaded_css() { + return elgg_get_loaded_external_files('css', 'head'); +} + +/** * Core registration function for external files * - * @param string $type Type of external resource + * @param string $type Type of external resource (js or css) * @param string $name Identifier used as key * @param string $url URL * @param string $location Location in the page to include the file @@ -216,58 +290,40 @@ function elgg_register_external_file($type, $name, $url, $location, $priority = } $url = elgg_format_url($url); - + $url = elgg_normalize_url($url); + if (!isset($CONFIG->externals)) { $CONFIG->externals = array(); } if (!isset($CONFIG->externals[$type])) { - $CONFIG->externals[$type] = array(); - } - - if (!isset($CONFIG->externals[$type][$location])) { - $CONFIG->externals[$type][$location] = array(); + $CONFIG->externals[$type] = array(); } $name = trim(strtolower($name)); - $item = new stdClass(); - $item->url = elgg_normalize_url($url); - $item->priority = max((int)$priority, 0); + if (isset($CONFIG->externals[$type][$name])) { + // update a registered item + $item = $CONFIG->externals[$type][$name]; - $CONFIG->externals[$type][$location][$name] = $item; + } else { + $item = new stdClass(); + $item->loaded = false; + } - return true; -} + $item->url = $url; + $item->priority = max((int)$priority, 0); + $item->location = $location; -/** - * Unregister a JavaScript file - * - * @param string $name The identifier for the JavaScript library - * - * @return bool - * @since 1.8.0 - */ -function elgg_unregister_js($name) { - return elgg_unregister_external_file('js', $name); -} + $CONFIG->externals[$type][$name] = $item; -/** - * Unregister a CSS file - * - * @param string $name The identifier for the CSS file - * - * @return bool - * @since 1.8.0 - */ -function elgg_unregister_css($name) { - return elgg_unregister_external_file('css', $name); + return true; } /** * Unregister an external file * - * @param string $type Type of file: javascript or css + * @param string $type Type of file: js or css * @param string $name The identifier of the file * * @return bool @@ -284,57 +340,72 @@ function elgg_unregister_external_file($type, $name) { return false; } - foreach ($CONFIG->externals[$type] as $location => $files) { - if (array_key_exists($name, $CONFIG->externals[$type][$location])) { - unset($CONFIG->externals[$type][$location][$name]); - return true; - } + $name = trim(strtolower($name)); + + if (array_key_exists($name, $CONFIG->externals[$type])) { + unset($CONFIG->externals[$type][$name]); + return true; } return false; } /** - * Get the JavaScript URLs + * Load an external resource for use on this page * - * @param string $location 'head' or 'footer' + * @param string $type Type of file: js or css + * @param string $name * - * @return array * @since 1.8.0 */ -function elgg_get_js($location = 'head') { - return elgg_get_external_file('js', $location); -} +function elgg_load_external_file($type, $name) { + global $CONFIG; -/** - * Get the CSS URLs - * - * @return array - * @since 1.8.0 - */ -function elgg_get_css() { - return elgg_get_external_file('css', 'head'); + if (!isset($CONFIG->externals)) { + $CONFIG->externals = array(); + } + + if (!isset($CONFIG->externals[$type])) { + $CONFIG->externals[$type] = array(); + } + + $name = trim(strtolower($name)); + + if (isset($CONFIG->externals[$type][$name])) { + // update a registered item + $CONFIG->externals[$type][$name]->loaded = true; + } else { + $item = new stdClass(); + $item->loaded = true; + $item->url = ''; + $item->location = ''; + $item->priority = 500; + + $CONFIG->externals[$type][$name] = $item; + } } /** * Get external resource descriptors * - * @param string $type Type of resource + * @param string $type Type of file: js or css * @param string $location Page location * * @return array * @since 1.8.0 */ -function elgg_get_external_file($type, $location) { +function elgg_get_loaded_external_files($type, $location) { global $CONFIG; - if (isset($CONFIG->externals) && - isset($CONFIG->externals[$type]) && - isset($CONFIG->externals[$type][$location])) { + if (isset($CONFIG->externals) && isset($CONFIG->externals[$type])) { + $items = array_values($CONFIG->externals[$type]); - $items = array_values($CONFIG->externals[$type][$location]); - usort($items, create_function('$a,$b','return $a->priority >= $b->priority;')); - array_walk($items, create_function('&$v,$k', '$v = $v->url;')); + $callback = "return \$v->loaded == true && \$v->location == $location;"; + $items = array_filter($items, create_function('&$v,$k', $callback)); + if ($items) { + usort($items, create_function('$a,$b','return $a->priority >= $b->priority;')); + array_walk($items, create_function('&$v,$k', '$v = $v->url;')); + } return $items; } return array(); @@ -1769,6 +1840,8 @@ function elgg_is_valid_options_for_batch_operation($options, $type) { */ function elgg_walled_garden_index() { elgg_register_css('elgg.walled_garden', '/css/walled_garden.css'); + elgg_load_css('elgg.walled_garden'); + $login = elgg_view('core/account/login_walled_garden'); echo elgg_view_page('', $login, 'walled_garden'); @@ -1816,6 +1889,11 @@ function elgg_init() { elgg_register_page_handler('js', 'js_page_handler'); elgg_register_page_handler('css', 'css_page_handler'); + elgg_register_js('elgg.autocomplete', 'js/lib/autocomplete.js'); + elgg_register_js('elgg.userpicker', 'js/lib/userpicker.js'); + elgg_register_js('elgg.friendspicker', 'js/lib/friends_picker.js'); + elgg_register_js('jquery.easing', 'vendors/jquery/jquery.easing.1.3.packed.js'); + // Trigger the shutdown:system event upon PHP shutdown. register_shutdown_function('_elgg_shutdown_hook'); diff --git a/engine/lib/views.php b/engine/lib/views.php index 3d126332a..637e9c460 100644 --- a/engine/lib/views.php +++ b/engine/lib/views.php @@ -1471,9 +1471,11 @@ 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('elgg', $url, 'head', 10); + elgg_load_js('elgg'); $url = elgg_get_simplecache_url('css', 'elgg'); elgg_register_css('elgg', $url, 10); + elgg_load_css('elgg'); } /** @@ -1495,6 +1497,9 @@ function elgg_views_boot() { 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_load_js('jquery'); + elgg_load_js('jquery-ui'); + elgg_load_js('jquery.form'); 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 b7057220b..1362b3c9d 100644 --- a/engine/tests/api/helpers.php +++ b/engine/tests/api/helpers.php @@ -106,7 +106,7 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest { // specify name $result = elgg_register_js('key', 'http://test1.com', 'footer'); $this->assertTrue($result); - $this->assertIdentical('http://test1.com', $CONFIG->externals['js']['footer']['key']->url); + $this->assertIdentical('http://test1.com', $CONFIG->externals['js']['key']->url); // send a bad url $result = elgg_register_js(); @@ -122,7 +122,7 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest { // specify name $result = elgg_register_css('key', 'http://test1.com'); $this->assertTrue($result); - $this->assertIdentical('http://test1.com', $CONFIG->externals['css']['head']['key']->url); + $this->assertIdentical('http://test1.com', $CONFIG->externals['css']['key']->url); } /** @@ -148,11 +148,25 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest { $this->assertFalse($result); $result = elgg_unregister_js('id2'); - $this->assertIdentical($urls['id3'], $CONFIG->externals['js']['head']['id3']->url); + $this->assertIdentical($urls['id3'], $CONFIG->externals['js']['id3']->url); } /** - * Test elgg_get_js() + * Test elgg_load_js() + */ + public function testElggLoadJS() { + global $CONFIG; + + // load before register + elgg_load_js('key'); + $result = elgg_register_js('key', 'http://test1.com', 'footer'); + $this->assertTrue($result); + $js_urls = elgg_get_loaded_js('footer'); + $this->assertIdentical(array('http://test1.com'), $js_urls); + } + + /** + * Test elgg_get_loaded_js() */ public function testElggGetJS() { global $CONFIG; @@ -162,14 +176,15 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest { $urls = array('id1' => "$base/urla", 'id2' => "$base/urlb", 'id3' => "$base/urlc"); foreach ($urls as $id => $url) { elgg_register_js($id, $url); + elgg_load_js($id); } - $js_urls = elgg_get_js('head'); + $js_urls = elgg_get_loaded_js('head'); $this->assertIdentical($js_urls[0], $urls['id1']); $this->assertIdentical($js_urls[1], $urls['id2']); $this->assertIdentical($js_urls[2], $urls['id3']); - $js_urls = elgg_get_js('footer'); + $js_urls = elgg_get_loaded_js('footer'); $this->assertIdentical(array(), $js_urls); } } diff --git a/mod/blog/lib/blog.php b/mod/blog/lib/blog.php index 94ab65da1..df307d70e 100644 --- a/mod/blog/lib/blog.php +++ b/mod/blog/lib/blog.php @@ -298,8 +298,7 @@ function blog_get_page_content_edit($page, $guid = 0, $revision = NULL) { elgg_push_breadcrumb($blog->title, $blog->getURL()); elgg_push_breadcrumb(elgg_echo('edit')); - $blog_js = elgg_get_simplecache_url('js', 'blog/save_draft'); - elgg_register_js('elgg.blog', $blog_js); + elgg_load_js('elgg.blog'); $content = elgg_view_form('blog/save', $vars, $body_vars); $sidebar = elgg_view('blog/sidebar/revisions', $vars); diff --git a/mod/blog/start.php b/mod/blog/start.php index 01d78b4dd..8ebb967cc 100644 --- a/mod/blog/start.php +++ b/mod/blog/start.php @@ -30,6 +30,10 @@ function blog_init() { // add to the main css elgg_extend_view('css/screen', 'blog/css'); + // register the blog's JavaScript + $blog_js = elgg_get_simplecache_url('js', 'blog/save_draft'); + elgg_register_js('elgg.blog', $blog_js); + // routing of urls elgg_register_page_handler('blog', 'blog_page_handler'); diff --git a/mod/developers/views/default/page/theme_preview.php b/mod/developers/views/default/page/theme_preview.php index 0fe7e4732..97df551ae 100644 --- a/mod/developers/views/default/page/theme_preview.php +++ b/mod/developers/views/default/page/theme_preview.php @@ -32,7 +32,7 @@ header("Content-type: text/html; charset=UTF-8"); <![endif]--> <?php -foreach (elgg_get_js() as $script) { +foreach (elgg_get_loaded_js() as $script) { ?> <script type="text/javascript" src="<?php echo $script; ?>"></script> <?php diff --git a/mod/notifications/index.php b/mod/notifications/index.php index 30ee1b74b..49c91fe0e 100644 --- a/mod/notifications/index.php +++ b/mod/notifications/index.php @@ -14,7 +14,8 @@ gatekeeper(); set_page_owner(elgg_get_logged_in_user_guid()); $js_url = elgg_get_simplecache_url('js', 'friendsPickerv1'); -elgg_register_js('friendsPicker', $js_url); +elgg_register_js('friendsPickerv1', $js_url); +elgg_load_js('friendsPickerv1'); // 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 fac8c083c..6731c59e3 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('elgg.friendspicker', 'js/lib/friends_picker.js'); -elgg_register_js('jquery.easing', 'vendors/jquery/jquery.easing.1.3.packed.js'); +elgg_load_js('elgg.friendspicker'); +elgg_load_js('jquery.easing'); ?> <div class="elgg-module elgg-module-info"> diff --git a/mod/pages/start.php b/mod/pages/start.php index 23c564da5..3404a8cd2 100644 --- a/mod/pages/start.php +++ b/mod/pages/start.php @@ -33,9 +33,15 @@ function pages_init() { elgg_register_action("pages/editwelcome", "$action_base/editwelcome.php"); elgg_register_action("pages/delete", "$action_base/delete.php"); - // Extend some views + // Extend the main css view elgg_extend_view('css/elgg', 'pages/css'); + // Register javascript needed for sidebar menu + $js_url = 'mod/pages/vendors/jquery-treeview/jquery.treeview.min.js'; + elgg_register_js('jquery-treeview', $js_url); + $css_url = 'mod/pages/vendors/jquery-treeview/jquery.treeview.css'; + elgg_register_css('jquery-treeview', $css_url); + // Register entity type for search elgg_register_entity_type('object', 'page'); elgg_register_entity_type('object', 'page_top'); @@ -100,10 +106,8 @@ function pages_page_handler($page) { elgg_load_library('elgg:pages'); // 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('jquery-treeview', $js_url); - $css_url = elgg_get_site_url() . 'mod/pages/vendors/jquery-treeview/jquery.treeview.css'; - elgg_register_css('jquery-treeview', $css_url); + elgg_load_js('jquery-treeview'); + elgg_load_css('jquery-treeview'); if (!isset($page[0])) { $page[0] = 'all'; diff --git a/mod/tinymce/start.php b/mod/tinymce/start.php index 81667f8d5..1b641ff86 100644 --- a/mod/tinymce/start.php +++ b/mod/tinymce/start.php @@ -8,6 +8,9 @@ function tinymce_init() { elgg_extend_view('css/elgg', 'tinymce/css'); elgg_extend_view('css/admin', 'tinymce/css'); + + 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')); elgg_extend_view('input/longtext', 'tinymce/init'); diff --git a/mod/tinymce/views/default/tinymce/init.php b/mod/tinymce/views/default/tinymce/init.php index 3fe08771c..35dd71613 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('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 +elgg_load_js('tinymce'); +elgg_load_js('elgg.tinymce');
\ No newline at end of file diff --git a/mod/zaudio/start.php b/mod/zaudio/start.php index c440456b7..b353dffb9 100644 --- a/mod/zaudio/start.php +++ b/mod/zaudio/start.php @@ -12,4 +12,7 @@ elgg_register_event_handler('init', 'system', 'zaudio_init'); function zaudio_init() { elgg_extend_view('css/elgg', 'zaudio/css'); + + $js_url = elgg_get_site_url() . 'mod/zaudio/audioplayer/audio-player.js'; + elgg_register_js('elgg.zaudio', $js_url); } diff --git a/mod/zaudio/views/default/zaudio/audioplayer.php b/mod/zaudio/views/default/zaudio/audioplayer.php index 6898506a6..bae725458 100644 --- a/mod/zaudio/views/default/zaudio/audioplayer.php +++ b/mod/zaudio/views/default/zaudio/audioplayer.php @@ -4,8 +4,7 @@ * @package ElggZAudio */ -$js_url = elgg_get_site_url() . 'mod/zaudio/audioplayer/audio-player.js'; -elgg_register_js('elgg.zaudio', $js_url); +elgg_load_js('elgg.zaudio'); $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/input/autocomplete.php b/views/default/input/autocomplete.php index 212333141..b16d08fc3 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('elgg.autocomplete', 'js/lib/autocomplete.js'); +elgg_load_js('elgg.autocomplete'); ?> diff --git a/views/default/input/friendspicker.php b/views/default/input/friendspicker.php index b5f347e0c..1a60e0d88 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('elgg.friendspicker', 'js/lib/friends_picker.js'); -elgg_register_js('jquery.easing', 'vendors/jquery/jquery.easing.1.3.packed.js'); +elgg_load_js('elgg.friendspicker'); +elgg_load_js('jquery.easing'); $chararray = elgg_echo('friendspicker:chararray'); diff --git a/views/default/input/userpicker.php b/views/default/input/userpicker.php index 8280305b0..656effc98 100644 --- a/views/default/input/userpicker.php +++ b/views/default/input/userpicker.php @@ -18,7 +18,7 @@ * */ -elgg_register_js('elgg.userpicker', 'js/lib/userpicker.js'); +elgg_load_js('elgg.userpicker'); function user_picker_add_user($user_id) { $user = get_entity($user_id); diff --git a/views/default/page/elements/head.php b/views/default/page/elements/head.php index 209eb35f4..5f9bf4798 100644 --- a/views/default/page/elements/head.php +++ b/views/default/page/elements/head.php @@ -30,8 +30,8 @@ END; $feedref = ""; } -$js = elgg_get_js('head'); -$css = elgg_get_css(); +$js = elgg_get_loaded_js('head'); +$css = elgg_get_loaded_css(); $version = get_version(); $release = get_version(true); |