Skip to content

Commit

Permalink
Make visbileBinding work for InfoBoxView
Browse files Browse the repository at this point in the history
REDMINE-20438
  • Loading branch information
tf committed Sep 21, 2023
1 parent 936a5d7 commit 6b01e4a
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 61 deletions.
2 changes: 1 addition & 1 deletion app/assets/stylesheets/pageflow/ui/forms.scss
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ textarea.short {
text-decoration: underline;
}

.input-hidden_via_binding {
.hidden_via_binding {
display: none;
}

Expand Down
31 changes: 31 additions & 0 deletions package/spec/editor/views/InfoBoxView-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Backbone from 'backbone';

import {InfoBoxView} from 'editor/views/InfoBoxView';

describe('InfoBoxView', () => {
describe('with visibleBindingValue option', () => {
it('hides element when value of attribute does not match', () => {
var view = new InfoBoxView({
model: new Backbone.Model({hidden: true}),
visibleBinding: 'hidden',
visibleBindingValue: false
});

view.render();

expect(view.$el).toHaveClass('hidden_via_binding');
});

it('does not set hidden class when value of attribute matches', () => {
var view = new InfoBoxView({
model: new Backbone.Model({hidden: false}),
visibleBinding: 'hidden',
visibleBindingValue: false
});

view.render();

expect(view.$el).not.toHaveClass('hidden_via_binding');
});
});
});
22 changes: 11 additions & 11 deletions package/spec/ui/views/mixins/inputView-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ describe('pageflow.inputView', () => {

view.render();

expect(view.$el).not.toHaveClass('input-hidden_via_binding');
expect(view.$el).not.toHaveClass('hidden_via_binding');
});

it('sets hidden class when attribute is false', () => {
Expand All @@ -520,7 +520,7 @@ describe('pageflow.inputView', () => {

view.render();

expect(view.$el).toHaveClass('input-hidden_via_binding');
expect(view.$el).toHaveClass('hidden_via_binding');
});

it('does not set hidden class when attribute is true', () => {
Expand All @@ -531,7 +531,7 @@ describe('pageflow.inputView', () => {

view.render();

expect(view.$el).not.toHaveClass('input-hidden_via_binding');
expect(view.$el).not.toHaveClass('hidden_via_binding');
});

it('sets hidden class when attribute changes to false', () => {
Expand All @@ -544,7 +544,7 @@ describe('pageflow.inputView', () => {

view.model.set('active', false);

expect(view.$el).toHaveClass('input-hidden_via_binding');
expect(view.$el).toHaveClass('hidden_via_binding');
});

it('allows overriding updateVisible method for custom behavior', () => {
Expand Down Expand Up @@ -583,7 +583,7 @@ describe('pageflow.inputView', () => {

view.render();

expect(view.$el).toHaveClass('input-hidden_via_binding');
expect(view.$el).toHaveClass('hidden_via_binding');
});

it('does not set hidden class when value of attribute matches', () => {
Expand All @@ -595,7 +595,7 @@ describe('pageflow.inputView', () => {

view.render();

expect(view.$el).not.toHaveClass('input-hidden_via_binding');
expect(view.$el).not.toHaveClass('hidden_via_binding');
});
});

Expand All @@ -609,7 +609,7 @@ describe('pageflow.inputView', () => {

view.render();

expect(view.$el).toHaveClass('input-hidden_via_binding');
expect(view.$el).toHaveClass('hidden_via_binding');
});

it('does not set hidden class when function returns true', () => {
Expand All @@ -621,7 +621,7 @@ describe('pageflow.inputView', () => {

view.render();

expect(view.$el).not.toHaveClass('input-hidden_via_binding');
expect(view.$el).not.toHaveClass('hidden_via_binding');
});

describe('with multiple binding attributes', () => {
Expand All @@ -648,7 +648,7 @@ describe('pageflow.inputView', () => {
view.render();
view.model.set('active', false);

expect(view.$el).toHaveClass('input-hidden_via_binding');
expect(view.$el).toHaveClass('hidden_via_binding');
});
});
});
Expand All @@ -663,7 +663,7 @@ describe('pageflow.inputView', () => {

view.render();

expect(view.$el).toHaveClass('input-hidden_via_binding');
expect(view.$el).toHaveClass('hidden_via_binding');
});

it('does not set hidden if true', () => {
Expand All @@ -675,7 +675,7 @@ describe('pageflow.inputView', () => {

view.render();

expect(view.$el).not.toHaveClass('input-hidden_via_binding');
expect(view.$el).not.toHaveClass('hidden_via_binding');
});
});
});
Expand Down
13 changes: 13 additions & 0 deletions package/src/editor/views/InfoBoxView.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import Marionette from 'backbone.marionette';

import {attributeBinding} from 'pageflow/ui';

export const InfoBoxView = Marionette.View.extend({
className: 'info_box',

mixins: [attributeBinding],

initialize() {
this.setupBooleanAttributeBinding('visible', this.updateVisible);
},

updateVisible: function() {
this.$el.toggleClass('hidden_via_binding',
this.getBooleanAttributBoundOption('visible') === false);
},

render: function() {
this.$el.addClass(this.options.level)
this.$el.html(this.options.text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const ConfigurationEditorTab = Base.extend({
},

visibleInputPropertyNames: function() {
return this.$el.find('.input:not(.input-hidden_via_binding)').map(function() {
return this.$el.find('.input:not(.hidden_via_binding)').map(function() {
return $(this).data('inputPropertyName');
}).get();
},
Expand Down
1 change: 1 addition & 0 deletions package/src/ui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export * from './views/tableCells/PresenceTableCellView';
export * from './views/tableCells/IconTableCellView';
export * from './views/tableCells/TextTableCellView';

export * from './views/mixins/attributeBinding';
export * from './views/mixins/inputWithPlaceholderText';
export * from './views/mixins/subviewContainer';
export * from './views/mixins/tooltipContainer';
Expand Down
50 changes: 50 additions & 0 deletions package/src/ui/views/mixins/attributeBinding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import _ from 'underscore';

export const attributeBinding = {
setupBooleanAttributeBinding(optionName, updateMethod) {
this.setupAttributeBinding(optionName, updateMethod, Boolean);
},

getBooleanAttributBoundOption(optionName) {
return this.getAttributeBoundOption(optionName, Boolean);
},

setupAttributeBinding: function(optionName, updateMethod, normalize = value => value) {
const binding = this.options[`${optionName}Binding`];
const view = this;

if (binding) {
_.flatten([binding]).forEach(attribute => {
this.listenTo(this.model, 'change:' + attribute, update);
});
}

update();

function update() {
updateMethod.call(view, view.getAttributeBoundOption(optionName, normalize));
}
},

getAttributeBoundOption(optionName, normalize = value => value) {
const binding = this.options[`${optionName}Binding`];
const bindingValueOptionName = `${optionName}BindingValue`;

const value = Array.isArray(binding) ?
binding.map(attribute => this.model.get(attribute)) :
this.model.get(binding);

if (bindingValueOptionName in this.options) {
return value === this.options[bindingValueOptionName];
}
else if (typeof this.options[optionName] === 'function') {
return normalize(this.options[optionName](value));
}
else if (optionName in this.options) {
return normalize(this.options[optionName]);
}
else if (binding) {
return normalize(value);
}
}
};
52 changes: 4 additions & 48 deletions package/src/ui/views/mixins/inputView.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _ from 'underscore';

import {attributeTranslationKeys, findTranslation, translationKeysWithSuffix} from '../../utils/i18nUtils';
import {attributeBinding} from './attributeBinding';

/**
* Mixin for input views handling common concerns like labels,
Expand Down Expand Up @@ -131,6 +132,8 @@ import {attributeTranslationKeys, findTranslation, translationKeysWithSuffix} fr
* @mixin
*/
export const inputView = {
...attributeBinding,

ui: {
label: 'label',
labelText: 'label .name',
Expand Down Expand Up @@ -262,54 +265,7 @@ export const inputView = {
},

updateVisible: function() {
this.$el.toggleClass('input-hidden_via_binding',
this.$el.toggleClass('hidden_via_binding',
this.getBooleanAttributBoundOption('visible') === false);
},

setupBooleanAttributeBinding(optionName, updateMethod) {
this.setupAttributeBinding(optionName, updateMethod, Boolean);
},

getBooleanAttributBoundOption(optionName) {
return this.getAttributeBoundOption(optionName, Boolean);
},

setupAttributeBinding: function(optionName, updateMethod, normalize = value => value) {
const binding = this.options[`${optionName}Binding`];
const view = this;

if (binding) {
_.flatten([binding]).forEach(attribute => {
this.listenTo(this.model, 'change:' + attribute, update);
});
}

update();

function update() {
updateMethod.call(view, view.getAttributeBoundOption(optionName, normalize));
}
},

getAttributeBoundOption(optionName, normalize = value => value) {
const binding = this.options[`${optionName}Binding`];
const bindingValueOptionName = `${optionName}BindingValue`;

const value = Array.isArray(binding) ?
binding.map(attribute => this.model.get(attribute)) :
this.model.get(binding);

if (bindingValueOptionName in this.options) {
return value === this.options[bindingValueOptionName];
}
else if (typeof this.options[optionName] === 'function') {
return normalize(this.options[optionName](value));
}
else if (optionName in this.options) {
return normalize(this.options[optionName]);
}
else if (binding) {
return normalize(value);
}
}
};

0 comments on commit 6b01e4a

Please sign in to comment.