Added support for preloading javascript libs ($template->preload_js()). Updated admin theme and Tigra Tree Menu to support this + JS_{HEADER,FOOTER} variables.
authorDan
Mon, 15 Jun 2009 20:09:58 -0400
changeset 1023 5a282dbf8fad
parent 1021 5990ac411f34
child 1024 722f4beeceb6
Added support for preloading javascript libs ($template->preload_js()). Updated admin theme and Tigra Tree Menu to support this + JS_{HEADER,FOOTER} variables.
includes/clientside/static/admin-menu.js
includes/clientside/static/enano-lib-basic.js
includes/template.php
plugins/SpecialAdmin.php
themes/admin/footer.tpl
themes/admin/header.tpl
--- a/includes/clientside/static/admin-menu.js	Fri Jun 05 22:09:42 2009 -0400
+++ b/includes/clientside/static/admin-menu.js	Mon Jun 15 20:09:58 2009 -0400
@@ -40,7 +40,7 @@
   var ck = 0;
 }
 
-function tree (a_items, a_template) {
+function tree (a_items, a_template, s_target) {
 
 	this.a_tpl      = a_template;
 	this.a_config   = a_items;
@@ -77,8 +77,12 @@
 	this.n_id = trees.length;
 	trees[this.n_id] = this;
 	
-	for (var i = 0; i < this.a_children.length; i++) {
-		document.write(this.a_children[i].init());
+	for (var i = 0; i < this.a_children.length; i++)
+  {
+    if ( s_target )
+      document.getElementById(s_target).innerHTML += this.a_children[i].init();
+    else
+      document.write(this.a_children[i].init());
 		this.a_children[i].open(false, true);
 	}
 }
--- a/includes/clientside/static/enano-lib-basic.js	Fri Jun 05 22:09:42 2009 -0400
+++ b/includes/clientside/static/enano-lib-basic.js	Mon Jun 15 20:09:58 2009 -0400
@@ -210,6 +210,7 @@
       _f(e);
     }
   }
+  onload_hooks = null;
 }
 
 var enano_hooks = {};
--- 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
+   <code>
+   $template->preload_js(array('jquery', 'jquery-ui'));
+   $template->preload_js('admin-menu.js');
+   </code>
+   * @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 = '<script type="text/javascript" src="' . cdnPath . '/includes/clientside/static/enano-lib-basic.js"></script>';
+      
+      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    <script type=\"text/javascript\" src=\"" . cdnPath . "/includes/clientside/static/$script\"></script>";
+          // special case for l10n: also load strings
+          if ( $script == 'l10n.js' )
+          {
+            global $lang;
+            $js_head .= "\n    <script type=\"text/javascript\" src=\"" . makeUrlNS("Special", "LangExportJSON/$lang->lang_id") . "\"></script>";
+          }
+          $loadlines[] = "loaded_components['$script'] = true;";
+        }
+        
+        // tell the system that this stuff is already loaded
+        $loadlines = implode("\n      ", $loadlines);
+        $js_head .= "\n    <script type=\"text/javascript\">
+      var loaded_components = loaded_components || {};
+      $loadlines
+    </script>";
+      }
+      
       $js_foot = <<<JSEOF
     <script type="text/javascript">
       // 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_head .= <<<JSEOF
       <!-- Only load a basic set of functions for now. Let the rest of the API load when the page is finished. -->
       <script type="text/javascript" src="$cdnpath/includes/clientside/jsres.php?early"></script>
 JSEOF;
+      }
       $js_foot = <<<JSEOF
     <!-- jsres.php is a wrapper script that compresses and caches single JS files to minimize requests -->
     <script type="text/javascript" src="$cdnpath/includes/clientside/jsres.php"></script>
@@ -557,6 +617,22 @@
       }
     //]]></script>
 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    <script type=\"text/javascript\" src=\"" . makeUrlNS("Special", "LangExportJSON/$lang->lang_id") . "\"></script>";
+        }
+        $scripts = implode(',', $this->js_preload);
+        $js_foot .= "\n    <script type=\"text/javascript\" src=\"" . cdnPath . "/includes/clientside/jsres.php?f=$scripts\"></script>";
     }
     
     $this->assign_bool(array(
--- a/plugins/SpecialAdmin.php	Fri Jun 05 22:09:42 2009 -0400
+++ b/plugins/SpecialAdmin.php	Mon Jun 15 20:09:58 2009 -0400
@@ -2051,20 +2051,26 @@
 {
   global $db, $session, $paths, $template, $plugins; // Common objects
   global $lang;
+  global $output;
   
-  if($session->auth_level < USER_LEVEL_ADMIN) {
+  if ( $session->auth_level < USER_LEVEL_ADMIN )
+  {
     redirect(makeUrlNS('Special', 'Login/'.$paths->page, 'level='.USER_LEVEL_ADMIN), 'Not authorized', 'You need an authorization level of '.USER_LEVEL_ADMIN.' to use this page, your auth level is: ' . $session->auth_level, 0);
     exit;
   }
   else
   {
     $template->set_theme('admin', 'default');
-    $template->add_header('<script type="text/javascript" src="' . cdnPath . '/includes/clientside/static/admin-menu.js"></script>');
+    $template->preload_js('fat');
+    $template->preload_js('ajax');
+    $template->preload_js('l10n');
+    $template->preload_js('jquery');
+    $template->preload_js('jquery-ui');
+    $template->preload_js('autofill');
+    $template->preload_js('admin-menu');
     
-    if( !isset( $_GET['noheaders'] ) ) 
-    {
-      $template->header();
-    }
+    $output->header();
+    
     echo $lang->get('adm_page_tagline');
     ?>
     <script type="text/javascript">
@@ -2197,26 +2203,21 @@
       'icon_27' : '<?php echo cdnPath; ?>/images/icons/minus.gif'       // junction for last opended node
     };
     
-    addOnloadHook(function()
-      {
-        load_component(['fat', 'ajax', 'l10n', 'autofill', 'jquery', 'jquery-ui']);
-        keepalive_onload();
-      });
-    
     <?php
     echo $paths->parseAdminTree(); // Make a Javascript array that defines the tree
     ?>
+    
+    addOnloadHook(function()
+      {
+        console.trace();
+        new tree(TREE_ITEMS, TREE_TPL, 'admin_tree');
+        keepalive_onload();
+      });
     </script>
     <table border="0" width="100%">
       <tr>
         <td class="holder" valign="top">
-          <div class="pad" style="padding-right: 20px;">
-            <script type="text/javascript">
-              if ( !KILL_SWITCH )
-              {
-                new tree(TREE_ITEMS, TREE_TPL);
-              }
-            </script>
+          <div class="pad" style="padding-right: 20px;" id="admin_tree">
           </div>
         </td>
         <td width="100%" valign="top">
@@ -2265,10 +2266,7 @@
     </table>
   
     <?php
-  }
-  if(!isset($_GET['noheaders']))
-  {
-    $template->footer();
+    $output->footer();
   }
 }
 
--- a/themes/admin/footer.tpl	Fri Jun 05 22:09:42 2009 -0400
+++ b/themes/admin/footer.tpl	Mon Jun 15 20:09:58 2009 -0400
@@ -38,6 +38,7 @@
     </tr>
     </table>
     
+    {JS_FOOTER}
     <script type="text/javascript" src="{CDNPATH}/themes/admin/js/menu.js"></script>
   </body>
 </html>
--- a/themes/admin/header.tpl	Fri Jun 05 22:09:42 2009 -0400
+++ b/themes/admin/header.tpl	Mon Jun 15 20:09:58 2009 -0400
@@ -9,7 +9,7 @@
     <link id="mdgCss" rel="stylesheet" type="text/css" href="{CDNPATH}/themes/{THEME_ID}/css-ie/iefixes.css" />
     <![endif]-->
     {JS_DYNAMIC_VARS}
-    <script type="text/javascript" src="{SCRIPTPATH}/includes/clientside/jsres.php"></script>
+    {JS_HEADER}
     {ADDITIONAL_HEADERS}
     </head>
   <body>