Guest User

Untitled

a guest
Aug 10th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. (function($, CKEDITOR, window) {
  2. "use strict";
  3.  
  4. function normalizeValue(str) {
  5. return str.replace(/<br\s?\/>\n?/g, '\n').trim();
  6. }
  7.  
  8. $.widget('lj.editor', jQuery.lj.basicWidget, {
  9. CKEditor: null,
  10.  
  11. options: {
  12. timerTimeout: 3000,
  13. globalInterval: 10000,
  14.  
  15. selectors: {
  16. RTEButton: '',
  17. plainTextButton: '',
  18.  
  19. plainTextContainer: '.b-updatepage-postbox',
  20. RTEContainer: '.b-updatepage-rtebox',
  21.  
  22. plainTextArea: '.b-updateform #body'
  23. }
  24. },
  25.  
  26. _create: function() {
  27. LJ.console.log('lj.editor create');
  28.  
  29. $.lj.basicWidget.prototype._create.apply(this);
  30.  
  31. this._setOption('rte', false);
  32.  
  33. this._bindControls();
  34. this._setupGlobalTimer();
  35. },
  36.  
  37. _setOption: function(key, value) {
  38. if (value !== undefined) {
  39. this.options[key] = value;
  40. }
  41.  
  42. switch(key) {
  43. case 'rte':
  44. if (value === true) {
  45. if (CKEDITOR && CKEDITOR.env.isCompatible) {
  46. this._useRTE();
  47. }
  48. } else {
  49. this._usePlainText();
  50. }
  51. break;
  52. }
  53. },
  54.  
  55. _createRTE: function() {
  56. var self = this;
  57.  
  58. LJ.console.log('creating RTE');
  59.  
  60. CKEDITOR.styleText = Site.statprefix + '/js/ck/contents.css?t=' + Site.version
  61. CKEDITOR.basePath = Site.statprefix + '/js/ck/';
  62. CKEDITOR.timestamp = Site.version;
  63.  
  64. $('#rte').append('<div id="ck"></div>');
  65. CKEDITOR.replace('ck', {
  66. skin: 'v2',
  67. baseHref: CKEDITOR.basePath,
  68. height: 350,
  69. language: Site.current_lang || 'en'
  70. })/*.on('instanceReady', function() {
  71. self.CKEditor = this;
  72.  
  73. this.resetDirty();
  74. self._syncData();
  75.  
  76. this.on('dataReady', function() {
  77. !CKEDITOR.env.ie && this.focus();
  78. });
  79. });*/
  80. },
  81.  
  82. _useRTE: function() {
  83. LJ.console.log('Using RTE');
  84. if (!this.CKEditor) {
  85. this._createRTE();
  86. } else {
  87. this._syncData();
  88. }
  89. },
  90.  
  91. _usePlainText: function() {
  92. LJ.console.log('Using plain text editor');
  93. this._syncData();
  94. },
  95.  
  96. _RTEData: function(value) {
  97. if (!this.CKEditor) {
  98. return "";
  99. }
  100.  
  101. if (value === undefined) {
  102. return this.CKEditor.getData();
  103. } else {
  104. this.CKEditor.setData(value);
  105. }
  106. },
  107.  
  108. _plainTextData: function(value) {
  109. if (value === undefined) {
  110. return this._el('plainTextArea').val();
  111. } else {
  112. this._el('plainTextArea').val(value);
  113. }
  114. },
  115.  
  116. _syncData: function() {
  117. if (this.options.rte) {
  118. this._RTEData(this._plainTextData());
  119. } else {
  120. this._plainTextData(this._RTEData());
  121. }
  122.  
  123. LJ.console.log('draft updating', this.getData());
  124. },
  125.  
  126. getData: function() {
  127. LJ.console.log('getting data');
  128. var data;
  129.  
  130. if (this.options.rte) {
  131. data = this._RTEData();
  132. } else {
  133. data = this._plainTextData();
  134. }
  135.  
  136. return normalizeValue(data);
  137. },
  138.  
  139. _setupGlobalTimer: function() {
  140. if (!this.globalTimer) {
  141. this.globalTimer = setInterval(this._save.bind(this), this.options.globalInterval);
  142. }
  143. },
  144.  
  145. _checkTimer: function() {
  146. if (this.timer) {
  147. this.timer = clearTimeout(this.timer);
  148. }
  149.  
  150. this.timer = setTimeout(this._save.bind(this), this.options.timerTimeout);
  151. },
  152.  
  153. _save: function() {
  154. var data = this.getData();
  155.  
  156. if (this.lastSaved !== data) {
  157. this.lastSaved = data;
  158. LJ.console.log('saving', data);
  159. }
  160. },
  161.  
  162. _bindControls: function() {
  163. var self = this;
  164.  
  165. var tabs = this.element.find('.b-updatepage-tabs').find('a');
  166.  
  167. tabs.bind("click", function() {
  168. tabs.parent().siblings().removeClass('b-updatepage-tab-active');
  169. $(this).parent().addClass('b-updatepage-tab-active');
  170. });
  171.  
  172. tabs.last().click(function() {
  173. self._setOption('rte', false);
  174.  
  175. self._el('RTEContainer').hide();
  176. self._el('plainTextContainer').show();
  177. });
  178. tabs.first().click(function() {
  179. alert('aaa!');
  180. self._setOption('rte', true);
  181. LJ.console.log('enabling rte');
  182.  
  183. self._el('RTEContainer').show();
  184. self._el('plainTextContainer').hide();
  185. });
  186.  
  187. $(document).submit(function(e) {
  188. LJ.console.log('sumbit');
  189.  
  190. self._plainTextData(self.getData());
  191.  
  192. e.preventDefault();
  193. return;
  194. });
  195.  
  196. $('.b-updatepage-event-section').keyup(function() {
  197. self._checkTimer();
  198. });
  199. },
  200.  
  201. _isDirty: function() {
  202. return this.lastSaved !== this.getData();
  203. }
  204. });
  205. })(jQuery, CKEDITOR);
Add Comment
Please, Sign In to add comment