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