weekly release 2.6dev
[moodle.git] / lib / editor / atto / plugins / link / yui / build / moodle-atto_link-button / moodle-atto_link-button-debug.js
blobc352f07b7fed0b724aeec70dbbec93d6adcb86bb
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     dialogue : null,
27     selection : null,
28     init : function(params) {
29         var display_chooser = function(e, elementid) {
30             e.preventDefault();
31             if (!M.editor_atto.is_active(elementid)) {
32                 M.editor_atto.focus(elementid);
33             }
34             M.atto_link.selection = M.editor_atto.get_selection();
35             if (M.atto_link.selection !== false && (!M.atto_link.selection.collapsed)) {
36                 var dialogue;
37                 if (!M.atto_link.dialogue) {
38                     dialogue = new M.core.dialogue({
39                         visible: false,
40                         modal: true,
41                         close: true,
42                         draggable: true
43                     });
44                 } else {
45                     dialogue = M.atto_link.dialogue;
46                 }
48                 dialogue.render();
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();
54                 dialogue.show();
55                 M.atto_link.dialogue = dialogue;
56             }
57         };
59         M.editor_atto.add_toolbar_button(params.elementid, 'link', params.icon, display_chooser, this);
60     },
61     resolve_anchors : function() {
62         // Find the first anchor tag in the selection.
63         var selectednode = M.editor_atto.get_selection_parent_node(),
64             anchornode,
65             url;
67         // Note this is a document fragment and YUI doesn't like them.
68         if (!selectednode) {
69             return;
70         }
72         anchornode = Y.one(selectednode).ancestor('a');
74         if (anchornode) {
75             url = anchornode.getAttribute('href');
76             if (url !== '') {
77                 M.atto_link.selection = M.editor_atto.get_selection_from_node(anchornode);
78                 Y.one('#atto_link_urlentry').set('value', url);
79             }
80         }
81     },
82     open_filepicker : function(e) {
83         var elementid = this.getAttribute('data-editor');
84         e.preventDefault();
86         M.editor_atto.show_filepicker(elementid, 'link', M.atto_link.filepicker_callback);
87     },
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);
94         }
95     },
96     set_link : function(e) {
97         e.preventDefault();
98         M.atto_link.dialogue.hide();
100         var input = e.currentTarget.get('parentNode').one('input');
102         var value = input.get('value');
103         if (value !== '') {
104             M.editor_atto.set_selection(M.atto_link.selection);
105             document.execCommand('unlink', false, null);
106             document.execCommand('createLink', false, value);
107         }
108     },
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') +
112                              '</label><br/>' +
113                              '<input type="url" value="" id="atto_link_urlentry" size="32"/>' +
114                              '<br/>' +
115                              '<button id="openlinkbrowser" data-editor="' + Y.Escape.html(elementid) + '">' +
116                              M.util.get_string('browserepositories', 'atto_link') +
117                              '</button>' +
118                              '<hr/>' +
119                              '<button id="atto_link_urlentrysubmit">' +
120                              M.util.get_string('createlink', 'atto_link') +
121                              '</button>' +
122                              '</form>' +
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);
127         return content;
128     }
132 }, '@VERSION@', {"requires": ["node", "escape"]});