forked from dokufreaks/plugin-blogtng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
256 lines (228 loc) · 7.86 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* Javascript for DokuWiki Plugin BlogTNG
*/
var blogtng = {
/**
* Attach the validation checking to the comment form
*
* @author Michael Klier <[email protected]>
*/
validate_attach: function(obj) {
if(!obj) return;
jQuery(obj).click(function() { return blogtng.validate(); });
},
/**
* Validates the comment form inputs and highlights
* missing fields on client side
*
* @author Michael Klier <[email protected]>
*/
validate: function() {
var inputs = new Array(
'blogtng__comment_name',
'blogtng__comment_mail',
'wiki__text');
for(var i = 0; i < inputs.length; i++) {
var input = jQuery("#"+inputs[i]).get(0);
if(input) {
if(!input.value) {
input.className = 'edit error';
input.focus();
return false;
} else {
input.className = 'edit';
}
}
}
return true;
},
/**
* Attach the AJAX preview action to the comment form
*
* @author Michael Klier <[email protected]>
*/
preview_attach: function(obj, wrap, previewid) {
if(!obj) return;
if(!wrap) return;
jQuery(obj).click(function(e) {
blogtng.preview(wrap,previewid);
e.preventDefault();
e.stopPropagation();
return false;
});
},
/**
* Uses AJAX to render and preview the comment before submitting
*
* @author Michael Klier <[email protected]>
*/
preview: function(wrap,previewid) {
if(!blogtng.validate()) return false;
var preview = jQuery("#"+previewid).get(0);
if(!preview){
if(!wrap) return false;
preview = document.createElement('div');
preview.id = previewid;
wrap.appendChild(preview);
}
preview.innerHTML = '<img src="'+DOKU_BASE+'/lib/images/throbber.gif" />';
var params = {
call: 'blogtng__comment_preview',
tplname: jQuery('#blogtng__comment_form').data('tplname')
};
var $name = jQuery('#blogtng__comment_name');
var $email = jQuery('#blogtng__comment_mail');
var $web = jQuery('#blogtng__comment_web');
var $text = jQuery('#wiki__text');
if($name.length > 0) params.name = $name.val();
if($email.length > 0) params.mail = $email.val();
if($web.length > 0) params.web = $web.val();
if($text.length > 0) params.text = $text.val();
jQuery.post(DOKU_BASE + 'lib/exe/ajax.php', params,
function(data){
if(data === '') return;
preview.innerHTML = data;
});
return false;
},
/**
* Attach the reply action to the comment numbers and add tooltip
* previews to reply syntax markers.
*
* @author Gina Haeussge <[email protected]>
*/
reply_attach: function() {
// attach reply action
var objs = jQuery('a.blogtng_num');
for (var i = 0; i < objs.length; i++) {
objs[i].title = LANG['plugins']['blogtng']['reply'];
jQuery(objs[i]).click(function(e) {
insertAtCarret('wiki__text','@#'+this.href.substring(this.href.lastIndexOf('#')+'#comment_'.length)+': ');
e.preventDefault();
e.stopPropagation();
return false;
});
}
// make "footnotes" from comment references
objs = jQuery('a.blogtng_reply');
for (var i = 0; i < objs.length; i++) {
jQuery(objs[i]).mouseover(function(e) {
commentPopup(e, this.href.substring(this.href.lastIndexOf('#')+'#comment_'.length));
});
}
},
/**
* Attach and handle the check-all checkbox.
*/
insert_checkall_checkbox: function() {
if(jQuery('#blogtng__admin').length == 0) return;
var th = jQuery('#blogtng__admin_checkall_th').get(0);
if(th) {
var html_checkbox = '<input type="checkbox" id="blogtng__admin_checkall" />';
th.innerHTML = html_checkbox;
var checkbox = jQuery('#blogtng__admin_checkall').get(0);
jQuery(checkbox).click(function(e) {
blogtng.checkall();
});
}
},
/**
* Set all checkboxes to checked.
*/
checkall: function() {
objs = jQuery('input.comment_cid');
if(objs) {
var num = objs.length;
for(var i=0;i<num;i++) {
if(objs[i].checked) {
objs[i].checked = false;
} else {
objs[i].checked = true;
}
}
}
}
};
/**
* Display an insitu comment popup. Heavily copied from the footnote insitu
* popup.
*
* FIXME: make the footnote one wrap a generic function to define popups?
*
* @author Andreas Gohr <[email protected]>
* @author Chris Smith <[email protected]>
* @author Gina Haeussge <[email protected]>
*/
function commentPopup(e, id){
var obj = e.target;
// get or create the comment popup div
var comment_div = jQuery('#insitu__comment').get(0);
if(!comment_div){
comment_div = document.createElement('div');
comment_div.id = 'insitu__comment';
comment_div.className = 'insitu-footnote JSpopup dokuwiki';
// autoclose on mouseout - ignoring bubbled up events
jQuery(comment_div).mouseout(function(e){
if(e.target != comment_div){
e.stopPropagation();
return;
}
// check if the element was really left
if(e.pageX){ // Mozilla
var bx1 = findPosX(comment_div);
var bx2 = bx1 + comment_div.offsetWidth;
var by1 = findPosY(comment_div);
var by2 = by1 + comment_div.offsetHeight;
var x = e.pageX;
var y = e.pageY;
if(x > bx1 && x < bx2 && y > by1 && y < by2){
// we're still inside boundaries
e.stopPropagation();
return;
}
}else{ // IE
if(e.offsetX > 0 && e.offsetX < comment_div.offsetWidth-1 &&
e.offsetY > 0 && e.offsetY < comment_div.offsetHeight-1){
// we're still inside boundaries
e.stopPropagation();
return;
}
}
// okay, hide it
comment_div.style.display='none';
});
document.body.appendChild(comment_div);
}
// locate the comment anchor element
var a = jQuery("#comment_"+id ).get(0);
if (!a){ return; }
// anchor parent is the footnote container, get its innerHTML
var content = new String (a.innerHTML);
// prefix ids on any elements with "insitu__" to ensure they remain unique
content = content.replace(/\bid=\"(.*?)\"/gi,'id="insitu__$1');
// now put the content into the wrapper
comment_div.innerHTML = content;
// position the div and make it visible
var x; var y;
if(e.pageX){ // Mozilla
x = e.pageX;
y = e.pageY;
}else{ // IE
x = e.offsetX;
y = e.offsetY;
}
comment_div.style.position = 'absolute';
comment_div.style.left = (x+2)+'px';
comment_div.style.top = (y+2)+'px';
comment_div.style.display = '';
}
/**
* Attach events
*/
jQuery(function() {
blogtng.validate_attach(jQuery('#blogtng__comment_submit').get(0));
blogtng.preview_attach(jQuery('#blogtng__preview_submit').get(0),jQuery('#blogtng__comment_form_wrap').get(0),'blogtng__comment_preview');
blogtng.reply_attach();
blogtng.insert_checkall_checkbox();
});
// vim:ts=4:sw=4:et: