Skip to content

Commit

Permalink
bugfix: clear currentRowClassName and currentColumnClassName when ref…
Browse files Browse the repository at this point in the history
…reshing selections
  • Loading branch information
warpech committed Feb 18, 2013
1 parent 3f2b0f6 commit 8df5de9
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 171 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## HEAD

Bugfix:
- clear currentRowClassName and currentColumnClassName when refreshing selections ([#398](https://github.com/warpech/jquery-handsontable/issues/398)) - thanks @jaycfields

## [0.8.4](https://github.com/warpech/jquery-handsontable/tree/v0.8.4) (Feb 17, 2013)

Features:
Expand Down
2 changes: 1 addition & 1 deletion dist/jquery.handsontable.full.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Licensed under the MIT license.
* http://handsontable.com/
*
* Date: Sun Feb 17 2013 21:43:36 GMT+0100 (Central European Standard Time)
* Date: Mon Feb 18 2013 19:34:23 GMT+0100 (Central European Standard Time)
*/

.handsontable {
Expand Down
142 changes: 88 additions & 54 deletions dist/jquery.handsontable.full.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Licensed under the MIT license.
* http://handsontable.com/
*
* Date: Sun Feb 17 2013 21:43:36 GMT+0100 (Central European Standard Time)
* Date: Mon Feb 18 2013 19:34:23 GMT+0100 (Central European Standard Time)
*/
/*jslint white: true, browser: true, plusplus: true, indent: 4, maxerr: 50 */

Expand Down Expand Up @@ -2170,11 +2170,11 @@ Handsontable.TableView = function (instance) {
cellRenderer: function (row, column, TD) {
that.applyCellTypeMethod('renderer', TD, {row: row, col: column}, instance.getDataAtCell(row, column));
},
currentRowClassName: settings.currentRowClassName,
currentColumnClassName: settings.currentColClassName,
selections: {
current: {
className: 'current',
highlightRowClassName: settings.currentRowClassName,
highlightColumnClassName: settings.currentColClassName,
border: {
width: 2,
color: '#5292F7',
Expand All @@ -2186,6 +2186,8 @@ Handsontable.TableView = function (instance) {
},
area: {
className: 'area',
highlightRowClassName: settings.currentRowClassName,
highlightColumnClassName: settings.currentColClassName,
border: {
width: 1,
color: '#89AFF9',
Expand Down Expand Up @@ -4222,14 +4224,14 @@ Handsontable.PluginHooks.push('afterGetColWidth', htManualColumnResize.getColWid
/**
* walkontable 0.1
*
* Date: Wed Jan 23 2013 01:38:29 GMT+0100 (Central European Standard Time)
* Date: Mon Feb 18 2013 19:31:46 GMT+0100 (Central European Standard Time)
*/

function WalkontableBorder(instance, settings) {
//reference to instance
this.instance = instance;
this.settings = settings;
this.wtDom = new WalkontableDom();
this.wtDom = this.instance.wtDom;

this.main = document.createElement("div");
this.main.style.position = 'absolute';
Expand Down Expand Up @@ -4409,11 +4411,11 @@ function Walkontable(settings) {

//bootstrap from settings
this.wtSettings = new WalkontableSettings(this, settings);
this.wtDom = new WalkontableDom(this);
this.wtTable = new WalkontableTable(this);
this.wtScroll = new WalkontableScroll(this);
this.wtWheel = new WalkontableWheel(this);
this.wtEvent = new WalkontableEvent(this);
this.wtDom = new WalkontableDom();

//find original headers
if (this.wtTable.THEAD.childNodes.length && this.wtTable.THEAD.childNodes[0].childNodes.length) {
Expand Down Expand Up @@ -4505,7 +4507,11 @@ Walkontable.prototype.getSetting = function (key, param1, param2, param3) {
Walkontable.prototype.hasSetting = function (key) {
return this.wtSettings.has(key);
};
function WalkontableDom() {
function WalkontableDom(instance) {
if (instance) {
this.instance = instance;
}
this.tdCache = [];
}

//goes up the DOM tree (including given element) until it finds an element that matches the nodeName
Expand All @@ -4529,6 +4535,50 @@ WalkontableDom.prototype.prevSiblings = function (elem) {
return out;
};

WalkontableDom.prototype.tdHasClass = function (trIndex, tdIndex, cls) {
return !!(this.tdCache[trIndex] && this.tdCache[trIndex][tdIndex] && this.tdCache[trIndex][tdIndex][cls]);
};

WalkontableDom.prototype.tdAddClass = function (trIndex, tdIndex, cls) {
if (!this.tdHasClass(trIndex, tdIndex, cls)) {
if (!this.tdCache[trIndex]) {
this.tdCache[trIndex] = [];
}
if (!this.tdCache[trIndex][tdIndex]) {
this.tdCache[trIndex][tdIndex] = {};
this.tdCache[trIndex][tdIndex]._node = this.instance.wtTable.getCell([trIndex + this.instance.getSetting('offsetRow'), tdIndex + this.instance.getSetting('offsetColumn')]);
}
this.tdCache[trIndex][tdIndex]._node.className += " " + cls;
this.tdCache[trIndex][tdIndex][cls] = true;
}
};

WalkontableDom.prototype.tdRemoveClass = function (trIndex, tdIndex, cls) {
if (this.tdHasClass(trIndex, tdIndex, cls)) {
var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
this.tdCache[trIndex][tdIndex]._node.className = this.tdCache[trIndex][tdIndex]._node.className.replace(reg, ' ').replace(/^\s\s*/, '').replace(/\s\s*$/, ''); //last 2 replaces do right trim (see http://blog.stevenlevithan.com/archives/faster-trim-javascript)
this.tdCache[trIndex][tdIndex][cls] = false;
}
};

WalkontableDom.prototype.tdResetCache = function () {
for (var i in this.tdCache) {
if (this.tdCache.hasOwnProperty(i)) {
for (var j in this.tdCache[i]) {
if (this.tdCache[i].hasOwnProperty(j)) {
for (var k in this.tdCache[i][j]) {
if (this.tdCache[i][j].hasOwnProperty(k)) {
if (k !== '_node') {
this.tdCache[i][j][k] = false;
}
}
}
}
}
}
}
};

//http://snipplr.com/view/3561/addclass-removeclass-hasclass/
WalkontableDom.prototype.hasClass = function (ele, cls) {
return ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
Expand Down Expand Up @@ -4668,7 +4718,7 @@ function WalkontableEvent(instance) {
//reference to instance
this.instance = instance;

this.wtDom = new WalkontableDom();
this.wtDom = this.instance.wtDom;

var dblClickOrigin = [null, null, null, null]
, dblClickTimeout = null;
Expand Down Expand Up @@ -5201,25 +5251,10 @@ function WalkontableSelection(instance, settings) {
this.instance = instance;
this.settings = settings;
this.selected = [];
this.wtDom = new WalkontableDom(this.instance);
if (settings.border) {
this.border = new WalkontableBorder(instance, settings);
}
this.onAdd = function (coords) {
var TD = instance.wtTable.getCell(coords);
if (typeof TD === 'object') {
if (settings.className) {
instance.wtDom.addClass(TD, settings.className);
}
}
};
/*this.onRemove = function (coords) {
var TD = instance.wtTable.getCell(coords);
if (typeof TD === 'object') {
if (settings.className) {
instance.wtDom.removeClass(TD, settings.className);
}
}
};*/
}

WalkontableSelection.prototype.add = function (coords) {
Expand Down Expand Up @@ -5289,57 +5324,58 @@ WalkontableSelection.prototype.getCorners = function () {
};

WalkontableSelection.prototype.draw = function (selectionsOnly) {
var TDs, TD, i, ilen, corners, r, c;
var corners, r, c;

ilen = this.selected.length;
if (ilen) {
var offsetRow = this.instance.getSetting('offsetRow')
, lastVisibleRow = offsetRow + this.instance.getSetting('displayRows') - 1
, offsetColumn = this.instance.getSetting('offsetColumn')
, lastVisibleColumn = offsetColumn + this.instance.getSetting('displayColumns') - 1;

if (this.selected.length) {
corners = this.getCorners();
var offsetRow = this.instance.getSetting('offsetRow')
, lastVisibleRow = offsetRow + this.instance.getSetting('displayRows') - 1
, offsetColumn = this.instance.getSetting('offsetColumn')
, lastVisibleColumn = offsetColumn + this.instance.getSetting('displayColumns') - 1
, currentRowClassName = this.instance.getSetting('currentRowClassName')
, currentColumnClassName = this.instance.getSetting('currentColumnClassName');

for (r = offsetRow; r <= lastVisibleRow; r++) {
for (c = offsetColumn; c <= lastVisibleColumn; c++) {
TD = this.instance.wtTable.getCell([r, c]);
if (r >= corners[0] && r <= corners[2] && c >= corners[1] && c <= corners[3]) {
//selected cell
currentRowClassName && this.instance.wtDom.removeClass(TD, currentRowClassName);
currentColumnClassName && this.instance.wtDom.removeClass(TD, currentColumnClassName);
this.onAdd([r, c], TD);
this.settings.highlightRowClassName && this.wtDom.tdRemoveClass(r - offsetRow, c - offsetColumn, this.settings.highlightRowClassName);
this.settings.highlightColumnClassName && this.wtDom.tdRemoveClass(r - offsetRow, c - offsetColumn, this.settings.highlightColumnClassName);
this.settings.className && this.wtDom.tdAddClass(r - offsetRow, c - offsetColumn, this.settings.className);
}
else if (r >= corners[0] && r <= corners[2]) {
//selection is in this row
currentColumnClassName && this.instance.wtDom.removeClass(TD, currentColumnClassName);
currentRowClassName && this.instance.wtDom.addClass(TD, currentRowClassName);
this.instance.wtDom.removeClass(TD, this.settings.className);
this.settings.highlightColumnClassName && this.wtDom.tdRemoveClass(r - offsetRow, c - offsetColumn, this.settings.highlightColumnClassName);
this.settings.highlightRowClassName && this.wtDom.tdAddClass(r - offsetRow, c - offsetColumn, this.settings.highlightRowClassName);
this.settings.className && this.wtDom.tdRemoveClass(r - offsetRow, c - offsetColumn, this.settings.className);
}
else if (c >= corners[1] && c <= corners[3]) {
//selection is in this column
currentRowClassName && this.instance.wtDom.removeClass(TD, currentRowClassName);
currentColumnClassName && this.instance.wtDom.addClass(TD, currentColumnClassName);
this.instance.wtDom.removeClass(TD, this.settings.className);
this.settings.highlightRowClassName && this.wtDom.tdRemoveClass(r - offsetRow, c - offsetColumn, this.settings.highlightRowClassName);
this.settings.highlightColumnClassName && this.wtDom.tdAddClass(r - offsetRow, c - offsetColumn, this.settings.highlightColumnClassName);
this.settings.className && this.wtDom.tdRemoveClass(r - offsetRow, c - offsetColumn, this.settings.className);
}
else {
//no selection
currentRowClassName && this.instance.wtDom.removeClass(TD, currentRowClassName);
currentColumnClassName && this.instance.wtDom.removeClass(TD, currentColumnClassName);
this.instance.wtDom.removeClass(TD, this.settings.className);
this.settings.highlightRowClassName && this.wtDom.tdRemoveClass(r - offsetRow, c - offsetColumn, this.settings.highlightRowClassName);
this.settings.highlightColumnClassName && this.wtDom.tdRemoveClass(r - offsetRow, c - offsetColumn, this.settings.highlightColumnClassName);
this.settings.className && this.wtDom.tdRemoveClass(r - offsetRow, c - offsetColumn, this.settings.className);
}
}
}

this.border && this.border.appear(corners);
}
else {
if (selectionsOnly && this.settings.className) {
TDs = this.instance.wtTable.TABLE.getElementsByTagName('TD');
for (i = 0, ilen = TDs.length; i < ilen; i++) {
this.instance.wtDom.removeClass(TDs[i], this.settings.className);
if (selectionsOnly) {
for (r = 0; r <= lastVisibleRow - offsetRow; r++) {
for (c = 0; c <= lastVisibleColumn - offsetColumn; c++) {
this.settings.highlightRowClassName && this.wtDom.tdRemoveClass(r, c, this.settings.highlightRowClassName);
this.settings.highlightColumnClassName && this.wtDom.tdRemoveClass(r, c, this.settings.highlightColumnClassName);
this.settings.className && this.wtDom.tdRemoveClass(r, c, this.settings.className);
}
}
}

this.border && this.border.disappear();
}
};
Expand Down Expand Up @@ -5621,7 +5657,7 @@ function WalkontableTable(instance) {
//reference to instance
this.instance = instance;
this.TABLE = this.instance.getSetting('table');
this.wtDom = new WalkontableDom();
this.wtDom = this.instance.wtDom;
this.wtDom.removeTextNodes(this.TABLE);

this.hasEmptyCellProblem = ($.browser.msie && (parseInt($.browser.version, 10) <= 7));
Expand Down Expand Up @@ -5869,9 +5905,6 @@ WalkontableTable.prototype.adjustAvailableNodes = function () {
for (c = 0; c < frozenColumnsCount; c++) {
TR.appendChild(document.createElement('TH'));
}
for (c = 0; c < displayTds; c++) {
TR.appendChild(document.createElement('TD'));
}
this.TBODY.appendChild(TR);
this.tbodyChildrenLength++;
}
Expand Down Expand Up @@ -5998,6 +6031,7 @@ WalkontableTable.prototype._doDraw = function () {

var visibilityFullRow = null;
var visibilityFullColumn = null;
this.wtDom.tdResetCache();

for (c = 0; c < displayTds; c++) { //in future use nextSibling; http://jsperf.com/nextsibling-vs-indexed-childnodes
if (this.visibilityEdgeColumn !== null && offsetColumn + c > this.visibilityEdgeColumn) {
Expand Down
2 changes: 1 addition & 1 deletion jquery.handsontable.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Licensed under the MIT license.
* http://handsontable.com/
*
* Date: Sun Feb 17 2013 21:43:36 GMT+0100 (Central European Standard Time)
* Date: Mon Feb 18 2013 19:34:23 GMT+0100 (Central European Standard Time)
*/

.handsontable {
Expand Down
Loading

0 comments on commit 8df5de9

Please sign in to comment.