Преглед изворни кода

#2207 Load css/js dynamically when page is loaded dynamically

Alex Trofimov пре 7 година
родитељ
комит
09bffdd3a6

+ 16 - 0
inc/classes/BxDolTemplate.php

@@ -1653,6 +1653,14 @@ class BxDolTemplate extends BxDolFactory implements iBxDolSingleton
         return $this->_processFiles('js', 'add', $mixedFiles, $bDynamic);
         return $this->_processFiles('js', 'add', $mixedFiles, $bDynamic);
     }
     }
 
 
+    /**
+     * get added js files
+     */ 
+    function getJs()
+    {
+        return $this->aPage['js_compiled'];
+    }
+    
     /**
     /**
      * Add System JS file(s) to global output.
      * Add System JS file(s) to global output.
      * System JS files are the files which are attached to all pages. They will be cached separately from the others.
      * System JS files are the files which are attached to all pages. They will be cached separately from the others.
@@ -1784,6 +1792,14 @@ class BxDolTemplate extends BxDolFactory implements iBxDolSingleton
         return $this->_processFiles('css', 'add', $mixedFiles, $bDynamic);
         return $this->_processFiles('css', 'add', $mixedFiles, $bDynamic);
     }
     }
 
 
+    /**
+     * get added css files
+     */ 
+    function getCss()
+    {
+        return $this->aPage['css_compiled'];
+    }
+
     /**
     /**
      * Add additional heavy css file (not very necessary) to load asynchronously for desktop browsers only
      * Add additional heavy css file (not very necessary) to load asynchronously for desktop browsers only
      * @param  mixed          $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
      * @param  mixed          $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.

+ 1 - 1
modules/boonex/photos/classes/BxPhotosModule.php

@@ -21,7 +21,7 @@ class BxPhotosModule extends BxBaseModTextModule
 
 
     public function actionViewEntryBrief()
     public function actionViewEntryBrief()
     {
     {
-        echo BxDolPage::getObjectInstance($this->_oConfig->CNF['OBJECT_PAGE_VIEW_ENTRY_BRIEF'], $this->_oTemplate)->getCode();
+        echo BxDolPage::getObjectInstance($this->_oConfig->CNF['OBJECT_PAGE_VIEW_ENTRY_BRIEF'], $this->_oTemplate)->getCodeDynamic();
     }
     }
 
 
     /**
     /**

+ 1 - 1
modules/boonex/timeline/classes/BxTimelineModule.php

@@ -381,7 +381,7 @@ class BxTimelineModule extends BxBaseModNotificationsModule implements iBxDolCon
 
 
     public function actionGetItemBrief()
     public function actionGetItemBrief()
     {
     {
-        echo BxDolPage::getObjectInstance($this->_oConfig->getObject('page_item_brief'), $this->_oTemplate)->getCode();
+        echo BxDolPage::getObjectInstance($this->_oConfig->getObject('page_item_brief'), $this->_oTemplate)->getCodeDynamic();
     }
     }
 
 
     public function actionResumeLiveUpdate()
     public function actionResumeLiveUpdate()

+ 36 - 0
template/scripts/BxBasePage.php

@@ -32,6 +32,42 @@ class BxBasePage extends BxDolPage
 		$this->_sStorage = 'sys_images';
 		$this->_sStorage = 'sys_images';
     }
     }
 
 
+    /**
+     * Very similar to BxBasePage::getCode
+     * but adds css and js files which are needed for the corect page display
+     */ 
+    public function getCodeDynamic ()
+    {
+        $oTemplate = BxDolTemplate::getInstance();
+
+        // get js&css before the page code is generated
+        $aCssBefore = $oTemplate->getCss();
+        $aJsBefore = $oTemplate->getJs();
+
+        // generate page code
+        $sContent = $this->getCode();
+
+        // get js&css after the page code is generated
+        $aCssAfter = $oTemplate->getCss();
+        $aJsAfter = $oTemplate->getJs();
+
+        // compare files which were added before and after page code is generated
+        $f = function ($a1, $a2) {
+            return strcasecmp($a1['url'], $a2['url']);
+        };
+        $aCssNew = array_udiff($aCssAfter, $aCssBefore, $f);
+        $aJsNew = array_udiff($aJsAfter, $aJsBefore, $f);
+
+        // add newly added js&css files in static mode
+        $sCss = $sJs = '';
+        foreach ($aCssNew as $a)
+            $sCss .= $oTemplate->addCss($a['url'], true);
+        foreach ($aJsNew as $a)
+            $sJs .= $oTemplate->addJs($a['url'], true);
+
+        return $sJs . $sCss . $sContent;
+    }
+    
     /**
     /**
      * Get page code with automatic caching, adding necessary css/js files and system template vars.
      * Get page code with automatic caching, adding necessary css/js files and system template vars.
      * @return string.
      * @return string.