Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('editor-br', function (Y, NAME) {
2
 
3
 
4
 
5
    /**
6
     * Plugin for Editor to normalize BR's.
7
     * @class Plugin.EditorBR
8
     * @extends Base
9
     * @constructor
10
     * @module editor
11
     * @submodule editor-br
12
     */
13
 
14
 
15
    var EditorBR = function() {
16
        EditorBR.superclass.constructor.apply(this, arguments);
17
    }, HOST = 'host', LI = 'li';
18
 
19
 
20
    Y.extend(EditorBR, Y.Base, {
21
        /**
22
        * Frame keyDown handler that normalizes BR's when pressing ENTER.
23
        * @private
24
        * @method _onKeyDown
25
        */
26
        _onKeyDown: function(e) {
27
            if (e.stopped) {
28
                e.halt();
29
                return;
30
            }
31
            if (e.keyCode === 13) {
32
                var host = this.get(HOST), inst = host.getInstance(),
33
                    sel = new inst.EditorSelection();
34
 
35
                if (sel) {
36
                    if (Y.UA.ie) {
37
                        if (!sel.anchorNode || (!sel.anchorNode.test(LI) && !sel.anchorNode.ancestor(LI))) {
38
                            host.execCommand('inserthtml', inst.EditorSelection.CURSOR);
39
                            e.halt();
40
                        }
41
                    }
42
                    if (Y.UA.webkit) {
43
                        if (!sel.anchorNode || (!sel.anchorNode.test(LI) && !sel.anchorNode.ancestor(LI))) {
44
                            host.frame._execCommand('insertlinebreak', null);
45
                            e.halt();
46
                        }
47
                    }
48
                }
49
            }
50
        },
51
        /**
52
        * Adds listeners for keydown in IE and Webkit. Also fires insertbeonreturn for supporting browsers.
53
        * @private
54
        * @method _afterEditorReady
55
        */
56
        _afterEditorReady: function() {
57
            var inst = this.get(HOST).getInstance(),
58
                container;
59
 
60
            try {
61
                inst.config.doc.execCommand('insertbronreturn', null, true);
62
            } catch (bre) {}
63
 
64
            if (Y.UA.ie || Y.UA.webkit) {
65
                container = inst.EditorSelection.ROOT;
66
 
67
                if (container.test('body')) {
68
                    container = inst.config.doc;
69
                }
70
 
71
                inst.on('keydown', Y.bind(this._onKeyDown, this), container);
72
            }
73
        },
74
        /**
75
        * Adds a nodeChange listener only for FF, in the event of a backspace or delete, it creates an empy textNode
76
        * inserts it into the DOM after the e.changedNode, then removes it. Causing FF to redraw the content.
77
        * @private
78
        * @method _onNodeChange
79
        * @param {Event} e The nodeChange event.
80
        */
81
        _onNodeChange: function(e) {
82
            switch (e.changedType) {
83
                case 'backspace-up':
84
                case 'backspace-down':
85
                case 'delete-up':
86
                    /*
87
                    * This forced FF to redraw the content on backspace.
88
                    * On some occasions FF will leave a cursor residue after content has been deleted.
89
                    * Dropping in the empty textnode and then removing it causes FF to redraw and
90
                    * remove the "ghost cursors"
91
                    */
92
                    var inst = this.get(HOST).getInstance(),
93
                        d = e.changedNode,
94
                        t = inst.config.doc.createTextNode(' ');
95
                    d.appendChild(t);
96
                    d.removeChild(t);
97
                    break;
98
            }
99
        },
100
        initializer: function() {
101
            var host = this.get(HOST);
102
            if (host.editorPara) {
103
                Y.error('Can not plug EditorBR and EditorPara at the same time.');
104
                return;
105
            }
106
            host.after('ready', Y.bind(this._afterEditorReady, this));
107
            if (Y.UA.gecko) {
108
                host.on('nodeChange', Y.bind(this._onNodeChange, this));
109
            }
110
        }
111
    }, {
112
        /**
113
        * editorBR
114
        * @static
115
        * @property NAME
116
        */
117
        NAME: 'editorBR',
118
        /**
119
        * editorBR
120
        * @static
121
        * @property NS
122
        */
123
        NS: 'editorBR',
124
        ATTRS: {
125
            host: {
126
                value: false
127
            }
128
        }
129
    });
130
 
131
    Y.namespace('Plugin');
132
 
133
    Y.Plugin.EditorBR = EditorBR;
134
 
135
 
136
 
137
}, '3.18.1', {"requires": ["editor-base"]});