From 9df61460d04f22372325586e6471cbc0af6a2d13 Mon Sep 17 00:00:00 2001
From: "michael.daniel" <michael@pcsg.de>
Date: Wed, 23 Jan 2019 13:56:26 +0100
Subject: [PATCH] feat: data-qui-options-* attributs in bricks.xml will no
 longer be ignored. refactor: Some code preformance improvements in
 BrickEdit.js. quiqqer/package-bricks#104

---
 bin/BrickEdit.js           | 65 ++++++++++++++++++++++++--------------
 src/QUI/Bricks/Manager.php | 25 +++++++++++----
 2 files changed, 61 insertions(+), 29 deletions(-)

diff --git a/bin/BrickEdit.js b/bin/BrickEdit.js
index 2bb196d..e6f22ad 100644
--- a/bin/BrickEdit.js
+++ b/bin/BrickEdit.js
@@ -26,7 +26,8 @@ define('package/quiqqer/bricks/bin/BrickEdit', [
     'css!package/quiqqer/bricks/bin/BrickEdit.css'
 
 ], function (QUI, QUIPanel, QUIConfirm, BrickAreas, QUIAjax, QUILocale,
-             Projects, QUIFormUtils, ControlUtils, Template, Bricks) {
+    Projects, QUIFormUtils, ControlUtils, Template, Bricks
+) {
     "use strict";
 
     var lg = 'quiqqer/bricks';
@@ -64,14 +65,14 @@ define('package/quiqqer/bricks/bin/BrickEdit', [
         initialize: function (options) {
             this.parent(options);
 
-            this.$availableBricks   = [];
+            this.$availableBricks = [];
             this.$availableSettings = [];
-            this.$customfields      = [];
-            this.$loaded            = false;
+            this.$customfields = [];
+            this.$loaded = false;
 
             this.$Container = null;
-            this.$Editor    = false;
-            this.$Areas     = false;
+            this.$Editor = false;
+            this.$Areas = false;
 
             this.addEvents({
                 onInject       : this.$onInject,
@@ -185,7 +186,7 @@ define('package/quiqqer/bricks/bin/BrickEdit', [
                  */
                 this.$availableBricks = bricks;
                 this.$availableSettings = brick.availableSettings;
-                this.$customfields      = brick.customfields;
+                this.$customfields = brick.customfields;
 
                 this.setAttribute('data', brick);
 
@@ -432,9 +433,9 @@ define('package/quiqqer/bricks/bin/BrickEdit', [
 
                         if (attributes.areas) {
                             areas = attributes.areas
-                                              .replace(/^,*/, '')
-                                              .replace(/,*$/, '')
-                                              .split(',');
+                                .replace(/^,*/, '')
+                                .replace(/,*$/, '')
+                                .split(',');
                         }
 
                         // areas
@@ -459,11 +460,11 @@ define('package/quiqqer/bricks/bin/BrickEdit', [
                             new Element('tr', {
                                 'class': i % 2 ? 'odd' : 'even',
                                 html   : '<td>' +
-                                '<label>' +
-                                '<input type="checkbox" name="flexible-' + data.name + '" />' +
-                                '<span>' + QUILocale.get(data.text[0], data.text[1]) + '</span>' +
-                                '</label>' +
-                                '</td>'
+                                    '<label>' +
+                                    '<input type="checkbox" name="flexible-' + data.name + '" />' +
+                                    '<span>' + QUILocale.get(data.text[0], data.text[1]) + '</span>' +
+                                    '</label>' +
+                                    '</td>'
                             }).inject(TBody);
                         }
 
@@ -678,7 +679,8 @@ define('package/quiqqer/bricks/bin/BrickEdit', [
 
                 TableExtra.setStyle('display', null);
 
-                var i, len, Row, text, Value, setting, extraFieldId;
+                var i, c, len, cLen, attr, Row, text, Value, setting,
+                    extraFieldId, dataAttributes;
 
                 var self = this,
                     id   = this.getId(),
@@ -686,8 +688,9 @@ define('package/quiqqer/bricks/bin/BrickEdit', [
 
                 // extra settings
                 for (i = 0, len = this.$availableSettings.length; i < len; i++) {
-                    setting      = this.$availableSettings[i];
+                    setting = this.$availableSettings[i];
                     extraFieldId = 'extraField_' + id + '_' + i;
+                    dataAttributes = setting['data-attributes'];
 
                     text = setting.text;
 
@@ -699,11 +702,11 @@ define('package/quiqqer/bricks/bin/BrickEdit', [
                     Row = new Element('tr', {
                         'class': i % 2 ? 'odd' : 'even',
                         html   : '<td>' +
-                        '    <label class="quiqqer-bricks-areas" for="' + extraFieldId + '">' +
-                        text +
-                        '    </label>' +
-                        '</td>' +
-                        '<td></td>'
+                            '    <label class="quiqqer-bricks-areas" for="' + extraFieldId + '">' +
+                            text +
+                            '    </label>' +
+                            '</td>' +
+                            '<td></td>'
                     }).inject(TableBody);
 
                     if (setting.type !== 'select') {
@@ -718,6 +721,14 @@ define('package/quiqqer/bricks/bin/BrickEdit', [
                             Value.set('data-qui', setting['data-qui']);
                         }
 
+                        if (typeof dataAttributes === 'object') {
+                            for (attr in dataAttributes) {
+                                if (dataAttributes.hasOwnProperty(attr)) {
+                                    Value.set(attr, dataAttributes[attr]);
+                                }
+                            }
+                        }
+
                         continue;
                     }
 
@@ -728,7 +739,7 @@ define('package/quiqqer/bricks/bin/BrickEdit', [
                     }).inject(Row.getElement('td:last-child'));
 
 
-                    for (var c = 0, clen = setting.options.length; c < clen; c++) {
+                    for (c = 0, cLen = setting.options.length; c < cLen; c++) {
                         text = setting.options[c].text;
 
                         if (typeOf(setting.options[c].text) === 'array') {
@@ -743,6 +754,14 @@ define('package/quiqqer/bricks/bin/BrickEdit', [
                             value: setting.options[c].value
                         }).inject(Value);
                     }
+
+                    if (typeof dataAttributes === 'object') {
+                        for (attr in dataAttributes) {
+                            if (dataAttributes.hasOwnProperty(attr)) {
+                                Value.set(attr, dataAttributes[attr]);
+                            }
+                        }
+                    }
                 }
 
                 TableExtra.setStyle('display', null);
diff --git a/src/QUI/Bricks/Manager.php b/src/QUI/Bricks/Manager.php
index 737b08e..3bd11fa 100644
--- a/src/QUI/Bricks/Manager.php
+++ b/src/QUI/Bricks/Manager.php
@@ -623,13 +623,26 @@ protected function parseSettingToBrickArray(\DOMElement $Setting)
             }
         }
 
+        $dataAttributes = [];
+
+        foreach ($Setting->attributes as $attribute) {
+            if ($attribute->nodeName === 'data-qui') {
+                continue;
+            }
+
+            if (strpos($attribute->nodeName, 'data-') !== false) {
+                $dataAttributes[$attribute->nodeName] = trim($attribute->nodeValue);
+            }
+        }
+
         return [
-            'name'     => $Setting->getAttribute('name'),
-            'text'     => QUI\Utils\DOM::getTextFromNode($Setting, false),
-            'type'     => $Setting->getAttribute('type'),
-            'class'    => $Setting->getAttribute('class'),
-            'data-qui' => $Setting->getAttribute('data-qui'),
-            'options'  => $options
+            'name'            => $Setting->getAttribute('name'),
+            'text'            => QUI\Utils\DOM::getTextFromNode($Setting, false),
+            'type'            => $Setting->getAttribute('type'),
+            'class'           => $Setting->getAttribute('class'),
+            'data-qui'        => $Setting->getAttribute('data-qui'),
+            'options'         => $options,
+            'data-attributes' => $dataAttributes
         ];
     }
 
-- 
GitLab