-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcompletion.js
124 lines (112 loc) · 4.27 KB
/
completion.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
/**
* Created by ishani on 01/03/2017.
*/
M.block_navbuttons = M.block_navbuttons || {};
M.block_navbuttons.completion = {};
M.block_navbuttons.completion.init = function (Y) {
var handle_success = function (id, o, args) {
if (o.responseText != 'OK') {
alert('An error occurred when attempting to mark your activity complete.\n\n(' + o.responseText + '.)');
// TODO: localize.
} else {
var current = args.state.get('value');
var btntype = args.btntype.get('value');
var altstr,
titlestr;
if (current == 1) {
// Successfully marked as complete, so change button to 'Mark incomplete'.
altstr = M.util.get_string('incompletebuttontext', 'block_navbuttons');
titlestr = M.util.get_string('incompletebuttontext', 'block_navbuttons');
args.state.set('value', 0);
if (btntype === 'icon') {
args.image.set('src', M.util.image_url('crossicon', 'block_navbuttons'));
args.image.set('alt', altstr);
args.image.set('title', titlestr);
} else {
args.submit.set('value', altstr);
}
} else {
altstr = M.util.get_string('completebuttontext', 'block_navbuttons');
titlestr = M.util.get_string('completebuttontext', 'block_navbuttons');
args.state.set('value', 1);
if (btntype === 'icon') {
args.image.set('src', M.util.image_url('tickicon', 'block_navbuttons'));
args.image.set('alt', altstr);
args.image.set('title', titlestr);
} else {
args.submit.set('value', altstr);
}
}
}
args.ajax.remove();
};
var handle_failure = function (id, o, args) {
alert('An error occurred when attempting to mark your activity complete.\n\n(' + o.responseText + '.)');
// TODO: localize.
args.ajax.remove();
};
var toggle = function (e) {
e.preventDefault();
var form = e.target;
var cmid = 0;
var completionstate = 0;
var state = null;
var submit = null;
var image = null;
var modulename = null;
var btntype = null;
var inputs = Y.Node.getDOMNode(form).getElementsByTagName('input');
for (var i = 0; i < inputs.length; i++) {
switch (inputs[i].name) {
case 'id':
cmid = inputs[i].value;
break;
case 'completionstate':
completionstate = inputs[i].value;
state = Y.one(inputs[i]);
break;
case 'modulename':
modulename = Y.one(inputs[i]);
break;
case 'btntype':
btntype = Y.one(inputs[i]);
break;
}
if (inputs[i].type === 'submit') {
submit = Y.one(inputs[i]);
}
if (inputs[i].type === 'image') {
image = Y.one(inputs[i]);
}
}
// Start spinning the ajax indicator.
var ajax = Y.Node.create('<div class="ajaxworking" />');
form.append(ajax);
var cfg = {
method: "POST",
data: 'id=' + cmid + '&completionstate=' + completionstate + '&btntype=' + btntype + '&fromajax=1&sesskey='
+ M.cfg.sesskey,
on: {
success: handle_success,
failure: handle_failure
},
arguments: {
state: state,
submit: submit,
image: image,
btntype: btntype,
ajax: ajax,
modulename: modulename
}
};
Y.use('io-base', function (Y) {
Y.io(M.cfg.wwwroot + '/blocks/navbuttons/togglecompletion.php', cfg);
});
};
// Register submit handlers on manual tick completion forms.
Y.all('form.togglecompletion').each(function (form) {
if (!form.hasClass('preventjs')) {
Y.on('submit', toggle, form);
}
});
};