import { dynamicText } from '../../configuration/constants';

export default (editor, opt = {}) => {
  const domc = editor.DomComponents;
  const defaultType = domc.getType('text');
  const defaultModel = defaultType.model;
  const defaultView = defaultType.view;

  // DEMO - 文本
  domc.addType(dynamicText, {
    model: defaultModel.extend({
      defaults: {
        ...defaultModel.prototype.defaults,
        name: "动态文本",
        resizable: true,
        dmode: 'absolute',
        highlightable: false,
        droppable: false,
        traits: [{
          name: 'source',
          label: '属性字段',
          changeProp: 1,
        }],
        "source": "",
        script: function () {
          const source = ('{[ source ]}').toString();

          $(this).attr('data-source', source);
        }
      }
    }, {
      isComponent(el) {
        if (el.getAttribute && el.getAttribute('data-gjs-type') == dynamicText) {
          return { type: dynamicText };
        }
      }
    }),
    view: defaultView.extend({
      init() {
        const evn = 'change:source';
        this.listenTo(this.model, evn, this.updateScript);
        this.listenTo(this.model, evn, this.em.handleUpdates.bind(this.em));
      }
    })
  });
}
