|
369 | 369 | * |
370 | 370 | */ |
371 | 371 | module.directive('uiGridCell', |
372 | | - ['$compile', 'uiGridConstants', 'uiGridEditConstants', 'gridUtil', '$parse', 'uiGridEditService', |
373 | | - function ($compile, uiGridConstants, uiGridEditConstants, gridUtil, $parse, uiGridEditService) { |
| 372 | + ['$compile', '$injector', 'uiGridConstants', 'uiGridEditConstants', 'gridUtil', '$parse', 'uiGridEditService', |
| 373 | + function ($compile, $injector, uiGridConstants, uiGridEditConstants, gridUtil, $parse, uiGridEditService) { |
374 | 374 | return { |
375 | 375 | priority: -100, // run after default uiGridCell directive |
376 | 376 | restrict: 'A', |
377 | 377 | scope: false, |
378 | | - link: function ($scope, $elm, $attrs) { |
| 378 | + require: '?^uiGrid', |
| 379 | + link: function ($scope, $elm, $attrs, uiGridCtrl) { |
379 | 380 | if (!$scope.col.colDef.enableCellEdit) { |
380 | 381 | return; |
381 | 382 | } |
|
406 | 407 |
|
407 | 408 | function beginEditFocus(evt) { |
408 | 409 | // gridUtil.logDebug('begin edit'); |
| 410 | + if (uiGridCtrl && uiGridCtrl.cellNav) { |
| 411 | + // NOTE(c0bra): This is causing a loop where focusCell causes beginEditFocus to be called.... |
| 412 | + uiGridCtrl.cellNav.focusCell($scope.row, $scope.col); |
| 413 | + } |
| 414 | + |
409 | 415 | evt.stopPropagation(); |
410 | 416 | beginEdit(); |
411 | 417 | } |
412 | 418 |
|
| 419 | + // If the cellNagv module is installed and we can get the uiGridCellNavConstants value injected, |
| 420 | + // then if the column has enableCellEditOnFocus set to true, we need to listen for cellNav events |
| 421 | + // to this cell and start editing when the "focus" reaches us |
| 422 | + try { |
| 423 | + var uiGridCellNavConstants = $injector.get('uiGridCellNavConstants'); |
| 424 | + |
| 425 | + if ($scope.col.colDef.enableCellEditOnFocus) { |
| 426 | + $scope.$on(uiGridCellNavConstants.CELL_NAV_EVENT, function (evt, rowCol) { |
| 427 | + if (rowCol.row === $scope.row && rowCol.col === $scope.col) { |
| 428 | + beginEdit(); |
| 429 | + } |
| 430 | + else { |
| 431 | + endEdit(); |
| 432 | + } |
| 433 | + }); |
| 434 | + } |
| 435 | + } |
| 436 | + catch (e) {} |
| 437 | + |
413 | 438 | function beginEditKeyDown(evt) { |
414 | 439 | if (uiGridEditService.isStartEditKey(evt)) { |
415 | 440 | beginEdit(); |
|
488 | 513 | * |
489 | 514 | */ |
490 | 515 | function beginEdit() { |
| 516 | + // If we are already editing, then just skip this so we don't try editing twice... |
| 517 | + if (inEdit) { |
| 518 | + return; |
| 519 | + } |
| 520 | + |
491 | 521 | if (!shouldEdit($scope.col, $scope.row)) { |
492 | 522 | return; |
493 | 523 | } |
|
521 | 551 |
|
522 | 552 | var cellElement; |
523 | 553 | $scope.$apply(function () { |
524 | | - inEdit = true; |
525 | | - cancelBeginEditEvents(); |
526 | | - cellElement = $compile(html)($scope.$new()); |
527 | | - var gridCellContentsEl = angular.element($elm.children()[0]); |
528 | | - isFocusedBeforeEdit = gridCellContentsEl.hasClass(':focus'); |
529 | | - gridCellContentsEl.addClass('ui-grid-cell-contents-hidden'); |
530 | | - $elm.append(cellElement); |
531 | | - } |
532 | | - ); |
| 554 | + inEdit = true; |
| 555 | + cancelBeginEditEvents(); |
| 556 | + var cellElement = angular.element(html); |
| 557 | + $elm.append(cellElement); |
| 558 | + $compile(cellElement)($scope.$new()); |
| 559 | + var gridCellContentsEl = angular.element($elm.children()[0]); |
| 560 | + isFocusedBeforeEdit = gridCellContentsEl.hasClass('ui-grid-cell-focus'); |
| 561 | + gridCellContentsEl.addClass('ui-grid-cell-contents-hidden'); |
| 562 | + }); |
533 | 563 |
|
534 | 564 | //stop editing when grid is scrolled |
535 | 565 | var deregOnGridScroll = $scope.$on(uiGridConstants.events.GRID_SCROLL, function () { |
|
564 | 594 | angular.element($elm.children()[1]).remove(); |
565 | 595 | gridCellContentsEl.removeClass('ui-grid-cell-contents-hidden'); |
566 | 596 | if (retainFocus && isFocusedBeforeEdit) { |
567 | | - gridCellContentsEl.focus(); |
| 597 | + gridCellContentsEl[0].focus(); |
568 | 598 | } |
569 | 599 | isFocusedBeforeEdit = false; |
570 | 600 | inEdit = false; |
|
608 | 638 | function (uiGridConstants, uiGridEditConstants) { |
609 | 639 | return { |
610 | 640 | scope: true, |
| 641 | + require: ['?^uiGrid', '?^uiGridRenderContainer'], |
611 | 642 | compile: function () { |
612 | 643 | return { |
613 | 644 | pre: function ($scope, $elm, $attrs) { |
614 | 645 |
|
615 | 646 | }, |
616 | | - post: function ($scope, $elm, $attrs) { |
| 647 | + post: function ($scope, $elm, $attrs, controllers) { |
| 648 | + var uiGridCtrl, renderContainerCtrl; |
| 649 | + if (controllers[0]) { uiGridCtrl = controllers[0]; } |
| 650 | + if (controllers[1]) { renderContainerCtrl = controllers[1]; } |
617 | 651 |
|
618 | 652 | //set focus at start of edit |
619 | 653 | $scope.$on(uiGridEditConstants.events.BEGIN_CELL_EDIT, function () { |
620 | 654 | $elm[0].focus(); |
621 | 655 | $elm[0].select(); |
622 | 656 | $elm.on('blur', function (evt) { |
| 657 | + console.log('stop edit!'); |
623 | 658 | $scope.stopEdit(evt); |
624 | 659 | }); |
625 | 660 | }); |
|
672 | 707 | break; |
673 | 708 | } |
674 | 709 | } |
| 710 | + // Pass the keydown event off to the cellNav service, if it exists |
| 711 | + else if (uiGridCtrl && uiGridCtrl.hasOwnProperty('cellNav') && renderContainerCtrl) { |
| 712 | + evt.uiGridTargetRenderContainerId = renderContainerCtrl.containerId; |
| 713 | + uiGridCtrl.cellNav.handleKeyDown(evt); |
| 714 | + } |
675 | 715 |
|
676 | 716 | return true; |
677 | 717 | }); |
|
0 commit comments