AjxSelectionManager.js
Summary
No overview generated for 'AjxSelectionManager.js'
AjxSelectionManager = function(anOwner) {
this._owner = anOwner;
};
AjxSelectionManager.SELECT_ONE_CLEAR_OTHERS = 0;
AjxSelectionManager.TOGGLE_ONE_LEAVE_OTHERS = 1;
AjxSelectionManager.SELECT_TO_ANCHOR = 2;
AjxSelectionManager.DESELECT_ALL = 3;
AjxSelectionManager.SELECT_ALL = 4;
AjxSelectionManager.prototype.getItems = function() {
if (this._selectedItems == null) {
this._selectedItems = this._createItemsCollection();
}
return this._selectedItems;
};
AjxSelectionManager.prototype.getLength = function() {
return this.getItems().length;
};
AjxSelectionManager.prototype.getAnchor = function() {
if (this._anchor == null) {
var items = this.getItems();
if (items.length > 0) {
this._anchor = items[0];
}
}
return this._anchor;
};
AjxSelectionManager.prototype.getCursor = function() {
if (this._cursor == null) {
this._cursor = this.getAnchor();
}
return this._cursor;
};
AjxSelectionManager.prototype.isSelected = function(item) {
return this.getItems().binarySearch(item) != -1;
};
AjxSelectionManager.prototype.selectOneItem = function(item) {
this.select(item, AjxSelectionManager.SELECT_ONE_CLEAR_OTHERS);
};
AjxSelectionManager.prototype.toggleItem = function(item) {
this.select(item, AjxSelectionManager.TOGGLE_ONE_LEAVE_OTHERS);
};
AjxSelectionManager.prototype.selectFromAnchorToItem = function(item) {
this.select(item, AjxSelectionManager.SELECT_TO_ANCHOR);
};
AjxSelectionManager.prototype.deselectAll = function() {
this.select(null, AjxSelectionManager.DESELECT_ALL);
};
AjxSelectionManager.prototype.selectAll = function() {
this.select(null, AjxSelectionManager.SELECT_ALL);
};
AjxSelectionManager.prototype.select = function(item, action) {
this._setAnchorAndCursor(item, action);
var oldItems = this._selectedItems;
var oldItemsCount = (oldItems == null) ? 0 : oldItems.length;
this._selectedItems = null;
this._selectedItems = this._createItemsCollection();
var itemCount = this._owner.getItemCount();
var needsSort = false;
var selectionChanged = false;
var selecting = false;
for (var i = 0; i < itemCount; ++i) {
var testItem = this._owner.getItem(i);
var oldSelectionExists = this._isItemOldSelection(testItem, oldItems);
var newSelectionExists = oldSelectionExists;
switch (action) {
case AjxSelectionManager.SELECT_TO_ANCHOR:
if (this._anchor == null) {
this._anchor = testItem;
}
var atEdge = (testItem == this._anchor || testItem == item);
var changed = false;
if (!selecting && atEdge) {
selecting = true;
changed = true;
}
newSelectionExists = selecting;
if ((!changed || this._anchor == item)
&& selecting && atEdge) {
selecting = false;
}
break;
case AjxSelectionManager.SELECT_ONE_CLEAR_OTHERS:
newSelectionExists = (testItem == item);
break;
case AjxSelectionManager.TOGGLE_ONE_LEAVE_OTHERS:
if (testItem == item) {
newSelectionExists = !oldSelectionExists ;
}
break;
case AjxSelectionManager.DESELECT_ALL:
newSelectionExists = false;
break;
case AjxSelectionManager.SELECT_ALL:
newSelectionExists = true;
break;
}
if (newSelectionExists) {
this._selectedItems.add(testItem);
needsSort = (this._selectedItems.length > 1);
}
if ( newSelectionExists != oldSelectionExists) {
if (this._owner.itemSelectionChanged != null) {
this._owner.itemSelectionChanged(testItem,
i, newSelectionExists);
}
selectionChanged = true;
}
}
selectionChanged = selectionChanged || (oldItemsCount !=
this._selectedItems.length);
if (needsSort) this._selectedItems.sort();
if (selectionChanged && this._owner.selectionChanged != null) {
this._owner.selectionChanged(item);
}
};
AjxSelectionManager.prototype.removeItem = function(item) {
if (this._selectedItems) {
var index = this._selectedItems.binarySearch(item);
if (index > -1) this._selectedItems.removeAt(index);
}
};
AjxSelectionManager.prototype._createItemsCollection = function() {
return new AjxVector();
};
AjxSelectionManager.prototype._isItemOldSelection = function (testItem, oldItems) {
var ret = false;
if (oldItems) {
var oldSelectionIndex = oldItems.binarySearch(testItem);
if (oldSelectionIndex > -1) {
oldItems.removeAt(oldSelectionIndex);
}
ret = (oldSelectionIndex != -1);
}
return ret;
};
AjxSelectionManager.prototype._setAnchorAndCursor = function (item, action) {
switch (action) {
case AjxSelectionManager.SELECT_TO_ANCHOR:
this._cursor = item;
break;
case AjxSelectionManager.SELECT_ONE_CLEAR_OTHERS:
this._anchor = item;
this._cursor = item;
break;
case AjxSelectionManager.TOGGLE_ONE_LEAVE_OTHERS:
this._anchor = item;
this._cursor = item;
break;
case AjxSelectionManager.DESELECT_ALL:
this._anchor = null;
this._cursor = null;
break;
case AjxSelectionManager.SELECT_ALL:
return;
}
};
Documentation generated by
JSDoc on Thu Apr 27 13:58:41 2006