kai-jacobsen/kontentblocks

View on GitHub
js/src/fields/controls/editor.js

Summary

Maintainability
A
0 mins
Test Coverage
var BaseView = require('../FieldControlBaseView');
var TinyMCE = require('common/TinyMCE');
module.exports = BaseView.extend({
  initialize: function () {
    this.render();
  },
  render: function () {
    var that = this;
    this.$textarea = this.$('textarea');
    var ed = tinymce.get(that.$textarea.attr('id'));
    that.attachEvents(ed);
    tinymce.on('AddEditor', function (event) {
      var editor = event.editor;
      if (editor) {
        that.attachEvents(editor);
      }

    });
  },
  attachEvents: function(editor) {
    var that = this;
    var settings = this.model.toJSON();
    if (editor && editor.id === that.$textarea.attr('id') && !that.editor) {
      that.editor = editor;
      editor.on('change', function () {
        that.update(editor.getContent());
      });

      if (settings.settings && settings.settings.charlimit) {
        var limit = settings.settings.charlimit;
        var $charlimit = that.$('.char-limit');
        editor.on('keyDown SetContent', function (ed, e) {
          var content = this.getContent({format: 'text'});
          var max = limit;
          var len = content.length;
          var rest = len - max;
          if (len >= max) {
            $charlimit.html('<span class="text-error" style="color: red;">Zu viele Zeichen:-' + rest + '</span>');
          } else {
            var charCount = max - len;
            $charlimit.html(charCount + ' Zeichen verbleiben');
          }
        })
      }

    }
  },
  derender: function () {
    this.stopListening();
    this.editor = null;
  },
  update: function (val) {
    this.model.set('value', val);
  },
  toString: function () {
    if (this.editor) {
      try {
        return this.editor.getContent();
      } catch (e) {
        return '';
      }
    }
    return '';
  }
});