/
full-list.js
113 lines (98 loc) · 3.37 KB
/
full-list.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
(function(){
'use strict';
let $sortBy = $('#sort-by'),
$fullList = $('#full-list'),
$ReorderBtn = $('#guide-reorder'),
$ReorderCancelBtn = $('#guide-reorder-cancel'),
EQG = !!window.EQG;
$sortBy.on('change',function(){
let baseUrl = $sortBy.data('base-url'),
val = $sortBy.val(),
url = `${baseUrl}?ajax&sort_by=${val}`;
$.Dialog.wait('Changing sort order');
$.get(url, $.mkAjaxHandler(function(){
if (!this.status) return $.Dialog.fail(false, this.message);
$fullList.html(this.html);
reobserve();
$ReorderBtn.prop('disabled', val.length > 0);
history.replaceState(history.state,'',this.stateUrl);
$.Dialog.close();
}));
});
const io = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (!entry.isIntersecting)
return;
const el = entry.target;
io.unobserve(el);
const img = new Image();
img.src = el.dataset.src;
$(img).on('load',function(){
if (el.classList.contains('border'))
img.classList.add('border');
$(el).replaceWith(img).css('opacity',0).animate({opacity:1},300);
});
});
});
function reobserve(){
$fullList.find('section > ul .image-promise').each((_, el) => io.observe(el));
}
reobserve();
if (window.Sortable){
$fullList.on('click','.sort-alpha',function(){
let $section = $(this).closest('section'),
$ul = $section.children('ul');
$ul.children().sort(function(a,b){
return $(a).text().trim().localeCompare($(b).text().trim());
}).appendTo($ul);
});
$ReorderBtn.on('click',function(){
if (!$ReorderBtn.hasClass('typcn-tick')){
$ReorderBtn.removeClass('typcn-arrow-unsorted darkblue').addClass('typcn-tick green').html('Save');
$fullList.addClass('sorting').children().each(function(){
let $names = $(this).children('ul');
$names.children().each(function(){
let $li = $(this);
$li.data('orig-index', $li.index());
}).children().moveAttr('href','data-href');
$names.sortable({ draggable: 'li' });
});
$('.sort-alpha').add($ReorderCancelBtn).removeClass('hidden');
}
else {
$.Dialog.wait('Re-ordering appearances');
let list = [];
$fullList.children().children('ul').children().each(function(){
list.push($(this).children().attr('data-href').split('/').pop().replace(/^(\d+)\D.*$/,'$1'));
});
const data = {
list: list.join(','),
ordering: $sortBy.val(),
};
if (EQG)
data.eqg = true;
$.API.post('/cg/full/reorder', data, function(){
if (!this.status) return $.Dialog.fail(false, this.message);
$fullList.removeClass('sorting').html(this.html);
reobserve();
$ReorderBtn.removeClass('typcn-tick green').addClass('typcn-arrow-unsorted darkblue').html('Re-order');
$ReorderCancelBtn.addClass('hidden');
$.Dialog.close();
});
}
});
$ReorderCancelBtn.on('click',function(){
$ReorderBtn.removeClass('typcn-tick green').addClass('typcn-arrow-unsorted darkblue').html('Re-order');
$fullList.removeClass('sorting').children().each(function(){
let $names = $(this).children('ul');
$names.children().sort(function(a, b){
a = $(a).data('orig-index');
b = $(b).data('orig-index');
return a > b ? 1 : (a < b ? -1 : 0);
}).appendTo($names).removeData('orig-index').children().moveAttr('data-href', 'href');
$names.sortable('destroy');
});
$('.sort-alpha').add($ReorderCancelBtn).addClass('hidden');
});
}
})();