constructor()

in public/src/js/models/article/editor.js [19:73]


    constructor(opts, article, all, defaults) {
        super();
        var {key, type, validator} = opts;
        var meta = article.meta[key] || ko.observable(defaults.meta);
        var field = article.fields[key] || ko.observable(defaults.field);

        this.all = all;
        this.article = article;
        this.field = field;
        this.items = [];
        this.key = key;
        this.meta = meta;
        this.opts = opts;
        this.type = type;
        this.label = opts.label + (opts.labelState ? ': ' + _.result(article.state, opts.labelState) : '');
        this.dropImage = ko.observable(!!opts.dropImage);
        this.underDrag = ko.observable(false);
        this.performValidation = true;

        if (type === 'list') {
            this.items = _.chain(opts.length)
                .times(i => Editor.create(opts.item, this.article, all, {
                    meta: (this.article.meta[key]() || [])[i]
                }))
                .filter(Boolean)
                .each(editor => this.subscribeOn(editor.meta, () => {
                    meta(this.items.map(item => item.meta()));
                }))
                .value();
        } else if (type === 'text') {
            this.listenOn(mediator, 'ui:open', this.onUIOpen);
        }

        this.hasFocus = ko.observable(false).extend({ rateLimit: 150 });
        this.length = ko.pureComputed(() => {
            return opts.maxLength ? opts.maxLength - this.value().length : undefined;
        });
        this.lengthAlert = ko.pureComputed(() => {
            return opts.maxLength && this.value().length > opts.maxLength;
        });
        this.displayEditor = ko.pureComputed(this.isVisible, this);
        this.updateText = ko.pureComputed({
            read: this.value,
            write: this.writeText,
            owner: this
        });

        if (validator && _.isFunction(this[validator.fn])) {
            this.subscribeOn(meta, () => {
                if (this.performValidation !== false) {
                    this[validator.fn](validator.params, meta);
                }
            });
        }
    }