Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 16... Línea 16...
16
/**
16
/**
17
 * Edit items in feedback module
17
 * Edit items in feedback module
18
 *
18
 *
19
 * @module     mod_feedback/edit
19
 * @module     mod_feedback/edit
20
 * @copyright  2016 Marina Glancy
20
 * @copyright  2016 Marina Glancy
-
 
21
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21
 */
22
 */
22
define(['jquery', 'core/ajax', 'core/str', 'core/notification'],
-
 
23
function($, ajax, str, notification) {
-
 
24
    var manager = {
-
 
25
        deleteItem: function(e) {
-
 
26
            e.preventDefault();
-
 
27
            var targetUrl = $(e.currentTarget).attr('href');
-
 
28
 
-
 
29
            str.get_strings([
-
 
30
                {
-
 
31
                    key:        'confirmation',
-
 
32
                    component:  'admin'
-
 
33
                },
-
 
34
                {
-
 
35
                    key:        'confirmdeleteitem',
-
 
36
                    component:  'mod_feedback'
-
 
37
                },
-
 
38
                {
-
 
39
                    key:        'yes',
-
 
40
                    component:  'moodle'
-
 
41
                },
-
 
42
                {
-
 
43
                    key:        'no',
-
 
44
                    component:  'moodle'
-
 
45
                }
-
 
46
            ])
-
 
47
            .then(function(s) {
-
 
48
                notification.confirm(s[0], s[1], s[2], s[3], function() {
-
 
49
                    window.location = targetUrl;
-
 
50
                });
-
 
51
 
-
 
52
                return;
-
 
53
            })
-
 
54
            .catch();
-
 
55
        },
-
 
Línea -... Línea 23...
-
 
23
 
-
 
24
"use strict";
-
 
25
 
-
 
26
import {addIconToContainerRemoveOnCompletion} from 'core/loadingicon';
-
 
27
import Notification from 'core/notification';
-
 
28
import Pending from 'core/pending';
-
 
29
import {prefetchStrings} from 'core/prefetch';
-
 
30
import SortableList from 'core/sortable_list';
-
 
31
import {getString, getStrings} from 'core/str';
-
 
32
import {add as addToast} from 'core/toast';
-
 
33
import {reorderQuestions} from 'mod_feedback/local/repository';
-
 
34
import Templates from 'core/templates';
-
 
35
 
-
 
36
const Selectors = {
-
 
37
    deleteQuestionButton: '[data-action="delete"]',
-
 
38
    sortableListRegion: '[data-region="questions-sortable-list"]',
-
 
39
    sortableElement: '[data-region="questions-sortable-list"] .feedback_itemlist[id]',
-
 
40
    sortableElementTitle: '[data-region="item-title"] span',
-
 
41
    questionLabel: '[data-region="questions-sortable-list"] .col-form-label',
-
 
42
    actionsMenuData: '[data-item-actions-menu]',
-
 
43
};
-
 
44
 
-
 
45
/**
-
 
46
 * Returns the Feedback question item id from the DOM id of an item.
-
 
47
 *
-
 
48
 * @param {String} id The dom id, f.g.: feedback_item_22
-
 
49
 * @return int
-
 
50
 */
-
 
51
const getItemId = (id) => {
-
 
52
    return Number(id.replace(/^.*feedback_item_/i, ''));
-
 
53
};
-
 
54
 
-
 
55
/**
-
 
56
 * Returns the order of the items in the sortable list.
-
 
57
 *
-
 
58
 * @param {Element} element The element to get the order from.
-
 
59
 * @return string
-
 
60
 */
-
 
61
const getItemOrder = (element) => {
-
 
62
    const sortableList = element.closest(Selectors.sortableListRegion);
-
 
63
    let itemOrder = [];
-
 
64
    sortableList.querySelectorAll(Selectors.sortableElement).forEach((item) => {
-
 
65
        itemOrder.push(getItemId(item.id));
-
 
66
    });
-
 
67
    return itemOrder.toString();
-
 
68
};
-
 
69
 
-
 
70
let initialized = false;
-
 
71
let moduleId = null;
-
 
72
 
-
 
73
/**
-
 
74
 * Initialise editor and all it's modules
-
 
75
 *
-
 
76
 * @param {Integer} cmId
-
 
77
 */
-
 
78
export const init = async(cmId) => {
-
 
79
 
-
 
80
    moduleId = cmId;
-
 
81
 
-
 
82
    // Ensure we only add our listeners once (can be called multiple times).
-
 
83
    if (initialized) {
-
 
84
        return;
-
 
85
    }
-
 
86
 
-
 
87
    prefetchStrings('core', [
-
 
88
        'yes',
-
 
89
        'no',
-
 
90
    ]);
-
 
91
    prefetchStrings('admin', [
-
 
92
        'confirmation',
-
 
93
    ]);
-
 
94
    prefetchStrings('mod_feedback', [
-
 
95
        'confirmdeleteitem',
-
 
96
        'questionmoved',
-
 
97
        'move_item',
-
 
98
    ]);
-
 
99
 
-
 
100
    await enhanceEditForm();
-
 
101
 
-
 
102
    // Add event listeners.
-
 
103
    document.addEventListener('click', async event => {
-
 
104
 
-
 
105
        // Delete question.
56
 
106
        const deleteButton = event.target.closest(Selectors.deleteQuestionButton);
-
 
107
        if (deleteButton) {
-
 
108
            event.preventDefault();
-
 
109
            const confirmationStrings = await getStrings([
-
 
110
                {key: 'confirmation', component: 'admin'},
-
 
111
                {key: 'confirmdeleteitem', component: 'mod_feedback'},
-
 
112
                {key: 'yes', component: 'core'},
-
 
113
                {key: 'no', component: 'core'},
-
 
114
            ]);
57
        setup: function() {
115
            Notification.confirm(...confirmationStrings, () => {
-
 
116
                window.location = deleteButton.getAttribute('href');
-
 
117
            });
-
 
118
            return;
-
 
119
        }
-
 
120
    });
-
 
121
 
-
 
122
    // Initialize sortable list to handle active conditions moving.
-
 
123
    const sortableList = new SortableList(document.querySelector(Selectors.sortableListRegion));
-
 
124
    sortableList.getElementName = element => Promise.resolve(element[0].querySelector(Selectors.sortableElementTitle)?.textContent);
-
 
125
 
-
 
126
    document.addEventListener(SortableList.EVENTS.elementDrop, event => {
-
 
127
        if (!event.detail.positionChanged) {
58
            $('body').delegate('[data-action="delete"]', 'click', manager.deleteItem);
128
            return;
-
 
129
        }
-
 
130
        const pendingPromise = new Pending('mod_feedback/questions:reorder');
-
 
131
        const itemOrder = getItemOrder(event.detail.element[0]);
-
 
132
        addIconToContainerRemoveOnCompletion(event.detail.element[0], pendingPromise);
-
 
133
        reorderQuestions(moduleId, itemOrder)
-
 
134
            .then(() => getString('questionmoved', 'mod_feedback'))
-
 
135
            .then(addToast)
-
 
136
            .then(() => pendingPromise.resolve())
59
        }
137
            .catch(Notification.exception);
Línea -... Línea 138...
-
 
138
    });
-
 
139
 
-
 
140
    initialized = true;
-
 
141
};
-
 
142
 
-
 
143
/**
-
 
144
 * Enhance the edit form by adding a move item button and an action menu to each question.
-
 
145
 *
-
 
146
 * @returns {Promise<void>}
-
 
147
 */
-
 
148
const enhanceEditForm = async() => {
-
 
149
    const questionLabels = document.querySelectorAll(Selectors.questionLabel);
-
 
150
    const movetitle = await getString('move_item', 'mod_feedback');
-
 
151
 
-
 
152
    const updates = Array.from(questionLabels).map(async(container) => {
-
 
153
        const label = container.querySelector(Selectors.actionsMenuData);
-
 
154
        if (!label) {
-
 
155
            return;
60
    };
156
        }
-
 
157
 
61
 
158
        try {
-
 
159
            const contextData = {
-
 
160
                movetitle,
-
 
161
                label: label.parentElement.outerHTML,
-
 
162
                actionsmenu: JSON.parse(label.dataset.itemActionsMenu || '{}'),
-
 
163
            };
-
 
164
            container.innerHTML = await Templates.render('mod_feedback/item_edit_enhanced_title', contextData);
-
 
165
        } catch (error) {
62
    return {
166
            await Notification.exception(error);
-
 
167
        }
63
        setup: manager.setup
168
    });