Subversion Repositories jquery

Compare Revisions

Ignore whitespace Rev 1 → Rev 2

/plugins/jquery.tableHelper.js
0,0 → 1,433
/****************************************************************
* jquery.tableHelper.js *
* *
* by: Dan VerWeire *
* email: [email protected] *
* *
* sponsored by: Pavilion Gift Company *
* www.paviliongift.com *
* *
* This library is free software; you can redistribute *
* it and/or modify it under the terms of the GNU *
* Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the *
* License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will *
* be useful, but WITHOUT ANY WARRANTY; without even the *
* implied warranty of MERCHANTABILITY or FITNESS FOR A *
* PARTICULAR PURPOSE. See the GNU Lesser General Public *
* License for more details. *
* *
* You should have received a copy of the GNU Lesser *
* General Public License along with this library; *
* Inc., 59 Temple Place, Suite 330, Boston, *
* MA 02111-1307 USA *
****************************************************************/
 
(function () {
var alphaColLookup = {}
var colIndex = 0;
 
for (var x = 97; x <= 122; x++) {
alphaColLookup[String.fromCharCode(x)] = colIndex++;
}
 
for (var x = 97; x <= 122; x++) {
for (var y = 97; y <= 122; y++) {
alphaColLookup[String.fromCharCode(x) + String.fromCharCode(y)] = colIndex++;
}
}
 
//helper function to quickly determine if an element is a table
isTable = function (el) {
return (el.nodeName == 'TABLE') ? true : false;
}
//helper function to quickly determine if an element is a tbody
isTbody = function (el) {
return (el.nodeName == 'TBODY') ? true : false;
}
 
//helper function to quickly determine if an element is a tfoot
isTfoot = function (el) {
return (el.nodeName == 'TFOOT') ? true : false;
}
 
//helper function to quickly determine if an element is a thead
isThead = function (el) {
return (el.nodeName == 'THEAD') ? true : false;
}
 
//helper function to quickly determine if an element is a table cell
isCell = function (el) {
return (el.nodeName == 'TD') ? true : false;
}
//helper function to quickly determine if an element is a table row
isRow = function (el) {
return (el.nodeName == 'TR') ? true : false;
}
 
getTable = function (el,jq) {
var tbl;
 
if (isTable(el) || isThead(el) || isTbody(el) || isTfoot(el)) {
jq = jq.not(el);
tbl = el;
}
else if (isCell(el)) {
tbl = el.parentNode.parentNode;
}
else if (isRow(el)) {
tbl = el.parentNode;
}
 
return { table : tbl, jq : jq }
}
 
getVirtTable = function (tbl) {
//tables which contain cells that have a rowSpan or cellSpan make the indices not friendly for
//any type of coordinate system. What this section does is loop through the actual table, building
//a map of that table where a 'merged cell' has a reference at every coordinate which it takes up.
//i tried other methods, but this is the one that ended up working. not sure if this is the most efficient.
//
//this method helps with including merged cells in an overlapping range
 
var virtTable = {};
if (tbl.rowLength) {
rowLength = tbl.rowLength+1;
}
else {
rowLength = tbl.rows.length
}
if (tbl.cellLength) {
colLength = tbl.cellLength+1;
}
else {
colLength = tbl.rows[0].cells.length;
}
for (var y = 0; y < rowLength; y++) {
var row = tbl.rows[y];
for (var x = 0; x < colLength; x++) {
var cell = row.cells[x];
if (cell) {
//this loop is for setting the references of a cell that might have a rowSpan or cellSpan
for (var a = 0; a < cell.rowSpan; a++) {
for (var b = 0; b < cell.colSpan; b++) {
if (!virtTable[y + a]) virtTable[y + a] = {};
if (!virtTable[y + a][x + b]) {
virtTable[y + a][x + b] = cell;
cell.x = x + b;
cell.y = y + a;
}
else {
var c = 0;
while (virtTable[y + a][x + b + c]) {
c++;
}
virtTable[y + a][x + b + c] = cell;
cell.x = x + b + c;
cell.y = y + a;
}
}
}
}
}
}
 
return virtTable;
}
 
//add the cell method to the jQuery object
//this method adds the cell at row,col in any tables in the current
//selection to the selection
//
//it will remove the table from the selection
jQuery.fn.cell = function (col,row) {
var jq = this;
this.each(function(i,el) {
var obj = getTable(this,jq);
jq = obj.jq;
tbl = obj.table;
var virtTable = getVirtTable(tbl);
if (virtTable && virtTable[row] && virtTable[row][col]) {
var cell = virtTable[row][col];
jq = jq.add(cell);
}
});
return jq;
};
jQuery.fn.row = function (row) {
var jq = this;
jq.each(function (i,el) {
var obj = getTable(this,jq);
jq = obj.jq;
tbl = obj.table;
var tmp = Array();
var rw = tbl.rows.item(row);
if (rw) {
for (var x = 0; x < rw.cells.length; x++) {
var cell = rw.cells.item(x);
tmp.push(cell);
}
jq = jq.add(tmp);
}
});
return jq;
}
 
jQuery.fn.alternate = function(start, interval) {
var jq = this;
 
jq.each(function (i,el) {
var obj = getTable(this,jq);
jq = obj.jq;
tbl = obj.table;
 
var tmp = Array();
for (var x = start; x < tbl.rows.length; x += interval) {
var rw = tbl.rows.item(x);
for (var y = 0; y < rw.cells.length; y++) {
var cell = rw.cells.item(y);
tmp.push(cell);
}
}
jq = jq.add(tmp);
});
 
return jq;
}
 
jQuery.fn.even = function () {
return this.alternate(0,2);
}
 
jQuery.fn.odd = function () {
return this.alternate(1,2);
}
jQuery.fn.all = function () {
return this.alternate(0,1);
}
 
jQuery.fn.col = function (col) {
var jq = this;
jq.each(function (i,el) {
var obj = getTable(this,jq);
jq = obj.jq;
tbl = obj.table;
var tmp = Array();
for (var y = 0; y < tbl.rows.length; y++) {
var cell = tbl.rows.item(y).cells.item(col)
tmp.push(cell);
}
jq = jq.add(tmp);
});
return jq;
}
jQuery.fn.tableEnd = function () {
var jq = this;
while (jq.length > 0 ) {
// jq.each(function (i, el) {
jq = jq.end();
// });
}
jq = jq.end();
return jq;
}
//resize the table, only grows the table at the moment
jQuery.fn.resize = function (cols,rows) {
var jq = this;
//fix off by one issue
cols--;
rows--;
result = this.each(function(i,el) {
this.cellLength = cols;
this.rowLength = rows;
for (var y = this.rows.length; y < rows + 1; y++) {
var rw = this.insertRow(-1);
}
//now for each row, check all the cells
for (var y = 0; y < this.rows.length; y++) {
var rw = this.rows.item(y);
for (var x = rw.cells.length; x < cols + 1; x ++) {
var td = rw.insertCell(-1);
}
}
});
return jq;
}
 
jQuery.fn.range = function (stRange) { //yes i called this stRange for fun reasons.
var jq = this;
 
if (stRange.indexOf(':') >= 0) {
var tokens = stRange.split(':');
var range = []
for (var x = 0; x < tokens.length; x++) {
var token = tokens[x];
var col = alphaColLookup[/[a-zA-Z]{1,2}/.exec(token)];
var row = (/[0-9]{1,}/.exec(token)) - 1;
range.push({row : row, col : col});
}
if (range.length == 2) {
//we have a start coord and a stop coord
jq.each(function(i,el) {
var cells = [];
var obj = getTable(this,jq);
jq = obj.jq;
tbl = obj.table;
var virtTable = getVirtTable(tbl);
 
//now grab the cells in the range from the virtual table.
for (var y = range[0].row ; y <= range[1].row ; y ++) {
for (var x = range[0].col; x <= range[1].col; x++) {
try {
if (virtTable[y][x]) {
cells.push(virtTable[y][x]);
}
}
catch (e) { alert('.range error: ' + e + ': ' + y + ', ' + x) };
}
}
 
//add the cells we collected to the jquery object
jq = jq.add(cells);
});
}
}
return jq;
}
 
jQuery.fn.merge = function (copyContents) {
var jq = this;
var contents = '';
var tl = {}, br = {};
 
var cells = [];
jq.each(function (i, el) {
if (isCell(this)) cells.push(this);
})
//find the top left most cell
if (cells.length > 0) {
var tl = {row : cells[0].y, col : cells[0].x, cell : cells[0]};
var br = {row : cells[0].y, col : cells[0].x, cell : cells[0]};
 
for (var x = 0; x < cells.length; x ++){
var cell = cells[x];
 
var row = cell.y;
var col = cell.x;
if (row < tl.row || col < tl.col ) tl = {row : row, col : col, cell : cell};
if (row > br.row || col > br.col ) br = {row : row, col : col, cell : cell};
}
for (var x = 0; x < cells.length; x ++){
var cell = cells[x];
if (cell != tl.cell) {
if (copyContents) contents += cell.innerHTML;
cell.parentNode.removeChild(cell);
}
}
 
tl.cell.colSpan = br.col - tl.col + 1;
tl.cell.rowSpan = br.row - tl.row + 1;
if (copyContents) tl.cell.innerHTML += contents;
}
//remove all the td's and then just add back in the topleft cell
jq = jq.not('td').add(tl.cell);
return jq;
}
 
jQuery.fn.unMerge = function () {
var jq = this;
var contents = '';
 
var cells = [];
jq.each(function (i, el) {
var done = false;
 
if (isCell(this)) {
var tbl = getTable(this).table;
var virtTable = getVirtTable(tbl);
//loop through the virtual table to find the top leftest reference to this merged cell.
for ( var y in virtTable ) {
var row = virtTable[y];
for (var x in row) {
var cell = row[x];
if (cell == this) {
rowSpan = this.rowSpan;
colSpan = this.colSpan;
//
//loop through the size of this cell and insert new cells
for ( var a = 0; a < rowSpan; a++ ) {
for ( var b = 0 ; b < colSpan; b++ ) {
//alert(parseInt(x) + colSpan);
//alert((parseInt(y) + a) + ' ' + y + ' ' + a)// + virtTable[String(y + a)][x])
var cell = jQuery('<td />').insertBefore(virtTable[(parseInt(y) + a)][parseInt(x) + colSpan]).get(0)
cells.push(cell);
}
}
$(this).remove();
done = true;
break;
}
}
if (done) break;
}
}
})
return jq.add(cells);
}
 
})();
jquery.tableHelper.js Property changes : Added: svn:executable ## -0,0 +1 ## +* \ No newline at end of property Index: jquery.networkConnection.js =================================================================== --- jquery.networkConnection.js (nonexistent) +++ jquery.networkConnection.js (revision 2) @@ -0,0 +1,81 @@ +;(function($) { + $.NetworkConnection = function (settings) { + $.NetworkConnection = new NetworkConnection(settings); + } + + function NetworkConnection(settings) { + this.pingURL = "hello.txt"; + this.pingTimeout = 2000; + this.pingInterval = this.pingTimeout + 100; + this.connectionStatus = null; + this.timeout = null; + this.lastTextStatus = null; + this.lastErrorThrown = null; + this.testCount = 0; + + $.extend(this, settings); + + this.isConnected = function () { + var self = this; + return (self.connectionStatus == 'connected') ? true : false; + } + + this.run = function () { + var self = this; + + self.doTest(); + + self.timeout = setInterval(function () { + self.doTest(); + }, self.pingInterval) + } + + this.doTest = function () { + var self = this; + var objTest = { + testNumber : self.testCount++, + startTime : (new Date()).getTime() + }; + + self.beforetest && self.beforetest(objTest); + + $.ajax({ + url : self.pingURL, + type : 'GET', + timeout : self.pingTimeout, + dataType : 'text', + cache : false, + success : function (data, textStatus) { + objTest.endTime = (new Date()).getTime(); + objTest.rtt = objTest.endTime - objTest.startTime; + objTest.textStatus = textStatus; + + self.lastTextStatus = textStatus + self.lastErrorThrown = null; + + self.connectionStatus != 'connected' && self.gainedConnection && self.gainedConnection(); + self.connectionStatus = objTest.connectionStatus = 'connected'; + + self.success && self.success(objTest); + self.complete && self.complete(objTest); + }, + error : function (XMLHttpRequest, textStatus, errorThrown) { + objTest.endTime = (new Date()).getTime(); + objTest.rtt = objTest.endTime - objTest.startTime; + objTest.textStatus = textStatus; + objTest.errorThrown = errorThrown; + + self.lastTextStatus = textStatus + self.lastErrorThrown = errorThrown; + + self.connectionStatus != 'disconnected' && self.lostConnection && self.lostConnection(); + self.connectionStatus = objTest.connectionStatus = 'disconnected'; + self.fail && self.fail(objTest); + self.complete && self.complete(objTest); + } + }) + } + + this.run(); + } +})(jQuery);
jquery.networkConnection.js Property changes : Added: svn:executable ## -0,0 +1 ## +* \ No newline at end of property Index: jquery.tableTracking.js =================================================================== --- jquery.tableTracking.js (nonexistent) +++ jquery.tableTracking.js (revision 2) @@ -0,0 +1,279 @@ +;(function($) { + $.extend({ + viewPort : function () { + var scrollLeft = $(document).scrollLeft(); + var scrollTop = $(document).scrollTop(); + + var windowWidth = $(window).width();// - 20; + var windowHeight = $(window).height();// - 20; + + var offsetCenterLeft = windowWidth / 2 + scrollLeft; + var offsetCenterTop = windowHeight / 2 + scrollTop; + + return { + left : scrollLeft, + right : scrollLeft + windowWidth, + top : scrollTop, + bottom : scrollTop + windowHeight, + center : { left : offsetCenterLeft, top : offsetCenterTop } + } + } + }); +})(jQuery); + +;(function($) { + $.fn.extend({ + tableTracking: function(settings) { + var options = { + colCSS : { + backgroundColor : '#eeeeee' + }, + rowCSS : { + backgroundColor : '#dddddd' + }, + cellCSS : { + backgroundColor : '#aaaaaa' + }, + toolTip : { + offsetTop : -5, + offsetLeft : -5, + css : { + border : 'black 2px solid', + position : 'absolute', + /*height : '100px', + width : '100px',*/ + backgroundColor : 'white' + } + }, + delay : 0, + follow : true, + clickLock : true, + forceHorizontalViewPort : true, + forceVirticalViewPort : false + } + + + var lastCell = null; + var lastCellIndex = null; + var lastRowIndex = null; + + $.extend(options, settings); + + return this.each(function() { + var $activeTip = null; + + if (this.tagName == 'TABLE') { + $(this).find('td').each(function () { + //var $cell = $(this); + var timer = null; + if (options.follow) { + this.onmouseover = function (e) { + e = e || event; + var target = e.target || e.srcElement; + + if (options.delay) { + timer = setTimeout(function (){ cellMouseOver(target) }, options.delay) + } + else { + cellMouseOver(target) + } + + } + this.onmouseout = function (e) { + e = e || event; + var target = e.target || e.srcElement; + + if (timer) { + clearTimeout(timer); + timer = null; + } + + cellMouseOut(target); + } + } + else { + this.onclick = function (e) { + e = e || event; + var target = e.target || e.srcElement; + cellClick(target) + } + } + }); + } + + function cellClick(cell) { + if (lastCell) { + cellMouseOut(lastCell); + } + + cellMouseOver(cell); + lastCell = cell; + } + + function rowMouseOver (row) { + for (var x = 0 ; x < row.cells.length; x++) { + swapCSS(row.cells[x], options.rowCSS); + } + } + + function rowMouseOut (row) { + for (var x = 0 ; x < row.cells.length; x++) { + swapCSS(row.cells[x]); + } + } + + function cellMouseOver (cell) { + colMouseOver(cell.parentNode.parentNode, cell.cellIndex); + rowMouseOver(cell.parentNode); + swapCSS(cell, options.cellCSS); + displayTip(cell); + + /*if (cell.cellIndex != lastCellIndex && lastCellIndex != null) { + colMouseOut(cell.parentNode.parentNode, lastCellIndex); + } + + if (cell.parentNode.rowIndex != lastRowIndex && lastRowIndex != null) { + rowMouseOut(cell.parentNode.parentNode.rows[lastRowIndex]); + } + + lastCellIndex = cell.cellIndex; + lastRowIndex = cell.parentNode.rowIndex;*/ + } + + function cellMouseOut (cell) { + colMouseOut(cell.parentNode.parentNode, cell.cellIndex); + rowMouseOut(cell.parentNode); + swapCSS(cell); + hideTip(); + } + + function colMouseOver (table, colIndex) { + for (var x = 0; x < table.rows.length; x++) { + var row = table.rows[x]; + + if (row.cells.length > colIndex) { + swapCSS(row.cells[colIndex], options.colCSS); + } + } + } + + function colMouseOut (table, colIndex) { + var x; + for (x = 0; x < table.rows.length; x++) { + var row = table.rows[x]; + + if (row.cells.length > colIndex) { + swapCSS(row.cells[colIndex]); + } + } + } + + function displayTip (cell) { + if (options.tipContents) { + var $cell = $(cell); + var contents = options.tipContents(cell); + var container = getTipContainer(cell); + container.css ({ + top : $cell.offset().top + $cell.outerHeight() + options.toolTip.offsetTop, + left : $cell.offset().left + $cell.outerWidth() + options.toolTip.offsetLeft + }) + + container.html(''); + + container.append(contents); + container.css({ display : 'inline'})//.dropShadow(); + } + } + + function hideTip () { + if ($activeTip) { + $activeTip.css({ display : 'none'}) + } + } + + function monitorActiveTip() { + var scrolling = false; + + $(window).bind('scrollstart', function () { scrolling = true }); + $(window).bind('scrollstop', function () { scrolling = false }); + + var interval = setInterval(function () { + if (!scrolling && $activeTip.css('display') != "none") { + //alert($activeTip.css('display')); + var outerWidth = $activeTip.outerWidth(); + var outerHeight = $activeTip.outerHeight(); + + var viewPort = $.viewPort(); + var position = $activeTip.position(); + + if (position.left + outerWidth > viewPort.right ) { + $activeTip.css({ + left : viewPort.right - outerWidth + }); + } + else if ( position.left < viewPort.left ) { + $activeTip.css({ + left : viewPort.left + }); + } + + /*if (position.top + outerHeight > viewPort.bottom) { + $activeTip.css({ + top : viewPort.bottom - outerHeight + }); + } + else if ( position.top < viewPort.top) { + $activeTip.css({ + top : viewPort.top + }); + }*/ + } + }, 100); + } + + function getTipContainer(appendTo) { + + if (!$activeTip) { + $activeTip = $('
').css(options.toolTip.css).appendTo(appendTo); + + if (options.forceHorizontalViewPort || options.forceVerticalViewPort) { + monitorActiveTip(); + } + } + + return $activeTip; + } + + function swapCSS(obj, css) { + var $obj = $(obj); + + if (!obj.origCSS) { + obj.origCSS = { }; + } + + if (obj.origCSS && !css) { + for (var at in obj.origCSS) { + var val = (obj.origCSS[at]) ? obj.origCSS[at] : ''; + obj.style[at] = val; + } + + obj.currCSS = null; + obj.origCSS = null; + } + else if (obj.currCSS != css) { + for (var attr in css) { + var currVal = $obj.css(attr) + if (!obj.origCSS[attr]) { + obj.origCSS[attr] = currVal; //(currVal) ? currVal : 'none'; + } + } + + obj.currCSS = css; + $obj.css(css); + } + } + }) + + } + }); +})(jQuery); \ No newline at end of file
jquery.tableTracking.js Property changes : Added: svn:executable ## -0,0 +1 ## +* \ No newline at end of property Index: jquery.collect.js =================================================================== --- jquery.collect.js (nonexistent) +++ jquery.collect.js (revision 2) @@ -0,0 +1,178 @@ +(function($) { +$.fn.collect = function (options) { + var $els = $([]); + var collect = false; + var sum = 0; + + var settings = { + addItem : null, + removeItem : null + } + + $.extend(settings, options) + + return this.each(function () { + $(this).addClass('collectable') + .bind('mousedown', function (e){ + var $el = $(this); + + if (!e.ctrlKey) { + clearItems() + } + + if (e.shiftKey || e.ctrlKey) { + collect = true; + if ($el.hasClass('isCollected')) { + removeItem($el); + } + else { + addItem($el); + } + } + + return false; + }) + .bind('mouseup', function (e) { + collect = false; + + return false; + }) + .bind('mouseover', function (e){ + if (collect && e.shiftKey && $(this).hasClass('collectable')) { + $el = $(this); + if ($el.hasClass('isCollected')) { + removeItem($el); + } + else { + addItem($el); + } + //$(document.body).append('adding.. '); + + } +/* else { + $(document.body).append('hello'); + }*/ + return false; + }); + }) + + function addItem($el) { + //$(document.body).append('addItem
'); + $el.addClass('isCollected'); + + $els = $els.add($el); + + if (settings.addItem) { + settings.addItem.call($el.get(0), $els); + } + } + + function removeItem($el) { + //$(document.body).append('removeItem
'); + $el.removeClass('isCollected'); + + $els = $els.not($el); + + if (settings.removeItem) { + settings.removeItem.call($el.get(0), $els); + } + } + + function clearItems() { + //$(document.body).append('clearItems
'); + $els.removeClass('isCollected'); + $els = $([]); + + if (settings.clearItems) { + settings.clearItems(); + } + } +} + +})(jQuery); + +(function($) { + $.fn.collectAndSum = function (options) { + var settings = { + offsetTop : -3, + offsetLeft : -3 + } + + $.extend(settings, options); + + var $sum = $('
') + .appendTo(document.body) + .addClass('collectableTip') + .toggle() + .click(function () { + $(this).fadeOut('fast'); + }); + + function addRemove($set, $this) { + var el = $this.get(0); + + if ($set.length == 0) { + $sum.fadeOut('fast'); + } + else { + + var offset = $this.offset(); + + var sum = 0; + var numCount = 0; + var min = null; + var max = null; + + $set.each(function () { + var val = parseFloat(this.innerHTML); + if (!isNaN(val)) { + sum += parseFloat(this.innerHTML); + numCount ++; + if (min == null) { + min = val; + } + else if (val < min) { + min = val; + } + + if (max == null) { + max = val; + } + else if (val > max) { + max = val; + } + } + }) + + var html = 'sum=' + formatFloat(sum) + '
'; + html += 'avg=' + formatFloat(sum/numCount) + '
'; + html += 'min=' + formatFloat(min) + '
'; + html += 'max=' + formatFloat(max) + '
'; + html += 'count=' + formatInt(numCount) + '
'; + + $sum.html(html).stop(null,true).animate({ + left : settings.offsetLeft + offset.left + $this.outerWidth(), + top : settings.offsetTop + offset.top + $this.outerHeight() + }) + + if ($sum.css('display') == 'none') { + $sum.fadeIn('slow'); + } + } + } + + return $(this).collect({ + addItem : function ($set) { + //this is the item being added + addRemove($set, $(this)); + }, + removeItem : function ($set) { + //this is the item being removed + addRemove($set,$($set.get($set.length -1))); + }, + clearItems : function () { + $sum.html('').fadeOut('fast'); + } + }); + } +})(jQuery); \ No newline at end of file
jquery.collect.js Property changes : Added: svn:executable ## -0,0 +1 ## +* \ No newline at end of property