Merge branch 'MDL-32509' of git://github.com/danpoltawski/moodle
[moodle.git] / lib / yui / 3.5.0 / build / createlink-base / createlink-base-debug.js
blob51c43d6d13d1b539b5f2b6d6265bab46b634e6f2
1 /*
2 YUI 3.5.0 (build 5089)
3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
7 YUI.add('createlink-base', function(Y) {
10     /**
11      * Adds prompt style link creation. Adds an override for the <a href="Plugin.ExecCommand.html#method_COMMANDS.createlink">createlink execCommand</a>.
12      * @class Plugin.CreateLinkBase
13      * @static
14      * @submodule createlink-base
15      * @module editor
16      */
17     
18     var CreateLinkBase = {};
19     /**
20     * Strings used by the plugin
21     * @property STRINGS
22     * @static
23     */
24     CreateLinkBase.STRINGS = {
25             /**
26             * String used for the Prompt
27             * @property PROMPT
28             * @static
29             */
30             PROMPT: 'Please enter the URL for the link to point to:',
31             /**
32             * String used as the default value of the Prompt
33             * @property DEFAULT
34             * @static
35             */
36             DEFAULT: 'http://'
37     };
39     Y.namespace('Plugin');
40     Y.Plugin.CreateLinkBase = CreateLinkBase;
42     Y.mix(Y.Plugin.ExecCommand.COMMANDS, {
43         /**
44         * Override for the createlink method from the <a href="Plugin.CreateLinkBase.html">CreateLinkBase</a> plugin.
45         * @for ExecCommand
46         * @method COMMANDS.createlink
47         * @static
48         * @param {String} cmd The command executed: createlink
49         * @return {Node} Node instance of the item touched by this command.
50         */
51         createlink: function(cmd) {
52             var inst = this.get('host').getInstance(), out, a, sel, holder,
53                 url = prompt(CreateLinkBase.STRINGS.PROMPT, CreateLinkBase.STRINGS.DEFAULT);
55             if (url) {
56                 holder = inst.config.doc.createElement('div');
57                 url = url.replace(/"/g, '').replace(/'/g, ''); //Remove single & double quotes
58                 url = inst.config.doc.createTextNode(url);
59                 holder.appendChild(url);
60                 url = holder.innerHTML;
62                 Y.log('Adding link: ' + url, 'info', 'createLinkBase');
64                 this.get('host')._execCommand(cmd, url);
65                 sel = new inst.EditorSelection();
66                 out = sel.getSelected();
67                 if (!sel.isCollapsed && out.size()) {
68                     //We have a selection
69                     a = out.item(0).one('a');
70                     if (a) {
71                         out.item(0).replace(a);
72                     }
73                     if (Y.UA.gecko) {
74                         if (a.get('parentNode').test('span')) {
75                             if (a.get('parentNode').one('br.yui-cursor')) {
76                                 a.get('parentNode').insert(a, 'before');
77                             }
78                         }
79                     }
80                 } else {
81                     //No selection, insert a new node..
82                     this.get('host').execCommand('inserthtml', '<a href="' + url + '">' + url + '</a>');
83                 }
84             }
85             return a;
86         }
87     });
91 }, '3.5.0' ,{skinnable:false, requires:['editor-base']});