MDL-41241 atto: YUIDoc cleanup for link plugin.
[moodle.git] / lib / editor / atto / plugins / link / yui / build / moodle-atto_link-button / moodle-atto_link-button-debug.js
blobb41a1b154ca0fc50da39e281d8411c74f0c38f6b
1 YUI.add('moodle-atto_link-button', function (Y, NAME) {
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * Atto text editor link plugin.
20  *
21  * @package    editor-atto
22  * @copyright  2013 Damyon Wiese  <damyon@moodle.com>
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
25 M.atto_link = M.atto_link || {
26     /**
27      * The window used to get the link details.
28      *
29      * @property dialogue
30      * @type M.core.dialogue
31      * @default null
32      */
33     dialogue : null,
35     /**
36      * The selection object returned by the browser.
37      *
38      * @property selection
39      * @type Range
40      * @default null
41      */
42     selection : null,
44     /**
45      * Display the chooser dialogue.
46      *
47      * @method init
48      * @param Event e
49      * @param string elementid
50      */
51     display_chooser : function(e, elementid) {
52         e.preventDefault();
53         if (!M.editor_atto.is_active(elementid)) {
54             M.editor_atto.focus(elementid);
55         }
56         M.atto_link.selection = M.editor_atto.get_selection();
57         if (M.atto_link.selection !== false && (!M.atto_link.selection.collapsed)) {
58             var dialogue;
59             if (!M.atto_link.dialogue) {
60                 dialogue = new M.core.dialogue({
61                     visible: false,
62                     modal: true,
63                     close: true,
64                     draggable: true
65                 });
66             } else {
67                 dialogue = M.atto_link.dialogue;
68             }
70             dialogue.render();
71             dialogue.set('bodyContent', M.atto_link.get_form_content(elementid));
72             dialogue.set('headerContent', M.util.get_string('createlink', 'atto_link'));
74             M.atto_link.resolve_anchors();
76             dialogue.show();
77             M.atto_link.dialogue = dialogue;
78         }
79     },
81     /**
82      * Add this button to the form.
83      *
84      * @method init
85      * @param {Object} params
86      */
87     init : function(params) {
88         M.editor_atto.add_toolbar_button(params.elementid, 'link', params.icon, this.display_chooser, this);
89     },
91     /**
92      * If there is selected text and it is part of an anchor link,
93      * extract the url (and target) from the link (and set them in the form).
94      *
95      * @method resolve_anchors
96      */
97     resolve_anchors : function() {
98         // Find the first anchor tag in the selection.
99         var selectednode = M.editor_atto.get_selection_parent_node(),
100             anchornode,
101             url;
103         // Note this is a document fragment and YUI doesn't like them.
104         if (!selectednode) {
105             return;
106         }
108         anchornode = Y.one(selectednode).ancestor('a');
110         if (anchornode) {
111             url = anchornode.getAttribute('href');
112             if (url !== '') {
113                 M.atto_link.selection = M.editor_atto.get_selection_from_node(anchornode);
114                 Y.one('#atto_link_urlentry').set('value', url);
115             }
116         }
117     },
119     /**
120      * Open the repository file picker.
121      *
122      * @method open_filepicker
123      * @param Event e
124      */
125     open_filepicker : function(e) {
126         var elementid = this.getAttribute('data-editor');
127         e.preventDefault();
129         M.editor_atto.show_filepicker(elementid, 'link', M.atto_link.filepicker_callback);
130     },
132     /**
133      * Called by the file picker when a link has been chosen.
134      *
135      * @method filepicker_callback
136      * @param {Object} params - contains selected url.
137      */
138     filepicker_callback : function(params) {
139         M.atto_link.dialogue.hide();
140         if (params.url !== '') {
141             M.editor_atto.set_selection(M.atto_link.selection);
142             document.execCommand('unlink', false, null);
143             document.execCommand('createLink', false, params.url);
144         }
145     },
147     /**
148      * The OK button has been pressed - make the changes to the source.
149      *
150      * @method set_link
151      * @param Event e
152      */
153     set_link : function(e) {
154         e.preventDefault();
155         M.atto_link.dialogue.hide();
157         var input = e.currentTarget.get('parentNode').one('input');
159         var value = input.get('value');
160         if (value !== '') {
161             M.editor_atto.set_selection(M.atto_link.selection);
162             document.execCommand('unlink', false, null);
163             document.execCommand('createLink', false, value);
164         }
165     },
167     /**
168      * Return the HTML of the form to show in the dialogue.
169      *
170      * @method get_form_content
171      * @param string elementid
172      * @return string
173      */
174     get_form_content : function(elementid) {
175         var content = Y.Node.create('<form>' +
176                              '<label for="atto_link_urlentry">' + M.util.get_string('enterurl', 'atto_link') +
177                              '</label><br/>' +
178                              '<input type="url" value="" id="atto_link_urlentry" size="32"/>' +
179                              '<br/>' +
180                              '<button id="openlinkbrowser" data-editor="' + Y.Escape.html(elementid) + '">' +
181                              M.util.get_string('browserepositories', 'atto_link') +
182                              '</button>' +
183                              '<hr/>' +
184                              '<button id="atto_link_urlentrysubmit">' +
185                              M.util.get_string('createlink', 'atto_link') +
186                              '</button>' +
187                              '</form>' +
188                              '<hr/>' + M.util.get_string('accessibilityhint', 'atto_link'));
190         content.one('#atto_link_urlentrysubmit').on('click', M.atto_link.set_link);
191         content.one('#openlinkbrowser').on('click', M.atto_link.open_filepicker);
192         return content;
193     }
197 }, '@VERSION@', {"requires": ["node", "escape"]});