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) {
13 * Plugin for Editor to support BiDirectional (bidi) text operations.
15 * @submodule editor-bidi
18 * Plugin for Editor to support BiDirectional (bidi) text operations.
19 * @class Plugin.EditorBidi
25 var EditorBidi = function() {
26 EditorBidi.superclass.constructor.apply(this, arguments);
27 }, HOST = 'host', DIR = 'dir', BODY = 'BODY', NODE_CHANGE = 'nodeChange',
28 B_C_CHANGE = 'bidiContextChange', FIRST_P = BODY + ' > p';
30 Y.extend(EditorBidi, Y.Base, {
32 * Place holder for the last direction when checking for a switch
34 * @property lastDirection
38 * Tells us that an initial bidi check has already been performed
40 * @property firstEvent
45 * Method checks to see if the direction of the text has changed based on a nodeChange event.
47 * @method _checkForChange
49 _checkForChange: function() {
50 var host = this.get(HOST),
51 inst = host.getInstance(),
52 sel = new inst.Selection(),
55 if (sel.isCollapsed) {
56 node = EditorBidi.blockParent(sel.focusNode);
57 direction = node.getStyle('direction');
58 if (direction !== this.lastDirection) {
59 host.fire(B_C_CHANGE, { changedTo: direction });
60 this.lastDirection = direction;
63 host.fire(B_C_CHANGE, { changedTo: 'select' });
64 this.lastDirection = null;
69 * Checked for a change after a specific nodeChange event has been fired.
71 * @method _afterNodeChange
73 _afterNodeChange: function(e) {
74 // If this is the first event ever, or an event that can result in a context change
75 if (this.firstEvent || EditorBidi.EVENTS[e.changedType]) {
76 this._checkForChange();
77 this.firstEvent = false;
82 * Checks for a direction change after a mouseup occurs.
84 * @method _afterMouseUp
86 _afterMouseUp: function(e) {
87 this._checkForChange();
88 this.firstEvent = false;
90 initializer: function() {
91 var host = this.get(HOST);
93 this.firstEvent = true;
95 host.after(NODE_CHANGE, Y.bind(this._afterNodeChange, this));
96 host.after('dom:mouseup', Y.bind(this._afterMouseUp, this));
100 * The events to check for a direction change on
105 'backspace-up': true,
107 'pagedown-down': true,
118 * More elements may be needed. BODY *must* be in the list to take care of the special case.
120 * blockParent could be changed to use inst.Selection.BLOCKS
121 * instead, but that would make Y.Plugin.EditorBidi.blockParent
122 * unusable in non-RTE contexts (it being usable is a nice
127 BLOCKS: Y.Selection.BLOCKS+',LI,HR,' + BODY,
129 * Template for creating a block element
131 * @property DIV_WRAPPER
133 DIV_WRAPPER: '<DIV></DIV>',
135 * Returns a block parent for a given element
137 * @method blockParent
139 blockParent: function(node, wrap) {
140 var parent = node, divNode, firstChild;
143 parent = Y.one(BODY);
146 if (!parent.test(EditorBidi.BLOCKS)) {
147 parent = parent.ancestor(EditorBidi.BLOCKS);
149 if (wrap && parent.test(BODY)) {
150 // This shouldn't happen if the RTE handles everything
151 // according to spec: we should get to a P before BODY. But
152 // we don't want to set the direction of BODY even if that
153 // happens, so we wrap everything in a DIV.
155 // The code is based on YUI3's Y.Selection._wrapBlock function.
156 divNode = Y.Node.create(EditorBidi.DIV_WRAPPER);
157 parent.get('children').each(function(node, index) {
161 divNode.append(node);
164 firstChild.replace(divNode);
165 divNode.prepend(firstChild);
171 * The data key to store on the node.
173 * @property _NODE_SELECTED
175 _NODE_SELECTED: 'bidiSelected',
177 * Generates a list of all the block parents of the current NodeList
181 addParents: function(nodeArray) {
182 var i, parent, addParent;
184 for (i = 0; i < nodeArray.length; i += 1) {
185 nodeArray[i].setData(EditorBidi._NODE_SELECTED, true);
188 // This works automagically, since new parents added get processed
189 // later themselves. So if there's a node early in the process that
190 // we haven't discovered some of its siblings yet, thus resulting in
191 // its parent not added, the parent will be added later, since those
192 // siblings will be added to the array and then get processed.
193 for (i = 0; i < nodeArray.length; i += 1) {
194 parent = nodeArray[i].get('parentNode');
196 // Don't add the parent if the parent is the BODY element.
197 // We don't want to change the direction of BODY. Also don't
198 // do it if the parent is already in the list.
199 if (!parent.test(BODY) && !parent.getData(EditorBidi._NODE_SELECTED)) {
201 parent.get('children').some(function(sibling) {
202 if (!sibling.getData(EditorBidi._NODE_SELECTED)) {
204 return true; // stop more processing
208 nodeArray.push(parent);
209 parent.setData(EditorBidi._NODE_SELECTED, true);
214 for (i = 0; i < nodeArray.length; i += 1) {
215 nodeArray[i].clearData(EditorBidi._NODE_SELECTED);
241 Y.namespace('Plugin');
243 Y.Plugin.EditorBidi = EditorBidi;
246 * bidi execCommand override for setting the text direction of a node.
247 * @for Plugin.ExecCommand
248 * @property COMMANDS.bidi
251 Y.Plugin.ExecCommand.COMMANDS.bidi = function(cmd, direction) {
252 var inst = this.getInstance(),
253 sel = new inst.Selection(),
255 selected, selectedBlocks, dir;
257 inst.Selection.filterBlocks();
258 if (sel.isCollapsed) { // No selection
259 block = EditorBidi.blockParent(sel.anchorNode);
261 //If no direction is set, auto-detect the proper setting to make it "toggle"
262 dir = block.getAttribute(DIR);
263 if (!dir || dir == 'ltr') {
269 block.setAttribute(DIR, direction);
271 } else { // some text is selected
272 selected = sel.getSelected();
274 selected.each(function(node) {
276 * Temporarily removed this check, should already be fixed
277 * in Y.Selection.getSelected()
279 //if (!node.test(BODY)) { // workaround for a YUI bug
280 selectedBlocks.push(EditorBidi.blockParent(node));
283 selectedBlocks = inst.all(EditorBidi.addParents(selectedBlocks));
284 selectedBlocks.setAttribute(DIR, direction);
285 returnValue = selectedBlocks;
288 this.get(HOST).get(HOST).editorBidi._checkForChange();
295 }, '3.3.0' ,{requires:['editor-base'], skinnable:false});