Skip to content

Commit

Permalink
Merge pull request #2084 from angular-ui/cellnav-and-speed-improvements
Browse files Browse the repository at this point in the history
Cellnav rewrite and speed improvements
  • Loading branch information
c0bra committed Nov 13, 2014
2 parents 2b25f24 + f3a45ef commit f0f168c
Show file tree
Hide file tree
Showing 10 changed files with 365 additions and 69 deletions.
297 changes: 264 additions & 33 deletions src/features/cellnav/js/cellnav.js

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion src/features/cellnav/less/cellNav.less
@@ -1,6 +1,11 @@
@import '../../../less/variables';

.ui-grid-cell-contents:focus {
// .ui-grid-cell-contents:focus {
// outline: 0;
// background-color: @focusedCell;
// }

.ui-grid-cell-focus {
outline: 0;
background-color: @focusedCell;
}
68 changes: 54 additions & 14 deletions src/features/edit/js/gridEdit.js
Expand Up @@ -369,13 +369,14 @@
*
*/
module.directive('uiGridCell',
['$compile', 'uiGridConstants', 'uiGridEditConstants', 'gridUtil', '$parse', 'uiGridEditService',
function ($compile, uiGridConstants, uiGridEditConstants, gridUtil, $parse, uiGridEditService) {
['$compile', '$injector', 'uiGridConstants', 'uiGridEditConstants', 'gridUtil', '$parse', 'uiGridEditService',
function ($compile, $injector, uiGridConstants, uiGridEditConstants, gridUtil, $parse, uiGridEditService) {
return {
priority: -100, // run after default uiGridCell directive
restrict: 'A',
scope: false,
link: function ($scope, $elm, $attrs) {
require: '?^uiGrid',
link: function ($scope, $elm, $attrs, uiGridCtrl) {
if (!$scope.col.colDef.enableCellEdit) {
return;
}
Expand Down Expand Up @@ -406,10 +407,34 @@

function beginEditFocus(evt) {
// gridUtil.logDebug('begin edit');
if (uiGridCtrl && uiGridCtrl.cellNav) {
// NOTE(c0bra): This is causing a loop where focusCell causes beginEditFocus to be called....
uiGridCtrl.cellNav.focusCell($scope.row, $scope.col);
}

evt.stopPropagation();
beginEdit();
}

// If the cellNagv module is installed and we can get the uiGridCellNavConstants value injected,
// then if the column has enableCellEditOnFocus set to true, we need to listen for cellNav events
// to this cell and start editing when the "focus" reaches us
try {
var uiGridCellNavConstants = $injector.get('uiGridCellNavConstants');

if ($scope.col.colDef.enableCellEditOnFocus) {
$scope.$on(uiGridCellNavConstants.CELL_NAV_EVENT, function (evt, rowCol) {
if (rowCol.row === $scope.row && rowCol.col === $scope.col) {
beginEdit();
}
else {
endEdit();
}
});
}
}
catch (e) {}

function beginEditKeyDown(evt) {
if (uiGridEditService.isStartEditKey(evt)) {
beginEdit();
Expand Down Expand Up @@ -488,6 +513,11 @@
*
*/
function beginEdit() {
// If we are already editing, then just skip this so we don't try editing twice...
if (inEdit) {
return;
}

if (!shouldEdit($scope.col, $scope.row)) {
return;
}
Expand Down Expand Up @@ -521,15 +551,15 @@

var cellElement;
$scope.$apply(function () {
inEdit = true;
cancelBeginEditEvents();
cellElement = $compile(html)($scope.$new());
var gridCellContentsEl = angular.element($elm.children()[0]);
isFocusedBeforeEdit = gridCellContentsEl.hasClass(':focus');
gridCellContentsEl.addClass('ui-grid-cell-contents-hidden');
$elm.append(cellElement);
}
);
inEdit = true;
cancelBeginEditEvents();
var cellElement = angular.element(html);
$elm.append(cellElement);
$compile(cellElement)($scope.$new());
var gridCellContentsEl = angular.element($elm.children()[0]);
isFocusedBeforeEdit = gridCellContentsEl.hasClass('ui-grid-cell-focus');
gridCellContentsEl.addClass('ui-grid-cell-contents-hidden');
});

//stop editing when grid is scrolled
var deregOnGridScroll = $scope.$on(uiGridConstants.events.GRID_SCROLL, function () {
Expand Down Expand Up @@ -564,7 +594,7 @@
angular.element($elm.children()[1]).remove();
gridCellContentsEl.removeClass('ui-grid-cell-contents-hidden');
if (retainFocus && isFocusedBeforeEdit) {
gridCellContentsEl.focus();
gridCellContentsEl[0].focus();
}
isFocusedBeforeEdit = false;
inEdit = false;
Expand Down Expand Up @@ -608,18 +638,23 @@
function (uiGridConstants, uiGridEditConstants) {
return {
scope: true,
require: ['?^uiGrid', '?^uiGridRenderContainer'],
compile: function () {
return {
pre: function ($scope, $elm, $attrs) {

},
post: function ($scope, $elm, $attrs) {
post: function ($scope, $elm, $attrs, controllers) {
var uiGridCtrl, renderContainerCtrl;
if (controllers[0]) { uiGridCtrl = controllers[0]; }
if (controllers[1]) { renderContainerCtrl = controllers[1]; }

//set focus at start of edit
$scope.$on(uiGridEditConstants.events.BEGIN_CELL_EDIT, function () {
$elm[0].focus();
$elm[0].select();
$elm.on('blur', function (evt) {
console.log('stop edit!');
$scope.stopEdit(evt);
});
});
Expand Down Expand Up @@ -672,6 +707,11 @@
break;
}
}
// Pass the keydown event off to the cellNav service, if it exists
else if (uiGridCtrl && uiGridCtrl.hasOwnProperty('cellNav') && renderContainerCtrl) {
evt.uiGridTargetRenderContainerId = renderContainerCtrl.containerId;
uiGridCtrl.cellNav.handleKeyDown(evt);
}

return true;
});
Expand Down
18 changes: 10 additions & 8 deletions src/js/core/directives/ui-grid-header-cell.js
Expand Up @@ -213,14 +213,16 @@
var filterDeregisters = [];
angular.forEach($scope.col.filters, function(filter, i) {
filterDeregisters.push($scope.$watch('col.filters[' + i + '].term', function(n, o) {
uiGridCtrl.grid.api.core.raise.filterChanged();
uiGridCtrl.grid.refresh()
.then(function () {
if (uiGridCtrl.prevScrollArgs && uiGridCtrl.prevScrollArgs.y && uiGridCtrl.prevScrollArgs.y.percentage) {
uiGridCtrl.fireScrollingEvent({ y: { percentage: uiGridCtrl.prevScrollArgs.y.percentage } });
}
// uiGridCtrl.fireEvent('force-vertical-scroll');
});
if (n !== o) {
uiGridCtrl.grid.api.core.raise.filterChanged();
uiGridCtrl.grid.refresh()
.then(function () {
if (uiGridCtrl.prevScrollArgs && uiGridCtrl.prevScrollArgs.y && uiGridCtrl.prevScrollArgs.y.percentage) {
uiGridCtrl.fireScrollingEvent({ y: { percentage: uiGridCtrl.prevScrollArgs.y.percentage } });
}
// uiGridCtrl.fireEvent('force-vertical-scroll');
});
}
}));
});
$scope.$on('$destroy', function() {
Expand Down
8 changes: 5 additions & 3 deletions src/js/core/directives/ui-grid-menu.js
Expand Up @@ -123,9 +123,11 @@ function ($compile, $timeout, $window, $document, gridUtil, uiGridConstants) {

// *** Auto hide when click elsewhere ******
var applyHideMenu = function(){
$scope.$apply(function () {
$scope.hideMenu();
});
if ($scope.shown) {
$scope.$apply(function () {
$scope.hideMenu();
});
}
};

if (typeof($scope.autoHide) === 'undefined' || $scope.autoHide === undefined) {
Expand Down
13 changes: 12 additions & 1 deletion src/js/core/factories/Grid.js
Expand Up @@ -1235,6 +1235,7 @@ angular.module('ui.grid')
*/
Grid.prototype.queueRefresh = function queueRefresh() {
var self = this;

if (self.refreshCanceller) {
$timeout.cancel(self.refreshCanceller);
}
Expand Down Expand Up @@ -1598,7 +1599,7 @@ angular.module('ui.grid')
*
*/
Grid.prototype.refresh = function refresh() {
// gridUtil.logDebug('grid refresh');
gridUtil.logDebug('grid refresh');

var self = this;

Expand Down Expand Up @@ -1664,6 +1665,11 @@ angular.module('ui.grid')
if (self.renderContainers.hasOwnProperty(containerId)) {
var container = self.renderContainers[containerId];

// Skip containers that have no canvasWidth set yet
if (container.canvasWidth === null || isNaN(container.canvasWidth)) {
continue;
}

if (container.header) {
containerHeadersToRecalc.push(container);
}
Expand All @@ -1684,6 +1690,11 @@ angular.module('ui.grid')
for (i = 0; i < containerHeadersToRecalc.length; i++) {
container = containerHeadersToRecalc[i];

// Skip containers that have no canvasWidth set yet
if (container.canvasWidth === null || isNaN(container.canvasWidth)) {
continue;
}

if (container.header) {
var oldHeaderHeight = container.headerHeight;
var headerHeight = gridUtil.outerElementHeight(container.header);
Expand Down
11 changes: 6 additions & 5 deletions src/js/core/factories/GridRenderContainer.js
Expand Up @@ -279,7 +279,9 @@ angular.module('ui.grid')
return;
}

scrollTop = this.getCanvasHeight() * scrollPercentage;
if (typeof(scrollTop) === 'undefined' || scrollTop === undefined || scrollTop === null) {
scrollTop = (this.getCanvasHeight() - this.getCanvasWidth()) * scrollPercentage;
}

this.adjustRows(scrollTop, scrollPercentage);

Expand All @@ -294,10 +296,9 @@ angular.module('ui.grid')
return;
}

// scrollLeft = uiGridCtrl.canvas[0].scrollWidth * scrollPercentage;
scrollLeft = this.getCanvasWidth() * scrollPercentage;

//gridUtil.logDebug('scrollPercentage', scrollPercentage);
if (typeof(scrollLeft) === 'undefined' || scrollLeft === undefined || scrollLeft === null) {
scrollLeft = (this.getCanvasWidth() - this.getViewportWidth()) * scrollPercentage;
}

this.adjustColumns(scrollLeft, scrollPercentage);

Expand Down
5 changes: 2 additions & 3 deletions src/js/core/services/ui-grid-util.js
Expand Up @@ -735,12 +735,11 @@ module.service('gridUtil', ['$log', '$window', '$document', '$http', '$templateC
* @description wraps the $log method, allowing us to choose different
* treatment within ui-grid if we so desired. At present we only log
* debug messages if uiGridConstants.LOG_DEBUG_MESSAGES is set to true
* @param {string} logMessage message to be logged to the console
*
*/
logDebug: function( logMessage ){
logDebug: function() {
if ( uiGridConstants.LOG_DEBUG_MESSAGES ){
$log.debug( logMessage );
$log.debug.apply($log, arguments);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/less/body.less
Expand Up @@ -4,6 +4,11 @@
// overflow: hidden;

.border-radius(0, @gridBorderRadius, @gridBorderRadius, 0);

// Prevent an outline from showing if we focus the render container element
&:focus {
outline: none;
}
}

.ui-grid-viewport {
Expand Down
2 changes: 1 addition & 1 deletion src/templates/ui-grid/uiGridViewport.html
@@ -1,6 +1,6 @@
<div class="ui-grid-viewport">
<div class="ui-grid-canvas">
<div ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by row.uid" class="ui-grid-row" ng-style="containerCtrl.rowStyle(rowRenderIndex)">
<div ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index" class="ui-grid-row" ng-style="containerCtrl.rowStyle(rowRenderIndex)">
<div ui-grid-row="row" row-render-index="rowRenderIndex"></div>
</div>
</div>
Expand Down

0 comments on commit f0f168c

Please sign in to comment.