2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('editor-bidi', function (Y, NAME) {
12 * Plugin for Editor to support BiDirectional (bidi) text operations.
13 * @class Plugin.EditorBidi
17 * @submodule editor-bidi
21 var EditorBidi = function() {
22 EditorBidi.superclass.constructor.apply(this, arguments);
23 }, HOST = 'host', DIR = 'dir', NODE_CHANGE = 'nodeChange',
24 B_C_CHANGE = 'bidiContextChange', STYLE = 'style';
26 Y.extend(EditorBidi, Y.Base, {
28 * Place holder for the last direction when checking for a switch
30 * @property lastDirection
34 * Tells us that an initial bidi check has already been performed
36 * @property firstEvent
41 * Method checks to see if the direction of the text has changed based on a nodeChange event.
43 * @method _checkForChange
45 _checkForChange: function() {
46 var host = this.get(HOST),
47 inst = host.getInstance(),
48 sel = new inst.EditorSelection(),
51 if (sel.isCollapsed) {
52 node = EditorBidi.blockParent(sel.focusNode, false, inst.EditorSelection.ROOT);
54 direction = node.getStyle('direction');
55 if (direction !== this.lastDirection) {
56 host.fire(B_C_CHANGE, { changedTo: direction });
57 this.lastDirection = direction;
61 host.fire(B_C_CHANGE, { changedTo: 'select' });
62 this.lastDirection = null;
67 * Checked for a change after a specific nodeChange event has been fired.
69 * @method _afterNodeChange
71 _afterNodeChange: function(e) {
72 // If this is the first event ever, or an event that can result in a context change
73 if (this.firstEvent || EditorBidi.EVENTS[e.changedType]) {
74 this._checkForChange();
75 this.firstEvent = false;
80 * Checks for a direction change after a mouseup occurs.
82 * @method _afterMouseUp
84 _afterMouseUp: function() {
85 this._checkForChange();
86 this.firstEvent = false;
88 initializer: function() {
89 var host = this.get(HOST);
91 this.firstEvent = true;
93 host.after(NODE_CHANGE, Y.bind(this._afterNodeChange, this));
94 host.after('dom:mouseup', Y.bind(this._afterMouseUp, this));
98 * The events to check for a direction change on
103 'backspace-up': true,
105 'pagedown-down': true,
116 * More elements may be needed. BODY *must* be in the list to take care of the special case.
118 * blockParent could be changed to use inst.EditorSelection.BLOCKS
119 * instead, but that would make Y.Plugin.EditorBidi.blockParent
120 * unusable in non-RTE contexts (it being usable is a nice
125 //BLOCKS: Y.EditorSelection.BLOCKS+',LI,HR,' + BODY,
126 BLOCKS: Y.EditorSelection.BLOCKS,
128 * Template for creating a block element
130 * @property DIV_WRAPPER
132 DIV_WRAPPER: '<DIV></DIV>',
134 * Returns a block parent for a given element
136 * @method blockParent
138 blockParent: function(node, wrap, root) {
139 var parent = node, divNode, firstChild;
141 root = root || Y.EditorSelection.ROOT;
147 if (!parent.test(EditorBidi.BLOCKS)) {
148 parent = parent.ancestor(EditorBidi.BLOCKS);
150 if (wrap && parent.compareTo(root)) {
151 // This shouldn't happen if the RTE handles everything
152 // according to spec: we should get to a P before ROOT. But
153 // we don't want to set the direction of ROOT even if that
154 // happens, so we wrap everything in a DIV.
156 // The code is based on YUI3's Y.EditorSelection._wrapBlock function.
157 divNode = Y.Node.create(EditorBidi.DIV_WRAPPER);
158 parent.get('children').each(function(node, index) {
162 divNode.append(node);
165 firstChild.replace(divNode);
166 divNode.prepend(firstChild);
172 * The data key to store on the node.
174 * @property _NODE_SELECTED
176 _NODE_SELECTED: 'bidiSelected',
178 * Generates a list of all the block parents of the current NodeList
182 addParents: function(nodeArray, root) {
183 var i, parent, addParent;
184 tester = function(sibling) {
185 if (!sibling.getData(EditorBidi._NODE_SELECTED)) {
187 return true; // stop more processing
191 root = root || Y.EditorSelection.ROOT;
193 for (i = 0; i < nodeArray.length; i += 1) {
194 nodeArray[i].setData(EditorBidi._NODE_SELECTED, true);
197 // This works automagically, since new parents added get processed
198 // later themselves. So if there's a node early in the process that
199 // we haven't discovered some of its siblings yet, thus resulting in
200 // its parent not added, the parent will be added later, since those
201 // siblings will be added to the array and then get processed.
202 for (i = 0; i < nodeArray.length; i += 1) {
203 parent = nodeArray[i].get('parentNode');
205 // Don't add the parent if the parent is the ROOT element.
206 // We don't want to change the direction of ROOT. Also don't
207 // do it if the parent is already in the list.
208 if (!root.compareTo(parent) && !parent.getData(EditorBidi._NODE_SELECTED)) {
210 parent.get('children').some(tester);
212 nodeArray.push(parent);
213 parent.setData(EditorBidi._NODE_SELECTED, true);
218 for (i = 0; i < nodeArray.length; i += 1) {
219 nodeArray[i].clearData(EditorBidi._NODE_SELECTED);
244 * Regex for testing/removing text-align style from an element
246 * @property RE_TEXT_ALIGN
248 RE_TEXT_ALIGN: /text-align:\s*\w*\s*;/,
250 * Method to test a node's style attribute for text-align and removing it.
252 * @method removeTextAlign
254 removeTextAlign: function(n) {
256 if (n.getAttribute(STYLE).match(EditorBidi.RE_TEXT_ALIGN)) {
257 n.setAttribute(STYLE, n.getAttribute(STYLE).replace(EditorBidi.RE_TEXT_ALIGN, ''));
259 if (n.hasAttribute('align')) {
260 n.removeAttribute('align');
267 Y.namespace('Plugin');
269 Y.Plugin.EditorBidi = EditorBidi;
272 * bidi execCommand override for setting the text direction of a node.
273 * This property is added to the `Y.Plugin.ExecCommands.COMMANDS`
276 * @for Plugin.ExecCommand
279 //TODO -- This should not add this command unless the plugin is added to the instance..
280 Y.Plugin.ExecCommand.COMMANDS.bidi = function(cmd, direction) {
281 var inst = this.getInstance(),
282 sel = new inst.EditorSelection(),
283 ns = this.get(HOST).get(HOST).editorBidi,
284 returnValue, block, b,
285 root = inst.EditorSelection.ROOT,
286 selected, selectedBlocks, dir;
289 Y.error('bidi execCommand is not available without the EditorBiDi plugin.');
293 inst.EditorSelection.filterBlocks();
295 if (sel.isCollapsed) { // No selection
296 block = EditorBidi.blockParent(sel.anchorNode, false, root);
298 block = root.one(inst.EditorSelection.BLOCKS);
300 //Remove text-align attribute if it exists
301 block = EditorBidi.removeTextAlign(block);
303 //If no direction is set, auto-detect the proper setting to make it "toggle"
304 dir = block.getAttribute(DIR);
305 if (!dir || dir === 'ltr') {
311 block.setAttribute(DIR, direction);
313 b = block.all('br.yui-cursor');
314 if (b.size() === 1 && block.get('childNodes').size() === 1) {
319 } else { // some text is selected
320 selected = sel.getSelected();
322 selected.each(function(node) {
323 selectedBlocks.push(EditorBidi.blockParent(node, false, root));
325 selectedBlocks = inst.all(EditorBidi.addParents(selectedBlocks, root));
326 selectedBlocks.each(function(n) {
328 //Remove text-align attribute if it exists
329 n = EditorBidi.removeTextAlign(n);
331 dir = n.getAttribute(DIR);
332 if (!dir || dir === 'ltr') {
338 n.setAttribute(DIR, d);
340 returnValue = selectedBlocks;
342 ns._checkForChange();
346 }, '3.13.0', {"requires": ["editor-base"]});