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
 * Controls the edit switch.
18
 *
19
 * @module     core/edit_switch
20
 * @copyright  2021 Bas Brands <bas@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import {call as fetchMany} from 'core/ajax';
25
import {dispatchEvent} from 'core/event_dispatcher';
26
import {exception as displayException} from 'core/notification';
27
 
28
/**
29
 * Change the Edit mode.
30
 *
31
 * @param {number} context The contextid that editing is being set for
32
 * @param {bool} setmode Whether editing is set or not
33
 * @return {Promise} Resolved with an array file the stored file url.
34
 */
35
const setEditMode = (context, setmode) => fetchMany([{
36
    methodname: 'core_change_editmode',
37
    args: {
38
        context,
39
        setmode,
40
    },
41
}])[0];
42
 
43
/**
44
 * Toggle the edit switch
45
 *
46
 * @method
47
 * @protected
48
 * @param {HTMLElement} editSwitch
49
 */
50
const toggleEditSwitch = editSwitch => {
51
    if (editSwitch.checked) {
52
        editSwitch.setAttribute('aria-checked', true);
53
    } else {
54
        editSwitch.setAttribute('aria-checked', false);
55
    }
56
 
57
    const event = notifyEditModeSet(editSwitch, editSwitch.checked);
58
    if (!event.defaultPrevented) {
59
        editSwitch.setAttribute('disabled', true);
60
        window.location = editSwitch.dataset.pageurl;
61
    }
62
};
63
 
64
/**
65
 * Names of events for core/edit_switch.
66
 *
67
 * @static
68
 * @property {String} editModeSet See {@link event:core/edit_switch/editModeSet}
69
 */
70
export const eventTypes = {
71
    /**
72
     * An event triggered when the edit mode toggled.
73
     *
74
     * @event core/edit_switch/editModeSet
75
     * @type {CustomEvent}
76
     * @property {HTMLElement} target The switch used to toggle the edit mode
77
     * @property {object} detail
78
     * @property {bool} detail.editMode
79
     */
80
    editModeSet: 'core/edit_switch/editModeSet',
81
};
82
 
83
/**
84
 * Dispatch the editModeSet event after changing the edit mode.
85
 *
86
 * This event is cancelable.
87
 *
88
 * The default action is to reload the page after toggling the edit mode.
89
 *
90
 * @method
91
 * @protected
92
 * @param {HTMLElement} container
93
 * @param {bool} editMode
94
 * @returns {CustomEvent}
95
 */
96
const notifyEditModeSet = (container, editMode) => dispatchEvent(
97
    eventTypes.editModeSet,
98
    {editMode},
99
    container,
100
    {cancelable: true}
101
);
102
 
103
/**
104
 * Add the eventlistener for the editswitch.
105
 *
106
 * @param {string} editingSwitchId The id of the editing switch to listen for
107
 */
108
export const init = editingSwitchId => {
109
    const editSwitch = document.getElementById(editingSwitchId);
110
    editSwitch.addEventListener('change', () => {
111
        setEditMode(editSwitch.dataset.context, editSwitch.checked)
112
        .then(result => {
113
            if (result.success) {
114
                toggleEditSwitch(editSwitch);
115
            } else {
116
                editSwitch.checked = false;
117
            }
118
            return;
119
        })
120
        .catch(displayException);
121
    });
122
};