engine/public/javascripts/admin/aloha/plugins/com.gentics.aloha.plugins.Link/plugin.js

7 lines
9.6 KiB
JavaScript

/*
* Aloha Editor
* Author & Copyright (c) 2010 Gentics Software GmbH
* aloha-sales@gentics.com
* Licensed unter the terms of http://www.aloha-editor.com/license.html
*/
GENTICS.Aloha.Link=new GENTICS.Aloha.Plugin("com.gentics.aloha.plugins.Link");GENTICS.Aloha.Link.languages=["en","de","fr"];GENTICS.Aloha.Link.config=["a"];GENTICS.Aloha.Link.targetregex="";GENTICS.Aloha.Link.target="";GENTICS.Aloha.Link.cssclassregex="";GENTICS.Aloha.Link.cssclass="";GENTICS.Aloha.Link.mouseOverLink=null;GENTICS.Aloha.Link.ressource=null;GENTICS.Aloha.Link.init=function(){if(GENTICS.Aloha.Link.settings.targetregex!=undefined){GENTICS.Aloha.Link.targetregex=GENTICS.Aloha.Link.settings.targetregex}if(GENTICS.Aloha.Link.settings.target!=undefined){GENTICS.Aloha.Link.target=GENTICS.Aloha.Link.settings.target}if(GENTICS.Aloha.Link.settings.cssclassregex!=undefined){GENTICS.Aloha.Link.cssclassregex=GENTICS.Aloha.Link.settings.cssclassregex}if(GENTICS.Aloha.Link.settings.cssclass!=undefined){GENTICS.Aloha.Link.cssclass=GENTICS.Aloha.Link.settings.cssclass}if(GENTICS.Aloha.Link.settings.ressource!=undefined){GENTICS.Aloha.Link.ressource=GENTICS.Aloha.Link.settings.ressource}this.initButtons();this.subscribeEvents();this.bindInteractions()};GENTICS.Aloha.Link.initButtons=function(){var that=this;this.formatLinkButton=new GENTICS.Aloha.ui.Button({iconClass:"GENTICS_button GENTICS_button_a",size:"small",onclick:function(){that.formatLink()},tooltip:this.i18n("button.addlink.tooltip"),toggle:true});GENTICS.Aloha.FloatingMenu.addButton("GENTICS.Aloha.continuoustext",this.formatLinkButton,GENTICS.Aloha.i18n(GENTICS.Aloha,"floatingmenu.tab.format"),1);this.insertLinkButton=new GENTICS.Aloha.ui.Button({iconClass:"GENTICS_button GENTICS_button_a",size:"small",onclick:function(){that.insertLink(false)},tooltip:this.i18n("button.addlink.tooltip"),toggle:false});GENTICS.Aloha.FloatingMenu.addButton("GENTICS.Aloha.continuoustext",this.insertLinkButton,GENTICS.Aloha.i18n(GENTICS.Aloha,"floatingmenu.tab.insert"),1);GENTICS.Aloha.FloatingMenu.createScope(this.getUID("link"),"GENTICS.Aloha.continuoustext");this.srcFieldButton=new GENTICS.Aloha.Link.SrcField();GENTICS.Aloha.FloatingMenu.addButton(this.getUID("link"),this.srcFieldButton,this.i18n("floatingmenu.tab.link"),1);GENTICS.Aloha.FloatingMenu.addButton(this.getUID("link"),new GENTICS.Aloha.ui.Button({iconClass:"GENTICS_button GENTICS_button_a_remove",size:"small",onclick:function(){that.removeLink()},tooltip:this.i18n("button.removelink.tooltip")}),this.i18n("floatingmenu.tab.link"),1)};GENTICS.Aloha.Link.bindInteractions=function(){var that=this;for(var i=0;i<GENTICS.Aloha.editables.length;i++){GENTICS.Aloha.editables[i].obj.keydown(function(e){if((that.isCrtlDown||that.isMetaDown)&&e.which==76){if(that.findLinkMarkup()){GENTICS.Aloha.FloatingMenu.userActivatedTab=that.i18n("floatingmenu.tab.link");GENTICS.Aloha.FloatingMenu.doLayout();that.srcFieldButton.focus()}else{that.insertLink()}return false}});GENTICS.Aloha.editables[i].obj.find("a").each(function(i){jQuery(this).mouseenter(function(e){GENTICS.Aloha.Log.debug(GENTICS.Aloha.Link,"mouse over link.");that.mouseOverLink=this;that.updateMousePointer()});jQuery(this).mouseleave(function(e){GENTICS.Aloha.Log.debug(GENTICS.Aloha.Link,"mouse left link.");that.moseOverLink=null;that.updateMousePointer()});jQuery(this).click(function(e){if(e.metaKey||e.ctrlKey){GENTICS.Aloha.activeEditable.blur();setTimeout(function(){location.href=e.target},0);e.stopPropagation();return false}})})}jQuery(document).keyup(function(e){switch(e.which){case 17:GENTICS.Aloha.Log.debug(GENTICS.Aloha.Link,"ctrl up");that.isCtrlDown=false;that.updateMousePointer();break;case 91:that.isMetaLDown=false;if(!that.isMetaRDown){that.isMetaDown=false;GENTICS.Aloha.Log.debug(GENTICS.Aloha.Link,"meta up")}that.updateMousePointer();break;case 93:that.isMetaRDown=false;if(!that.isMetaLDown){that.isMetaDown=false;GENTICS.Aloha.Log.debug(GENTICS.Aloha.Link,"meta up")}that.updateMousePointer();break}});jQuery(document).keydown(function(e){switch(e.which){case 17:GENTICS.Aloha.Log.debug(GENTICS.Aloha.Link,"meta ctrl");that.isCtrlDown=true;that.updateMousePointer();break;case 91:GENTICS.Aloha.Log.debug(GENTICS.Aloha.Link,"meta down");that.isMetaLDown=true;that.isMetaDown=true;that.updateMousePointer();break;case 93:GENTICS.Aloha.Log.debug(GENTICS.Aloha.Link,"meta down");that.isMetaRDown=true;that.isMetaDown=true;that.updateMousePointer();break}})};GENTICS.Aloha.Link.updateMousePointer=function(){var that=this;if((that.isCrtlDown||that.isMetaDown)&&that.mouseOverLink!=null){GENTICS.Aloha.Log.debug(GENTICS.Aloha.Link,"set pointer");jQuery(that.mouseOverLink).removeClass("GENTICS_link_text");jQuery(that.mouseOverLink).addClass("GENTICS_link_pointer")}else{jQuery(that.mouseOverLink).removeClass("GENTICS_link_pointer");jQuery(that.mouseOverLink).addClass("GENTICS_link_text")}};GENTICS.Aloha.Link.subscribeEvents=function(){var that=this;GENTICS.Aloha.EventRegistry.subscribe(GENTICS.Aloha,"selectionChanged",function(event,rangeObject){if(GENTICS.Aloha.activeEditable){var config=that.getEditableConfig(GENTICS.Aloha.activeEditable.obj);if(jQuery.inArray("a",config)!=-1){that.formatLinkButton.show();that.insertLinkButton.show()}else{that.formatLinkButton.hide();that.insertLinkButton.hide();return}var foundMarkup=that.findLinkMarkup(rangeObject);if(foundMarkup){that.insertLinkButton.hide();that.formatLinkButton.setPressed(true);GENTICS.Aloha.FloatingMenu.setScope(that.getUID("link"));that.srcFieldButton.setAnchor(foundMarkup)}else{that.formatLinkButton.setPressed(false);that.srcFieldButton.setAnchor(null)}GENTICS.Aloha.FloatingMenu.doLayout()}})};GENTICS.Aloha.Link.findLinkMarkup=function(range){if(typeof range=="undefined"){var range=GENTICS.Aloha.Selection.getRangeObject()}return range.findMarkup(function(){return this.nodeName.toLowerCase()=="a"},GENTICS.Aloha.activeEditable.obj)};GENTICS.Aloha.Link.formatLink=function(){var that=this;var range=GENTICS.Aloha.Selection.getRangeObject();if(GENTICS.Aloha.activeEditable){if(that.findLinkMarkup(range)){that.removeLink()}else{that.insertLink()}}};GENTICS.Aloha.Link.insertLink=function(extendToWord){var that=this;if(that.findLinkMarkup(range)){return}GENTICS.Aloha.FloatingMenu.userActivatedTab=this.i18n("floatingmenu.tab.link");var range=GENTICS.Aloha.Selection.getRangeObject();if(range.isCollapsed()&&extendToWord!=false){GENTICS.Utils.Dom.extendToWord(range)}if(range.isCollapsed()){var linkText=this.i18n("newlink.defaulttext");var newLink=jQuery('<a href="#">'+linkText+"</a>");GENTICS.Utils.Dom.insertIntoDOM(newLink,range,jQuery(GENTICS.Aloha.activeEditable.obj));range.startContainer=range.endContainer=newLink.contents().get(0);range.startOffset=0;range.endOffset=linkText.length}else{var newLink=jQuery('<a href="#"></a>');GENTICS.Utils.Dom.addMarkup(range,newLink,false)}range.select();this.srcFieldButton.focus()};GENTICS.Aloha.Link.removeLink=function(){var that=this;var range=GENTICS.Aloha.Selection.getRangeObject();var foundMarkup=that.findLinkMarkup();if(foundMarkup){GENTICS.Utils.Dom.removeFromDOM(foundMarkup,range,true);GENTICS.Aloha.activeEditable.obj[0].focus();range.select()}};GENTICS.Aloha.Link.makeClean=function(obj){obj.find("a").each(function(){})};Ext.ux.LinkSrcButton=Ext.extend(Ext.Component,{wrapper:null,input:null,anchor:null,onRender:function(){var that=this;Ext.ux.LinkSrcButton.superclass.onRender.apply(this,arguments);this.wrapper=jQuery(this.el.dom);this.input=jQuery('<input id="GENTICS_Aloha_plugin_Link" type="text" style="width:300px">');if(GENTICS.Aloha.Link.settings.ressource||1==1){var data=[{id:1,text:"Link A",url:"/page1"},{id:2,text:"Link B",url:"/page2"},{id:3,text:"Link C",url:"/page3"},{id:4,text:"Link D",url:"/page4"}];this.input.autocomplete(data,{formatItem:function(item){return item.text+'<br /><i style="font-size:75%;color:#555">'+item.url+"</i>"}}).result(function(event,item){that.input.val(item.url);jQuery(that.anchor).attr("id",item.id)})}this.input.keyup(function(event){if(that.anchor){if(that.input.val().length>=2){jQuery(that.anchor).attr("href",that.input.val())}else{jQuery(that.anchor).attr("href","#")}}if(event.keyCode==13||event.keyCode==27){GENTICS.Aloha.activeEditable.obj[0].focus();GENTICS.Aloha.Selection.getRangeObject().select()}else{that.setAttribute(that.input.val(),"target",GENTICS.Aloha.Link.targetregex,GENTICS.Aloha.Link.target);that.setAttribute(that.input.val(),"class",GENTICS.Aloha.Link.cssclassregex,GENTICS.Aloha.Link.cssclass)}});this.input.blur(function(event){if(that.input.val()==""){GENTICS.Aloha.Link.removeLink()}});this.wrapper.append(this.input);if(this.getFocus){this.getFocus=undefined;this.input.select();this.input.focus()}},setAttribute:function(input,attr,regex,value){var that=this;if(typeof value!="undefined"){var setTarget=true;if(typeof regex!="undefined"){var regex=new RegExp(regex);if(input.match(regex)){setTarget=true}else{setTarget=false}}if(setTarget){jQuery(that.anchor).attr(attr,value)}else{jQuery(that.anchor).removeAttr(attr)}}},setAnchor:function(anchor){this.anchor=anchor;if(this.input){if(this.anchor){this.input.val(jQuery(this.anchor).attr("href"))}else{this.input.val("")}}},focus:function(){if(this.input){this.input.select();this.input.focus()}else{this.getFocus=true}}});Ext.reg("linksrcbutton",Ext.ux.LinkSrcButton);GENTICS.Aloha.Link.SrcField=function(){GENTICS.Aloha.ui.Button.apply(this,arguments)};GENTICS.Aloha.Link.SrcField.prototype=new GENTICS.Aloha.ui.Button();GENTICS.Aloha.Link.SrcField.prototype.getExtConfigProperties=function(){return{xtype:"linksrcbutton",id:this.id}};GENTICS.Aloha.Link.SrcField.prototype.setAnchor=function(anchor){if(this.extButton){this.extButton.setAnchor(anchor)}};GENTICS.Aloha.Link.SrcField.prototype.focus=function(){if(this.extButton){this.extButton.focus()}};