Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('editor-bidi', function (Y, NAME) {
2
 
3
 
4
    /**
5
     * Plugin for Editor to support BiDirectional (bidi) text operations.
6
     * @class Plugin.EditorBidi
7
     * @extends Base
8
     * @constructor
9
     * @module editor
10
     * @submodule editor-bidi
11
     */
12
 
13
 
14
    var EditorBidi = function() {
15
        EditorBidi.superclass.constructor.apply(this, arguments);
16
    }, HOST = 'host', DIR = 'dir', NODE_CHANGE = 'nodeChange',
17
    B_C_CHANGE = 'bidiContextChange', STYLE = 'style';
18
 
19
    Y.extend(EditorBidi, Y.Base, {
20
        /**
21
        * Place holder for the last direction when checking for a switch
22
        * @private
23
        * @property lastDirection
24
        */
25
        lastDirection: null,
26
        /**
27
        * Tells us that an initial bidi check has already been performed
28
        * @private
29
        * @property firstEvent
30
        */
31
        firstEvent: null,
32
 
33
        /**
34
        * Method checks to see if the direction of the text has changed based on a nodeChange event.
35
        * @private
36
        * @method _checkForChange
37
        */
38
        _checkForChange: function() {
39
            var host = this.get(HOST),
40
                inst = host.getInstance(),
41
                sel = new inst.EditorSelection(),
42
                node, direction;
43
 
44
            if (sel.isCollapsed) {
45
                node = EditorBidi.blockParent(sel.focusNode, false, inst.EditorSelection.ROOT);
46
                if (node) {
47
                    direction = node.getStyle('direction');
48
                    if (direction !== this.lastDirection) {
49
                        host.fire(B_C_CHANGE, { changedTo: direction });
50
                        this.lastDirection = direction;
51
                    }
52
                }
53
            } else {
54
                host.fire(B_C_CHANGE, { changedTo: 'select' });
55
                this.lastDirection = null;
56
            }
57
        },
58
 
59
        /**
60
        * Checked for a change after a specific nodeChange event has been fired.
61
        * @private
62
        * @method _afterNodeChange
63
        */
64
        _afterNodeChange: function(e) {
65
            // If this is the first event ever, or an event that can result in a context change
66
            if (this.firstEvent || EditorBidi.EVENTS[e.changedType]) {
67
                this._checkForChange();
68
                this.firstEvent = false;
69
            }
70
        },
71
 
72
        /**
73
        * Checks for a direction change after a mouseup occurs.
74
        * @private
75
        * @method _afterMouseUp
76
        */
77
        _afterMouseUp: function() {
78
            this._checkForChange();
79
            this.firstEvent = false;
80
        },
81
        initializer: function() {
82
            var host = this.get(HOST);
83
 
84
            this.firstEvent = true;
85
 
86
            host.after(NODE_CHANGE, Y.bind(this._afterNodeChange, this));
87
            host.after('dom:mouseup', Y.bind(this._afterMouseUp, this));
88
        }
89
    }, {
90
        /**
91
        * The events to check for a direction change on
92
        * @property EVENTS
93
        * @static
94
        */
95
        EVENTS: {
96
            'backspace-up': true,
97
            'pageup-up': true,
98
            'pagedown-down': true,
99
            'end-up': true,
100
            'home-up': true,
101
            'left-up': true,
102
            'up-up': true,
103
            'right-up': true,
104
            'down-up': true,
105
            'delete-up': true
106
        },
107
 
108
        /**
109
        * More elements may be needed. BODY *must* be in the list to take care of the special case.
110
        *
111
        * blockParent could be changed to use inst.EditorSelection.BLOCKS
112
        * instead, but that would make Y.Plugin.EditorBidi.blockParent
113
        * unusable in non-RTE contexts (it being usable is a nice
114
        * side-effect).
115
        * @property BLOCKS
116
        * @static
117
        */
118
        //BLOCKS: Y.EditorSelection.BLOCKS+',LI,HR,' + BODY,
119
        BLOCKS: Y.EditorSelection.BLOCKS,
120
        /**
121
        * Template for creating a block element
122
        * @static
123
        * @property DIV_WRAPPER
124
        */
125
        DIV_WRAPPER: '<DIV></DIV>',
126
        /**
127
        * Returns a block parent for a given element
128
        * @static
129
        * @method blockParent
130
        */
131
        blockParent: function(node, wrap, root) {
132
            var parent = node, divNode, firstChild;
133
 
134
            root = root || Y.EditorSelection.ROOT;
135
 
136
            if (!parent) {
137
                parent = root;
138
            }
139
 
140
            if (!parent.test(EditorBidi.BLOCKS)) {
141
                parent = parent.ancestor(EditorBidi.BLOCKS);
142
            }
143
            if (wrap && parent.compareTo(root)) {
144
                // This shouldn't happen if the RTE handles everything
145
                // according to spec: we should get to a P before ROOT. But
146
                // we don't want to set the direction of ROOT even if that
147
                // happens, so we wrap everything in a DIV.
148
 
149
                // The code is based on YUI3's Y.EditorSelection._wrapBlock function.
150
                divNode = Y.Node.create(EditorBidi.DIV_WRAPPER);
151
                parent.get('children').each(function(node, index) {
152
                    if (index === 0) {
153
                        firstChild = node;
154
                    } else {
155
                        divNode.append(node);
156
                    }
157
                });
158
                firstChild.replace(divNode);
159
                divNode.prepend(firstChild);
160
                parent = divNode;
161
            }
162
            return parent;
163
        },
164
        /**
165
        * The data key to store on the node.
166
        * @static
167
        * @property _NODE_SELECTED
168
        */
169
        _NODE_SELECTED: 'bidiSelected',
170
        /**
171
        * Generates a list of all the block parents of the current NodeList
172
        * @static
173
        * @method addParents
174
        */
175
        addParents: function(nodeArray, root) {
176
            var i, parent, addParent;
177
                tester = function(sibling) {
178
                    if (!sibling.getData(EditorBidi._NODE_SELECTED)) {
179
                        addParent = false;
180
                        return true; // stop more processing
181
                    }
182
                };
183
 
184
            root = root || Y.EditorSelection.ROOT;
185
 
186
            for (i = 0; i < nodeArray.length; i += 1) {
187
                nodeArray[i].setData(EditorBidi._NODE_SELECTED, true);
188
            }
189
 
190
            // This works automagically, since new parents added get processed
191
            // later themselves. So if there's a node early in the process that
192
            // we haven't discovered some of its siblings yet, thus resulting in
193
            // its parent not added, the parent will be added later, since those
194
            // siblings will be added to the array and then get processed.
195
            for (i = 0; i < nodeArray.length; i += 1) {
196
                parent = nodeArray[i].get('parentNode');
197
 
198
                // Don't add the parent if the parent is the ROOT element.
199
                // We don't want to change the direction of ROOT. Also don't
200
                // do it if the parent is already in the list.
201
                if (!root.compareTo(parent) && !parent.getData(EditorBidi._NODE_SELECTED)) {
202
                    addParent = true;
203
                    parent.get('children').some(tester);
204
                    if (addParent) {
205
                        nodeArray.push(parent);
206
                        parent.setData(EditorBidi._NODE_SELECTED, true);
207
                    }
208
                }
209
            }
210
 
211
            for (i = 0; i < nodeArray.length; i += 1) {
212
                nodeArray[i].clearData(EditorBidi._NODE_SELECTED);
213
            }
214
 
215
            return nodeArray;
216
        },
217
 
218
 
219
        /**
220
        * editorBidi
221
        * @static
222
        * @property NAME
223
        */
224
        NAME: 'editorBidi',
225
        /**
226
        * editorBidi
227
        * @static
228
        * @property NS
229
        */
230
        NS: 'editorBidi',
231
        ATTRS: {
232
            host: {
233
                value: false
234
            }
235
        },
236
        /**
237
        * Regex for testing/removing text-align style from an element
238
        * @static
239
        * @property RE_TEXT_ALIGN
240
        */
241
        RE_TEXT_ALIGN: /text-align:\s*\w*\s*;/,
242
        /**
243
        * Method to test a node's style attribute for text-align and removing it.
244
        * @static
245
        * @method removeTextAlign
246
        */
247
        removeTextAlign: function(n) {
248
            if (n) {
249
                if (n.getAttribute(STYLE).match(EditorBidi.RE_TEXT_ALIGN)) {
250
                    n.setAttribute(STYLE, n.getAttribute(STYLE).replace(EditorBidi.RE_TEXT_ALIGN, ''));
251
                }
252
                if (n.hasAttribute('align')) {
253
                    n.removeAttribute('align');
254
                }
255
            }
256
            return n;
257
        }
258
    });
259
 
260
    Y.namespace('Plugin');
261
 
262
    Y.Plugin.EditorBidi = EditorBidi;
263
 
264
    /**
265
     * bidi execCommand override for setting the text direction of a node.
266
     * This property is added to the `Y.Plugin.ExecCommands.COMMANDS`
267
     * collection.
268
     *
269
     * @for Plugin.ExecCommand
270
     * @property bidi
271
     */
272
    //TODO -- This should not add this command unless the plugin is added to the instance..
273
    Y.Plugin.ExecCommand.COMMANDS.bidi = function(cmd, direction) {
274
        var inst = this.getInstance(),
275
            sel = new inst.EditorSelection(),
276
            ns = this.get(HOST).get(HOST).editorBidi,
277
            returnValue, block, b,
278
            root = inst.EditorSelection.ROOT,
279
            selected, selectedBlocks, dir;
280
 
281
        if (!ns) {
282
            Y.error('bidi execCommand is not available without the EditorBiDi plugin.');
283
            return;
284
        }
285
 
286
        inst.EditorSelection.filterBlocks();
287
 
288
        if (sel.isCollapsed) { // No selection
289
            block = EditorBidi.blockParent(sel.anchorNode, false, root);
290
            if (!block) {
291
                block = root.one(inst.EditorSelection.BLOCKS);
292
            }
293
            //Remove text-align attribute if it exists
294
            block = EditorBidi.removeTextAlign(block);
295
            if (!direction) {
296
                //If no direction is set, auto-detect the proper setting to make it "toggle"
297
                dir = block.getAttribute(DIR);
298
                if (!dir || dir === 'ltr') {
299
                    direction = 'rtl';
300
                } else {
301
                    direction = 'ltr';
302
                }
303
            }
304
            block.setAttribute(DIR, direction);
305
            if (Y.UA.ie) {
306
                b = block.all('br.yui-cursor');
307
                if (b.size() === 1 && block.get('childNodes').size() === 1) {
308
                    b.remove();
309
                }
310
            }
311
            returnValue = block;
312
        } else { // some text is selected
313
            selected = sel.getSelected();
314
            selectedBlocks = [];
315
            selected.each(function(node) {
316
                selectedBlocks.push(EditorBidi.blockParent(node, false, root));
317
            });
318
            selectedBlocks = inst.all(EditorBidi.addParents(selectedBlocks, root));
319
            selectedBlocks.each(function(n) {
320
                var d = direction;
321
                //Remove text-align attribute if it exists
322
                n = EditorBidi.removeTextAlign(n);
323
                if (!d) {
324
                    dir = n.getAttribute(DIR);
325
                    if (!dir || dir === 'ltr') {
326
                        d = 'rtl';
327
                    } else {
328
                        d = 'ltr';
329
                    }
330
                }
331
                n.setAttribute(DIR, d);
332
            });
333
            returnValue = selectedBlocks;
334
        }
335
        ns._checkForChange();
336
        return returnValue;
337
    };
338
 
339
 
340
}, '3.18.1', {"requires": ["editor-base"]});