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