1 | var __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 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import { Directive, ElementRef, Input, Renderer2, ViewContainerRef } from '@angular/core';
|
17 | import { Subject } from 'rxjs';
|
18 | import * as i0 from "@angular/core";
|
19 | var DataTableDirective = (function () {
|
20 | function DataTableDirective(el, vcr, renderer) {
|
21 | this.el = el;
|
22 | this.vcr = vcr;
|
23 | this.renderer = renderer;
|
24 | |
25 |
|
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 |
|
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 |
|
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 |
|
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 |
|
72 | setTimeout(function () {
|
73 |
|
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 |
|
82 | if (resolvedDTOptions.rowCallback) {
|
83 | resolvedDTOptions.rowCallback(row, data, index);
|
84 | }
|
85 | }
|
86 | };
|
87 |
|
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 |
|
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 |
|
102 | var i = columns.filter(function (c) { return c.visible !== false; }).findIndex(function (e) { return e.id === el.id; });
|
103 |
|
104 | var rowFromCol = row.childNodes.item(i);
|
105 |
|
106 | var rowVal = $(rowFromCol).text();
|
107 | var rowValAfter = pipe.transform.apply(pipe, __spreadArray([rowVal], pipeArgs, false));
|
108 |
|
109 | $(rowFromCol).text(rowValAfter);
|
110 | });
|
111 | };
|
112 | DataTableDirective.prototype.applyNgRefTemplate = function (row, columns, data) {
|
113 | var _this = this;
|
114 |
|
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 |
|
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 |
|
122 | $(cellFromIndex).html('');
|
123 |
|
124 |
|
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 | }());
|
145 | export { DataTableDirective };
|
146 | i0.ɵɵ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 |
|
\ | No newline at end of file |