Created
August 14, 2010 18:46
-
-
Save bga/524574 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
@file | |
@author Fyodorov "bga" Alexander <[email protected]> | |
@section LICENSE | |
Copyright (c) 2009-2010, Fyodorov "Bga" Alexander <[email protected]> | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
* Redistributions of source code must retain the above copyright | |
notice, this list of conditions and the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright | |
notice, this list of conditions and the following disclaimer in the | |
documentation and/or other materials provided with the distribution. | |
* The name of the developer may not be used to endorse or promote | |
products derived from this software without specific prior | |
written permission. | |
THIS SOFTWARE IS PROVIDED BY FYODOROV "BGA" ALEXANDER "AS IS" AND ANY EXPRESS OR | |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
IN NO EVENT SHALL FYODOROV "BGA" ALEXANDER BE LIABLE FOR ANY DIRECT, INDIRECT, | |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
@section DESCRIPTION | |
cross browser support functions for DOM.Node | |
*/ | |
$jb.Loader._scope(). | |
//_require("$jb/$jb.nav.js"). | |
_require("$jb/$G.Function.js"). | |
_require("$jb/$G.Array.js"). | |
//_require("$jb/$G.Object.js"). | |
_require("$jb/OOP.js"). | |
_require("$jb/$jb._dom.js"). | |
//_require("$A/$A.Table.js"). | |
_willDeclared("$A/$A.SortableTable.js"). | |
_completed(function($G, $jb, $A){ | |
var _dom = $jb._dom; | |
$A.SortableTable = function(v) | |
{ | |
$jb.DOM.Plugin.call(this, v); | |
//$A.Table.call(this, v); | |
this.contentTBodyEl; | |
this.curSortColomnPos; | |
this.dataManagers = []; | |
this.colomnSortOrders = []; | |
this._onSortButtonClick = this._onSortButtonClick._fBind(this); | |
this._updateAll(); | |
}; | |
$A.SortableTable._staticDeriveFrom($jb.DOM.Plugin); | |
//$A.SortableTable._staticDeriveFrom($A.Table); | |
$A.SortableTable.DataCmp = {}; | |
$A.SortableTable.DataCmp._number = function(a, b) | |
{ | |
return b - a; | |
}; | |
$A.SortableTable.DataCmp._string = function(a, b) | |
{ | |
return 2*(b > a) - 1; | |
}; | |
$A.SortableTable.DataCmp._date = function(a, b) | |
{ | |
return b - a; | |
}; | |
$A.SortableTable.prototype._updateAll = function() | |
{ | |
//this.contentTBodyEl = this._find(this._attr('tbody-content-class'))[0]; | |
this.contentTBodyEl = this[0].tBodies[0]; | |
this._updateDataManagers(); | |
this.headerRowsCount = this[0].tHead.rows.length; | |
//console.log('headerRowsCount', this.headerRowsCount); | |
this._find(this._attr('sort-button-class'))._reattachEvent('click', this._onSortButtonClick); | |
/* | |
var i = -1, len = this.dataManagers.length; | |
while(++i < len && this.dataManagers[i] == null) | |
; | |
if(i == len) | |
console.error('if you use $A.SortableTable you must provide at least one sortable colomn'); | |
this._sortByColomn(i, 1); | |
*/ | |
}; | |
$A.SortableTable.prototype._updateDataManagers = function() | |
{ | |
var sortableColomnClass = this._attr('sortable-colomn-class').slice(1); | |
var vs = this._find('> thead > tr > th'); | |
var dataManagers = this.dataManagers; | |
var i = vs.length; while(i--) | |
{ | |
var v = _dom(vs[i]); | |
if(v._hasClass(sortableColomnClass)) | |
dataManagers[vs[i].cellIndex] = {_getData: eval(v._attr('data-getter')), _cmp: eval(v._attr('data-cmp'))}; | |
} | |
//console.log(dataManagers); | |
}; | |
$A.SortableTable.prototype._onSortButtonClick = function(e) | |
{ | |
var v = _dom(e.target); | |
var a = e.target; | |
while(a && a.nodeName != 'TH') | |
a = a.parentNode; | |
var pos = a.cellIndex; | |
if(pos == this.curSortColomnPos) | |
{ | |
this._sortByColomn(pos, -this.colomnSortOrders[pos]); | |
} | |
else | |
{ | |
this._sortByColomn(pos, this.colomnSortOrders[pos] || 1); | |
} | |
}; | |
$A.SortableTable.prototype._sortByColomn = function(pos, order) | |
{ | |
//console.log('sort by col pos = ', pos, ' order = ', order); | |
if(pos >= this[0].rows[0].length) | |
return; | |
if(pos == this.curSortColomnPos) | |
{ | |
if(this.colomnSortOrders[pos] == order) | |
return; | |
this.__reverseRows(); | |
this.lastCollectedData_ = this.lastCollectedData_.reverse(); | |
this.colomnSortOrders[pos] = order; | |
} | |
else | |
{ | |
if(this.curSortColomnPos != null) | |
{ | |
_dom(this[0].tHead.rows[0].cells[this.curSortColomnPos]). | |
_removeClass(this._attr('sort-order-down-class')). | |
_removeClass(this._attr('sort-order-up-class')); | |
} | |
this.curSortColomnPos = pos; | |
this.colomnSortOrders[pos] = order; | |
this._resortSortableTable(); | |
} | |
var th = _dom(this[0].tHead.rows[0].cells[pos]); | |
//console.log('order = ', order); | |
if(order == 1) | |
th._removeClass(this._attr('sort-order-down-class'))._addClass(this._attr('sort-order-up-class')); | |
else | |
th._removeClass(this._attr('sort-order-up-class'))._addClass(this._attr('sort-order-down-class')); | |
}; | |
$A.SortableTable.prototype._resortSortableTable = function() | |
{ | |
var pos = this.curSortColomnPos, order = this.colomnSortOrders[pos]; | |
var data = this.__collectData(pos); | |
var len = data.length, indexes = new Array(len), i; | |
if(len > 0) | |
{ | |
//console.log('data = ', data); | |
i = -1; while(++i < len) | |
indexes[i] = i; | |
var _dataCmp = this.dataManagers[pos]._cmp; | |
var _cmp; | |
if(order === -1) | |
{ | |
_cmp = function(a, b) | |
{ | |
return _dataCmp(data[a], data[b]); | |
}; | |
} | |
else | |
{ | |
_cmp = function(a, b) | |
{ | |
return _dataCmp(data[b], data[a]); | |
}; | |
} | |
indexes.sort(_cmp); | |
//console.log('indexes = ', indexes); | |
this.__moveRows(indexes); | |
data = this.__sortData(data, indexes); | |
} | |
//console.log('data = ', data); | |
this.lastCollectedData_ = data; | |
}; | |
$A.SortableTable.prototype._cellContentUpdated = function(cell) | |
{ | |
if(cell.cellIndex != this.curSortColomnPos) | |
return; | |
this._rowContentDataUpdated(cell.parentNode); | |
}; | |
$A.SortableTable.prototype._rowContentUpdated = function(row) | |
{ | |
var pos = this.curSortColomnPos, dataManager = this.dataManagers[pos]; | |
var d; | |
var rowIndex = row.rowIndex - this.headerRowsCount; | |
var lastCollectedData = this.lastCollectedData_; | |
var len = lastCollectedData.length; | |
if((d = dataManager._getData(row.cells[pos])) === this.lastCollectedData_[rowIndex]) | |
return; | |
lastCollectedData[rowIndex] = d; | |
if( | |
(rowIndex > 0 && lastCollectedData[rowIndex - 1] < lastCollectedData[rowIndex]) && | |
(rowIndex < len - 1 && lastCollectedData[rowIndex] < lastCollectedData[rowIndex + 1]) | |
) | |
return; | |
if(rowIndex != len - 1) | |
{ | |
var tmp = lastCollectedData[rowIndex]; | |
lastCollectedData[rowIndex] = lastCollectedData[len - 1]; | |
lastCollectedData[len - 1] = tmp; | |
} | |
var _dataCmp = dataManager._cmp, _cmp; | |
if(this.colomnSortOrders[pos] === 1) | |
{ | |
_cmp = _dataCmp; | |
} | |
else | |
{ | |
_cmp = function(a, b){ return _dataCmp(b, a); }; | |
} | |
var i = lastCollectedData._bIndexOf(d, _cmp, 0, len - 2); | |
//console.log('insert before ' + lastCollectedData); | |
//console.log(d); | |
//console.log(i); | |
if(i < 0) | |
{ | |
lastCollectedData.splice(0, 0, lastCollectedData[len - 1]); | |
--lastCollectedData.length; | |
this.contentTBodyEl.insertBefore(row, this.contentTBodyEl.firstChild); | |
} | |
else if(i >= len) | |
{ | |
this.contentTBodyEl.appendChild(row); | |
} | |
else | |
{ | |
// ??? | |
--i; | |
while(++i < len && _cmp(lastCollectedData[i], d) > 0) | |
; | |
lastCollectedData.splice(i, 0, lastCollectedData[len - 1]); | |
--lastCollectedData.length; | |
this.contentTBodyEl.insertBefore(row, this.contentTBodyEl.rows[i]); | |
} | |
//console.log('insert after ' + lastCollectedData); | |
}; | |
$A.SortableTable.prototype._tableContentUpdated = function() | |
{ | |
this._resortSortableTable(); | |
}; | |
$A.SortableTable.prototype._insertRow = function(row) | |
{ | |
this.lastCollectedData_.push(null); | |
this.contentTBodyEl.appendChild(row); | |
this._rowContentUpdated(row); | |
}; | |
$A.SortableTable.prototype._removeRow = function(row) | |
{ | |
//console.log('_removeRow data = ', '' + this.lastCollectedData_); | |
this.lastCollectedData_.splice(row.rowIndex - this.headerRowsCount, 1); | |
//console.log('_removeRow data = ', '' + this.lastCollectedData_); | |
this.contentTBodyEl.removeChild(row); | |
}; | |
// o-'\o (bicycle) yeah! %) | |
if( | |
(function() | |
{ | |
var a; | |
try | |
{ | |
a = Array.prototype.slice.call($de.childNodes, 0); | |
} | |
catch(err) | |
{ | |
a = null; | |
} | |
return a != null; | |
//return false; | |
}) | |
) | |
{ | |
(function(_slice) | |
{ | |
$A.SortableTable.prototype.__dumpNodes = function(cn, b, e) | |
{ | |
return _slice.call(cn, b, e); | |
}; | |
})(Array.prototype.slice); | |
} | |
else | |
{ | |
$A.SortableTable.prototype.__dumpNodes = function(cn, b, e) | |
{ | |
if(b >= e) | |
return []; | |
if(e == null) | |
e = cn.length; | |
if(b == null) | |
b = 0; | |
var i = e, a = new Array(e - b); | |
if(b > 0) | |
{ | |
while(--i >= b) | |
a[i] = cn[i]; | |
} | |
else | |
{ | |
while(i--) | |
a[i] = cn[i]; | |
} | |
return a; | |
}; | |
} | |
$A.SortableTable.prototype.__reverseRows = function() | |
{ | |
var p = this.contentTBodyEl, cn = p.rows, len = cn.length, i; | |
var trs = this.__dumpNodes(cn, 0 , len - 1); | |
//console.log('reverse', trs); | |
var df = $d.createDocumentFragment(); | |
i = len - 1; while(i--) | |
{ | |
df.appendChild(trs[i]); | |
} | |
p.appendChild(df); | |
}; | |
$A.SortableTable.prototype.__moveRows = function(indexes) | |
{ | |
var p = this.contentTBodyEl, cn = p.rows; | |
var trs = this.__dumpNodes(cn, 0, cn.length); | |
//console.log('move', trs); | |
//console.log('indexes', indexes); | |
var df = $d.createDocumentFragment(); | |
if(this.colomnSortOrders[this.curSortColomnPos] === 1) | |
{ | |
var i = -1, len = indexes.length; while(++i < len) | |
{ | |
df.appendChild(trs[indexes[i]]); | |
} | |
} | |
else | |
{ | |
var i = indexes.length; while(i--) | |
{ | |
df.appendChild(trs[indexes[i]]); | |
} | |
} | |
p.appendChild(df); | |
}; | |
$A.SortableTable.prototype.__collectData = function(pos) | |
{ | |
var dataManager = this.dataManagers[pos]; | |
var _getData = dataManager._getData; | |
var contentTBodyEl = this.contentTBodyEl; | |
var rows = contentTBodyEl.rows; | |
var len = rows.length; | |
var data = new Array(len); | |
var i = -1; while(++i < len) | |
{ | |
data[i] = _getData(rows[i].cells[pos]); | |
} | |
return data; | |
}; | |
$A.SortableTable.prototype.__sortData = function(data, indexes) | |
{ | |
var newData = new Array(data.length); | |
var i = data.length; while(i--) | |
{ | |
newData[i] = data[indexes[i]]; | |
} | |
return newData; | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment