-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmodule.js
147 lines (122 loc) · 4.52 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
// 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/>.
/**
* Handle submitting question and voting action of hotquestion
* using Ajax of YUI.
*
* @package mod_hotquestion
* @copyright 2011 Sun Zhigang
* @copyright 2016 onwards AL Rachels [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
M.modHotquestion = {};
M.modHotquestion.Y = {};
M.modHotquestion.questionbox = {};
M.modHotquestion.submitbutton = {};
M.modHotquestion.init = function(Y) {
M.modHotquestion.Y = Y;
// Init question box.
M.modHotquestion.questionbox = Y.one('#id_question');
M.modHotquestion.questionbox.on('valueChange', M.modHotquestion.questionchanged);
// Init submit button.
M.modHotquestion.submitbutton = Y.one('#id_submitbutton');
if (M.modHotquestion.getquestion() == '') {
M.modHotquestion.submitbutton.set('disabled', 'disabled');
}
Y.on("submit", M.modHotquestion.submit, '#mform1');
// Bind toolbar buttons.
Y.on('click', M.modHotquestion.refresh, '.hotquestion_vote');
Y.on('click', M.modHotquestion.refresh, '.toolbutton');
// Bind io events.
Y.on('io:success', M.modHotquestion.iocomplete);
Y.on('io:failure', M.modHotquestion.iofailure);
};
M.modHotquestion.iocomplete = function(transactionid, response, arguments) {
var Y = M.modHotquestion.Y;
// Update questions.
var contentdiv = Y.one('#questions_list');
contentdiv.set("innerHTML", response.responseText);
// Clean up form if this is a submit IO.
if (arguments.caller == 'submit') {
M.modHotquestion.questionbox.set('value', '');
M.modHotquestion.questionbox.removeAttribute('disabled');
M.modHotquestion.submitbutton.set('disabled', 'disabled');
}
// Rebind buttons.
Y.on('click', M.modHotquestion.refresh, '.hotquestion_vote');
Y.on('click', M.modHotquestion.refresh, '.toolbutton');
};
M.modHotquestion.iofailure = function(transactionid, response, arguments) {
M.modHotquestion.submitbutton.removeAttribute('disabled');
M.modHotquestion.questionbox.removeAttribute('disabled');
alert(M.str.hotquestion.connectionerror);
};
M.modHotquestion.refresh = function(e) {
e.preventDefault();
var data = e.currentTarget.get('href').split('?', 2)[1];
data += '&ajax=1';
var cfg = {
method: "GET",
data: data,
arguments: {
caller: 'refresh',
}
};
var request = M.modHotquestion.Y.io('view.php', cfg);
};
M.modHotquestion.getquestion = function() {
var question = M.modHotquestion.questionbox.get('value');
return YAHOO.lang.trim(question);
};
M.modHotquestion.questionchanged = function(e) {
var question = M.modHotquestion.getquestion();
var submitbutton = M.modHotquestion.submitbutton;
if (question == '') {
submitbutton.set('disabled', 'disabled');
} else {
submitbutton.removeAttribute('disabled');
}
};
M.modHotquestion.submit = function(e) {
e.preventDefault();
var question = M.modHotquestion.getquestion();
if (question == '') {
return; // Ignore empty question.
}
// To avoid multiple clicks and editing.
M.modHotquestion.submitbutton.set('disabled', 'disabled');
M.modHotquestion.questionbox.set('disabled', 'disabled');
// Get all input components.
var inputs = M.modHotquestion.Y.all('#mform1 input');
// Construct post data.
var data = '';
inputs.each(function(node, index, nodelist) {
if (node.get('type') != 'checkbox') {
data += node.get('name') + '=' + node.get('value') + '&';
} else {
data += node.get('name') + '=' + node.get('checked') + '&';
}
});
data += 'question=' + question + '&';
data += 'ajax=1';
var cfg = {
method: "POST",
data: data,
arguments: {
caller: 'submit',
}
};
var request = M.modHotquestion.Y.io('view.php', cfg);
};