-
Notifications
You must be signed in to change notification settings - Fork 77
/
ui-grid.edit.js
1325 lines (1190 loc) · 52.3 KB
/
ui-grid.edit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* ui-grid - v4.12.7 - 2024-04-12
* http://ui-grid.info/
* Copyright (c) 2024 ; License: MIT
*/
(function () {
'use strict';
/**
* @ngdoc overview
* @name ui.grid.edit
* @description
*
* # ui.grid.edit
*
* <div class="alert alert-success" role="alert"><strong>Stable</strong> This feature is stable. There should no longer be breaking api changes without a deprecation warning.</div>
*
* This module provides cell editing capability to ui.grid. The goal was to emulate keying data in a spreadsheet via
* a keyboard.
* <br/>
* <br/>
* To really get the full spreadsheet-like data entry, the ui.grid.cellNav module should be used. This will allow the
* user to key data and then tab, arrow, or enter to the cells beside or below.
*
* <div doc-module-components="ui.grid.edit"></div>
*/
var module = angular.module('ui.grid.edit', ['ui.grid']);
/**
* @ngdoc object
* @name ui.grid.edit.constant:uiGridEditConstants
*
* @description constants available in edit module
*/
module.constant('uiGridEditConstants', {
EDITABLE_CELL_TEMPLATE: /EDITABLE_CELL_TEMPLATE/g,
// must be lowercase because template bulder converts to lower
EDITABLE_CELL_DIRECTIVE: /editable_cell_directive/g,
events: {
BEGIN_CELL_EDIT: 'uiGridEventBeginCellEdit',
END_CELL_EDIT: 'uiGridEventEndCellEdit',
CANCEL_CELL_EDIT: 'uiGridEventCancelCellEdit'
}
});
/**
* @ngdoc service
* @name ui.grid.edit.service:uiGridEditService
*
* @description Services for editing features
*/
module.service('uiGridEditService', ['$q', 'uiGridConstants', 'gridUtil',
function ($q, uiGridConstants, gridUtil) {
var service = {
initializeGrid: function (grid) {
service.defaultGridOptions(grid.options);
grid.registerColumnBuilder(service.editColumnBuilder);
grid.edit = {};
/**
* @ngdoc object
* @name ui.grid.edit.api:PublicApi
*
* @description Public Api for edit feature
*/
var publicApi = {
events: {
edit: {
/**
* @ngdoc event
* @name afterCellEdit
* @eventOf ui.grid.edit.api:PublicApi
* @description raised when cell editing is complete
* <pre>
* gridApi.edit.on.afterCellEdit(scope,function(rowEntity, colDef) {})
* </pre>
* @param {object} rowEntity the options.data element that was edited
* @param {object} colDef the column that was edited
* @param {object} newValue new value
* @param {object} oldValue old value
*/
afterCellEdit: function (rowEntity, colDef, newValue, oldValue) {
},
/**
* @ngdoc event
* @name beginCellEdit
* @eventOf ui.grid.edit.api:PublicApi
* @description raised when cell editing starts on a cell
* <pre>
* gridApi.edit.on.beginCellEdit(scope,function(rowEntity, colDef) {})
* </pre>
* @param {object} rowEntity the options.data element that was edited
* @param {object} colDef the column that was edited
* @param {object} triggerEvent the event that triggered the edit. Useful to prevent losing keystrokes on some
* complex editors
*/
beginCellEdit: function (rowEntity, colDef, triggerEvent) {
},
/**
* @ngdoc event
* @name cancelCellEdit
* @eventOf ui.grid.edit.api:PublicApi
* @description raised when cell editing is cancelled on a cell
* <pre>
* gridApi.edit.on.cancelCellEdit(scope,function(rowEntity, colDef) {})
* </pre>
* @param {object} rowEntity the options.data element that was edited
* @param {object} colDef the column that was edited
*/
cancelCellEdit: function (rowEntity, colDef) {
}
}
},
methods: {
edit: { }
}
};
grid.api.registerEventsFromObject(publicApi.events);
// grid.api.registerMethodsFromObject(publicApi.methods);
},
defaultGridOptions: function (gridOptions) {
/**
* @ngdoc object
* @name ui.grid.edit.api:GridOptions
*
* @description Options for configuring the edit feature, these are available to be
* set using the ui-grid {@link ui.grid.class:GridOptions gridOptions}
*/
/**
* @ngdoc object
* @name enableCellEdit
* @propertyOf ui.grid.edit.api:GridOptions
* @description If defined, sets the default value for the editable flag on each individual colDefs
* if their individual enableCellEdit configuration is not defined. Defaults to undefined.
*/
/**
* @ngdoc object
* @name cellEditableCondition
* @propertyOf ui.grid.edit.api:GridOptions
* @description If specified, either a value or function to be used by all columns before editing.
* If false, then editing of cell is not allowed.
* @example
* <pre>
* function($scope, triggerEvent) {
* //use $scope.row.entity, $scope.col.colDef and triggerEvent to determine if editing is allowed
* return true;
* }
* </pre>
*/
gridOptions.cellEditableCondition = gridOptions.cellEditableCondition === undefined ? true : gridOptions.cellEditableCondition;
/**
* @ngdoc object
* @name editableCellTemplate
* @propertyOf ui.grid.edit.api:GridOptions
* @description If specified, cellTemplate to use as the editor for all columns.
* <br/> defaults to 'ui-grid/cellTextEditor'
*/
/**
* @ngdoc object
* @name enableCellEditOnFocus
* @propertyOf ui.grid.edit.api:GridOptions
* @description If true, then editor is invoked as soon as cell receives focus. Default false.
* <br/>_requires cellNav feature and the edit feature to be enabled_
*/
// enableCellEditOnFocus can only be used if cellnav module is used
gridOptions.enableCellEditOnFocus = gridOptions.enableCellEditOnFocus === undefined ? false : gridOptions.enableCellEditOnFocus;
},
/**
* @ngdoc service
* @name editColumnBuilder
* @methodOf ui.grid.edit.service:uiGridEditService
* @description columnBuilder function that adds edit properties to grid column
* @returns {promise} promise that will load any needed templates when resolved
*/
editColumnBuilder: function (colDef, col, gridOptions) {
var promises = [];
/**
* @ngdoc object
* @name ui.grid.edit.api:ColumnDef
*
* @description Column Definition for edit feature, these are available to be
* set using the ui-grid {@link ui.grid.class:GridOptions.columnDef gridOptions.columnDefs}
*/
/**
* @ngdoc object
* @name enableCellEdit
* @propertyOf ui.grid.edit.api:ColumnDef
* @description enable editing on column
*/
colDef.enableCellEdit = colDef.enableCellEdit === undefined ? (gridOptions.enableCellEdit === undefined ?
(colDef.type !== 'object') : gridOptions.enableCellEdit) : colDef.enableCellEdit;
/**
* @ngdoc object
* @name cellEditableCondition
* @propertyOf ui.grid.edit.api:ColumnDef
* @description If specified, either a value or function evaluated before editing cell. If falsy, then editing of cell is not allowed.
* @example
* <pre>
* function($scope, triggerEvent) {
* //use $scope.row.entity, $scope.col.colDef and triggerEvent to determine if editing is allowed
* return true;
* }
* </pre>
*/
colDef.cellEditableCondition = colDef.cellEditableCondition === undefined ? gridOptions.cellEditableCondition : colDef.cellEditableCondition;
/**
* @ngdoc object
* @name editableCellTemplate
* @propertyOf ui.grid.edit.api:ColumnDef
* @description cell template to be used when editing this column. Can be Url or text template
* <br/>Defaults to gridOptions.editableCellTemplate
*/
if (colDef.enableCellEdit) {
colDef.editableCellTemplate = colDef.editableCellTemplate || gridOptions.editableCellTemplate || 'ui-grid/cellEditor';
promises.push(gridUtil.getTemplate(colDef.editableCellTemplate)
.then(
function (template) {
col.editableCellTemplate = template;
},
function (res) {
// Todo handle response error here?
throw new Error("Couldn't fetch/use colDef.editableCellTemplate '" + colDef.editableCellTemplate + "'");
}));
}
/**
* @ngdoc object
* @name enableCellEditOnFocus
* @propertyOf ui.grid.edit.api:ColumnDef
* @requires ui.grid.cellNav
* @description If true, then editor is invoked as soon as cell receives focus. Default false.
* <br>_requires both the cellNav feature and the edit feature to be enabled_
*/
// enableCellEditOnFocus can only be used if cellnav module is used
colDef.enableCellEditOnFocus = colDef.enableCellEditOnFocus === undefined ? gridOptions.enableCellEditOnFocus : colDef.enableCellEditOnFocus;
/**
* @ngdoc string
* @name editModelField
* @propertyOf ui.grid.edit.api:ColumnDef
* @description a bindable string value that is used when binding to edit controls instead of colDef.field
* <br/> example: You have a complex property on and object like state:{abbrev: 'MS',name: 'Mississippi'}. The
* grid should display state.name in the cell and sort/filter based on the state.name property but the editor
* requires the full state object.
* <br/>colDef.field = 'state.name'
* <br/>colDef.editModelField = 'state'
*/
// colDef.editModelField
return $q.all(promises);
},
/**
* @ngdoc service
* @name isStartEditKey
* @methodOf ui.grid.edit.service:uiGridEditService
* @description Determines if a keypress should start editing. Decorate this service to override with your
* own key events. See service decorator in angular docs.
* @param {Event} evt keydown event
* @returns {boolean} true if an edit should start
*/
isStartEditKey: function (evt) {
return !(evt.metaKey ||
evt.keyCode === uiGridConstants.keymap.ESC ||
evt.keyCode === uiGridConstants.keymap.SHIFT ||
evt.keyCode === uiGridConstants.keymap.CTRL ||
evt.keyCode === uiGridConstants.keymap.ALT ||
evt.keyCode === uiGridConstants.keymap.WIN ||
evt.keyCode === uiGridConstants.keymap.CAPSLOCK ||
evt.keyCode === uiGridConstants.keymap.LEFT ||
(evt.keyCode === uiGridConstants.keymap.TAB && evt.shiftKey) ||
evt.keyCode === uiGridConstants.keymap.RIGHT ||
evt.keyCode === uiGridConstants.keymap.TAB ||
evt.keyCode === uiGridConstants.keymap.UP ||
(evt.keyCode === uiGridConstants.keymap.ENTER && evt.shiftKey) ||
evt.keyCode === uiGridConstants.keymap.DOWN ||
evt.keyCode === uiGridConstants.keymap.ENTER);
}
};
return service;
}]);
/**
* @ngdoc directive
* @name ui.grid.edit.directive:uiGridEdit
* @element div
* @restrict A
*
* @description Adds editing features to the ui-grid directive.
*
* @example
<example module="app">
<file name="app.js">
var app = angular.module('app', ['ui.grid', 'ui.grid.edit']);
app.controller('MainCtrl', ['$scope', function ($scope) {
$scope.data = [
{ name: 'Bob', title: 'CEO' },
{ name: 'Frank', title: 'Lowly Developer' }
];
$scope.columnDefs = [
{name: 'name', enableCellEdit: true},
{name: 'title', enableCellEdit: true}
];
}]);
</file>
<file name="index.html">
<div ng-controller="MainCtrl">
<div ui-grid="{ data: data, columnDefs: columnDefs }" ui-grid-edit></div>
</div>
</file>
</example>
*/
module.directive('uiGridEdit', ['gridUtil', 'uiGridEditService', function (gridUtil, uiGridEditService) {
return {
replace: true,
priority: 0,
require: '^uiGrid',
scope: false,
compile: function () {
return {
pre: function ($scope, $elm, $attrs, uiGridCtrl) {
uiGridEditService.initializeGrid(uiGridCtrl.grid);
},
post: function ($scope, $elm, $attrs, uiGridCtrl) {
}
};
}
};
}]);
/**
* @ngdoc directive
* @name ui.grid.edit.directive:uiGridRenderContainer
* @element div
* @restrict A
*
* @description Adds keydown listeners to renderContainer element so we can capture when to begin edits
*
*/
module.directive('uiGridViewport', [ 'uiGridEditConstants',
function ( uiGridEditConstants) {
return {
replace: true,
priority: -99998, // run before cellNav
require: ['^uiGrid', '^uiGridRenderContainer'],
scope: false,
compile: function () {
return {
post: function ($scope, $elm, $attrs, controllers) {
var uiGridCtrl = controllers[0];
// Skip attaching if edit and cellNav is not enabled
if (!uiGridCtrl.grid.api.edit || !uiGridCtrl.grid.api.cellNav) { return; }
var containerId = controllers[1].containerId;
// no need to process for other containers
if (containerId !== 'body') {
return;
}
// refocus on the grid
$scope.$on(uiGridEditConstants.events.CANCEL_CELL_EDIT, function () {
uiGridCtrl.focus();
});
$scope.$on(uiGridEditConstants.events.END_CELL_EDIT, function () {
uiGridCtrl.focus();
});
}
};
}
};
}]);
/**
* @ngdoc directive
* @name ui.grid.edit.directive:uiGridCell
* @element div
* @restrict A
*
* @description Stacks on top of ui.grid.uiGridCell to provide in-line editing capabilities to the cell
* Editing Actions.
*
* Binds edit start events to the uiGridCell element. When the events fire, the gridCell element is appended
* with the columnDef.editableCellTemplate element ('cellEditor.html' by default).
*
* The editableCellTemplate should respond to uiGridEditConstants.events.BEGIN\_CELL\_EDIT angular event
* and do the initial steps needed to edit the cell (setfocus on input element, etc).
*
* When the editableCellTemplate recognizes that the editing is ended (blur event, Enter key, etc.)
* it should emit the uiGridEditConstants.events.END\_CELL\_EDIT event.
*
* If editableCellTemplate recognizes that the editing has been cancelled (esc key)
* it should emit the uiGridEditConstants.events.CANCEL\_CELL\_EDIT event. The original value
* will be set back on the model by the uiGridCell directive.
*
* Events that invoke editing:
* - dblclick
* - F2 keydown (when using cell selection)
*
* Events that end editing:
* - Dependent on the specific editableCellTemplate
* - Standards should be blur and enter keydown
*
* Events that cancel editing:
* - Dependent on the specific editableCellTemplate
* - Standards should be Esc keydown
*
* Grid Events that end editing:
* - uiGridConstants.events.GRID_SCROLL
*
*/
/**
* @ngdoc object
* @name ui.grid.edit.api:GridRow
*
* @description GridRow options for edit feature, these are available to be
* set internally only, by other features
*/
/**
* @ngdoc object
* @name enableCellEdit
* @propertyOf ui.grid.edit.api:GridRow
* @description enable editing on row, grouping for example might disable editing on group header rows
*/
module.directive('uiGridCell',
['$compile', '$injector', '$timeout', 'uiGridConstants', 'uiGridEditConstants', 'gridUtil', '$parse', 'uiGridEditService', '$rootScope', '$q',
function ($compile, $injector, $timeout, uiGridConstants, uiGridEditConstants, gridUtil, $parse, uiGridEditService, $rootScope, $q) {
var touchstartTimeout = 500;
if ($injector.has('uiGridCellNavService')) {
var uiGridCellNavService = $injector.get('uiGridCellNavService');
}
return {
priority: -100, // run after default uiGridCell directive
restrict: 'A',
scope: false,
require: '?^uiGrid',
link: function ($scope, $elm, $attrs, uiGridCtrl) {
var html,
origCellValue,
cellModel,
cancelTouchstartTimeout,
inEdit = false;
var editCellScope;
if (!$scope.col.colDef.enableCellEdit) {
return;
}
var cellNavNavigateDereg = function() {};
var viewPortKeyDownDereg = function() {};
var setEditable = function() {
if ($scope.col.colDef.enableCellEdit && $scope.row.enableCellEdit !== false) {
if (!$scope.beginEditEventsWired) { // prevent multiple attachments
registerBeginEditEvents();
}
} else {
if ($scope.beginEditEventsWired) {
cancelBeginEditEvents();
}
}
};
setEditable();
var rowWatchDereg = $scope.$watch('row', function (n, o) {
if (n !== o) {
setEditable();
}
});
$scope.$on('$destroy', function destroyEvents() {
rowWatchDereg();
// unbind all jquery events in order to avoid memory leaks
$elm.off();
});
function registerBeginEditEvents() {
$elm.on('dblclick', beginEdit);
// Add touchstart handling. If the users starts a touch and it doesn't end after X milliseconds, then start the edit
$elm.on('touchstart', touchStart);
if (uiGridCtrl && uiGridCtrl.grid.api.cellNav) {
viewPortKeyDownDereg = uiGridCtrl.grid.api.cellNav.on.viewPortKeyDown($scope, function (evt, rowCol) {
if (rowCol === null) {
return;
}
if (rowCol.row === $scope.row && rowCol.col === $scope.col && !$scope.col.colDef.enableCellEditOnFocus) {
// important to do this before scrollToIfNecessary
beginEditKeyDown(evt);
}
});
cellNavNavigateDereg = uiGridCtrl.grid.api.cellNav.on.navigate($scope, function (newRowCol, oldRowCol, evt) {
if ($scope.col.colDef.enableCellEditOnFocus) {
// Don't begin edit if the cell hasn't changed
if (newRowCol.row === $scope.row && newRowCol.col === $scope.col &&
(evt === null || (evt && (evt.type === 'click' || evt.type === 'keydown')))) {
$timeout(function() {
beginEdit(evt);
});
}
}
});
}
$scope.beginEditEventsWired = true;
}
function touchStart(event) {
// jQuery masks events
if (typeof(event.originalEvent) !== 'undefined' && event.originalEvent !== undefined) {
event = event.originalEvent;
}
// Bind touchend handler
$elm.on('touchend', touchEnd);
// Start a timeout
cancelTouchstartTimeout = $timeout(function() { }, touchstartTimeout);
// Timeout's done! Start the edit
cancelTouchstartTimeout.then(function () {
// Use setTimeout to start the edit because beginEdit expects to be outside of $digest
setTimeout(beginEdit, 0);
// Undbind the touchend handler, we don't need it anymore
$elm.off('touchend', touchEnd);
}).catch(angular.noop);
}
// Cancel any touchstart timeout
function touchEnd() {
$timeout.cancel(cancelTouchstartTimeout);
$elm.off('touchend', touchEnd);
}
function cancelBeginEditEvents() {
$elm.off('dblclick', beginEdit);
$elm.off('keydown', beginEditKeyDown);
$elm.off('touchstart', touchStart);
cellNavNavigateDereg();
viewPortKeyDownDereg();
$scope.beginEditEventsWired = false;
}
function beginEditKeyDown(evt) {
if (uiGridEditService.isStartEditKey(evt)) {
beginEdit(evt);
}
}
function shouldEdit(col, row, triggerEvent) {
return !row.isSaving &&
( angular.isFunction(col.colDef.cellEditableCondition) ?
col.colDef.cellEditableCondition($scope, triggerEvent) :
col.colDef.cellEditableCondition );
}
function beginEdit(triggerEvent) {
// we need to scroll the cell into focus before invoking the editor
$scope.grid.api.core.scrollToIfNecessary($scope.row, $scope.col)
.then(function () {
beginEditAfterScroll(triggerEvent);
});
}
/**
* @ngdoc property
* @name editDropdownOptionsArray
* @propertyOf ui.grid.edit.api:ColumnDef
* @description an array of values in the format
* [ {id: xxx, value: xxx} ], which is populated
* into the edit dropdown
*
*/
/**
* @ngdoc property
* @name editDropdownIdLabel
* @propertyOf ui.grid.edit.api:ColumnDef
* @description the label for the "id" field
* in the editDropdownOptionsArray. Defaults
* to 'id'
* @example
* <pre>
* $scope.gridOptions = {
* columnDefs: [
* {name: 'status', editableCellTemplate: 'ui-grid/dropdownEditor',
* editDropdownOptionsArray: [{code: 1, status: 'active'}, {code: 2, status: 'inactive'}],
* editDropdownIdLabel: 'code', editDropdownValueLabel: 'status' }
* ],
* </pre>
*
*/
/**
* @ngdoc property
* @name editDropdownRowEntityOptionsArrayPath
* @propertyOf ui.grid.edit.api:ColumnDef
* @description a path to a property on row.entity containing an
* array of values in the format
* [ {id: xxx, value: xxx} ], which will be used to populate
* the edit dropdown. This can be used when the dropdown values are dependent on
* the backing row entity.
* If this property is set then editDropdownOptionsArray will be ignored.
* @example
* <pre>
* $scope.gridOptions = {
* columnDefs: [
* {name: 'status', editableCellTemplate: 'ui-grid/dropdownEditor',
* editDropdownRowEntityOptionsArrayPath: 'foo.bars[0].baz',
* editDropdownIdLabel: 'code', editDropdownValueLabel: 'status' }
* ],
* </pre>
*
*/
/**
* @ngdoc service
* @name editDropdownOptionsFunction
* @methodOf ui.grid.edit.api:ColumnDef
* @description a function returning an array of values in the format
* [ {id: xxx, value: xxx} ], which will be used to populate
* the edit dropdown. This can be used when the dropdown values are dependent on
* the backing row entity with some kind of algorithm.
* If this property is set then both editDropdownOptionsArray and
* editDropdownRowEntityOptionsArrayPath will be ignored.
* @param {object} rowEntity the options.data element that the returned array refers to
* @param {object} colDef the column that implements this dropdown
* @returns {object} an array of values in the format
* [ {id: xxx, value: xxx} ] used to populate the edit dropdown
* @example
* <pre>
* $scope.gridOptions = {
* columnDefs: [
* {name: 'status', editableCellTemplate: 'ui-grid/dropdownEditor',
* editDropdownOptionsFunction: function(rowEntity, colDef) {
* if (rowEntity.foo === 'bar') {
* return [{id: 'bar1', value: 'BAR 1'},
* {id: 'bar2', value: 'BAR 2'},
* {id: 'bar3', value: 'BAR 3'}];
* } else {
* return [{id: 'foo1', value: 'FOO 1'},
* {id: 'foo2', value: 'FOO 2'}];
* }
* },
* editDropdownIdLabel: 'code', editDropdownValueLabel: 'status' }
* ],
* </pre>
*
*/
/**
* @ngdoc property
* @name editDropdownValueLabel
* @propertyOf ui.grid.edit.api:ColumnDef
* @description the label for the "value" field
* in the editDropdownOptionsArray. Defaults
* to 'value'
* @example
* <pre>
* $scope.gridOptions = {
* columnDefs: [
* {name: 'status', editableCellTemplate: 'ui-grid/dropdownEditor',
* editDropdownOptionsArray: [{code: 1, status: 'active'}, {code: 2, status: 'inactive'}],
* editDropdownIdLabel: 'code', editDropdownValueLabel: 'status' }
* ],
* </pre>
*
*/
/**
* @ngdoc property
* @name editDropdownFilter
* @propertyOf ui.grid.edit.api:ColumnDef
* @description A filter that you would like to apply to the values in the options list
* of the dropdown. For example if you were using angular-translate you might set this
* to `'translate'`
* @example
* <pre>
* $scope.gridOptions = {
* columnDefs: [
* {name: 'status', editableCellTemplate: 'ui-grid/dropdownEditor',
* editDropdownOptionsArray: [{code: 1, status: 'active'}, {code: 2, status: 'inactive'}],
* editDropdownIdLabel: 'code', editDropdownValueLabel: 'status', editDropdownFilter: 'translate' }
* ],
* </pre>
*
*/
function beginEditAfterScroll(triggerEvent) {
// 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, triggerEvent)) {
return;
}
var modelField = $scope.row.getQualifiedColField($scope.col);
if ($scope.col.colDef.editModelField) {
modelField = gridUtil.preEval('row.entity.' + $scope.col.colDef.editModelField);
}
cellModel = $parse(modelField);
// get original value from the cell
origCellValue = cellModel($scope);
html = $scope.col.editableCellTemplate;
html = html.replace(uiGridConstants.MODEL_COL_FIELD, modelField);
html = html.replace(uiGridConstants.COL_FIELD, 'grid.getCellValue(row, col)');
var optionFilter = $scope.col.colDef.editDropdownFilter ? '|' + $scope.col.colDef.editDropdownFilter : '';
html = html.replace(uiGridConstants.CUSTOM_FILTERS, optionFilter);
var inputType = 'text';
switch ($scope.col.colDef.type) {
case 'boolean':
inputType = 'checkbox';
break;
case 'number':
inputType = 'number';
break;
case 'date':
inputType = 'date';
break;
}
html = html.replace('INPUT_TYPE', inputType);
// In order to fill dropdown options we use:
// - A function/promise or
// - An array inside of row entity if no function exists or
// - A single array for the whole column if none of the previous exists.
var editDropdownOptionsFunction = $scope.col.colDef.editDropdownOptionsFunction;
if (editDropdownOptionsFunction) {
$q.when(editDropdownOptionsFunction($scope.row.entity, $scope.col.colDef))
.then(function(result) {
$scope.editDropdownOptionsArray = result;
});
} else {
var editDropdownRowEntityOptionsArrayPath = $scope.col.colDef.editDropdownRowEntityOptionsArrayPath;
if (editDropdownRowEntityOptionsArrayPath) {
$scope.editDropdownOptionsArray = resolveObjectFromPath($scope.row.entity, editDropdownRowEntityOptionsArrayPath);
}
else {
$scope.editDropdownOptionsArray = $scope.col.colDef.editDropdownOptionsArray;
}
}
$scope.editDropdownIdLabel = $scope.col.colDef.editDropdownIdLabel ? $scope.col.colDef.editDropdownIdLabel : 'id';
$scope.editDropdownValueLabel = $scope.col.colDef.editDropdownValueLabel ? $scope.col.colDef.editDropdownValueLabel : 'value';
var createEditor = function() {
inEdit = true;
cancelBeginEditEvents();
var cellElement = angular.element(html);
$elm.append(cellElement);
editCellScope = $scope.$new();
$compile(cellElement)(editCellScope);
var gridCellContentsEl = angular.element($elm.children()[0]);
gridCellContentsEl.addClass('ui-grid-cell-contents-hidden');
};
if (!$rootScope.$$phase) {
$scope.$apply(createEditor);
} else {
createEditor();
}
// stop editing when grid is scrolled
var deregOnGridScroll = $scope.col.grid.api.core.on.scrollBegin($scope, function () {
if ($scope.grid.disableScrolling) {
return;
}
endEdit();
$scope.grid.api.edit.raise.afterCellEdit($scope.row.entity, $scope.col.colDef, cellModel($scope), origCellValue);
deregOnGridScroll();
deregOnEndCellEdit();
deregOnCancelCellEdit();
});
// end editing
var deregOnEndCellEdit = $scope.$on(uiGridEditConstants.events.END_CELL_EDIT, function () {
endEdit();
$scope.grid.api.edit.raise.afterCellEdit($scope.row.entity, $scope.col.colDef, cellModel($scope), origCellValue);
deregOnEndCellEdit();
deregOnGridScroll();
deregOnCancelCellEdit();
});
// cancel editing
var deregOnCancelCellEdit = $scope.$on(uiGridEditConstants.events.CANCEL_CELL_EDIT, function () {
cancelEdit();
deregOnCancelCellEdit();
deregOnGridScroll();
deregOnEndCellEdit();
});
$scope.$broadcast(uiGridEditConstants.events.BEGIN_CELL_EDIT, triggerEvent);
$timeout(function () {
// execute in a timeout to give any complex editor templates a cycle to completely render
$scope.grid.api.edit.raise.beginCellEdit($scope.row.entity, $scope.col.colDef, triggerEvent);
});
}
function endEdit() {
$scope.grid.disableScrolling = false;
if (!inEdit) {
return;
}
// sometimes the events can't keep up with the keyboard and grid focus is lost, so always focus
// back to grid here. The focus call needs to be before the $destroy and removal of the control,
// otherwise ng-model-options of UpdateOn: 'blur' will not work.
if (uiGridCtrl && uiGridCtrl.grid.api.cellNav) {
uiGridCtrl.focus();
}
var gridCellContentsEl = angular.element($elm.children()[0]);
// remove edit element
editCellScope.$destroy();
var children = $elm.children();
for (var i = 1; i < children.length; i++) {
angular.element(children[i]).remove();
}
gridCellContentsEl.removeClass('ui-grid-cell-contents-hidden');
inEdit = false;
registerBeginEditEvents();
$scope.grid.api.core.notifyDataChange( uiGridConstants.dataChange.EDIT );
}
function cancelEdit() {
$scope.grid.disableScrolling = false;
if (!inEdit) {
return;
}
cellModel.assign($scope, origCellValue);
$scope.$apply();
$scope.grid.api.edit.raise.cancelCellEdit($scope.row.entity, $scope.col.colDef);
endEdit();
}
// resolves a string path against the given object
// shamelessly borrowed from
// http://stackoverflow.com/questions/6491463/accessing-nested-javascript-objects-with-string-key
function resolveObjectFromPath(object, path) {
path = path.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
path = path.replace(/^\./, ''); // strip a leading dot
var a = path.split('.');
while (a.length) {
var n = a.shift();
if (n in object) {
object = object[n];
} else {
return;
}
}
return object;
}
}
};
}]);
/**
* @ngdoc directive
* @name ui.grid.edit.directive:uiGridEditor
* @element div
* @restrict A
*
* @description input editor directive for editable fields.
* Provides EndEdit and CancelEdit events
*
* Events that end editing:
* blur and enter keydown
*
* Events that cancel editing:
* - Esc keydown
*
*/
module.directive('uiGridEditor',
['gridUtil', 'uiGridConstants', 'uiGridEditConstants','$timeout', 'uiGridEditService',
function (gridUtil, uiGridConstants, uiGridEditConstants, $timeout, uiGridEditService) {
return {
scope: true,
require: ['?^uiGrid', '?^uiGridRenderContainer', 'ngModel'],
compile: function () {
return {
pre: function ($scope, $elm, $attrs) {
},
post: function ($scope, $elm, $attrs, controllers) {
var uiGridCtrl, renderContainerCtrl, ngModel;
if (controllers[0]) { uiGridCtrl = controllers[0]; }
if (controllers[1]) { renderContainerCtrl = controllers[1]; }
if (controllers[2]) { ngModel = controllers[2]; }
// set focus at start of edit
$scope.$on(uiGridEditConstants.events.BEGIN_CELL_EDIT, function () {
// must be in a timeout since it requires a new digest cycle
$timeout(function () {
$elm[0].focus();
// only select text if it is not being replaced below in the cellNav viewPortKeyPress
if ($elm[0].select && ($scope.col.colDef.enableCellEditOnFocus || !(uiGridCtrl && uiGridCtrl.grid.api.cellNav))) {
$elm[0].select();
}
else {
// some browsers (Chrome) stupidly, imo, support the w3 standard that number, email, ...
// fields should not allow setSelectionRange. We ignore the error for those browsers
// https://www.w3.org/Bugs/Public/show_bug.cgi?id=24796
try {
$elm[0].setSelectionRange($elm[0].value.length, $elm[0].value.length);
}
catch (ex) {
// ignore
}
}
});
// set the keystroke that started the edit event
// we must do this because the BeginEdit is done in a different event loop than the intitial
// keydown event
// fire this event for the keypress that is received
if (uiGridCtrl && uiGridCtrl.grid.api.cellNav) {
var viewPortKeyDownUnregister = uiGridCtrl.grid.api.cellNav.on.viewPortKeyPress($scope, function (evt, rowCol) {
if (uiGridEditService.isStartEditKey(evt)) {
var code = typeof evt.which === 'number' ? evt.which : evt.keyCode;
if (code > 0) {
ngModel.$setViewValue(String.fromCharCode(code), evt);
ngModel.$render();
}
}
viewPortKeyDownUnregister();
});
}
// macOS will blur the checkbox when clicked in Safari and Firefox,
// to get around this, we disable the blur handler on mousedown,
// and then focus the checkbox and re-enable the blur handler after $timeout
$elm.on('mousedown', function(evt) {
if ($elm[0].type === 'checkbox') {
$elm.off('blur', $scope.stopEdit);
$timeout(function() {
$elm[0].focus();
$elm.on('blur', $scope.stopEdit);
});
}
});
if ($elm[0]) {
$elm[0].focus();
}
$elm.on('blur', $scope.stopEdit);
});
$scope.deepEdit = false;
$scope.stopEdit = function (evt) {
if ($scope.inputForm && !$scope.inputForm.$valid) {
evt.stopPropagation();
$scope.$emit(uiGridEditConstants.events.CANCEL_CELL_EDIT);
}
else {
$scope.$emit(uiGridEditConstants.events.END_CELL_EDIT);