From f9a557217ffd18f8ed0ee29f10d72a9b44ea8d20 Mon Sep 17 00:00:00 2001 From: Robert Plummer Date: Sat, 6 Feb 2016 16:21:29 -0500 Subject: [PATCH] Cells that are scrolled to that do not yet exist, now are created Highlighting (dragging over a range of cells) nearly working --- Sheet/sheet.js | 139 ++++++++++++++++++++++++++++++++----------- index.html | 6 +- jquery.sheet.js | 154 +++++++++++++++++++++++++++++++++++------------- 3 files changed, 223 insertions(+), 76 deletions(-) diff --git a/Sheet/sheet.js b/Sheet/sheet.js index 6a95a3c..cffbd5d 100644 --- a/Sheet/sheet.js +++ b/Sheet/sheet.js @@ -1422,6 +1422,36 @@ $.sheet = { return cell; }, + cellFromTd: function(td) { + if (td.jSCell !== u && td.jSCell !== null) return td.jSCell; + + var loc = jS.locationFromTd(td), + spreadsheet = jS.spreadsheets[jS.i], + row, + rowIndex = spreadsheet.length, + rowMax = loc.row, + colIndex, + colMax = loc.col, + cell; + + while (rowIndex <= rowMax && (spreadsheet[rowIndex] === u)) { + spreadsheet[rowIndex++] = []; + } + + row = spreadsheet[loc.row]; + colIndex = row.length; + colMax = loc.col; + while (colIndex < colMax && (row[colIndex] === u)) { + row[colIndex++] = null; + } + + cell = row[colIndex] = new Sheet.Cell(jS.i, td, jS, jS.cellHandler); + cell.columnIndex = loc.col; + cell.rowIndex = loc.row; + + return cell; + }, + /** * * @param {String} cellId @@ -3570,28 +3600,22 @@ $.sheet = { /** * Detects if an object is a td within a spreadsheet's table - * @param {jQuery|HTMLElement} o target + * @param {HTMLElement} element * @returns {Boolean} * @memberOf jS */ - isCell:function (o) { - if (o && o.jSCell !== u) { - return true; - } - return false; + isCell:function (element) { + return element.nodeName === 'TD'; }, /** * Detects if an object is a bar td within a spreadsheet's table - * @param {HTMLElement} o target + * @param {HTMLElement} element * @returns {Boolean} * @memberOf jS */ - isBar:function (o) { - if (o.tagName == 'TH') { - return true; - } - return false; + isBar:function (element) { + return element.tagName === 'TH'; }, /** @@ -4359,6 +4383,51 @@ $.sheet = { } }, + cycleTableArea: function (fn, row1, col1, row2, col2, groupify) { + var trs = jS.obj.pane().tBody.children, + tr, + td, + tds = [], + cells = [], + _row1, + _col1, + _row2, + _col2; + + if (row1 < row2) { + //added to zero's so as not to increment if this number is important when I ++ later + _row1 = row1 + 0; + _row2 = row2 + 0; + } else { + _row1 = row2 + 0; + _row2 = row1 + 0; + } + + while (_row1 <= _row2 && (tr = trs[_row1]) !== u) { + if (col1 < col2) { + _col1 = col1 + 0; + _col2 = col2 + 0; + } else { + _col1 = col2 + 0; + _col2 = col1 + 0; + } + + while (_col1 <= _col2 && (td = tr.children[_col1]) !== u) { + if (groupify) { + tds.push(td); + cells.push(jS.cellFromTd(td)); + } else { + fn(td, jS.cellFromTd(td)); + } + _col1++; + } + _row1++; + } + + if (groupify) { + fn(tds, cells); + } + }, /** * @type Sheet.Theme */ @@ -4606,10 +4675,10 @@ $.sheet = { if (td === null) return; - var cell = td.jSCell, + var cell = jS.cellFromTd(td), v; - if (cell === u || cell === null) return; + if (cell.uneditable) return; jS.trigger('sheetCellEdit', [cell]); @@ -4655,7 +4724,7 @@ $.sheet = { if (!doNotClearHighlighted) { jS.highlighter - .set(cell.td) //themeroll the cell and bars + .set(cell.td) //highlight the cell and bars .setStart(cell) .setEnd(cell); } @@ -4686,12 +4755,7 @@ $.sheet = { if (isDrag) { var pane = jS.obj.pane(), highlighter = jS.highlighter, - grid = { - startRowIndex: cell.rowIndex, - startColumnIndex: cell.columnIndex, - endRowIndex: 0, - endColumnIndex: 0 - }, + x1, y1, x2, y2, lastTouchedRowIndex = cell.rowIndex, lastTouchedColumnIndex = cell.columnIndex; @@ -4700,18 +4764,15 @@ $.sheet = { var target = e.target || e.srcElement; - if (jS.isBusy()) { + if (jS.isBusy() || !jS.isCell(target)) { return false; } if (target.jSCell === u) return false; - var touchedCell = target.jSCell, + var touchedCell = jS.cellFromTd(target), ok = true; - grid.endColumnIndex = touchedCell.columnIndex; - grid.endRowIndex = touchedCell.rowIndex; - if (directional) { ok = (cell.columnIndex === touchedCell.columnIndex || cell.rowIndex === touchedCell.rowIndex); } @@ -4724,9 +4785,13 @@ $.sheet = { selectModel(target); //highlight the cells - jS.cycleCellArea(function (o) { - highlighter.set(o.td); - }, jS.orderedGrid(grid)); + jS.cycleTableArea(function (tds) { + highlighter.set(tds); + }, + cell.rowIndex, + cell.columnIndex, + touchedCell.rowIndex, + touchedCell.columnIndex, true); } jS.followMe(target); @@ -4734,8 +4799,8 @@ $.sheet = { var mouseY = e.clientY, mouseX = e.clientX, offset = pane.$enclosure.offset(), - up = touchedCell.rowIndex, - left = touchedCell.columnIndex, + up = target.cellIndex, + left = target.parentNode.rowIndex, move = false, previous; @@ -4755,8 +4820,9 @@ $.sheet = { if (up < 1 || left < 1) { return false; } - previous = jS.spreadsheets[jS.i][up][left]; - jS.followMe(previous.td, true); + //table tbody tr td + previous = target.parentNode.parentNode.children[up].children[left]; + jS.followMe(previous, true); } lastTouchedColumnIndex = touchedCell.columnIndex; @@ -5984,6 +6050,13 @@ $.sheet = { return cell.td || null; }, + locationFromTd: function(td) { + var scrolledArea = jS.obj.pane().actionUI.scrolledArea; + return { + row: scrolledArea.row + td.parentNode.rowIndex, + col: scrolledArea.col + td.cellIndex + }; + }, /** * Gets the td row and column index as an object {row, col} * @param {HTMLTableCellElement} td diff --git a/index.html b/index.html index 95da6c2..e1d7177 100644 --- a/index.html +++ b/index.html @@ -216,8 +216,8 @@

Enterprise Ready

- - + + @@ -229,7 +229,7 @@

Enterprise Ready

- + diff --git a/jquery.sheet.js b/jquery.sheet.js index 9026553..d0696c4 100644 --- a/jquery.sheet.js +++ b/jquery.sheet.js @@ -3832,6 +3832,36 @@ $.sheet = { return cell; }, + cellFromTd: function(td) { + if (td.jSCell !== u && td.jSCell !== null) return td.jSCell; + + var loc = jS.locationFromTd(td), + spreadsheet = jS.spreadsheets[jS.i], + row, + rowIndex = spreadsheet.length, + rowMax = loc.row, + colIndex, + colMax = loc.col, + cell; + + while (rowIndex <= rowMax && (spreadsheet[rowIndex] === u)) { + spreadsheet[rowIndex++] = []; + } + + row = spreadsheet[loc.row]; + colIndex = row.length; + colMax = loc.col; + while (colIndex < colMax && (row[colIndex] === u)) { + row[colIndex++] = null; + } + + cell = row[colIndex] = new Sheet.Cell(jS.i, td, jS, jS.cellHandler); + cell.columnIndex = loc.col; + cell.rowIndex = loc.row; + + return cell; + }, + /** * * @param {String} cellId @@ -5980,28 +6010,22 @@ $.sheet = { /** * Detects if an object is a td within a spreadsheet's table - * @param {jQuery|HTMLElement} o target + * @param {HTMLElement} element * @returns {Boolean} * @memberOf jS */ - isCell:function (o) { - if (o && o.jSCell !== u) { - return true; - } - return false; + isCell:function (element) { + return element.nodeName === 'TD'; }, /** * Detects if an object is a bar td within a spreadsheet's table - * @param {HTMLElement} o target + * @param {HTMLElement} element * @returns {Boolean} * @memberOf jS */ - isBar:function (o) { - if (o.tagName == 'TH') { - return true; - } - return false; + isBar:function (element) { + return element.tagName === 'TH'; }, /** @@ -6769,6 +6793,51 @@ $.sheet = { } }, + cycleTableArea: function (fn, row1, col1, row2, col2, groupify) { + var trs = jS.obj.pane().tBody.children, + tr, + td, + tds = [], + cells = [], + _row1, + _col1, + _row2, + _col2; + + if (row1 < row2) { + //added to zero's so as not to increment if this number is important when I ++ later + _row1 = row1 + 0; + _row2 = row2 + 0; + } else { + _row1 = row2 + 0; + _row2 = row1 + 0; + } + + while (_row1 <= _row2 && (tr = trs[_row1]) !== u) { + if (col1 < col2) { + _col1 = col1 + 0; + _col2 = col2 + 0; + } else { + _col1 = col2 + 0; + _col2 = col1 + 0; + } + + while (_col1 <= _col2 && (td = tr.children[_col1]) !== u) { + if (groupify) { + tds.push(td); + cells.push(jS.cellFromTd(td)); + } else { + fn(td, jS.cellFromTd(td)); + } + _col1++; + } + _row1++; + } + + if (groupify) { + fn(tds, cells); + } + }, /** * @type Sheet.Theme */ @@ -7016,10 +7085,10 @@ $.sheet = { if (td === null) return; - var cell = td.jSCell, + var cell = jS.cellFromTd(td), v; - if (cell === u || cell === null) return; + if (cell.uneditable) return; jS.trigger('sheetCellEdit', [cell]); @@ -7065,7 +7134,7 @@ $.sheet = { if (!doNotClearHighlighted) { jS.highlighter - .set(cell.td) //themeroll the cell and bars + .set(cell.td) //highlight the cell and bars .setStart(cell) .setEnd(cell); } @@ -7096,12 +7165,7 @@ $.sheet = { if (isDrag) { var pane = jS.obj.pane(), highlighter = jS.highlighter, - grid = { - startRowIndex: cell.rowIndex, - startColumnIndex: cell.columnIndex, - endRowIndex: 0, - endColumnIndex: 0 - }, + x1, y1, x2, y2, lastTouchedRowIndex = cell.rowIndex, lastTouchedColumnIndex = cell.columnIndex; @@ -7110,18 +7174,15 @@ $.sheet = { var target = e.target || e.srcElement; - if (jS.isBusy()) { + if (jS.isBusy() || !jS.isCell(target)) { return false; } if (target.jSCell === u) return false; - var touchedCell = target.jSCell, + var touchedCell = jS.cellFromTd(target), ok = true; - grid.endColumnIndex = touchedCell.columnIndex; - grid.endRowIndex = touchedCell.rowIndex; - if (directional) { ok = (cell.columnIndex === touchedCell.columnIndex || cell.rowIndex === touchedCell.rowIndex); } @@ -7134,9 +7195,13 @@ $.sheet = { selectModel(target); //highlight the cells - jS.cycleCellArea(function (o) { - highlighter.set(o.td); - }, jS.orderedGrid(grid)); + jS.cycleTableArea(function (tds) { + highlighter.set(tds); + }, + cell.rowIndex, + cell.columnIndex, + touchedCell.rowIndex, + touchedCell.columnIndex, true); } jS.followMe(target); @@ -7144,8 +7209,8 @@ $.sheet = { var mouseY = e.clientY, mouseX = e.clientX, offset = pane.$enclosure.offset(), - up = touchedCell.rowIndex, - left = touchedCell.columnIndex, + up = target.cellIndex, + left = target.parentNode.rowIndex, move = false, previous; @@ -7165,8 +7230,9 @@ $.sheet = { if (up < 1 || left < 1) { return false; } - previous = jS.spreadsheets[jS.i][up][left]; - jS.followMe(previous.td, true); + //table tbody tr td + previous = target.parentNode.parentNode.children[up].children[left]; + jS.followMe(previous, true); } lastTouchedColumnIndex = touchedCell.columnIndex; @@ -8394,6 +8460,13 @@ $.sheet = { return cell.td || null; }, + locationFromTd: function(td) { + var scrolledArea = jS.obj.pane().actionUI.scrolledArea; + return { + row: scrolledArea.row + td.parentNode.rowIndex, + col: scrolledArea.col + td.cellIndex + }; + }, /** * Gets the td row and column index as an object {row, col} * @param {HTMLTableCellElement} td @@ -11007,7 +11080,8 @@ $.printSource = function (s) { HTML.maxStoredDependencies = 100; return HTML; -})(jQuery, document, String);/** +})(jQuery, document, String); +/** * @project jQuery.sheet() The Ajax Spreadsheet - http://code.google.com/p/jquerysheet/ * @author RobertLeePlummerJr@gmail.com * $Id: jquery.sheet.dts.js 933 2013-08-28 12:59:30Z robertleeplummerjr $ @@ -12164,7 +12238,8 @@ $.printSource = function (s) { JSONLoader.maxStoredDependencies = 100; return JSONLoader; -})(jQuery, document, String);/** +})(jQuery, document, String); +/** * @project jQuery.sheet() The Ajax Spreadsheet - http://code.google.com/p/jquerysheet/ * @author RobertLeePlummerJr@gmail.com * $Id: jquery.sheet.dts.js 933 2013-08-28 12:59:30Z robertleeplummerjr $ @@ -12616,6 +12691,7 @@ $.printSource = function (s) { return Constructor; })(jQuery, document); + /** * Creates the scrolling system used by each spreadsheet */ @@ -14954,8 +15030,7 @@ if (!Array.prototype.indexOf) { } return Sheet; -})(jQuery, document, window, Date, String, Number, Boolean, Math, RegExp, Error); -/* parser generated by jison 0.4.15 */ +})(jQuery, document, window, Date, String, Number, Boolean, Math, RegExp, Error);/* parser generated by jison 0.4.15 */ /* Returns a Parser object of the following structure: @@ -16475,8 +16550,7 @@ exports.main = function commonjsMain(args) { if (typeof module !== 'undefined' && require.main === module) { exports.main(process.argv.slice(1)); } -} -/* parser generated by jison 0.4.15 */ +}/* parser generated by jison 0.4.15 */ /* Returns a Parser object of the following structure: @@ -17396,4 +17470,4 @@ exports.main = function commonjsMain(args) { if (typeof module !== 'undefined' && require.main === module) { exports.main(process.argv.slice(1)); } -} +} \ No newline at end of file