2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('editor-bidi',function(Y){var EditorBidi=function(){EditorBidi.superclass.constructor.apply(this,arguments);},HOST='host',DIR='dir',BODY='BODY',NODE_CHANGE='nodeChange',B_C_CHANGE='bidiContextChange',FIRST_P=BODY+' > p';Y.extend(EditorBidi,Y.Base,{lastDirection:null,firstEvent:null,_checkForChange:function(){var host=this.get(HOST),inst=host.getInstance(),sel=new inst.Selection(),node,direction;if(sel.isCollapsed){node=EditorBidi.blockParent(sel.focusNode);direction=node.getStyle('direction');if(direction!==this.lastDirection){host.fire(B_C_CHANGE,{changedTo:direction});this.lastDirection=direction;}}else{host.fire(B_C_CHANGE,{changedTo:'select'});this.lastDirection=null;}},_afterNodeChange:function(e){if(this.firstEvent||EditorBidi.EVENTS[e.changedType]){this._checkForChange();this.firstEvent=false;}},_afterMouseUp:function(e){this._checkForChange();this.firstEvent=false;},initializer:function(){var host=this.get(HOST);this.firstEvent=true;host.after(NODE_CHANGE,Y.bind(this._afterNodeChange,this));host.after('dom:mouseup',Y.bind(this._afterMouseUp,this));}},{EVENTS:{'backspace-up':true,'pageup-up':true,'pagedown-down':true,'end-up':true,'home-up':true,'left-up':true,'up-up':true,'right-up':true,'down-up':true,'delete-up':true},BLOCKS:Y.Selection.BLOCKS+',LI,HR,'+BODY,DIV_WRAPPER:'<DIV></DIV>',blockParent:function(node,wrap){var parent=node,divNode,firstChild;if(!parent){parent=Y.one(BODY);}
9 if(!parent.test(EditorBidi.BLOCKS)){parent=parent.ancestor(EditorBidi.BLOCKS);}
10 if(wrap&&parent.test(BODY)){divNode=Y.Node.create(EditorBidi.DIV_WRAPPER);parent.get('children').each(function(node,index){if(index===0){firstChild=node;}else{divNode.append(node);}});firstChild.replace(divNode);divNode.prepend(firstChild);parent=divNode;}
11 return parent;},_NODE_SELECTED:'bidiSelected',addParents:function(nodeArray){var i,parent,addParent;for(i=0;i<nodeArray.length;i+=1){nodeArray[i].setData(EditorBidi._NODE_SELECTED,true);}
12 for(i=0;i<nodeArray.length;i+=1){parent=nodeArray[i].get('parentNode');if(!parent.test(BODY)&&!parent.getData(EditorBidi._NODE_SELECTED)){addParent=true;parent.get('children').some(function(sibling){if(!sibling.getData(EditorBidi._NODE_SELECTED)){addParent=false;return true;}});if(addParent){nodeArray.push(parent);parent.setData(EditorBidi._NODE_SELECTED,true);}}}
13 for(i=0;i<nodeArray.length;i+=1){nodeArray[i].clearData(EditorBidi._NODE_SELECTED);}
14 return nodeArray;},NAME:'editorBidi',NS:'editorBidi',ATTRS:{host:{value:false}}});Y.namespace('Plugin');Y.Plugin.EditorBidi=EditorBidi;Y.Plugin.ExecCommand.COMMANDS.bidi=function(cmd,direction){var inst=this.getInstance(),sel=new inst.Selection(),returnValue,block,selected,selectedBlocks,dir;inst.Selection.filterBlocks();if(sel.isCollapsed){block=EditorBidi.blockParent(sel.anchorNode);if(!direction){dir=block.getAttribute(DIR);if(!dir||dir=='ltr'){direction='rtl';}else{direction='ltr';}}
15 block.setAttribute(DIR,direction);returnValue=block;}else{selected=sel.getSelected();selectedBlocks=[];selected.each(function(node){selectedBlocks.push(EditorBidi.blockParent(node));});selectedBlocks=inst.all(EditorBidi.addParents(selectedBlocks));selectedBlocks.setAttribute(DIR,direction);returnValue=selectedBlocks;}
16 this.get(HOST).get(HOST).editorBidi._checkForChange();return returnValue;};},'3.3.0',{requires:['editor-base'],skinnable:false});