Procházet zdrojové kódy

#1307 Forms: dynamic loading of form's datepicker

Alex Trofimov před 8 roky
rodič
revize
a64948f3da

+ 13 - 0
inc/js/jquery.webForms.js

@@ -9,7 +9,20 @@
 (function($) {
 (function($) {
 
 
     $.fn.addWebForms = function() {
     $.fn.addWebForms = function() {
+        if ('undefined' === typeof(glJsLoadOnaddWebForms) || !glJsLoadOnaddWebForms.length) {
+            $.fn.processWebForms.apply(this);
+        }
+        else {
+            var $this = this;
+            bx_get_scripts(glJsLoadOnaddWebForms, function () {
+                $.fn.processWebForms.apply($this);
+            });
+        }
 
 
+        return this;
+    }
+    
+    $.fn.processWebForms = function() {
         // switchers
         // switchers
         $('.bx-switcher-cont', this).each(function() {
         $('.bx-switcher-cont', this).each(function() {
             var eSwitcher = $(this);
             var eSwitcher = $(this);

+ 4 - 3
modules/base/general/classes/BxBaseModGeneralFormEntry.php

@@ -14,6 +14,8 @@
  */
  */
 class BxBaseModGeneralFormEntry extends BxTemplFormView
 class BxBaseModGeneralFormEntry extends BxTemplFormView
 {
 {
+    protected static $_isCssJsGeneralModuleAdded = false;
+        
     protected $MODULE;
     protected $MODULE;
 
 
     protected $_oModule;
     protected $_oModule;
@@ -255,11 +257,10 @@ class BxBaseModGeneralFormEntry extends BxTemplFormView
 
 
     function addCssJs ()
     function addCssJs ()
     {
     {
-        if (!isset($this->aParams['view_mode']) || !$this->aParams['view_mode']) {
-            if (self::$_isCssJsAdded)
-                return;
+        if ((!isset($this->aParams['view_mode']) || !$this->aParams['view_mode']) && !self::$_isCssJsGeneralModuleAdded) {
             $this->_oModule->_oTemplate->addCss('forms.css');
             $this->_oModule->_oTemplate->addCss('forms.css');
             $this->_oModule->_oTemplate->addJs('modules/base/general/js/|forms.js');
             $this->_oModule->_oTemplate->addJs('modules/base/general/js/|forms.js');
+            self::$_isCssJsGeneralModuleAdded = true;
         }
         }
 
 
         return parent::addCssJs ();
         return parent::addCssJs ();

+ 5 - 2
modules/boonex/events/classes/BxEventsFormEntry.php

@@ -14,6 +14,8 @@
  */
  */
 class BxEventsFormEntry extends BxBaseModGroupsFormEntry
 class BxEventsFormEntry extends BxBaseModGroupsFormEntry
 {
 {
+    protected static $_isCssJsEventsAdded = false;
+    
     protected $_iContentId = 0;
     protected $_iContentId = 0;
 
 
     public function __construct($aInfo, $oTemplate = false)
     public function __construct($aInfo, $oTemplate = false)
@@ -245,10 +247,11 @@ class BxEventsFormEntry extends BxBaseModGroupsFormEntry
     
     
     function addCssJs ()
     function addCssJs ()
     {
     {
-        if (!self::$_isCssJsAdded) {
+        if ((!isset($this->aParams['view_mode']) || !$this->aParams['view_mode']) && !self::$_isCssJsEventsAdded) {
             $this->oTemplate->addJs('intervals.js');
             $this->oTemplate->addJs('intervals.js');
             $this->oTemplate->addCss('informer.css');
             $this->oTemplate->addCss('informer.css');
-        }
+            self::$_isCssJsEventsAdded = true;
+        }   
 
 
         parent::addCssJs ();
         parent::addCssJs ();
     }
     }

+ 108 - 50
template/scripts/BxBaseFormView.php

@@ -12,6 +12,9 @@ class BxBaseFormView extends BxDolForm
     protected static $_isToggleJsAdded = false;
     protected static $_isToggleJsAdded = false;
 
 
     protected static $_isCssJsAdded = false;
     protected static $_isCssJsAdded = false;
+    protected static $_isCssJsUiAdded = false;
+    protected static $_isCssJsMinicolorsAdded = false;
+    protected static $_isCssJsTimepickerAdded = false;
     protected static $_isCssJsAddedViewMode = false;
     protected static $_isCssJsAddedViewMode = false;
 
 
     /**
     /**
@@ -132,10 +135,12 @@ class BxBaseFormView extends BxDolForm
             $bDynamicMode = true;
             $bDynamicMode = true;
 
 
         $this->_bDynamicMode = $bDynamicMode;
         $this->_bDynamicMode = $bDynamicMode;
-        $this->addCssJs ();
         $this->aFormAttrs = $this->_replaceMarkers($this->aFormAttrs);
         $this->aFormAttrs = $this->_replaceMarkers($this->aFormAttrs);
+        $this->sCode = $this->genForm();
+
+        $this->addCssJs ();
         $sDynamicCssJs = $this->_processCssJs();
         $sDynamicCssJs = $this->_processCssJs();
-        return $sDynamicCssJs . ($this->sCode = $this->genForm());
+        return $sDynamicCssJs . $this->sCode;
     }
     }
 
 
     /**
     /**
@@ -821,6 +826,23 @@ BLAH;
         if ('datetime' == $aAttrs['type'])
         if ('datetime' == $aAttrs['type'])
             $aAttrs['type'] = 'date_time';
             $aAttrs['type'] = 'date_time';
 
 
+        switch($aAttrs['type']) {
+            case 'date_time':
+                $this->addCssJsUi ();
+                $this->addCssJsTimepicker ();
+                break;
+            case 'slider':
+            case 'doublerange':
+            case 'datepicker':
+                $this->addCssJsUi ();
+                break;
+            case 'rgba':
+            case 'rgb':
+                $this->addCssJsMinicolors ();
+                break;
+        }
+
+
         if (isset($aInput['name'])) $aAttrs['name'] = $aInput['name'];
         if (isset($aInput['name'])) $aAttrs['name'] = $aInput['name'];
         if (isset($aInput['value'])) $aAttrs['value'] = $aInput['value'];
         if (isset($aInput['value'])) $aAttrs['value'] = $aInput['value'];
         if (isset($aInput['db']['pass']) && ('DateUtc' == $aInput['db']['pass'] || 'DateTimeUtc' == $aInput['db']['pass'])) $aAttrs['data-utc'] = 1;
         if (isset($aInput['db']['pass']) && ('DateUtc' == $aInput['db']['pass'] || 'DateTimeUtc' == $aInput['db']['pass'])) $aAttrs['data-utc'] = 1;
@@ -1047,9 +1069,7 @@ BLAH;
 
 
     protected function genCustomInputUsernamesSuggestions ($aInput)
     protected function genCustomInputUsernamesSuggestions ($aInput)
     {
     {
-        $this->_addJs(array(
-            'jquery.form.min.js',
-        ));
+        $this->addCssJsUi();
 
 
         $sVals = '';
         $sVals = '';
         if (!empty($aInput['value']) && is_array($aInput['value'])) {
         if (!empty($aInput['value']) && is_array($aInput['value'])) {
@@ -1438,54 +1458,86 @@ BLAH;
         }
         }
     }
     }
 
 
-    function addCssJs ()
+    function addCssJsTimepicker ()
     {
     {
-        if (isset($this->aParams['view_mode']) && $this->aParams['view_mode']) {
+        if (self::$_isCssJsTimepickerAdded)
+            return; 
 
 
-            if (self::$_isCssJsAddedViewMode)
-                return;
+        $aCalendarLangs = array ('af' => 1, 'am' => 1, 'bg' => 1, 'ca' => 1, 'cs' => 1, 'da' => 1, 'de' => 1, 'el' => 1, 'es' => 1, 'et' => 1, 'eu' => 1, 'fa' => 1, 'fi' => 1, 'fr' => 1, 'gl' => 1, 'he' => 1, 'hr' => 1, 'hu' => 1, 'id' => 1, 'it' => 1, 'ja' => 1, 'ko' => 1, 'lt' => 1, 'lv' => 1, 'mk' => 1, 'nl' => 1, 'no' => 1, 'pl' => 1, 'pt-BR' => 1, 'pt' => 1, 'ro' => 1, 'ru' => 1, 'sk' => 1, 'sl' => 1, 'sr-RS' => 1, 'sr-YU' => 1, 'sv' => 1, 'th' => 1, 'tr' => 1, 'uk' => 1, 'vi' => 1, 'zh-CN' => 1, 'zh-TW' => 1);
 
 
-            $this->_addCss('forms.css');
+        $sCalendarLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aCalendarLangs);            
+        $this->_addCss('timepicker-addon/jquery-ui-timepicker-addon.css');
+        
+        $this->_addJs(array(
+            'timepicker-addon/jquery-ui-timepicker-addon.min.js',
+            'timepicker-addon/jquery-ui-sliderAccess.js',
+            'timepicker-addon/i18n/jquery-ui-timepicker-' . $sCalendarLang . '.js',
+            'jquery.ui.touch-punch.min.js',
+        ), "'undefined' === typeof($.fn.datetimepicker)");
 
 
-            self::$_isCssJsAddedViewMode = true;
+        self::$_isCssJsTimepickerAdded = true;
+    }
+    
+    function addCssJsUi ()
+    {
+        if (self::$_isCssJsUiAdded)
+            return;
 
 
-        } else {
-                
-            if (self::$_isCssJsAdded)
-                return;
+        $aUiLangs = array ('af' => 1, 'ar-DZ' => 1, 'ar' => 1, 'az' => 1, 'be' => 1, 'bg' => 1, 'bs' => 1, 'ca' => 1, 'cs' => 1, 'cy-GB' => 1, 'da' => 1, 'de' => 1, 'el' => 1, 'en-AU' => 1, 'en-GB' => 1, 'en-NZ' => 1, 'en' => 1, 'eo' => 1, 'es' => 1, 'et' => 1, 'eu' => 1, 'fa' => 1, 'fi' => 1, 'fo' => 1, 'fr-CA' => 1, 'fr-CH' => 1, 'fr' => 1, 'gl' => 1, 'he' => 1, 'hi' => 1, 'hr' => 1, 'hu' => 1, 'hy' => 1, 'id' => 1, 'is' => 1, 'it' => 1, 'ja' => 1, 'ka' => 1, 'kk' => 1, 'km' => 1, 'ko' => 1, 'ky' => 1, 'lb' => 1, 'lt' => 1, 'lv' => 1, 'mk' => 1, 'ml' => 1, 'ms' => 1, 'nb' => 1, 'nl-BE' => 1, 'nl' => 1, 'nn' => 1, 'no' => 1, 'pl' => 1, 'pt-BR' => 1, 'pt' => 1, 'rm' => 1, 'ro' => 1, 'ru' => 1, 'sk' => 1, 'sl' => 1, 'sq' => 1, 'sr-SR' => 1, 'sr' => 1, 'sv' => 1, 'ta' => 1, 'th' => 1, 'tj' => 1, 'tr' => 1, 'uk' => 1, 'vi' => 1, 'zh-CN' => 1, 'zh-HK' => 1, 'zh-TW' => 1);
 
 
-            $aCss = array(
-                'forms.css',
+        $sUiLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aUiLangs);
+        
+        $this->_addCss('jquery-ui/jquery-ui.css');
+        $this->_addJs(array(
+            'jquery-ui/i18n/jquery.ui.datepicker-' . $sUiLang . '.min.js',
+            'jquery-ui/jquery-ui.custom.min.js',
+        ), "'undefined' === typeof($.widget)");
 
 
-                'jquery-ui/jquery-ui.css',
+        self::$_isCssJsUiAdded = true;
+    }
 
 
-                'timepicker-addon/jquery-ui-timepicker-addon.css',
+    function addCssJsMinicolors ()
+    {
+        if (self::$_isCssJsMinicolorsAdded)
+            return;       
 
 
-            	BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'jquery-minicolors/|jquery.minicolors.css',
-            );
+        $this->_addCss(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'jquery-minicolors/|jquery.minicolors.css');
+        $this->_addJs('jquery-minicolors/jquery.minicolors.min.js', "'undefined' === typeof($.minicolors)");
 
 
-            $aUiLangs = array ('af' => 1, 'ar-DZ' => 1, 'ar' => 1, 'az' => 1, 'be' => 1, 'bg' => 1, 'bs' => 1, 'ca' => 1, 'cs' => 1, 'cy-GB' => 1, 'da' => 1, 'de' => 1, 'el' => 1, 'en-AU' => 1, 'en-GB' => 1, 'en-NZ' => 1, 'en' => 1, 'eo' => 1, 'es' => 1, 'et' => 1, 'eu' => 1, 'fa' => 1, 'fi' => 1, 'fo' => 1, 'fr-CA' => 1, 'fr-CH' => 1, 'fr' => 1, 'gl' => 1, 'he' => 1, 'hi' => 1, 'hr' => 1, 'hu' => 1, 'hy' => 1, 'id' => 1, 'is' => 1, 'it' => 1, 'ja' => 1, 'ka' => 1, 'kk' => 1, 'km' => 1, 'ko' => 1, 'ky' => 1, 'lb' => 1, 'lt' => 1, 'lv' => 1, 'mk' => 1, 'ml' => 1, 'ms' => 1, 'nb' => 1, 'nl-BE' => 1, 'nl' => 1, 'nn' => 1, 'no' => 1, 'pl' => 1, 'pt-BR' => 1, 'pt' => 1, 'rm' => 1, 'ro' => 1, 'ru' => 1, 'sk' => 1, 'sl' => 1, 'sq' => 1, 'sr-SR' => 1, 'sr' => 1, 'sv' => 1, 'ta' => 1, 'th' => 1, 'tj' => 1, 'tr' => 1, 'uk' => 1, 'vi' => 1, 'zh-CN' => 1, 'zh-HK' => 1, 'zh-TW' => 1);
+        self::$_isCssJsMinicolorsAdded = true;
+    }
 
 
-            $aCalendarLangs = array ('af' => 1, 'am' => 1, 'bg' => 1, 'ca' => 1, 'cs' => 1, 'da' => 1, 'de' => 1, 'el' => 1, 'es' => 1, 'et' => 1, 'eu' => 1, 'fa' => 1, 'fi' => 1, 'fr' => 1, 'gl' => 1, 'he' => 1, 'hr' => 1, 'hu' => 1, 'id' => 1, 'it' => 1, 'ja' => 1, 'ko' => 1, 'lt' => 1, 'lv' => 1, 'mk' => 1, 'nl' => 1, 'no' => 1, 'pl' => 1, 'pt-BR' => 1, 'pt' => 1, 'ro' => 1, 'ru' => 1, 'sk' => 1, 'sl' => 1, 'sr-RS' => 1, 'sr-YU' => 1, 'sv' => 1, 'th' => 1, 'tr' => 1, 'uk' => 1, 'vi' => 1, 'zh-CN' => 1, 'zh-TW' => 1);
+    function addCssJsViewMode ()
+    {
+        if (self::$_isCssJsAddedViewMode)
+            return;
 
 
-            $sCalendarLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aCalendarLangs);
-            $sUiLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aUiLangs);
+        $this->_addCss('forms.css');
 
 
-            $aJs = array(
-                'jquery-ui/jquery-ui.custom.min.js',                
+        self::$_isCssJsAddedViewMode = true;
+    }
 
 
-                'jquery-ui/i18n/jquery.ui.datepicker-' . $sUiLang . '.min.js',
+    function addCssJsCore ()
+    {
+        if (self::$_isCssJsAdded)
+            return;
 
 
-            	'jquery-minicolors/jquery.minicolors.min.js',
+        $this->_addCss('forms.css');
+        $this->_addJs('jquery.webForms.js', "'undefined' === typeof($.fn.addWebForms)");
 
 
-                'timepicker-addon/jquery-ui-timepicker-addon.min.js',
-                'timepicker-addon/jquery-ui-sliderAccess.js',
-                'timepicker-addon/i18n/jquery-ui-timepicker-' . $sCalendarLang . '.js',
-                'jquery.ui.touch-punch.min.js',
+        self::$_isCssJsAdded = true;
+    }
 
 
-                'jquery.webForms.js',
-            );
+    function addCssJs ()
+    {
+        if (isset($this->aParams['view_mode']) && $this->aParams['view_mode']) {
+
+            $this->addCssJsViewMode ();
 
 
+        } else {
+
+            $this->addCssJsCore();
+                
             foreach ($this->aInputs as $aInput) {
             foreach ($this->aInputs as $aInput) {
                 if (!isset($aInput['type']) || 'files' != $aInput['type'] || !isset($aInput['uploaders']))
                 if (!isset($aInput['type']) || 'files' != $aInput['type'] || !isset($aInput['uploaders']))
                     continue;
                     continue;
@@ -1496,12 +1548,6 @@ BLAH;
                         $oUploader->addCssJs();
                         $oUploader->addCssJs();
                 }
                 }
             }
             }
-
-            $this->_addJs($aJs);
-            $this->_addCss($aCss);            
-
-            self::$_isCssJsAdded = true;
-
         }
         }
     }
     }
 
 
@@ -1510,25 +1556,37 @@ BLAH;
         $sRet = '';
         $sRet = '';
         if ($this->_bDynamicMode) {
         if ($this->_bDynamicMode) {
             $sRet .= $this->oTemplate->addCss($this->_aCss, true);
             $sRet .= $this->oTemplate->addCss($this->_aCss, true);
-            $sJs = $this->oTemplate->addJs($this->_aJs, true);
-            if (preg_match_all("/src=\"(.*?)\"/", $sJs, $aMatches))
-                $sRet .= "<script>
-                    bx_get_scripts(" . json_encode($aMatches[1]) . ");
-                </script>";
+
+
+            $sRet .= "\n<script>\n";
+            $sRet .= "\nglJsLoadOnaddWebForms = []\n";
+            foreach ($this->_aJs as $sCondition => $aJs) {
+                $sJs = $this->oTemplate->addJs($aJs, true);
+                if (!$sJs)
+                    continue;
+                
+                $sRet .= "if ($sCondition)\n";
+                if (preg_match_all("/src=\"(.*?)\"/", $sJs, $aMatches))
+                    $sRet .= "\tglJsLoadOnaddWebForms = glJsLoadOnaddWebForms.concat(" . json_encode($aMatches[1]) . ");\n";
+            }
+            $sRet .= "\n</script>\n";
         }
         }
         else {
         else {
             $this->oTemplate->addCss($this->_aCss);
             $this->oTemplate->addCss($this->_aCss);
-            $this->oTemplate->addJs($this->_aJs);
+            
+            $aJs = array();
+            foreach ($this->_aJs as $sCondition => $a)
+                $aJs = array_merge($aJs, $a);
+            $this->oTemplate->addJs($aJs);
         }
         }
         return $sRet;
         return $sRet;
     }
     }
 
 
-    protected function _addJs($mixed)
+    protected function _addJs($mixed, $sJsCondition)
     {
     {
         if (!is_array($mixed))
         if (!is_array($mixed))
             $mixed = array($mixed);
             $mixed = array($mixed);
-        foreach ($mixed as $s)
-            $this->_aJs[$s] = $s;
+        $this->_aJs[$sJsCondition] = $mixed;
     }
     }
 
 
     protected function _addCss($mixed)
     protected function _addCss($mixed)