From 9fcc449eed18a8772d71ceb4c960b4541c7996eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ruga=C5=82a?= Date: Fri, 9 Apr 2021 15:28:11 +0200 Subject: [PATCH] Add support for Textarea, Remove copy button and translation for button --- .../Controller/ObjectController.php | 31 ---- .../DivanteTranslationBundle.php | 1 + .../js/pimcore/object/elementservice.js | 52 ++---- .../public/js/pimcore/object/tags/input.js | 43 ++--- .../public/js/pimcore/object/tags/textarea.js | 166 ++++++++++++++++++ .../public/js/pimcore/object/tags/wysiwyg.js | 15 +- .../Resources/translations/admin.de.yml | 1 + .../Resources/translations/admin.en.yml | 1 + .../Resources/translations/admin.pl.yml | 1 + 9 files changed, 198 insertions(+), 113 deletions(-) create mode 100644 src/DivanteTranslationBundle/Resources/public/js/pimcore/object/tags/textarea.js create mode 100644 src/DivanteTranslationBundle/Resources/translations/admin.de.yml create mode 100644 src/DivanteTranslationBundle/Resources/translations/admin.en.yml create mode 100644 src/DivanteTranslationBundle/Resources/translations/admin.pl.yml diff --git a/src/DivanteTranslationBundle/Controller/ObjectController.php b/src/DivanteTranslationBundle/Controller/ObjectController.php index d49c349..dd68645 100644 --- a/src/DivanteTranslationBundle/Controller/ObjectController.php +++ b/src/DivanteTranslationBundle/Controller/ObjectController.php @@ -64,35 +64,4 @@ public function translateFieldAction(Request $request, ProviderFactory $provider 'data' => $data, ]); } - - /** - * @Route("/get-field-data", methods={"GET"}) - */ - public function getFieldDataAction(Request $request): JsonResponse - { - $object = DataObject::getById($request->get('sourceId')); - - if (!$object instanceof DataObject) { - return $this->adminJson([ - 'success' => false, - 'message' => 'Object doesn\'t exist', - ]); - } - - $fieldName = 'get' . ucfirst($request->get('fieldName')); - - try { - $data = $object->$fieldName($this->sourceLanguage); - } catch (\Throwable $exception) { - return $this->adminJson([ - 'success' => false, - 'message' => 'Field not found', - ]); - } - - return $this->adminJson([ - 'success' => true, - 'data' => $data, - ]); - } } diff --git a/src/DivanteTranslationBundle/DivanteTranslationBundle.php b/src/DivanteTranslationBundle/DivanteTranslationBundle.php index f0e8233..06119e4 100644 --- a/src/DivanteTranslationBundle/DivanteTranslationBundle.php +++ b/src/DivanteTranslationBundle/DivanteTranslationBundle.php @@ -18,6 +18,7 @@ public function getJsPaths(): array '/bundles/divantetranslation/js/pimcore/startup.js', '/bundles/divantetranslation/js/pimcore/object/tags/input.js', '/bundles/divantetranslation/js/pimcore/object/tags/wysiwyg.js', + '/bundles/divantetranslation/js/pimcore/object/tags/textarea.js', '/bundles/divantetranslation/js/pimcore/object/elementservice.js', ]; } diff --git a/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/elementservice.js b/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/elementservice.js index 78e9cf2..7de5bf5 100644 --- a/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/elementservice.js +++ b/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/elementservice.js @@ -13,40 +13,6 @@ pimcore.registerNS("pimcore.object.elementservice.x"); -pimcore.object.elementservice.addCopyButton = function (id, fieldName, component, type, lang) { - return new Ext.Button({ - iconCls: "pimcore_icon_copy", - cls: 'pimcore_button_transparent', - tooltip: t("copy_from_source_lang"), - handler: function () { - Ext.Ajax.request({ - url: "/admin/object/get-field-data", - method: "GET", - params: { - sourceId: id, - fieldName: fieldName, - lang: lang, - type: type - }, - success: function (response) { - var res = Ext.decode(response.responseText); - if (res.success) { - if (type == 'wysiwyg') { - CKEDITOR.instances[component.editableDivId].setData(res.data); - } else { - component.setRawValue(res.data); - } - } else { - pimcore.helpers.showPrettyError('object', t("error"), t("saving_failed"), res.message); - } - } - }); - }.bind(this), - style: "margin-left: 10px; filter:grayscale(100%);", - }); -}; - - pimcore.object.elementservice.translateButton = function (id, fieldName, component, type, lang) { return new Ext.Button({ iconCls: "pimcore_icon_translations", @@ -63,13 +29,19 @@ pimcore.object.elementservice.translateButton = function (id, fieldName, compone type: type }, success: function (response) { - var res = Ext.decode(response.responseText); + if (res.success) { - if (type == 'wysiwyg') { - CKEDITOR.instances[component.editableDivId].setData(res.data); - } else { - component.setRawValue(res.data); + switch (type) { + case 'wysiwyg': + CKEDITOR.instances[component.editableDivId].setData(res.data); + break; + case 'input': + component.setRawValue(res.data); + break; + case 'textarea': + component.component.setValue(res.data); + break; } } else { pimcore.helpers.showPrettyError('object', t("error"), t("saving_failed"), res.message); @@ -79,4 +51,4 @@ pimcore.object.elementservice.translateButton = function (id, fieldName, compone }.bind(this), style: "margin-left: 10px; filter:grayscale(100%);", }); -}; \ No newline at end of file +}; diff --git a/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/tags/input.js b/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/tags/input.js index 0c97b3b..46f5d5f 100644 --- a/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/tags/input.js +++ b/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/tags/input.js @@ -48,6 +48,18 @@ pimcore.object.tags.input = Class.create(pimcore.object.tags.input, { this.component = new Ext.form.TextField(input); + if (this.context.language) { + this.translateButton = new pimcore.object.elementservice.translateButton( + this.object.data.general.o_id, + this.fieldConfig.name, + this.component, + 'input', + this.context.language + ); + } else { + this.translateButton = {}; + } + if (this.fieldConfig.showCharCount) { var charCount = Ext.create("Ext.Panel", { bodyStyle: '', @@ -73,42 +85,17 @@ pimcore.object.tags.input = Class.create(pimcore.object.tags.input, { }, items: [ this.component, - charCount + charCount, + this.translateButton, ] }); } else { - if (this.context.language) { - this.copyButton = new pimcore.object.elementservice.addCopyButton( - this.object.data.general.o_id, - this.fieldConfig.name, - this.component, - 'input', - this.context.language - ); - } - else { - this.copyButton = {}; - - } - if (this.context.language) { - this.translateButton = new pimcore.object.elementservice.translateButton( - this.object.data.general.o_id, - this.fieldConfig.name, - this.component, - 'input', - this.context.language - ); - } else { - this.translateButton = {}; - } - return Ext.create('Ext.form.FieldContainer', { labelWidth: input.width, layout: 'hbox', items: [ this.component, - this.copyButton, this.translateButton, ], componentCls: "object_field", @@ -119,4 +106,4 @@ pimcore.object.tags.input = Class.create(pimcore.object.tags.input, { }); } }, -}); \ No newline at end of file +}); diff --git a/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/tags/textarea.js b/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/tags/textarea.js new file mode 100644 index 0000000..45e402a --- /dev/null +++ b/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/tags/textarea.js @@ -0,0 +1,166 @@ +/** + * Pimcore + * + * This source file is available under two different licenses: + * - GNU General Public License version 3 (GPLv3) + * - Pimcore Enterprise License (PEL) + * Full copyright and license information is available in + * LICENSE.md which is distributed with this source code. + * + * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) + * @license http://www.pimcore.org/license GPLv3 and PEL + */ + +pimcore.registerNS("pimcore.object.tags.textarea"); +pimcore.object.tags.textarea = Class.create(pimcore.object.tags.abstract, { + + type: "textarea", + + initialize: function (data, fieldConfig) { + this.data = data; + this.fieldConfig = fieldConfig; + + }, + + getGridColumnEditor: function(field) { + var editorConfig = {}; + + if (field.config) { + if (field.config.width) { + if (intval(field.config.width) > 10) { + editorConfig.width = field.config.width; + } + } + } + + if(field.layout.noteditable) { + return null; + } + // TEXTAREA + if (field.type == "textarea") { + return new Ext.form.TextArea(editorConfig); + } + }, + + getGridColumnFilter: function(field) { + return {type: 'string', dataIndex: field.key}; + }, + + getLayoutEdit: function () { + + + if (intval(this.fieldConfig.width) < 1) { + this.fieldConfig.width = 250; + } + if (intval(this.fieldConfig.height) < 1) { + this.fieldConfig.height = 250; + } + + var labelWidth = this.fieldConfig.labelWidth ? this.fieldConfig.labelWidth : 100; + + var conf = { + name: this.fieldConfig.name, + width: this.fieldConfig.width, + height: this.fieldConfig.height, + fieldLabel: this.fieldConfig.title, + labelWidth: labelWidth + }; + + if (!this.fieldConfig.showCharCount) { + conf.componentCls = "object_field object_field_type_" + this.type; + } + + if (this.context.language) { + this.translateButton = new pimcore.object.elementservice.translateButton( + this.object.data.general.o_id, + this.fieldConfig.name, + this, + 'textarea', + this.context.language + ); + } else { + this.translateButton = {}; + } + + conf.width += conf.labelWidth; + + if (this.data) { + conf.value = this.data; + } + if(this.fieldConfig.maxLength) { + conf.maxLength = this.fieldConfig.maxLength; + conf.enforceMaxLength = true; + } + + this.component = new Ext.form.TextArea(conf); + + if(this.fieldConfig.showCharCount) { + var charCount = Ext.create("Ext.Panel", { + bodyStyle: '', + margin: '0 0 0 0', + bodyCls: 'char_count', + width: conf.width, + height: 17 + }); + + this.component.setStyle("margin-bottom", "0"); + this.component.addListener("change", function(charCount) { + this.updateCharCount(this.component, charCount); + }.bind(this, charCount)); + + //init word count + this.updateCharCount(this.component, charCount); + + return Ext.create("Ext.Panel", { + cls: "object_field object_field_type_" + this.type, + style: "margin-bottom: 10px", + layout: { + type: 'vbox', + align: 'left' + }, + items: [ + this.component, + charCount, + this.translateButton, + ] + }); + + } else { + return Ext.create("Ext.Panel", { + cls: "object_field object_field_type_" + this.type, + style: "margin-bottom: 10px", + layout: { + type: 'vbox', + align: 'left' + }, + items: [ + this.component, + this.translateButton, + ] + }); + } + }, + + updateCharCount: function(textField, charCount) { + if( this.fieldConfig.maxLength) { + charCount.setHtml(textField.getValue().length + "/" + this.fieldConfig.maxLength); + } else { + charCount.setHtml(textField.getValue().length); + } + }, + + + getLayoutShow: function () { + var layout = this.getLayoutEdit(); + this.component.setReadOnly(true); + return layout; + }, + + getValue: function () { + return this.component.getValue(); + }, + + getName: function () { + return this.fieldConfig.name; + } +}); diff --git a/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/tags/wysiwyg.js b/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/tags/wysiwyg.js index cbf7bd4..f6f3247 100644 --- a/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/tags/wysiwyg.js +++ b/src/DivanteTranslationBundle/Resources/public/js/pimcore/object/tags/wysiwyg.js @@ -25,18 +25,6 @@ pimcore.object.tags.wysiwyg = Class.create(pimcore.object.tags.wysiwyg, { this.ckeditor = null; } }.bind(this)); - if (this.context.language) { - this.copyButton = new pimcore.object.elementservice.addCopyButton( - this.object.data.general.o_id, - this.fieldConfig.name, - this, - 'wysiwyg', - this.context.language - ); - } - else { - this.copyButton = {}; - } if (this.fieldConfig.width) { width = this.fieldConfig.width; } @@ -60,7 +48,6 @@ pimcore.object.tags.wysiwyg = Class.create(pimcore.object.tags.wysiwyg, { layout: 'hbox', items: [ this.component, - this.copyButton, this.translateButton, ], componentCls: "object_field custom_wysiwyg", @@ -71,4 +58,4 @@ pimcore.object.tags.wysiwyg = Class.create(pimcore.object.tags.wysiwyg, { }, }); }, -}); \ No newline at end of file +}); diff --git a/src/DivanteTranslationBundle/Resources/translations/admin.de.yml b/src/DivanteTranslationBundle/Resources/translations/admin.de.yml new file mode 100644 index 0000000..bcad626 --- /dev/null +++ b/src/DivanteTranslationBundle/Resources/translations/admin.de.yml @@ -0,0 +1 @@ +translate_field: Übersetzen diff --git a/src/DivanteTranslationBundle/Resources/translations/admin.en.yml b/src/DivanteTranslationBundle/Resources/translations/admin.en.yml new file mode 100644 index 0000000..f781412 --- /dev/null +++ b/src/DivanteTranslationBundle/Resources/translations/admin.en.yml @@ -0,0 +1 @@ +translate_field: Translate diff --git a/src/DivanteTranslationBundle/Resources/translations/admin.pl.yml b/src/DivanteTranslationBundle/Resources/translations/admin.pl.yml new file mode 100644 index 0000000..2c8c6f3 --- /dev/null +++ b/src/DivanteTranslationBundle/Resources/translations/admin.pl.yml @@ -0,0 +1 @@ +translate_field: Tłumacz