1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
12 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
13 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
14 | function verb(n) { return function (v) { return step([n, v]); }; }
|
15 | function step(op) {
|
16 | if (f) throw new TypeError("Generator is already executing.");
|
17 | while (_) try {
|
18 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
19 | if (y = 0, t) op = [op[0] & 2, t.value];
|
20 | switch (op[0]) {
|
21 | case 0: case 1: t = op; break;
|
22 | case 4: _.label++; return { value: op[1], done: false };
|
23 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
24 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
25 | default:
|
26 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
27 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
28 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
29 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
30 | if (t[2]) _.ops.pop();
|
31 | _.trys.pop(); continue;
|
32 | }
|
33 | op = body.call(thisArg, _);
|
34 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
35 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
36 | }
|
37 | };
|
38 | Object.defineProperty(exports, "__esModule", { value: true });
|
39 | exports.DatatableConfiguration = exports.PaginationConfiguration = exports.TableConfiguration = exports.Element = void 0;
|
40 | var Element = (function () {
|
41 | function Element() {
|
42 | }
|
43 | return Element;
|
44 | }());
|
45 | exports.Element = Element;
|
46 | var TableConfiguration = (function () {
|
47 | function TableConfiguration() {
|
48 | this._class = "";
|
49 | this.ordersBy = [];
|
50 | this.header = [];
|
51 | this.data = [];
|
52 | }
|
53 | TableConfiguration.prototype.addClass = function (_class) {
|
54 | this._class += _class + " ";
|
55 | return this;
|
56 | };
|
57 | TableConfiguration.prototype.addHeader = function (text, _class) {
|
58 | this.header.push({ text: text, _class: _class ? _class : "" });
|
59 | this.ordersBy.push(0);
|
60 | return this;
|
61 | };
|
62 | TableConfiguration.prototype.addData = function (texts, _classes) {
|
63 | var c = [];
|
64 | for (var i = 0; i < texts.length; i++) {
|
65 | c.push({
|
66 | text: texts[i],
|
67 | _class: _classes && _classes[i] ? _classes[i] : ""
|
68 | });
|
69 | }
|
70 | this.data.push(c);
|
71 | return this;
|
72 | };
|
73 | return TableConfiguration;
|
74 | }());
|
75 | exports.TableConfiguration = TableConfiguration;
|
76 | var PaginationConfiguration = (function () {
|
77 | function PaginationConfiguration() {
|
78 | this.total = 0;
|
79 | this.pageSize = 10;
|
80 | this.pageCount = 0;
|
81 | this.currentPage = 0;
|
82 | this.left = 0;
|
83 | this.right = 0;
|
84 | this.maxPages = 2;
|
85 | this._class = "";
|
86 | }
|
87 | PaginationConfiguration.prototype.calculate = function () {
|
88 | this.pageCount = Math.ceil(this.total / this.pageSize);
|
89 | this.left = Math.max(0, this.currentPage - this.maxPages);
|
90 | this.right = Math.min(this.pageCount, this.currentPage + 1 + this.maxPages);
|
91 | };
|
92 | PaginationConfiguration.prototype.addClass = function (_class) {
|
93 | this._class += _class + " ";
|
94 | return this;
|
95 | };
|
96 | return PaginationConfiguration;
|
97 | }());
|
98 | exports.PaginationConfiguration = PaginationConfiguration;
|
99 | var DatatableConfiguration = (function () {
|
100 | function DatatableConfiguration(repository, req, alias) {
|
101 | this.table = new TableConfiguration();
|
102 | this._classes = [];
|
103 | this.mapTransformers = {};
|
104 | this.pagination = new PaginationConfiguration();
|
105 | this.alias = "e";
|
106 | this.repository = repository;
|
107 | this.req = req;
|
108 | this.setupPageRequested();
|
109 | this.urlNoPage = getUrlWithoutPage(this.req);
|
110 | this.urlNoOrder = getUrlWithoutOrder(this.req);
|
111 | this.urlNoPageNoOrder = getUrlWithoutPageOrOrder(this.req);
|
112 | this.urlNoFilter = getUrlWithoutFilter(this.req);
|
113 | if (alias) {
|
114 | this.alias = alias;
|
115 | }
|
116 | }
|
117 | DatatableConfiguration.prototype.setPageSize = function (size) {
|
118 | this.pagination.pageSize = size;
|
119 | };
|
120 | DatatableConfiguration.prototype.addTableClass = function (_class) {
|
121 | this.table.addClass(_class);
|
122 | return this;
|
123 | };
|
124 | DatatableConfiguration.prototype.addTableHeader = function (text, _class) {
|
125 | this.table.addHeader(text, _class);
|
126 | return this;
|
127 | };
|
128 | DatatableConfiguration.prototype.addDataMap = function (map, _classes) {
|
129 | this.map = map;
|
130 | if (_classes) {
|
131 | this._classes = _classes;
|
132 | }
|
133 | else {
|
134 | this._classes = [];
|
135 | for (var i = 0; i < this.map.length; i++) {
|
136 | this._classes.push("");
|
137 | }
|
138 | }
|
139 | return this;
|
140 | };
|
141 | DatatableConfiguration.prototype.addDataTransformer = function (label, exe) {
|
142 | this.mapTransformers[label] = exe;
|
143 | return this;
|
144 | };
|
145 | DatatableConfiguration.prototype.addPaginationClass = function (_class) {
|
146 | this.pagination.addClass(_class);
|
147 | return this;
|
148 | };
|
149 | DatatableConfiguration.prototype.setupPageRequested = function () {
|
150 | this.pagination.currentPage = 0;
|
151 | if (this.req.query.page) {
|
152 | this.pagination.currentPage = Number(this.req.query.page);
|
153 | if (this.pagination.currentPage > 0) {
|
154 | this.pagination.currentPage -= 1;
|
155 | }
|
156 | }
|
157 | };
|
158 | DatatableConfiguration.prototype.getQueryValue = function (key) {
|
159 | for (var q in this.req.query) {
|
160 | if (q.toLowerCase() !== key) {
|
161 | continue;
|
162 | }
|
163 | if (this.req.query[q] instanceof Array) {
|
164 | return this.req.query[q];
|
165 | }
|
166 | return [this.req.query[q]];
|
167 | }
|
168 | return [];
|
169 | };
|
170 | DatatableConfiguration.prototype.addOrderBy = function (query) {
|
171 | var ordersBy = this.getQueryValue("orderby");
|
172 | if (ordersBy.length > 0) {
|
173 | var order = ordersBy[0].split(":");
|
174 | this.setOrderBy(order[0], order[1]);
|
175 | query = query.orderBy(this.alias + "." + order[0], order[1] == "desc" ? "DESC" : "ASC");
|
176 | for (var i = 1; i < ordersBy.length; i++) {
|
177 | var order_1 = ordersBy[i].split(":");
|
178 | this.setOrderBy(order_1[0], order_1[1]);
|
179 | query = query.addOrderBy(this.alias + "." + order_1[0], order_1[1] == "desc" ? "DESC" : "ASC");
|
180 | }
|
181 | }
|
182 | return query;
|
183 | };
|
184 | DatatableConfiguration.prototype.setOrderBy = function (column, order) {
|
185 | for (var i = 0; i < this.map.length; i++) {
|
186 | if (this.map[i] == column) {
|
187 | this.table.ordersBy[i] = order == "desc" ? -1 : 1;
|
188 | }
|
189 | }
|
190 | };
|
191 | DatatableConfiguration.prototype.addFilterBy = function (query) {
|
192 | var filtersBy = this.getQueryValue("filterby");
|
193 | if (filtersBy.length) {
|
194 | var f = filtersBy[0].split(":");
|
195 | var o = {};
|
196 | o[f[0] + "_" + 0] = f[1];
|
197 | query = query.where(this.alias + "." + f[0] + " = :" + f[0] + "_" + 0, o);
|
198 | for (var i = 1; i < filtersBy.length; i++) {
|
199 | var f_1 = filtersBy[i].split(":");
|
200 | var o_1 = {};
|
201 | o_1[f_1[0] + "_" + i] = f_1[1];
|
202 | query = query.andWhere(this.alias + "." + f_1[0] + " = :" + f_1[0] + "_" + i, o_1);
|
203 | }
|
204 | }
|
205 | return query;
|
206 | };
|
207 | DatatableConfiguration.prototype.fetchData = function (query) {
|
208 | return __awaiter(this, void 0, void 0, function () {
|
209 | var select_1, _a, skip, result, _i, result_1, r, line, _b, _c, label, cell;
|
210 | var _this = this;
|
211 | return __generator(this, function (_d) {
|
212 | switch (_d.label) {
|
213 | case 0:
|
214 | if (!query) {
|
215 | select_1 = [];
|
216 | this.map.forEach(function (v) {
|
217 | if (!v.startsWith(":")) {
|
218 | select_1.push(_this.alias + "." + v.replace("-", ""));
|
219 | }
|
220 | });
|
221 | query = this.repository.createQueryBuilder(this.alias).select(select_1);
|
222 | }
|
223 | query = this.addFilterBy(query);
|
224 | _a = this.pagination;
|
225 | return [4 , query.getCount()];
|
226 | case 1:
|
227 | _a.total = _d.sent();
|
228 | this.pagination.calculate();
|
229 | query = this.addOrderBy(query);
|
230 | skip = this.pagination.currentPage * this.pagination.pageSize;
|
231 | query = query.skip(skip).take(this.pagination.pageSize);
|
232 | return [4 , query.getMany()];
|
233 | case 2:
|
234 | result = _d.sent();
|
235 | for (_i = 0, result_1 = result; _i < result_1.length; _i++) {
|
236 | r = result_1[_i];
|
237 | line = [];
|
238 | for (_b = 0, _c = this.map; _b < _c.length; _b++) {
|
239 | label = _c[_b];
|
240 | if (label.startsWith("-"))
|
241 | continue;
|
242 | cell = r[label];
|
243 | if (this.mapTransformers[label]) {
|
244 | cell = this.mapTransformers[label](cell, r);
|
245 | }
|
246 | line.push(cell);
|
247 | }
|
248 | this.table.addData(line, this._classes);
|
249 | }
|
250 | return [2 ];
|
251 | }
|
252 | });
|
253 | });
|
254 | };
|
255 | return DatatableConfiguration;
|
256 | }());
|
257 | exports.DatatableConfiguration = DatatableConfiguration;
|
258 | function getUrlWithoutPage(req) {
|
259 | return getUrlWithoutParameter(req, ["page"]);
|
260 | }
|
261 | function getUrlWithoutOrder(req) {
|
262 | return getUrlWithoutParameter(req, ["orderby"]);
|
263 | }
|
264 | function getUrlWithoutPageOrOrder(req) {
|
265 | return getUrlWithoutParameter(req, ["orderby", "page"]);
|
266 | }
|
267 | function getUrlWithoutFilter(req) {
|
268 | return getUrlWithoutParameter(req, ["filter", "filterby"]);
|
269 | }
|
270 | function getUrlWithoutParameter(req, parameters) {
|
271 | var u = (req.baseUrl + req.path).replace(/\/$/, "") + "?";
|
272 | for (var key in req.query) {
|
273 | var found = false;
|
274 | for (var _i = 0, parameters_1 = parameters; _i < parameters_1.length; _i++) {
|
275 | var p = parameters_1[_i];
|
276 | if (key.toLowerCase() == p) {
|
277 | found = true;
|
278 | break;
|
279 | }
|
280 | }
|
281 | if (found) {
|
282 | continue;
|
283 | }
|
284 | u += generateQueryValue(key, req.query[key]);
|
285 | }
|
286 | return u;
|
287 | }
|
288 | function generateQueryValue(key, q) {
|
289 | var m = key + "=";
|
290 | if (q instanceof Array) {
|
291 | m += q[0] + "&";
|
292 | for (var i = 1; i < q.length; i++) {
|
293 | m += key + "=" + q[i] + "&";
|
294 | }
|
295 | return m;
|
296 | }
|
297 | return m + q + "&";
|
298 | }
|