1 line
4.4 KiB
JavaScript
1 line
4.4 KiB
JavaScript
tinymce.PluginManager.add("link",function(e){function t(t){return function(){var n=e.settings.link_list;if(typeof n=="string"){tinymce.util.XHR.send({url:n,success:function(e){t(tinymce.util.JSON.parse(e))}})}else{t(n)}}}function n(t){function p(e){var t=a.find("#text");if(!t.value()||e.lastControl&&t.value()==e.lastControl.text()){t.value(e.control.text())}a.find("#href").value(e.control.value())}function d(){var n=[{text:"None",value:""}];tinymce.each(t,function(t){n.push({text:t.text||t.title,value:e.convertURL(t.value||t.url,"href"),menu:t.menu})});return n}function v(t){var n=[{text:"None",value:""}];tinymce.each(e.settings.rel_list,function(e){n.push({text:e.text||e.title,value:e.value,selected:t===e.value})});return n}function m(){var t=[];if(!e.settings.style_list){t.push({text:"None",value:""});t.push({text:"button",value:"btn btn-default"})}tinymce.each(e.settings.style_list,function(e){t.push({text:e.text||e.title,value:e.value,selected:targetValue===e.value})});return t}function g(t){var n=[];if(!e.settings.target_list){n.push({text:"None",value:""});n.push({text:"New window",value:"_blank"})}tinymce.each(e.settings.target_list,function(e){n.push({text:e.text||e.title,value:e.value,selected:t===e.value})});return n}function y(t){var n=[];tinymce.each(e.dom.select("a:not([href])"),function(e){var r=e.name||e.id;if(r){n.push({text:r,value:"#"+r,selected:t.indexOf("#"+r)!=-1})}});if(n.length){n.unshift({text:"None",value:""});return{name:"anchor",type:"listbox",label:"Anchors",values:n,onselect:p}}}function b(){if(l){l.value(e.convertURL(this.value(),"href"))}if(!u&&n.text.length===0&&w){this.parent().parent().find("#text")[0].value(this.value())}}var n={},r=e.selection,i=e.dom,s,o,u;var a,f,l,c,h;s=r.getNode();o=i.getParent(s,"a[href]");var w=true;if(/</.test(r.getContent())){w=false}else if(o){var E=o.childNodes,S;if(E.length===0){w=false}else{for(S=E.length-1;S>=0;S--){if(E[S].nodeType!=3){w=false;break}}}}n.text=u=o?o.innerText||o.textContent:r.getContent({format:"text"});n.href=o?i.getAttrib(o,"href"):"";n.target=o?i.getAttrib(o,"target"):e.settings.default_link_target||"";n.rel=o?i.getAttrib(o,"rel"):"";n.button_style=i.getAttrib(o,"button_style");if(w){f={name:"text",type:"textbox",size:40,label:"Text to display",onchange:function(){n.text=this.value()}}}if(t){l={type:"listbox",label:"Link list",values:d(),onselect:p,value:e.convertURL(n.href,"href"),onPostRender:function(){l=this}}}buttonStyle={name:"button_style",type:"listbox",label:"button style",values:m()};if(e.settings.target_list!==false){h={name:"target",type:"listbox",label:"Target",values:g(n.target)}}if(e.settings.rel_list){c={name:"rel",type:"listbox",label:"Rel",values:v(n.rel)}}a=e.windowManager.open({title:"Insert link",data:n,body:[{name:"href",type:"filepicker",filetype:"file",size:40,autofocus:true,label:"Url",onchange:b,onkeyup:b},f,y(n.href),l,c,h,buttonStyle],onSubmit:function(t){function a(t,n){var r=e.selection.getRng();window.setTimeout(function(){e.windowManager.confirm(t,function(t){e.selection.setRng(r);n(t)})},0)}function f(){console.log(n);console.log(o);if(o){e.focus();if(w&&n.text!=u){o.innerText=n.text}i.setAttribs(o,{href:s,target:n.target?n.target:null,rel:n.rel?n.rel:null,"class":n.button_style?n.button_style:null});r.select(o);e.undoManager.add()}else{if(w){e.insertContent(i.createHTML("a",{href:s,target:n.target?n.target:null,rel:n.rel?n.rel:null,"class":n.button_style?n.button_style:null},i.encode(n.text)))}else{e.execCommand("mceInsertLink",false,{href:s,target:n.target,rel:n.rel?n.rel:null,"class":n.button_style?n.button_style:null})}}}var n=t.data,s=n.href;if(!s){e.execCommand("unlink");return}if(s.indexOf("@")>0&&s.indexOf("//")==-1&&s.indexOf("mailto:")==-1){a("The URL you entered seems to be an email address. Do you want to add the required mailto: prefix?",function(e){if(e){s="mailto:"+s}f()});return}if(/^\s*www\./i.test(s)){a("The URL you entered seems to be an external link. Do you want to add the required http:// prefix?",function(e){if(e){s="http://"+s}f()});return}f()}})}e.addButton("link",{icon:"link",tooltip:"Insert/edit link",shortcut:"Ctrl+K",onclick:t(n),stateSelector:"a[href]"});e.addButton("unlink",{icon:"unlink",tooltip:"Remove link",cmd:"unlink",stateSelector:"a[href]"});e.addShortcut("Ctrl+K","",t(n));this.showDialog=n;e.addMenuItem("link",{icon:"link",text:"Insert link",shortcut:"Ctrl+K",onclick:t(n),stateSelector:"a[href]",context:"insert",prependToContext:true})}) |