Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function($, CKEDITOR, window) {
- "use strict";
- function normalizeValue(str) {
- return str.replace(/<br\s?\/>\n?/g, '\n').trim();
- }
- $.widget('lj.editor', jQuery.lj.basicWidget, {
- CKEditor: null,
- options: {
- timerTimeout: 3000,
- globalInterval: 10000,
- selectors: {
- RTEButton: '',
- plainTextButton: '',
- plainTextContainer: '.b-updatepage-postbox',
- RTEContainer: '.b-updatepage-rtebox',
- plainTextArea: '.b-updateform #body'
- }
- },
- _create: function() {
- LJ.console.log('lj.editor create');
- $.lj.basicWidget.prototype._create.apply(this);
- this._setOption('rte', false);
- this._bindControls();
- this._setupGlobalTimer();
- },
- _setOption: function(key, value) {
- if (value !== undefined) {
- this.options[key] = value;
- }
- switch(key) {
- case 'rte':
- if (value === true) {
- if (CKEDITOR && CKEDITOR.env.isCompatible) {
- this._useRTE();
- }
- } else {
- this._usePlainText();
- }
- break;
- }
- },
- _createRTE: function() {
- var self = this;
- LJ.console.log('creating RTE');
- CKEDITOR.styleText = Site.statprefix + '/js/ck/contents.css?t=' + Site.version
- CKEDITOR.basePath = Site.statprefix + '/js/ck/';
- CKEDITOR.timestamp = Site.version;
- $('#rte').append('<div id="ck"></div>');
- CKEDITOR.replace('ck', {
- skin: 'v2',
- baseHref: CKEDITOR.basePath,
- height: 350,
- language: Site.current_lang || 'en'
- })/*.on('instanceReady', function() {
- self.CKEditor = this;
- this.resetDirty();
- self._syncData();
- this.on('dataReady', function() {
- !CKEDITOR.env.ie && this.focus();
- });
- });*/
- },
- _useRTE: function() {
- LJ.console.log('Using RTE');
- if (!this.CKEditor) {
- this._createRTE();
- } else {
- this._syncData();
- }
- },
- _usePlainText: function() {
- LJ.console.log('Using plain text editor');
- this._syncData();
- },
- _RTEData: function(value) {
- if (!this.CKEditor) {
- return "";
- }
- if (value === undefined) {
- return this.CKEditor.getData();
- } else {
- this.CKEditor.setData(value);
- }
- },
- _plainTextData: function(value) {
- if (value === undefined) {
- return this._el('plainTextArea').val();
- } else {
- this._el('plainTextArea').val(value);
- }
- },
- _syncData: function() {
- if (this.options.rte) {
- this._RTEData(this._plainTextData());
- } else {
- this._plainTextData(this._RTEData());
- }
- LJ.console.log('draft updating', this.getData());
- },
- getData: function() {
- LJ.console.log('getting data');
- var data;
- if (this.options.rte) {
- data = this._RTEData();
- } else {
- data = this._plainTextData();
- }
- return normalizeValue(data);
- },
- _setupGlobalTimer: function() {
- if (!this.globalTimer) {
- this.globalTimer = setInterval(this._save.bind(this), this.options.globalInterval);
- }
- },
- _checkTimer: function() {
- if (this.timer) {
- this.timer = clearTimeout(this.timer);
- }
- this.timer = setTimeout(this._save.bind(this), this.options.timerTimeout);
- },
- _save: function() {
- var data = this.getData();
- if (this.lastSaved !== data) {
- this.lastSaved = data;
- LJ.console.log('saving', data);
- }
- },
- _bindControls: function() {
- var self = this;
- var tabs = this.element.find('.b-updatepage-tabs').find('a');
- tabs.bind("click", function() {
- tabs.parent().siblings().removeClass('b-updatepage-tab-active');
- $(this).parent().addClass('b-updatepage-tab-active');
- });
- tabs.last().click(function() {
- self._setOption('rte', false);
- self._el('RTEContainer').hide();
- self._el('plainTextContainer').show();
- });
- tabs.first().click(function() {
- alert('aaa!');
- self._setOption('rte', true);
- LJ.console.log('enabling rte');
- self._el('RTEContainer').show();
- self._el('plainTextContainer').hide();
- });
- $(document).submit(function(e) {
- LJ.console.log('sumbit');
- self._plainTextData(self.getData());
- e.preventDefault();
- return;
- });
- $('.b-updatepage-event-section').keyup(function() {
- self._checkTimer();
- });
- },
- _isDirty: function() {
- return this.lastSaved !== this.getData();
- }
- });
- })(jQuery, CKEDITOR);
Add Comment
Please, Sign In to add comment