shopping order sort. maybe needs some fine tuning

This commit is contained in:
ville rantanen
2017-07-04 09:50:54 +03:00
parent 96c35d836d
commit 57abc49f1b
2 changed files with 43 additions and 7 deletions

View File

@@ -1,3 +1,22 @@
Array.prototype.stableSort = function(cmp) {
cmp = !!cmp ? cmp : (a, b) => {
if (a < b) return -1;
if (a > b) return 1;
return 0;
};
let stabilizedThis = this.map((el, index) => [el, index]);
let stableCmp = (a, b) => {
let order = cmp(a[0], b[0]);
if (order != 0) return order;
return a[1] - b[1];
}
stabilizedThis.sort(stableCmp);
for (let i=0; i<this.length; i++) {
this[i] = stabilizedThis[i][0];
}
return this;
}
function insertAtEnd(myField, myValue) {
myField.value += myValue;
}
@@ -76,15 +95,32 @@ window.onclick = function(event) {
}
}
function sortMarkdown() {
function sortMarkdown(boughtOrder=false) {
var divs = document.getElementsByClassName("entry");
divs = Array.prototype.slice.call(divs, 0);
divs.sort(function(a, b) {
return a.children[1].innerHTML.localeCompare(b.children[1].innerHTML);
});
if (boughtOrder) {
divs.stableSort(orderStringSorter);
} else {
divs.stableSort(fullStringSorter);
}
var parent = document.getElementById('entry_loop');
parent.innerHTML = "";
for(var i = 0, l = divs.length; i < l; i++) {
parent.appendChild(divs[i]);
}
}
function fullStringSorter(a,b) {
var aValue=a.children[1].innerHTML;
var bValue=b.children[1].innerHTML;
if (aValue < bValue) return -1;
if (aValue > bValue) return 1;
return 0;
}
function orderStringSorter(a,b) {
var aValue=a.children[1].innerHTML.substring(0,3);
var bValue=b.children[1].innerHTML.substring(0,3);
if ( aValue == "[x]" && bValue == "[ ]" ) return 1;
if ( aValue == "[ ]" && bValue == "[x]" ) return -1;
return 0;
}

View File

@@ -52,9 +52,9 @@
<form action="{{ url_for('sort_flip') }}" method=post>
<input type=hidden name=shopid value="{{ shopid }}">
{% if autosort %}
<input class=submit type=submit name=toggleAll value="Autosort: On">
<input class=submit type=submit name=toggleAll value="Auto order: On">
{% else %}
<input class=submit type=submit name=toggleAll value="Autosort: Off">
<input class=submit type=submit name=toggleAll value="Auto order: Off">
{% endif %}
</form>
</span>
@@ -99,6 +99,6 @@
<dl><input type=hidden name=shopid value={{ shopid }}><input class="submit notify" type=submit value=Delete onclick="return confirm('Do you really want to remove shop {{ shop }}?');">
</dl></form>
{% if autosort %}
<script language=javascript>sortMarkdown();</script>
<script language=javascript>sortMarkdown(boughtOrder=true);</script>
{% endif %}
{% endblock %}