forked from lorenzofox3/Smart-Table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Smart-Table.debug.js
944 lines (818 loc) · 38 KB
/
Smart-Table.debug.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
/* Column module */
(function (global, angular) {
"use strict";
var smartTableColumnModule = angular.module('smartTable.column', ['smartTable.templateUrlList']).constant('DefaultColumnConfiguration', {
isSortable: true,
isEditable: false,
type: 'text',
//it is useless to have that empty strings, but it reminds what is available
headerTemplateUrl: '',
map: '',
label: '',
sortPredicate: '',
formatFunction: '',
formatParameter: '',
filterPredicate: '',
cellTemplateUrl: '',
headerClass: '',
cellClass: ''
});
function ColumnProvider(DefaultColumnConfiguration, templateUrlList) {
function Column(config) {
if (!(this instanceof Column)) {
return new Column(config);
}
angular.extend(this, config);
}
this.setDefaultOption = function (option) {
angular.extend(Column.prototype, option);
};
DefaultColumnConfiguration.headerTemplateUrl = templateUrlList.defaultHeader;
this.setDefaultOption(DefaultColumnConfiguration);
this.$get = function () {
return Column;
};
}
ColumnProvider.$inject = ['DefaultColumnConfiguration', 'templateUrlList'];
smartTableColumnModule.provider('Column', ColumnProvider);
//make it global so it can be tested
global.ColumnProvider = ColumnProvider;
})(window, angular);
/* Directives */
(function (angular) {
"use strict";
angular.module('smartTable.directives', ['smartTable.templateUrlList', 'smartTable.templates'])
.directive('smartTable', ['templateUrlList', 'DefaultTableConfiguration', function (templateList, defaultConfig) {
return {
restrict: 'EA',
scope: {
columnCollection: '=columns',
dataCollection: '=rows',
config: '='
},
replace: 'true',
templateUrl: templateList.smartTable,
controller: 'TableCtrl',
link: function (scope, element, attr, ctrl) {
var templateObject;
scope.$watch('config', function (config) {
var newConfig = angular.extend({}, defaultConfig, config),
length = scope.columns !== undefined ? scope.columns.length : 0;
ctrl.setGlobalConfig(newConfig);
//remove the checkbox column if needed
if (newConfig.selectionMode !== 'multiple' || newConfig.displaySelectionCheckbox !== true) {
for (var i = length - 1; i >= 0; i--) {
if (scope.columns[i].isSelectionColumn === true) {
ctrl.removeColumn(i);
}
}
} else {
//add selection box column if required
ctrl.insertColumn({cellTemplateUrl: templateList.selectionCheckbox, headerTemplateUrl: templateList.selectAllCheckbox, isSelectionColumn: true}, 0);
}
}, true);
//insert columns from column config
scope.$watch('columnCollection', function (oldValue, newValue) {
ctrl.clearColumns();
if (scope.columnCollection) {
for (var i = 0, l = scope.columnCollection.length; i < l; i++) {
ctrl.insertColumn(scope.columnCollection[i]);
}
} else {
//or guess data Structure
if (scope.dataCollection && scope.dataCollection.length > 0) {
templateObject = scope.dataCollection[0];
angular.forEach(templateObject, function (value, key) {
if (key[0] != '$') {
ctrl.insertColumn({label: key, map: key});
}
});
}
}
}, true);
//if item are added or removed into the data model from outside the grid
scope.$watch('dataCollection.length', function (oldValue, newValue) {
if (oldValue !== newValue) {
ctrl.sortBy();//it will trigger the refresh... some hack ?
}
});
}
};
}])
//just to be able to select the row
.directive('smartTableDataRow', function () {
return {
require: '^smartTable',
restrict: 'C',
link: function (scope, element, attr, ctrl) {
var _config;
if ((_config = scope.config) != null) {
if (typeof _config.rowFunction === "function") {
_config.rowFunction(scope, element, attr, ctrl);
}
}
element.bind('click', function () {
scope.$apply(function () {
ctrl.toggleSelection(scope.dataRow);
})
});
}
};
})
//header cell with sorting functionality or put a checkbox if this column is a selection column
.directive('smartTableHeaderCell',function () {
return {
restrict: 'C',
require: '^smartTable',
link: function (scope, element, attr, ctrl) {
element.bind('click', function () {
scope.$apply(function () {
ctrl.sortBy(scope.column);
});
})
}
};
}).directive('smartTableSelectAll', function () {
return {
restrict: 'C',
require: '^smartTable',
link: function (scope, element, attr, ctrl) {
element.bind('click', function (event) {
ctrl.toggleSelectionAll(element[0].checked === true);
})
}
};
})
//credit to Valentyn shybanov : http://stackoverflow.com/questions/14544741/angularjs-directive-to-stoppropagation
.directive('stopEvent', function () {
return {
restrict: 'A',
link: function (scope, element, attr) {
element.bind(attr.stopEvent, function (e) {
e.stopPropagation();
});
}
}
})
//the global filter
.directive('smartTableGlobalSearch', ['templateUrlList', function (templateList) {
return {
restrict: 'C',
require: '^smartTable',
scope: {
columnSpan: '@'
},
templateUrl: templateList.smartTableGlobalSearch,
replace: false,
link: function (scope, element, attr, ctrl) {
scope.searchValue = '';
scope.$watch('searchValue', function (value) {
//todo perf improvement only filter on blur ?
ctrl.search(value);
});
}
}
}])
//a customisable cell (see templateUrl) and editable
//TODO check with the ng-include strategy
.directive('smartTableDataCell', ['$filter', '$http', '$templateCache', '$compile', '$parse', function (filter, http, templateCache, compile, parse) {
return {
restrict: 'C',
link: function (scope, element) {
var
column = scope.column,
isSimpleCell = !column.isEditable,
row = scope.dataRow,
format = filter('format'),
getter = parse(column.map),
childScope;
//can be useful for child directives
scope.$watch('dataRow', function (value) {
scope.formatedValue = format(getter(row), column.formatFunction, column.formatParameter);
if (isSimpleCell === true) {
element.text(scope.formatedValue);
}
}, true);
function defaultContent() {
if (column.isEditable) {
element.html('<div editable-cell="" row="dataRow" column="column" type="column.type"></div>');
compile(element.contents())(scope);
} else {
element.text(scope.formatedValue);
}
}
scope.$watch('column.cellTemplateUrl', function (value) {
if (value) {
//we have to load the template (and cache it) : a kind of ngInclude
http.get(value, {cache: templateCache}).success(function (response) {
isSimpleCell = false;
//create a scope
childScope = scope.$new();
//compile the element with its new content and new scope
element.html(response);
compile(element.contents())(childScope);
}).error(defaultContent);
} else {
defaultContent();
}
});
}
};
}])
//directive that allows type to be bound in input
.directive('inputType', function () {
return {
restrict: 'A',
priority: 1,
link: function (scope, ielement, iattr) {
//force the type to be set before inputDirective is called
var type = scope.$eval(iattr.type);
iattr.$set('type', type);
}
};
})
//an editable content in the context of a cell (see row, column)
.directive('editableCell', ['templateUrlList', '$parse', function (templateList, parse) {
return {
restrict: 'EA',
require: '^smartTable',
templateUrl: templateList.editableCell,
scope: {
row: '=',
column: '=',
type: '='
},
replace: true,
link: function (scope, element, attrs, ctrl) {
var form = angular.element(element.children()[1]),
input = angular.element(form.children()[0]),
getter = parse(scope.column.map);
//init values
scope.isEditMode = false;
scope.$watch('row', function () {
scope.value = getter(scope.row);
}, true);
scope.submit = function () {
//update model if valid
if (scope.myForm.$valid === true) {
ctrl.updateDataRow(scope.row, scope.column.map, scope.value);
ctrl.sortBy();//it will trigger the refresh... (ie it will sort, filter, etc with the new value)
}
scope.toggleEditMode();
};
scope.toggleEditMode = function () {
scope.value = getter(scope.row);
scope.isEditMode = scope.isEditMode !== true;
};
scope.$watch('isEditMode', function (newValue) {
if (newValue === true) {
input[0].select();
input[0].focus();
}
});
input.bind('blur', function () {
scope.$apply(function () {
scope.submit();
});
});
}
};
}]);
})(angular);
/* Filters */
(function (angular) {
"use strict";
angular.module('smartTable.filters', []).
constant('DefaultFilters', ['currency', 'date', 'json', 'lowercase', 'number', 'uppercase']).
filter('format', ['$filter', 'DefaultFilters', function (filter, defaultfilters) {
return function (value, formatFunction, filterParameter) {
var returnFunction;
if (formatFunction && angular.isFunction(formatFunction)) {
returnFunction = formatFunction;
} else {
returnFunction = defaultfilters.indexOf(formatFunction) !== -1 ? filter(formatFunction) : function (value) {
return value;
};
}
return returnFunction(value, filterParameter);
};
}]);
})(angular);
/*table module */
(function (angular) {
"use strict";
angular.module('smartTable.table', ['smartTable.column', 'smartTable.utilities', 'smartTable.directives', 'smartTable.filters', 'ui.bootstrap.pagination.smartTable'])
.constant('DefaultTableConfiguration', {
selectionMode: 'none',
isGlobalSearchActivated: false,
displaySelectionCheckbox: false,
isPaginationEnabled: true,
itemsByPage: 10,
maxSize: 5,
//just to remind available option
sortAlgorithm: '',
filterAlgorithm: ''
})
.controller('TableCtrl', ['$scope', 'Column', '$filter', '$parse', 'ArrayUtility', 'DefaultTableConfiguration', function (scope, Column, filter, parse, arrayUtility, defaultConfig) {
scope.columns = [];
scope.dataCollection = scope.dataCollection || [];
scope.displayedCollection = []; //init empty array so that if pagination is enabled, it does not spoil performances
scope.numberOfPages = calculateNumberOfPages(scope.dataCollection);
scope.currentPage = 1;
scope.holder = {isAllSelected: false};
var predicate = {},
lastColumnSort;
function isAllSelected() {
var i,
l = scope.displayedCollection.length;
for (i = 0; i < l; i++) {
if (scope.displayedCollection[i].isSelected !== true) {
return false;
}
}
return true;
}
function calculateNumberOfPages(array) {
if (!angular.isArray(array) || array.length === 0 || scope.itemsByPage < 1) {
return 1;
}
return Math.ceil(array.length / scope.itemsByPage);
}
function sortDataRow(array, column) {
var sortAlgo = (scope.sortAlgorithm && angular.isFunction(scope.sortAlgorithm)) === true ? scope.sortAlgorithm : filter('orderBy');
if (column) {
return arrayUtility.sort(array, sortAlgo, column.sortPredicate, column.reverse);
} else {
return array;
}
}
function selectDataRow(array, selectionMode, index, select) {
var dataRow, oldValue;
if ((!angular.isArray(array)) || (selectionMode !== 'multiple' && selectionMode !== 'single')) {
return;
}
if (index >= 0 && index < array.length) {
dataRow = array[index];
if (selectionMode === 'single') {
//unselect all the others
for (var i = 0, l = array.length; i < l; i++) {
oldValue = array[i].isSelected;
array[i].isSelected = false;
if (oldValue === true) {
scope.$emit('selectionChange', {item: array[i]});
}
}
}
dataRow.isSelected = select;
scope.holder.isAllSelected = isAllSelected();
scope.$emit('selectionChange', {item: dataRow});
}
}
/**
* set the config (config parameters will be available through scope
* @param config
*/
this.setGlobalConfig = function (config) {
angular.extend(scope, defaultConfig, config);
};
/**
* change the current page displayed
* @param page
*/
this.changePage = function (page) {
var oldPage = scope.currentPage;
if (angular.isNumber(page.page)) {
scope.currentPage = page.page;
scope.displayedCollection = this.pipe(scope.dataCollection);
scope.holder.isAllSelected = isAllSelected();
scope.$emit('changePage', {oldValue: oldPage, newValue: scope.currentPage});
}
};
/**
* set column as the column used to sort the data (if it is already the case, it will change the reverse value)
* @method sortBy
* @param column
*/
this.sortBy = function (column) {
var index = scope.columns.indexOf(column);
if (index !== -1) {
if (column.isSortable === true) {
// reset the last column used
if (lastColumnSort && lastColumnSort !== column) {
lastColumnSort.reverse = 'none';
}
column.sortPredicate = column.sortPredicate || column.map;
column.reverse = column.reverse !== true;
lastColumnSort = column;
}
}
scope.displayedCollection = this.pipe(scope.dataCollection);
};
/**
* set the filter predicate used for searching
* @param input
* @param column
*/
this.search = function (input, column) {
//update column and global predicate
if (column && scope.columns.indexOf(column) !== -1) {
predicate[column.map] = input;
} else {
predicate = {$: input};
}
scope.displayedCollection = this.pipe(scope.dataCollection);
};
/**
* combine sort, search and limitTo operations on an array,
* @param array
* @returns Array, an array result of the operations on input array
*/
this.pipe = function (array) {
var filterAlgo = (scope.filterAlgorithm && angular.isFunction(scope.filterAlgorithm)) === true ? scope.filterAlgorithm : filter('filter'),
output;
//filter and sort are commutative
output = sortDataRow(arrayUtility.filter(array, filterAlgo, predicate), lastColumnSort);
scope.numberOfPages = calculateNumberOfPages(output);
return scope.isPaginationEnabled ? arrayUtility.fromTo(output, (scope.currentPage - 1) * scope.itemsByPage, scope.itemsByPage) : output;
};
/*////////////
Column API
///////////*/
/**
* insert a new column in scope.collection at index or push at the end if no index
* @param columnConfig column configuration used to instantiate the new Column
* @param index where to insert the column (at the end if not specified)
*/
this.insertColumn = function (columnConfig, index) {
var column = new Column(columnConfig);
arrayUtility.insertAt(scope.columns, index, column);
};
/**
* remove the column at columnIndex from scope.columns
* @param columnIndex index of the column to be removed
*/
this.removeColumn = function (columnIndex) {
arrayUtility.removeAt(scope.columns, columnIndex);
};
/**
* move column located at oldIndex to the newIndex in scope.columns
* @param oldIndex index of the column before it is moved
* @param newIndex index of the column after the column is moved
*/
this.moveColumn = function (oldIndex, newIndex) {
arrayUtility.moveAt(scope.columns, oldIndex, newIndex);
};
/**
* remove all columns
*/
this.clearColumns = function () {
scope.columns.length = 0;
};
/*///////////
ROW API
*/
/**
* select or unselect the item of the displayedCollection with the selection mode set in the scope
* @param dataRow
*/
this.toggleSelection = function (dataRow) {
var index = scope.dataCollection.indexOf(dataRow);
if (index !== -1) {
selectDataRow(scope.dataCollection, scope.selectionMode, index, dataRow.isSelected !== true);
}
};
/**
* select/unselect all the currently displayed rows
* @param value if true select, else unselect
*/
this.toggleSelectionAll = function (value) {
var i = 0,
l = scope.displayedCollection.length;
if (scope.selectionMode !== 'multiple') {
return;
}
for (; i < l; i++) {
selectDataRow(scope.displayedCollection, scope.selectionMode, i, value === true);
}
};
/**
* remove the item at index rowIndex from the displayed collection
* @param rowIndex
* @returns {*} item just removed or undefined
*/
this.removeDataRow = function (rowIndex) {
var toRemove = arrayUtility.removeAt(scope.displayedCollection, rowIndex);
arrayUtility.removeAt(scope.dataCollection, scope.dataCollection.indexOf(toRemove));
};
/**
* move an item from oldIndex to newIndex in displayedCollection
* @param oldIndex
* @param newIndex
*/
this.moveDataRow = function (oldIndex, newIndex) {
arrayUtility.moveAt(scope.displayedCollection, oldIndex, newIndex);
};
/**
* update the model, it can be a non existing yet property
* @param dataRow the dataRow to update
* @param propertyName the property on the dataRow ojbect to update
* @param newValue the value to set
*/
this.updateDataRow = function (dataRow, propertyName, newValue) {
var index = scope.displayedCollection.indexOf(dataRow),
getter = parse(propertyName),
setter = getter.assign,
oldValue;
if (index !== -1) {
oldValue = getter(scope.displayedCollection[index]);
if (oldValue !== newValue) {
setter(scope.displayedCollection[index], newValue);
scope.$emit('updateDataRow', {item: scope.displayedCollection[index]});
}
}
};
}]);
})(angular);
angular.module('smartTable.templates', ['partials/defaultCell.html', 'partials/defaultHeader.html', 'partials/editableCell.html', 'partials/globalSearchCell.html', 'partials/pagination.html', 'partials/selectAllCheckbox.html', 'partials/selectionCheckbox.html', 'partials/smartTable.html']);
angular.module("partials/defaultCell.html", []).run(["$templateCache", function($templateCache) {
$templateCache.put("partials/defaultCell.html",
"{{formatedValue}}");
}]);
angular.module("partials/defaultHeader.html", []).run(["$templateCache", function($templateCache) {
$templateCache.put("partials/defaultHeader.html",
"<span class=\"header-content\" ng-class=\"{'sort-ascent':column.reverse==false,'sort-descent':column.reverse==true}\">{{column.label}}</span>");
}]);
angular.module("partials/editableCell.html", []).run(["$templateCache", function($templateCache) {
$templateCache.put("partials/editableCell.html",
"<div ng-dblclick=\"toggleEditMode($event)\">\n" +
" <span ng-hide=\"isEditMode\">{{value | format:column.formatFunction:column.formatParameter}}</span>\n" +
"\n" +
" <form ng-submit=\"submit()\" ng-show=\"isEditMode\" name=\"myForm\">\n" +
" <input name=\"myInput\" ng-model=\"value\" type=\"type\" input-type/>\n" +
" </form>\n" +
"</div>");
}]);
angular.module("partials/globalSearchCell.html", []).run(["$templateCache", function($templateCache) {
$templateCache.put("partials/globalSearchCell.html",
"<label>Search :</label>\n" +
"<input type=\"text\" ng-model=\"searchValue\"/>");
}]);
angular.module("partials/pagination.html", []).run(["$templateCache", function($templateCache) {
$templateCache.put("partials/pagination.html",
"<div class=\"pagination\">\n" +
" <ul>\n" +
" <li ng-repeat=\"page in pages\" ng-class=\"{active: page.active, disabled: page.disabled}\"><a\n" +
" ng-click=\"selectPage(page.number)\">{{page.text}}</a></li>\n" +
" </ul>\n" +
"</div> ");
}]);
angular.module("partials/selectAllCheckbox.html", []).run(["$templateCache", function($templateCache) {
$templateCache.put("partials/selectAllCheckbox.html",
"<input class=\"smart-table-select-all\" type=\"checkbox\" ng-model=\"holder.isAllSelected\"/>");
}]);
angular.module("partials/selectionCheckbox.html", []).run(["$templateCache", function($templateCache) {
$templateCache.put("partials/selectionCheckbox.html",
"<input type=\"checkbox\" ng-model=\"dataRow.isSelected\" stop-event=\"click\"/>");
}]);
angular.module("partials/smartTable.html", []).run(["$templateCache", function($templateCache) {
$templateCache.put("partials/smartTable.html",
"<table class=\"smart-table\">\n" +
" <thead>\n" +
" <tr class=\"smart-table-global-search-row\" ng-show=\"isGlobalSearchActivated\">\n" +
" <td class=\"smart-table-global-search\" column-span=\"{{columns.length}}\" colspan=\"{{columnSpan}}\">\n" +
" </td>\n" +
" </tr>\n" +
" <tr class=\"smart-table-header-row\">\n" +
" <th ng-repeat=\"column in columns\" ng-include=\"column.headerTemplateUrl\"\n" +
" class=\"smart-table-header-cell {{column.headerClass}}\" scope=\"col\">\n" +
" </th>\n" +
" </tr>\n" +
" </thead>\n" +
" <tbody>\n" +
" <tr ng-repeat=\"dataRow in displayedCollection\" ng-class=\"{selected:dataRow.isSelected}\"\n" +
" class=\"smart-table-data-row\">\n" +
" <td ng-repeat=\"column in columns\" class=\"smart-table-data-cell {{column.cellClass}}\"></td>\n" +
" </tr>\n" +
" </tbody>\n" +
" <tfoot ng-show=\"isPaginationEnabled\">\n" +
" <tr class=\"smart-table-footer-row\">\n" +
" <td colspan=\"{{columns.length}}\">\n" +
" <div pagination-smart-table=\"\" num-pages=\"numberOfPages\" max-size=\"maxSize\" current-page=\"currentPage\"></div>\n" +
" </td>\n" +
" </tr>\n" +
" </tfoot>\n" +
"</table>\n" +
"\n" +
"\n" +
"");
}]);
(function (angular) {
"use strict";
angular.module('smartTable.templateUrlList', [])
.constant('templateUrlList', {
smartTable: 'partials/smartTable.html',
smartTableGlobalSearch: 'partials/globalSearchCell.html',
editableCell: 'partials/editableCell.html',
selectionCheckbox: 'partials/selectionCheckbox.html',
selectAllCheckbox: 'partials/selectAllCheckbox.html',
defaultHeader: 'partials/defaultHeader.html',
pagination: 'partials/pagination.html'
});
})(angular);
(function (angular) {
"use strict";
angular.module('smartTable.utilities', [])
.factory('ArrayUtility', function () {
/**
* remove the item at index from arrayRef and return the removed item
* @param arrayRef
* @param index
* @returns {*}
*/
var removeAt = function (arrayRef, index) {
if (index >= 0 && index < arrayRef.length) {
return arrayRef.splice(index, 1)[0];
}
},
/**
* insert item in arrayRef at index or a the end if index is wrong
* @param arrayRef
* @param index
* @param item
*/
insertAt = function (arrayRef, index, item) {
if (index >= 0 && index < arrayRef.length) {
arrayRef.splice(index, 0, item);
} else {
arrayRef.push(item);
}
},
/**
* move the item at oldIndex to newIndex in arrayRef
* @param arrayRef
* @param oldIndex
* @param newIndex
*/
moveAt = function (arrayRef, oldIndex, newIndex) {
var elementToMove;
if (oldIndex >= 0 && oldIndex < arrayRef.length && newIndex >= 0 && newIndex < arrayRef.length) {
elementToMove = arrayRef.splice(oldIndex, 1)[0];
arrayRef.splice(newIndex, 0, elementToMove);
}
},
/**
* sort arrayRef according to sortAlgorithm following predicate and reverse
* @param arrayRef
* @param sortAlgorithm
* @param predicate
* @param reverse
* @returns {*}
*/
sort = function (arrayRef, sortAlgorithm, predicate, reverse) {
if (!sortAlgorithm || !angular.isFunction(sortAlgorithm)) {
return arrayRef;
} else {
return sortAlgorithm(arrayRef, predicate, reverse === true);//excpet if reverse is true it will take it as false
}
},
/**
* filter arrayRef according with filterAlgorithm and predicate
* @param arrayRef
* @param filterAlgorithm
* @param predicate
* @returns {*}
*/
filter = function (arrayRef, filterAlgorithm, predicate) {
if (!filterAlgorithm || !angular.isFunction(filterAlgorithm)) {
return arrayRef;
} else {
return filterAlgorithm(arrayRef, predicate);
}
},
/**
* return an array, part of array ref starting at min and the size of length
* @param arrayRef
* @param min
* @param length
* @returns {*}
*/
fromTo = function (arrayRef, min, length) {
var out = [],
limit,
start;
if (!angular.isArray(arrayRef)) {
return arrayRef;
}
start = Math.max(min, 0);
start = Math.min(start, (arrayRef.length - 1) > 0 ? arrayRef.length - 1 : 0);
length = Math.max(0, length);
limit = Math.min(start + length, arrayRef.length);
for (var i = start; i < limit; i++) {
out.push(arrayRef[i]);
}
return out;
};
return {
removeAt: removeAt,
insertAt: insertAt,
moveAt: moveAt,
sort: sort,
filter: filter,
fromTo: fromTo
};
});
})(angular);
(function (angular) {
angular.module('ui.bootstrap.pagination.smartTable', ['smartTable.templateUrlList'])
.constant('paginationConfig', {
boundaryLinks: false,
directionLinks: true,
firstText: 'First',
previousText: '<',
nextText: '>',
lastText: 'Last'
})
.directive('paginationSmartTable', ['paginationConfig', 'templateUrlList', function (paginationConfig, templateUrlList) {
return {
restrict: 'EA',
require: '^smartTable',
scope: {
numPages: '=',
currentPage: '=',
maxSize: '='
},
templateUrl: templateUrlList.pagination,
replace: true,
link: function (scope, element, attrs, ctrl) {
// Setup configuration parameters
var boundaryLinks = angular.isDefined(attrs.boundaryLinks) ? scope.$eval(attrs.boundaryLinks) : paginationConfig.boundaryLinks;
var directionLinks = angular.isDefined(attrs.directionLinks) ? scope.$eval(attrs.directionLinks) : paginationConfig.directionLinks;
var firstText = angular.isDefined(attrs.firstText) ? attrs.firstText : paginationConfig.firstText;
var previousText = angular.isDefined(attrs.previousText) ? attrs.previousText : paginationConfig.previousText;
var nextText = angular.isDefined(attrs.nextText) ? attrs.nextText : paginationConfig.nextText;
var lastText = angular.isDefined(attrs.lastText) ? attrs.lastText : paginationConfig.lastText;
// Create page object used in template
function makePage(number, text, isActive, isDisabled) {
return {
number: number,
text: text,
active: isActive,
disabled: isDisabled
};
}
scope.$watch('numPages + currentPage + maxSize', function () {
scope.pages = [];
// Default page limits
var startPage = 1, endPage = scope.numPages;
// recompute if maxSize
if (scope.maxSize && scope.maxSize < scope.numPages) {
startPage = Math.max(scope.currentPage - Math.floor(scope.maxSize / 2), 1);
endPage = startPage + scope.maxSize - 1;
// Adjust if limit is exceeded
if (endPage > scope.numPages) {
endPage = scope.numPages;
startPage = endPage - scope.maxSize + 1;
}
}
// Add page number links
for (var number = startPage; number <= endPage; number++) {
var page = makePage(number, number, scope.isActive(number), false);
scope.pages.push(page);
}
// Add previous & next links
if (directionLinks) {
var previousPage = makePage(scope.currentPage - 1, previousText, false, scope.noPrevious());
scope.pages.unshift(previousPage);
var nextPage = makePage(scope.currentPage + 1, nextText, false, scope.noNext());
scope.pages.push(nextPage);
}
// Add first & last links
if (boundaryLinks) {
var firstPage = makePage(1, firstText, false, scope.noPrevious());
scope.pages.unshift(firstPage);
var lastPage = makePage(scope.numPages, lastText, false, scope.noNext());
scope.pages.push(lastPage);
}
if (scope.currentPage > scope.numPages) {
scope.selectPage(scope.numPages);
}
});
scope.noPrevious = function () {
return scope.currentPage === 1;
};
scope.noNext = function () {
return scope.currentPage === scope.numPages;
};
scope.isActive = function (page) {
return scope.currentPage === page;
};
scope.selectPage = function (page) {
if (!scope.isActive(page) && page > 0 && page <= scope.numPages) {
scope.currentPage = page;
ctrl.changePage({ page: page });
}
};
}
};
}]);
})(angular);