Browse Source

Convert ClassTagView to ts

pull/4306/head
Alex 4 years ago
parent
commit
ffd233c2fd
  1. 10
      src/selector_manager/index.js
  2. 58
      src/selector_manager/view/ClassTagView.ts
  3. 8
      test/specs/selector_manager/view/ClassTagView.js

10
src/selector_manager/index.js

@ -133,7 +133,10 @@ export default () => {
// Global selectors container // Global selectors container
this.all = new Selectors(config.selectors); this.all = new Selectors(config.selectors);
this.selected = new Selectors([], { em, config }); this.selected = new Selectors([], { em, config });
this.states = new Collection(config.states, { model: State }); this.states = new Collection(
config.states.map(state => new State(state)),
{ model: State }
);
this.model = new Model({ cFirst: config.componentFirst, _undo: true }); this.model = new Model({ cFirst: config.componentFirst, _undo: true });
this.__initListen({ this.__initListen({
collections: [this.states, this.selected], collections: [this.states, this.selected],
@ -352,7 +355,10 @@ export default () => {
* ]); * ]);
*/ */
setStates(states, opts) { setStates(states, opts) {
return this.states.reset(states, opts); return this.states.reset(
states.map(state => new State(state)),
opts
);
}, },
/** /**

58
src/selector_manager/view/ClassTagView.js → src/selector_manager/view/ClassTagView.ts

@ -1,11 +1,12 @@
import { View } from '../../common'; import { View } from "../../common";
import State from "../model/State";
const inputProp = 'contentEditable'; const inputProp = "contentEditable";
export default class ClassTagView extends View { export default class ClassTagView extends View<State> {
template() { template() {
const { pfx, model, config } = this; const { pfx, model, config } = this;
const label = model.get('label') || ''; const label = model.get("label") || "";
return ` return `
<span id="${pfx}checkbox" class="${pfx}tag-status" data-tag-status></span> <span id="${pfx}checkbox" class="${pfx}tag-status" data-tag-status></span>
@ -18,22 +19,30 @@ export default class ClassTagView extends View {
events() { events() {
return { return {
'click [data-tag-remove]': 'removeTag', "click [data-tag-remove]": "removeTag",
'click [data-tag-status]': 'changeStatus', "click [data-tag-status]": "changeStatus",
'dblclick [data-tag-name]': 'startEditTag', "dblclick [data-tag-name]": "startEditTag",
'focusout [data-tag-name]': 'endEditTag', "focusout [data-tag-name]": "endEditTag",
}; };
} }
config: any;
initialize(o = {}) { module: any;
coll: any;
pfx: any;
ppfx: any;
em: any;
inputEl?: HTMLElement;
constructor(o: any = {}) {
super(o);
const config = o.config || {}; const config = o.config || {};
this.config = config; this.config = config;
this.module = o.module; this.module = o.module;
this.coll = o.coll || null; this.coll = o.coll || null;
this.pfx = config.stylePrefix || ''; this.pfx = config.stylePrefix || "";
this.ppfx = config.pStylePrefix || ''; this.ppfx = config.pStylePrefix || "";
this.em = config.em; this.em = config.em;
this.listenTo(this.model, 'change:active', this.updateStatus); this.listenTo(this.model, "change:active", this.updateStatus);
} }
/** /**
@ -42,7 +51,7 @@ export default class ClassTagView extends View {
*/ */
getInputEl() { getInputEl() {
if (!this.inputEl) { if (!this.inputEl) {
this.inputEl = this.el.querySelector('[data-tag-name]'); this.inputEl = this.el.querySelector("[data-tag-name]") as HTMLElement;
} }
return this.inputEl; return this.inputEl;
@ -55,7 +64,8 @@ export default class ClassTagView extends View {
startEditTag() { startEditTag() {
const { em } = this; const { em } = this;
const inputEl = this.getInputEl(); const inputEl = this.getInputEl();
inputEl[inputProp] = true; inputEl;
inputEl[inputProp] = "true";
inputEl.focus(); inputEl.focus();
em && em.setEditing(1); em && em.setEditing(1);
} }
@ -70,15 +80,15 @@ export default class ClassTagView extends View {
const inputEl = this.getInputEl(); const inputEl = this.getInputEl();
const label = inputEl.textContent; const label = inputEl.textContent;
const em = this.em; const em = this.em;
const sm = em && em.get('SelectorManager'); const sm = em && em.get("SelectorManager");
inputEl[inputProp] = false; inputEl[inputProp] = "false";
em && em.setEditing(0); em && em.setEditing(0);
if (sm) { if (sm) {
const name = sm.escapeName(label); const name = sm.escapeName(label);
if (sm.get(name)) { if (sm.get(name)) {
inputEl.innerText = model.get('label'); inputEl.innerText = model.get("label");
} else { } else {
model.set({ name, label }); model.set({ name, label });
} }
@ -91,7 +101,7 @@ export default class ClassTagView extends View {
*/ */
changeStatus() { changeStatus() {
const { model } = this; const { model } = this;
model.set('active', !model.get('active')); model.set("active", !model.get("active"));
} }
/** /**
@ -110,14 +120,14 @@ export default class ClassTagView extends View {
updateStatus() { updateStatus() {
const { model, $el, config } = this; const { model, $el, config } = this;
const { iconTagOn, iconTagOff } = config; const { iconTagOn, iconTagOff } = config;
const $chk = $el.find('[data-tag-status]'); const $chk = $el.find("[data-tag-status]");
if (model.get('active')) { if (model.get("active")) {
$chk.html(iconTagOn); $chk.html(iconTagOn);
$el.removeClass('opac50'); $el.removeClass("opac50");
} else { } else {
$chk.html(iconTagOff); $chk.html(iconTagOff);
$el.addClass('opac50'); $el.addClass("opac50");
} }
} }
@ -125,7 +135,7 @@ export default class ClassTagView extends View {
const pfx = this.pfx; const pfx = this.pfx;
const ppfx = this.ppfx; const ppfx = this.ppfx;
this.$el.html(this.template()); this.$el.html(this.template());
this.$el.attr('class', `${pfx}tag ${ppfx}three-bg`); this.$el.attr("class", `${pfx}tag ${ppfx}three-bg`);
this.updateStatus(); this.updateStatus();
return this; return this;
} }

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

@ -15,12 +15,12 @@ describe('ClassTagView', () => {
em = new EditorModel(); em = new EditorModel();
var model = coll.add({ var model = coll.add({
name: 'test', name: 'test',
label: testLabel label: testLabel,
}); });
obj = new ClassTagView({ obj = new ClassTagView({
config: { em }, config: { em },
model, model,
coll coll,
}); });
//obj.target = { get() {} }; //obj.target = { get() {} };
//_.extend(obj.target, Backbone.Events); //_.extend(obj.target, Backbone.Events);
@ -82,12 +82,12 @@ describe('ClassTagView', () => {
test('On double click label input is enable', () => { test('On double click label input is enable', () => {
obj.$el.find('#tag-label').trigger('dblclick'); obj.$el.find('#tag-label').trigger('dblclick');
expect(obj.getInputEl().contentEditable).toEqual(true); expect(obj.getInputEl().contentEditable).toEqual('true');
}); });
test('On blur label input turns back disabled', () => { test('On blur label input turns back disabled', () => {
obj.$el.find('#tag-label').trigger('dblclick'); obj.$el.find('#tag-label').trigger('dblclick');
obj.endEditTag(); obj.endEditTag();
expect(obj.getInputEl().contentEditable).toEqual(false); expect(obj.getInputEl().contentEditable).toEqual('false');
}); });
}); });

Loading…
Cancel
Save