-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make visbileBinding work for InfoBoxView
REDMINE-20438
- Loading branch information
Showing
8 changed files
with
112 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters