You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

editor.min.js 14KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * Roundcube editor js library
  3. *
  4. * This file is part of the Roundcube Webmail client
  5. *
  6. * @licstart The following is the entire license notice for the
  7. * JavaScript code in this file.
  8. *
  9. * Copyright (c) 2006-2014, The Roundcube Dev Team
  10. *
  11. * The JavaScript code in this page is free software: you can
  12. * redistribute it and/or modify it under the terms of the GNU
  13. * General Public License (GNU GPL) as published by the Free Software
  14. * Foundation, either version 3 of the License, or (at your option)
  15. * any later version. The code is distributed WITHOUT ANY WARRANTY;
  16. * without even the implied warranty of MERCHANTABILITY or FITNESS
  17. * FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
  18. *
  19. * As additional permission under GNU GPL version 3 section 7, you
  20. * may distribute non-source (e.g., minimized or compacted) forms of
  21. * that code without the copy of the GNU GPL normally required by
  22. * section 4, provided you include this license notice and a URL
  23. * through which recipients can access the Corresponding Source.
  24. *
  25. * @licend The above is the entire license notice
  26. * for the JavaScript code in this file.
  27. *
  28. * @author Eric Stadtherr <estadtherr@gmail.com>
  29. * @author Aleksander Machniak <alec@alec.pl>
  30. */
  31. 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=4031300",theme:"modern",language:l.lang,content_css:rcmail.assets_path("program/js/tinymce/roundcube/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,
  32. 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(c){c.xhr.setRequestHeader("X-Roundcube-Request",rcmail.env.request_token)}));"identity"==l.mode?
  33. $.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(c,a,d,b){h.file_browser_callback(c,a,d)},file_browser_callback_types:"image"}):$.extend(k,{plugins:"autolink charmap code colorpicker directionality link image media nonbreaking paste table tabfocus textcolor searchreplace spellchecker",
  34. 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(c,a,d,b){h.file_browser_callback(c,a,d)},file_browser_callback_types:"image media"});
  35. $.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);
  36. k.setup=function(c){c.on("init",function(a){h.init_callback(a)});c.on("SpellcheckStart SpellcheckEnd",function(a){h.spellcheck_active="spellcheckstart"==a.type;h.spellcheck_observer()});c.on("keypress",function(){rcmail.compose_type_activity++})};this.id=m;this.editor=null;tinymce.init(k);this.init_callback=function(c){this.editor=c.target;if("compose"==rcmail.env.action){var a=$("#"+this.id),d=$("div.mce-toolbar-grp:first",a.parent()).height();if(200<d||d>a.height())return setTimeout(function(){h.init_callback(c)},
  37. 300);var a={},d=rcube_find_object("_from"),b=rcmail.env.compose_focus_elem;rcmail.env.default_font&&(a["font-family"]=rcmail.env.default_font);rcmail.env.default_font_size&&(a["font-size"]=rcmail.env.default_font_size);(a["font-family"]||a["font-size"])&&$(this.editor.getBody()).css(a);d&&"select-one"==d.type&&(rcmail.env.identities_initialized||rcmail.change_identity(d),b&&b.id!=this.id&&(window.focus(),b.focus(),rcmail.env.compose_focus_elem=null));h.tabindex(b&&b.id==h.id);$(window).resize()}};
  38. this.tabindex=function(c){if("mail"==rcmail.env.task&&this.editor){var a=this.editor.getElement(),d=this.editor.getBody(),b=this.editor.getContentAreaContainer().childNodes[0];a&&b&&(b.tabIndex=a.tabIndex);if(0<a.tabIndex){var f=null,b=[":prev",":next"],a=tinymce.DOM.select("*[tabindex="+a.tabIndex+"]:not(iframe)");tinymce.each(a,function(a,b){if(a.id==h.id)return f=b,!1});null!==f&&(a[f-1]&&a[f-1].id&&(b[0]=a[f-1].id),a[f+1]&&a[f+1].id&&(b[1]=a[f+1].id),this.editor.settings.tabfocus_elements=b.join(","))}bw.mz&&
  39. 25>bw.vendver&&$(d).prop("contenteditable",!1).prop("contenteditable",!0);c&&d.focus()}};this.toggle=function(c,a){var d,b,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(c)b=f.val(),g&&(b=b.replace(/\r\n/,"\n"),b=b.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&&
  40. (a=$(h.editor.getBody()),h.tabindex(!0),h.editor.selection.setCursorLocation(a.children().first().get(0)))},a?(d(b),b=!0):b=rcmail.plain2html(b,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")}b=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)};a?(n(f.val()),b=!0):b=rcmail.html2plain(b,
  41. n);!b&&d&&this.editor.dom.setHTML("_rc_sig",d)}return b};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 c=this.editor;c?c.plugins&&c.plugins.spellchecker&&this.spellcheck_active&&(c.execCommand("mceSpellCheck",!1),this.spellcheck_observer()):(c=this.spellchecker)&&c.state&&"ready"!=c.state&&"no_error_found"!=c.state&&
  42. $(c.spell_span).trigger("click")};this.spellcheck_state=function(){var c;if(this.editor)return this.spellcheck_active;if((c=this.spellchecker)&&c.state)return"ready"!=c.state&&"no_error_found"!=c.state};this.spellcheck_resume=function(c){var a=this.editor;if(a)a.plugins.spellchecker.markErrors(c);else if(a=this.spellchecker)a.prepare(!1,!0),a.processData(c)};this.get_language=function(){if(this.editor)return this.editor.settings.spellchecker_language||rcmail.env.spell_lang;if(this.spellchecker)return GOOGIE_CUR_LANG};
  43. this.set_language=function(c){var a=this.editor;a&&(a.settings.spellchecker_language=c);(a=this.spellchecker)&&a.setCurrentLanguage(c)};this.replace=function(c){var a=this.editor;if(a)a.getWin().focus(),a.selection.setContent(rcmail.quote_html(c).replace(/\r?\n/g,"<br/>"),{format:"text"});else if(a=rcube_find_object(this.id)){var d=$(a).is(":focus")?rcmail.get_input_selection(a):{start:0,end:0},b=a.value;pre=b.substring(0,d.start);end=b.substring(d.end,b.length);a.value=pre+c+end;rcmail.set_caret_pos(a,
  44. d.start+c.length);a.focus()}};this.get_content=function(c){var a=this.editor,d="",b=!1;c=$.extend({refresh:!0,selection:!1,nosig:!1,format:"html"},c);c.refresh&&this.spellcheck_stop();if(a)c.selection&&(d=a.selection.getContent({format:c.format})),d||(d=a.getContent({format:c.format}),b="text"==c.format);else if(a=rcube_find_object(this.id))c.selection&&$(a).is(":focus")&&(d=rcmail.get_input_selection(a).text),d||(d=a.value,b=!0);b&&c.nosig&&(c=d.indexOf("-- \n"),0<c&&(d=d.substring(0,c)));return d};
  45. this.change_signature=function(c,a){var d,b;b=-1;var f=$("#"+this.id),e=f.val(),g=rcmail.env.identity;this.editor?a&&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)):
  46. (e.appendChild(f),d=rcmail.env.top_posting&&rcmail.env.compose_mode?e.firstChild:$(f).prev())),f.innerHTML=rcmail.env.signatures[c]?rcmail.env.signatures[c].html:""):rcmail.env.top_posting||(d=$(this.editor.getBody()).children().last()):(a&&g&&rcmail.env.signatures&&rcmail.env.signatures[g]&&(g=rcmail.env.signatures[g].text,g=g.replace(/\r\n/g,"\n"),b=rcmail.env.top_posting?e.indexOf(g):e.lastIndexOf(g),0<=b&&(e=e.substring(0,b)+e.substring(b+g.length,e.length))),a&&rcmail.env.signatures&&rcmail.env.signatures[c]?
  47. (g=rcmail.env.signatures[c].text,g=g.replace(/\r\n/g,"\n"),0<=b?(e=e.substring(0,b)+g+e.substring(b,e.length),b-=1):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),b=pos):(e="\n\n"+g+"\n\n"+e.replace(/^[\r\n]+/,""),b=0):(e=e.replace(/[\r\n]+$/,""),b=!rcmail.env.top_posting&&e.length?e.length+1:0,e+="\n\n"+g):(b=e.length,e+="\n\n"+g)):b=rcmail.env.top_posting?0:e.length,f.val(e),
  48. rcmail.set_caret_pos(f.get(0),b));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(c,a,d){var b,f,e,g=[];a=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>',
  49. buttons:[{text:"Cancel",onclick:function(){h.file_browser_close()}}]});rcmail.env.file_browser_field=c;rcmail.env.file_browser_type=d;for(b in rcmail.env.attachments)(f=h.file_browser_entry(b,rcmail.env.attachments[b]))&&g.push(f);g.length&&$("#image-selector-list > ul").append(g).find("li:first").focus();$("div.mce-abs-end",a.getEl()).append($('<div class="hint">').text($("div.hint",rcmail.gui_objects.uploadform).text()));f=$("#image-upload-button").append($("<span>").text(rcmail.get_label("add"+
  50. 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||
  51. (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",
  52. 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(c){var a=$("#"+rcmail.env.file_browser_field);c&&a.val(c);this.editor.windowManager.close();a.focus();rcmail.env.old_file_drop&&(rcmail.gui_objects.filedrop=rcmail.env.old_file_drop)};this.file_browser_entry=function(c,a){if(a.complete&&a.mimetype){rcmail.file_upload_id&&rcmail.set_busy(!1,null,rcmail.file_upload_id);var d,b;switch(rcmail.env.file_browser_type){case "image":d=
  53. /^image\//i;break;case "media":d=/^video\//i;b="program/js/tinymce/roundcube/video.png";break;default:return}if(d.test(a.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="+c,b=$("<img>").attr({title:a.name,src:b?b:d+"&_thumbnail=1"}),$("<li>").attr({tabindex:0}).data("url",d).append($('<span class="img">').append(b)).append($('<span class="name">').text(a.name)).click(function(){h.file_browser_close($(this).data("url"))}).keydown(function(a){if(13==
  54. 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(c,a){function d(a){b||(b=f.offset());e.css({top:a.pageY-b.top-10+"px",left:a.pageX-b.left-10+"px"})}var b,f=$(c),e=$("<input>").attr("name","_file[]"),g=$("<form>").attr({method:"post",
  55. enctype:"multipart/form-data"});a&&(e.attr("name",$('input[type="file"]',a).attr("name")),g.attr("action",$(a).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/)&&
  56. 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)}};