Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
/**
17
 * unilabel helper for activity picker
18
 *
19
 * @author      Andreas Grabs <info@grabs-edv.de>
20
 * @copyright   2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
21
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import log from 'core/log';
25
 
26
/**
27
 * Switch the input to a visible activity link.
28
 *
29
 * @param {Element} currentinput The url input field which is bound to the picker
30
 * @param {Element} activitylinksrc The activity element from the picker list to be cloned to the url input
31
 * @param {String} url
32
 * @param {Boolean} makedirty Should the from be dirty after switching
33
 * @param {String} deletestr The delete string for the delete button.
34
 */
35
export const switchInput = (currentinput, activitylinksrc, url, makedirty, deletestr) => {
36
    /**
37
     * Go recursive through all child elements given from element and apply the callback function.
38
     *
39
     * @param {Element} element
40
     * @param {CallableFunction} callback
41
     */
42
    const childrenAll = (element, callback) => {
43
        for (const child of element.children) {
44
            childrenAll(child, callback);
45
        }
46
        callback(element);
47
    };
48
 
49
    /**
50
     * Make the form dirty so the changechecker is aware of.
51
     *
52
     * @param {Element} currentinput
53
     */
54
    const makeFormDirty = (currentinput) => {
55
        // To make the moodle form aware of the change, we set the data-initial-value to its original value.
56
        currentinput.closest('form').dataset.formDirty = true;
57
    };
58
 
59
    currentinput.value = url;
60
    currentinput.type = 'hidden';
61
    currentinput.dataset.initialValue = currentinput.value;
62
    let activitylink = activitylinksrc.closest('.activitytitle').cloneNode(true); // The new clone might have ids.
63
 
64
    log.debug('Remove all links from clone');
65
    childrenAll(activitylink, (e) => {
66
        e.removeAttribute('id'); // Remove all ids.
67
        if (e.nodeName.toLowerCase() == 'div' && e.classList.contains('unilabel-activity-picker-info')) {
68
            e.remove();
69
        }
70
        if (e.nodeName.toLowerCase() == 'a') {
71
            e.target = '_blank';
72
            e.classList.remove('stretched-link');
73
        }
74
    });
75
 
76
    activitylink.classList.add('unilabel-input-replacement', 'border-primary', 'rounded');
77
    let deletelinkcontainer = document.createElement('div');
78
    let deletelink = document.createElement('a');
79
    let deleteicon = document.createElement('i');
80
 
81
    deleteicon.classList.add('fa', 'fa-times', 'text-danger');
82
    deleteicon.dataset.inputid = currentinput.id; // Add data attribute because it often is the click target.
83
    deleteicon.title = deletestr;
84
 
85
    deletelink.insertAdjacentElement('afterbegin', deleteicon);
86
    deletelink.classList.add('unilabel-replacement-delete');
87
    deletelink.href = '#';
88
    deletelink.dataset.inputid = currentinput.id; // Add the data attribute to find the input field.
89
    deletelink.title = deletestr;
90
 
91
    deletelinkcontainer.insertAdjacentElement('afterbegin', deletelink);
92
    activitylink.insertAdjacentElement('beforeend', deletelinkcontainer);
93
    currentinput.insertAdjacentElement('afterend', activitylink);
94
 
95
    if (makedirty) {
96
        makeFormDirty(currentinput);
97
    }
98
 
99
    deletelink.addEventListener('click', (e) => {
100
        e.preventDefault();
101
        activitylink.remove();
102
        let currentinput = document.querySelector('#' + e.target.dataset.inputid);
103
        currentinput.value = '';
104
        currentinput.type = 'text';
105
        currentinput.focus();
106
 
107
        makeFormDirty(currentinput);
108
    });
109
};