Browse Source

Fix class removing from Components. Fixes #661

pull/712/head
Artur Arseniev 9 years ago
parent
commit
d2198ca634
  1. 4
      src/commands/index.js
  2. 13
      src/dom_components/model/Component.js
  3. 11
      src/dom_components/view/ComponentView.js
  4. 2
      src/selector_manager/view/ClassTagView.js
  5. 9
      src/selector_manager/view/ClassTagsView.js
  6. 4
      src/style_manager/view/SectorsView.js
  7. 7
      test/specs/dom_components/model/Component.js
  8. 13
      test/specs/dom_components/view/ComponentV.js
  9. 10
      test/specs/selector_manager/view/ClassTagView.js

4
src/commands/index.js

@ -139,7 +139,7 @@ module.exports = () => {
var collection = sel.collection; var collection = sel.collection;
var index = collection.indexOf(sel); var index = collection.indexOf(sel);
collection.add(sel.clone(), {at: index + 1}); collection.add(sel.clone(), {at: index + 1});
ed.trigger('component:update', sel); sel.emitUpdate()
}, },
}; };
@ -164,7 +164,7 @@ module.exports = () => {
const onEnd = (e, opts) => { const onEnd = (e, opts) => {
em.runDefault(); em.runDefault();
em.set('selectedComponent', sel); em.set('selectedComponent', sel);
ed.trigger('component:update', sel); sel.emitUpdate()
dragger && dragger.blur(); dragger && dragger.blur();
}; };

13
src/dom_components/model/Component.js

@ -168,6 +168,8 @@ module.exports = Backbone.Model.extend(Styleable).extend({
this.initComponents(); this.initComponents();
this.initToolbar(); this.initToolbar();
this.set('status', ''); this.set('status', '');
this.listenTo(this.get('classes'), 'add remove change',
() => this.emitUpdate('classes'));
this.init(); this.init();
}, },
@ -282,7 +284,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({
*/ */
getAttributes() { getAttributes() {
const classes = []; const classes = [];
const attributes = this.get('attributes') || {}; const attributes = { ...this.get('attributes') };
// Add classes // Add classes
this.get('classes').each(cls => classes.push(cls.get('name'))); this.get('classes').each(cls => classes.push(cls.get('name')));
@ -723,7 +725,14 @@ module.exports = Backbone.Model.extend(Styleable).extend({
}) })
return scr; return scr;
} },
emitUpdate(property) {
const em = this.em;
const event = 'component:update' + (property ? `:${property}` : '');
em && em.trigger(event, this.model);
},
},{ },{

11
src/dom_components/view/ComponentView.js

@ -56,14 +56,11 @@ module.exports = Backbone.View.extend({
* @private * @private
*/ */
handleChange() { handleChange() {
var em = this.em; const model = this.model;
if(em) { model.emitUpdate();
var model = this.model;
em.trigger('component:update', model);
for(var prop in model.changed) { for (let prop in model.changed) {
em.trigger('component:update:' + prop, model); model.emitUpdate(prop);
}
} }
}, },

2
src/selector_manager/view/ClassTagView.js

@ -89,7 +89,6 @@ module.exports = require('backbone').View.extend({
*/ */
changeStatus() { changeStatus() {
this.model.set('active', !this.model.get('active')); this.model.set('active', !this.model.get('active'));
this.target.trigger('targetClassUpdated');
}, },
@ -107,7 +106,6 @@ module.exports = require('backbone').View.extend({
sel && sel.get & sel.get('classes').remove(model); sel && sel.get & sel.get('classes').remove(model);
coll && coll.remove(model); coll && coll.remove(model);
setTimeout(() => this.remove(), 0); setTimeout(() => this.remove(), 0);
em && em.trigger('targetClassRemoved');
}, },

9
src/selector_manager/view/ClassTagsView.js

@ -52,7 +52,7 @@ module.exports = Backbone.View.extend({
this.em = this.target; this.em = this.target;
this.listenTo(this.target ,'change:selectedComponent',this.componentChanged); this.listenTo(this.target ,'change:selectedComponent',this.componentChanged);
this.listenTo(this.target, 'targetClassUpdated', this.updateSelector); this.listenTo(this.target, 'component:update:classes', this.updateSelector);
this.listenTo(this.collection, 'add', this.addNew); this.listenTo(this.collection, 'add', this.addNew);
this.listenTo(this.collection, 'reset', this.renderClasses); this.listenTo(this.collection, 'reset', this.renderClasses);
@ -190,8 +190,6 @@ module.exports = Backbone.View.extend({
stateChanged(e) { stateChanged(e) {
if(this.compTarget){ if(this.compTarget){
this.compTarget.set('state', this.$states.val()); this.compTarget.set('state', this.$states.val());
if(this.target)
this.target.trigger('targetStateUpdated');
this.updateSelector(); this.updateSelector();
} }
}, },
@ -219,11 +217,6 @@ module.exports = Backbone.View.extend({
compCls.add(model); compCls.add(model);
var lenA = compCls.length; var lenA = compCls.length;
this.collection.add(model); this.collection.add(model);
if (lenA > lenB) {
target.trigger('targetClassAdded');
}
this.updateStateVis(); this.updateStateVis();
} }
} }

4
src/style_manager/view/SectorsView.js

@ -19,10 +19,10 @@ module.exports = Backbone.View.extend({
body.removeChild(dummy); body.removeChild(dummy);
this.propTarget = target; this.propTarget = target;
const coll = this.collection; const coll = this.collection;
const events = 'change:selectedComponent component:update:classes change:device';
this.listenTo(coll, 'add', this.addTo); this.listenTo(coll, 'add', this.addTo);
this.listenTo(coll, 'reset', this.render); this.listenTo(coll, 'reset', this.render);
this.listenTo(this.target, 'change:selectedComponent targetClassAdded targetClassRemoved targetClassUpdated ' + this.listenTo(this.target, events, this.targetUpdated);
'targetStateUpdated targetStyleUpdated change:device', this.targetUpdated);
}, },

7
test/specs/dom_components/model/Component.js

@ -198,6 +198,13 @@ module.exports = {
expect(result.length).toEqual(2); expect(result.length).toEqual(2);
}); });
it('removeClass actually removes classes from attributes', () => {
obj.addClass('class1');
obj.removeClass('class1');
const result = obj.getAttributes();
expect(result.class).toEqual(undefined);
});
it('setAttributes', () => { it('setAttributes', () => {
obj.setAttributes({ obj.setAttributes({
id: 'test', id: 'test',

13
test/specs/dom_components/view/ComponentV.js

@ -1,6 +1,7 @@
const ComponentView = require('dom_components/view/ComponentView'); const ComponentView = require('dom_components/view/ComponentView');
const Component = require('dom_components/model/Component'); const Component = require('dom_components/model/Component');
const DomComponents = require('dom_components'); const DomComponents = require('dom_components');
const Editor = require('editor/model/Editor');
module.exports = { module.exports = {
run() { run() {
@ -13,13 +14,16 @@ module.exports = {
var hClass = 'hc-state'; var hClass = 'hc-state';
var dcomp; var dcomp;
var compOpts; var compOpts;
let em;
beforeEach(() => { beforeEach(() => {
em = new Editor({});
dcomp = new DomComponents(); dcomp = new DomComponents();
compOpts = { compOpts = {
em,
componentTypes: dcomp.componentTypes, componentTypes: dcomp.componentTypes,
}; };
model = new Component(); model = new Component({}, compOpts);
view = new ComponentView({ view = new ComponentView({
model model
}); });
@ -121,6 +125,13 @@ module.exports = {
expect(view.$el.html()).toEqual('<span data-highlightable="1"></span><div title="test" data-highlightable="1"></div>'); expect(view.$el.html()).toEqual('<span data-highlightable="1"></span><div title="test" data-highlightable="1"></div>');
}); });
it('removeClass removes classes from attributes', () => {
model.addClass('class1');
model.removeClass('class1');
const result = model.getAttributes();
expect(result.class).toEqual(undefined);
});
}); });
} }
}; };

10
test/specs/selector_manager/view/ClassTagView.js

@ -69,14 +69,6 @@ module.exports = {
setTimeout(() => expect(fixtures.innerHTML).toNotExist(), 0) setTimeout(() => expect(fixtures.innerHTML).toNotExist(), 0)
}); });
it('On remove triggers event', () => {
var spy = sinon.spy();
sinon.stub(obj.target, 'get').returns(0);
obj.target.on("targetClassRemoved", spy);
obj.$el.find('#close').trigger('click');
expect(spy.called).toEqual(true);
});
it('Checkbox toggles status', () => { it('Checkbox toggles status', () => {
var spy = sinon.spy(); var spy = sinon.spy();
obj.model.on("change:active", spy); obj.model.on("change:active", spy);
@ -89,7 +81,7 @@ module.exports = {
it('On toggle triggers event', () => { it('On toggle triggers event', () => {
var spy = sinon.spy(); var spy = sinon.spy();
sinon.stub(obj.target, 'get').returns(0); sinon.stub(obj.target, 'get').returns(0);
obj.target.on("targetClassUpdated", spy); obj.target.on("component:update:classes", spy);
obj.$el.find('#checkbox').trigger('click'); obj.$el.find('#checkbox').trigger('click');
expect(spy.called).toEqual(true); expect(spy.called).toEqual(true);
}); });

Loading…
Cancel
Save