Your IP : 13.59.110.86


Current Path : /usr/local/mgr5/skins/orion/src/
Upload File :
Current File : //usr/local/mgr5/skins/orion/src/App.TinyMCE.js

/**
 * TinyMCE connector
 *  @param {object} window  global object
 *  @param {function} $ jQuery library
 *  @param {object} EventMgr EventMgr library
 *  @param {object} App Application
 **/

App.TinyMCE = function(window, $, EventMgr, App) {
  'use strict';
  var CACHE = {},
      mainWrapperSelector = '#main-wrapper';

  function cleanCache(tabId) {
    if (CACHE[tabId] && window.tinymce) {
      var l = CACHE[tabId].length,
          editors = window.tinymce.editors,
          eLen = editors.length,
          eLenTmp, id;

      while (l--) {
        id = CACHE[tabId][l];
        eLenTmp = eLen;
        while (eLenTmp--) {
          if (editors[eLenTmp].id === id) {
            EventMgr.trigger('removeDatafromLocalStorage', { id: id });
            editors[eLenTmp].remove();
          }
        }
      }
      delete CACHE[tabId];
    }
  }
  /**
   * Обновляем контент из вне, по сетвалуес например
   * d = { id, content };
   */
  function setContent(e, d) {
    var editors = window.tinymce.editors,
        l = editors.length;
    while (l--) {
      if (editors[l].id === d.id) {
        editors[l].setContent(d.content);
      }
    }
  }

  function cleanCacheWrapper(e, data) {
    var tabId = data.tabId;
    cleanCache(tabId);
  }

  function initEditor(elems) {
    appendEditor(elems);
  }

  function appendEditor(elems) {
    if (!elems) { return; }
    var l = elems.length,
        id, tabId, height,
        TOOLBAR_HEIGHT = 35 + 34 + 37,
        myplugin;

    while (l--) {
      myplugin = '';
      id = elems[l].getAttribute('id');
      tabId = elems[l].getAttribute('data-tabid');
      height = elems[l].offsetHeight - TOOLBAR_HEIGHT;
      //check for needs plugin
      if (window.localStorage && window.localStorage.getItem(id)) {
        myplugin = 'isp_bill_macro';
      }
      /* jslint loopfunc:true */
      window.tinymce.init({
        selector: '#' + id,
        language: pageInfo.lang,
        resize: false,
        height: height,
        relative_urls: false,
        convert_urls: false,
        setup: function(ed) {
          ed.on('blur', function(e) {
            ed.save();
            $('#' + ed.id).trigger('change');
          });
        },
        plugins: [
          'advlist autolink lists link image charmap print preview anchor',
          'searchreplace visualblocks code fullscreen',
          'insertdatetime media table contextmenu paste ' + myplugin
        ],
        toolbar: 'insertfile undo redo pastetext | fontselect | fontsizeselect | styleselect | bold italic |' +
            ' alignleft aligncenter alignright alignjustify |' +
            ' bullist numlist outdent indent | link image | ' + myplugin
      });
      /* jslint loopfunc:false */
      CACHE[tabId].push(id);
    }
  }

  function loadEditorScript(elems) {
    var file = '/manimg/common/tinymce/tinymce.min.js';
    App.Common.required(file, initEditor, elems);
  }

  function loadEditor(e, data) {
    var tabId = data.tabId,
        editor, textareas;
    cleanCache(tabId);
    CACHE[tabId] = [];
    textareas = $('#cont-' + tabId + ' .b-text-editor');
    if (textareas.length > 0) {
      if (typeof window.tinymce !== 'undefined') {
        appendEditor(textareas);
      } else {
        loadEditorScript(textareas);
      }
    }
  }

  function syncContent(e, data) {
    var tabId = this.getAttribute('data-tabid');
    if (CACHE[tabId] && window.tinymce) {
      var l = CACHE[tabId].length,
          editors = window.tinymce.editors,
          eLen = editors.length,
          eLenTmp, id;

      while (l--) {
        id = CACHE[tabId][l];
        eLenTmp = eLen;
        while (eLenTmp--) {
          if (editors[eLenTmp].id === id) {
            editors[eLenTmp].save();
          }
        }
      }
    }
  }

  function saveVariablesForPlugin(e, data) {
    var tabId = data.tabId,
        id = data.name + '-' + tabId;
    EventMgr.trigger('writeDatatoLocalStorage', {id: id, data: data.vars });
  }

  function init() {
    EventMgr.bind('appendForm', loadEditor);
    EventMgr.bind('closeTabEvent', cleanCacheWrapper);
    EventMgr.on(mainWrapperSelector,
        '.i-button_type_ok, .i-button_type_setvalue', 'click', syncContent);
    EventMgr.bind('preSetValues', syncContent);
    EventMgr.bind('saveVarsTinyMCE', saveVariablesForPlugin);
    EventMgr.bind('TinyMCE_setContent', setContent);
  }

  var api = {
    init: init
  };

  return api;
}(window, $, EventMgr, App);