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
 * Some UI stuff for participants page.
18
 * This is also used by the report/participants/index.php because it has the same functionality.
19
 *
20
 * @module     core_user/participants
21
 * @copyright  2017 Damyon Wiese
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
import * as DynamicTable from 'core_table/dynamic';
26
import * as Str from 'core/str';
27
import CheckboxToggleAll from 'core/checkbox-toggleall';
28
import CustomEvents from 'core/custom_interaction_events';
29
import DynamicTableSelectors from 'core_table/local/dynamic/selectors';
30
import ModalEvents from 'core/modal_events';
31
import Notification from 'core/notification';
32
import Pending from 'core/pending';
33
import jQuery from 'jquery';
34
import {showAddNote, showSendMessage} from 'core_user/local/participants/bulkactions';
35
import 'core/inplace_editable';
36
 
37
const Selectors = {
38
    bulkActionSelect: "#formactionid",
39
    bulkUserSelectedCheckBoxes: "input[data-togglegroup='participants-table'][data-toggle='slave']:checked",
40
    checkCountButton: "#checkall",
41
    showCountText: '[data-region="participant-count"]',
42
    showCountToggle: '[data-action="showcount"]',
43
    stateHelpIcon: '[data-region="state-help-icon"]',
44
    tableForm: uniqueId => `form[data-table-unique-id="${uniqueId}"]`,
45
};
46
 
47
export const init = ({
48
    uniqueid,
49
    noteStateNames = {},
50
}) => {
51
    const root = document.querySelector(Selectors.tableForm(uniqueid));
52
    const getTableFromUniqueId = uniqueId => root.querySelector(DynamicTableSelectors.main.fromRegionId(uniqueId));
53
 
54
    /**
55
     * Private method.
56
     *
57
     * @method registerEventListeners
58
     * @private
59
     */
60
    const registerEventListeners = () => {
61
        CustomEvents.define(Selectors.bulkActionSelect, [CustomEvents.events.accessibleChange]);
62
        jQuery(Selectors.bulkActionSelect).on(CustomEvents.events.accessibleChange, e => {
63
            const bulkActionSelect = e.target.closest('select');
64
            const action = bulkActionSelect.value;
65
            const tableRoot = getTableFromUniqueId(uniqueid);
66
            const checkboxes = tableRoot.querySelectorAll(Selectors.bulkUserSelectedCheckBoxes);
67
            const pendingPromise = new Pending('core_user/participants:bulkActionSelect');
68
 
69
            if (action.indexOf('#') !== -1) {
70
                e.preventDefault();
71
 
72
                const ids = [];
73
                checkboxes.forEach(checkbox => {
74
                    ids.push(checkbox.getAttribute('name').replace('user', ''));
75
                });
76
 
77
                let bulkAction;
78
                if (action === '#messageselect') {
79
                    bulkAction = showSendMessage(ids);
80
                } else if (action === '#addgroupnote') {
81
                    bulkAction = showAddNote(
82
                        root.dataset.courseId,
83
                        ids,
84
                        noteStateNames,
85
                        root.querySelector(Selectors.stateHelpIcon)
86
                    );
87
                }
88
 
89
                if (bulkAction) {
90
                    const pendingBulkAction = new Pending('core_user/participants:bulkActionSelected');
91
                    bulkAction
92
                    .then(modal => {
93
                        modal.getRoot().on(ModalEvents.hidden, () => {
94
                            // Focus on the action select when the dialog is closed.
95
                            bulkActionSelect.focus();
96
                        });
97
 
98
                        pendingBulkAction.resolve();
99
                        return modal;
100
                    })
101
                    .catch(Notification.exception);
102
                }
103
            } else if (action !== '' && checkboxes.length) {
104
                bulkActionSelect.form.submit();
105
            }
106
 
107
            resetBulkAction(bulkActionSelect);
108
            pendingPromise.resolve();
109
        });
110
 
111
        root.addEventListener('click', e => {
112
            // Handle clicking of the "Select all" actions.
113
            const checkCountButton = root.querySelector(Selectors.checkCountButton);
114
            const checkCountButtonClicked = checkCountButton && checkCountButton.contains(e.target);
115
 
116
            if (checkCountButtonClicked) {
117
                e.preventDefault();
118
 
119
                const tableRoot = getTableFromUniqueId(uniqueid);
120
 
121
                DynamicTable.setPageSize(tableRoot, checkCountButton.dataset.targetPageSize)
122
                .then(tableRoot => {
123
                    // Update the toggle state.
124
                    CheckboxToggleAll.setGroupState(root, 'participants-table', true);
125
 
126
                    return tableRoot;
127
                })
128
                .catch(Notification.exception);
129
            }
130
        });
131
 
132
        // When the content is refreshed, update the row counts in various places.
133
        root.addEventListener(DynamicTable.Events.tableContentRefreshed, e => {
134
            const checkCountButton = root.querySelector(Selectors.checkCountButton);
135
 
136
            const tableRoot = e.target;
137
 
138
            const defaultPageSize = parseInt(tableRoot.dataset.tableDefaultPerPage, 10);
139
            const currentPageSize = parseInt(tableRoot.dataset.tablePageSize, 10);
140
            const totalRowCount = parseInt(tableRoot.dataset.tableTotalRows, 10);
141
 
142
            CheckboxToggleAll.updateSlavesFromMasterState(root, 'participants-table');
143
 
144
            const pageCountStrings = [
145
                {
146
                    key: 'countparticipantsfound',
147
                    component: 'core_user',
148
                    param: totalRowCount,
149
                },
150
            ];
151
 
152
            if (totalRowCount <= defaultPageSize) {
153
                if (checkCountButton) {
154
                    checkCountButton.classList.add('hidden');
155
                }
156
            } else if (totalRowCount <= currentPageSize) {
157
                // The are fewer than the current page size.
158
                pageCountStrings.push({
159
                    key: 'selectalluserswithcount',
160
                    component: 'core',
161
                    param: defaultPageSize,
162
                });
163
 
164
                if (checkCountButton) {
165
                    // The 'Check all [x]' button is only visible when there are values to set.
166
                    checkCountButton.classList.add('hidden');
167
                }
168
            } else {
169
                pageCountStrings.push({
170
                    key: 'selectalluserswithcount',
171
                    component: 'core',
172
                    param: totalRowCount,
173
                });
174
 
175
                if (checkCountButton) {
176
                    checkCountButton.classList.remove('hidden');
177
                }
178
            }
179
 
180
            Str.get_strings(pageCountStrings)
181
            .then(([showingParticipantCountString, selectCountString]) => {
182
                const showingParticipantCount = root.querySelector(Selectors.showCountText);
183
                showingParticipantCount.innerHTML = showingParticipantCountString;
184
 
185
                if (selectCountString && checkCountButton) {
186
                    checkCountButton.value = selectCountString;
187
                }
188
 
189
                return;
190
            })
191
            .catch(Notification.exception);
192
        });
193
    };
194
 
195
    const resetBulkAction = bulkActionSelect => {
196
        bulkActionSelect.value = '';
197
    };
198
 
199
    registerEventListeners();
200
};