UNPKG

7.55 kBJavaScriptView Raw
1var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
2 if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
3 if (ar || !(i in from)) {
4 if (!ar) ar = Array.prototype.slice.call(from, 0, i);
5 ar[i] = from[i];
6 }
7 }
8 return to.concat(ar || Array.prototype.slice.call(from));
9};
10/**
11 * @license
12 *
13 * Use of this source code is governed by an MIT-style license that can be
14 * found in the LICENSE file at https://raw.githubusercontent.com/l-lin/angular-datatables/master/LICENSE
15 */
16import { Directive, ElementRef, Input, Renderer2, ViewContainerRef } from '@angular/core';
17import { Subject } from 'rxjs';
18import * as i0 from "@angular/core";
19var DataTableDirective = /** @class */ (function () {
20 function DataTableDirective(el, vcr, renderer) {
21 this.el = el;
22 this.vcr = vcr;
23 this.renderer = renderer;
24 /**
25 * The DataTable option you pass to configure your table.
26 */
27 this.dtOptions = {};
28 }
29 DataTableDirective.prototype.ngOnInit = function () {
30 var _this = this;
31 if (this.dtTrigger) {
32 this.dtTrigger.subscribe(function (options) {
33 _this.displayTable(options);
34 });
35 }
36 else {
37 this.displayTable(null);
38 }
39 };
40 DataTableDirective.prototype.ngOnDestroy = function () {
41 if (this.dtTrigger) {
42 this.dtTrigger.unsubscribe();
43 }
44 if (this.dt) {
45 this.dt.destroy(true);
46 }
47 };
48 DataTableDirective.prototype.displayTable = function (dtOptions) {
49 var _this = this;
50 // assign new options if provided
51 if (dtOptions) {
52 this.dtOptions = dtOptions;
53 }
54 this.dtInstance = new Promise(function (resolve, reject) {
55 Promise.resolve(_this.dtOptions).then(function (resolvedDTOptions) {
56 // validate object
57 var isTableEmpty = Object.keys(resolvedDTOptions).length === 0 && $('tbody tr', _this.el.nativeElement).length === 0;
58 if (isTableEmpty) {
59 reject('Both the table and dtOptions cannot be empty');
60 return;
61 }
62 // Set a column unique
63 if (resolvedDTOptions.columns) {
64 resolvedDTOptions.columns.forEach(function (col) {
65 var _a;
66 if (((_a = col.id) !== null && _a !== void 0 ? _a : '').trim() === '') {
67 col.id = _this.getColumnUniqueId();
68 }
69 });
70 }
71 // Using setTimeout as a "hack" to be "part" of NgZone
72 setTimeout(function () {
73 // Assign DT properties here
74 var options = {
75 rowCallback: function (row, data, index) {
76 if (resolvedDTOptions.columns) {
77 var columns = resolvedDTOptions.columns;
78 _this.applyNgPipeTransform(row, columns);
79 _this.applyNgRefTemplate(row, columns, data);
80 }
81 // run user specified row callback if provided.
82 if (resolvedDTOptions.rowCallback) {
83 resolvedDTOptions.rowCallback(row, data, index);
84 }
85 }
86 };
87 // merge user's config with ours
88 options = Object.assign({}, resolvedDTOptions, options);
89 _this.dt = $(_this.el.nativeElement).DataTable(options);
90 resolve(_this.dt);
91 });
92 });
93 });
94 };
95 DataTableDirective.prototype.applyNgPipeTransform = function (row, columns) {
96 // Filter columns with pipe declared
97 var colsWithPipe = columns.filter(function (x) { return x.ngPipeInstance && !x.ngTemplateRef; });
98 colsWithPipe.forEach(function (el) {
99 var pipe = el.ngPipeInstance;
100 var pipeArgs = el.ngPipeArgs || [];
101 // find index of column using `data` attr
102 var i = columns.filter(function (c) { return c.visible !== false; }).findIndex(function (e) { return e.id === el.id; });
103 // get <td> element which holds data using index
104 var rowFromCol = row.childNodes.item(i);
105 // Transform data with Pipe and PipeArgs
106 var rowVal = $(rowFromCol).text();
107 var rowValAfter = pipe.transform.apply(pipe, __spreadArray([rowVal], pipeArgs, false));
108 // Apply transformed string to <td>
109 $(rowFromCol).text(rowValAfter);
110 });
111 };
112 DataTableDirective.prototype.applyNgRefTemplate = function (row, columns, data) {
113 var _this = this;
114 // Filter columns using `ngTemplateRef`
115 var colsWithTemplate = columns.filter(function (x) { return x.ngTemplateRef && !x.ngPipeInstance; });
116 colsWithTemplate.forEach(function (el) {
117 var _a = el.ngTemplateRef, ref = _a.ref, context = _a.context;
118 // get <td> element which holds data using index
119 var i = columns.filter(function (c) { return c.visible !== false; }).findIndex(function (e) { return e.id === el.id; });
120 var cellFromIndex = row.childNodes.item(i);
121 // reset cell before applying transform
122 $(cellFromIndex).html('');
123 // render onto DOM
124 // finalize context to be sent to user
125 var _context = Object.assign({}, context, context === null || context === void 0 ? void 0 : context.userData, {
126 adtData: data
127 });
128 var instance = _this.vcr.createEmbeddedView(ref, _context);
129 _this.renderer.appendChild(cellFromIndex, instance.rootNodes[0]);
130 });
131 };
132 DataTableDirective.prototype.getColumnUniqueId = function () {
133 var result = '';
134 var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
135 for (var i = 0; i < 6; i++) {
136 var randomIndex = Math.floor(Math.random() * characters.length);
137 result += characters.charAt(randomIndex);
138 }
139 return result.trim();
140 };
141 DataTableDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.2", ngImport: i0, type: DataTableDirective, deps: [{ token: i0.ElementRef }, { token: i0.ViewContainerRef }, { token: i0.Renderer2 }], target: i0.ɵɵFactoryTarget.Directive });
142 DataTableDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "17.0.2", type: DataTableDirective, selector: "[datatable]", inputs: { dtOptions: "dtOptions", dtTrigger: "dtTrigger" }, ngImport: i0 });
143 return DataTableDirective;
144}());
145export { DataTableDirective };
146i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.2", ngImport: i0, type: DataTableDirective, decorators: [{
147 type: Directive,
148 args: [{
149 selector: '[datatable]'
150 }]
151 }], ctorParameters: function () { return [{ type: i0.ElementRef }, { type: i0.ViewContainerRef }, { type: i0.Renderer2 }]; }, propDecorators: { dtOptions: [{
152 type: Input
153 }], dtTrigger: [{
154 type: Input
155 }] } });
156//# sourceMappingURL=angular-datatables.directive.js.map
\No newline at end of file