ソースを参照

Ticket #4980 - Studio: CodeMirror for 'Config API' fields in Pages and Menus.

AntonLV 1 年間 前
親
コミット
42c29929bd

+ 18 - 0
inc/js/classes/BxDolForm.js

@@ -12,6 +12,7 @@ function BxDolForm(oOptions)
         return;
 
     this._sObjName = oOptions.sObjName === undefined ? 'oForm' : oOptions.sObjName; // javascript object name, to run current object instance from onTimer
+    this._sId = oOptions.sId; // form HTML ID
     this._sName = oOptions.sName; // form name
     this._sObject = oOptions.sObject; // form object
     this._sDisplay = oOptions.sDisplay; // form display
@@ -29,8 +30,25 @@ function BxDolForm(oOptions)
 
     if(this._bLeavePageConfirmation)
         this.initLeavePageConfirmation();
+
+    if(oOptions.bInitCodeMirror != undefined && oOptions.bInitCodeMirror)
+        this.initCodeMirror();
 }
 
+BxDolForm.prototype.initCodeMirror = function()
+{
+    var oSelector = $('#' + this._sId + ' .bx-form-input-code');
+
+    for(var i = 0; i < oSelector.length; i++) {
+        var e = CodeMirror.fromTextArea(oSelector.get(i), {
+            lineNumbers: true,
+            mode: "htmlmixed",
+            htmlMode: true,
+            matchBrackets: true
+        });
+    }
+};
+
 BxDolForm.prototype.initLeavePageConfirmation = function()
 {
     var $this = this;

+ 19 - 0
modules/boonex/developer/classes/BxDevBuilderPage.php

@@ -62,6 +62,11 @@ class BxDevBuilderPage extends BxTemplStudioBuilderPage
         foreach($aLayouts as $aLayout)
             $oForm->aInputs['layout_id']['values'][] = array('key' => $aLayout['id'], 'value' => _t($aLayout['title']));
 
+        if(($sKey = 'config_api') && isset($oForm->aInputs[$sKey]))
+            $oForm->aInputs[$sKey] = array_merge($oForm->aInputs[$sKey], [
+                'code' => 1
+            ]);
+
         $oForm->initChecker();
         if($oForm->isSubmittedAndValid()) {
             $sObject = $oForm->getCleanValue('object');
@@ -247,6 +252,7 @@ class BxDevBuilderPage extends BxTemplStudioBuilderPage
                 'info' => '',
                 'value' => $this->aPageRebuild['config_api'],
                 'required' => '',
+                'code' => '1',
                 'db' => array (
                     'pass' => 'XssHtml',
                 ),
@@ -443,6 +449,11 @@ class BxDevBuilderPage extends BxTemplStudioBuilderPage
 
         $oForm->aInputs = $this->addInArray($oForm->aInputs, 'visible_for_levels', $this->getBlockContent($aBlock));
 
+        if(($sKey = 'config_api') && isset($oForm->aInputs[$sKey]))
+            $oForm->aInputs[$sKey] = array_merge($oForm->aInputs[$sKey], [
+                'code' => 1
+            ]);
+
         $oForm->aInputs['controls'][0]['value'] = _t('_bx_dev_bp_btn_block_save');
         $oForm->aInputs['controls'][2]['attrs']['onclick'] = $this->getPageJsObject() . ".deleteBlock(" . $aBlock['id'] . ")";
 
@@ -482,6 +493,14 @@ class BxDevBuilderPage extends BxTemplStudioBuilderPage
         BxDolForm::setSubmittedValue('content', $sValue, $oForm->aFormAttrs['method']);
     }
 
+    protected function _getPageEditForm()
+    {
+        $oForm = parent::_getPageEditForm();
+        $oForm->addCssJsCodeMirror();
+
+        return $oForm;
+    }
+
     protected function _getTmplVarsBlockPanelTop()
     {
         $aResult = parent::_getTmplVarsBlockPanelTop();

+ 5 - 0
modules/boonex/developer/classes/BxDevNavigationItems.php

@@ -182,6 +182,11 @@ class BxDevNavigationItems extends BxTemplStudioNavigationItems
             3 => _t('_adm_nav_txt_block_hidden_on_col_wide'),
             4 => _t('_adm_nav_txt_block_hidden_on_col_full')
         ];
+
+        if(($sKey = 'config_api') && isset($aInputs[$sKey]))
+            $aInputs[$sKey] = array_merge($aInputs[$sKey], [
+                'code' => 1
+            ]);
     }
 }
 /** @} */

+ 92 - 72
studio/template/scripts/BxBaseStudioBuilderPage.php

@@ -276,49 +276,9 @@ class BxBaseStudioBuilderPage extends BxDolStudioBuilderPage
         $sJsObject = $this->getPageJsObject();
         $oTemplate = BxDolStudioTemplate::getInstance();
 
-        $sModule = BX_DOL_STUDIO_MODULE_CUSTOM;
+        $sModule = BX_DOL_STUDIO_MODULE_CUSTOM;        
 
-        $aForm = array(
-            'form_attrs' => array(
-                'id' => 'adm-bp-page-create',
-                'action' => sprintf($this->sPageUrl, $this->sType, $this->sPage) . '&bp_action=' . $this->sActionPageCreate,
-                'method' => 'post'
-            ),
-            'params' => array (
-                'db' => array(
-                    'table' => 'sys_objects_page',
-                    'key' => 'id',
-                    'uri' => '',
-                    'uri_title' => '',
-                    'submit_name' => 'do_submit'
-                ),
-            ),
-            'inputs' => array (
-                'module' => array(
-                    'type' => 'hidden',
-                    'name' => 'module',
-                    'value' => $sModule,
-                    'db' => array (
-                        'pass' => 'Xss',
-                    ),
-                ),
-                'deletable' => array(
-                    'type' => 'hidden',
-                    'name' => 'deletable',
-                    'value' => 1,
-                    'db' => array (
-                        'pass' => 'Int',
-                    ),
-                ),
-                'settings' => array(
-                    'type' => 'custom',
-                    'name' => 'settings',
-                    'content' => $oTemplate->parseHtmlByName('bp_edit_page_form.html', $this->_getTmplVarsPageSettings()),
-                ),
-            )
-        );
-
-        $oForm = new BxTemplStudioFormView($aForm);
+        $oForm = $this->_getPageCreateForm();
         $oForm->initChecker();
 
         if($oForm->isSubmittedAndValid()) {
@@ -328,7 +288,7 @@ class BxBaseStudioBuilderPage extends BxDolStudioBuilderPage
 
             $sLanguage = BxDolStudioLanguagesUtils::getInstance()->getCurrentLangName(false);
 
-            $sObject = BxDolForm::getSubmittedValue('title-' . $sLanguage, $aForm['form_attrs']['method']);
+            $sObject = BxDolForm::getSubmittedValue('title-' . $sLanguage, $oForm->aFormAttrs['method']);
             $sObject = uriGenerate($sObject, 'sys_objects_page', 'object', ['empty' => 'object']);
 
             $sUri = $oForm->getCleanValue('uri');
@@ -338,7 +298,7 @@ class BxBaseStudioBuilderPage extends BxDolStudioBuilderPage
             	return ['msg' => _t('_adm_bp_err_page_uri')];
 
             $iVisibleFor = BxDolStudioUtils::getVisibilityValue($oForm->getCleanValue('visible_for'), $oForm->getCleanValue('visible_for_levels'));
-            BxDolForm::setSubmittedValue('visible_for_levels', $iVisibleFor, $aForm['form_attrs']['method']);
+            BxDolForm::setSubmittedValue('visible_for_levels', $iVisibleFor, $oForm->aFormAttrs['method']);
             unset($oForm->aInputs['visible_for']);
 
             $aValsToAdd = [
@@ -359,7 +319,7 @@ class BxBaseStudioBuilderPage extends BxDolStudioBuilderPage
 
         $sContent = BxTemplStudioFunctions::getInstance()->popupBox($this->aHtmlIds['add_popup_id'], _t('_adm_bp_txt_create_popup'), $oTemplate->parseHtmlByName('bp_add_page.html', array(
             'js_object' => $sJsObject,
-            'form_id' => $aForm['form_attrs']['id'],
+            'form_id' => $oForm->getId(),
             'form' => $oForm->getCode(true)
         )));
 
@@ -371,31 +331,7 @@ class BxBaseStudioBuilderPage extends BxDolStudioBuilderPage
         $sJsObject = $this->getPageJsObject();
         $oTemplate = BxDolStudioTemplate::getInstance();
 
-        $aForm = array(
-            'form_attrs' => array(
-                'id' => 'adm-bp-page-edit',
-                'action' => sprintf($this->sPageUrl, $this->sType, $this->sPage) . '&bp_action=' . $this->sActionPageEdit,
-                'method' => 'post'
-            ),
-            'params' => array (
-                'db' => array(
-                    'table' => 'sys_objects_page',
-                    'key' => 'id',
-                    'uri' => '',
-                    'uri_title' => '',
-                    'submit_name' => 'do_submit'
-                ),
-            ),
-            'inputs' => array (
-                'settings' => array(
-                    'type' => 'custom',
-                    'name' => 'settings',
-                    'content' => $oTemplate->parseHtmlByName('bp_edit_page_form.html', $this->_getTmplVarsPageSettings($this->aPageRebuild, false)),
-                ),
-            )
-        );
-
-        $oForm = new BxTemplStudioFormView($aForm);
+        $oForm = $this->_getPageEditForm();
         $oForm->initChecker();
 
         if($oForm->isSubmittedAndValid()) {
@@ -407,7 +343,7 @@ class BxBaseStudioBuilderPage extends BxDolStudioBuilderPage
             $mixedVisibleForLevels = $oForm->getCleanValue('visible_for_levels');
             if($mixedVisibleFor !== false && $mixedVisibleForLevels !== false) {
                 $iVisibleFor = BxDolStudioUtils::getVisibilityValue($mixedVisibleFor, $mixedVisibleForLevels);
-                BxDolForm::setSubmittedValue('visible_for_levels', $iVisibleFor, $aForm['form_attrs']['method']);
+                BxDolForm::setSubmittedValue('visible_for_levels', $iVisibleFor, $oForm->aFormAttrs['method']);
                 unset($oForm->aInputs['visible_for']);
             }
 
@@ -436,7 +372,7 @@ class BxBaseStudioBuilderPage extends BxDolStudioBuilderPage
 
         $sContent = BxTemplStudioFunctions::getInstance()->popupBox($this->aHtmlIds['edit_popup_id'], _t('_adm_bp_txt_settings_popup'), $oTemplate->parseHtmlByName('bp_edit_page.html', array(
             'js_object' => $sJsObject,
-            'form_id' => $aForm['form_attrs']['id'],
+            'form_id' => $oForm->getId(),
             'form' => $oForm->getCode(true)
         )));
 
@@ -1929,6 +1865,90 @@ class BxBaseStudioBuilderPage extends BxDolStudioBuilderPage
         return $aBlocks;
     }
 
+    protected function _getPageCreateForm()
+    {
+        $oTemplate = BxDolStudioTemplate::getInstance();
+
+        $sForm = 'adm-bp-page-create';
+        $aForm = array(
+            'form_attrs' => array(
+                'id' => $sForm,
+                'name' => $sForm,
+                'action' => sprintf($this->sPageUrl, $this->sType, $this->sPage) . '&bp_action=' . $this->sActionPageCreate,
+                'method' => 'post'
+            ),
+            'params' => array (
+                'db' => array(
+                    'table' => 'sys_objects_page',
+                    'key' => 'id',
+                    'uri' => '',
+                    'uri_title' => '',
+                    'submit_name' => 'do_submit'
+                ),
+                'display' => $sForm
+            ),
+            'inputs' => array (
+                'module' => array(
+                    'type' => 'hidden',
+                    'name' => 'module',
+                    'value' => BX_DOL_STUDIO_MODULE_CUSTOM,
+                    'db' => array (
+                        'pass' => 'Xss',
+                    ),
+                ),
+                'deletable' => array(
+                    'type' => 'hidden',
+                    'name' => 'deletable',
+                    'value' => 1,
+                    'db' => array (
+                        'pass' => 'Int',
+                    ),
+                ),
+                'settings' => array(
+                    'type' => 'custom',
+                    'name' => 'settings',
+                    'content' => $oTemplate->parseHtmlByName('bp_edit_page_form.html', $this->_getTmplVarsPageSettings()),
+                ),
+            )
+        );
+
+        return new BxTemplStudioFormView($aForm);
+    }
+
+    protected function _getPageEditForm()
+    {
+        $oTemplate = BxDolStudioTemplate::getInstance();
+
+        $sForm = 'adm-bp-page-edit';
+        $aForm = array(
+            'form_attrs' => array(
+                'id' => $sForm,
+                'name' => $sForm,
+                'action' => sprintf($this->sPageUrl, $this->sType, $this->sPage) . '&bp_action=' . $this->sActionPageEdit,
+                'method' => 'post'
+            ),
+            'params' => array (
+                'db' => array(
+                    'table' => 'sys_objects_page',
+                    'key' => 'id',
+                    'uri' => '',
+                    'uri_title' => '',
+                    'submit_name' => 'do_submit'
+                ),
+                'display' => $sForm
+            ),
+            'inputs' => array (
+                'settings' => array(
+                    'type' => 'custom',
+                    'name' => 'settings',
+                    'content' => $oTemplate->parseHtmlByName('bp_edit_page_form.html', $this->_getTmplVarsPageSettings($this->aPageRebuild, false)),
+                ),
+            )
+        );
+
+        return new BxTemplStudioFormView($aForm);
+    }
+
     protected function _getTmplVarsBlockPanelTop()
     {
         $sJsObject = $this->getPageJsObject();

+ 25 - 0
template/scripts/BxBaseFormView.php

@@ -15,6 +15,7 @@ class BxBaseFormView extends BxDolForm
     protected static $_isCssJsUiAdded = false;
     protected static $_isCssJsUiSortableAdded = false;
     protected static $_isCssJsMinicolorsAdded = false;
+    protected static $_isCssJsCodeMirrorAdded = false;
     protected static $_isCssJsLabelsAdded = false;
     protected static $_isCssJsTimepickerAdded = false;
     protected static $_isCssJsAddedViewMode = false;
@@ -524,11 +525,13 @@ class BxBaseFormView extends BxDolForm
 
         $sCode = "if(window['" . $sJsObjName . "'] == undefined) window['" . $sJsObjName . "'] = new " . $sJsObjClass . "(" . json_encode(array(
             'sObjName' => $sJsObjName,
+            'sId' => $this->getId(),
             'sName' => $this->getName(),
             'sObject' => isset($this->aParams['object']) ? $this->aParams['object'] : '',
             'sDisplay' => isset($this->aParams['display']) ? $this->aParams['display'] : '',
             'sRootUrl' => BX_DOL_URL_ROOT,
             'aHtmlIds' => $this->_aHtmlIds,
+            'bInitCodeMirror' => self::$_isCssJsCodeMirrorAdded === true,
             'bLeavePageConfirmation' => getParam('sys_form_lpc_enable') == 'on',
             'sTxtLeavePageConfirmation' => _t('_sys_leave_page_confirmation')
         )) . ");";
@@ -1593,6 +1596,10 @@ BLAH;
             $sClassAdd .= ' bx-form-input-html ' . $sUniq;
         }
 
+        if(isset($aInput['code']) && $aInput['code'] && $this->addCodeEditor($aInput['code'], $aInput, $sUniq)){
+            $sClassAdd .= ' bx-form-input-code ' . $sUniq;
+        }
+
         $sValue = isset($aInput['value']) ? bx_process_output((isset($aInput['html']) && $aInput['html']) || (isset($aInput['code']) && $aInput['code']) ? $aInput['value'] : strip_tags($aInput['value']), BX_DATA_TEXT, array('no_process_macros')) : '';
 
         return $this->oTemplate->parseHtmlByName('form_field_textarea.html', [
@@ -1630,6 +1637,13 @@ BLAH;
         return true;
     }
     
+    function addCodeEditor($iViewMode, &$aInput, $sUniq)
+    {
+        $this->addCssJsCodeMirror();
+
+        return true;
+    }
+    
     function getHtmlEditorQueryParams($aInput)
     {
         $aQueryParams = ['i' => $aInput['name'], 'f' => $this->aFormAttrs['id'], 'fi' => ''];
@@ -2569,6 +2583,17 @@ BLAH;
         self::$_isCssJsMinicolorsAdded = true;
     }
 
+    function addCssJsCodeMirror ()
+    {
+        if (self::$_isCssJsCodeMirrorAdded)
+            return;       
+
+        $this->_addCss([BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'codemirror/|codemirror.css']);
+        $this->_addJs(['codemirror/codemirror.min.js'], "'undefined' === typeof(CodeMirror)");
+
+        self::$_isCssJsCodeMirrorAdded = true;
+    }
+
     function addCssJsViewMode ()
     {
         if (self::$_isCssJsAddedViewMode)