list 순서변경 예제 코드
splice 는 자기 자신을 update 하면서 제거된 item 를 리턴하는 함수다. (mutable)
이 점을 활용하여 list 순서를 바꾸면 좀 더 간편한 것 같다.
function onItemTop() {
changeOrder(this.list, this.selectedIndx, 0);
}
function onItemUp() {
changeOrder(this.list, this.selectedIndx, this.selectedIndx - 1);
}
function onItemDown() {
changeOrder(this.list, this.selectedIndx, this.selectedIndx + 1);
}
function onItemBottom() {
changeOrder(this.list, this.selectedIndx, this.list.length - 1);
}
function changeOrder(list, fromIndex, toIndex) {
var minIndx = 0;
var maxIndx = list.length - 1;
if ( maxIndx < toIndex ) { return; }
if ( toIndex < minIndx ) { return; }
list.splice(toIndex, 0, list.splice(fromIndex, 1)[0]);
this.selectedIndx = toIndex;
}