1 YUI.add('moodle-atto_link-button', function (Y, NAME) {
3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * Atto text editor link plugin.
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
25 M.atto_link = M.atto_link || {
27 * The window used to get the link details.
30 * @type M.core.dialogue
36 * The selection object returned by the browser.
45 * Display the chooser dialogue.
49 * @param string elementid
51 display_chooser : function(e, elementid) {
53 if (!M.editor_atto.is_active(elementid)) {
54 M.editor_atto.focus(elementid);
56 M.atto_link.selection = M.editor_atto.get_selection();
57 if (M.atto_link.selection !== false && (!M.atto_link.selection.collapsed)) {
59 if (!M.atto_link.dialogue) {
60 dialogue = new M.core.dialogue({
67 dialogue = M.atto_link.dialogue;
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();
77 M.atto_link.dialogue = dialogue;
82 * Add this button to the form.
85 * @param {Object} params
87 init : function(params) {
88 M.editor_atto.add_toolbar_button(params.elementid, 'link', params.icon, this.display_chooser, this);
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).
95 * @method resolve_anchors
97 resolve_anchors : function() {
98 // Find the first anchor tag in the selection.
99 var selectednode = M.editor_atto.get_selection_parent_node(),
103 // Note this is a document fragment and YUI doesn't like them.
108 anchornode = Y.one(selectednode).ancestor('a');
111 url = anchornode.getAttribute('href');
113 M.atto_link.selection = M.editor_atto.get_selection_from_node(anchornode);
114 Y.one('#atto_link_urlentry').set('value', url);
120 * Open the repository file picker.
122 * @method open_filepicker
125 open_filepicker : function(e) {
126 var elementid = this.getAttribute('data-editor');
129 M.editor_atto.show_filepicker(elementid, 'link', M.atto_link.filepicker_callback);
133 * Called by the file picker when a link has been chosen.
135 * @method filepicker_callback
136 * @param {Object} params - contains selected url.
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);
148 * The OK button has been pressed - make the changes to the source.
153 set_link : function(e) {
155 M.atto_link.dialogue.hide();
157 var input = e.currentTarget.get('parentNode').one('input');
159 var value = input.get('value');
161 M.editor_atto.set_selection(M.atto_link.selection);
162 document.execCommand('unlink', false, null);
163 document.execCommand('createLink', false, value);
168 * Return the HTML of the form to show in the dialogue.
170 * @method get_form_content
171 * @param string elementid
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') +
178 '<input type="url" value="" id="atto_link_urlentry" size="32"/>' +
180 '<button id="openlinkbrowser" data-editor="' + Y.Escape.html(elementid) + '">' +
181 M.util.get_string('browserepositories', 'atto_link') +
184 '<button id="atto_link_urlentrysubmit">' +
185 M.util.get_string('createlink', 'atto_link') +
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);
197 }, '@VERSION@', {"requires": ["node", "escape"]});