-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathmd-data-table.js
More file actions
2391 lines (2022 loc) · 85.2 KB
/
md-data-table.js
File metadata and controls
2391 lines (2022 loc) · 85.2 KB
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
(function(){
'use strict';
/**
* @description
* Component resolution and creation flow:
*
* Because directives are not containing each other in their templates (e.g. not a general parent-child
* relationship), that's why the resolution of different components are not obvious. They are working with
* transclusion and it's rule will apply to the process flow.
* Here is an overview on what directives and which part of that will execute in which order.
*
* 1. `mdtTable` controller
* - basic services initialized for future usage by other directives
*
* 2. `mdtTable` link
* - transclude `mdtHeaderRow` and all `mdtRow` elements generated contents (however it's not relevant,
* the real generated content is generated with the help of the collected data by `TableStorageService`.
* - bind some helper functions for real the generated content
*
* 3. Header resolution
*
* 3.1. `mdtHeaderRow` link
* - transclude all `mdtColumn` directive's generated contents
*
* 3.2. `mdtColumn` link(s)
* - add columns by the help of the `mdtTable` public API
* - contents generated but not yet transcluded
*
* 4. Rows resolution
*
* 4.1. `mdtRow` controller(s)
* - public function created which able to add a cell data to the locally stored array
*
* 4.2. `mdtRow` link(s)
* - transclude all `mdtCell` directive's generated contents
* - add the collected row data to the service by the help of `mdtTable` public API
*
* 4.3. `mdtCell` link(s)
* - add cells data by the help of `mdtRow` public API
* - contents generated but not yet transcluded
*
*/
angular.module('mdDataTable', ['mdtTemplates', 'ngMaterial', 'ngMdIcons', 'ngSanitize']);
}());
(function(){
'use strict';
InlineEditModalCtrl.$inject = ['$scope', 'position', 'cellData', 'mdtTranslations', '$timeout', '$mdDialog'];
function InlineEditModalCtrl($scope, position, cellData, mdtTranslations, $timeout, $mdDialog){
$timeout(function() {
var el = $('md-dialog');
el.css('position', 'fixed');
el.css('top', position['top']);
el.css('left', position['left']);
el.find('input[type="text"]').focus();
});
$scope.cellData = cellData;
$scope.mdtTranslations = mdtTranslations;
$scope.saveRow = saveRow;
$scope.cancel = cancel;
function saveRow(){
if($scope.editFieldForm.$valid){
$mdDialog.hide(cellData.value);
}
}
function cancel(){
$mdDialog.cancel();
}
}
angular
.module('mdDataTable')
.controller('InlineEditModalCtrl', InlineEditModalCtrl);
}());
(function(){
'use strict';
mdtAlternateHeadersDirective.$inject = ['_'];
function mdtAlternateHeadersDirective(_){
return {
restrict: 'E',
templateUrl: '/main/templates/mdtAlternateHeaders.html',
transclude: true,
replace: true,
scope: true,
require: '^mdtTable',
link: function($scope, element, attrs, ctrl){
$scope.deleteSelectedRows = deleteSelectedRows;
$scope.getNumberOfSelectedRows = _.bind(ctrl.dataStorage.getNumberOfSelectedRows, ctrl.dataStorage);
function deleteSelectedRows(){
if ($scope.deleteRowCallback({rows: ctrl.dataStorage.getSelectedRows()}) !== false) {
ctrl.dataStorage.deleteSelectedRows();
}
}
}
};
}
angular
.module('mdDataTable')
.directive('mdtAlternateHeaders', mdtAlternateHeadersDirective);
}());
(function(){
'use strict';
/**
* @ngdoc directive
* @name mdtTable
* @restrict E
*
* @description
* The base HTML tag for the component.
*
* @param {object=} tableCard when set table will be embedded within a card, with data manipulation tools available
* at the top and bottom.
*
* Properties:
*
* - `{boolean=}` `visible` - enable/disable table card explicitly
* - `{string}` `title` - the title of the card
* - `{boolean=}` `columnSelector` - enables the column selection for the table (you can disable certain columns from the list selection, using `exclude-from-column-selector`, see the related docs)
* - `{array=}` `actionIcons` - (not implemented yet)
*
* @param {boolean=} selectableRows when set each row will have a checkbox
* @param {boolean=} virtualRepeat when set, virtual scrolling will be applied to the table. You must set a fixed
* height to the `.md-virtual-repeat-container` class in order to make it work properly. Since virtual
* scrolling is working with fixed height.
* @param {String=} alternateHeaders some table cards may require headers with actions instead of titles.
* Two possible approaches to this are to display persistent actions, or a contextual header that activates
* when items are selected
*
* Assignable values are:
*
* - 'contextual' - when set table will have kind of dynamic header. E.g.: When selecting rows, the header will
* change and it'll show the number of selected rows and a delete icon on the right.
* - 'persistentActions' - (not implemented yet)
*
* @param {function(rows)=} deleteRowCallback callback function when deleting rows.
* At default an array of the deleted row's data will be passed as the argument.
* When `table-row-id` set for the deleted row then that value will be passed.
*
* @param {function(rows)=} selectedRowCallback callback function when selecting rows.
* At default an array of the selected row's data will be passed as the argument.
* When `table-row-id` set for the selected row then that value will be passed.
*
* @param {boolean=} animateSortIcon sort icon will be animated on change
* @param {boolean=} rippleEffect ripple effect will be applied on the columns when clicked (not implemented yet)
* @param {boolean=} paginatedRows if set then basic pagination will applied to the bottom of the table.
*
* Properties:
*
* - `{boolean=}` `isEnabled` - enables pagination
* - `{array}` `rowsPerPageValues` - set page sizes. Example: [5,10,20]
*
* @param {object=} mdtRow passing rows data through this attribute will initialize the table with data. Additional
* benefit instead of using `mdt-row` element directive is that it makes possible to listen on data changes.
*
* Properties:
*
* - `{array}` `data` - the input data for rows
* - `{integer|string=}` `table-row-id-key` - the uniq identifier for a row
* - `{function(rowData)=}` `table-row-class-name` - callback to specify the class name of a row
* - `{array}` `column-keys` - specifying property names for the passed data array. Makes it possible to
* configure which property assigned to which column in the table. The list should provided at the same order
* as it was specified inside `mdt-header-row` element directive.
*
* @param {function(page, pageSize, options)=} mdtRowPaginator providing the data for the table by a function. Should set a
* function which returns a promise when it's called. When the function is called, these parameters will be
* passed: `page` and `pageSize` which can help implementing an ajax-based paging, and `options` which is providing
* more information.
*
* Currently these are available from options:
* - `array` `columnFilter` - an array of the filtered column sets (todo: create a demo for it)
* - `array` `columnSort` - an array of the sorted column sets. You can inspect which column did you
* sorted and if its in asc or desc order (todo: create a demo for it)
*
* @param {string=} mdtRowPaginatorErrorMessage overrides default error message when promise gets rejected by the
* paginator function.
*
* @param {string=} mdtRowPaginatorNoResultsMessage overrides default 'no results' message.
*
* @param {function(loadPageCallback)=} mdtTriggerRequest provide a callback function for manually triggering an
* ajax request. Can be useful when you want to populate the results in the table manually. (e.g.: having a
* search field in your page which then can trigger a new request in the table to show the results based on
* that filter.
*
* @param {object=} mdtTranslations accepts various key-value pairs for custom translations.
*
* @param {boolean=} mdtLoadingIndicator if set then loading indicator can be customised
*
* Properties:
*
* - `{string=}` `color` - passing a css compatible format as a color will set the color for the loading indicator
*
* @example
* <h2>`mdt-row` attribute:</h2>
*
* When column names are: `Product name`, `Creator`, `Last Update`
* The passed data row's structure: `id`, `item_name`, `update_date`, `created_by`
*
* Then the following setup will parse the data to the right columns:
* <pre>
* <mdt-table
* mdt-row="{
* 'data': controller.data,
* 'table-row-id-key': 'id',
* 'column-keys': ['item_name', 'update_date', 'created_by']
* }">
*
* <mdt-header-row>
* <mdt-column>Product name</mdt-column>
* <mdt-column>Creator</mdt-column>
* <mdt-column>Last Update</mdt-column>
* </mdt-header-row>
* </mdt-table>
* </pre>
*/
mdtTableDirective.$inject = ['TableDataStorageFactory', 'EditCellFeature', 'SelectableRowsFeature', 'PaginationFeature', 'ColumnSelectorFeature', '_'];
function mdtTableDirective(TableDataStorageFactory,
EditCellFeature,
SelectableRowsFeature,
PaginationFeature,
ColumnSelectorFeature,
_){
return {
restrict: 'E',
templateUrl: '/main/templates/mdtTable.html',
transclude: true,
scope: {
tableCard: '=',
selectableRows: '=',
alternateHeaders: '=',
deleteRowCallback: '&',
selectedRowCallback: '&',
saveRowCallback: '&',
animateSortIcon: '=',
rippleEffect: '=',
paginatedRows: '=',
mdtRow: '=',
mdtRowPaginator: '&?',
mdtRowPaginatorErrorMessage:'@',
mdtRowPaginatorNoResultsMessage:'@',
virtualRepeat: '=',
mdtTriggerRequest: '&?',
mdtTranslations: '=?',
mdtLoadingIndicator: '=?'
},
controller: ['$scope', function mdtTable($scope){
var vm = this;
$scope.rippleEffectCallback = function(){
return $scope.rippleEffect ? $scope.rippleEffect : false;
};
_setDefaultTranslations();
_initTableStorage();
PaginationFeature.initFeature($scope, vm);
ColumnSelectorFeature.initFeature($scope, vm);
_processData();
// initialization of the storage service
function _initTableStorage(){
vm.dataStorage = TableDataStorageFactory.getInstance();
}
// set translations or fallback to a default value
function _setDefaultTranslations(){
$scope.mdtTranslations = $scope.mdtTranslations || {};
$scope.mdtTranslations.rowsPerPage = $scope.mdtTranslations.rowsPerPage || 'Rows per page:';
$scope.mdtTranslations.largeEditDialog = $scope.mdtTranslations.largeEditDialog || {};
$scope.mdtTranslations.largeEditDialog.saveButtonLabel = $scope.mdtTranslations.largeEditDialog.saveButtonLabel || 'Save';
$scope.mdtTranslations.largeEditDialog.cancelButtonLabel = $scope.mdtTranslations.largeEditDialog.cancelButtonLabel || 'Cancel';
}
// fill storage with values if set
function _processData(){
if(_.isEmpty($scope.mdtRow)) {
return;
}
//local search/filter
if (angular.isUndefined($scope.mdtRowPaginator)) {
$scope.$watch('mdtRow', function (mdtRow) {
vm.dataStorage.storage = [];
_addRawDataToStorage(mdtRow['data']);
}, true);
}else{
//if it's used for 'Ajax pagination'
}
}
function _addRawDataToStorage(data){
var rowId;
var columnValues = [];
_.each(data, function(row){
rowId = _.get(row, $scope.mdtRow['table-row-id-key']);
columnValues = [];
_.each($scope.mdtRow['column-keys'], function(columnKey){
columnValues.push({
attributes: {
editableField: false
},
rowId: rowId,
columnKey: columnKey,
value: _.get(row, columnKey)
});
});
vm.dataStorage.addRowData(rowId, columnValues);
});
}
}],
link: function($scope, element, attrs, ctrl, transclude){
$scope.dataStorage = ctrl.dataStorage;
_injectContentIntoTemplate();
_initEditCellFeature();
_initSelectableRowsFeature();
PaginationFeature.startFeature(ctrl);
ColumnSelectorFeature.initFeatureHeaderValues($scope.dataStorage.header, ctrl.columnSelectorFeature);
function _injectContentIntoTemplate(){
transclude(function (clone) {
var headings = [];
var body = [];
var customCell = [];
// Use plain JS to append content
_.each(clone, function (child) {
if ( child.classList !== undefined ) {
if ( child.classList.contains('theadTrRow')) {
headings.push(child);
}
else if( child.classList.contains('customCell') ) {
customCell.push(child);
}
else {
body.push(child);
}
} else {
body.push(child);
}
});
var reader = element[0].querySelector('.mdtTable-reader');
_.each(headings, function (heading) {
reader.appendChild( heading );
});
_.each(body, function (item) {
reader.appendChild( item );
});
});
}
function _initEditCellFeature(){
//TODO: make it possible to only register feature if there is at least
// one column which requires it.
// for that we need to change the place where we register edit-row.
// Remove mdt-row attributes --> do it in mdt-row attribute directive on mdtTable
EditCellFeature.addRequiredFunctions($scope, ctrl);
}
function _initSelectableRowsFeature(){
SelectableRowsFeature.getInstance({
$scope: $scope,
ctrl: ctrl
});
}
}
};
}
angular
.module('mdDataTable')
.directive('mdtTable', mdtTableDirective);
}());
(function(){
'use strict';
mdtAjaxPaginationHelperFactory.$inject = ['ColumnFilterFeature', 'ColumnSortFeature', 'PaginatorTypeProvider', '_'];
function mdtAjaxPaginationHelperFactory(ColumnFilterFeature, ColumnSortFeature, PaginatorTypeProvider, _){
function mdtAjaxPaginationHelper(params){
this.paginatorType = PaginatorTypeProvider.AJAX;
this.dataStorage = params.dataStorage;
this.rowOptions = params.mdtRowOptions;
this.paginatorFunction = params.mdtRowPaginatorFunction;
this.mdtRowPaginatorErrorMessage = params.mdtRowPaginatorErrorMessage || 'Ajax error during loading contents.';
this.mdtRowPaginatorNoResultsMessage = params.mdtRowPaginatorNoResultsMessage || 'No results.';
this.mdtTriggerRequest = params.mdtTriggerRequest;
if(params.paginationSetting &&
params.paginationSetting.hasOwnProperty('rowsPerPageValues') &&
params.paginationSetting.rowsPerPageValues.length > 0){
this.rowsPerPageValues = params.paginationSetting.rowsPerPageValues;
}else{
this.rowsPerPageValues = [10,20,30,50,100];
}
this.rowsPerPage = this.rowsPerPageValues[0];
this.page = 1;
this.totalResultCount = 0;
this.totalPages = 0;
this.isLoading = false;
//fetching the 1st page
//this.fetchPage(this.page);
//triggering ajax call manually
if(this.mdtTriggerRequest) {
params.mdtTriggerRequest({
loadPageCallback: this.fetchPage.bind(this)
});
}
}
mdtAjaxPaginationHelper.prototype.getStartRowIndex = function(){
return (this.page-1) * this.rowsPerPage;
};
mdtAjaxPaginationHelper.prototype.getEndRowIndex = function(){
var lastItem = this.getStartRowIndex() + this.rowsPerPage - 1;
if(this.totalResultCount < lastItem){
return this.totalResultCount - 1;
}
return lastItem;
};
mdtAjaxPaginationHelper.prototype.getTotalRowsCount = function(){
return this.totalResultCount;
};
mdtAjaxPaginationHelper.prototype.getRows = function(){
return this.dataStorage.storage;
};
mdtAjaxPaginationHelper.prototype.previousPage = function(){
var that = this;
if(this.hasPreviousPage()){
this.fetchPage(this.page-1).then(function(){
that.page--;
});
}
};
mdtAjaxPaginationHelper.prototype.nextPage = function(){
var that = this;
if(this.hasNextPage()){
this.fetchPage(this.page+1).then(function(){
that.page++;
});
}
};
mdtAjaxPaginationHelper.prototype.getFirstPage = function(){
this.page = 1;
this.fetchPage(this.page);
};
mdtAjaxPaginationHelper.prototype.hasNextPage = function(){
return this.page < this.totalPages;
};
mdtAjaxPaginationHelper.prototype.hasPreviousPage = function(){
return this.page > 1;
};
mdtAjaxPaginationHelper.prototype.fetchPage = function(page){
this.isLoading = true;
var that = this;
var callbackArguments = {page: page, pageSize: this.rowsPerPage, options: {}};
ColumnFilterFeature.appendAppliedFiltersToCallbackArgument(this.dataStorage, callbackArguments);
ColumnSortFeature.appendSortedColumnToCallbackArgument(this.dataStorage, callbackArguments);
return this.paginatorFunction(callbackArguments)
.then(function(data){
that.dataStorage.storage = [];
that.setRawDataToStorage(that, data.results, that.rowOptions['table-row-id-key'], that.rowOptions['column-keys'], that.rowOptions);
that.totalResultCount = data.totalResultCount;
that.totalPages = Math.ceil(data.totalResultCount / that.rowsPerPage);
if(that.totalResultCount == 0){
that.isNoResults = true;
}else{
that.isNoResults = false;
}
that.isLoadError = false;
that.isLoading = false;
}, function(){
that.dataStorage.storage = [];
that.isLoadError = true;
that.isLoading = false;
that.isNoResults = true;
});
};
mdtAjaxPaginationHelper.prototype.setRawDataToStorage = function(that, data, tableRowIdKey, columnKeys, rowOptions){
var rowId;
var columnValues = [];
_.each(data, function(row){
rowId = _.get(row, tableRowIdKey);
columnValues = [];
_.each(columnKeys, function(columnKey){
//TODO: centralize adding column values into one place.
// Duplication occurs at mdtCellDirective's link function.
// Duplication in mdtTableDirective `_addRawDataToStorage` method!
columnValues.push({
attributes: {
editableField: false
},
rowId: rowId,
columnKey: columnKey,
value: _.get(row, columnKey)
});
});
var className = rowOptions['table-row-class-name'] ? rowOptions['table-row-class-name'](row) : false;
that.dataStorage.addRowData(rowId, columnValues, className);
});
};
mdtAjaxPaginationHelper.prototype.setRowsPerPage = function(rowsPerPage){
this.rowsPerPage = rowsPerPage;
this.getFirstPage();
};
return {
getInstance: function(dataStorage, isEnabled, paginatorFunction, rowOptions){
return new mdtAjaxPaginationHelper(dataStorage, isEnabled, paginatorFunction, rowOptions);
}
};
}
angular
.module('mdDataTable')
.service('mdtAjaxPaginationHelperFactory', mdtAjaxPaginationHelperFactory);
}());
(function(){
'use strict';
mdtLodashFactory.$inject = ['$window'];
function mdtLodashFactory($window){
if(!$window._){
throw Error('Lodash does not found. Please make sure you load Lodash before any source for mdDataTable');
}
return $window._;
}
angular
.module('mdDataTable')
.factory('_', mdtLodashFactory);
}());
(function(){
'use strict';
mdtPaginationHelperFactory.$inject = ['PaginatorTypeProvider', '_'];
function mdtPaginationHelperFactory(PaginatorTypeProvider, _){
function mdtPaginationHelper(dataStorage, paginationSetting){
this.paginatorType = PaginatorTypeProvider.ARRAY;
this.dataStorage = dataStorage;
if(paginationSetting &&
paginationSetting.hasOwnProperty('rowsPerPageValues') &&
paginationSetting.rowsPerPageValues.length > 0){
this.rowsPerPageValues = paginationSetting.rowsPerPageValues;
}else{
this.rowsPerPageValues = [10,20,30,50,100];
}
this.rowsPerPage = this.rowsPerPageValues[0];
this.page = 1;
}
mdtPaginationHelper.prototype.calculateVisibleRows = function (){
var that = this;
_.each(this.dataStorage.storage, function (rowData, index) {
if(index >= that.getStartRowIndex() && index <= that.getEndRowIndex()) {
rowData.optionList.visible = true;
} else {
rowData.optionList.visible = false;
}
});
};
mdtPaginationHelper.prototype.getStartRowIndex = function(){
return (this.page-1) * this.rowsPerPage;
};
mdtPaginationHelper.prototype.getEndRowIndex = function(){
var lastItem = this.getStartRowIndex() + this.rowsPerPage-1;
if(this.dataStorage.storage.length < lastItem){
return this.dataStorage.storage.length - 1;
}
return lastItem;
};
mdtPaginationHelper.prototype.getTotalRowsCount = function(){
return this.dataStorage.storage.length;
};
mdtPaginationHelper.prototype.getRows = function(){
this.calculateVisibleRows();
return this.dataStorage.storage;
};
mdtPaginationHelper.prototype.previousPage = function(){
if(this.hasPreviousPage()){
this.page--;
}
};
mdtPaginationHelper.prototype.nextPage = function(){
if(this.hasNextPage()){
this.page++;
}
};
mdtPaginationHelper.prototype.hasNextPage = function(){
var totalPages = Math.ceil(this.getTotalRowsCount() / this.rowsPerPage);
return this.page < totalPages;
};
mdtPaginationHelper.prototype.hasPreviousPage = function(){
return this.page > 1;
};
mdtPaginationHelper.prototype.setRowsPerPage = function(rowsPerPage){
this.rowsPerPage = rowsPerPage;
this.page = 1;
};
return {
getInstance: function(dataStorage, isEnabled){
return new mdtPaginationHelper(dataStorage, isEnabled);
}
};
}
angular
.module('mdDataTable')
.service('mdtPaginationHelperFactory', mdtPaginationHelperFactory);
}());
(function(){
'use strict';
TableDataStorageFactory.$inject = ['$log', '_'];
function TableDataStorageFactory($log, _){
function TableDataStorageService(){
this.storage = [];
this.header = [];
this.customCells = {};
}
TableDataStorageService.prototype.addHeaderCellData = function(ops){
this.header.push(ops);
};
TableDataStorageService.prototype.addRowData = function(explicitRowId, rowArray, className){
if(!(rowArray instanceof Array)){
$log.error('`rowArray` parameter should be array');
return;
}
this.storage.push({
rowId: explicitRowId,
optionList: {
selected: false,
deleted: false,
visible: true,
className: className || false
},
data: rowArray
});
};
TableDataStorageService.prototype.getRowData = function(index){
if(!this.storage[index]){
$log.error('row is not exists at index: '+index);
return;
}
return this.storage[index].data;
};
TableDataStorageService.prototype.getRowOptions = function(index){
if(!this.storage[index]){
$log.error('row is not exists at index: '+index);
return;
}
return this.storage[index].optionList;
};
TableDataStorageService.prototype.setAllRowsSelected = function(isSelected, isPaginationEnabled){
if(typeof isSelected === 'undefined'){
$log.error('`isSelected` parameter is required');
return;
}
_.each(this.storage, function(rowData){
if(isPaginationEnabled) {
if (rowData.optionList.visible) {
rowData.optionList.selected = isSelected ? true : false;
}
}else{
rowData.optionList.selected = isSelected ? true : false;
}
});
};
TableDataStorageService.prototype.isAnyRowSelected = function(){
return _.some(this.storage, function(rowData){
return rowData.optionList.selected === true && rowData.optionList.deleted === false;
});
};
TableDataStorageService.prototype.getNumberOfSelectedRows = function(){
var res = _.countBy(this.storage, function(rowData){
return rowData.optionList.selected === true && rowData.optionList.deleted === false ? 'selected' : 'unselected';
});
return res.selected ? res.selected : 0;
};
TableDataStorageService.prototype.deleteSelectedRows = function(){
var deletedRows = [];
_.each(this.storage, function(rowData){
if(rowData.optionList.selected && rowData.optionList.deleted === false){
if(rowData.rowId){
deletedRows.push(rowData.rowId);
//Fallback when no id was specified
} else{
deletedRows.push(rowData.data);
}
rowData.optionList.deleted = true;
}
});
return deletedRows;
};
TableDataStorageService.prototype.getSelectedRows = function(){
var selectedRows = [];
_.each(this.storage, function(rowData){
if(rowData.optionList.selected && rowData.optionList.deleted === false){
if(rowData.rowId){
selectedRows.push(rowData.rowId);
//Fallback when no id was specified
} else{
selectedRows.push(rowData.data);
}
}
});
return selectedRows;
};
TableDataStorageService.prototype.getSavedRowData = function(rowData){
var rawRowData = [];
_.each(rowData.data, function(aCell){
rawRowData.push(aCell.value);
});
return rawRowData;
};
return {
getInstance: function(){
return new TableDataStorageService();
}
};
}
angular
.module('mdDataTable')
.factory('TableDataStorageFactory', TableDataStorageFactory);
}());
(function(){
'use strict';
PaginationFeature.$inject = ['mdtPaginationHelperFactory', 'mdtAjaxPaginationHelperFactory'];
function PaginationFeature(mdtPaginationHelperFactory, mdtAjaxPaginationHelperFactory){
var service = this;
service.initFeature = initFeature;
service.startFeature = startFeature;
function initFeature(scope, ctrl){
if(!scope.mdtRowPaginator){
ctrl.mdtPaginationHelper = scope.mdtPaginationHelper = mdtPaginationHelperFactory
.getInstance(ctrl.dataStorage, scope.paginatedRows, scope.mdtRow);
}else{
ctrl.mdtPaginationHelper = scope.mdtPaginationHelper = mdtAjaxPaginationHelperFactory.getInstance({
dataStorage: ctrl.dataStorage,
paginationSetting: scope.paginatedRows,
mdtRowOptions: scope.mdtRow,
mdtRowPaginatorFunction: scope.mdtRowPaginator,
mdtRowPaginatorErrorMessage: scope.mdtRowPaginatorErrorMessage,
mdtRowPaginatorNoResultsMessage: scope.mdtRowPaginatorNoResultsMessage,
mdtTriggerRequest: scope.mdtTriggerRequest
});
}
scope.isPaginationEnabled = function(){
if(scope.paginatedRows === true ||
(scope.paginatedRows && scope.paginatedRows.hasOwnProperty('isEnabled') && scope.paginatedRows.isEnabled === true)){
return true;
}
return false;
};
ctrl.paginationFeature = {
startPaginationFeature: function() {
if (scope.mdtRowPaginator) {
scope.mdtPaginationHelper.fetchPage(1);
}
}
};
}
function startFeature(ctrl){
ctrl.paginationFeature.startPaginationFeature();
}
}
angular
.module('mdDataTable')
.service('PaginationFeature', PaginationFeature);
}());
(function(){
'use strict';
SelectableRowsFeatureFactory.$inject = ['$timeout'];
function SelectableRowsFeatureFactory($timeout){
function SelectableRowsFeature(params){
this.$scope = params.$scope;
this.ctrl = params.ctrl;
this.$scope.onCheckboxChange = _.bind(this.onCheckboxChange, this);
}
SelectableRowsFeature.prototype.onCheckboxChange = function(){
var that = this;
// we need to push it to the event loop to make it happen last
// (e.g.: all the elements can be selected before we call the callback)
$timeout(function(){
that.$scope.selectedRowCallback({
rows: that.ctrl.dataStorage.getSelectedRows()
});
},0);
};
return {
getInstance: function(params){
return new SelectableRowsFeature(params);
}
};
}
angular
.module('mdDataTable')
.service('SelectableRowsFeature', SelectableRowsFeatureFactory);
}());
(function(){
'use strict';
ColumnAlignmentHelper.$inject = ['ColumnOptionProvider'];
function ColumnAlignmentHelper(ColumnOptionProvider){
var service = this;
service.getColumnAlignClass = getColumnAlignClass;
function getColumnAlignClass(alignRule) {
if (alignRule === ColumnOptionProvider.ALIGN_RULE.ALIGN_RIGHT) {
return 'rightAlignedColumn';
} else {
return 'leftAlignedColumn';
}
}
}
angular
.module('mdDataTable')
.service('ColumnAlignmentHelper', ColumnAlignmentHelper);
}());
(function(){
'use strict';
/**
* @name ColumnOptionProvider
* @returns possible assignable column options you can give
*
* @describe Representing the assignable properties to the columns you can give.
*/
var ColumnOptionProvider = {
ALIGN_RULE : {
ALIGN_LEFT: 'left',
ALIGN_RIGHT: 'right'
}
};
angular
.module('mdDataTable')
.value('ColumnOptionProvider', ColumnOptionProvider);
})();
(function(){
'use strict';
/**
* @name PaginatorTypeProvider
* @returns possible values for different type of paginators
*
* @describe Representing the possible paginator types.
*/
var PaginatorTypeProvider = {
AJAX : 'ajax',
ARRAY : 'array'
};
angular
.module('mdDataTable')
.value('PaginatorTypeProvider', PaginatorTypeProvider);
})();
(function(){
'use strict';
/**
* @ngdoc directive
* @name mdtCell
* @restrict E
* @requires mdtTable
* @requires mdtRow
*
* @description
* Representing a cell which should be placed inside `mdt-row` element directive.
*
* @param {boolean=} htmlContent if set to true, then html content can be placed into the content of the directive.
* @param {string=} editableField if set, then content can be editable.
*
* Available modes are:
*
* - "smallEditDialog" - A simple, one-field edit dialog on click
* - "largeEditDialog" - A complex, flexible edit edit dialog on click