forked from cosmocode/recommend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
78 lines (67 loc) · 2.34 KB
/
script.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
(function () {
/* Lib */
var recommend_ajax_call = 'plugin_recommend';
function sack_form(form, fnc) {
var ajax = new sack(DOKU_BASE + 'lib/exe/ajax.php');
ajax.setVar('call', recommend_ajax_call);
function serializeByTag(tag) {
var inps = form.getElementsByTagName(tag);
for (var inp in inps) {
if (inps[inp].name) {
ajax.setVar(inps[inp].name, inps[inp].value);
}
}
}
serializeByTag('input');
serializeByTag('textarea');
ajax.onCompletion = fnc;
ajax.runAJAX();
return false;
}
function bind(fnc, val) {
return function () {
return fnc(val);
};
}
function change_form_handler(forms, handler) {
if (!forms) return;
for (var formid in forms) {
var form = forms[formid];
form.onsubmit = bind(handler, form);
}
}
/* Recommend */
function recommend_box(content) {
var div = $('recommend_box');
if (!div) {
div = document.createElement('div');
div.id = 'recommend_box';
} else if (content === '') {
div.parentNode.removeChild(div);
return;
}
div.innerHTML = content;
document.body.appendChild(div);
return div;
}
function recommend_handle() {
if (this.response === "AJAX call '" + recommend_ajax_call + "' unknown!\n") {
/* No user logged in. */
return;
}
if (this.responseStatus[0] === 204) {
var box = recommend_box('<form id="recommend_plugin" accept-charset="utf-8" method="post" action="?do=recommend"><div class="no"><fieldset><legend>Finished</legend<p>Thanks for recommending our site.</p><input type="submit" class="button" value="Cancel" name="do[cancel]"/></fieldset></div></form>');
} else {
var box = recommend_box(this.response);
box.getElementsByTagName('label')[0].focus();
change_form_handler(box.getElementsByTagName('form'),
function (form) {return sack_form(form, recommend_handle); });
}
var inputs = box.getElementsByTagName('input');
inputs[inputs.length - 1].onclick = function() {recommend_box(''); return false;};
}
addInitEvent(function () {
change_form_handler(getElementsByClass('btn_recommend', document, 'form'),
function (form) {return sack_form(form, recommend_handle); });
});
}());