@hotstaq/admin-panel 0.3.1 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,10 +26,34 @@ export class AdminEdit extends HotComponent
26
26
  * The modal id.
27
27
  */
28
28
  modalId: string;
29
+ /**
30
+ * The text for the add button.
31
+ */
32
+ add_text: string;
33
+ /**
34
+ * The location to put the add button.
35
+ */
36
+ add_place_here: string;
37
+ /**
38
+ * The text for the edit button.
39
+ */
40
+ edit_text: string;
41
+ /**
42
+ * The location to put the edit button.
43
+ */
44
+ edit_place_here: string;
45
+ /**
46
+ * The text for the remove button.
47
+ */
48
+ remove_text: string;
49
+ /**
50
+ * The location to put the remove button.
51
+ */
52
+ remove_place_here: string;
29
53
  /**
30
54
  * The hot-class attribute to pass to add to the modal's classes.
31
55
  */
32
- class: string;
56
+ css_class: string;
33
57
  /**
34
58
  * The bootstrap modal instance.
35
59
  */
@@ -50,9 +74,10 @@ export class AdminEdit extends HotComponent
50
74
  */
51
75
  protected selectedFields: any[];
52
76
  /**
53
- * What to execute when the save button is clicked.
77
+ * What to execute when the save button is clicked. If this returns true, the modal will close.
78
+ * If this returns false, the modal will not close.
54
79
  */
55
- onsave: (modalType: string, values: any, selectedFields: any[]) => Promise<void>;
80
+ onsave: (modalType: string, values: any, selectedFields: any[]) => Promise<boolean>;
56
81
  /**
57
82
  * What to execute when the edit button is clicked.
58
83
  */
@@ -77,7 +102,13 @@ export class AdminEdit extends HotComponent
77
102
  this.schema = "";
78
103
 
79
104
  this.modalId = "";
80
- this.class = "";
105
+ this.add_text = "Add";
106
+ this.add_place_here = "dashboardHeader";
107
+ this.edit_text = "Edit";
108
+ this.edit_place_here = "dashboardHeader";
109
+ this.remove_text = "Remove";
110
+ this.remove_place_here = "dashboardHeader";
111
+ this.css_class = "";
81
112
  this.modal = null;
82
113
  this.modalType = "add";
83
114
  this.closeOnSave = true;
@@ -90,7 +121,8 @@ export class AdminEdit extends HotComponent
90
121
  /**
91
122
  * Process the hot fields.
92
123
  */
93
- async processHotFields (onField: (htmlElement: Element, field: { name: string; type: string; }) => Promise<void>): Promise<void>
124
+ async processHotFields (onField: (htmlElement: Element,
125
+ field: { name: string; type: string; input: string; }) => Promise<void>): Promise<void>
94
126
  {
95
127
  const elms = this.htmlElements[1].querySelectorAll ("[hot-field]");
96
128
 
@@ -99,6 +131,7 @@ export class AdminEdit extends HotComponent
99
131
  const elm = elms[iIdx];
100
132
  const field = $(elm).attr ("hot-field");
101
133
  let fieldType = $(elm).attr ("hot-field-type");
134
+ let fieldInput = $(elm).attr ("hot-field-input");
102
135
 
103
136
  if ((fieldType == null) || (fieldType === ""))
104
137
  fieldType = "text";
@@ -106,7 +139,7 @@ export class AdminEdit extends HotComponent
106
139
  // @ts-ignore
107
140
  elm.fieldType = fieldType;
108
141
 
109
- await onField (elm, { name: field, type: fieldType });
142
+ await onField (elm, { name: field, type: fieldType, input: fieldInput });
110
143
  }
111
144
  }
112
145
 
@@ -176,42 +209,57 @@ export class AdminEdit extends HotComponent
176
209
  }
177
210
 
178
211
  this.modalType = "edit";
179
- let attachedList = document.getElementById (this.attached_list);
180
212
 
181
- // @ts-ignore
182
- let hotComponent: AdminTable = attachedList.hotComponent;
183
- let selectedField = hotComponent.getSelected ();
213
+ this.selectedFields = [];
184
214
 
185
- if (selectedField != null)
215
+ if (this.attached_list !== "")
186
216
  {
187
- await this.processHotFields (
188
- async (htmlElement: Element, field: { name: string; type: string; }) =>
189
- {
190
- // @ts-ignore
191
- const value = selectedField[field.name];
192
-
193
- if (value != null)
194
- $(htmlElement).val (value);
195
- });
217
+ let attachedList = document.getElementById (this.attached_list);
196
218
 
197
- this.selectedFields = [selectedField];
219
+ // @ts-ignore
220
+ let hotComponent: AdminTable = attachedList.hotComponent;
221
+ let selectedField = hotComponent.getSelected ();
198
222
 
199
- if (this.onedit != null)
223
+ if (selectedField != null)
200
224
  {
201
- if (typeof (this.onedit) === "string")
202
- this.onedit = (<(modalType: string, selectedFields: any[]) => Promise<boolean>>new Function (this.onedit));
225
+ await this.processHotFields (
226
+ async (htmlElement: Element, field: { name: string; type: string; input: string; }) =>
227
+ {
228
+ // @ts-ignore
229
+ let value = selectedField[field.name];
230
+
231
+ if (field.input != null)
232
+ {
233
+ if (field.input !== "")
234
+ {
235
+ let func = new Function (field.input);
236
+ value = await func.call (this, value);
237
+ }
238
+ }
203
239
 
204
- let result = await this.onedit (this.modalType, this.selectedFields);
240
+ if (value != null)
241
+ $(htmlElement).val (value);
242
+ });
205
243
 
206
- if (result != null)
207
- {
208
- if (result === false)
209
- return;
210
- }
244
+ this.selectedFields = [selectedField];
211
245
  }
246
+ }
212
247
 
213
- bootstrap.Modal.getInstance (`#${this.modalId}`).show ();
248
+ if (this.onedit != null)
249
+ {
250
+ if (typeof (this.onedit) === "string")
251
+ this.onedit = (<(modalType: string, selectedFields: any[]) => Promise<boolean>>new Function (this.onedit));
252
+
253
+ let result = await this.onedit (this.modalType, this.selectedFields);
254
+
255
+ if (result != null)
256
+ {
257
+ if (result === false)
258
+ return;
259
+ }
214
260
  }
261
+
262
+ bootstrap.Modal.getInstance (`#${this.modalId}`).show ();
215
263
  }
216
264
 
217
265
  /**
@@ -235,36 +283,41 @@ export class AdminEdit extends HotComponent
235
283
 
236
284
  this.modalType = "remove";
237
285
 
238
- let attachedList = document.getElementById (this.attached_list);
239
- // @ts-ignore
240
- let hotComponent: AdminTable = attachedList.hotComponent;
241
- let checkedRows = hotComponent.getCheckedRows ();
242
286
  let whereFields: any[] = [];
287
+ let hotComponent: AdminTable = null;
243
288
 
244
- if (checkedRows.length > 0)
289
+ if (this.attached_list !== "")
245
290
  {
246
- for (let i = 0; i < checkedRows.length; i++)
291
+ let attachedList = document.getElementById (this.attached_list);
292
+ // @ts-ignore
293
+ hotComponent = attachedList.hotComponent;
294
+ let checkedRows = hotComponent.getCheckedRows ();
295
+
296
+ if (checkedRows.length > 0)
247
297
  {
248
- let checkedRow = checkedRows[i];
249
- whereFields.push (checkedRow);
298
+ for (let i = 0; i < checkedRows.length; i++)
299
+ {
300
+ let checkedRow = checkedRows[i];
301
+ whereFields.push (checkedRow);
302
+ }
250
303
  }
251
- }
252
304
 
253
- let selectedField = hotComponent.getSelected ();
305
+ let selectedField = hotComponent.getSelected ();
306
+
307
+ if (selectedField != null)
308
+ whereFields = [selectedField];
309
+ }
254
310
 
255
- if (selectedField != null)
256
- whereFields = [selectedField];
311
+ const confirmed: boolean = confirm ("Are you sure you want to remove this item?");
257
312
 
258
- if (whereFields.length > 0)
313
+ if (confirmed === true)
259
314
  {
260
- const confirmed: boolean = confirm ("Are you sure you want to remove this item?");
261
-
262
- if (confirmed === true)
315
+ for (let i = 0; i < whereFields.length; i++)
263
316
  {
264
- for (let i = 0; i < whereFields.length; i++)
265
- {
266
- let whereField = whereFields[i];
317
+ let whereField = whereFields[i];
267
318
 
319
+ if (hotComponent != null)
320
+ {
268
321
  // Remove any fields that are marked as remove.
269
322
  for (let key in whereField)
270
323
  {
@@ -276,27 +329,48 @@ export class AdminEdit extends HotComponent
276
329
  delete whereField[key];
277
330
  }
278
331
  }
332
+ }
279
333
 
280
- if (this.ondelete != null)
281
- {
282
- if (typeof (this.ondelete) === "string")
283
- this.ondelete = (<(selectedField: any) => Promise<void>>new Function (this.ondelete));
334
+ if (this.ondelete != null)
335
+ {
336
+ if (typeof (this.ondelete) === "string")
337
+ this.ondelete = (<(selectedField: any) => Promise<void>>new Function (this.ondelete));
284
338
 
285
- await this.ondelete (whereField);
286
- }
287
- else
288
- {
289
- let removeUrl: string = Hot.Data.AdminPanel.removeUrl;
339
+ await this.ondelete (whereField);
340
+ }
341
+ else
342
+ {
343
+ let removeUrl: string = Hot.Data.AdminPanel.removeUrl;
290
344
 
291
- await Hot.jsonRequest (removeUrl, {
292
- schema: this.schema,
293
- whereFields: whereField
294
- });
295
- }
345
+ await Hot.jsonRequest (removeUrl, {
346
+ schema: this.schema,
347
+ whereFields: whereField
348
+ });
296
349
  }
297
-
298
- await hotComponent.refreshList ();
299
350
  }
351
+
352
+ if (whereFields.length === 0)
353
+ {
354
+ if (this.ondelete != null)
355
+ {
356
+ if (typeof (this.ondelete) === "string")
357
+ this.ondelete = (<(selectedField: any) => Promise<void>>new Function (this.ondelete));
358
+
359
+ await this.ondelete (null);
360
+ }
361
+ else
362
+ {
363
+ let removeUrl: string = Hot.Data.AdminPanel.removeUrl;
364
+
365
+ await Hot.jsonRequest (removeUrl, {
366
+ schema: this.schema,
367
+ whereFields: null
368
+ });
369
+ }
370
+ }
371
+
372
+ if (hotComponent != null)
373
+ await hotComponent.refreshList ();
300
374
  }
301
375
  }
302
376
 
@@ -312,15 +386,33 @@ export class AdminEdit extends HotComponent
312
386
  if (field.type === "remove")
313
387
  return;
314
388
 
315
- values[field.name] = $(htmlElement).val ();
389
+ let value = $(htmlElement).val ();
390
+
391
+ if ($(htmlElement).attr ("hot-value") != null)
392
+ value = $(htmlElement).attr ("hot-value");
393
+
394
+ if (field.type === "array")
395
+ {
396
+ if (values[field.name] == null)
397
+ values[field.name] = [];
398
+
399
+ values[field.name].push (value);
400
+
401
+ return;
402
+ }
403
+
404
+ values[field.name] = value;
316
405
  });
317
406
 
318
407
  if (this.onsave != null)
319
408
  {
320
409
  if (typeof (this.onsave) === "string")
321
- this.onsave = (<(modalType: string, values: any, selectedFields: any[]) => Promise<void>>new Function (this.onsave));
410
+ this.onsave = (<(modalType: string, values: any, selectedFields: any[]) => Promise<boolean>>new Function (this.onsave));
411
+
412
+ let result = await this.onsave (this.modalType, values, this.selectedFields);
322
413
 
323
- await this.onsave (this.modalType, values, this.selectedFields);
414
+ if (result === false)
415
+ return;
324
416
  }
325
417
  else
326
418
  {
@@ -349,8 +441,23 @@ export class AdminEdit extends HotComponent
349
441
  {
350
442
  if (field.type === "remove")
351
443
  return;
352
-
353
- whereFields[field.name] = $(htmlElement).val ();
444
+
445
+ let value = $(htmlElement).val ();
446
+
447
+ if ($(htmlElement).attr ("hot-value") != null)
448
+ value = $(htmlElement).attr ("hot-value");
449
+
450
+ if (field.type === "array")
451
+ {
452
+ if (whereFields[field.name] == null)
453
+ whereFields[field.name] = [];
454
+
455
+ whereFields[field.name].push (value);
456
+
457
+ return;
458
+ }
459
+
460
+ whereFields[field.name] = value;
354
461
  });
355
462
 
356
463
  let editUrl: string = Hot.Data.AdminPanel.editUrl;
@@ -363,11 +470,14 @@ export class AdminEdit extends HotComponent
363
470
  }
364
471
  }
365
472
 
366
- let attachedList = document.getElementById (this.attached_list);
367
- // @ts-ignore
368
- let table: AdminTable = attachedList.hotComponent;
473
+ if (this.attached_list !== "")
474
+ {
475
+ let attachedList = document.getElementById (this.attached_list);
476
+ // @ts-ignore
477
+ let table: AdminTable = attachedList.hotComponent;
369
478
 
370
- await table.refreshList ();
479
+ await table.refreshList ();
480
+ }
371
481
 
372
482
  if (this.closeOnSave === true)
373
483
  {
@@ -396,10 +506,10 @@ export class AdminEdit extends HotComponent
396
506
 
397
507
  this.modalId = `${this.name}Modal`;
398
508
 
399
- return ([{
509
+ let outputObj = [{
400
510
  html: `
401
511
  <!-- ${this.title} Modal Start -->
402
- <div class="modal fade ${this.class}" id="${this.modalId}" tabindex="-1" aria-labelledby="${this.name}ModalLabel" aria-hidden="true">
512
+ <div class="modal fade ${this.css_class}" id="${this.modalId}" tabindex="-1" aria-labelledby="${this.name}ModalLabel" aria-hidden="true">
403
513
  <div class="modal-dialog">
404
514
  <div class="modal-content">
405
515
  <div class="modal-header">
@@ -418,18 +528,32 @@ export class AdminEdit extends HotComponent
418
528
  </div>
419
529
  <!-- ${this.title} Modal End -->`,
420
530
  documentSelector: "body"
421
- },
531
+ }];
532
+
533
+ if (this.add_place_here !== "")
422
534
  {
423
- html: `<button id = "${this.modalId}-add-btn" type="button" class="btn btn-sm btn-outline-secondary" onclick = "this.addClicked ();">Add</button>`,
424
- documentSelector: `hot-place-here[name="dashboardHeader"]`
425
- },
535
+ outputObj.push ({
536
+ html: `<button id = "${this.modalId}-add-btn" type="button" class="btn btn-sm btn-outline-secondary" onclick = "this.addClicked ();">${this.add_text}</button>`,
537
+ documentSelector: `hot-place-here[name="${this.add_place_here}"]`
538
+ });
539
+ }
540
+
541
+ if (this.edit_place_here !== "")
426
542
  {
427
- html: `<button id = "${this.modalId}-edit-btn" type="button" class="btn btn-sm btn-outline-secondary" onclick = "this.editClicked ();">Edit</button>`,
428
- documentSelector: `hot-place-here[name="dashboardHeader"]`
429
- },
543
+ outputObj.push ({
544
+ html: `<button id = "${this.modalId}-edit-btn" type="button" class="btn btn-sm btn-outline-secondary" onclick = "this.editClicked ();">${this.edit_text}</button>`,
545
+ documentSelector: `hot-place-here[name="${this.edit_place_here}"]`
546
+ });
547
+ }
548
+
549
+ if (this.remove_place_here !== "")
430
550
  {
431
- html: `<button id = "${this.modalId}-remove-btn" type="button" class="btn btn-sm btn-outline-secondary" onclick = "this.removeClicked ();">Remove</button>`,
432
- documentSelector: `hot-place-here[name="dashboardHeader"]`
433
- }]);
551
+ outputObj.push ({
552
+ html: `<button id = "${this.modalId}-remove-btn" type="button" class="btn btn-sm btn-outline-secondary" onclick = "this.removeClicked ();">${this.remove_text}</button>`,
553
+ documentSelector: `hot-place-here[name="${this.remove_place_here}"]`
554
+ });
555
+ }
556
+
557
+ return (outputObj);
434
558
  }
435
559
  }
@@ -277,6 +277,12 @@ export class AdminTable extends HotComponent
277
277
  */
278
278
  addRow (fields: { [name: string]: any }[])
279
279
  {
280
+ if (fields == null)
281
+ return;
282
+
283
+ if (fields.length == 0)
284
+ return;
285
+
280
286
  let tbody = this.htmlElements[1].getElementsByTagName ("tbody")[0];
281
287
  let index: number = this.rowElements.length;
282
288
  let rowStr = `<tr onclick = "this.parentNode.parentNode.parentNode.parentNode.hotComponent.selectRow (this, ${index});">`;
@@ -382,10 +388,8 @@ export class AdminTable extends HotComponent
382
388
  /**
383
389
  * Refresh the list.
384
390
  */
385
- async refreshList ()
391
+ async refreshList (list: any[] = null)
386
392
  {
387
- let list = null;
388
-
389
393
  if (this.onlist != null)
390
394
  {
391
395
  if (typeof (this.onlist) === "string")
@@ -395,23 +399,29 @@ export class AdminTable extends HotComponent
395
399
  }
396
400
  else
397
401
  {
398
- let listUrl: string = Hot.Data.AdminPanel.listUrl;
402
+ let listUrl: string = "";
399
403
 
400
404
  if (this.listurl !== "")
401
405
  listUrl = this.listurl;
402
406
 
403
- list = await Hot.jsonRequest (listUrl, {
404
- schema: this.schema
405
- });
407
+ if (listUrl !== "")
408
+ {
409
+ list = await Hot.jsonRequest (listUrl, {
410
+ schema: this.schema
411
+ });
412
+ }
406
413
  }
407
414
 
408
415
  this.clearRows ();
409
416
 
410
- for (let iIdx = 0; iIdx < list.length; iIdx++)
417
+ if (list != null)
411
418
  {
412
- let fields = list[iIdx];
419
+ for (let iIdx = 0; iIdx < list.length; iIdx++)
420
+ {
421
+ let fields = list[iIdx];
413
422
 
414
- this.addRow (fields);
423
+ this.addRow (fields);
424
+ }
415
425
  }
416
426
  }
417
427
 
@@ -10,7 +10,7 @@ export class AdminText extends HotComponent
10
10
  * The classes to set for the input.
11
11
  * @default "form-control"
12
12
  */
13
- class: string;
13
+ css_class: string;
14
14
 
15
15
  constructor (copy: HotComponent | HotStaq, api: HotAPI)
16
16
  {
@@ -18,7 +18,7 @@ export class AdminText extends HotComponent
18
18
 
19
19
  this.tag = "admin-text";
20
20
  this.no_output = "0";
21
- this.class = "form-control"
21
+ this.css_class = "form-control"
22
22
  }
23
23
 
24
24
  /**
@@ -46,7 +46,7 @@ export class AdminText extends HotComponent
46
46
  value = this.value;
47
47
 
48
48
  if (this.no_output === "1")
49
- return (`<div><input class="${this.class}" type = "hidden" value = "${value}" /></div>`);
49
+ return (`<div><input class="${this.css_class}" type = "hidden" value = "${value}" /></div>`);
50
50
 
51
51
  const field = this.htmlElements[0].getAttribute ("hot-field");
52
52
  let field_type = this.htmlElements[0].getAttribute ("hot-field-type");
@@ -56,7 +56,7 @@ export class AdminText extends HotComponent
56
56
 
57
57
  return (`<div>
58
58
  <label class="form-label">${this.inner}</label>
59
- <input class="${this.class}" type = "text" hot-field = "${field}" hot-field-type = "${field_type}" value = "${value}" />
59
+ <input class="${this.css_class}" type = "text" hot-field = "${field}" hot-field-type = "${field_type}" value = "${value}" />
60
60
  </div>`);
61
61
  }
62
62
  }