From 5285471b7c6f2b4ef7dc02fb2a9c231e2216eef7 Mon Sep 17 00:00:00 2001 From: Brett Profitt Date: Tue, 23 Aug 2011 15:46:22 -0700 Subject: Fixes #3355. Added ElggPriorityList. Adapted the externals system to use it. --- engine/lib/elgglib.php | 139 +++++++++++++++++-------------------------------- engine/lib/views.php | 29 ++++------- 2 files changed, 59 insertions(+), 109 deletions(-) (limited to 'engine/lib') diff --git a/engine/lib/elgglib.php b/engine/lib/elgglib.php index b6b603e79..198ffe60c 100644 --- a/engine/lib/elgglib.php +++ b/engine/lib/elgglib.php @@ -167,7 +167,7 @@ function forward($location = "", $reason = 'system') { * @param string $name An identifier for the JavaScript library * @param string $url URL of the JavaScript file * @param string $location Page location: head or footer. (default: head) - * @param int $priority Priority of the CSS file (lower numbers load earlier) + * @param int $priority Priority of the JS file (lower numbers load earlier) * * @return bool * @since 1.8.0 @@ -288,28 +288,24 @@ 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 (!$CONFIG->externals[$type] instanceof ElggPriorityList) { - $CONFIG->externals[$type] = new ElggPriorityList(); - } + elgg_bootstrap_externals_data_structure($type); $name = trim(strtolower($name)); $priority = max((int)$priority, 0); + $item = elgg_extract($name, $CONFIG->externals_map[$type]); - $index = elgg_get_external_file_priority($name, $type); - - if ($index !== false) { + if ($item) { // updating a registered item - $item = $CONFIG->externals[$type][$index]; + // don't update loaded because it could already be set $item->url = $url; $item->location = $location; - // remove old saved priority - elgg_remove_external_file_priority($name, $type); - $priority = $CONFIG->externals[$type]->move($index, $priority); + // if loaded before registered, that means it hasn't been added to the list yet + if ($CONFIG->externals[$type]->contains($item)) { + $priority = $CONFIG->externals[$type]->move($item, $priority); + } else { + $priority = $CONFIG->externals[$type]->add($item, $priority); + } } else { $item = new stdClass(); $item->loaded = false; @@ -319,10 +315,9 @@ function elgg_register_external_file($type, $name, $url, $location, $priority = $priority = $CONFIG->externals[$type]->add($item, $priority); } - // save priority map so we can update if added again - elgg_save_external_file_priority($priority, $name, $type); + $CONFIG->externals_map[$type][$name] = $item; - return true; + return $priority !== false; } /** @@ -337,22 +332,14 @@ function elgg_register_external_file($type, $name, $url, $location, $priority = function elgg_unregister_external_file($type, $name) { global $CONFIG; - if (!isset($CONFIG->externals)) { - return false; - } - - if (!$CONFIG->externals[$type] instanceof ElggPriorityList) { - return false; - } + elgg_bootstrap_externals_data_structure($type); $name = trim(strtolower($name)); - - $priority = elgg_get_external_file_priority($name, $type); + $item = elgg_extract($name, $CONFIG->externals_map[$type]); - if ($priority !== false) { - elgg_remove_external_file_priority($name, $type); - unset($CONFIG->externals[$type][$priority]); - return true; + if ($item) { + unset($CONFIG->externals_map[$type][$name]); + return $CONFIG->externals[$type]->remove($item); } return false; @@ -370,21 +357,15 @@ function elgg_unregister_external_file($type, $name) { function elgg_load_external_file($type, $name) { global $CONFIG; - if (!isset($CONFIG->externals)) { - $CONFIG->externals = array(); - } - - if (!$CONFIG->externals[$type] instanceof ElggPriorityList) { - $CONFIG->externals[$type] = new ElggPriorityList(); - } + elgg_bootstrap_externals_data_structure($type); $name = trim(strtolower($name)); - $priority = elgg_get_external_file_priority($name, $type); + $item = elgg_extract($name, $CONFIG->externals_map[$type]); - if ($priority !== false) { + if ($item) { // update a registered item - $CONFIG->externals[$type][$priority]->loaded = true; + $item->loaded = true; } else { $item = new stdClass(); $item->loaded = true; @@ -392,59 +373,10 @@ function elgg_load_external_file($type, $name) { $item->location = ''; $priority = $CONFIG->externals[$type]->add($item); - elgg_save_external_file_priority($priority, $name, $type); + $CONFIG->externals_map[$type][$name] = $item; } } -/** - * Gets the priority of an external by name and type. - * - * @param type $name - * @param type $type - * @return type - */ -function elgg_get_external_file_priority($name, $type) { - global $CONFIG; - - if (!isset($CONFIG->externals_priorities[$type][$name])) { - return false; - } - - return $CONFIG->externals_priorities[$type][$name]; -} - -function elgg_save_external_file_priority($priority, $name, $type) { - global $CONFIG; - - if (!isset($CONFIG->externals_priorities)) { - $CONFIG->externals_priorities = array(); - } - - if (!isset($CONFIG->externals_priorities[$type])) { - $CONFIG->externals_priorities[$type] = array(); - } - - $CONFIG->externals_priorities[$type][$name] = $priority; - - return true; -} - -function elgg_remove_external_file_priority($name, $type) { - global $CONFIG; - - if (!isset($CONFIG->externals_priorities)) { - $CONFIG->externals_priorities = array(); - } - - if (!isset($CONFIG->externals_priorities[$type])) { - $CONFIG->externals_priorities[$type] = array(); - } - - unset($CONFIG->externals_priorities[$type][$name]); - - return true; -} - /** * Get external resource descriptors * @@ -470,6 +402,31 @@ function elgg_get_loaded_external_files($type, $location) { return array(); } +/** + * Bootstraps the externals data structure in $CONFIG. + * + * @param string $type The type of external, js or css. + */ +function elgg_bootstrap_externals_data_structure($type) { + global $CONFIG; + + if (!isset($CONFIG->externals)) { + $CONFIG->externals = array(); + } + + if (!$CONFIG->externals[$type] instanceof ElggPriorityList) { + $CONFIG->externals[$type] = new ElggPriorityList(); + } + + if (!isset($CONFIG->externals_map)) { + $CONFIG->externals_map = array(); + } + + if (!isset($CONFIG->externals_map[$type])) { + $CONFIG->externals_map[$type] = array(); + } +} + /** * Returns a list of files in $directory. * diff --git a/engine/lib/views.php b/engine/lib/views.php index 7686a8bef..fe3265347 100644 --- a/engine/lib/views.php +++ b/engine/lib/views.php @@ -1479,21 +1479,6 @@ function autoregister_views($view_base, $folder, $base_location_path, $viewtype) return FALSE; } -/** - * Add the core Elgg head elements that could be cached - * - * @return void - */ -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'); -} - /** * Add the rss link to the extras when if needed * @@ -1548,12 +1533,17 @@ function elgg_views_boot() { elgg_register_simplecache_view('css/ie6'); elgg_register_simplecache_view('js/elgg'); - elgg_register_js('jquery', '/vendors/jquery/jquery-1.6.2.min.js', 'head', 1); - elgg_register_js('jquery-ui', '/vendors/jquery/jquery-ui-1.8.16.min.js', 'head', 2); + elgg_register_js('jquery', '/vendors/jquery/jquery-1.6.2.min.js', 'head'); + elgg_register_js('jquery-ui', '/vendors/jquery/jquery-ui-1.8.16.min.js', 'head'); elgg_register_js('jquery.form', '/vendors/jquery/jquery.form.js'); + + $elgg_js_url = elgg_get_simplecache_url('js', 'elgg'); + elgg_register_js('elgg', $elgg_js_url, 'head'); + elgg_load_js('jquery'); elgg_load_js('jquery-ui'); elgg_load_js('jquery.form'); + elgg_load_js('elgg'); elgg_register_simplecache_view('js/lightbox'); $lightbox_js_url = elgg_get_simplecache_url('js', 'lightbox'); @@ -1561,7 +1551,10 @@ function elgg_views_boot() { $lightbox_css_url = 'vendors/jquery/fancybox/jquery.fancybox-1.3.4.css'; elgg_register_css('lightbox', $lightbox_css_url); - elgg_register_event_handler('ready', 'system', 'elgg_views_register_core_head_elements'); + $elgg_css_url = elgg_get_simplecache_url('css', 'elgg'); + elgg_register_css('elgg', $elgg_css_url, 1); + elgg_load_css('elgg'); + elgg_register_event_handler('pagesetup', 'system', 'elgg_views_add_rss_link'); // discover the built-in view types -- cgit v1.2.3