~jonas-drange/ubuntu-webcatalog/tos-redirect

« back to all changes in this revision

Viewing changes to src/webcatalog/static/yui/3.10.3/build/editor-base/editor-base-debug.js

  • Committer: Tarmac
  • Author(s): Stephen Stewart
  • Date: 2013-06-26 09:19:32 UTC
  • mfrom: (184.1.4 ubuntu-global-nav)
  • Revision ID: tarmac-20130626091932-8urtuli368k8p7ds
[r=beuno,jonas-drange] add ubuntu global nav to apps.ubuntu.com

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
YUI 3.10.3 (build 2fb5187)
 
3
Copyright 2013 Yahoo! Inc. All rights reserved.
 
4
Licensed under the BSD License.
 
5
http://yuilibrary.com/license/
 
6
*/
 
7
 
 
8
YUI.add('editor-base', function (Y, NAME) {
 
9
 
 
10
 
 
11
    /**
 
12
     * Base class for Editor. Handles the business logic of Editor, no GUI involved only utility methods and events.
 
13
     *
 
14
     *      var editor = new Y.EditorBase({
 
15
     *          content: 'Foo'
 
16
     *      });
 
17
     *      editor.render('#demo');
 
18
     *
 
19
     * @class EditorBase
 
20
     * @extends Base
 
21
     * @module editor
 
22
     * @main editor
 
23
     * @submodule editor-base
 
24
     * @constructor
 
25
     */
 
26
 
 
27
    var EditorBase = function() {
 
28
        EditorBase.superclass.constructor.apply(this, arguments);
 
29
    }, LAST_CHILD = ':last-child', BODY = 'body';
 
30
 
 
31
    Y.extend(EditorBase, Y.Base, {
 
32
        /**
 
33
        * Internal reference to the Y.Frame instance
 
34
        * @property frame
 
35
        */
 
36
        frame: null,
 
37
        initializer: function() {
 
38
            var frame = new Y.Frame({
 
39
                designMode: true,
 
40
                title: EditorBase.STRINGS.title,
 
41
                use: EditorBase.USE,
 
42
                dir: this.get('dir'),
 
43
                extracss: this.get('extracss'),
 
44
                linkedcss: this.get('linkedcss'),
 
45
                defaultblock: this.get('defaultblock'),
 
46
                host: this
 
47
            }).plug(Y.Plugin.ExecCommand);
 
48
 
 
49
 
 
50
            frame.after('ready', Y.bind(this._afterFrameReady, this));
 
51
            frame.addTarget(this);
 
52
 
 
53
            this.frame = frame;
 
54
 
 
55
            this.publish('nodeChange', {
 
56
                emitFacade: true,
 
57
                bubbles: true,
 
58
                defaultFn: this._defNodeChangeFn
 
59
            });
 
60
 
 
61
            //this.plug(Y.Plugin.EditorPara);
 
62
        },
 
63
        destructor: function() {
 
64
            this.frame.destroy();
 
65
 
 
66
            this.detachAll();
 
67
        },
 
68
        /**
 
69
        * Copy certain styles from one node instance to another (used for new paragraph creation mainly)
 
70
        * @method copyStyles
 
71
        * @param {Node} from The Node instance to copy the styles from
 
72
        * @param {Node} to The Node instance to copy the styles to
 
73
        */
 
74
        copyStyles: function(from, to) {
 
75
            if (from.test('a')) {
 
76
                //Don't carry the A styles
 
77
                return;
 
78
            }
 
79
            var styles = ['color', 'fontSize', 'fontFamily', 'backgroundColor', 'fontStyle' ],
 
80
                newStyles = {};
 
81
 
 
82
            Y.each(styles, function(v) {
 
83
                newStyles[v] = from.getStyle(v);
 
84
            });
 
85
            if (from.ancestor('b,strong')) {
 
86
                newStyles.fontWeight = 'bold';
 
87
            }
 
88
            if (from.ancestor('u')) {
 
89
                if (!newStyles.textDecoration) {
 
90
                    newStyles.textDecoration = 'underline';
 
91
                }
 
92
            }
 
93
            to.setStyles(newStyles);
 
94
        },
 
95
        /**
 
96
        * Holder for the selection bookmark in IE.
 
97
        * @property _lastBookmark
 
98
        * @private
 
99
        */
 
100
        _lastBookmark: null,
 
101
        /**
 
102
        * Resolves the e.changedNode in the nodeChange event if it comes from the document. If
 
103
        * the event came from the document, it will get the last child of the last child of the document
 
104
        * and return that instead.
 
105
        * @method _resolveChangedNode
 
106
        * @param {Node} n The node to resolve
 
107
        * @private
 
108
        */
 
109
        _resolveChangedNode: function(n) {
 
110
            var inst = this.getInstance(), lc, lc2, found, sel;
 
111
            if (n && n.test(BODY)) {
 
112
                sel = new inst.EditorSelection();
 
113
                if (sel && sel.anchorNode) {
 
114
                    n = sel.anchorNode;
 
115
                }
 
116
            }
 
117
            if (inst && n && n.test('html')) {
 
118
                lc = inst.one(BODY).one(LAST_CHILD);
 
119
                while (!found) {
 
120
                    if (lc) {
 
121
                        lc2 = lc.one(LAST_CHILD);
 
122
                        if (lc2) {
 
123
                            lc = lc2;
 
124
                        } else {
 
125
                            found = true;
 
126
                        }
 
127
                    } else {
 
128
                        found = true;
 
129
                    }
 
130
                }
 
131
                if (lc) {
 
132
                    if (lc.test('br')) {
 
133
                        if (lc.previous()) {
 
134
                            lc = lc.previous();
 
135
                        } else {
 
136
                            lc = lc.get('parentNode');
 
137
                        }
 
138
                    }
 
139
                    if (lc) {
 
140
                        n = lc;
 
141
                    }
 
142
                }
 
143
            }
 
144
            if (!n) {
 
145
                //Fallback to make sure a node is attached to the event
 
146
                n = inst.one(BODY);
 
147
            }
 
148
            return n;
 
149
        },
 
150
        /**
 
151
        * The default handler for the nodeChange event.
 
152
        * @method _defNodeChangeFn
 
153
        * @param {Event} e The event
 
154
        * @private
 
155
        */
 
156
        _defNodeChangeFn: function(e) {
 
157
            var startTime = (new Date()).getTime(),
 
158
                inst = this.getInstance(), sel,
 
159
                changed, endTime,
 
160
                cmds = {}, family, fsize, classes = [],
 
161
                fColor = '', bColor = '', bq,
 
162
                normal = false;
 
163
 
 
164
            if (Y.UA.ie) {
 
165
                try {
 
166
                    sel = inst.config.doc.selection.createRange();
 
167
                    if (sel.getBookmark) {
 
168
                        this._lastBookmark = sel.getBookmark();
 
169
                    }
 
170
                } catch (ie) {}
 
171
            }
 
172
 
 
173
            e.changedNode = this._resolveChangedNode(e.changedNode);
 
174
 
 
175
 
 
176
            /*
 
177
            * @TODO
 
178
            * This whole method needs to be fixed and made more dynamic.
 
179
            * Maybe static functions for the e.changeType and an object bag
 
180
            * to walk through and filter to pass off the event to before firing..
 
181
            */
 
182
 
 
183
            switch (e.changedType) {
 
184
                case 'tab':
 
185
                    if (!e.changedNode.test('li, li *') && !e.changedEvent.shiftKey) {
 
186
                        e.changedEvent.frameEvent.preventDefault();
 
187
                        Y.log('Overriding TAB key to insert HTML: HALTING', 'info', 'editor');
 
188
                        if (Y.UA.webkit) {
 
189
                            this.execCommand('inserttext', '\t');
 
190
                        } else if (Y.UA.gecko) {
 
191
                            this.frame.exec._command('inserthtml', EditorBase.TABKEY);
 
192
                        } else if (Y.UA.ie) {
 
193
                            this.execCommand('inserthtml', EditorBase.TABKEY);
 
194
                        }
 
195
                    }
 
196
                    break;
 
197
                case 'backspace-up':
 
198
                    // Fixes #2531090 - Joins text node strings so they become one for bidi
 
199
                    if (Y.UA.webkit && e.changedNode) {
 
200
                        e.changedNode.set('innerHTML', e.changedNode.get('innerHTML'));
 
201
                    }
 
202
                    break;
 
203
            }
 
204
            if (Y.UA.webkit && e.commands && (e.commands.indent || e.commands.outdent)) {
 
205
                /*
 
206
                * When executing execCommand 'indent or 'outdent' Webkit applies
 
207
                * a class to the BLOCKQUOTE that adds left/right margin to it
 
208
                * This strips that style so it is just a normal BLOCKQUOTE
 
209
                */
 
210
                bq = inst.all('.webkit-indent-blockquote, blockquote');
 
211
                if (bq.size()) {
 
212
                    bq.setStyle('margin', '');
 
213
                }
 
214
            }
 
215
 
 
216
            changed = this.getDomPath(e.changedNode, false);
 
217
 
 
218
            if (e.commands) {
 
219
                cmds = e.commands;
 
220
            }
 
221
 
 
222
 
 
223
            Y.each(changed, function(el) {
 
224
                var tag = el.tagName.toLowerCase(),
 
225
                    cmd = EditorBase.TAG2CMD[tag], s,
 
226
                    n, family2, cls, bColor2;
 
227
 
 
228
                if (cmd) {
 
229
                    cmds[cmd] = 1;
 
230
                }
 
231
 
 
232
                //Bold and Italic styles
 
233
                s = el.currentStyle || el.style;
 
234
 
 
235
                if ((''+s.fontWeight) === 'normal') {
 
236
                    normal = true;
 
237
                }
 
238
                if ((''+s.fontWeight) === 'bold') { //Cast this to a string
 
239
                    cmds.bold = 1;
 
240
                }
 
241
                if (Y.UA.ie) {
 
242
                    if (s.fontWeight > 400) {
 
243
                        cmds.bold = 1;
 
244
                    }
 
245
                }
 
246
                if (s.fontStyle === 'italic') {
 
247
                    cmds.italic = 1;
 
248
                }
 
249
 
 
250
                if (s.textDecoration.indexOf('underline') > -1) {
 
251
                    cmds.underline = 1;
 
252
                }
 
253
                if (s.textDecoration.indexOf('line-through') > -1) {
 
254
                    cmds.strikethrough = 1;
 
255
                }
 
256
 
 
257
                n = inst.one(el);
 
258
                if (n.getStyle('fontFamily')) {
 
259
                    family2 = n.getStyle('fontFamily').split(',')[0].toLowerCase();
 
260
                    if (family2) {
 
261
                        family = family2;
 
262
                    }
 
263
                    if (family) {
 
264
                        family = family.replace(/'/g, '').replace(/"/g, '');
 
265
                    }
 
266
                }
 
267
 
 
268
                fsize = EditorBase.NORMALIZE_FONTSIZE(n);
 
269
 
 
270
 
 
271
                cls = el.className.split(' ');
 
272
                Y.each(cls, function(v) {
 
273
                    if (v !== '' && (v.substr(0, 4) !== 'yui_')) {
 
274
                        classes.push(v);
 
275
                    }
 
276
                });
 
277
 
 
278
                fColor = EditorBase.FILTER_RGB(n.getStyle('color'));
 
279
                bColor2 = EditorBase.FILTER_RGB(s.backgroundColor);
 
280
                if (bColor2 !== 'transparent') {
 
281
                    if (bColor2 !== '') {
 
282
                        bColor = bColor2;
 
283
                    }
 
284
                }
 
285
 
 
286
            });
 
287
 
 
288
            if (normal) {
 
289
                delete cmds.bold;
 
290
                delete cmds.italic;
 
291
            }
 
292
 
 
293
            e.dompath = inst.all(changed);
 
294
            e.classNames = classes;
 
295
            e.commands = cmds;
 
296
 
 
297
            //TODO Dont' like this, not dynamic enough..
 
298
            if (!e.fontFamily) {
 
299
                e.fontFamily = family;
 
300
            }
 
301
            if (!e.fontSize) {
 
302
                e.fontSize = fsize;
 
303
            }
 
304
            if (!e.fontColor) {
 
305
                e.fontColor = fColor;
 
306
            }
 
307
            if (!e.backgroundColor) {
 
308
                e.backgroundColor = bColor;
 
309
            }
 
310
 
 
311
            endTime = (new Date()).getTime();
 
312
            Y.log('_defNodeChangeTimer 2: ' + (endTime - startTime) + 'ms', 'info', 'selection');
 
313
        },
 
314
        /**
 
315
        * Walk the dom tree from this node up to body, returning a reversed array of parents.
 
316
        * @method getDomPath
 
317
        * @param {Node} node The Node to start from
 
318
        */
 
319
        getDomPath: function(node, nodeList) {
 
320
            var domPath = [], domNode,
 
321
                inst = this.frame.getInstance();
 
322
 
 
323
            domNode = inst.Node.getDOMNode(node);
 
324
            //return inst.all(domNode);
 
325
 
 
326
            while (domNode !== null) {
 
327
 
 
328
                if ((domNode === inst.config.doc.documentElement) || (domNode === inst.config.doc) || !domNode.tagName) {
 
329
                    domNode = null;
 
330
                    break;
 
331
                }
 
332
 
 
333
                if (!inst.DOM.inDoc(domNode)) {
 
334
                    domNode = null;
 
335
                    break;
 
336
                }
 
337
 
 
338
                //Check to see if we get el.nodeName and nodeType
 
339
                if (domNode.nodeName && domNode.nodeType && (domNode.nodeType === 1)) {
 
340
                    domPath.push(domNode);
 
341
                }
 
342
 
 
343
                if (domNode === inst.config.doc.body) {
 
344
                    domNode = null;
 
345
                    break;
 
346
                }
 
347
 
 
348
                domNode = domNode.parentNode;
 
349
            }
 
350
 
 
351
            /*{{{ Using Node
 
352
            while (node !== null) {
 
353
                if (node.test('html') || node.test('doc') || !node.get('tagName')) {
 
354
                    node = null;
 
355
                    break;
 
356
                }
 
357
                if (!node.inDoc()) {
 
358
                    node = null;
 
359
                    break;
 
360
                }
 
361
                //Check to see if we get el.nodeName and nodeType
 
362
                if (node.get('nodeName') && node.get('nodeType') && (node.get('nodeType') == 1)) {
 
363
                    domPath.push(inst.Node.getDOMNode(node));
 
364
                }
 
365
 
 
366
                if (node.test('body')) {
 
367
                    node = null;
 
368
                    break;
 
369
                }
 
370
 
 
371
                node = node.get('parentNode');
 
372
            }
 
373
            }}}*/
 
374
 
 
375
            if (domPath.length === 0) {
 
376
                domPath[0] = inst.config.doc.body;
 
377
            }
 
378
 
 
379
            if (nodeList) {
 
380
                return inst.all(domPath.reverse());
 
381
            } else {
 
382
                return domPath.reverse();
 
383
            }
 
384
 
 
385
        },
 
386
        /**
 
387
        * After frame ready, bind mousedown & keyup listeners
 
388
        * @method _afterFrameReady
 
389
        * @private
 
390
        */
 
391
        _afterFrameReady: function() {
 
392
            var inst = this.frame.getInstance();
 
393
 
 
394
            this.frame.on('dom:mouseup', Y.bind(this._onFrameMouseUp, this));
 
395
            this.frame.on('dom:mousedown', Y.bind(this._onFrameMouseDown, this));
 
396
            this.frame.on('dom:keydown', Y.bind(this._onFrameKeyDown, this));
 
397
 
 
398
            if (Y.UA.ie) {
 
399
                this.frame.on('dom:activate', Y.bind(this._onFrameActivate, this));
 
400
                this.frame.on('dom:beforedeactivate', Y.bind(this._beforeFrameDeactivate, this));
 
401
            }
 
402
            this.frame.on('dom:keyup', Y.bind(this._onFrameKeyUp, this));
 
403
            this.frame.on('dom:keypress', Y.bind(this._onFrameKeyPress, this));
 
404
            this.frame.on('dom:paste', Y.bind(this._onPaste, this));
 
405
 
 
406
            inst.EditorSelection.filter();
 
407
            this.fire('ready');
 
408
        },
 
409
        /**
 
410
        * Caches the current cursor position in IE.
 
411
        * @method _beforeFrameDeactivate
 
412
        * @private
 
413
        */
 
414
        _beforeFrameDeactivate: function(e) {
 
415
            if (e.frameTarget.test('html')) { //Means it came from a scrollbar
 
416
                return;
 
417
            }
 
418
            var inst = this.getInstance(),
 
419
                sel = inst.config.doc.selection.createRange();
 
420
 
 
421
            if (sel.compareEndPoints && !sel.compareEndPoints('StartToEnd', sel)) {
 
422
                sel.pasteHTML('<var id="yui-ie-cursor">');
 
423
            }
 
424
        },
 
425
        /**
 
426
        * Moves the cached selection bookmark back so IE can place the cursor in the right place.
 
427
        * @method _onFrameActivate
 
428
        * @private
 
429
        */
 
430
        _onFrameActivate: function(e) {
 
431
            if (e.frameTarget.test('html')) { //Means it came from a scrollbar
 
432
                return;
 
433
            }
 
434
            var inst = this.getInstance(),
 
435
                sel = new inst.EditorSelection(),
 
436
                range = sel.createRange(),
 
437
                cur = inst.all('#yui-ie-cursor');
 
438
 
 
439
            if (cur.size()) {
 
440
                cur.each(function(n) {
 
441
                    n.set('id', '');
 
442
                    if (range.moveToElementText) {
 
443
                        try {
 
444
                            range.moveToElementText(n._node);
 
445
                            var moved = range.move('character', -1);
 
446
                            if (moved === -1) { //Only move up if we actually moved back.
 
447
                                range.move('character', 1);
 
448
                            }
 
449
                            range.select();
 
450
                            range.text = '';
 
451
                        } catch (e) {}
 
452
                    }
 
453
                    n.remove();
 
454
                });
 
455
            }
 
456
        },
 
457
        /**
 
458
        * Fires nodeChange event
 
459
        * @method _onPaste
 
460
        * @private
 
461
        */
 
462
        _onPaste: function(e) {
 
463
            this.fire('nodeChange', { changedNode: e.frameTarget, changedType: 'paste', changedEvent: e.frameEvent });
 
464
        },
 
465
        /**
 
466
        * Fires nodeChange event
 
467
        * @method _onFrameMouseUp
 
468
        * @private
 
469
        */
 
470
        _onFrameMouseUp: function(e) {
 
471
            this.fire('nodeChange', { changedNode: e.frameTarget, changedType: 'mouseup', changedEvent: e.frameEvent  });
 
472
        },
 
473
        /**
 
474
        * Fires nodeChange event
 
475
        * @method _onFrameMouseDown
 
476
        * @private
 
477
        */
 
478
        _onFrameMouseDown: function(e) {
 
479
            this.fire('nodeChange', { changedNode: e.frameTarget, changedType: 'mousedown', changedEvent: e.frameEvent  });
 
480
        },
 
481
        /**
 
482
        * Caches a copy of the selection for key events. Only creating the selection on keydown
 
483
        * @property _currentSelection
 
484
        * @private
 
485
        */
 
486
        _currentSelection: null,
 
487
        /**
 
488
        * Holds the timer for selection clearing
 
489
        * @property _currentSelectionTimer
 
490
        * @private
 
491
        */
 
492
        _currentSelectionTimer: null,
 
493
        /**
 
494
        * Flag to determine if we can clear the selection or not.
 
495
        * @property _currentSelectionClear
 
496
        * @private
 
497
        */
 
498
        _currentSelectionClear: null,
 
499
        /**
 
500
        * Fires nodeChange event
 
501
        * @method _onFrameKeyDown
 
502
        * @private
 
503
        */
 
504
        _onFrameKeyDown: function(e) {
 
505
            var inst, sel;
 
506
            if (!this._currentSelection) {
 
507
                if (this._currentSelectionTimer) {
 
508
                    this._currentSelectionTimer.cancel();
 
509
                }
 
510
                this._currentSelectionTimer = Y.later(850, this, function() {
 
511
                    this._currentSelectionClear = true;
 
512
                });
 
513
 
 
514
                inst = this.frame.getInstance();
 
515
                sel = new inst.EditorSelection(e);
 
516
 
 
517
                this._currentSelection = sel;
 
518
            } else {
 
519
                sel = this._currentSelection;
 
520
            }
 
521
 
 
522
            inst = this.frame.getInstance();
 
523
            sel = new inst.EditorSelection();
 
524
 
 
525
            this._currentSelection = sel;
 
526
 
 
527
            if (sel && sel.anchorNode) {
 
528
                this.fire('nodeChange', { changedNode: sel.anchorNode, changedType: 'keydown', changedEvent: e.frameEvent });
 
529
                if (EditorBase.NC_KEYS[e.keyCode]) {
 
530
                    this.fire('nodeChange', {
 
531
                        changedNode: sel.anchorNode,
 
532
                        changedType: EditorBase.NC_KEYS[e.keyCode],
 
533
                        changedEvent: e.frameEvent
 
534
                    });
 
535
                    this.fire('nodeChange', {
 
536
                        changedNode: sel.anchorNode,
 
537
                        changedType: EditorBase.NC_KEYS[e.keyCode] + '-down',
 
538
                        changedEvent: e.frameEvent
 
539
                    });
 
540
                }
 
541
            }
 
542
        },
 
543
        /**
 
544
        * Fires nodeChange event
 
545
        * @method _onFrameKeyPress
 
546
        * @private
 
547
        */
 
548
        _onFrameKeyPress: function(e) {
 
549
            var sel = this._currentSelection;
 
550
 
 
551
            if (sel && sel.anchorNode) {
 
552
                this.fire('nodeChange', { changedNode: sel.anchorNode, changedType: 'keypress', changedEvent: e.frameEvent });
 
553
                if (EditorBase.NC_KEYS[e.keyCode]) {
 
554
                    this.fire('nodeChange', {
 
555
                        changedNode: sel.anchorNode,
 
556
                        changedType: EditorBase.NC_KEYS[e.keyCode] + '-press',
 
557
                        changedEvent: e.frameEvent
 
558
                    });
 
559
                }
 
560
            }
 
561
        },
 
562
        /**
 
563
        * Fires nodeChange event for keyup on specific keys
 
564
        * @method _onFrameKeyUp
 
565
        * @private
 
566
        */
 
567
        _onFrameKeyUp: function(e) {
 
568
            var inst = this.frame.getInstance(),
 
569
                sel = new inst.EditorSelection(e);
 
570
 
 
571
            if (sel && sel.anchorNode) {
 
572
                this.fire('nodeChange', { changedNode: sel.anchorNode, changedType: 'keyup', selection: sel, changedEvent: e.frameEvent  });
 
573
                if (EditorBase.NC_KEYS[e.keyCode]) {
 
574
                    this.fire('nodeChange', {
 
575
                        changedNode: sel.anchorNode,
 
576
                        changedType: EditorBase.NC_KEYS[e.keyCode] + '-up',
 
577
                        selection: sel,
 
578
                        changedEvent: e.frameEvent
 
579
                    });
 
580
                }
 
581
            }
 
582
            if (this._currentSelectionClear) {
 
583
                this._currentSelectionClear = this._currentSelection = null;
 
584
            }
 
585
        },
 
586
        /**
 
587
        * Pass through to the frame.execCommand method
 
588
        * @method execCommand
 
589
        * @param {String} cmd The command to pass: inserthtml, insertimage, bold
 
590
        * @param {String} val The optional value of the command: Helvetica
 
591
        * @return {Node/NodeList} The Node or Nodelist affected by the command. Only returns on override commands, not browser defined commands.
 
592
        */
 
593
        execCommand: function(cmd, val) {
 
594
            var ret = this.frame.execCommand(cmd, val),
 
595
                inst = this.frame.getInstance(),
 
596
                sel = new inst.EditorSelection(), cmds = {},
 
597
                e = { changedNode: sel.anchorNode, changedType: 'execcommand', nodes: ret };
 
598
 
 
599
            switch (cmd) {
 
600
                case 'forecolor':
 
601
                    e.fontColor = val;
 
602
                    break;
 
603
                case 'backcolor':
 
604
                    e.backgroundColor = val;
 
605
                    break;
 
606
                case 'fontsize':
 
607
                    e.fontSize = val;
 
608
                    break;
 
609
                case 'fontname':
 
610
                    e.fontFamily = val;
 
611
                    break;
 
612
            }
 
613
 
 
614
            cmds[cmd] = 1;
 
615
            e.commands = cmds;
 
616
 
 
617
            this.fire('nodeChange', e);
 
618
 
 
619
            return ret;
 
620
        },
 
621
        /**
 
622
        * Get the YUI instance of the frame
 
623
        * @method getInstance
 
624
        * @return {YUI} The YUI instance bound to the frame.
 
625
        */
 
626
        getInstance: function() {
 
627
            return this.frame.getInstance();
 
628
        },
 
629
        /**
 
630
        * Renders the Y.Frame to the passed node.
 
631
        * @method render
 
632
        * @param {Selector/HTMLElement/Node} node The node to append the Editor to
 
633
        * @return {EditorBase}
 
634
        * @chainable
 
635
        */
 
636
        render: function(node) {
 
637
            this.frame.set('content', this.get('content'));
 
638
            this.frame.render(node);
 
639
            return this;
 
640
        },
 
641
        /**
 
642
        * Focus the contentWindow of the iframe
 
643
        * @method focus
 
644
        * @param {Function} fn Callback function to execute after focus happens
 
645
        * @return {EditorBase}
 
646
        * @chainable
 
647
        */
 
648
        focus: function(fn) {
 
649
            this.frame.focus(fn);
 
650
            return this;
 
651
        },
 
652
        /**
 
653
        * Handles the showing of the Editor instance. Currently only handles the iframe
 
654
        * @method show
 
655
        * @return {EditorBase}
 
656
        * @chainable
 
657
        */
 
658
        show: function() {
 
659
            this.frame.show();
 
660
            return this;
 
661
        },
 
662
        /**
 
663
        * Handles the hiding of the Editor instance. Currently only handles the iframe
 
664
        * @method hide
 
665
        * @return {EditorBase}
 
666
        * @chainable
 
667
        */
 
668
        hide: function() {
 
669
            this.frame.hide();
 
670
            return this;
 
671
        },
 
672
        /**
 
673
        * (Un)Filters the content of the Editor, cleaning YUI related code. //TODO better filtering
 
674
        * @method getContent
 
675
        * @return {String} The filtered content of the Editor
 
676
        */
 
677
        getContent: function() {
 
678
            var html = '', inst = this.getInstance();
 
679
            if (inst && inst.EditorSelection) {
 
680
                html = inst.EditorSelection.unfilter();
 
681
            }
 
682
            //Removing the _yuid from the objects in IE
 
683
            html = html.replace(/ _yuid="([^>]*)"/g, '');
 
684
            return html;
 
685
        }
 
686
    }, {
 
687
        /**
 
688
        * @static
 
689
        * @method NORMALIZE_FONTSIZE
 
690
        * @description Pulls the fontSize from a node, then checks for string values (x-large, x-small)
 
691
        * and converts them to pixel sizes. If the parsed size is different from the original, it calls
 
692
        * node.setStyle to update the node with a pixel size for normalization.
 
693
        */
 
694
        NORMALIZE_FONTSIZE: function(n) {
 
695
            var size = n.getStyle('fontSize'), oSize = size;
 
696
 
 
697
            switch (size) {
 
698
                case '-webkit-xxx-large':
 
699
                    size = '48px';
 
700
                    break;
 
701
                case 'xx-large':
 
702
                    size = '32px';
 
703
                    break;
 
704
                case 'x-large':
 
705
                    size = '24px';
 
706
                    break;
 
707
                case 'large':
 
708
                    size = '18px';
 
709
                    break;
 
710
                case 'medium':
 
711
                    size = '16px';
 
712
                    break;
 
713
                case 'small':
 
714
                    size = '13px';
 
715
                    break;
 
716
                case 'x-small':
 
717
                    size = '10px';
 
718
                    break;
 
719
            }
 
720
            if (oSize !== size) {
 
721
                n.setStyle('fontSize', size);
 
722
            }
 
723
            return size;
 
724
        },
 
725
        /**
 
726
        * @static
 
727
        * @property TABKEY
 
728
        * @description The HTML markup to use for the tabkey
 
729
        */
 
730
        TABKEY: '<span class="tab">&nbsp;&nbsp;&nbsp;&nbsp;</span>',
 
731
        /**
 
732
        * @static
 
733
        * @method FILTER_RGB
 
734
        * @param String css The CSS string containing rgb(#,#,#);
 
735
        * @description Converts an RGB color string to a hex color, example: rgb(0, 255, 0) converts to #00ff00
 
736
        * @return String
 
737
        */
 
738
        FILTER_RGB: function(css) {
 
739
            if (css.toLowerCase().indexOf('rgb') !== -1) {
 
740
                var exp = new RegExp("(.*?)rgb\\s*?\\(\\s*?([0-9]+).*?,\\s*?([0-9]+).*?,\\s*?([0-9]+).*?\\)(.*?)", "gi"),
 
741
                    rgb = css.replace(exp, "$1,$2,$3,$4,$5").split(','),
 
742
                    r, g, b;
 
743
 
 
744
                if (rgb.length === 5) {
 
745
                    r = parseInt(rgb[1], 10).toString(16);
 
746
                    g = parseInt(rgb[2], 10).toString(16);
 
747
                    b = parseInt(rgb[3], 10).toString(16);
 
748
 
 
749
                    r = r.length === 1 ? '0' + r : r;
 
750
                    g = g.length === 1 ? '0' + g : g;
 
751
                    b = b.length === 1 ? '0' + b : b;
 
752
 
 
753
                    css = "#" + r + g + b;
 
754
                }
 
755
            }
 
756
            return css;
 
757
        },
 
758
        /**
 
759
        * @static
 
760
        * @property TAG2CMD
 
761
        * @description A hash table of tags to their execcomand's
 
762
        */
 
763
        TAG2CMD: {
 
764
            'b': 'bold',
 
765
            'strong': 'bold',
 
766
            'i': 'italic',
 
767
            'em': 'italic',
 
768
            'u': 'underline',
 
769
            'sup': 'superscript',
 
770
            'sub': 'subscript',
 
771
            'img': 'insertimage',
 
772
            'a' : 'createlink',
 
773
            'ul' : 'insertunorderedlist',
 
774
            'ol' : 'insertorderedlist'
 
775
        },
 
776
        /**
 
777
        * Hash table of keys to fire a nodeChange event for.
 
778
        * @static
 
779
        * @property NC_KEYS
 
780
        * @type Object
 
781
        */
 
782
        NC_KEYS: {
 
783
            8: 'backspace',
 
784
            9: 'tab',
 
785
            13: 'enter',
 
786
            32: 'space',
 
787
            33: 'pageup',
 
788
            34: 'pagedown',
 
789
            35: 'end',
 
790
            36: 'home',
 
791
            37: 'left',
 
792
            38: 'up',
 
793
            39: 'right',
 
794
            40: 'down',
 
795
            46: 'delete'
 
796
        },
 
797
        /**
 
798
        * The default modules to use inside the Frame
 
799
        * @static
 
800
        * @property USE
 
801
        * @type Array
 
802
        */
 
803
        USE: ['node', 'selector-css3', 'editor-selection', 'stylesheet'],
 
804
        /**
 
805
        * The Class Name: editorBase
 
806
        * @static
 
807
        * @property NAME
 
808
        */
 
809
        NAME: 'editorBase',
 
810
        /**
 
811
        * Editor Strings.  By default contains only the `title` property for the
 
812
        * Title of frame document (default "Rich Text Editor").
 
813
        *
 
814
        * @static
 
815
        * @property STRINGS
 
816
        */
 
817
        STRINGS: {
 
818
            title: 'Rich Text Editor'
 
819
        },
 
820
        ATTRS: {
 
821
            /**
 
822
            * The content to load into the Editor Frame
 
823
            * @attribute content
 
824
            */
 
825
            content: {
 
826
                value: '<br class="yui-cursor">',
 
827
                setter: function(str) {
 
828
                    if (str.substr(0, 1) === "\n") {
 
829
                        Y.log('Stripping first carriage return from content before injecting', 'warn', 'editor');
 
830
                        str = str.substr(1);
 
831
                    }
 
832
                    if (str === '') {
 
833
                        str = '<br class="yui-cursor">';
 
834
                    }
 
835
                    if (str === ' ') {
 
836
                        if (Y.UA.gecko) {
 
837
                            str = '<br class="yui-cursor">';
 
838
                        }
 
839
                    }
 
840
                    return this.frame.set('content', str);
 
841
                },
 
842
                getter: function() {
 
843
                    return this.frame.get('content');
 
844
                }
 
845
            },
 
846
            /**
 
847
            * The value of the dir attribute on the HTML element of the frame. Default: ltr
 
848
            * @attribute dir
 
849
            */
 
850
            dir: {
 
851
                writeOnce: true,
 
852
                value: 'ltr'
 
853
            },
 
854
            /**
 
855
            * @attribute linkedcss
 
856
            * @description An array of url's to external linked style sheets
 
857
            * @type String
 
858
            */
 
859
            linkedcss: {
 
860
                value: '',
 
861
                setter: function(css) {
 
862
                    if (this.frame) {
 
863
                        this.frame.set('linkedcss', css);
 
864
                    }
 
865
                    return css;
 
866
                }
 
867
            },
 
868
            /**
 
869
            * @attribute extracss
 
870
            * @description A string of CSS to add to the Head of the Editor
 
871
            * @type String
 
872
            */
 
873
            extracss: {
 
874
                value: false,
 
875
                setter: function(css) {
 
876
                    if (this.frame) {
 
877
                        this.frame.set('extracss', css);
 
878
                    }
 
879
                    return css;
 
880
                }
 
881
            },
 
882
            /**
 
883
            * @attribute defaultblock
 
884
            * @description The default tag to use for block level items, defaults to: p
 
885
            * @type String
 
886
            */
 
887
            defaultblock: {
 
888
                value: 'p'
 
889
            }
 
890
        }
 
891
    });
 
892
 
 
893
    Y.EditorBase = EditorBase;
 
894
 
 
895
    /**
 
896
    * @event nodeChange
 
897
    * @description Fired from several mouse/key/paste event points.
 
898
    * @param {Event.Facade} event An Event Facade object with the following specific properties added:
 
899
    * <dl>
 
900
    *   <dt>changedEvent</dt><dd>The event that caused the nodeChange</dd>
 
901
    *   <dt>changedNode</dt><dd>The node that was interacted with</dd>
 
902
    *   <dt>changedType</dt><dd>The type of change: mousedown, mouseup, right, left, backspace, tab, enter, etc..</dd>
 
903
    *   <dt>commands</dt><dd>The list of execCommands that belong to this change and the dompath that's associated with the changedNode</dd>
 
904
    *   <dt>classNames</dt><dd>An array of classNames that are applied to the changedNode and all of it's parents</dd>
 
905
    *   <dt>dompath</dt><dd>A sorted array of node instances that make up the DOM path from the changedNode to body.</dd>
 
906
    *   <dt>backgroundColor</dt><dd>The cascaded backgroundColor of the changedNode</dd>
 
907
    *   <dt>fontColor</dt><dd>The cascaded fontColor of the changedNode</dd>
 
908
    *   <dt>fontFamily</dt><dd>The cascaded fontFamily of the changedNode</dd>
 
909
    *   <dt>fontSize</dt><dd>The cascaded fontSize of the changedNode</dd>
 
910
    * </dl>
 
911
    * @type {Event.Custom}
 
912
    */
 
913
 
 
914
    /**
 
915
    * @event ready
 
916
    * @description Fired after the frame is ready.
 
917
    * @param {Event.Facade} event An Event Facade object.
 
918
    * @type {Event.Custom}
 
919
    */
 
920
 
 
921
 
 
922
 
 
923
 
 
924
 
 
925
}, '3.10.3', {"requires": ["base", "frame", "node", "exec-command", "editor-selection"]});