From f6155a9e080f9e92490a4dfcd47de7fbb928047b Mon Sep 17 00:00:00 2001 From: Naydenov Date: Tue, 25 Feb 2020 12:12:03 +0200 Subject: [PATCH 1/6] [FEATURE] CheckBox for boolean properties --- app/scripts/modules/ui/DataView.js | 41 +++++++++++++++++++ .../modules/ui/helpers/DataViewHelper.js | 24 +++++++++++ 2 files changed, 65 insertions(+) diff --git a/app/scripts/modules/ui/DataView.js b/app/scripts/modules/ui/DataView.js index 5d46581e..0b0e4553 100644 --- a/app/scripts/modules/ui/DataView.js +++ b/app/scripts/modules/ui/DataView.js @@ -185,6 +185,8 @@ DataView.prototype._generateHTMLForKeyValuePair = function (key, currentView) { valueHTML = JSONFormatter.formatJSONtoHTML(value); } else if (typeof type === 'object') { valueHTML = DVHelper.wrapInSelectTag(value, attributes, type); + } else if (type === 'boolean') { + valueHTML = DVHelper.wrapInCheckBox(value, attributes); } else { valueHTML = DVHelper.valueNeedsQuotes(value, DVHelper.wrapInTag('value', value, attributes)); } @@ -355,6 +357,10 @@ DataView.prototype._onClickHandler = function () { if (targetElement.nodeName === 'SELECT') { that._onChangeHandler(targetElement); } + + if (targetElement.nodeName === 'INPUT') { + that._onCheckBoxHandler(targetElement); + } }; }; @@ -460,4 +466,39 @@ DataView.prototype._onChangeHandler = function (target) { }; }; +/** + * Change event handler for the boolean values. + * @param {element} target - HTML DOM element + * @private + */ +DataView.prototype._onCheckBoxHandler = function (target) { + var that = this; + + if (!target) { + return; + } + + /** + * Handler for change event. + * @param {Object} e + */ + target.onchange = function (e) { + var propertyData = {}, + propertyName, + target = e.target; + + propertyData.controlId = target.getAttribute('data-control-id'); + + propertyName = target.getAttribute('data-property-name'); + propertyData.property = propertyName.charAt(0).toUpperCase() + propertyName.slice(1); + + propertyData.value = target.checked; + + that.onPropertyUpdated(propertyData); + + target.removeEventListener('onchange', this); + that._selectValue = true; + }; +}; + module.exports = DataView; diff --git a/app/scripts/modules/ui/helpers/DataViewHelper.js b/app/scripts/modules/ui/helpers/DataViewHelper.js index 27635da0..a3694e22 100644 --- a/app/scripts/modules/ui/helpers/DataViewHelper.js +++ b/app/scripts/modules/ui/helpers/DataViewHelper.js @@ -143,6 +143,29 @@ function _wrapInSelectTag (value, attributes, type) { return html; } +/** + * @param {boolean} value + * @param {Object} attributes + * @returns {string} + * @private + */ +function _wrapInCheckBox (value, attributes) { + var html = ''; + + html = '' : ' />'; + html += ''; + + return html; +} + /** * Check if property value needs quotes. * @param {string|boolean|number|null} value @@ -372,5 +395,6 @@ module.exports = { toggleCollapse: _toggleCollapse, wrapInTag: _wrapInTag, wrapInSelectTag: _wrapInSelectTag, + wrapInCheckBox: _wrapInCheckBox, valueNeedsQuotes: _valueNeedsQuotes }; From 1a2ab24e4b1502a437ca5a8f9474aaa15e964a0e Mon Sep 17 00:00:00 2001 From: Naydenov Date: Thu, 5 Mar 2020 14:57:06 +0200 Subject: [PATCH 2/6] removed multiline declaration of variables --- app/scripts/modules/ui/DataView.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/scripts/modules/ui/DataView.js b/app/scripts/modules/ui/DataView.js index 0b0e4553..4d06b5dc 100644 --- a/app/scripts/modules/ui/DataView.js +++ b/app/scripts/modules/ui/DataView.js @@ -483,9 +483,9 @@ DataView.prototype._onCheckBoxHandler = function (target) { * @param {Object} e */ target.onchange = function (e) { - var propertyData = {}, - propertyName, - target = e.target; + var propertyData = {}; + var propertyName; + var target = e.target; propertyData.controlId = target.getAttribute('data-control-id'); From 6e11c667f109fbc289d0a378e8df41e0930982e9 Mon Sep 17 00:00:00 2001 From: Naydenov Date: Mon, 9 Mar 2020 10:59:06 +0200 Subject: [PATCH 3/6] test added --- tests/modules/ui/DataView.spec.js | 32 ++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/tests/modules/ui/DataView.spec.js b/tests/modules/ui/DataView.spec.js index bc44c3cf..a7c0d8c2 100644 --- a/tests/modules/ui/DataView.spec.js +++ b/tests/modules/ui/DataView.spec.js @@ -759,7 +759,7 @@ describe('DataView', function () { }); }); - describe('_changeHandler', function () { + describe('_changeHandler enum type', function () { var sampleView; var blurHandlerSpy; var changeHandlerSpy; @@ -789,6 +789,36 @@ describe('DataView', function () { }); }); + describe('_changeHandler boolean type', function () { + var sampleView; + var blurHandlerSpy; + var checkBoxHandler; + var dataViewElement; + var selectBox; + + beforeEach(function () { + sampleView = new DataViewComponent('data-view'); + sampleView.setData(mockDataWithTypes); + dataViewElement = document.getElementById('data-view'); + blurHandlerSpy = sinon.spy(DataViewComponent.prototype, '_onBlurHandler'); + checkBoxHandler = sinon.spy(DataViewComponent.prototype, '_onCheckBoxHandler'); + selectBox = dataViewElement.querySelector('input[type="checkbox"][data-control-id]'); + }); + + afterEach(function () { + dataViewElement = null; + blurHandlerSpy.restore(); + checkBoxHandler.restore(); + }); + + it('should not trigger blur handler', function () { + selectBox.click(); + selectBox.dispatchEvent(new Event('change')); + expect(blurHandlerSpy.notCalled).to.equal(true); + expect(checkBoxHandler.notCalled).to.equal(false); + }); + }); + describe('_onEnterHandler', function () { var sampleView; var enterHandlerSpy; From 9540e754cf2a83dfe9121bdfaa5104be9b694aab Mon Sep 17 00:00:00 2001 From: Naydenov Date: Tue, 10 Mar 2020 16:40:06 +0200 Subject: [PATCH 4/6] fixed review comments --- app/scripts/modules/ui/helpers/DataViewHelper.js | 10 +++++----- app/styles/less/themes/base/globals.less | 5 +++++ tests/styles/themes/dark/dark.css | 4 ++++ tests/styles/themes/light/light.css | 4 ++++ 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/scripts/modules/ui/helpers/DataViewHelper.js b/app/scripts/modules/ui/helpers/DataViewHelper.js index a3694e22..cbb2705b 100644 --- a/app/scripts/modules/ui/helpers/DataViewHelper.js +++ b/app/scripts/modules/ui/helpers/DataViewHelper.js @@ -152,13 +152,13 @@ function _wrapInSelectTag (value, attributes, type) { function _wrapInCheckBox (value, attributes) { var html = ''; - html = '' : ' />'; - html += '