Gazelle/static/functions/sort.js

132 lines
3.3 KiB
JavaScript
Raw Normal View History

2012-10-27 08:00:09 +00:00
var sortableTable;
2014-03-16 08:00:50 +00:00
$(function () {
2012-10-30 08:00:18 +00:00
2014-03-16 08:00:50 +00:00
// tips/notes:
// In HTML add data-sorter="false" to table headings (TH) that should not be sorted
// or add data-sorter="myParser" to THs that require a custom parser
// sorts dates placed in the title attribute of a td
2012-10-30 08:00:18 +00:00
$.tablesorter.addParser({
2014-03-16 08:00:50 +00:00
id: 'relativeTime',
2012-10-30 08:00:18 +00:00
is: function (s) {
return false;
},
2014-03-16 08:00:50 +00:00
format: function (str, table, td) {
2012-10-30 08:00:18 +00:00
return td.title;
},
type: 'text'
});
2014-03-16 08:00:50 +00:00
// sort to ignore (English) articles
// add data-sorter="ignoreArticles" to THs
$.tablesorter.addParser({
id: 'ignoreArticles',
$format: $.tablesorter.getParserById('text').format,
articlesRegEx: /^(?:the\s|a\s|an\s)/i,
is: function () {
return false;
},
format: function (s, table) {
return this.$format((s || '').replace(this.articlesRegEx, ''), table);
},
type: 'text'
});
2012-10-27 08:00:09 +00:00
sortableTable = {
2014-03-16 08:00:50 +00:00
container: $('#manage_collage_table'),
form: $('#drag_drop_collage_form'),
serialInput: $('#drag_drop_collage_sort_order'),
check: $('#check_all'),
counter: function () {
2012-10-27 08:00:09 +00:00
var x = 10;
$('input.sort_numbers').each(function () {
this.value = x;
x += 10;
});
this.serializer();
},
2014-03-16 08:00:50 +00:00
color: function () {
2012-10-27 08:00:09 +00:00
var i = 0, css;
$('tr.drag').each(function () {
css = i % 2 === 0 ? ['rowa', 'rowb'] : ['rowb', 'rowa'];
$(this).removeClass(css[0]).addClass(css[1]);
i++;
});
},
2014-03-16 08:00:50 +00:00
serializer: function () {
2012-10-27 08:00:09 +00:00
this.serialInput.val(this.container.sortable('serialize'));
},
2014-03-16 08:00:50 +00:00
save: function () {
2012-10-27 08:00:09 +00:00
sortableTable.form.submit();
},
2014-03-16 08:00:50 +00:00
widthFix: function (e, row) {
2012-10-27 08:00:09 +00:00
row.children('td').each(function () {
$(this).width($(this).width());
});
return row;
},
2014-03-16 08:00:50 +00:00
init: function () {
2012-10-27 08:00:09 +00:00
$('.drag_drop_save').removeClass('hidden');
2012-07-11 08:00:16 +00:00
2012-10-27 08:00:09 +00:00
this.noteToggle();
this.draggable();
this.tableSorter();
if (this.check.length !== 0) {
this.checks();
} else {
$('.save_sortable_collage').click(sortableTable.save);
2012-07-11 08:00:16 +00:00
}
2012-10-27 08:00:09 +00:00
},
2014-03-16 08:00:50 +00:00
draggable: function () {
2012-10-27 08:00:09 +00:00
this.container.sortable({
items: '.drag',
axis: 'y',
containment: '.thin',
forcePlaceholderSize: true,
helper: sortableTable.widthFix,
stop: sortableTable.postSort
});
},
2014-03-16 08:00:50 +00:00
tableSorter: function () {
2012-10-27 08:00:09 +00:00
this.container.tablesorter({
2014-03-16 08:00:50 +00:00
cssHeader: 'headerSort',
cssDesc: 'headerSortUp',
cssAsc: 'headerSortDown',
textExtraction: sortableTable.extractor
2012-10-27 08:00:09 +00:00
}).on('sortEnd', sortableTable.postSort);
},
2014-03-16 08:00:50 +00:00
extractor: function (node) {
2012-10-27 08:00:09 +00:00
return node.textContent || node.innerText;
},
2014-03-16 08:00:50 +00:00
postSort: function () {
2012-10-27 08:00:09 +00:00
sortableTable.color();
sortableTable.counter();
},
2014-03-16 08:00:50 +00:00
noteToggle: function () {
2013-11-05 08:01:12 +00:00
var span = $('<a href="#" class="brackets tooltip" title="Toggle note">Hide</a>').click(function (e) {
2012-10-27 08:00:09 +00:00
e.preventDefault();
$('#drag_drop_textnote > :first-child').toggle();
2012-10-30 08:00:18 +00:00
var $this = $(this);
2013-08-28 23:08:41 +00:00
$this.text($this.text() === 'Hide' ? 'Show' : 'Hide');
2012-10-27 08:00:09 +00:00
});
$('#sorting_head').append(' ', span);
},
2014-03-16 08:00:50 +00:00
checks: function () {
2012-10-27 08:00:09 +00:00
this.check.on('click', 'input', function () {
var s = this.checked ?
2014-03-16 08:00:50 +00:00
'td.center :checkbox:not(:checked)' :
'td.center :checked';
2012-10-27 08:00:09 +00:00
$(s).click();
}).find('span').html('<input type="checkbox" />');
2014-03-16 08:00:50 +00:00
this.container.on('click', 'td > :checkbox', function () {
2012-10-27 08:00:09 +00:00
$(this).parents('tr').toggleClass('row_checked');
}).on('dblclick', 'tr.drag', function () {
$(this).find(':checkbox').click();
});
}
};
sortableTable.init();
2013-06-27 08:01:06 +00:00
});