diff -r 5990ac411f34 -r 5a282dbf8fad includes/template.php --- a/includes/template.php Fri Jun 05 22:09:42 2009 -0400 +++ b/includes/template.php Mon Jun 15 20:09:58 2009 -0400 @@ -21,6 +21,7 @@ var $elements = false; var $page_id = false; var $namespace = false; + var $js_preload = array(); /** * Page action conditions @@ -438,6 +439,30 @@ } /** + * Queue a Javascript file to be loaded with the page instead of on demand. + * @param mixed Javascript file string or array thereof, extensions are optional + * @example + + $template->preload_js(array('jquery', 'jquery-ui')); + $template->preload_js('admin-menu.js'); + + * @return null + */ + + function preload_js($filemixed) + { + if ( is_string($filemixed) ) + $files = array($filemixed); + else if ( is_array($filemixed) ) + $files = $filemixed; + else + // :-/ + return null; + + $this->js_preload = array_values(array_merge($this->js_preload, $files)); + } + + /** * Global, only-called-once init. Goes to all themes. */ @@ -516,8 +541,40 @@ if ( getConfig('cdn_path') ) { // we're on a CDN, point to static includes - // probably should have a way to compress stuff like this before uploading to CDN + // CLI javascript compression script: includes/clientside/jscompress.php $js_head = ''; + + if ( !empty($this->js_preload) ) + { + $loadlines = array(); + + // make unique + foreach ( $this->js_preload as &$script ) + { + $script = preg_replace('/\.js$/', '', $script) . '.js'; + } + $this->js_preload = array_unique($this->js_preload); + + foreach ( $this->js_preload as $script ) + { + $js_head .= "\n "; + // special case for l10n: also load strings + if ( $script == 'l10n.js' ) + { + global $lang; + $js_head .= "\n "; + } + $loadlines[] = "loaded_components['$script'] = true;"; + } + + // tell the system that this stuff is already loaded + $loadlines = implode("\n ", $loadlines); + $js_head .= "\n "; + } + $js_foot = << // This initializes the Javascript runtime when the DOM is ready - not when the page is @@ -536,11 +593,14 @@ else { $cdnpath = cdnPath; + $js_head = ''; + // point to jsres compressor - $js_head = << JSEOF; + } $js_foot = << @@ -557,6 +617,22 @@ } //]]> JSEOF; + + if ( !empty($this->js_preload) ) + { + foreach ( $this->js_preload as &$script ) + { + $script = preg_replace('/\.js$/', '', $script) . '.js'; + } + $this->js_preload = array_unique($this->js_preload); + if ( in_array('l10n.js', $this->js_preload) ) + { + // special case for l10n: also load strings + global $lang; + $js_foot .= "\n "; + } + $scripts = implode(',', $this->js_preload); + $js_foot .= "\n "; } $this->assign_bool(array(