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 |
* A javascript module to handle summary modal.
|
|
|
18 |
*
|
|
|
19 |
* @module core_calendar/summary_modal
|
|
|
20 |
* @copyright 2017 Simey Lameze <simey@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
import $ from 'jquery';
|
|
|
25 |
import * as CustomEvents from 'core/custom_interaction_events';
|
|
|
26 |
import Modal from 'core/modal';
|
|
|
27 |
import CalendarEvents from './events';
|
|
|
28 |
import * as CalendarCrud from 'core_calendar/crud';
|
|
|
29 |
import * as ModalEvents from 'core/modal_events';
|
|
|
30 |
|
|
|
31 |
const SELECTORS = {
|
|
|
32 |
ROOT: "[data-region='summary-modal-container']",
|
|
|
33 |
EDIT_BUTTON: '[data-action="edit"]',
|
|
|
34 |
DELETE_BUTTON: '[data-action="delete"]',
|
|
|
35 |
};
|
|
|
36 |
|
|
|
37 |
export default class ModalEventSummary extends Modal {
|
|
|
38 |
static TEMPLATE = 'core_calendar/event_summary_modal';
|
|
|
39 |
static TYPE = 'core_calendar-event_summary';
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Get the edit button element from the footer. The button is cached
|
|
|
43 |
* as it's not expected to change.
|
|
|
44 |
*
|
|
|
45 |
* @method getEditButton
|
|
|
46 |
* @return {object} button element
|
|
|
47 |
*/
|
|
|
48 |
getEditButton() {
|
|
|
49 |
if (typeof this.editButton == 'undefined') {
|
|
|
50 |
this.editButton = this.getFooter().find(SELECTORS.EDIT_BUTTON);
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
return this.editButton;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Get the delete button element from the footer. The button is cached
|
|
|
58 |
* as it's not expected to change.
|
|
|
59 |
*
|
|
|
60 |
* @method getDeleteButton
|
|
|
61 |
* @return {object} button element
|
|
|
62 |
*/
|
|
|
63 |
getDeleteButton() {
|
|
|
64 |
if (typeof this.deleteButton == 'undefined') {
|
|
|
65 |
this.deleteButton = this.getFooter().find(SELECTORS.DELETE_BUTTON);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
return this.deleteButton;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Get the id for the event being shown in this modal. This value is
|
|
|
73 |
* not cached because it will change depending on which event is
|
|
|
74 |
* being displayed.
|
|
|
75 |
*
|
|
|
76 |
* @method getEventId
|
|
|
77 |
* @return {int}
|
|
|
78 |
*/
|
|
|
79 |
getEventId() {
|
|
|
80 |
return this.getBody().find(SELECTORS.ROOT).attr('data-event-id');
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Get the title for the event being shown in this modal. This value is
|
|
|
85 |
* not cached because it will change depending on which event is
|
|
|
86 |
* being displayed.
|
|
|
87 |
*
|
|
|
88 |
* @method getEventTitle
|
|
|
89 |
* @return {String}
|
|
|
90 |
*/
|
|
|
91 |
getEventTitle() {
|
|
|
92 |
return this.getBody().find(SELECTORS.ROOT).attr('data-event-title');
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Get the number of events in the series for the event being shown in
|
|
|
97 |
* this modal. This value is not cached because it will change
|
|
|
98 |
* depending on which event is being displayed.
|
|
|
99 |
*
|
|
|
100 |
* @method getEventCount
|
|
|
101 |
* @return {int}
|
|
|
102 |
*/
|
|
|
103 |
getEventCount() {
|
|
|
104 |
return this.getBody().find(SELECTORS.ROOT).attr('data-event-count');
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* Get the url for the event being shown in this modal.
|
|
|
109 |
*
|
|
|
110 |
* @method getEventUrl
|
|
|
111 |
* @return {String}
|
|
|
112 |
*/
|
|
|
113 |
getEditUrl() {
|
|
|
114 |
return this.getBody().find(SELECTORS.ROOT).attr('data-edit-url');
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Is this an action event.
|
|
|
119 |
*
|
|
|
120 |
* @method getEventUrl
|
|
|
121 |
* @return {String}
|
|
|
122 |
*/
|
|
|
123 |
isActionEvent() {
|
|
|
124 |
return (this.getBody().find(SELECTORS.ROOT).attr('data-action-event') == 'true');
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Set up all of the event handling for the modal.
|
|
|
129 |
*
|
|
|
130 |
* @method registerEventListeners
|
|
|
131 |
*/
|
|
|
132 |
registerEventListeners() {
|
|
|
133 |
// Apply parent event listeners.
|
|
|
134 |
super.registerEventListeners(this);
|
|
|
135 |
|
|
|
136 |
// We have to wait for the modal to finish rendering in order to ensure that
|
|
|
137 |
// the data-event-title property is available to use as the modal title.
|
|
|
138 |
M.util.js_pending('core_calendar/summary_modal:registerEventListeners:bodyRendered');
|
|
|
139 |
this.getRoot().on(ModalEvents.bodyRendered, function() {
|
|
|
140 |
this.getModal().data({
|
|
|
141 |
eventTitle: this.getEventTitle(),
|
|
|
142 |
eventId: this.getEventId(),
|
|
|
143 |
eventCount: this.getEventCount(),
|
|
|
144 |
})
|
|
|
145 |
.attr('data-type', 'event');
|
|
|
146 |
CalendarCrud.registerRemove(this.getModal());
|
|
|
147 |
M.util.js_complete('core_calendar/summary_modal:registerEventListeners:bodyRendered');
|
|
|
148 |
}.bind(this));
|
|
|
149 |
|
|
|
150 |
$('body').on(CalendarEvents.deleted, function() {
|
|
|
151 |
// Close the dialogue on delete.
|
|
|
152 |
this.hide();
|
|
|
153 |
}.bind(this));
|
|
|
154 |
|
|
|
155 |
CustomEvents.define(this.getEditButton(), [
|
|
|
156 |
CustomEvents.events.activate
|
|
|
157 |
]);
|
|
|
158 |
|
|
|
159 |
this.getEditButton().on(CustomEvents.events.activate, function(e, data) {
|
|
|
160 |
if (this.isActionEvent()) {
|
|
|
161 |
// Action events cannot be edited on the event form and must be redirected to the module UI.
|
|
|
162 |
$('body').trigger(CalendarEvents.editActionEvent, [this.getEditUrl()]);
|
|
|
163 |
} else {
|
|
|
164 |
// When the edit button is clicked we fire an event for the calendar UI to handle.
|
|
|
165 |
// We don't care how the UI chooses to handle it.
|
|
|
166 |
$('body').trigger(CalendarEvents.editEvent, [this.getEventId()]);
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
// There is nothing else for us to do so let's hide.
|
|
|
170 |
this.hide();
|
|
|
171 |
|
|
|
172 |
// We've handled this event so no need to propagate it.
|
|
|
173 |
e.preventDefault();
|
|
|
174 |
e.stopPropagation();
|
|
|
175 |
data.originalEvent.preventDefault();
|
|
|
176 |
data.originalEvent.stopPropagation();
|
|
|
177 |
}.bind(this));
|
|
|
178 |
}
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
ModalEventSummary.registerModalType();
|