includes/template.php
changeset 1023 5a282dbf8fad
parent 1003 28e2f75d66fd
child 1043 ac1ecaacb48e
equal deleted inserted replaced
1021:5990ac411f34 1023:5a282dbf8fad
    19   var $theme_initted = false;
    19   var $theme_initted = false;
    20   var $page_initted = false;
    20   var $page_initted = false;
    21   var $elements = false;
    21   var $elements = false;
    22   var $page_id = false;
    22   var $page_id = false;
    23   var $namespace = false;
    23   var $namespace = false;
       
    24   var $js_preload = array();
    24   
    25   
    25   /**
    26   /**
    26    * Page action conditions
    27    * Page action conditions
    27    * @var array
    28    * @var array
    28    */
    29    */
   436     }
   437     }
   437     return true;
   438     return true;
   438   }
   439   }
   439   
   440   
   440   /**
   441   /**
       
   442    * Queue a Javascript file to be loaded with the page instead of on demand.
       
   443    * @param mixed Javascript file string or array thereof, extensions are optional
       
   444    * @example
       
   445    <code>
       
   446    $template->preload_js(array('jquery', 'jquery-ui'));
       
   447    $template->preload_js('admin-menu.js');
       
   448    </code>
       
   449    * @return null
       
   450    */
       
   451   
       
   452   function preload_js($filemixed)
       
   453   {
       
   454     if ( is_string($filemixed) )
       
   455       $files = array($filemixed);
       
   456     else if ( is_array($filemixed) )
       
   457       $files = $filemixed;
       
   458     else
       
   459       // :-/
       
   460       return null;
       
   461       
       
   462     $this->js_preload = array_values(array_merge($this->js_preload, $files));
       
   463   }
       
   464   
       
   465   /**
   441    * Global, only-called-once init. Goes to all themes.
   466    * Global, only-called-once init. Goes to all themes.
   442    */
   467    */
   443   
   468   
   444   function init_global_vars()
   469   function init_global_vars()
   445   {
   470   {
   514     // Set up javascript includes
   539     // Set up javascript includes
   515     // these depend heavily on whether we have a CDN to work with or not
   540     // these depend heavily on whether we have a CDN to work with or not
   516     if ( getConfig('cdn_path') )
   541     if ( getConfig('cdn_path') )
   517     {
   542     {
   518       // we're on a CDN, point to static includes
   543       // we're on a CDN, point to static includes
   519       // probably should have a way to compress stuff like this before uploading to CDN
   544       // CLI javascript compression script: includes/clientside/jscompress.php
   520       $js_head = '<script type="text/javascript" src="' . cdnPath . '/includes/clientside/static/enano-lib-basic.js"></script>';
   545       $js_head = '<script type="text/javascript" src="' . cdnPath . '/includes/clientside/static/enano-lib-basic.js"></script>';
       
   546       
       
   547       if ( !empty($this->js_preload) )
       
   548       {
       
   549         $loadlines = array();
       
   550         
       
   551         // make unique
       
   552         foreach ( $this->js_preload as &$script )
       
   553         {
       
   554           $script = preg_replace('/\.js$/', '', $script) . '.js';
       
   555         }
       
   556         $this->js_preload = array_unique($this->js_preload);
       
   557         
       
   558         foreach ( $this->js_preload as $script )
       
   559         {
       
   560           $js_head .= "\n    <script type=\"text/javascript\" src=\"" . cdnPath . "/includes/clientside/static/$script\"></script>";
       
   561           // special case for l10n: also load strings
       
   562           if ( $script == 'l10n.js' )
       
   563           {
       
   564             global $lang;
       
   565             $js_head .= "\n    <script type=\"text/javascript\" src=\"" . makeUrlNS("Special", "LangExportJSON/$lang->lang_id") . "\"></script>";
       
   566           }
       
   567           $loadlines[] = "loaded_components['$script'] = true;";
       
   568         }
       
   569         
       
   570         // tell the system that this stuff is already loaded
       
   571         $loadlines = implode("\n      ", $loadlines);
       
   572         $js_head .= "\n    <script type=\"text/javascript\">
       
   573       var loaded_components = loaded_components || {};
       
   574       $loadlines
       
   575     </script>";
       
   576       }
       
   577       
   521       $js_foot = <<<JSEOF
   578       $js_foot = <<<JSEOF
   522     <script type="text/javascript">
   579     <script type="text/javascript">
   523       // This initializes the Javascript runtime when the DOM is ready - not when the page is
   580       // This initializes the Javascript runtime when the DOM is ready - not when the page is
   524       // done loading, because enano-lib-basic still has to load some 15 other script files
   581       // done loading, because enano-lib-basic still has to load some 15 other script files
   525       // check for the init function - this is a KHTML fix
   582       // check for the init function - this is a KHTML fix
   534 JSEOF;
   591 JSEOF;
   535     }
   592     }
   536     else
   593     else
   537     {
   594     {
   538       $cdnpath = cdnPath;
   595       $cdnpath = cdnPath;
       
   596       $js_head = '';
       
   597       
   539       // point to jsres compressor
   598       // point to jsres compressor
   540       $js_head = <<<JSEOF
   599       $js_head .= <<<JSEOF
   541       <!-- Only load a basic set of functions for now. Let the rest of the API load when the page is finished. -->
   600       <!-- Only load a basic set of functions for now. Let the rest of the API load when the page is finished. -->
   542       <script type="text/javascript" src="$cdnpath/includes/clientside/jsres.php?early"></script>
   601       <script type="text/javascript" src="$cdnpath/includes/clientside/jsres.php?early"></script>
   543 JSEOF;
   602 JSEOF;
       
   603       }
   544       $js_foot = <<<JSEOF
   604       $js_foot = <<<JSEOF
   545     <!-- jsres.php is a wrapper script that compresses and caches single JS files to minimize requests -->
   605     <!-- jsres.php is a wrapper script that compresses and caches single JS files to minimize requests -->
   546     <script type="text/javascript" src="$cdnpath/includes/clientside/jsres.php"></script>
   606     <script type="text/javascript" src="$cdnpath/includes/clientside/jsres.php"></script>
   547     <script type="text/javascript">//<![CDATA[
   607     <script type="text/javascript">//<![CDATA[
   548       // This initializes the Javascript runtime when the DOM is ready - not when the page is
   608       // This initializes the Javascript runtime when the DOM is ready - not when the page is
   555         enano_init();
   615         enano_init();
   556         window.onload = function(e) {  };
   616         window.onload = function(e) {  };
   557       }
   617       }
   558     //]]></script>
   618     //]]></script>
   559 JSEOF;
   619 JSEOF;
       
   620       
       
   621       if ( !empty($this->js_preload) )
       
   622       {
       
   623         foreach ( $this->js_preload as &$script )
       
   624         {
       
   625           $script = preg_replace('/\.js$/', '', $script) . '.js';
       
   626         }
       
   627         $this->js_preload = array_unique($this->js_preload);
       
   628         if ( in_array('l10n.js', $this->js_preload) )
       
   629         {
       
   630           // special case for l10n: also load strings
       
   631           global $lang;
       
   632           $js_foot .= "\n    <script type=\"text/javascript\" src=\"" . makeUrlNS("Special", "LangExportJSON/$lang->lang_id") . "\"></script>";
       
   633         }
       
   634         $scripts = implode(',', $this->js_preload);
       
   635         $js_foot .= "\n    <script type=\"text/javascript\" src=\"" . cdnPath . "/includes/clientside/jsres.php?f=$scripts\"></script>";
   560     }
   636     }
   561     
   637     
   562     $this->assign_bool(array(
   638     $this->assign_bool(array(
   563         'fixed_menus' => false,
   639         'fixed_menus' => false,
   564         'export' => false,
   640         'export' => false,