swaptable.js
Summary
No overview generated for 'swaptable.js'
SwapTable = {
getFalse: function() {
return false;
},
findParentElement: function(current_element, selector) {
while (current_element !== null && ! $(current_element).match(selector)) {
current_element = current_element.parentNode;
}
return current_element;
},
getSwapRoot: function(current_element) {
return SwapTable.findParentElement(current_element, 'div.swap_table');
},
getElementText: function(e) {
var elementText = null;
if (e.nodeValue !== null) {
elementText = e.nodeValue;
} else if (e.value !== undefined) {
elementText = e.value
} else {
var children = e.childNodes;
for (var i = 0; i < children.length && elementText == null; ++i) {
elementText = SwapTable.getElementText(children[i]);
}
}
return elementText;
},
getTextBySelector: function(base_element, selector) {
var elements = $(base_element).getElementsBySelector(selector);
var text = null
for (var i = 0; i < elements.length && text === null; ++i) {
text = SwapTable.getElementText(elements[i]);
}
return text;
},
wrapInArray: function (arg) {
if (arg === undefined || arg == null) {
return [];
} else if (arg instanceof Array) {
return arg;
} else {
return [arg];
}
},
selectOnClick: function(e) {
var swap_root = SwapTable.getSwapRoot(this);
var clicked_row = $(SwapTable.findParentElement(this, 'tr'));
if (Myva.is_IE) {
e = window.event;
}
for (var i = 0; i < swap_root.swapOptions.beforeClick.length; ++i) {
if (! swap_root.swapOptions.beforeClick[i](this, e)) {
return;
}
}
if (e.ctrlKey) {
if (clicked_row.hasClassName('selected')) {
--swap_root.selectedCount;
} else {
++swap_root.selectedCount;
}
clicked_row.toggleClassName('selected');
} else if (e.shiftKey) {
if (! clicked_row.hasClassName('selected')) {
clicked_row.addClassName('selected');
++swap_root.selectedCount;
}
if (clicked_row != swap_root.selectedFirst) {
var all_rows = $(SwapTable.findParentElement(this, 'tbody')).getElementsBySelector('tr');
for (var i = 0, found = -1; found < 1 && i < all_rows.length; ++i) {
if (all_rows[i] == swap_root.selectedFirst
|| all_rows[i] == SwapTable.findParentElement(this, 'tr')) {
++found;
} else if (found == 0 && ! all_rows[i].hasClassName('selected')) {
$(all_rows[i]).addClassName('selected');
++swap_root.selectedCount;
}
}
}
} else {
var selected_rows = $(SwapTable.findParentElement(this, 'tbody')).getElementsBySelector('tr.selected');
for (var i = 0; i < selected_rows.length; ++i) {
selected_rows[i].removeClassName('selected');
}
$(SwapTable.findParentElement(this, 'tr')).addClassName('selected');
swap_root.selectedCount = 1;
swap_root.selectedFirst = SwapTable.findParentElement(this, 'tr');
}
swap_root.updateCounts(swap_root.selectedCount, swap_root.selectedCountElements, swap_root.swapOptions.selectedCountElements);
for (var i = 0; i < swap_root.swapOptions.afterClick.length; ++i) {
if (! swap_root.swapOptions.afterClick[i](this, e)) {
return;
}
}
},
swapOnDoubleClick: function(e) {
var row = $(SwapTable.findParentElement(this, 'tr'));
SwapTable.getSwapRoot(row).swapRow([row]);
},
updateCounts: function(count) {
for (var i = 1; i < arguments.length; ++i) {
for (var j = 0; j < arguments[i].length; ++j) {
$(arguments[i][j]).update('' + count);
}
}
return i;
},
setCounts: function() {
this.totalCount = this.getElementsBySelector('tr').length;
this.updateCounts(this.totalCount, this.totalCountElements, this.swapOptions.totalCountElements);
this.selectedCount = this.getElementsBySelector('tr.selected').length;
this.updateCounts(this.selectedCount, this.selectedCountElements, this.swapOptions.selectedCountElements);
},
clickRow: function(row) {
target_cells = $(row).getElementsBySelector('td');
if (target_cells.length > 0) {
if (Myva.is_IE) {
target_cells[0].click();
} else {
click_event = target_cells[0].ownerDocument.createEvent('MouseEvents');
click_event.initMouseEvent('click', true, true, target_cells[0].ownerDocument.defaultView,
1, 0, 0, 0, 0, false, false, false, false, 0, null);
target_cells[0].dispatchEvent(click_event);
}
}
},
swapRow: function(row) {
if (! this.swapTarget) {
return;
}
row = SwapTable.wrapInArray(row);
for (var i = 0; i < this.swapOptions.beforeSwap.length; ++i) {
if (! this.swapOptions.beforeSwap[i](this, row)) {
return;
}
}
var target_rows = this.swapTarget.getElementsBySelector('tr');
var current_target_row_i = 0;
var target_tbody = this.swapTarget.getElementsBySelector('tbody');
if (target_tbody.length > 0) {
target_tbody = target_tbody[0];
} else {
return;
}
for (var row_i = 0; row_i < row.length; ++row_i) {
row[row_i].addClassName('selected');
var source_cells = $(row[row_i]).getElementsBySelector('td');
if (this.swapOptions.visible !== undefined) {
for (var i = 0; i < source_cells.length && i < this.swapOptions.visible.length; ++i) {
if ((this.swapOptions.visible[i] && ! source_cells[i].visible())
|| (! this.swapOptions.visible[i] && source_cells[i].visible())) {
source_cells[i].toggle();
}
}
}
if (current_target_row_i < target_rows.length) {
findSortRow :
for (; current_target_row_i < target_rows.length; ++current_target_row_i) {
target_rows[current_target_row_i].removeClassName('selected');
var target_cells = $(target_rows[current_target_row_i]).getElementsBySelector('td');
findSortCell :
for (var j = 0; j < this.swapOptions.orderBy.length; ++j) {
if (
(SwapTable.getElementText(target_cells[this.swapOptions.orderBy[j]]) > SwapTable.getElementText(source_cells[this.swapOptions.orderBy[j]]))
|| (isBlankString(SwapTable.getElementText(target_cells[this.swapOptions.orderBy[j]])) && ! isBlankString(SwapTable.getElementText(source_cells[this.swapOptions.orderBy[j]])))
) {
target_rows[current_target_row_i].parentNode.insertBefore(row[row_i], target_rows[current_target_row_i]);
break findSortRow;
}
if (
(SwapTable.getElementText(target_cells[this.swapOptions.orderBy[j]]) < SwapTable.getElementText(source_cells[this.swapOptions.orderBy[j]]))
|| (! isBlankString(SwapTable.getElementText(target_cells[this.swapOptions.orderBy[j]])) && isBlankString(SwapTable.getElementText(source_cells[this.swapOptions.orderBy[j]])))
) {
break findSortCell;
}
}
}
}
if (current_target_row_i == target_rows.length) {
target_tbody.appendChild(row[row_i]);
}
}
for (; current_target_row_i < target_rows.length; ++current_target_row_i) {
target_rows[current_target_row_i].removeClassName('selected');
}
if (row.length > 0) {
this.swapTarget.selectedFirst = row[0];
this.totalCount -= row.length;
this.updateCounts(this.totalCount, this.totalCountElements, this.swapOptions.totalCountElements);
this.selectedCount -= row.length;
this.updateCounts(this.selectedCount, this.selectedCountElements, this.swapOptions.selectedCountElements);
this.swapTarget.totalCount += row.length;
this.swapTarget.updateCounts(this.swapTarget.totalCount, this.swapTarget.totalCountElements, this.swapTarget.swapOptions.totalCountElements);
this.swapTarget.selectedCount = row.length;
this.swapTarget.updateCounts(this.swapTarget.selectedCount, this.swapTarget.selectedCountElements, this.swapTarget.swapOptions.selectedCountElements);
}
if (this.selectedCount > 0) {
this.selectedFirst = this.getElementsBySelector('tr.selected')[0];
} else {
var rows_left = this.getElementsBySelector('tr');
if (rows_left.length > 0) {
this.selectedFirst = rows_left[0];
} else {
this.selectedFirst = null;
}
}
for (var i = 0; i < this.swapOptions.afterSwap.length; ++i) {
if (! this.swapOptions.afterSwap[i](this, row)) {
return;
}
}
},
swapSelected: function() {
this.swapRow(this.getElementsBySelector('tr.selected'));
},
create: function(swap_root, swap_target, swap_options) {
swap_root = $(swap_root);
swap_root.swapTarget = $(swap_target);
swap_root.swapOptions = swap_options;
swap_root.clickRow = SwapTable.clickRow
swap_root.setCounts = SwapTable.setCounts;
swap_root.swapRow = SwapTable.swapRow;
swap_root.swapSelected = SwapTable.swapSelected;
swap_root.updateCounts = SwapTable.updateCounts;
if (swap_root.swapOptions.orderBy === undefined) {
swap_root.swapOptions.orderBy = new Array();
swap_root.swapOptions.orderBy[0] = 0;
}
swap_root.swapOptions.beforeClick = SwapTable.wrapInArray(swap_root.swapOptions.beforeClick);
swap_root.swapOptions.afterClick = SwapTable.wrapInArray(swap_root.swapOptions.afterClick);
swap_root.swapOptions.beforeSwap = SwapTable.wrapInArray(swap_root.swapOptions.beforeSwap);
swap_root.swapOptions.afterSwap = SwapTable.wrapInArray(swap_root.swapOptions.afterSwap);
swap_root.swapOptions.totalCountElements = SwapTable.wrapInArray(swap_root.swapOptions.totalCountElements);
for (var i = 0; i < swap_root.swapOptions.totalCountElements.count; ++i) {
swap_root.swapOptions.totalCountElements[i] = $(swap_root.swapOptions.totalCountElements[i]);
}
swap_root.swapOptions.selectedCountElements = SwapTable.wrapInArray(swap_root.swapOptions.selectedCountElements);
for (var i = 0; i < swap_root.swapOptions.selectedCountElements.count; ++i) {
swap_root.swapOptions.selectedCountElements = $(swap_root.swapOptions.selectedCountElements);
}
swap_root.addClassName('swap_table');
var cells = swap_root.getElementsBySelector('td');
for (var i = 0; i < cells.length; ++i) {
if (cells[i].getElementsBySelector('input[type="text"]').length == 0) {
cells[i].onmousedown = SwapTable.getFalse;
cells[i].onselectstart = SwapTable.getFalse;
cells[i].onclick = SwapTable.selectOnClick;
cells[i].ondblclick = SwapTable.swapOnDoubleClick;
}
}
swap_root.totalCountElements = swap_root.getElementsBySelector('.total_count');
swap_root.selectedCountElements = swap_root.getElementsBySelector('.selected_count');
if (cells.length > 0) {
swap_root.selectedFirst = cells[0].parentNode;
} else {
swap_root.selectedFirst = null;
}
swap_root.setCounts();
}
};
Documentation generated by
JSDoc on Fri Aug 24 10:02:55 2007