plugin.js 8.71 KB
Newer Older
Julien Jorry committed
1
!function(){var t=!1;CKEDITOR.plugins.add("balloonpanel",{init:function(){t||(CKEDITOR.document.appendStyleSheet(this.path+"skins/"+CKEDITOR.skin.name+"/balloonpanel.css"),t=!0)}}),CKEDITOR.ui.balloonPanel=function(t,e){for(var i in this.editor=t,CKEDITOR.tools.extend(this,{width:360,height:"auto",triangleWidth:20,triangleHeight:20,triangleMinDistance:40},e,!0),this.templates={},this.templateDefinitions)this.templates[i]=new CKEDITOR.template(this.templateDefinitions[i]);this.parts={},this.focusables={},this.showListeners={},this.activeShowListeners={},this.rect={visible:!1},this.build(),t.on("destroy",(function(){this.destroy()}),this)},CKEDITOR.ui.balloonPanel.prototype={templateDefinitions:{panel:'<div class="cke {id} cke_reset_all cke_chrome cke_balloon cke_editor_{name} cke_{langDir} '+CKEDITOR.env.cssClass+'" dir="{langDir}" title="'+(CKEDITOR.env.gecko?" ":"")+'" lang="{langCode}" role="dialog" style="{style}" tabindex="-1" aria-labelledby="cke_{name}_arialbl"></div>',content:'<div class="cke_balloon_content">{content}</div>',title:'<div class="cke_balloon_title" role="presentation">{title}</div>',close:'<a class="cke_balloon_close_button" href="javascript:void(0)" title="Close" role="button" tabindex="-1"><span class="cke_label">X</span></a>',triangleOuter:'<span class="cke_balloon_triangle cke_balloon_triangle_outer"></span>',triangleInner:'<span class="cke_balloon_triangle cke_balloon_triangle_inner">&#8203;</span>'},build:function(){var t=this.editor;this.parts={title:CKEDITOR.dom.element.createFromHtml(this.templates.title.output({title:this.title})),close:CKEDITOR.dom.element.createFromHtml(this.templates.close.output()),panel:CKEDITOR.dom.element.createFromHtml(this.templates.panel.output({id:t.id,langDir:t.lang.dir,langCode:t.langCode,name:t.name,style:"display:none;",voiceLabel:t.lang.editorPanel+", "+t.name})),content:CKEDITOR.dom.element.createFromHtml(this.templates.content.output({content:this.content||""})),triangleOuter:CKEDITOR.dom.element.createFromHtml(this.templates.triangleOuter.output()),triangleInner:CKEDITOR.dom.element.createFromHtml(this.templates.triangleInner.output())},this.parts.panel.append(this.parts.title,1),this.parts.panel.append(this.parts.close,1),this.parts.panel.append(this.parts.triangleOuter),this.parts.panel.append(this.parts.content),this.parts.triangleOuter.append(this.parts.triangleInner),this.registerFocusable(this.parts.panel),this.registerFocusable(this.parts.close),this.parts.title.unselectable(),this.parts.close.unselectable(),CKEDITOR.document.getBody().append(this.parts.panel),this.resize(this.width,this.height),this.on("show",this.activateShowListeners,this),this.on("hide",this.deactivateShowListeners,this),this.parts.close.on("click",(function(t){this.hide(),t.data.preventDefault()}),this)},show:function(){this.rect.visible||(this.rect.visible=!0,this.parts.panel.show(),this.fire("show"))},hide:function(){this.rect.visible&&(this.rect.visible=!1,this.parts.panel.hide(),this.blur(),this.fire("hide"))},blur:function(){this.editor.focus()},move:function(t,e){this.rect.left=e,this.rect.top=t,this.parts.panel.setStyles({left:CKEDITOR.tools.cssLength(e),top:CKEDITOR.tools.cssLength(t)})},attach:function(){function t(t,e){return Math.max(0,Math.min(t.right,e.right)-Math.max(t.left,e.left))*Math.max(0,Math.min(t.bottom,e.bottom)-Math.max(t.top,e.top))}function e(t,e,i,n){return(t={top:t,left:e}).right=t.left+i,t.bottom=t.top+n,t}function i(t,e){return t.right=e.right,t.width=t.right-t.left,e.y&&(t.y=e.y),t}function n(t){var e=s(t,!0);return t=s(t),e=i(e[0],e.pop()),t=i(t[0],t.pop()),e.bottom=t.bottom,e.height=e.bottom-e.top,t.y&&(e.y=t.y),t.top=e.top,t.height=e.height,[e,t]}function s(t,e){var i=e?t[0]:t[t.length-1],n=e?"top":"bottom";return CKEDITOR.tools.array.filter(t,(function(t){if(t[n]===i[n])return t}))}var o,a,l,h,r={right:"left",top:"bottom",topLeft:"bottomLeft",topRight:"bottomRight",bottom:"top",bottomLeft:"topLeft",bottomRight:"topRight",left:"right"};return function(s,c){if(s instanceof CKEDITOR.dom.selection){var g,p=s.getRanges();p=s.isFake&&s.isInTable()?CKEDITOR.tools.array.map(p,(function(t){return t.getClientRects(!0)[0]})):p[p.length-1].getClientRects(!0);g=(m=p[0])===(b=p[p.length-1])?[m]:m.top===b.top?[i(m,b)]:n(p)}(c instanceof CKEDITOR.dom.element||!c)&&(c={focusElement:c}),!0===(c=CKEDITOR.tools.extend(c,{show:!0})).show&&this.show(),this.fire("attach"),o=CKEDITOR.document.getWindow(),a=this.editor.window.getFrame(),l=this.editor.editable(),!(h=l.isInline())&&CKEDITOR.env.safari&&(a=a.getParent());var m,d,u,f,b=(p=this.getWidth())*(m=this.getHeight());d=s.getClientRect&&s.getClientRect(!0);var v=h?l.getClientRect(!0):a.getClientRect(!0),_=o.getViewPaneSize(),w=o.getScrollPosition(),C={top:Math.max(v.top,w.y),left:Math.max(v.left,w.x),right:Math.min(v.right,_.width+w.x),bottom:Math.min(v.bottom,_.height+w.y)};for(var R in h&&this.editor.elementMode===CKEDITOR.ELEMENT_MODE_INLINE&&((C=this._getViewPaneRect(o)).right+=this.triangleWidth,C.bottom+=this.triangleHeight),g?(CKEDITOR.tools.array.forEach(g,(function(t){this._adjustElementRect(t,h?C:v)}),this),d=this._getAlignments(g[0],p,m),1<g.length&&(d["bottom hcenter"]=this._getAlignments(g[1],p,m)["bottom hcenter"]),f={"top hcenter":!0,"bottom hcenter":!0}):(this._adjustElementRect(d,h?C:v),d=this._getAlignments(d,p,m)),f||d){if(f=e(d[R].top,d[R].left,p,m),0===(f=d[R].areaDifference=b-t(f,C))){u=R;break}u||(u=R),f<d[u].areaDifference&&(u=R)}R=(f=this.parts.panel.getAscendant((function(t){return!(t instanceof CKEDITOR.dom.document)&&"static"!==t.getComputedStyle("position")})))?parseInt(f.getComputedStyle("margin-left"),10):0,f=f?parseInt(f.getComputedStyle("margin-top"),10):0,this.move(d[u].top-f,d[u].left-R),u=u.split(" "),this.setTriangle(r[u[0]],u[1]),!1!==c.focusElement&&(c.focusElement||this.parts.panel).focus()}}(),resize:function(t,e){this.rect.width=t,this.rect.height=e,this.parts.panel.setStyles({width:CKEDITOR.tools.cssLength(t),height:CKEDITOR.tools.cssLength(e)})},getWidth:function(){return"auto"===this.rect.width?this.parts.panel.getClientRect().width:this.rect.width},getHeight:function(){return"auto"===this.rect.height?this.parts.panel.getClientRect().height:this.rect.height},setTriangle:function(t,e){var i=this.parts.triangleOuter,n=this.parts.triangleInner;this.triangleSide&&(i.removeClass("cke_balloon_triangle_"+this.triangleSide),i.removeClass("cke_balloon_triangle_align_"+this.triangleAlign),n.removeClass("cke_balloon_triangle_"+this.triangleSide)),this.triangleSide=t,this.triangleAlign=e,i.addClass("cke_balloon_triangle_"+t),i.addClass("cke_balloon_triangle_align_"+e),n.addClass("cke_balloon_triangle_"+t)},registerFocusable:function(t){this.editor.focusManager.add(t),this.focusables[t.getUniqueId()]=t},deregisterFocusable:function(t){this.editor.focusManager.remove(t),delete this.focusables[t.getUniqueId()]},addShowListener:function(t){var e=CKEDITOR.tools.getNextNumber();this.showListeners[e]=t,this.rect.visible&&this.activateShowListener(e);var i=this;return{removeListener:function(){i.removeShowListener(e)}}},removeShowListener:function(t){this.deactivateShowListener(t),delete this.showListeners[t]},activateShowListener:function(t){this.activeShowListeners[t]=this.showListeners[t].call(this)},deactivateShowListener:function(t){this.activeShowListeners[t]&&this.activeShowListeners[t].removeListener(),delete this.activeShowListeners[t]},activateShowListeners:function(){for(var t in this.showListeners)this.activateShowListener(t)},deactivateShowListeners:function(){for(var t in this.activeShowListeners)this.deactivateShowListener(t)},destroy:function(){this.deactivateShowListeners(),this.parts.panel.remove()},setTitle:function(t){this.parts.title.setHtml(t)},_getAlignments:function(t,e,i){return{"right vcenter":{top:t.top+t.height/2-i/2,left:t.right+this.triangleWidth},"left vcenter":{top:t.top+t.height/2-i/2,left:t.left-e-this.triangleWidth},"top hcenter":{top:t.top-i-this.triangleHeight,left:t.left+t.width/2-e/2},"top left":{top:t.top-i-this.triangleHeight,left:t.left+t.width/2-this.triangleMinDistance},"top right":{top:t.top-i-this.triangleHeight,left:t.right-t.width/2-e+this.triangleMinDistance},"bottom hcenter":{top:t.bottom+this.triangleHeight,left:t.left+t.width/2-e/2},"bottom left":{top:t.bottom+this.triangleHeight,left:t.left+t.width/2-this.triangleMinDistance},"bottom right":{top:t.bottom+this.triangleHeight,left:t.right-t.width/2-e+this.triangleMinDistance}}},_adjustElementRect:function(t,e){t.left=Math.max(e.left,Math.min(e.right-1,t.left)),t.right=Math.max(e.left,Math.min(e.right,t.right)),t.top=Math.max(e.top,Math.min(e.bottom-1,t.top)),t.bottom=Math.max(e.top,Math.min(e.bottom,t.bottom))},_getViewPaneRect:function(t){var e=t.getScrollPosition();return t=t.getViewPaneSize(),{top:e.y,bottom:e.y+t.height,left:e.x,right:e.x+t.width}}},CKEDITOR.event.implementOn(CKEDITOR.ui.balloonPanel.prototype)}();