Browse Source

added method to disable/enable the button, using specific class for disabled buttons, extracted toogleActive method to improve testability

pull/479/head
Arthur Almeida 9 years ago
parent
commit
6ecbc229fc
  1. 20
      src/panels/view/ButtonView.js
  2. 31
      test/specs/panels/view/ButtonView.js

20
src/panels/view/ButtonView.js

@ -13,6 +13,7 @@ module.exports = Backbone.View.extend({
this.ppfx = this.config.pStylePrefix || ''; this.ppfx = this.config.pStylePrefix || '';
this.id = this.pfx + this.model.get('id'); this.id = this.pfx + this.model.get('id');
this.activeCls = this.pfx + 'active'; this.activeCls = this.pfx + 'active';
this.disableCls = this.pfx + 'disable';
this.btnsVisCls = this.pfx + 'visible'; this.btnsVisCls = this.pfx + 'visible';
this.parentM = o.parentM || null; this.parentM = o.parentM || null;
this.className = this.pfx + 'btn' + (cls ? ' ' + cls : ''); this.className = this.pfx + 'btn' + (cls ? ' ' + cls : '');
@ -21,6 +22,7 @@ module.exports = Backbone.View.extend({
this.listenTo(this.model, 'change:bntsVis', this.updateBtnsVis); this.listenTo(this.model, 'change:bntsVis', this.updateBtnsVis);
this.listenTo(this.model, 'change:attributes', this.updateAttributes); this.listenTo(this.model, 'change:attributes', this.updateAttributes);
this.listenTo(this.model, 'change:className', this.updateClassName); this.listenTo(this.model, 'change:className', this.updateClassName);
this.listenTo(this.model, 'change:disable', this.updateDisable);
if(this.model.get('buttons').length){ if(this.model.get('buttons').length){
this.$el.on('mousedown', this.startTimer); this.$el.on('mousedown', this.startTimer);
@ -234,6 +236,15 @@ module.exports = Backbone.View.extend({
} }
}, },
updateDisable() {
if(this.model.get('disable')) {
this.$el.addClass(this.disableCls);
} else {
this.$el.removeClass(this.disableCls);
}
},
/** /**
* Update active style status * Update active style status
* *
@ -255,9 +266,18 @@ module.exports = Backbone.View.extend({
clicked(e) { clicked(e) {
if(this.model.get('bntsVis') ) if(this.model.get('bntsVis') )
return; return;
if(this.model.get('disable') )
return;
this.toogleActive();
},
toogleActive() {
if(this.parentM) if(this.parentM)
this.swapParent(); this.swapParent();
var active = this.model.get('active'); var active = this.model.get('active');
this.model.set('active', !active); this.model.set('active', !active);

31
test/specs/panels/view/ButtonView.js

@ -14,7 +14,7 @@ module.exports = {
beforeEach(() => { beforeEach(() => {
model = new Button(); model = new Button();
view = new ButtonView({ view = new ButtonView({
model model: model
}); });
document.body.innerHTML = '<div id="fixtures"></div>'; document.body.innerHTML = '<div id="fixtures"></div>';
fixtures = document.body.querySelector('#fixtures'); fixtures = document.body.querySelector('#fixtures');
@ -55,6 +55,35 @@ module.exports = {
expect(view.el.getAttribute('class')).toEqual(btnClass); expect(view.el.getAttribute('class')).toEqual(btnClass);
}); });
it('Disable the button', () => {
model.set('disable', true, {silent: true});
view.updateDisable();
expect(view.el.getAttribute('class')).toEqual(btnClass + ' disable');
});
it('Enable the disabled button', () => {
model.set('disable', true, {silent: true});
view.updateDisable();
expect(view.el.getAttribute('class')).toEqual(btnClass + ' disable');
model.set('disable', false, {silent: true});
view.updateDisable();
expect(view.el.getAttribute('class')).toEqual(btnClass);
});
it('Cancels the click action when button is disabled', () => {
const stub = sinon.stub(view, 'toogleActive');
model.set('disable', true, {silent: true});
view.clicked();
expect(stub.called).toEqual(false);
});
it('Enable the click action when button is enable', () => {
const stub = sinon.stub(view, 'toogleActive');
model.set('disable', false, {silent: true});
view.clicked();
expect(stub.called).toEqual(true);
});
it('Renders correctly', () => { it('Renders correctly', () => {
expect(view.render()).toExist(); expect(view.render()).toExist();
}); });

Loading…
Cancel
Save