57 lines
14 KiB
JavaScript
57 lines
14 KiB
JavaScript
/**
|
|
* Roundcube editor js library
|
|
*
|
|
* This file is part of the Roundcube Webmail client
|
|
*
|
|
* @licstart The following is the entire license notice for the
|
|
* JavaScript code in this file.
|
|
*
|
|
* Copyright (c) 2006-2014, The Roundcube Dev Team
|
|
*
|
|
* The JavaScript code in this page is free software: you can
|
|
* redistribute it and/or modify it under the terms of the GNU
|
|
* General Public License (GNU GPL) as published by the Free Software
|
|
* Foundation, either version 3 of the License, or (at your option)
|
|
* any later version. The code is distributed WITHOUT ANY WARRANTY;
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
* FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
|
|
*
|
|
* As additional permission under GNU GPL version 3 section 7, you
|
|
* may distribute non-source (e.g., minimized or compacted) forms of
|
|
* that code without the copy of the GNU GPL normally required by
|
|
* section 4, provided you include this license notice and a URL
|
|
* through which recipients can access the Corresponding Source.
|
|
*
|
|
* @licend The above is the entire license notice
|
|
* for the JavaScript code in this file.
|
|
*
|
|
* @author Eric Stadtherr <estadtherr@gmail.com>
|
|
* @author Aleksander Machniak <alec@alec.pl>
|
|
*/
|
|
function rcube_text_editor(l,m){var h=this,p=location.href.replace(/[?#].*$/,"").replace(/\/$/,""),k={selector:"#"+($("#"+m).is(".mce_editor")?m:"fake-editor-id"),cache_suffix:"s=4050100",theme:"modern",language:l.lang,content_css:rcmail.assets_path("program/resources/tinymce/content.css"),menubar:!1,statusbar:!1,toolbar_items_size:"small",extended_valid_elements:"font[face|size|color|style],span[id|class|align|style]",relative_urls:!1,remove_script_host:!1,convert_urls:!1,image_description:!1,paste_webkit_style:"color font-size font-family",
|
|
paste_data_images:!0,browser_spellcheck:!0};this.spellcheck_observer=function(){};l.spellchecker&&(this.spellchecker=l.spellchecker,l.spellcheck_observer&&(this.spellchecker.spelling_state_observer=this.spellcheck_observer=l.spellcheck_observer));tinymce.registered_request_token||(tinymce.registered_request_token=!0,tinymce.util.XHR.on("beforeSend",function(a){a.xhr.setRequestHeader("X-Roundcube-Request",rcmail.env.request_token)}));"identity"==l.mode?$.extend(k,{plugins:"autolink charmap code colorpicker hr image link paste tabfocus textcolor",
|
|
toolbar:"bold italic underline alignleft aligncenter alignright alignjustify | outdent indent charmap hr link unlink image code forecolor | fontselect fontsizeselect",file_browser_callback:function(a,b,d,c){h.file_browser_callback(a,b,d)},file_browser_callback_types:"image"}):$.extend(k,{plugins:"autolink charmap code colorpicker directionality link image media nonbreaking paste table tabfocus textcolor searchreplace spellchecker",toolbar:"bold italic underline | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent ltr rtl blockquote | forecolor backcolor | fontselect fontsizeselect | link unlink table | $extra charmap image media | code searchreplace undo redo",
|
|
spellchecker_rpc_url:p+"/?_task=utils&_action=spell_html&_remote=1",spellchecker_language:rcmail.env.spell_lang,accessibility_focus:!1,file_browser_callback:function(a,b,d,c){h.file_browser_callback(a,b,d)},file_browser_callback_types:"image media"});$.each(l.extra_plugins||[],function(){0>k.plugins.indexOf(this)&&(k.plugins=k.plugins+" "+this)});$.each(l.extra_buttons||[],function(){0>k.toolbar.indexOf(this)&&(k.toolbar=k.toolbar.replace("$extra","$extra "+this))});$.each(l.disabled_plugins||[],
|
|
function(){k.plugins=k.plugins.replace(this,"")});$.each(l.disabled_buttons||[],function(){k.toolbar=k.toolbar.replace(this,"")});k.toolbar=k.toolbar.replace("$extra","").replace(/\|\s+\|/g,"|");window.rcmail_editor_settings&&$.extend(k,window.rcmail_editor_settings);k.setup=function(a){a.on("init",function(a){h.init_callback(a)});a.on("SpellcheckStart SpellcheckEnd",function(a){h.spellcheck_active="spellcheckstart"==a.type;h.spellcheck_observer()});a.on("keypress",function(){rcmail.compose_type_activity++})};
|
|
this.id=m;this.editor=null;tinymce.init(k);this.init_callback=function(a){this.editor=a.target;if("compose"==rcmail.env.action){var b=$("#"+this.id),d=$("div.mce-toolbar-grp:first",b.parent()).height();if(200<d||d>b.height())return setTimeout(function(){h.init_callback(a)},300);var b={},d=rcube_find_object("_from"),c=rcmail.env.compose_focus_elem;rcmail.env.default_font&&(b["font-family"]=rcmail.env.default_font);rcmail.env.default_font_size&&(b["font-size"]=rcmail.env.default_font_size);(b["font-family"]||
|
|
b["font-size"])&&$(this.editor.getBody()).css(b);d&&"select-one"==d.type&&(rcmail.env.identities_initialized||rcmail.change_identity(d),c&&c.id!=this.id&&(window.focus(),c.focus(),rcmail.env.compose_focus_elem=null));h.tabindex(c&&c.id==h.id);$(window).resize()}};this.tabindex=function(a){if("mail"==rcmail.env.task&&this.editor){var b=this.editor.getElement(),d=this.editor.getBody(),c=this.editor.getContentAreaContainer().childNodes[0];b&&c&&(c.tabIndex=b.tabIndex);if(0<b.tabIndex){var f=null,c=[":prev",
|
|
":next"],b=tinymce.DOM.select("*[tabindex="+b.tabIndex+"]:not(iframe)");tinymce.each(b,function(a,b){if(a.id==h.id)return f=b,!1});null!==f&&(b[f-1]&&b[f-1].id&&(c[0]=b[f-1].id),b[f+1]&&b[f+1].id&&(c[1]=b[f+1].id),this.editor.settings.tabfocus_elements=c.join(","))}bw.mz&&25>bw.vendver&&$(d).prop("contenteditable",!1).prop("contenteditable",!0);a&&d.focus()}};this.toggle=function(a,b){var d,c,f=$("#"+this.id),e=rcmail.env.identity?rcmail.env.signatures[rcmail.env.identity]:null,g=e&&e.text&&1<e.text.length;
|
|
this.spellcheck_stop();if(a)c=f.val(),g&&(c=c.replace(/\r\n/,"\n"),c=c.replace(e.text.replace(/\r\n/,"\n"),"\u0002\u0003")),d=function(a){g&&(a=a.replace("\u0002\u0003",'<div id="_rc_sig">'+e.html+"</div>"));f.val(a);tinymce.execCommand("mceAddEditor",!1,h.id);h.editor&&(a=$(h.editor.getBody()),h.tabindex(!0),h.editor.selection.setCursorLocation(a.children().first().get(0)))},b?(d(c),c=!0):c=rcmail.plain2html(c,d);else if(this.editor){if(g){if(d=this.editor.dom.get("_rc_sig"))d=d.innerHTML;this.editor.dom.setHTML("_rc_sig",
|
|
"\u0002\u0003")}c=this.editor.getContent();var n=function(a){tinymce.execCommand("mceRemoveEditor",!1,h.id);h.editor=null;g&&(a=a.replace("\u0002\u0003","\n"+e.text));f.val(a).focus();rcmail.set_caret_pos(f.get(0),0)};b?(n(f.val()),c=!0):c=rcmail.html2plain(c,n);!c&&d&&this.editor.dom.setHTML("_rc_sig",d)}return c};this.spellcheck_start=function(){this.editor?(tinymce.execCommand("mceSpellCheck",!0),this.spellcheck_observer()):this.spellchecker&&this.spellchecker.spellCheck&&this.spellchecker.spellCheck()};
|
|
this.spellcheck_stop=function(){var a=this.editor;a?a.plugins&&a.plugins.spellchecker&&this.spellcheck_active&&(a.execCommand("mceSpellCheck",!1),this.spellcheck_observer()):(a=this.spellchecker)&&a.state&&"ready"!=a.state&&"no_error_found"!=a.state&&$(a.spell_span).trigger("click")};this.spellcheck_state=function(){var a;if(this.editor)return this.spellcheck_active;if((a=this.spellchecker)&&a.state)return"ready"!=a.state&&"no_error_found"!=a.state};this.spellcheck_resume=function(a){var b=this.editor;
|
|
if(b)b.plugins.spellchecker.markErrors(a);else if(b=this.spellchecker)b.prepare(!1,!0),b.processData(a)};this.get_language=function(){if(this.editor)return this.editor.settings.spellchecker_language||rcmail.env.spell_lang;if(this.spellchecker)return GOOGIE_CUR_LANG};this.set_language=function(a){var b=this.editor;b&&(b.settings.spellchecker_language=a);(b=this.spellchecker)&&b.setCurrentLanguage(a)};this.replace=function(a){var b,d=this.editor;if(!a)return!1;if(d)d.getWin().focus(),"object"==$.type(a)&&
|
|
"html"in a?(a=a.html,b="html"):("object"==$.type(a)&&(a=a.text||""),a=rcmail.quote_html(a).replace(/\r?\n/g,"<br/>"),b="text"),d.selection.setContent(a,{format:b});else if(d=rcube_find_object(this.id)){b=$(d).is(":focus")?rcmail.get_input_selection(d):{start:0,end:0};var c=d.value;pre=c.substring(0,b.start);end=c.substring(b.end,c.length);"object"==$.type(a)&&(a=a.text||"");d.value=pre+a+end;rcmail.set_caret_pos(d,b.start+a.length);d.focus()}};this.get_content=function(a){var b=this.editor,d="",c=
|
|
!1;a=$.extend({refresh:!0,selection:!1,nosig:!1,format:"html"},a);a.refresh&&this.spellcheck_stop();if(b)a.selection&&(d=b.selection.getContent({format:a.format})),d||(d=b.getContent({format:a.format}),c="text"==a.format);else if(b=rcube_find_object(this.id))a.selection&&$(b).is(":focus")&&(d=rcmail.get_input_selection(b).text),d||(d=b.value,c=!0);c&&a.nosig&&(a=d.indexOf("-- \n"),0<a&&(d=d.substring(0,a)));return d};this.change_signature=function(a,b){var d,c;c=-1;var f=$("#"+this.id),e=f.val(),
|
|
g=rcmail.env.identity;this.editor?b&&rcmail.env.signatures?(f=this.editor.dom.get("_rc_sig"),f||(e=this.editor.getBody(),f=$('<div id="_rc_sig"></div>').get(0),rcmail.env.top_posting&&!rcmail.env.sig_below&&rcmail.env.compose_mode&&(1<e.childNodes.length||$(e).text())?(this.editor.getWin().focus(),g=this.editor.selection.getNode(),$(f).insertBefore("BODY"==g.nodeName?e.firstChild:g.nextSibling),$("<p>").append($("<br>")).insertBefore(f)):(e.appendChild(f),d=rcmail.env.top_posting&&rcmail.env.compose_mode?
|
|
e.firstChild:$(f).prev())),f.innerHTML=rcmail.env.signatures[a]?rcmail.env.signatures[a].html:""):rcmail.env.top_posting||(d=$(this.editor.getBody()).children().last()):(b&&g&&rcmail.env.signatures&&rcmail.env.signatures[g]&&(g=rcmail.env.signatures[g].text,g=g.replace(/\r\n/g,"\n"),c=rcmail.env.top_posting?e.indexOf(g):e.lastIndexOf(g),0<=c&&(e=e.substring(0,c)+e.substring(c+g.length,e.length))),b&&rcmail.env.signatures&&rcmail.env.signatures[a]?(g=rcmail.env.signatures[a].text,g=g.replace(/\r\n/g,
|
|
"\n"),0<=c?(e=e.substring(0,c)+g+e.substring(c,e.length),--c):e&&rcmail.env.compose_mode?rcmail.env.top_posting&&!rcmail.env.sig_below?(pos=rcmail.get_caret_pos(f.get(0)))?(e=e.substring(0,pos)+"\n"+g+"\n\n"+e.substring(pos,e.length),c=pos):(e="\n\n"+g+"\n\n"+e.replace(/^[\r\n]+/,""),c=0):(e=e.replace(/[\r\n]+$/,""),c=!rcmail.env.top_posting&&e.length?e.length+1:0,e+="\n\n"+g):(c=e.length,e+="\n\n"+g)):c=rcmail.env.top_posting?0:e.length,f.val(e),rcmail.set_caret_pos(f.get(0),c));this.editor&&d&&
|
|
d.length&&(this.editor.selection.setCursorLocation(d.get(0)),this.editor.getWin().scroll(0,d.offset().top))};this.save=function(){this.editor&&this.editor.save()};this.focus=function(){(this.editor||rcube_find_object(this.id)).focus()};this.file_browser_callback=function(a,b,d){var c,f,e,g=[];b=this.editor.windowManager.open({title:rcmail.get_label("select"+d),width:500,height:300,html:'<div id="image-selector-list"><ul></ul></div><div id="image-selector-form"><div id="image-upload-button" class="mce-widget mce-btn" role="button" tabindex="0"></div></div>',
|
|
buttons:[{text:"Cancel",onclick:function(){h.file_browser_close()}}]});rcmail.env.file_browser_field=a;rcmail.env.file_browser_type=d;for(c in rcmail.env.attachments)(f=h.file_browser_entry(c,rcmail.env.attachments[c]))&&g.push(f);g.length&&$("#image-selector-list > ul").append(g).find("li:first").focus();$("div.mce-abs-end",b.getEl()).append($('<div class="hint">').text($("div.hint",rcmail.gui_objects.uploadform).text()));f=$("#image-upload-button").append($("<span>").text(rcmail.get_label("add"+
|
|
d)));e=f.parents(".mce-panel").find("button:last").parent();f.keydown(function(a){if(9==a.which)return rcube_event.get_modifier(a)==SHIFT_KEY?$("#image-selector-list li:last").focus():e.focus(),!1});e.keydown(function(a){if(9==a.which)return rcube_event.get_modifier(a)==SHIFT_KEY?f.focus():$("#image-selector-list li:first").focus(),!1});this.hack_file_input(f,rcmail.gui_objects.uploadform);if(window.XMLHttpRequest&&XMLHttpRequest.prototype&&XMLHttpRequest.prototype.sendAsBinary||window.FormData)rcmail.env.filedrop||
|
|
(rcmail.env.filedrop={}),rcmail.gui_objects.filedrop&&(rcmail.env.old_file_drop=rcmail.gui_objects.filedrop),rcmail.gui_objects.filedrop=$("#image-selector-form"),rcmail.gui_objects.filedrop.addClass("droptarget").on("dragover dragleave",function(a){a.preventDefault();a.stopPropagation();$(this)["dragover"==a.type?"addClass":"removeClass"]("hover")}).get(0).addEventListener("drop",function(a){return rcmail.file_dropped(a)},!1);rcmail.env.file_dialog_event||(rcmail.env.file_dialog_event=!0,rcmail.addEventListener("fileuploaded",
|
|
function(a){if(a=h.file_browser_entry(a.name,a.attachment))$("#image-selector-list > ul").prepend(a),a.focus()}))};this.file_browser_close=function(a){var b=$("#"+rcmail.env.file_browser_field);a&&b.val(a);this.editor.windowManager.close();b.focus();rcmail.env.old_file_drop&&(rcmail.gui_objects.filedrop=rcmail.env.old_file_drop)};this.file_browser_entry=function(a,b){if(b.complete&&b.mimetype){rcmail.file_upload_id&&rcmail.set_busy(!1,null,rcmail.file_upload_id);var d,c;switch(rcmail.env.file_browser_type){case "image":d=
|
|
/^image\//i;break;case "media":d=/^video\//i;c="program/resources/tinymce/video.png";break;default:return}if(d.test(b.mimetype))return d=rcmail.env.comm_path+"&_from="+rcmail.env.action+(rcmail.env.compose_id?"&_id="+rcmail.env.compose_id+"&_action=display-attachment":"&_action=upload-display")+"&_file="+a,c=$("<img>").attr({title:b.name,src:c?c:d+"&_thumbnail=1"}),$("<li>").attr({tabindex:0}).data("url",d).append($('<span class="img">').append(c)).append($('<span class="name">').text(b.name)).click(function(){h.file_browser_close($(this).data("url"))}).keydown(function(a){if(13==
|
|
a.which)h.file_browser_close($(this).data("url"));else if(9==a.which)return rcube_event.get_modifier(a)==SHIFT_KEY?$(this).prev().focus().length||$("#image-upload-button").parents(".mce-panel").find("button:last").parent().focus():$(this).next().focus().length||$("#image-upload-button").focus(),!1})}};this.hack_file_input=function(a,b){function d(a){c||(c=f.offset());e.css({top:a.pageY-c.top-10+"px",left:a.pageX-c.left-10+"px"})}var c,f=$(a),e=$("<input>").attr("name","_file[]"),g=$("<form>").attr({method:"post",
|
|
enctype:"multipart/form-data"});b&&(e.attr("name",$('input[type="file"]',b).attr("name")),g.attr("action",$(b).attr("action")).append($("<input>").attr({type:"hidden",name:"_token",value:rcmail.env.request_token})));e.attr({type:"file",multiple:"multiple",size:5,title:"",tabindex:-1}).change(function(){rcmail.upload_file(g,"upload")}).click(function(){setTimeout(function(){f.mouseleave()},20)}).css({opacity:0,cursor:"pointer",position:"relative",outline:"none"}).appendTo(g);navigator.userAgent.match(/Firefox|MSIE/)&&
|
|
e.css({marginLeft:"-80px"});f.css({overflow:"hidden",cursor:"pointer"}).mouseenter(function(){this.__active=!0}).mousemove(function(a){this.__active?d(a):$(this).mouseleave()}).mouseleave(function(){e.css({top:"-10000px",left:"-10000px"});this.__active=!1}).click(function(a){this.__active||(this.__active=!0,d(a),e.trigger(a))}).keydown(function(a){13==a.which&&e.trigger("click")}).mouseleave().append(g)}};
|