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 $ from 'jquery';
25
import log from 'core/log';
26
 
27
export const init = async(formid) => {
28
    let currentinput;
29
    let maybeactivityelement;
30
    let modalid = 'unilabel-modal-activity-picker-' + formid;
31
    const str = await import('core/str');
32
    const deletestr = await str.get_string('delete');
33
    const inputswitcher = await import('mod_unilabel/activity_picker_input_switcher');
34
 
35
    $('#' + modalid).on('show.bs.modal', function() {
36
        $('#' + modalid).appendTo('body');
37
        currentinput = document.querySelector('#' + document.querySelector('#' + modalid).dataset.inputid);
38
        maybeactivityelement = currentinput.parentElement.querySelector('div.activitytitle.unilabel-input-replacement');
39
    });
40
 
41
    document.querySelector('#unilabel-activity-picker').addEventListener('click', (e) => {
42
        if (!e.target.classList.contains('activity-picker-link')) {
43
            return;
44
        }
45
        e.preventDefault();
46
        e.stopPropagation();
47
 
48
        if (maybeactivityelement) {
49
            log.debug('There already is an replacement element. It must be remove before a new one is added.');
50
            maybeactivityelement.remove();
51
        }
52
 
53
        $('#unilabel-modal-activity-picker-' + formid).modal('hide');
54
        if (e.target.classList.contains('activity-picker-link')) {
55
            let url = e.target.href;
56
            let activitylinksrc = e.target.closest('.activitytitle');
57
            inputswitcher.switchInput(currentinput, activitylinksrc, url, true, deletestr);
58
        }
59
    });
60
 
61
    $("#search-" + formid).on("keyup", function() {
62
        let value = $(this).val().toLowerCase();
63
        $("#unilabel-activity-picker-list li").filter((index, element) => {
64
            // Looking for data-filterstring we can apply the search term.
65
            if (element.dataset.filterstring.toLowerCase().indexOf(value) > -1) {
66
                $(element).slideDown();
67
            } else {
68
                $(element).slideUp();
69
            }
70
            return index;
71
        });
72
    });
73
};