"use strict";
sortApp.service( "orderHandler" , function() {
/*
@param list - id of element to be sorted declared by "data-table-id" attribute value
@param sortBy - sorting pattern declared by "data-sort-id" attribute value
*/
this.sort = function( list , sortBy ) {
// Get specific table and selector
var table = document.querySelector("[data-table-id="+list+"]"),
current = table.getElementsByClassName("sort")[0],
sort;
// If there was a selection already get its settings
if( current ) {
var currentName = current.getAttribute("data-sort-id"),
currentSortingMethod = current.classList.contains("up");
// Remove selection
current.classList.remove( "sort" , "up" , "down" );
}
switch( sortBy == currentName ) {
case true:
switch( currentSortingMethod ) {
case true:
sort = "-"+currentName;
current.classList.add( "sort" , "down" );
break;
case false:
sort = currentName;
current.classList.add( "sort" , "up" );
break;
}
break;
case false:
sort = sortBy;
table.querySelector("[data-sort-id="+sortBy+"]").classList.add( "sort" , "up" );
break;
}
return sort;
}
});