-
Notifications
You must be signed in to change notification settings - Fork 29
/
module.js
228 lines (216 loc) · 8.89 KB
/
module.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Javascript helper function for OUBlog.
*
* @package mod
* @subpackage oublog
* @copyright 2013 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
M.mod_oublog = M.mod_oublog || {};
M.mod_oublog.init = function(Y) {
M.mod_oublog.hidewarning(Y);
var comments = Y.one('#id_allowcomments');
if (comments) {
comments.on('change', function(e){M.mod_oublog.hidewarning(Y);});
}
};
M.mod_oublog.hidewarning = function(Y) {
var field = Y.one('#publicwarningmarker');
if (field) {
field = field.get('parentNode').get('parentNode');
var select = Y.one('#id_allowcomments');
field.setStyle('display', select.get('value') == 2 ? 'block' : 'none');
}
};
/*Discovery 'block'*/
M.mod_oublog.init_showhide = function(Y, name, curpref) {
var block = Y.one('.oublog_statsview_content_' + name);
if (block) {
var showhide = block.one('.block_action_oublog');
var form = block.one('form.mform');
if (showhide && form) {
var hideinfo = block.one('.oublog_stats_minus');
var showinfo = block.one('.oublog_stats_plus');
if (curpref == 1) {
form.addClass('oublog_displaynone');
hideinfo.addClass('oublog_displaynone');
showinfo.removeClass('oublog_displaynone');
}
showhide.on(['click', 'keypress'], function(e) {
if (!e.keyCode || (e.keyCode && e.keyCode == 13)) {
e.preventDefault();
if (curpref == 1) {
hideinfo.removeClass('oublog_displaynone');
showinfo.addClass('oublog_displaynone');
form.removeClass('oublog_displaynone');
if (!Y.one('body').hasClass('notloggedin')) {
require(['core_user/repository'], (userRepository) => {
userRepository.setUserPreference('mod_oublog_hidestatsform_' + name, 0);
});
}
curpref = 0;
} else {
hideinfo.addClass('oublog_displaynone');
showinfo.removeClass('oublog_displaynone');
form.addClass('oublog_displaynone');
if (!Y.one('body').hasClass('notloggedin')) {
require(['core_user/repository'], (userRepository) => {
userRepository.setUserPreference('mod_oublog_hidestatsform_' + name, 1);
});
}
curpref = 1;
}
}
});
}
}
};
M.mod_oublog.init_deleteandemail = function(Y, cmid, postid) {
this.Y = Y;
this.YAHOO = Y.YUI2;
// Trap for individual 'Delete' links.
var delbtns = Y.one('a.oublog_deleteandemail_' + postid);
delbtns.on('click', function(e) {
var uri = e.target.get('href');
// Show the dialogue.
delbtns.set('disabled', false);
var content = M.util.get_string('deleteemailpostdescription', 'oublog');
var panel = new M.core.dialogue({
headerContent: M.util.get_string('confirm', 'moodle'),
bodyContent: content,
width: 400,
centered: true,
render: true,
zIndex: 50,
buttons: {},
plugins: [Y.Plugin.Drag],
modal: true});
// Add the two Delete and Cancel buttons to the bottom of the dialog.
panel.addButton({
label: M.util.get_string('delete', 'oublog'),
section: Y.WidgetStdMod.FOOTER,
action : function (e) {
e.preventDefault();
// Add on the 'confirm' delete marker to the link uri.
uri += '&confirm=1';
document.location.href = uri;
panel.hide();
panel.destroy();
}
});
panel.addButton({
label: M.util.get_string('deleteandemail', 'oublog'),
section: Y.WidgetStdMod.FOOTER,
action : function (e) {
e.preventDefault();
// Add on the 'email' marker to the link uri.
uri += '&email=1';
document.location.href = uri;
panel.hide();
panel.destroy();
}
});
panel.addButton({
value : 'Cancel',
section: Y.WidgetStdMod.FOOTER,
action : function (e) {
e.preventDefault();
panel.hide();
panel.destroy();
Y.one('a.oublog_deleteandemail_' + postid).focus();
}
});
e.preventDefault();
Y.one('a.oublog_deleteandemail_' + postid).focus();
panel.show();
});
};
/* Import table */
M.mod_oublog.init_posttable = function(Y) {
var includehead = Y.one('.flexible .header.c3');
var postchecks = Y.all('.flexible td.c3 input[type=checkbox]');
if (includehead && postchecks) {
// Add select all/none links to column header.
includehead.append('<a href="#" class="oublog_import_all">' + M.util.get_string('import_step1_all', 'oublog') +
'</a> / <a href="#" class="oublog_import_none">' + M.util.get_string('import_step1_none', 'oublog') + '</a>');
var all = Y.one('.flexible .c3 .oublog_import_all');
if (all) {
all.on('click', function(e) {
postchecks.set('checked', true);
postchecks.each(function(check){updatepreselect(check);});
e.preventDefault();
return false;
});
}
var none = Y.one('.flexible .c3 .oublog_import_none');
if (none) {
none.on('click', function(e) {
postchecks.set('checked', false);
postchecks.each(function(check){updatepreselect(check);});
e.preventDefault();
return false;});
}
}
var updatepreselect = function(check) {
var preselect = preselectinput.get('value');
var id = check.get('name').substr(5);
if (check.get('checked')) {
// Add id to preselect value.
if (id) {
var prearray = preselect.split(',');
for (var i = 0, len = prearray.length; i < len; i++) {
if (prearray[i] == id) {
// Already have, return.
return;
}
}
prearray.push(id);
preselectinput.set('value', prearray.join());
updatelinks(prearray);
}
} else {
// De-selecting, remove from preselect.
if (preselect && id) {
var prearray = preselect.split(',');
for (var i = 0, len = prearray.length; i < len; i++) {
if (prearray[i] == id) {
prearray.splice(i, 1);
preselectinput.set('value', prearray.join());
updatelinks(prearray);
return;
}
}
}
}
};
var updatelinks = function(prearray) {
// Update link query strings.
Y.all('.oublog_import_step1 form .paging a, .flexible a').each(function(link) {
var linkurl = link.get('href');
var params = Y.QueryString.parse(linkurl.substr(linkurl.indexOf('&')));
params.preselected = prearray.join();
var newurl = Y.QueryString.stringify(params);
link.set('href', linkurl.substr(0, linkurl.indexOf('&')) + '&' + newurl);
});
};
var preselectinput = Y.one('form input[name=preselected]');
if (postchecks && preselectinput) {
postchecks.on('click', function(check) {
updatepreselect(check.target);
});
}
};