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