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 || {
28 init : function(params) {
29 var display_chooser = function(e, elementid) {
31 if (!M.editor_atto.is_active(elementid)) {
32 M.editor_atto.focus(elementid);
34 M.atto_link.selection = M.editor_atto.get_selection();
35 if (M.atto_link.selection !== false && (!M.atto_link.selection.collapsed)) {
37 if (!M.atto_link.dialogue) {
38 dialogue = new M.core.dialogue({
45 dialogue = M.atto_link.dialogue;
49 dialogue.set('bodyContent', M.atto_link.get_form_content(elementid));
50 dialogue.set('headerContent', M.util.get_string('createlink', 'atto_link'));
52 M.atto_link.resolve_anchors();
55 M.atto_link.dialogue = dialogue;
59 M.editor_atto.add_toolbar_button(params.elementid, 'link', params.icon, display_chooser, this);
61 resolve_anchors : function() {
62 // Find the first anchor tag in the selection.
63 var selectednode = M.editor_atto.get_selection_parent_node(),
67 // Note this is a document fragment and YUI doesn't like them.
72 anchornode = Y.one(selectednode).ancestor('a');
75 url = anchornode.getAttribute('href');
77 M.atto_link.selection = M.editor_atto.get_selection_from_node(anchornode);
78 Y.one('#atto_link_urlentry').set('value', url);
82 open_filepicker : function(e) {
83 var elementid = this.getAttribute('data-editor');
86 M.editor_atto.show_filepicker(elementid, 'link', M.atto_link.filepicker_callback);
88 filepicker_callback : function(params) {
89 M.atto_link.dialogue.hide();
90 if (params.url !== '') {
91 M.editor_atto.set_selection(M.atto_link.selection);
92 document.execCommand('unlink', false, null);
93 document.execCommand('createLink', false, params.url);
96 set_link : function(e) {
98 M.atto_link.dialogue.hide();
100 var input = e.currentTarget.get('parentNode').one('input');
102 var value = input.get('value');
104 M.editor_atto.set_selection(M.atto_link.selection);
105 document.execCommand('unlink', false, null);
106 document.execCommand('createLink', false, value);
109 get_form_content : function(elementid) {
110 var content = Y.Node.create('<form>' +
111 '<label for="atto_link_urlentry">' + M.util.get_string('enterurl', 'atto_link') +
113 '<input type="url" value="" id="atto_link_urlentry" size="32"/>' +
115 '<button id="openlinkbrowser" data-editor="' + Y.Escape.html(elementid) + '">' +
116 M.util.get_string('browserepositories', 'atto_link') +
119 '<button id="atto_link_urlentrysubmit">' +
120 M.util.get_string('createlink', 'atto_link') +
123 '<hr/>' + M.util.get_string('accessibilityhint', 'atto_link'));
125 content.one('#atto_link_urlentrysubmit').on('click', M.atto_link.set_link);
126 content.one('#openlinkbrowser').on('click', M.atto_link.open_filepicker);
132 }, '@VERSION@', {"requires": ["node", "escape"]});