Răsfoiți Sursa

Ticket #192 - Votes: Dynamic JS/CSS loading.

AntonLV 11 ani în urmă
părinte
comite
dac20bed24

+ 9 - 0
inc/classes/BxDolMenu.php

@@ -86,6 +86,8 @@ class BxDolMenu extends BxDol implements iBxDolFactoryObject, iBxDolReplaceable
     protected static $SEL_MODULE = '';
     protected static $SEL_NAME = '';
 
+    protected $_bDynamicMode;
+
     protected $_sSelModule;
     protected $_sSelName;
 
@@ -103,6 +105,8 @@ class BxDolMenu extends BxDol implements iBxDolFactoryObject, iBxDolReplaceable
     {
         parent::__construct();
 
+        $this->_bDynamicMode = false;
+
         $this->_sObject = isset($aObject['object']) ? $aObject['object'] : 'bx-menu-obj-' . time() . rand(0, PHP_INT_MAX);
         $this->_aObject = $aObject;
         $this->_oQuery = new BxDolMenuQuery($this->_aObject);
@@ -204,6 +208,11 @@ class BxDolMenu extends BxDol implements iBxDolFactoryObject, iBxDolReplaceable
         $this->_sSelName = $sName;
     }
 
+    public function setDynamicMode ($bDynamicMode)
+    {
+        $this->_bDynamicMode = $bDynamicMode;
+    }
+
     /**
      * Add replace markers.
      * @param $a array of markers as key => value

+ 20 - 0
inc/classes/BxDolTemplate.php

@@ -873,6 +873,26 @@ class BxDolTemplate extends BxDol implements iBxDolSingleton
     {
         return $this->_getAbsoluteLocation('path', $this->_sFolderCss, $sName, $sCheckIn);
     }
+    /**
+     * Get full URL of JS file.
+     *
+     * @param  string $sName    - JS file name.
+     * @return string full URL.
+     */
+    function getJsUrl($sName)
+    {
+        return $this->_getAbsoluteLocationJs('url', $sName);
+    }
+    /**
+     * Get full Path of JS file.
+     *
+     * @param  string $sName    - JS file name.
+     * @return string full URL.
+     */
+    function getJsPath($sName)
+    {
+        return $this->_getAbsoluteLocationJs('path', $sName);
+    }
 
     /**
      * Get menu.

+ 10 - 3
inc/classes/BxDolVote.php

@@ -86,13 +86,20 @@ define('BX_DOL_VOTE_USAGE_DEFAULT', BX_DOL_VOTE_USAGE_BLOCK);
 
 class BxDolVote extends BxDolObject
 {
-    public function __construct($sSystem, $iId, $iInit = true)
+	protected $_oTemplate;
+
+    public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
     {
         parent::__construct($sSystem, $iId, $iInit);
         if(empty($this->_sSystem))
             return;
 
         $this->_oQuery = new BxDolVoteQuery($this);
+
+        if ($oTemplate)
+            $this->_oTemplate = $oTemplate;
+        else
+            $this->_oTemplate = BxDolTemplate::getInstance();
     }
 
     /**
@@ -102,7 +109,7 @@ class BxDolVote extends BxDolObject
      * @param $iInit perform initialization
      * @return null on error, or ready to use class instance
      */
-    public static function getObjectInstance($sSys, $iId, $iInit = true)
+    public static function getObjectInstance($sSys, $iId, $iInit = true, $oTemplate = false)
     {
         if(isset($GLOBALS['bxDolClasses']['BxDolVote!' . $sSys . $iId]))
             return $GLOBALS['bxDolClasses']['BxDolVote!' . $sSys . $iId];
@@ -118,7 +125,7 @@ class BxDolVote extends BxDolObject
                 require_once(BX_DIRECTORY_PATH_ROOT . $aSystems[$sSys]['class_file']);
         }
 
-        $o = new $sClassName($sSys, $iId, $iInit);
+        $o = new $sClassName($sSys, $iId, $iInit, $oTemplate);
         return ($GLOBALS['bxDolClasses']['BxDolVote!' . $sSys . $iId] = $o);
     }
 

+ 4 - 0
inc/js/functions.js

@@ -646,4 +646,8 @@ function bx_activate_anim_icons(sColor)
     });
 }
 
+bx_get_style = function(sUrl) {
+	$("head").append($("<link rel='stylesheet' href='" + sUrl + "' type='text/css' />"));
+};
+
 /** @} */

+ 2 - 2
modules/boonex/air/data/template/system/scripts/BxTemplVote.php

@@ -12,9 +12,9 @@
  */
 class BxTemplVote extends BxBaseVote
 {
-    function __construct($sSystem, $iId, $iInit = 1)
+    function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
     {
-        parent::__construct($sSystem, $iId, $iInit);
+        parent::__construct($sSystem, $iId, $iInit, $oTemplate);
     }
 }
 

+ 2 - 2
modules/boonex/lagoon/data/template/system/scripts/BxTemplVote.php

@@ -12,9 +12,9 @@
  */
 class BxTemplVote extends BxBaseVote
 {
-    function __construct($sSystem, $iId, $iInit = 1)
+    function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
     {
-        parent::__construct($sSystem, $iId, $iInit);
+        parent::__construct($sSystem, $iId, $iInit, $oTemplate);
     }
 }
 

+ 2 - 2
modules/boonex/ocean/data/template/system/scripts/BxTemplVote.php

@@ -12,9 +12,9 @@
  */
 class BxTemplVote extends BxBaseVote
 {
-    function __construct($sSystem, $iId, $iInit = 1)
+    function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
     {
-        parent::__construct($sSystem, $iId, $iInit);
+        parent::__construct($sSystem, $iId, $iInit, $oTemplate);
     }
 }
 

+ 33 - 16
template/scripts/BxBaseVote.php

@@ -12,6 +12,8 @@
  */
 class BxBaseVote extends BxDolVote
 {
+	protected $_bCssJsAdded;
+
     protected $_sJsObjName;
     protected $_sStylePrefix;
 
@@ -23,9 +25,11 @@ class BxBaseVote extends BxDolVote
     protected $_sTmplNameDoVoteLikes;
     protected $_sTmplNameDoVoteStars;
 
-    public function __construct($sSystem, $iId, $iInit = 1)
+    public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
     {
-        parent::__construct($sSystem, $iId, $iInit);
+        parent::__construct($sSystem, $iId, $iInit, $oTemplate);
+
+        $this->_bCssJsAdded = false;
 
         $this->_sJsObjName = 'oVote' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
         $this->_sStylePrefix = 'bx-vote';
@@ -57,12 +61,15 @@ class BxBaseVote extends BxDolVote
         $this->_sTmplNameDoVoteStars = 'vote_do_vote_stars.html';
     }
 
-    public function addCssJs()
+    public function addCssJs($bDynamicMode = false)
     {
-        $oTemplate = BxDolTemplate::getInstance();
+    	if($bDynamicMode || $this->_bCssJsAdded)
+    		return;
+
+    	$this->_oTemplate->addJs(array('BxDolVote.js'));
+        $this->_oTemplate->addCss(array('vote.css'));
 
-        $oTemplate->addJs('BxDolVote.js');
-        $oTemplate->addCss('vote.css');
+        $this->_bCssJsAdded = true;
     }
 
     public function getJsObjectName()
@@ -70,7 +77,7 @@ class BxBaseVote extends BxDolVote
         return $this->_sJsObjName;
     }
 
-    public function getJsScript()
+    public function getJsScript($bDynamicMode = false)
     {
         $aParams = array(
             'sObjName' => $this->_sJsObjName,
@@ -82,9 +89,17 @@ class BxBaseVote extends BxDolVote
             'sStylePrefix' => $this->_sStylePrefix,
             'aHtmlIds' => $this->_aHtmlIds
         );
+        $sCode = "var " . $this->_sJsObjName . " = new BxDolVote(" . json_encode($aParams) . ");";
 
-        $this->addCssJs();
-        return BxDolTemplate::getInstance()->_wrapInTagJsCode("var " . $this->_sJsObjName . " = new BxDolVote(" . json_encode($aParams) . ");");
+        if($bDynamicMode) {
+			$sCode = "$.getScript('" . bx_js_string($this->_oTemplate->getJsUrl('BxDolVote.js'), BX_ESCAPE_STR_APOS) . "', function(data, textStatus, jqxhr) {
+				bx_get_style('" . bx_js_string($this->_oTemplate->getCssUrl('vote.css'), BX_ESCAPE_STR_APOS) . "');
+				" . $sCode . "
+        	}); ";
+        }
+
+        $this->addCssJs($bDynamicMode);
+        return $this->_oTemplate->_wrapInTagJsCode($sCode);
     }
 
     public function getJsClick()
@@ -101,7 +116,7 @@ class BxBaseVote extends BxDolVote
 
         $aVote = $this->_oQuery->getVote($this->getId());
 
-        return BxDolTemplate::getInstance()->parseHtmlByName($this->_sTmplNameCounter, array(
+        return $this->_oTemplate->parseHtmlByName($this->_sTmplNameCounter, array(
             'href' => 'javascript:void(0)',
             'title' => _t('_vote_do_like_by'),
             'bx_repeat:attrs' => array(
@@ -129,6 +144,8 @@ class BxBaseVote extends BxDolVote
 
     public function getElement($aParams = array())
     {
+    	$bDynamicMode = isset($aParams['dynamic_mode']) && $aParams['dynamic_mode'] === true;
+
         $bLike =  $this->isLikeMode();
         $sType = $bLike ? BX_DOL_VOTE_TYPE_LIKES : BX_DOL_VOTE_TYPE_STARS;
 
@@ -150,7 +167,7 @@ class BxBaseVote extends BxDolVote
         $aParams['is_voted'] = $this->_oQuery->isVoted($iObjectId, $iAuthorId) ? true : false;
 
         $sTmplName = 'vote_element_' . (!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_VOTE_USAGE_DEFAULT) . '.html';
-        return BxDolTemplate::getInstance()->parseHtmlByName($sTmplName, array(
+        return $this->_oTemplate->parseHtmlByName($sTmplName, array(
             'style_prefix' => $this->_sStylePrefix,
             'html_id' => $this->_aHtmlIds['main_' . $sType],
             'class' => $this->_sStylePrefix . '-' . $sType . ($bShowDoVoteAsButton ? '-button' : '') . ($bShowDoVoteAsButtonSmall ? '-button-small' : ''),
@@ -168,7 +185,7 @@ class BxBaseVote extends BxDolVote
                     'counter' => $this->getCounter()
                 )
             ),
-            'script' => $this->getJsScript()
+            'script' => $this->getJsScript($bDynamicMode)
         ));
     }
 
@@ -205,7 +222,7 @@ class BxBaseVote extends BxDolVote
             );
         }
 
-        return BxDolTemplate::getInstance()->parseHtmlByName($this->_sTmplNameDoVoteStars, array(
+        return $this->_oTemplate->parseHtmlByName($this->_sTmplNameDoVoteStars, array(
             'style_prefix' => $this->_sStylePrefix,
             'bx_repeat:stars' => $aTmplVarsStars,
             'bx_if:show_legend' => array(
@@ -236,7 +253,7 @@ class BxBaseVote extends BxDolVote
 		if($bDisabled)
 			$sClass .= $bShowDoVoteAsButton || $bShowDoVoteAsButtonSmall ? ' bx-btn-disabled' : 'bx-vote-disabled';
 
-        return BxDolTemplate::getInstance()->parseHtmlByName($this->_sTmplNameDoVoteLikes, array(
+        return $this->_oTemplate->parseHtmlByName($this->_sTmplNameDoVoteLikes, array(
             'style_prefix' => $this->_sStylePrefix,
             'class' => $sClass,
             'title' => _t($bVoted && $this->isUndo() ? '_vote_do_unlike' : '_vote_do_like'),
@@ -259,7 +276,7 @@ class BxBaseVote extends BxDolVote
     protected function _getLabelDoLike($aParams = array())
     {
     	$bVoted = isset($aParams['is_voted']) && $aParams['is_voted'] === true;
-        return BxDolTemplate::getInstance()->parseHtmlByName('vote_do_vote_likes_label.html', array(
+        return $this->_oTemplate->parseHtmlByName('vote_do_vote_likes_label.html', array(
         	'bx_if:show_icon' => array(
         		'condition' => isset($aParams['show_do_vote_icon']) && $aParams['show_do_vote_icon'] == true,
         		'content' => array(
@@ -291,7 +308,7 @@ class BxBaseVote extends BxDolVote
         if(empty($aTmplUsers))
             $aTmplUsers = MsgBox(_t('_Empty'));
 
-        return BxDolTemplate::getInstance()->parseHtmlByName('vote_by_list.html', array(
+        return $this->_oTemplate->parseHtmlByName('vote_by_list.html', array(
             'style_prefix' => $this->_sStylePrefix,
             'bx_repeat:list' => $aTmplUsers
         ));