snappymail/dev/Common/HtmlEditor.js

277 lines
5.1 KiB
JavaScript
Raw Normal View History

2014-09-05 06:49:03 +08:00
(function () {
2014-08-25 23:49:01 +08:00
'use strict';
2014-08-20 23:03:12 +08:00
var
2014-08-25 23:49:01 +08:00
window = require('window'),
_ = require('_'),
2014-09-05 06:49:03 +08:00
Globals = require('Common/Globals'),
Settings = require('Storage/Settings')
2014-08-20 23:03:12 +08:00
;
2014-08-25 15:10:51 +08:00
2014-08-20 23:03:12 +08:00
/**
* @constructor
* @param {Object} oElement
* @param {Function=} fOnBlur
* @param {Function=} fOnReady
* @param {Function=} fOnModeChange
*/
2014-08-27 23:59:44 +08:00
function HtmlEditor(oElement, fOnBlur, fOnReady, fOnModeChange)
{
2014-08-25 15:10:51 +08:00
this.editor = null;
this.iBlurTimer = 0;
this.fOnBlur = fOnBlur || null;
this.fOnReady = fOnReady || null;
this.fOnModeChange = fOnModeChange || null;
2014-08-25 15:10:51 +08:00
this.$element = $(oElement);
2014-08-20 23:03:12 +08:00
2014-08-25 15:10:51 +08:00
this.resize = _.throttle(_.bind(this.resize, this), 100);
2014-08-20 23:03:12 +08:00
2014-08-25 15:10:51 +08:00
this.init();
}
2014-08-20 23:03:12 +08:00
2014-08-27 23:59:44 +08:00
HtmlEditor.prototype.blurTrigger = function ()
{
2014-08-20 23:03:12 +08:00
if (this.fOnBlur)
{
var self = this;
2014-08-25 15:10:51 +08:00
window.clearTimeout(this.iBlurTimer);
this.iBlurTimer = window.setTimeout(function () {
2014-08-20 23:03:12 +08:00
self.fOnBlur();
}, 200);
}
};
2014-08-27 23:59:44 +08:00
HtmlEditor.prototype.focusTrigger = function ()
{
2014-08-20 23:03:12 +08:00
if (this.fOnBlur)
{
2014-08-20 23:03:12 +08:00
window.clearTimeout(this.iBlurTimer);
}
2014-08-20 23:03:12 +08:00
};
2014-08-20 23:03:12 +08:00
/**
* @return {boolean}
*/
2014-08-27 23:59:44 +08:00
HtmlEditor.prototype.isHtml = function ()
2014-08-20 23:03:12 +08:00
{
return this.editor ? 'wysiwyg' === this.editor.mode : false;
};
2014-08-20 23:03:12 +08:00
/**
* @return {boolean}
*/
2014-08-27 23:59:44 +08:00
HtmlEditor.prototype.checkDirty = function ()
2014-08-20 23:03:12 +08:00
{
return this.editor ? this.editor.checkDirty() : false;
};
2014-08-27 23:59:44 +08:00
HtmlEditor.prototype.resetDirty = function ()
{
2014-08-20 23:03:12 +08:00
if (this.editor)
{
2014-08-20 23:03:12 +08:00
this.editor.resetDirty();
}
2014-08-20 23:03:12 +08:00
};
/**
* @param {boolean=} bWrapIsHtml = false
2014-08-20 23:03:12 +08:00
* @return {string}
*/
2014-08-27 23:59:44 +08:00
HtmlEditor.prototype.getData = function (bWrapIsHtml)
2014-08-20 23:03:12 +08:00
{
if (this.editor)
{
2014-08-20 23:03:12 +08:00
if ('plain' === this.editor.mode && this.editor.plugins.plain && this.editor.__plain)
{
2014-08-20 23:03:12 +08:00
return this.editor.__plain.getRawData();
}
2014-08-20 23:03:12 +08:00
return bWrapIsHtml ?
'<div data-html-editor-font-wrapper="true" style="font-family: arial, sans-serif; font-size: 13px;">' +
this.editor.getData() + '</div>' : this.editor.getData();
}
2014-08-12 22:27:41 +08:00
2014-08-20 23:03:12 +08:00
return '';
};
2014-08-27 23:59:44 +08:00
HtmlEditor.prototype.modeToggle = function (bPlain)
{
2014-08-20 23:03:12 +08:00
if (this.editor)
{
2014-08-20 23:03:12 +08:00
if (bPlain)
{
if ('plain' === this.editor.mode)
{
this.editor.setMode('wysiwyg');
}
}
else
{
if ('wysiwyg' === this.editor.mode)
{
this.editor.setMode('plain');
}
}
this.resize();
}
2014-08-20 23:03:12 +08:00
};
2014-08-27 23:59:44 +08:00
HtmlEditor.prototype.setHtml = function (sHtml, bFocus)
{
2014-08-20 23:03:12 +08:00
if (this.editor)
2014-02-13 00:01:53 +08:00
{
2014-08-20 23:03:12 +08:00
this.modeToggle(true);
this.editor.setData(sHtml);
2014-08-20 23:03:12 +08:00
if (bFocus)
{
this.focus();
}
}
2014-08-20 23:03:12 +08:00
};
2014-08-27 23:59:44 +08:00
HtmlEditor.prototype.setPlain = function (sPlain, bFocus)
{
2014-08-20 23:03:12 +08:00
if (this.editor)
{
this.modeToggle(false);
if ('plain' === this.editor.mode && this.editor.plugins.plain && this.editor.__plain)
{
return this.editor.__plain.setRawData(sPlain);
}
else
{
this.editor.setData(sPlain);
}
2014-08-14 17:26:58 +08:00
2014-08-20 23:03:12 +08:00
if (bFocus)
{
this.focus();
}
}
};
2014-08-14 17:26:58 +08:00
2014-08-27 23:59:44 +08:00
HtmlEditor.prototype.init = function ()
2014-08-20 23:03:12 +08:00
{
if (this.$element && this.$element[0])
{
var
self = this,
fInit = function () {
2014-08-14 17:26:58 +08:00
2014-08-20 23:03:12 +08:00
var
oConfig = Globals.oHtmlEditorDefaultConfig,
2014-08-27 23:59:44 +08:00
sLanguage = Settings.settingsGet('Language'),
bSource = !!Settings.settingsGet('AllowHtmlEditorSourceButton')
2014-08-20 23:03:12 +08:00
;
2014-08-14 17:26:58 +08:00
2014-08-20 23:03:12 +08:00
if (bSource && oConfig.toolbarGroups && !oConfig.toolbarGroups.__SourceInited)
{
oConfig.toolbarGroups.__SourceInited = true;
oConfig.toolbarGroups.push({name: 'document', groups: ['mode', 'document', 'doctools']});
}
2014-08-14 17:26:58 +08:00
2014-08-20 23:03:12 +08:00
oConfig.enterMode = window.CKEDITOR.ENTER_BR;
oConfig.shiftEnterMode = window.CKEDITOR.ENTER_BR;
2014-08-14 17:26:58 +08:00
2014-08-20 23:03:12 +08:00
oConfig.language = Globals.oHtmlEditorLangsMap[sLanguage] || 'en';
if (window.CKEDITOR.env)
2014-08-14 17:26:58 +08:00
{
2014-08-20 23:03:12 +08:00
window.CKEDITOR.env.isCompatible = true;
2014-08-14 17:26:58 +08:00
}
2014-08-20 23:03:12 +08:00
self.editor = window.CKEDITOR.appendTo(self.$element[0], oConfig);
2014-08-14 17:26:58 +08:00
2014-08-20 23:03:12 +08:00
self.editor.on('key', function(oEvent) {
if (oEvent && oEvent.data && 9 /* Tab */ === oEvent.data.keyCode)
{
return false;
}
});
2014-08-14 17:26:58 +08:00
2014-08-20 23:03:12 +08:00
self.editor.on('blur', function() {
self.blurTrigger();
});
2014-08-14 17:26:58 +08:00
2014-08-20 23:03:12 +08:00
self.editor.on('mode', function() {
2014-08-14 17:26:58 +08:00
2014-08-20 23:03:12 +08:00
self.blurTrigger();
2014-08-14 17:26:58 +08:00
2014-08-20 23:03:12 +08:00
if (self.fOnModeChange)
{
self.fOnModeChange('plain' !== self.editor.mode);
}
});
2014-08-14 17:26:58 +08:00
2014-08-20 23:03:12 +08:00
self.editor.on('focus', function() {
self.focusTrigger();
2014-08-14 17:26:58 +08:00
});
2014-08-20 23:03:12 +08:00
if (self.fOnReady)
{
self.editor.on('instanceReady', function () {
self.editor.setKeystroke(window.CKEDITOR.CTRL + 65 /* A */, 'selectAll');
2014-09-16 04:54:20 +08:00
self.editor.editable().addClass('cke_enable_context_menu');
2014-08-20 23:03:12 +08:00
self.fOnReady();
self.__resizable = true;
self.resize();
});
}
2014-08-14 17:26:58 +08:00
}
2014-08-20 23:03:12 +08:00
;
if (window.CKEDITOR)
{
fInit();
}
else
{
window.__initEditor = fInit;
2014-08-14 17:26:58 +08:00
}
2014-08-20 23:03:12 +08:00
}
};
2014-08-27 23:59:44 +08:00
HtmlEditor.prototype.focus = function ()
2014-08-20 23:03:12 +08:00
{
if (this.editor)
{
2014-08-20 23:03:12 +08:00
this.editor.focus();
}
2014-08-20 23:03:12 +08:00
};
2014-08-27 23:59:44 +08:00
HtmlEditor.prototype.blur = function ()
2014-08-20 23:03:12 +08:00
{
if (this.editor)
{
2014-08-20 23:03:12 +08:00
this.editor.focusManager.blur(true);
}
2014-08-20 23:03:12 +08:00
};
2014-08-27 23:59:44 +08:00
HtmlEditor.prototype.resize = function ()
{
2014-08-20 23:03:12 +08:00
if (this.editor && this.__resizable)
{
try
{
this.editor.resize(this.$element.width(), this.$element.innerHeight());
}
catch (e) {}
}
};
2014-08-27 23:59:44 +08:00
HtmlEditor.prototype.clear = function (bFocus)
{
2014-08-20 23:03:12 +08:00
this.setHtml('', bFocus);
};
2014-08-27 23:59:44 +08:00
module.exports = HtmlEditor;
2014-09-05 06:49:03 +08:00
}());