vito / chyrp

The ultra-lightweight ultra-flexible blogging engine with a fetish for birds and misspellings.

This URL has Read+Write access

chyrp / includes / admin.js.php
100644 482 lines (413 sloc) 16.287 kb
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<?php
define('JAVASCRIPT', true);
require_once "common.php";
error_reporting(0);
header("Content-Type: application/x-javascript");
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 03 Jun 1991 05:30:00 GMT");
$action = $_GET['action'];
$page = fallback($_GET['page'], 1, true);
$more_options_string = (empty($_COOKIE['show_more_options'])) ? __("More Options &raquo;") : __("&laquo; Fewer Options") ;
?>
<!-- --><script>
$(function(){
// Scan AJAX responses for errors.
$(document).ajaxComplete(function(imconfused, request){
var response = request.responseText
if (isError(response))
alert(response.replace(/(HEY_JAVASCRIPT_THIS_IS_AN_ERROR_JUST_SO_YOU_KNOW|<([^>]+)>\n?)/gm, ""))
})
 
if ($("#more_options").size()) {
// Fancify the "More Options" links.
$(document.createElement("a")).attr({
id: "more_options_link",
href: "javascript:void(0)"
}).addClass("more_options_link").html("<?php echo $more_options_string; ?>").insertBefore(".buttons")
$("#more_options").clone().insertAfter("#more_options_link").removeClass("js_disabled")<?php if (empty($_COOKIE['show_more_options'])): ?>.css("display", "none")<?php endif; ?>
 
$("#more_options_link").click(function(){
if ($("#more_options").css("display") == "none") {
$(this).html("<?php echo __("&laquo; Fewer Options"); ?>")
Cookie.set("show_more_options", "true", 30)
} else {
$(this).html("<?php echo __("More Options &raquo;"); ?>")
Cookie.destroy("show_more_options")
}
$("#more_options").slideToggle()
})
}
 
// Remove things that only exist for JS-disabled users.
$(".js_disabled").remove()
$(".js_enabled").css("display", "block")
 
$("textarea").keyup(function(event){
if ($(this).val().match(/\\ct/gm))
$(this).val($(this).val().replace(/\\ct/gm, " "))
})
 
// Automated PNG fixing.
$.ifixpng("<?php echo $config->chyrp_url; ?>/admin/images/icons/pixel.gif")
$("img[@src$=.png]").ifixpng()
 
<?php if (match("/(edit|write)_/", $_GET['action'])): ?>
prepare_write_bling()
 
<?php endif; ?>
// "Help" links should open in popup windows.
$(".help").click(function(){
window.open($(this).attr("href"), "help", "status=0, height=350, width=300")
return false;
})
 
// AJAX post deletion.
$(".post_delete_link").click(function(){
if (!confirm("<?php echo __("Are you sure you want to delete this post?\\n\\nIt cannot be restored if you do this. If you wish to hide it, save it as a draft."); ?>")) return false
var id = $(this).attr("id").replace(/post_delete_/, "")
Post.destroy(id)
return false
})
 
// Content previewing.
prepare_previewer()
 
// Checkbox toggling.
togglers()
 
// Page list reordering
reorder_pages()
 
// Extension enabling/disabling (drag'n'drop)
extend_draggables()
 
<?php if ($_GET['action'] == "extend_modules" or $_GET['action'] == "extend_feathers"): ?>
draw_conflicts()
 
$(window).resize(function(){
draw_conflicts()
})
<?php endif; ?>
})
 
var Post = {
destroy: function(id) {
$("#post_"+id+" .target, #post_"+id+".target").loader()
$.post("<?php echo $config->chyrp_url; ?>/includes/ajax.php", { action: "delete_post", id: id }, function(response){
$("#post_"+id+" .target, #post_"+id+".target").loader(true)
if (isError(response)) return
$("#post_"+id).animate({ height: "hide", opacity: "hide" }).remove()
})
}
}
 
function prepare_write_bling() {
// Add the "Bookmarklet" with JS to the write nav since only JS-enabled users can use it.
$(document.createElement("li")).addClass("bookmarklet right").html("<?php echo _f("Bookmarklet: %s", array('<a class=\"no_drag\" href=\"javascript:var%20d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,s=(e?e():(k)?k():(x?x.createRange().text:0)),f=\''.$config->chyrp_url.'/includes/bookmarklet.php\',l=d.location,e=encodeURIComponent,p=\'?url=\'+e(l.href)+\'&title=\'+e(d.title)+\'&selection=\'+e(s),u=f+p;a=function(){if(!w.open(u,\'t\',\'toolbar=0,resizable=0,status=1,width=450,height=430\'))l.href=u;};if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else%20a();void(0)\">Chyrp!</a>')); ?>").prependTo(".write_post_nav")
 
// Auto-expand text fields & auto-grow textareas.
$("input.text").each(function(){
if ($(this).parent().parent().attr("class") == "more_options") return
$(this).css("min-width", $(this).width()).Autoexpand()
})
$("textarea").each(function(){
$(this).css("min-height", $(this).height()).autogrow()
})
 
// Make the Feathers sortable
$("#sub-nav li").css("float", "left")
$("#sub-nav").sortable({
axis: "x",
containment: "#sub-nav",
placeholder: "feathers_sort",
opacity: 0.8,
delay: 1,
revert: true,
cancel: "a.no_drag, a[href$=write_page]",
update: function(){
$.post("<?php echo $config->chyrp_url; ?>/includes/ajax.php", "action=reorder_feathers&"+$("#sub-nav").sortable("serialize"))
}
})
}
 
function togglers() {
var all_checked = true
$("#toggler").html('<label for="toggle">Toggle All</label><input class="checkbox" type="checkbox" name="toggle" id="toggle" />')
$(".toggler").html('<input class="checkbox" type="checkbox" name="toggle" id="toggle" />')
$("#toggle").click(function(){
$("form#new_group, form#group_edit, table").find(":checkbox").not("#toggle").each(function(){
this.checked = document.getElementById("toggle").checked
})
})
$("form#new_group, form#group_edit").find(":checkbox").not("#toggle").each(function(){
if (!all_checked) return
all_checked = this.checked
})
if ($("#toggler").size())
document.getElementById("toggle").checked = all_checked
}
 
function prepare_previewer() {
if ($(".preview_me").length > 0) {
var feather = ($("#write_feather").size()) ? $("#write_feather").val() : ""
var feather = ($("#edit_feather").size()) ? $("#edit_feather").val() : feather
$(document.createElement("div")).css("display", "none").attr("id", "preview").insertBefore("#write_form, #edit_form")
$(document.createElement("button")).html("<?php echo __("Preview &rarr;"); ?>").attr({ "type": "submit", "accesskey": "p" }).click(function(){
$("#preview").load("<?php echo $config->chyrp_url; ?>/includes/ajax.php", { action: "preview", content: $(".preview_me").val(), feather: feather }, function(){
$(this).fadeIn("fast")
})
return false
}).appendTo(".buttons")
}
}
 
function extend_draggables() {
$(".enable h2, .disable h2").append(" <span class=\"sub\"><?php echo __("(drag)"); ?></span>")
$(".disable ul li, .enable ul li").draggable({
zIndex: 100,
cancel: "a",
revert: true
})
$(".enable ul, .disable ul").droppable({
accept: "ul.extend li",
tolerance: "pointer",
activeClass: "active",
hoverClass: "hover",
drop: function(ev, ui) {
var classes = $(this).parent().attr("class").split(" ")
var box = $(this)
var confirmed = false
var previous = $(ui.draggable).parent().parent().attr("class").split(" ")[0]
var action = classes[0]
var type = classes[1]
var extension = $(ui.draggable).attr("class").split(" ")[0]
 
if (previous == action)
return
 
$.post("<?php echo $config->chyrp_url; ?>/includes/ajax.php", { action: "check_confirm", check: extension, type: type }, function(data){
if (data != "" && action == "disable")
var confirmed = (confirm(data)) ? 1 : 0
 
$.ajax({ type: "post", dataType: "json", url: "<?php echo $config->chyrp_url; ?>/includes/ajax.php", data: { action: action + "_" + type, extension: extension, confirm: confirmed }, beforeSend: function(){
box.loader()
}, success: function(json){
box.loader(true)
$(json.notifications).each(function(){
if (this == "") return
alert(this.replace(/<([^>]+)>\n?/gm, ""))
})
} })
})
 
$(ui.draggable).css({ left: 0, right: 0, top: 0, bottom: 0 }).appendTo(this)
 
$("ul.extend").height("auto")
$("ul.extend").each(function(){
if ($(".enable ul.extend").height() > $(this).height())
$(this).height($(".enable ul.extend").height())
if ($(".disable ul.extend").height() > $(this).height())
$(this).height($(".disable ul.extend").height())
draw_conflicts()
})
}
})
$(".info_link").click(function(){
$(this).parent().find(".description").toggle("blind", {}, null, function(){ draw_conflicts() })
return false
})
$("ul.extend li").css("cursor", "move")
$("ul.extend li .description").css("display", "none")
$("ul.extend").each(function(){
if ($(".enable ul.extend").height() > $(this).height())
$(this).height($(".enable ul.extend").height())
if ($(".disable ul.extend").height() > $(this).height())
$(this).height($(".disable ul.extend").height())
})
}
 
function get_parent_hash() {
var parent_hash = ""
$(".sort_pages li").each(function(){
var id = $(this).attr("id").replace(/page_list_/, "")
var parent = $(this).parent().parent() // this > #sort_pages > page_list_(id)
var parent_id = (/page_list_/.test(parent.attr("id"))) ? parent.attr("id").replace(/page_list_/, "") : 0
$(this).attr("parent", parent_id)
parent_hash += "&parent["+id+"]="+parent_id
})
return parent_hash
}
 
function reorder_pages() {
var parent_hash = ""
$(".sort_pages li").css({
cursor: "move",
background: "#f9f9f9",
padding: ".15em .5em",
marginBottom: ".5em",
border: "1px solid #ddd"
})
 
$(".page-item").css("cursor", "move")
$("#content > .sort_pages").attr("id", "sort_pages").NestedSortable({
accept: "page-item",
opacity: 0.8,
nestingPxSpace: 5,
onStop: function(){
var serialize = $.SortSerialize("sort_pages")
var parent_hash = get_parent_hash()
 
$("#content > .sort_pages").loader()
$.post("<?php echo $config->url; ?>/includes/ajax.php", "action=organize_pages&"+serialize.hash+parent_hash, function(){
$("#content > .sort_pages").loader(true)
})
}
})
$(".sort_pages input").remove()
}
 
function remove_from_array(value, array) {
for (i = 0; i < array.length; i++)
if (array[i] == value)
array.splice(i, 1)
return array
}
 
function draw_conflicts() {
if (!$(".extend li.conflict").size() && !($.browser.safari || $.browser.opera || ($.browser.mozilla && $.browser.version >= 1.9)))
return
 
$("#canvas").remove()
 
$("#header, #welcome, #sub-nav, #content a.button, .extend li, #footer, h1, h2").css({
position: "relative",
zIndex: 2
})
$("#header ul li a").css({
position: "relative",
zIndex: 3
})
 
$(document.createElement("canvas")).attr("id", "canvas").prependTo("body")
$("#canvas").css({
position: "absolute",
top: 0,
bottom: 0,
zIndex: 1
}).attr({ width: $(document).width(), height: $(document).height() })
 
var canvas = document.getElementById("canvas").getContext("2d")
var displayed = []
 
$(".extend li.conflict").each(function(){
var classes = $(this).attr("class").split(" ")
classes.shift() // Remove the module's safename class
 
// Remove the "conflict" class
remove_from_array("conflict", classes);
remove_from_array("ui-draggable", classes);
remove_from_array("ui-draggable-dragging", classes);
 
for (i = 0; i < classes.length; i++) {
var conflict = classes[i].replace("conflict_", "module_")
 
if (displayed[$(this).attr("id")+" :: "+conflict])
continue;
 
canvas.strokeStyle = "#d12f19"
canvas.fillStyle = "#fbe3e4"
canvas.lineWidth = 3
 
var this_status = $(this).parent().parent().attr("class").split(" ")[0] + "d"
var conflict_status = $("#"+conflict).parent().parent().attr("class").split(" ")[0] + "d"
 
if (conflict_status != this_status) {
var line_from_x = (conflict_status == "disabled") ? $("#"+conflict).offset().left : $("#"+conflict).offset().left + $("#"+conflict).outerWidth()
var line_from_y = $("#"+conflict).offset().top + 12
 
var line_to_x = (conflict_status == "enabled") ? $(this).offset().left : $(this).offset().left + $(this).outerWidth()
var line_to_y = $(this).offset().top + 12
 
// Line
canvas.moveTo(line_from_x, line_from_y)
canvas.lineTo(line_to_x, line_to_y)
canvas.stroke()
 
// Beginning circle
canvas.beginPath()
if (conflict_status == "disabled")
canvas.arc(line_from_x, line_from_y, 5, 1.35, -1.35, false)
else
canvas.arc(line_from_x, line_from_y, 5, -1.35, 1.35, false)
canvas.fill()
canvas.stroke()
 
// Ending circle
canvas.beginPath()
if (conflict_status == "disabled")
canvas.arc(line_to_x, line_to_y, 5, -1.75, 1.75, false)
else
canvas.arc(line_to_x, line_to_y, 5, 1.75, -1.75, false)
canvas.fill()
canvas.stroke()
} else if (conflict_status == "disabled") {
var line_from_x = $("#"+conflict).offset().left
var line_from_y = $("#"+conflict).offset().top + 12
var line_to_x = $(this).offset().left
var line_to_y = $(this).offset().top + 12
var median = line_from_y + ((line_to_y - line_from_y) / 2)
var curve = line_from_x - 25
 
// Line
canvas.beginPath();
canvas.moveTo(line_from_x, line_from_y)
canvas.quadraticCurveTo(curve, median, line_to_x, line_to_y);
canvas.stroke();
 
// Beginning circle
canvas.beginPath()
canvas.arc(line_from_x, line_from_y, 5, 1.35, -1.35, false)
canvas.fill()
canvas.stroke()
 
// Ending circle
canvas.beginPath()
canvas.arc(line_to_x, line_to_y, 5, 1.35, -1.35, false)
canvas.fill()
canvas.stroke()
} else if (conflict_status == "enabled") {
var line_from_x = $("#"+conflict).offset().left + $("#"+conflict).outerWidth()
var line_from_y = $("#"+conflict).offset().top + 12
var line_to_x = $(this).offset().left + $(this).outerWidth()
var line_to_y = $(this).offset().top + 12
var median = line_from_y + ((line_to_y - line_from_y) / 2)
var curve = line_from_x + 25
 
// Line
canvas.beginPath();
canvas.moveTo(line_from_x, line_from_y)
canvas.quadraticCurveTo(curve, median, line_to_x, line_to_y);
canvas.stroke();
 
// Beginning circle
canvas.beginPath()
canvas.arc(line_from_x, line_from_y, 5, -1.75, 1.75, false)
canvas.fill()
canvas.stroke()
 
// Ending circle
canvas.beginPath()
canvas.arc(line_to_x, line_to_y, 5, -1.75, 1.75, false)
canvas.fill()
canvas.stroke()
}
 
displayed[conflict+" :: "+$(this).attr("id")] = true
}
})
 
return true
}
 
// "Loading..." overlay.
$.fn.loader = function(remove) {
if (remove) {
$(this).next().remove()
return this
}
 
var offset = $(this).offset()
var width = $(this).outerWidth()
var loading_top = ($(this).outerHeight() / 2) - 11
var loading_left = ($(this).outerWidth() / 2) - 63
 
$(this).after("<div class=\"load_overlay\"><img src=\"<?php echo $config->chyrp_url; ?>/includes/close.png\" style=\"display: none\" class=\"close\" /><img src=\"<?php echo $config->chyrp_url; ?>/includes/loading.gif\" style=\"display: none\" class=\"loading\" /></div>")
 
$(".load_overlay .loading").css({
position: "absolute",
top: loading_top+"px",
left: loading_left+"px",
display: "inline"
})
 
$(".load_overlay .close").css({
position: "absolute",
top: "3px",
right: "3px",
color: "#fff",
cursor: "pointer",
display: "inline"
}).click(function(){ $(this).parent().remove() })
 
$(".load_overlay").css({
position: "absolute",
top: offset.top,
left: offset.left,
zIndex: 100,
width: $(this).outerWidth(),
height: $(this).outerHeight(),
background: ($.browser.msie) ? "transparent" : "transparent url('<?php echo $config->chyrp_url; ?>/includes/trans.png')",
textAlign: "center",
filter: ($.browser.msie) ? "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='<?php echo $config->chyrp_url; ?>/includes/trans.png');" : ""
})
 
return this
}
 
var Cookie = {
set: function(name, value, expires) {
var today = new Date()
today.setTime( today.getTime() )
 
if (expires)
expires = expires * 1000 * 60 * 60 * 24
 
var expires_date = new Date(today.getTime() + (expires))
 
document.cookie = name+"="+escape(value)+
((expires) ? ";expires="+expires_date.toGMTString() : "" )+";path=/"
},
destroy: function(name) {
document.cookie = name+"=;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT"
}
}
 
// Used to check if AJAX responses are errors.
function isError(text) {
return /HEY_JAVASCRIPT_THIS_IS_AN_ERROR_JUST_SO_YOU_KNOW/m.test(text);
}
 
<?php $trigger->call("admin_javascript"); ?>
<!-- --></script>