[2] | 1 | function cRichTextEditor(){ |
---|
[5083] | 2 | this.emwindow = new Array; |
---|
| 3 | this.editor = "body_1"; |
---|
| 4 | this.table = ""; |
---|
| 5 | this.id = "1"; |
---|
| 6 | this.saveFlag = 0; |
---|
| 7 | this.signatures = false; |
---|
| 8 | this.replyController = false; |
---|
| 9 | this.newImageId = false; |
---|
| 10 | this.plain = new Array; |
---|
| 11 | this.editorReady = true; |
---|
[5973] | 12 | this.hoho = ''; |
---|
[2] | 13 | } |
---|
| 14 | |
---|
[5083] | 15 | // This code was written by Tyler Akins and has been placed in the |
---|
| 16 | // public domain. It would be nice if you left this header intact. |
---|
| 17 | // Base64 code from Tyler Akins -- http://rumkin.com |
---|
[320] | 18 | |
---|
[5083] | 19 | var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; |
---|
[2] | 20 | |
---|
[5083] | 21 | var ua = navigator.userAgent.toLowerCase(); |
---|
| 22 | if (ua.indexOf(" chrome/") >= 0 || ua.indexOf(" firefox/") >= 0 || ua.indexOf(' gecko/') >= 0) { |
---|
| 23 | var StringMaker = function () { |
---|
| 24 | this.str = ""; |
---|
| 25 | this.length = 0; |
---|
| 26 | this.append = function (s) { |
---|
| 27 | this.str += s; |
---|
| 28 | this.length += s.length; |
---|
| 29 | } |
---|
| 30 | this.prepend = function (s) { |
---|
| 31 | this.str = s + this.str; |
---|
| 32 | this.length += s.length; |
---|
| 33 | } |
---|
| 34 | this.toString = function () { |
---|
| 35 | return this.str; |
---|
| 36 | } |
---|
| 37 | } |
---|
| 38 | } else { |
---|
| 39 | var StringMaker = function () { |
---|
| 40 | this.parts = []; |
---|
| 41 | this.length = 0; |
---|
| 42 | this.append = function (s) { |
---|
| 43 | this.parts.push(s); |
---|
| 44 | this.length += s.length; |
---|
| 45 | } |
---|
| 46 | this.prepend = function (s) { |
---|
| 47 | this.parts.unshift(s); |
---|
| 48 | this.length += s.length; |
---|
| 49 | } |
---|
| 50 | this.toString = function () { |
---|
| 51 | return this.parts.join(''); |
---|
| 52 | } |
---|
| 53 | } |
---|
[320] | 54 | } |
---|
| 55 | |
---|
[5083] | 56 | cRichTextEditor.prototype.fromJSON = function( value ) |
---|
[320] | 57 | { |
---|
[5911] | 58 | if(!value) |
---|
| 59 | return ''; |
---|
| 60 | return (new Function( "return " + this.decode64( value )))(); |
---|
[5083] | 61 | } |
---|
[790] | 62 | |
---|
[5083] | 63 | cRichTextEditor.prototype.decode64 = function(input) { |
---|
| 64 | if( typeof input === "undefined" ) return ''; |
---|
[4309] | 65 | |
---|
[5083] | 66 | var output = new StringMaker(); |
---|
| 67 | var chr1, chr2, chr3; |
---|
| 68 | var enc1, enc2, enc3, enc4; |
---|
| 69 | var i = 0; |
---|
[790] | 70 | |
---|
[5083] | 71 | // remove all characters that are not A-Z, a-z, 0-9, +, /, or = |
---|
| 72 | input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); |
---|
[4291] | 73 | |
---|
[5083] | 74 | while (i < input.length) { |
---|
| 75 | enc1 = keyStr.indexOf(input.charAt(i++)); |
---|
| 76 | enc2 = keyStr.indexOf(input.charAt(i++)); |
---|
| 77 | enc3 = keyStr.indexOf(input.charAt(i++)); |
---|
| 78 | enc4 = keyStr.indexOf(input.charAt(i++)); |
---|
[4291] | 79 | |
---|
[5083] | 80 | chr1 = (enc1 << 2) | (enc2 >> 4); |
---|
| 81 | chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); |
---|
| 82 | chr3 = ((enc3 & 3) << 6) | enc4; |
---|
[2] | 83 | |
---|
[5083] | 84 | output.append(String.fromCharCode(chr1)); |
---|
[3130] | 85 | |
---|
[5083] | 86 | if (enc3 != 64) { |
---|
| 87 | output.append(String.fromCharCode(chr2)); |
---|
[2] | 88 | } |
---|
[5083] | 89 | if (enc4 != 64) { |
---|
| 90 | output.append(String.fromCharCode(chr3)); |
---|
[2] | 91 | } |
---|
| 92 | } |
---|
[5083] | 93 | |
---|
| 94 | return output.toString(); |
---|
[2] | 95 | } |
---|
| 96 | |
---|
[4291] | 97 | |
---|
[5083] | 98 | cRichTextEditor.prototype.loadEditor = function(ID) { |
---|
[2] | 99 | |
---|
[5083] | 100 | var parentDiv = document.getElementById("body_position_" + ID); |
---|
| 101 | var pObj = "body_" + ID; |
---|
| 102 | var textArea = document.createElement("TEXTAREA"); |
---|
| 103 | textArea.id = pObj; |
---|
| 104 | textArea.style.width = '100%'; |
---|
| 105 | parentDiv.appendChild(textArea); |
---|
| 106 | RichTextEditor.plain[ID] = false; |
---|
| 107 | |
---|
| 108 | if(preferences.plain_text_editor == 1) |
---|
[5417] | 109 | { |
---|
| 110 | RichTextEditor.plain[ID] = true; |
---|
| 111 | RichTextEditor.editorReady = true; |
---|
| 112 | } |
---|
[5083] | 113 | else |
---|
[5417] | 114 | RichTextEditor.active(pObj); |
---|
[2] | 115 | } |
---|
| 116 | |
---|
[5911] | 117 | cRichTextEditor.prototype.loadEditor2 = function(ID) { |
---|
| 118 | var pObj = "body_" + ID; |
---|
| 119 | RichTextEditor.plain[ID] = false; |
---|
| 120 | |
---|
| 121 | if(preferences.plain_text_editor == 1) |
---|
| 122 | { |
---|
| 123 | RichTextEditor.plain[ID] = true; |
---|
| 124 | RichTextEditor.editorReady = true; |
---|
| 125 | } |
---|
| 126 | else |
---|
| 127 | RichTextEditor.active(pObj); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | |
---|
[5083] | 131 | cRichTextEditor.prototype.getSignaturesOptions = function() { |
---|
| 132 | |
---|
| 133 | if(RichTextEditor.signatures !== false) |
---|
| 134 | return RichTextEditor.signatures; |
---|
| 135 | |
---|
| 136 | var signatures = this.fromJSON( preferences.signatures ); |
---|
| 137 | var signature_types = this.fromJSON( preferences.signature_types ); |
---|
[2] | 138 | |
---|
[5083] | 139 | for( key in signatures ) |
---|
| 140 | if( !signature_types[key] ) |
---|
| 141 | signatures[key] = signatures[key].replace( /\n/g, "<br>" ); |
---|
| 142 | |
---|
| 143 | RichTextEditor.signatures = signatures; |
---|
| 144 | return signatures; |
---|
[2] | 145 | |
---|
[271] | 146 | } |
---|
| 147 | |
---|
[5083] | 148 | cRichTextEditor.prototype.getSignatureDefault = function() { |
---|
| 149 | |
---|
| 150 | if(RichTextEditor.signatures === false) |
---|
| 151 | RichTextEditor.signatures = RichTextEditor.getSignaturesOptions(); |
---|
| 152 | |
---|
| 153 | if(!RichTextEditor.signatures || |
---|
| 154 | !RichTextEditor.signatures[preferences.signature_default || ""]) |
---|
| 155 | { |
---|
| 156 | preferences.use_signature = "0"; //Desabilita o uso da assinatura |
---|
| 157 | return ''; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | return unescape(RichTextEditor.signatures[preferences.signature_default]); |
---|
[4472] | 161 | |
---|
[271] | 162 | } |
---|
| 163 | |
---|
| 164 | |
---|
| 165 | |
---|
[5083] | 166 | cRichTextEditor.prototype.execPosInstance = function(inst) { |
---|
[5459] | 167 | if(RichTextEditor.editorReady === false) |
---|
| 168 | { |
---|
[5417] | 169 | var editor = CKEDITOR.instances[inst]; |
---|
| 170 | var id = inst.replace('body_',''); |
---|
[5911] | 171 | var content = $("#content_id_"+id) |
---|
[5417] | 172 | editor.document.on('keydown', function(event) |
---|
| 173 | { |
---|
| 174 | away = false; |
---|
[5911] | 175 | var save_link = content.find(".save"); |
---|
[5924] | 176 | save_link.unbind("click").click(function(){ |
---|
[5911] | 177 | openTab.toPreserve[id] = true;save_msg(id); |
---|
| 178 | }); |
---|
| 179 | save_link.button({ disabled: false }); |
---|
[5417] | 180 | }); |
---|
[5083] | 181 | |
---|
| 182 | |
---|
| 183 | // IM Module Enabled |
---|
| 184 | if( window.parent.loadscript && loadscript.autoStatusIM ) |
---|
| 185 | { |
---|
[5417] | 186 | CKEDITOR.instances[inst].document.on('keydown', function(event){ |
---|
| 187 | loadscript.autoStatusIM; |
---|
| 188 | }); |
---|
[2] | 189 | } |
---|
[5459] | 190 | |
---|
[5417] | 191 | if (preferences.auto_save_draft == 1) |
---|
[5083] | 192 | { |
---|
[5459] | 193 | autoSaveControl.status[id] = true; |
---|
| 194 | autoSaveControl.timer[id] = window.setInterval( "autoSave("+id+")" ,autosave_time); |
---|
| 195 | |
---|
| 196 | CKEDITOR.instances[inst].document.on('keydown', function(event){ |
---|
| 197 | autoSaveControl.status[id] = false; |
---|
| 198 | }) |
---|
| 199 | } |
---|
| 200 | |
---|
[5417] | 201 | $(".cke_editor").css("white-space", "normal"); |
---|
[6754] | 202 | |
---|
| 203 | if(typeof(preferences.font_size_editor) !== 'undefined') |
---|
| 204 | $(editor.document.$.body).css("font-size",preferences.font_size_editor); |
---|
| 205 | if(typeof(preferences.font_family_editor) !== 'undefined') |
---|
| 206 | $(editor.document.$.body).css("font-family",preferences.font_family_editor); |
---|
| 207 | |
---|
| 208 | RichTextEditor.editorReady = true; |
---|
| 209 | } |
---|
[2] | 210 | } |
---|
[5459] | 211 | |
---|
[5083] | 212 | cRichTextEditor.prototype.setPlain = function (active,id){ |
---|
| 213 | RichTextEditor.plain[id] = active; |
---|
[5911] | 214 | var content = $("#content_id_"+id); |
---|
[6715] | 215 | var div = $("<div>").attr("display", "none"); |
---|
[5083] | 216 | if(active === true) |
---|
| 217 | { |
---|
| 218 | CKEDITOR.instances['body_'+id].destroy(); |
---|
| 219 | var height = document.body.scrollHeight; |
---|
| 220 | height -= 330; |
---|
| 221 | $('#body_'+id).height(height); |
---|
[6754] | 222 | div.html($('#body_'+id).val()); |
---|
| 223 | /*Remove as tags html, tabulações e quebras de linha quando for utilizado o editor de texto simples*/ |
---|
| 224 | div.html($.trim(div.text().replace(/[\t]+/g, '').replace(/[\n]+/g, '\n'))); |
---|
| 225 | /*Insere o texto sem formatação no textarea*/ |
---|
[6715] | 226 | $('#body_'+id).val(div.text()); |
---|
[6754] | 227 | |
---|
[5083] | 228 | $('#body_'+id).keydown(function(event) { |
---|
| 229 | away = false; |
---|
[5911] | 230 | save_link = content.find(".save")[0]; |
---|
[5083] | 231 | save_link.onclick = function onclick() {openTab.toPreserve[id] = true;save_msg(id);} ; |
---|
[5417] | 232 | $("#save_message_options_"+id).button({ disabled: false }); |
---|
| 233 | //save_link.className = 'message_options'; |
---|
[5083] | 234 | }); |
---|
[5417] | 235 | $("[name=textplain_rt_checkbox_"+id+"]").button({ disabled: false }); |
---|
[5083] | 236 | } |
---|
[6715] | 237 | else{ |
---|
[5417] | 238 | RichTextEditor.active('body_'+id, id); |
---|
[6754] | 239 | /*Insere somente quebras de linha para que o texto convertido não fique todo em uma linha só*/ |
---|
| 240 | div.html($('#body_'+id).val().replace(/[\n]+/g, '<br>')); |
---|
| 241 | $('#body_'+id).val(div.html()); |
---|
[6715] | 242 | } |
---|
[2] | 243 | } |
---|
| 244 | |
---|
[5083] | 245 | cRichTextEditor.prototype.getData = function (inst){ |
---|
| 246 | var id = inst.replace('body_',''); |
---|
| 247 | |
---|
| 248 | if(RichTextEditor.plain[id] === true) |
---|
| 249 | return $('#'+inst).val(); |
---|
| 250 | else |
---|
| 251 | return CKEDITOR.instances[inst].getData(); |
---|
[2] | 252 | } |
---|
[5083] | 253 | cRichTextEditor.prototype.setData = function (id,data){ |
---|
| 254 | |
---|
[6754] | 255 | if(this.plain[id.replace('body_','')] === true) |
---|
| 256 | $('#'+id).val(data); |
---|
| 257 | else |
---|
| 258 | CKEDITOR.instances[id].setData(data); |
---|
[5083] | 259 | } |
---|
[5973] | 260 | cRichTextEditor.prototype.setInitData = function (id,data,reply,recursion, callback){ |
---|
[5962] | 261 | var content = $("#content_id_"+id); |
---|
| 262 | if(recursion === undefined){ |
---|
| 263 | recursion = 1; |
---|
| 264 | }else{ |
---|
| 265 | recursion++; |
---|
| 266 | } |
---|
| 267 | if(this.plain[id] === true){ |
---|
| 268 | data = data.replace( new RegExp('<pre>((.\n*)*)</pre>'),'$1'); |
---|
| 269 | if($('#'+id) !== undefined){ |
---|
[5083] | 270 | $('#'+id).val(data); |
---|
[5962] | 271 | if (reply === undefined){ |
---|
| 272 | $('#to_'+id).focus(); |
---|
[5485] | 273 | } |
---|
[5962] | 274 | } |
---|
| 275 | else{ |
---|
| 276 | setTimeout(function() {RichTextEditor.setInitData(id,data,reply,recursion); }, 500); |
---|
| 277 | } |
---|
| 278 | } |
---|
| 279 | else{ |
---|
| 280 | if( RichTextEditor.editorReady === true && CKEDITOR.instances['body_'+id] !== undefined ){ |
---|
| 281 | var editor = CKEDITOR.instances['body_'+id]; |
---|
| 282 | var selection = editor.getSelection(); |
---|
| 283 | var fontSize = ''; |
---|
| 284 | var fontFamily = ''; |
---|
| 285 | if(typeof(preferences.font_size_editor) !== 'undefined') |
---|
| 286 | fontSize = 'font-size:' + preferences.font_size_editor; |
---|
| 287 | if(fontSize != '') |
---|
| 288 | fontFamily = ';' |
---|
| 289 | if(typeof(preferences.font_family_editor) !== 'undefined') |
---|
| 290 | fontFamily += 'font-family:' + preferences.font_family_editor + ';'; |
---|
| 291 | var divBr = '<div style="'+fontSize+fontFamily+'"><br type="_moz"></div>'; |
---|
| 292 | |
---|
| 293 | if(selection !== undefined && selection !== null){ |
---|
| 294 | var selectionRanges = selection.getRanges(); |
---|
| 295 | } |
---|
| 296 | if(reply !== undefined){ |
---|
| 297 | if(reply == 'edit') |
---|
| 298 | editor.insertHtml(data); |
---|
| 299 | else |
---|
| 300 | editor.insertHtml(divBr+data); |
---|
| 301 | editor.focus(); |
---|
[5973] | 302 | } |
---|
| 303 | |
---|
| 304 | if(selection !== null){ |
---|
| 305 | if(selectionRanges[selectionRanges.length-1] !== undefined){ |
---|
| 306 | selectionRanges[selectionRanges.length-1].setStart(selectionRanges[selectionRanges.length-1].getTouchedStartNode().getParents()[1].getChild(0), 0); |
---|
| 307 | selectionRanges[selectionRanges.length-1].setEnd(selectionRanges[selectionRanges.length-1].getTouchedStartNode().getParents()[1].getChild(0), 0); |
---|
| 308 | } |
---|
| 309 | selection.selectRanges(selectionRanges); |
---|
| 310 | } |
---|
[5962] | 311 | |
---|
| 312 | if (is_webkit){ |
---|
[5385] | 313 | $('#cke_contents_body_'+id+'>iframe').scrollTo(':first'); |
---|
[5962] | 314 | } |
---|
[5973] | 315 | if(callback !== undefined) |
---|
| 316 | callback(); |
---|
[5962] | 317 | } |
---|
| 318 | else if(recursion < 20){ |
---|
| 319 | setTimeout(function() {RichTextEditor.setInitData(id,data,reply,recursion); }, 500); |
---|
| 320 | } |
---|
| 321 | } |
---|
[2] | 322 | } |
---|
| 323 | |
---|
[5083] | 324 | cRichTextEditor.prototype.destroy = function(id) |
---|
| 325 | { |
---|
| 326 | //Remove Instancia do editor |
---|
| 327 | if( CKEDITOR.instances[id] !== undefined ) |
---|
| 328 | CKEDITOR.remove(CKEDITOR.instances[id]); |
---|
[2] | 329 | } |
---|
[5417] | 330 | cRichTextEditor.prototype.active = function(id, just_id) |
---|
[5083] | 331 | { |
---|
| 332 | |
---|
| 333 | //Remove Instancia do editor caso ela exista |
---|
| 334 | if( CKEDITOR.instances[id] !== undefined ) |
---|
| 335 | CKEDITOR.remove(CKEDITOR.instances[id]); |
---|
| 336 | |
---|
[5134] | 337 | var height = document.body.scrollHeight; |
---|
[5460] | 338 | height -= 375; |
---|
[5083] | 339 | $('#'+id).ckeditor( |
---|
[5911] | 340 | function() { |
---|
| 341 | RichTextEditor.execPosInstance(id) |
---|
| 342 | }, |
---|
| 343 | { |
---|
| 344 | toolbar:'mail', |
---|
| 345 | height: height |
---|
| 346 | } |
---|
| 347 | ); |
---|
| 348 | //$("[name=textplain_rt_checkbox_"+just_id+"]").button({ disabled: false }); |
---|
[5083] | 349 | } |
---|
| 350 | cRichTextEditor.prototype.focus = function(id) |
---|
| 351 | { |
---|
| 352 | if(RichTextEditor.plain[id] === true) |
---|
| 353 | $('#body_'+id).focus(); |
---|
| 354 | else |
---|
| 355 | CKEDITOR.instances['body_'+id].focus(); |
---|
| 356 | |
---|
| 357 | } |
---|
| 358 | //Função reseta o atributo contentEditable para resolver bug de cursor ao trocar abas |
---|
| 359 | cRichTextEditor.prototype.setEditable = function(id) { |
---|
| 360 | if( CKEDITOR.instances['body_'+ id] === undefined ) return; |
---|
| 361 | var element = CKEDITOR.instances['body_'+ id].document.getBody(); |
---|
| 362 | element.removeAttribute('contentEditable'); |
---|
| 363 | element.setAttribute('contentEditable','true'); |
---|
| 364 | } |
---|
[2] | 365 | //Build the Object |
---|
[320] | 366 | RichTextEditor = new cRichTextEditor(); |
---|