+
+ // Auto sort control
+ const sortOperations = {
+ name: function(a, b) {
+ const aName = a.getAttribute('data-name').trim().toLowerCase();
+ const bName = b.getAttribute('data-name').trim().toLowerCase();
+ return aName.localeCompare(bName);
+ },
+ created: function(a, b) {
+ const aTime = Number(a.getAttribute('data-created'));
+ const bTime = Number(b.getAttribute('data-created'));
+ return bTime - aTime;
+ },
+ updated: function(a, b) {
+ const aTime = Number(a.getAttribute('data-update'));
+ const bTime = Number(b.getAttribute('data-update'));
+ return bTime - aTime;
+ },
+ chaptersFirst: function(a, b) {
+ const aType = a.getAttribute('data-type');
+ const bType = b.getAttribute('data-type');
+ if (aType === bType) {
+ return 0;
+ }
+ return (aType === 'chapter' ? -1 : 1);
+ },
+ chaptersLast: function(a, b) {
+ const aType = a.getAttribute('data-type');
+ const bType = b.getAttribute('data-type');
+ if (aType === bType) {
+ return 0;
+ }
+ return (aType === 'chapter' ? 1 : -1);
+ },
+ };
+
+ let lastSort = '';
+ let reverse = false;
+ const reversableTypes = ['name', 'created', 'updated'];
+
+ $container.on('click', '.sort-box-options [data-sort]', function(event) {
+ event.preventDefault();
+ const $sortLists = $(this).closest('.sort-box').find('ul');
+ const sort = $(this).attr('data-sort');
+
+ reverse = (lastSort === sort) ? !reverse : false;
+ let sortFunction = sortOperations[sort];
+ if (reverse && reversableTypes.includes(sort)) {
+ sortFunction = function(a, b) {
+ return 0 - sortOperations[sort](a, b)
+ };
+ }
+
+ $sortLists.each(function() {
+ const $list = $(this);
+ $list.children('li').sort(sortFunction).appendTo($list);
+ });
+
+ lastSort = sort;
+ updateMapInput();
+ });
+