This repository has been archived by the owner on Feb 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 489
/
Copy pathangular-datatables.directive.js
117 lines (105 loc) · 4.66 KB
/
angular-datatables.directive.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
'use strict';
angular.module('datatables.directive', ['datatables.instances', 'datatables.renderer', 'datatables.options', 'datatables.util'])
.directive('datatable', dataTable);
/* @ngInject */
function dataTable($q, $http, DTRendererFactory, DTRendererService, DTPropertyUtil) {
return {
restrict: 'A',
scope: {
dtOptions: '=',
dtColumns: '=',
dtColumnDefs: '=',
datatable: '@'
},
compile: compileDirective,
controller: ControllerDirective
};
/* @ngInject */
function compileDirective(tElm) {
var _staticHTML = tElm[0].innerHTML;
return function postLink($scope, $elem, iAttrs, ctrl) {
function handleChanges(newVal, oldVal) {
if (newVal !== oldVal) {
ctrl.render($elem, ctrl.buildOptionsPromise(), _staticHTML);
}
}
// Options can hold heavy data, and other deep/large objects.
// watchcollection can improve this by only watching shallowly
var watchFunction = iAttrs.dtDisableDeepWatchers ? '$watchCollection' : '$watch';
angular.forEach(['dtColumns', 'dtColumnDefs', 'dtOptions'], function(tableDefField) {
$scope[watchFunction].call($scope, tableDefField, handleChanges, true);
});
DTRendererService.showLoading($elem);
ctrl.render($elem, ctrl.buildOptionsPromise(), _staticHTML);
};
}
/* @ngInject */
function ControllerDirective($scope) {
var _dtInstance;
var vm = this;
vm.buildOptionsPromise = buildOptionsPromise;
vm.render = render;
function buildOptionsPromise() {
var defer = $q.defer();
// Build options
$q.all([
$q.when($scope.dtOptions),
$q.when($scope.dtColumns),
$q.when($scope.dtColumnDefs)
]).then(function(results) {
var dtOptions = results[0],
dtColumns = results[1],
dtColumnDefs = results[2];
// Since Angular 1.3, the promise throws a "Maximum call stack size exceeded" when cloning
// See https://github.com/l-lin/angular-datatables/issues/110
DTPropertyUtil.deleteProperty(dtOptions, '$promise');
DTPropertyUtil.deleteProperty(dtColumns, '$promise');
DTPropertyUtil.deleteProperty(dtColumnDefs, '$promise');
var options;
if (angular.isDefined(dtOptions)) {
options = {};
angular.extend(options, dtOptions);
// Set the columns
if (angular.isArray(dtColumns)) {
options.aoColumns = dtColumns;
}
// Set the column defs
if (angular.isArray(dtColumnDefs)) {
options.aoColumnDefs = dtColumnDefs;
}
// HACK to resolve the language source manually instead of DT
// See https://github.com/l-lin/angular-datatables/issues/181
if (options.language && options.language.url) {
var languageDefer = $q.defer();
$http.get(options.language.url).success(function(language) {
languageDefer.resolve(language);
});
options.language = languageDefer.promise;
}
}
return DTPropertyUtil.resolveObjectPromises(options, ['data', 'aaData', 'fnPromise']);
}).then(function(options) {
defer.resolve(options);
});
return defer.promise;
}
function render($elem, optionsPromise, staticHTML) {
optionsPromise.then(function(options) {
DTRendererService.preRender(options);
var isNgDisplay = $scope.datatable && $scope.datatable === 'ng';
// Render dataTable
if (_dtInstance && _dtInstance._renderer) {
_dtInstance._renderer.withOptions(options)
.render($elem, $scope, staticHTML).then(function(dtInstance) {
_dtInstance = dtInstance;
});
} else {
DTRendererFactory.fromOptions(options, isNgDisplay)
.render($elem, $scope, staticHTML).then(function(dtInstance) {
_dtInstance = dtInstance;
});
}
});
}
}
}