@websy/websy-designs 1.3.2 → 1.3.3

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.
@@ -3310,6 +3310,7 @@ class WebsyResultList {
3310
3310
  this.rows = []
3311
3311
  this.apiService = new WebsyDesigns.APIService('/api')
3312
3312
  this.templateService = new WebsyDesigns.APIService('')
3313
+ this.activeTemplate = ''
3313
3314
  if (!elementId) {
3314
3315
  console.log('No element Id provided for Websy Search List')
3315
3316
  return
@@ -3331,16 +3332,17 @@ class WebsyResultList {
3331
3332
  appendData (d) {
3332
3333
  let startIndex = this.rows.length
3333
3334
  this.rows = this.rows.concat(d)
3335
+ this.activeTemplate = this.options.template
3334
3336
  const html = this.buildHTML(d, startIndex)
3335
3337
  const el = document.getElementById(this.elementId)
3336
3338
  el.innerHTML += html.replace(/\n/g, '')
3337
3339
  }
3338
- buildHTML (d, startIndex = 0) {
3340
+ buildHTML (d, startIndex = 0, inputTemplate) {
3339
3341
  let html = ``
3340
3342
  if (this.options.template) {
3341
3343
  if (d.length > 0) {
3342
3344
  d.forEach((row, ix) => {
3343
- let template = `${ix > 0 ? '-->' : ''}${this.options.template}${ix < d.length - 1 ? '<!--' : ''}`
3345
+ let template = `${ix > 0 ? '-->' : ''}${inputTemplate || this.options.template}${ix < d.length - 1 ? '<!--' : ''}`
3344
3346
  // find conditional elements
3345
3347
  let ifMatches = [...template.matchAll(/<\s*if[^>]*>([\s\S]*?)<\s*\/\s*if>/g)]
3346
3348
  ifMatches.forEach(m => {
@@ -3419,16 +3421,39 @@ class WebsyResultList {
3419
3421
  }
3420
3422
  }
3421
3423
  })
3424
+ let forMatches = [...template.matchAll(/<\s*for[^>]*>([\s\S]*?)<\s*\/\s*for>/g)]
3425
+ forMatches.forEach(m => {
3426
+ let itemsMatch = m[0].match(/(items=["|']\w.+)["|']/g)
3427
+ let forMarkup = m[0].match(/<\s*for[^>]*>/)
3428
+ let withoutFor = m[0].replace(forMarkup, '').replace('</for>', '').replace(/<\s*for[^>]*>/g, '')
3429
+ if (itemsMatch && itemsMatch[0]) {
3430
+ let c = itemsMatch[0].trim().replace('items=', '')
3431
+ if (c.split('')[0] === '"') {
3432
+ c = c.replace(/"/g, '')
3433
+ }
3434
+ else if (c.split('')[0] === '\'') {
3435
+ c = c.replace(/'/g, '')
3436
+ }
3437
+ let items = row
3438
+ let parts = c.split('.')
3439
+ parts.forEach(p => {
3440
+ items = items[p]
3441
+ })
3442
+ template = template.replace(m[0], this.buildHTML(items, 0, withoutFor))
3443
+ }
3444
+ })
3422
3445
  let tagMatches = [...template.matchAll(/(\sdata-event=["|']\w.+)["|']/g)]
3423
3446
  tagMatches.forEach(m => {
3424
3447
  if (m[0] && m.index > -1) {
3425
3448
  template = template.replace(m[0], `${m[0]} data-id=${startIndex + ix}`)
3426
3449
  }
3427
- })
3428
- for (let key in row) {
3450
+ })
3451
+ let flatRow = this.flattenObject(row)
3452
+ for (let key in flatRow) {
3429
3453
  let rg = new RegExp(`{${key}}`, 'gm')
3430
- template = template.replace(rg, row[key])
3454
+ template = template.replace(rg, flatRow[key] || '')
3431
3455
  }
3456
+ template = template.replace(/\{(.*?)\}/g, '')
3432
3457
  html += template
3433
3458
  })
3434
3459
  }
@@ -3453,6 +3478,27 @@ class WebsyResultList {
3453
3478
  }
3454
3479
  return null
3455
3480
  }
3481
+ flattenObject (obj) {
3482
+ const toReturn = {}
3483
+ for (const i in obj) {
3484
+ if (!obj.hasOwnProperty(i)) {
3485
+ continue
3486
+ }
3487
+ if (typeof obj[i] === 'object') {
3488
+ const flatObject = this.flattenObject(obj[i])
3489
+ for (const x in flatObject) {
3490
+ if (!flatObject.hasOwnProperty(x)) {
3491
+ continue
3492
+ }
3493
+ toReturn[i + '.' + x] = flatObject[x]
3494
+ }
3495
+ }
3496
+ else {
3497
+ toReturn[i] = obj[i]
3498
+ }
3499
+ }
3500
+ return JSON.parse(JSON.stringify(toReturn))
3501
+ }
3456
3502
  handleClick (event) {
3457
3503
  if (event.target.classList.contains('clickable')) {
3458
3504
  let l = event.target.getAttribute('data-event')
@@ -3538,7 +3584,7 @@ class WebsyRouter {
3538
3584
  if (this.options.onHide) {
3539
3585
  this.on('hide', this.options.onHide)
3540
3586
  }
3541
- this.init()
3587
+ // this.init()
3542
3588
  }
3543
3589
  addGroup (group) {
3544
3590
  if (!this.groups[group]) {
@@ -4087,7 +4133,7 @@ class WebsySearch {
4087
4133
  }, this.options.searchTimeout)
4088
4134
  }
4089
4135
  else {
4090
- if (this.options.onSearch) {
4136
+ if (this.options.onSearch && (event.key === 'Delete' || event.key === 'Backspace')) {
4091
4137
  this.options.onSearch('')
4092
4138
  }
4093
4139
  }
@@ -3628,6 +3628,7 @@ var WebsyResultList = /*#__PURE__*/function () {
3628
3628
  this.rows = [];
3629
3629
  this.apiService = new WebsyDesigns.APIService('/api');
3630
3630
  this.templateService = new WebsyDesigns.APIService('');
3631
+ this.activeTemplate = '';
3631
3632
 
3632
3633
  if (!elementId) {
3633
3634
  console.log('No element Id provided for Websy Search List');
@@ -3656,6 +3657,7 @@ var WebsyResultList = /*#__PURE__*/function () {
3656
3657
  value: function appendData(d) {
3657
3658
  var startIndex = this.rows.length;
3658
3659
  this.rows = this.rows.concat(d);
3660
+ this.activeTemplate = this.options.template;
3659
3661
  var html = this.buildHTML(d, startIndex);
3660
3662
  var el = document.getElementById(this.elementId);
3661
3663
  el.innerHTML += html.replace(/\n/g, '');
@@ -3666,12 +3668,13 @@ var WebsyResultList = /*#__PURE__*/function () {
3666
3668
  var _this23 = this;
3667
3669
 
3668
3670
  var startIndex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
3671
+ var inputTemplate = arguments.length > 2 ? arguments[2] : undefined;
3669
3672
  var html = "";
3670
3673
 
3671
3674
  if (this.options.template) {
3672
3675
  if (d.length > 0) {
3673
3676
  d.forEach(function (row, ix) {
3674
- var template = "".concat(ix > 0 ? '-->' : '').concat(_this23.options.template).concat(ix < d.length - 1 ? '<!--' : ''); // find conditional elements
3677
+ var template = "".concat(ix > 0 ? '-->' : '').concat(inputTemplate || _this23.options.template).concat(ix < d.length - 1 ? '<!--' : ''); // find conditional elements
3675
3678
 
3676
3679
  var ifMatches = _toConsumableArray(template.matchAll(/<\s*if[^>]*>([\s\S]*?)<\s*\/\s*if>/g));
3677
3680
 
@@ -3755,6 +3758,31 @@ var WebsyResultList = /*#__PURE__*/function () {
3755
3758
  }
3756
3759
  });
3757
3760
 
3761
+ var forMatches = _toConsumableArray(template.matchAll(/<\s*for[^>]*>([\s\S]*?)<\s*\/\s*for>/g));
3762
+
3763
+ forMatches.forEach(function (m) {
3764
+ var itemsMatch = m[0].match(/(items=["|']\w.+)["|']/g);
3765
+ var forMarkup = m[0].match(/<\s*for[^>]*>/);
3766
+ var withoutFor = m[0].replace(forMarkup, '').replace('</for>', '').replace(/<\s*for[^>]*>/g, '');
3767
+
3768
+ if (itemsMatch && itemsMatch[0]) {
3769
+ var c = itemsMatch[0].trim().replace('items=', '');
3770
+
3771
+ if (c.split('')[0] === '"') {
3772
+ c = c.replace(/"/g, '');
3773
+ } else if (c.split('')[0] === '\'') {
3774
+ c = c.replace(/'/g, '');
3775
+ }
3776
+
3777
+ var items = row;
3778
+ var parts = c.split('.');
3779
+ parts.forEach(function (p) {
3780
+ items = items[p];
3781
+ });
3782
+ template = template.replace(m[0], _this23.buildHTML(items, 0, withoutFor));
3783
+ }
3784
+ });
3785
+
3758
3786
  var tagMatches = _toConsumableArray(template.matchAll(/(\sdata-event=["|']\w.+)["|']/g));
3759
3787
 
3760
3788
  tagMatches.forEach(function (m) {
@@ -3763,11 +3791,14 @@ var WebsyResultList = /*#__PURE__*/function () {
3763
3791
  }
3764
3792
  });
3765
3793
 
3766
- for (var key in row) {
3794
+ var flatRow = _this23.flattenObject(row);
3795
+
3796
+ for (var key in flatRow) {
3767
3797
  var rg = new RegExp("{".concat(key, "}"), 'gm');
3768
- template = template.replace(rg, row[key]);
3798
+ template = template.replace(rg, flatRow[key] || '');
3769
3799
  }
3770
3800
 
3801
+ template = template.replace(/\{(.*?)\}/g, '');
3771
3802
  html += template;
3772
3803
  });
3773
3804
  } else if (this.options.noRowsHTML) {
@@ -3788,6 +3819,33 @@ var WebsyResultList = /*#__PURE__*/function () {
3788
3819
 
3789
3820
  return null;
3790
3821
  }
3822
+ }, {
3823
+ key: "flattenObject",
3824
+ value: function flattenObject(obj) {
3825
+ var toReturn = {};
3826
+
3827
+ for (var i in obj) {
3828
+ if (!obj.hasOwnProperty(i)) {
3829
+ continue;
3830
+ }
3831
+
3832
+ if (_typeof(obj[i]) === 'object') {
3833
+ var flatObject = this.flattenObject(obj[i]);
3834
+
3835
+ for (var x in flatObject) {
3836
+ if (!flatObject.hasOwnProperty(x)) {
3837
+ continue;
3838
+ }
3839
+
3840
+ toReturn[i + '.' + x] = flatObject[x];
3841
+ }
3842
+ } else {
3843
+ toReturn[i] = obj[i];
3844
+ }
3845
+ }
3846
+
3847
+ return JSON.parse(JSON.stringify(toReturn));
3848
+ }
3791
3849
  }, {
3792
3850
  key: "handleClick",
3793
3851
  value: function handleClick(event) {
@@ -3909,9 +3967,8 @@ var WebsyRouter = /*#__PURE__*/function () {
3909
3967
 
3910
3968
  if (this.options.onHide) {
3911
3969
  this.on('hide', this.options.onHide);
3912
- }
3970
+ } // this.init()
3913
3971
 
3914
- this.init();
3915
3972
  }
3916
3973
 
3917
3974
  _createClass(WebsyRouter, [{
@@ -4588,7 +4645,7 @@ var WebsySearch = /*#__PURE__*/function () {
4588
4645
  }
4589
4646
  }, this.options.searchTimeout);
4590
4647
  } else {
4591
- if (this.options.onSearch) {
4648
+ if (this.options.onSearch && (event.key === 'Delete' || event.key === 'Backspace')) {
4592
4649
  this.options.onSearch('');
4593
4650
  }
4594
4651
  }