[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 | { |
---|
[5083] | 57 | return (new Function( "return " + this.decode64( value )))(); |
---|
| 58 | } |
---|
[790] | 59 | |
---|
[5083] | 60 | cRichTextEditor.prototype.decode64 = function(input) { |
---|
| 61 | if( typeof input === "undefined" ) return ''; |
---|
[4309] | 62 | |
---|
[5083] | 63 | var output = new StringMaker(); |
---|
| 64 | var chr1, chr2, chr3; |
---|
| 65 | var enc1, enc2, enc3, enc4; |
---|
| 66 | var i = 0; |
---|
[790] | 67 | |
---|
[5083] | 68 | // remove all characters that are not A-Z, a-z, 0-9, +, /, or = |
---|
| 69 | input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); |
---|
[4291] | 70 | |
---|
[5083] | 71 | while (i < input.length) { |
---|
| 72 | enc1 = keyStr.indexOf(input.charAt(i++)); |
---|
| 73 | enc2 = keyStr.indexOf(input.charAt(i++)); |
---|
| 74 | enc3 = keyStr.indexOf(input.charAt(i++)); |
---|
| 75 | enc4 = keyStr.indexOf(input.charAt(i++)); |
---|
[4291] | 76 | |
---|
[5083] | 77 | chr1 = (enc1 << 2) | (enc2 >> 4); |
---|
| 78 | chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); |
---|
| 79 | chr3 = ((enc3 & 3) << 6) | enc4; |
---|
[2] | 80 | |
---|
[5083] | 81 | output.append(String.fromCharCode(chr1)); |
---|
[3130] | 82 | |
---|
[5083] | 83 | if (enc3 != 64) { |
---|
| 84 | output.append(String.fromCharCode(chr2)); |
---|
[2] | 85 | } |
---|
[5083] | 86 | if (enc4 != 64) { |
---|
| 87 | output.append(String.fromCharCode(chr3)); |
---|
[2] | 88 | } |
---|
| 89 | } |
---|
[5083] | 90 | |
---|
| 91 | return output.toString(); |
---|
[2] | 92 | } |
---|
| 93 | |
---|
[4291] | 94 | |
---|
[5083] | 95 | cRichTextEditor.prototype.loadEditor = function(ID) { |
---|
[2] | 96 | |
---|
[5083] | 97 | var parentDiv = document.getElementById("body_position_" + ID); |
---|
| 98 | var pObj = "body_" + ID; |
---|
| 99 | var textArea = document.createElement("TEXTAREA"); |
---|
| 100 | textArea.id = pObj; |
---|
| 101 | textArea.style.width = '100%'; |
---|
| 102 | parentDiv.appendChild(textArea); |
---|
| 103 | RichTextEditor.plain[ID] = false; |
---|
| 104 | |
---|
| 105 | if(preferences.plain_text_editor == 1) |
---|
[5158] | 106 | { |
---|
[5083] | 107 | RichTextEditor.plain[ID] = true; |
---|
[5158] | 108 | RichTextEditor.editorReady = true; |
---|
| 109 | } |
---|
[5083] | 110 | else |
---|
| 111 | RichTextEditor.active(pObj); |
---|
[2] | 112 | } |
---|
| 113 | |
---|
[5083] | 114 | cRichTextEditor.prototype.getSignaturesOptions = function() { |
---|
| 115 | |
---|
| 116 | if(RichTextEditor.signatures !== false) |
---|
| 117 | return RichTextEditor.signatures; |
---|
| 118 | |
---|
| 119 | var signatures = this.fromJSON( preferences.signatures ); |
---|
| 120 | var signature_types = this.fromJSON( preferences.signature_types ); |
---|
[2] | 121 | |
---|
[5083] | 122 | for( key in signatures ) |
---|
| 123 | if( !signature_types[key] ) |
---|
| 124 | signatures[key] = signatures[key].replace( /\n/g, "<br>" ); |
---|
| 125 | |
---|
| 126 | RichTextEditor.signatures = signatures; |
---|
| 127 | return signatures; |
---|
[2] | 128 | |
---|
[271] | 129 | } |
---|
| 130 | |
---|
[5083] | 131 | cRichTextEditor.prototype.getSignatureDefault = function() { |
---|
| 132 | |
---|
| 133 | if(RichTextEditor.signatures === false) |
---|
| 134 | RichTextEditor.signatures = RichTextEditor.getSignaturesOptions(); |
---|
| 135 | |
---|
| 136 | if(!RichTextEditor.signatures || |
---|
| 137 | !RichTextEditor.signatures[preferences.signature_default || ""]) |
---|
| 138 | { |
---|
| 139 | preferences.use_signature = "0"; //Desabilita o uso da assinatura |
---|
| 140 | return ''; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | return unescape(RichTextEditor.signatures[preferences.signature_default]); |
---|
[4472] | 144 | |
---|
[271] | 145 | } |
---|
| 146 | |
---|
| 147 | cRichTextEditor.prototype.createImage = function(){ |
---|
[5083] | 148 | if (preferences.auto_save_draft == 1) |
---|
| 149 | { |
---|
[646] | 150 | autosave_time = 200000; |
---|
| 151 | clearTimeout(openTab.autosave_timer[currentTab]); |
---|
[5083] | 152 | } |
---|
| 153 | |
---|
[271] | 154 | var form = document.getElementById("attachment_window"); |
---|
[5083] | 155 | |
---|
[271] | 156 | if (form == null){ |
---|
| 157 | form = document.createElement("DIV"); |
---|
| 158 | form.id = "attachment_window"; |
---|
| 159 | form.style.visibility = "hidden"; |
---|
| 160 | form.style.position = "absolute"; |
---|
| 161 | form.style.background = "#eeeeee"; |
---|
| 162 | form.style.left = "0px"; |
---|
| 163 | form.style.top = "0px"; |
---|
| 164 | form.style.width = "0px"; |
---|
| 165 | form.style.height = "0px"; |
---|
| 166 | document.body.appendChild(form); |
---|
| 167 | } |
---|
[1296] | 168 | var form_upload = Element('form_upload'); |
---|
| 169 | if (form_upload == null) |
---|
| 170 | form_upload = document.createElement("DIV"); |
---|
[271] | 171 | form_upload.id = "form_upload"; |
---|
| 172 | form_upload.style.position = "absolute"; |
---|
| 173 | form_upload.style.top = "5px"; |
---|
| 174 | form_upload.style.left = "5px"; |
---|
| 175 | form_upload.name = get_lang("Upload File"); |
---|
[5083] | 176 | form_upload.style.width = "450px"; |
---|
| 177 | form_upload.style.height = "75px"; |
---|
[271] | 178 | form_upload.innerHTML = get_lang('Select the desired image file')+':<br>'+ |
---|
[5083] | 179 | '<input name="image_at" maxlength="255" size="40" id="inputFile_img" type="file"><br>' + |
---|
| 180 | '<input title="' + get_lang('Include') + '" value="' + get_lang('Include') + '"' + |
---|
| 181 | 'type="button" onclick="RichTextEditor.addInputFile();"> ' + |
---|
| 182 | '<input title="' + get_lang('Close') + '" value="' + get_lang('Close') + '"' + |
---|
| 183 | ' type="button" onclick="win.close()">'; |
---|
[271] | 184 | form.appendChild(form_upload); |
---|
| 185 | |
---|
| 186 | this.showWindow(form); |
---|
| 187 | } |
---|
| 188 | cRichTextEditor.prototype.showWindow = function (div){ |
---|
| 189 | |
---|
| 190 | if(! div) { |
---|
| 191 | return; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | if(! this.emwindow[div.id]) { |
---|
| 195 | div.style.width = div.firstChild.style.width; |
---|
| 196 | div.style.height = div.firstChild.style.height; |
---|
| 197 | div.style.zIndex = "10000"; |
---|
| 198 | var title = div.firstChild.name; |
---|
| 199 | var wHeight = div.offsetHeight + "px"; |
---|
| 200 | var wWidth = div.offsetWidth + "px"; |
---|
| 201 | div.style.width = div.offsetWidth - 5; |
---|
| 202 | |
---|
| 203 | win = new dJSWin({ |
---|
| 204 | id: 'win_'+div.id, |
---|
| 205 | content_id: div.id, |
---|
| 206 | width: wWidth, |
---|
| 207 | height: wHeight, |
---|
| 208 | title_color: '#3978d6', |
---|
| 209 | bg_color: '#eee', |
---|
| 210 | title: title, |
---|
| 211 | title_text_color: 'white', |
---|
| 212 | button_x_img: '../phpgwapi/images/winclose.gif', |
---|
[5083] | 213 | border: true}); |
---|
[271] | 214 | |
---|
| 215 | this.emwindow[div.id] = win; |
---|
| 216 | win.draw(); |
---|
| 217 | } |
---|
| 218 | else |
---|
| 219 | win = this.emwindow[div.id]; |
---|
| 220 | win.open(); |
---|
| 221 | } |
---|
[5083] | 222 | cRichTextEditor.prototype.addInputFile = function() |
---|
[2] | 223 | { |
---|
[5083] | 224 | //Begin: Verify if the image extension is allowed. |
---|
| 225 | var imgExtensions = new Array("jpeg", "jpg", "gif", "png", "bmp", "xbm", "tiff", "pcx"); |
---|
| 226 | var inputFile = document.getElementById('inputFile_img'); |
---|
| 227 | if(!inputFile.value) return false; |
---|
| 228 | var fileExtension = inputFile.value.split("."); |
---|
| 229 | fileExtension = fileExtension[(fileExtension.length-1)]; |
---|
| 230 | var deniedExtension = true; |
---|
| 231 | for(var i=0; i<imgExtensions.length; i++) { |
---|
| 232 | if(imgExtensions[i].toUpperCase() == fileExtension.toUpperCase()) { |
---|
| 233 | deniedExtension = false; |
---|
| 234 | break; |
---|
| 235 | } |
---|
| 236 | } |
---|
| 237 | if(deniedExtension) { |
---|
| 238 | alert(get_lang('File extension forbidden or invalid file') + '.'); |
---|
| 239 | return false; |
---|
| 240 | } |
---|
| 241 | // End: Verify image extension. |
---|
| 242 | var id = currentTab; |
---|
| 243 | divFiles = document.getElementById("divFiles_"+id); |
---|
| 244 | var countDivFiles = divFiles.childNodes.length + 1; |
---|
[2] | 245 | |
---|
[5083] | 246 | var divFiles = document.getElementById('divFiles_'+id); |
---|
| 247 | inputFile.id = 'inputFile_'+id +"_"+countDivFiles; |
---|
| 248 | inputFile.name = 'file_'+countDivFiles; |
---|
| 249 | divFile = document.createElement('DIV'); |
---|
| 250 | divFile.appendChild(inputFile); |
---|
| 251 | divFiles.appendChild(divFile); |
---|
| 252 | |
---|
| 253 | var form_upload = document.getElementById('form_upload'); |
---|
| 254 | form_upload.parentNode.removeChild(form_upload); |
---|
| 255 | win.close(); |
---|
| 256 | |
---|
| 257 | RichTextEditor.saveFlag = 0; // See if save function finished |
---|
| 258 | var save_link = document.getElementById("save_message_options_"+id); |
---|
| 259 | save_link.onclick = function () {}; |
---|
| 260 | this.newImageId = new Date().getTime(); |
---|
| 261 | CKEDITOR.instances['body_'+id].insertHtml('<img id="'+this.newImageId+'" src=""/>'); |
---|
| 262 | save_msg(id,true); |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | cRichTextEditor.prototype.execPosInstance = function(inst) { |
---|
| 266 | |
---|
| 267 | var editor = CKEDITOR.instances[inst]; |
---|
| 268 | |
---|
| 269 | |
---|
| 270 | var id = inst.replace('body_',''); |
---|
| 271 | |
---|
| 272 | editor.document.on('keydown', function(event) |
---|
| 273 | { |
---|
| 274 | away = false; |
---|
| 275 | var save_link = Element("save_message_options_"+id); |
---|
| 276 | save_link.onclick = function onclick() {openTab.toPreserve[id] = true;save_msg(id);} ; |
---|
| 277 | save_link.className = 'message_options'; |
---|
| 278 | }); |
---|
| 279 | |
---|
| 280 | //Adicionando atalhos de preferencias |
---|
| 281 | //TODO: Não esta pegando o numero (keycode) |
---|
| 282 | // if (preferences.use_shortcuts == '1') |
---|
| 283 | // { |
---|
| 284 | // CKEDITOR.instances[inst].document.on('keyup', function(event){ |
---|
| 285 | // if(event.keyCode == 27){ |
---|
| 286 | // delete_border(id,'false');} //Tecla delete fechar aba |
---|
| 287 | // }); |
---|
| 288 | // } |
---|
| 289 | |
---|
| 290 | // IM Module Enabled |
---|
| 291 | if( window.parent.loadscript && loadscript.autoStatusIM ) |
---|
| 292 | { |
---|
| 293 | CKEDITOR.instances[inst].document.on('keydown', function(event){ |
---|
| 294 | loadscript.autoStatusIM; |
---|
| 295 | }); |
---|
[2] | 296 | } |
---|
[5083] | 297 | |
---|
| 298 | if (preferences.auto_save_draft == 1) |
---|
| 299 | { |
---|
| 300 | openTab.autosave_timer[id] = false; |
---|
| 301 | var save_link = document.getElementById("save_message_options"+id); |
---|
| 302 | CKEDITOR.instances[inst].document.on('keydown', function(event){ |
---|
| 303 | if (openTab.autosave_timer[id]) |
---|
| 304 | clearTimeout(openTab.autosave_timer[id]); |
---|
| 305 | openTab.autosave_timer[id] = setTimeout("save_msg("+id+")", autosave_time); |
---|
| 306 | |
---|
| 307 | }); |
---|
| 308 | } |
---|
| 309 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 310 | |
---|
| 311 | |
---|
| 312 | |
---|
| 313 | RichTextEditor.editorReady = true; |
---|
[2] | 314 | } |
---|
[5083] | 315 | cRichTextEditor.prototype.setPlain = function (active,id){ |
---|
| 316 | RichTextEditor.plain[id] = active; |
---|
| 317 | if(active === true) |
---|
| 318 | { |
---|
| 319 | CKEDITOR.instances['body_'+id].destroy(); |
---|
| 320 | var height = document.body.scrollHeight; |
---|
| 321 | height -= 330; |
---|
| 322 | $('#body_'+id).height(height); |
---|
| 323 | $('#body_'+id).keydown(function(event) { |
---|
| 324 | away = false; |
---|
| 325 | var save_link = Element("save_message_options_"+id); |
---|
| 326 | save_link.onclick = function onclick() {openTab.toPreserve[id] = true;save_msg(id);} ; |
---|
| 327 | save_link.className = 'message_options'; |
---|
| 328 | }); |
---|
[2] | 329 | |
---|
[5083] | 330 | } |
---|
| 331 | else |
---|
| 332 | RichTextEditor.active('body_'+id); |
---|
[2] | 333 | } |
---|
| 334 | |
---|
[5083] | 335 | cRichTextEditor.prototype.getData = function (inst){ |
---|
| 336 | var id = inst.replace('body_',''); |
---|
| 337 | |
---|
| 338 | if(RichTextEditor.plain[id] === true) |
---|
| 339 | return $('#'+inst).val(); |
---|
| 340 | else |
---|
| 341 | return CKEDITOR.instances[inst].getData(); |
---|
[2] | 342 | } |
---|
[5083] | 343 | cRichTextEditor.prototype.setData = function (id,data){ |
---|
| 344 | |
---|
| 345 | if(this.plain[id] === true) |
---|
| 346 | $('#'+id).val(data); |
---|
| 347 | else |
---|
| 348 | CKEDITOR.instances[id].setData(data) |
---|
| 349 | } |
---|
| 350 | cRichTextEditor.prototype.setInitData = function (id,data,reply,recursion){ |
---|
| 351 | |
---|
| 352 | if(recursion === undefined) recursion = 1; |
---|
| 353 | else recursion++; |
---|
| 354 | |
---|
| 355 | if(this.plain[id] === true) |
---|
| 356 | { |
---|
| 357 | if($('#'+id) !== undefined) |
---|
| 358 | $('#'+id).val(data); |
---|
| 359 | else |
---|
| 360 | setTimeout(function() {RichTextEditor.setInitData(id,data,reply,recursion); }, 500); |
---|
| 361 | } |
---|
| 362 | else |
---|
| 363 | { |
---|
| 364 | if( RichTextEditor.editorReady === true && CKEDITOR.instances['body_'+id] !== undefined ) |
---|
| 365 | { |
---|
| 366 | var editor = CKEDITOR.instances['body_'+id]; |
---|
| 367 | editor.insertHtml(''); |
---|
| 368 | var selection = editor.getSelection(); |
---|
| 369 | if(selection !== undefined && selection !== null) var selectionRanges = selection.getRanges(); |
---|
[2] | 370 | |
---|
[5134] | 371 | var fontSize = ''; |
---|
| 372 | var fontFamily = ''; |
---|
[5083] | 373 | |
---|
[5134] | 374 | if(typeof(preferences.font_size_editor) !== 'undefined') |
---|
| 375 | fontSize = 'font-size:' + preferences.font_size_editor; |
---|
| 376 | |
---|
| 377 | if(fontSize != '') |
---|
| 378 | fontFamily = ';' |
---|
| 379 | |
---|
| 380 | if(typeof(preferences.font_family_editor) !== 'undefined') |
---|
| 381 | fontFamily += 'font-family:' + preferences.font_family_editor + ';'; |
---|
| 382 | |
---|
| 383 | var divBr = '<div style="'+fontSize+fontFamily+'"><br type="_moz"></div>'; |
---|
[5083] | 384 | |
---|
[5158] | 385 | if(reply === undefined) |
---|
| 386 | editor.insertHtml(divBr+divBr+data); |
---|
| 387 | else if(reply == 'edit') |
---|
| 388 | editor.insertHtml(data); |
---|
| 389 | else |
---|
| 390 | editor.insertHtml(divBr+data); |
---|
[5134] | 391 | |
---|
| 392 | if(selection !== null) selection.selectRanges(selectionRanges); |
---|
[5385] | 393 | |
---|
| 394 | //Coloca o scroll do editor no inicio |
---|
| 395 | if (is_webkit){ |
---|
| 396 | $('#cke_contents_body_'+id+'>iframe').scrollTo(':first'); |
---|
| 397 | } |
---|
[5083] | 398 | //Caso não for uma resposta votla o foco para o input to |
---|
| 399 | if(reply === undefined) |
---|
| 400 | setTimeout("$('#to_"+id+"').focus()",100); |
---|
| 401 | |
---|
| 402 | } |
---|
| 403 | else if(recursion < 20) |
---|
| 404 | setTimeout(function() {RichTextEditor.setInitData(id,data,reply,recursion); }, 500); |
---|
| 405 | } |
---|
[2] | 406 | } |
---|
| 407 | |
---|
[5083] | 408 | cRichTextEditor.prototype.destroy = function(id) |
---|
| 409 | { |
---|
| 410 | //Remove Instancia do editor |
---|
| 411 | if( CKEDITOR.instances[id] !== undefined ) |
---|
| 412 | CKEDITOR.remove(CKEDITOR.instances[id]); |
---|
[2] | 413 | } |
---|
[5083] | 414 | cRichTextEditor.prototype.active = function(id) |
---|
| 415 | { |
---|
| 416 | |
---|
| 417 | //Remove Instancia do editor caso ela exista |
---|
| 418 | if( CKEDITOR.instances[id] !== undefined ) |
---|
| 419 | CKEDITOR.remove(CKEDITOR.instances[id]); |
---|
| 420 | |
---|
[5134] | 421 | var height = document.body.scrollHeight; |
---|
| 422 | height -= 425; |
---|
| 423 | |
---|
[5083] | 424 | $('#'+id).ckeditor( |
---|
| 425 | function() {RichTextEditor.execPosInstance(id)}, |
---|
| 426 | { |
---|
[5134] | 427 | toolbar:'mail', |
---|
| 428 | height: height |
---|
[5083] | 429 | }); |
---|
[2] | 430 | |
---|
[5083] | 431 | } |
---|
| 432 | cRichTextEditor.prototype.focus = function(id) |
---|
| 433 | { |
---|
| 434 | if(RichTextEditor.plain[id] === true) |
---|
| 435 | $('#body_'+id).focus(); |
---|
| 436 | else |
---|
| 437 | CKEDITOR.instances['body_'+id].focus(); |
---|
| 438 | |
---|
| 439 | } |
---|
| 440 | //Função reseta o atributo contentEditable para resolver bug de cursor ao trocar abas |
---|
| 441 | cRichTextEditor.prototype.setEditable = function(id) { |
---|
| 442 | if( CKEDITOR.instances['body_'+ id] === undefined ) return; |
---|
| 443 | var element = CKEDITOR.instances['body_'+ id].document.getBody(); |
---|
| 444 | element.removeAttribute('contentEditable'); |
---|
| 445 | element.setAttribute('contentEditable','true'); |
---|
| 446 | } |
---|
[2] | 447 | //Build the Object |
---|
[320] | 448 | RichTextEditor = new cRichTextEditor(); |
---|