diff -r 000000000000 -r b97547f5f1fa web/data/cubicweb.sortable.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/data/cubicweb.sortable.js Wed Nov 05 15:52:50 2008 +0100 @@ -0,0 +1,249 @@ +/* Adapted from MochiKit's example to use custom cubicweb attribute + and a stable sort (merge sort) instead of default's js array sort + +merge sort JS implementation was found here : +http://en.literateprograms.org/Merge_sort_(JavaScript) + + +On page load, the SortableManager: + +- Finds the table by its id (sortable_table). +- Parses its thead for columns with a "mochi:format" attribute. +- Parses the data out of the tbody based upon information given in the + "cubicweb:sorvalue" attribute, and clones the tr elements for later re-use. +- Clones the column header th elements for use as a template when drawing + sort arrow columns. +- Stores away a reference to the tbody, as it will be replaced on each sort. + +On sort request: + +- Sorts the data based on the given key and direction +- Creates a new tbody from the rows in the new ordering +- Replaces the column header th elements with clickable versions, adding an + indicator (↑ or ↓) to the most recently sorted column. + +*/ + +//************** merge sort implementation ***************// +Sortable = {} + +Sortable.msort = function(array, begin, end, cmpfunc) { + var size=end-begin; + if(size<2) return; + + var begin_right=begin+Math.floor(size/2); + + Sortable.msort(array, begin, begin_right, cmpfunc); + Sortable.msort(array, begin_right, end, cmpfunc); + Sortable.merge(array, begin, begin_right, end, cmpfunc); +} + +Sortable.merge_sort = function(array, cmpfunc) { + Sortable.msort(array, 0, array.length, cmpfunc); +} + +Sortable.merge = function(array, begin, begin_right, end, cmpfunc) { + for(;begin array[begin_right] + if(cmpfunc(array[begin], array[begin_right]) == 1) { + var v = array[begin]; + array[begin] = array[begin_right]; + Sortable.insert(array, begin_right, end, v, cmpfunc); + } + } +} + +Array.prototype.swap=function(a, b) { + var tmp = this[a]; + this[a] = this[b]; + this[b] = tmp; +} + + +Sortable.insert = function(array, begin, end, v, cmpfunc) { + // while(begin+1