]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/yui3/build/editor/exec-command.js
Release 6.2.2
[Github/sugarcrm.git] / include / javascript / yui3 / build / editor / exec-command.js
1 /*
2  Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3  Code licensed under the BSD License:
4  http://developer.yahoo.com/yui/license.html
5  version: 3.3.0
6  build: 3167
7  */
8 YUI.add('exec-command',function(Y){var ExecCommand=function(){ExecCommand.superclass.constructor.apply(this,arguments);};Y.extend(ExecCommand,Y.Base,{_lastKey:null,_inst:null,command:function(action,value){var fn=ExecCommand.COMMANDS[action];if(fn){return fn.call(this,action,value);}else{return this._command(action,value);}},_command:function(action,value){var inst=this.getInstance();try{try{inst.config.doc.execCommand('styleWithCSS',null,1);}catch(e1){try{inst.config.doc.execCommand('useCSS',null,0);}catch(e2){}}
9 inst.config.doc.execCommand(action,null,value);}catch(e){}},getInstance:function(){if(!this._inst){this._inst=this.get('host').getInstance();}
10 return this._inst;},initializer:function(){Y.mix(this.get('host'),{execCommand:function(action,value){return this.exec.command(action,value);},_execCommand:function(action,value){return this.exec._command(action,value);}});this.get('host').on('dom:keypress',Y.bind(function(e){this._lastKey=e.keyCode;},this));}},{NAME:'execCommand',NS:'exec',ATTRS:{host:{value:false}},COMMANDS:{wrap:function(cmd,tag){var inst=this.getInstance();return(new inst.Selection()).wrapContent(tag);},inserthtml:function(cmd,html){var inst=this.getInstance();if(inst.Selection.hasCursor()||Y.UA.ie){return(new inst.Selection()).insertContent(html);}else{this._command('inserthtml',html);}},insertandfocus:function(cmd,html){var inst=this.getInstance(),out,sel;if(inst.Selection.hasCursor()){html+=inst.Selection.CURSOR;out=this.command('inserthtml',html);sel=new inst.Selection();sel.focusCursor(true,true);}else{this.command('inserthtml',html);}
11 return out;},insertbr:function(cmd){var inst=this.getInstance(),cur,sel=new inst.Selection();sel.setCursor();cur=sel.getCursor();cur.insert('<br>','before');sel.focusCursor(true,false);return((cur&&cur.previous)?cur.previous():null);},insertimage:function(cmd,img){return this.command('inserthtml','<img src="'+img+'">');},addclass:function(cmd,cls){var inst=this.getInstance();return(new inst.Selection()).getSelected().addClass(cls);},removeclass:function(cmd,cls){var inst=this.getInstance();return(new inst.Selection()).getSelected().removeClass(cls);},forecolor:function(cmd,val){var inst=this.getInstance(),sel=new inst.Selection(),n;if(!Y.UA.ie){this._command('useCSS',false);}
12 if(inst.Selection.hasCursor()){if(sel.isCollapsed){if(sel.anchorNode&&(sel.anchorNode.get('innerHTML')==='&nbsp;')){sel.anchorNode.setStyle('color',val);n=sel.anchorNode;}else{n=this.command('inserthtml','<span style="color: '+val+'">'+inst.Selection.CURSOR+'</span>');sel.focusCursor(true,true);}
13 return n;}else{return this._command(cmd,val);}}else{this._command(cmd,val);}},backcolor:function(cmd,val){var inst=this.getInstance(),sel=new inst.Selection(),n;if(Y.UA.gecko||Y.UA.opera){cmd='hilitecolor';}
14 if(!Y.UA.ie){this._command('useCSS',false);}
15 if(inst.Selection.hasCursor()){if(sel.isCollapsed){if(sel.anchorNode&&(sel.anchorNode.get('innerHTML')==='&nbsp;')){sel.anchorNode.setStyle('backgroundColor',val);n=sel.anchorNode;}else{n=this.command('inserthtml','<span style="background-color: '+val+'">'+inst.Selection.CURSOR+'</span>');sel.focusCursor(true,true);}
16 return n;}else{return this._command(cmd,val);}}else{this._command(cmd,val);}},hilitecolor:function(){return ExecCommand.COMMANDS.backcolor.apply(this,arguments);},fontname:function(cmd,val){this._command('fontname',val);var inst=this.getInstance(),sel=new inst.Selection();if(sel.isCollapsed&&(this._lastKey!=32)){if(sel.anchorNode.test('font')){sel.anchorNode.set('face',val);}}},fontsize:function(cmd,val){this._command('fontsize',val);var inst=this.getInstance(),sel=new inst.Selection();if(sel.isCollapsed&&sel.anchorNode&&(this._lastKey!=32)){if(Y.UA.webkit){if(sel.anchorNode.getStyle('lineHeight')){sel.anchorNode.setStyle('lineHeight','');}}
17 if(sel.anchorNode.test('font')){sel.anchorNode.set('size',val);}else if(Y.UA.gecko){var p=sel.anchorNode.ancestor(inst.Selection.DEFAULT_BLOCK_TAG);if(p){p.setStyle('fontSize','');}}}}}});var fixIETags=function(cmd,tag,rule){var inst=this.getInstance(),doc=inst.config.doc,sel=doc.selection.createRange(),o=doc.queryCommandValue(cmd),html,reg,m,p,d,s,c;if(o){html=sel.htmlText;reg=new RegExp(rule,'g');m=html.match(reg);if(m){html=html.replace(rule+';','').replace(rule,'');sel.pasteHTML('<var id="yui-ie-bs">');p=doc.getElementById('yui-ie-bs');d=doc.createElement('div');s=doc.createElement(tag);d.innerHTML=html;if(p.parentNode!==inst.config.doc.body){p=p.parentNode;}
18 c=d.childNodes;p.parentNode.replaceChild(s,p);Y.each(c,function(f){s.appendChild(f);});sel.collapse();sel.moveToElementText(s);sel.select();}}
19 this._command(cmd);};if(Y.UA.ie){ExecCommand.COMMANDS.bold=function(){fixIETags.call(this,'bold','b','FONT-WEIGHT: bold');}
20 ExecCommand.COMMANDS.italic=function(){fixIETags.call(this,'italic','i','FONT-STYLE: italic');}
21 ExecCommand.COMMANDS.underline=function(){fixIETags.call(this,'underline','u','TEXT-DECORATION: underline');}}
22 Y.namespace('Plugin');Y.Plugin.ExecCommand=ExecCommand;},'3.3.0',{requires:['frame'],skinnable:false});