/
admin.js
628 lines (539 loc) · 27.8 KB
/
admin.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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
define(["config", "popup", "api", "msg", "items", "forms", "jquery", "jqueryui/sortable"], function(_config, popup, api, msg, items, forms) {
// array of function pointers for various admin menus
var menus = [
admin_menu
];
// div for storing menus
var menu_div;
// optional control panel area for admin functions
var controls = $("<div>").addClass("admin_controls alert");
// make generic drop down menu! (I want to have lots of these!)
function generic_menu(name, icon, obj) {
// don't return anything if there are no menu items
if (obj.length === 0) return null;
var m = $("<div>").addClass("btn-group").append('<a class="btn dropdown-toggle" data-toggle="dropdown" href="#"><i class="az4im '+icon+'"></i> '+name+' <span class="caret"></span></a>');
var list = $("<ul>").addClass("dropdown-menu pull-right").appendTo(m);
for(var ii=0; ii<obj.length; ii++) {
list.append($("<li><a href='#'>"+obj[ii].name+"</a></li>").click(obj[ii].func));
}
return m;
}
// defines admin controls!
function admin_menu(data, list) {
// return if not admin!
if (!_config.settings.database_scan && !_config.settings.database_pricer) return;
// return if we're not showing edit tools
if (!_config.editTools) return;
var menus = [];
if (data && (data.cats || data.items) ) {
// force page refresh
menus.push(
{
name: "Force Refresh",
func: function() {
list.reload();
return false;
}
}
);
}
// only allow item adding if there are no child categories
if (_config.settings.database_scan && list.type == "cat" && data && data.cats && data.cats.length === 0) {
menus.push(
{
name: "Add Items",
func: function() {
popup.form([
{
type: "textarea",
name: "items"
},
{
type: "radio",
name: "inserttop",
options: [
{value: "true", name: "Top"},
{value: "false", name: "Bottom"}
],
value: "true"
}
], "Add Items to Category", function(form) {
api.post("admin/add/items/"+data.id, form, function(response) {
// show error/success message
if (response.error) {
msg.error(response.error);
} else {
msg.success(response.success);
// reload list object
if (list.reload) list.reload();
}
popup.hide();
});
});
return false;
}
}
);
}
// add new category to this category
// note that cats usually report one item as it's top-rated item
if (_config.settings.database_admin && list.type == "cat" && data && data.items && !data.items[0]) {
menus.push(
{
name: "Add New Category",
func: function() {
popup.form([
{
type: "text",
name: "name",
label: "Category Name"
},
{
type: "text",
name: "icon",
label: "Icon"
}
], "Add New Child Category", function(form) {
api.post("admin/add/cat/"+data.id, form, function(response) {
// show error/success message
if (response.error) {
msg.error(response.error);
} else {
msg.success(response.success);
// reload list object
if (list.reload) list.reload();
}
popup.hide();
});
});
return false;
}
}
);
}
// edit all items
if ((_config.settings.database_edit || _config.settings.database_pricer) && data && data.items && data.items.length > 0) {
menus.push(
{
name: "Edit All Items",
func: function() {
if (!list.reload) return false;
list.reload(function(){
// get refreshed item data
data.items = list.data;
var h = $("<div>");
var edit_item = function(new_data) {
// find image to save
var data_img;
for(var ii=0; ii<data.items.length; ii++) {
if (data.items[ii].id == new_data.id)
data_img = data.items[ii].image;
}
api.post("edit/item/"+new_data.id, new_data, function(res) {
if (res.item) {
msg.success("Successfully edited item "+res.item.id);
// restore item image
res.item.image = data_img;
// update this editor div
update_editor(res.item);
} else if (res.error) {
msg.error(res.error);
}
});
};
var update_editor = function(item) {
$("#item_edit_"+item.id).html(
items.editor(item, edit_item)
);
};
for(var ii=0; ii<data.items.length; ii++) {
h.append($("<div>")
.attr("id", "item_edit_"+data.items[ ii ].id)
.html(
items.editor(data.items[ ii ], edit_item)
)
);
h.append("<hr />");
}
list.list.html(h);
controls.html(
$("<button>").addClass("btn btn-warning")
.text("Stop Editing")
.click(function() {
list.reload();
})
);
menu_div.append(controls);
});
return false;
}
}
);
}
// edit page
if (_config.settings.database_admin && list.type == "cat" && data) {
menus.push(
{
name: "Edit Page",
func: function() {
popup.form([
{
type: "textarea",
name: "page",
label: "Page Content",
value: data.page,
width: "600px"
}
], "Edit Category Page", function(form) {
console.log(form);
api.post("edit/page/"+data.id, form, function(response) {
// show error/success message
if (response.error) {
msg.error(response.error);
} else {
msg.success(response.success);
// reload list object
if (list.reload) list.reload();
}
popup.hide();
});
});
return false;
}
}
);
}
// get cat IDs
if (_config.settings.database_scan && list.type == "cat" && data) {
menus.push(
{
name: "Get Item IDs",
func: function() {
api.post("admin/get/cat/"+data.id, {}, function(d) {
popup.form([
{
type: "textarea",
name: "page",
label: "ID List",
value: d.data,
width: "600px"
}
], "Whee");
});
return false;
}
}
);
}
// moderate add items
if (_config.settings.database_admin && list.type=="cat" && data && data.items && data.items.length) {
var isMods = false;
for(var ij=0; ij<data.items.length; ij++) {
if (data.items[ij].hidden) isMods = true;
}
if (isMods) {
menus.push(
{
name: "Approve new items",
func: function() {
// send new order to server automagically upon dropping an item
api.post("admin/moderate/add_items", {
cat_id: data.id
}, function(response) {
// show error/success message
if (response.error) {
msg.error(response.error);
} else {
msg.success(response.success);
}
// reload list
if (list.reload) list.reload();
});
return false;
}
}
);
}
}
// reorder items
if (_config.settings.database_scan && list.type=="cat" && data && data.items && data.cats && (data.items.length || data.cats.length) ) {
menus.push(
{
name: "Organise Items",
func: function() {
// remember current list page setting
var mem = list.page_items;
list.page_items = 0;
// reload the list without any pages on
list.reload(function(){
// grab actual list object
var l = $(list.body.find(".az4list")[0]);
// make list sortable
l.sortable({
update : function () {
// send new order to server automagically upon dropping an item
api.post("admin/reorder/"+data.id, {
order: $(this).sortable("toArray")
}, function(response) {
// show error/success message
if (response.error) {
msg.error(response.error);
} else {
msg.success(response.success);
}
// don't reload the list so we're able to sort multiple items quickly
});
}
});
// highlight that we're doing something by colouring all items
l.find("li").addClass("alert-info");
// now that list is reloaded, restore item page number
list.page_items = mem;
// cancel button
controls.html(
$("<button>").addClass("btn btn-warning")
.text("Stop Sorting")
.click(function() {
list.reload();
})
);
menu_div.append(controls);
});
return false;
}
}
);
}
// mass editor
if (_config.settings.database_edit && (list.type=="cat" || list.type=="update") && data && data.items && data.items.length ) {
menus.push(
{
name: "Mass Edit",
func: function() {
// remember current list page setting
var mem = list.page_items;
list.page_items = 0;
// reload the list without any pages on
list.reload(function(){
// grab actual list object
var l = $(list.body.find(".az4list")[0]);
var items2edit = [];
// remove item click handlers and replace with item selector
l.find("a.thumbnail").each(function() {
$(this).off('click');
$(this).click(function() {
var found = -1;
for(var ii=0; ii<items2edit.length; ii++) {
if (items2edit[ ii ] == $(this).parent().attr("id")) {
found = ii;
}
}
if (found >= 0) {
$(this).removeClass("highlight");
items2edit.splice(found, 1);
} else {
$(this).addClass("highlight");
items2edit.push(parseInt($(this).parent().attr("id")));
}
return false;
});
});
// now that list is reloaded, restore item page number
list.page_items = mem;
controls.html("");
// setup mass edit options
var options = [];
for(var ii in _config.settings.prices) {
options.push({
name: _config.settings.prices[ ii ].name+" "+ii,
value: _config.settings.prices[ ii ].field
});
}
options.push({
name: "Furniture Slots",
value: "slots"
});
// work out if we have a default value for the dropdown
if (data.country) {
var default_func = "";
if (_config.regions[ data.country.toLowerCase() ]) {
default_func = _config.settings.prices[ _config.regions[ data.country.toLowerCase() ].pricer ].field;
}
}
var inputs = [
{
type: "dropdown",
name: "function",
options: options,
change: function() {
// modify form if we have a special dropdown
var new_type = $(this).attr("value");
if (dropdowns[ new_type ]) {
// we have a dropdown!
$(".massedit_text").hide();
$(".massedit_dropdown").show();
// build new list of dropdowns
var h = "";
for(var ii=0; ii<dropdowns[ new_type ].length; ii++) {
h += "<option value='"+dropdowns[ new_type ][ii].value+"'>"+dropdowns[ new_type ][ii].name+"</option>";
}
$(".massedit_dropdown").html(h);
} else {
// normal text entry
$(".massedit_text").show();
$(".massedit_dropdown").hide();
}
},
value: default_func
},
{
type: "text",
name: "value_text",
cssclass: "massedit_text"
},
{
type: "dropdown",
name: "value_dropdown",
options: [],
cssclass: "massedit_dropdown hide"
},
{
type: "submit",
value: "Make Changes"
}
];
// some editing functions can have dropdowns instead of text entry
var dropdown_devs = [];
for(var ii in _config.settings.devs) {
dropdown_devs.push({
name: _config.settings.devs[ ii ],
value: ii
});
}
var dropdown_types = [];
for(var ii in _config.settings.item_types) {
dropdown_types.push({
name: _config.settings.item_types[ ii ],
value: ii
});
}
var dropdowns = {
gender: [
{
name: "None",
value: ""
},
{
name: "Male",
value: "M"
},
{
name: "Female",
value: "F"
}
],
dev: dropdown_devs,
type: dropdown_types
};
var dropdown_names = {
gender: "Gender",
dev: "Developer",
type: "Item Type"
};
// add dropdowns to inputs list
for(var ii in dropdowns) {
options.push({
name: dropdown_names[ ii ],
value: ii
});
}
var form = forms(inputs, function(form_data, reset) {
if (!items2edit.length) return;
var data = {
ids: items2edit.join(",")
};
// choose which data input to use
if (dropdowns[ form_data['function'] ]) {
data[ form_data['function'] ] = form_data['value_dropdown'];
} else {
data[ form_data['function'] ] = form_data['value_text'];
}
api.post("edit/items", data, function(result) {
if (result.success) {
msg.success(result.success);
} else if (result.error) {
msg.error(result.error);
}
});
// allow form to be resubmitted
reset();
});
controls.append(form);
// select all button
controls.append(
$("<button>").addClass("btn btn-info")
.text("Select All")
.click(function() {
l.find("a.thumbnail").each(function() {
if (!$(this).hasClass("highlight")) {
$(this).click();
}
});
return false;
})
);
// select none button
controls.append(
$("<button>").addClass("btn btn-info")
.text("Select None")
.click(function() {
l.find("a.thumbnail").each(function() {
if ($(this).hasClass("highlight")) {
$(this).click();
}
});
return false;
})
);
// invert button
controls.append(
$("<button>").addClass("btn btn-info")
.text("Invert Selection")
.click(function() {
l.find("a.thumbnail").each(function() {
$(this).click();
});
return false;
})
);
// cancel button
controls.append(
$("<button>").addClass("btn btn-warning")
.text("Stop Editing")
.click(function() {
list.reload();
})
);
menu_div.append(controls);
});
return false;
}
}
);
}
return generic_menu("Admin Tools", "admin", menus);
}
function build_menus(list) {
var data = list.fetch_data;
if (!data) return "";
menu_div = $("<div>").addClass("admin_menus");
for(var ii=0; ii<menus.length; ii++) {
var t = menus[ii](data, list);
if (t) menu_div.append(t);
}
return menu_div;
}
return {
build_menus: build_menus
};
});