@websy/websy-designs 1.3.2 → 1.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.
@@ -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, locator = []) {
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, [...locator, `${startIndex + ix}:${c}`]))
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
- template = template.replace(m[0], `${m[0]} data-id=${startIndex + ix}`)
3448
+ template = template.replace(m[0], `${m[0]} data-id=${startIndex + ix} data-locator='${locator.join(';')}'`)
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')
@@ -3460,15 +3506,30 @@ class WebsyResultList {
3460
3506
  l = l.split('(')
3461
3507
  let params = []
3462
3508
  const id = event.target.getAttribute('data-id')
3509
+ const locator = event.target.getAttribute('data-locator')
3463
3510
  if (l[1]) {
3464
3511
  l[1] = l[1].replace(')', '')
3465
3512
  params = l[1].split(',')
3466
3513
  }
3467
3514
  l = l[0]
3515
+ let data = this.rows
3516
+ if (locator !== '') {
3517
+ let locatorItems = locator.split(';')
3518
+ locatorItems.forEach(loc => {
3519
+ let locatorParts = loc.split(':')
3520
+ if (data[locatorParts[0]]) {
3521
+ data = data[locatorParts[0]]
3522
+ let parts = locatorParts[1].split('.')
3523
+ parts.forEach(p => {
3524
+ data = data[p]
3525
+ })
3526
+ }
3527
+ })
3528
+ }
3468
3529
  params = params.map(p => {
3469
3530
  if (typeof p !== 'string' && typeof p !== 'number') {
3470
- if (this.rows[+id]) {
3471
- p = this.rows[+id][p]
3531
+ if (data[+id]) {
3532
+ p = data[+id][p]
3472
3533
  }
3473
3534
  }
3474
3535
  else if (typeof p === 'string') {
@@ -3478,7 +3539,7 @@ class WebsyResultList {
3478
3539
  })
3479
3540
  if (event.target.classList.contains('clickable') && this.options.listeners.click[l]) {
3480
3541
  event.stopPropagation()
3481
- this.options.listeners.click[l].call(this, event, this.rows[+id], ...params)
3542
+ this.options.listeners.click[l].call(this, event, data[+id], ...params)
3482
3543
  }
3483
3544
  }
3484
3545
  }
@@ -3538,7 +3599,7 @@ class WebsyRouter {
3538
3599
  if (this.options.onHide) {
3539
3600
  this.on('hide', this.options.onHide)
3540
3601
  }
3541
- this.init()
3602
+ // this.init()
3542
3603
  }
3543
3604
  addGroup (group) {
3544
3605
  if (!this.groups[group]) {
@@ -4087,7 +4148,7 @@ class WebsySearch {
4087
4148
  }, this.options.searchTimeout)
4088
4149
  }
4089
4150
  else {
4090
- if (this.options.onSearch) {
4151
+ if (this.options.onSearch && (event.key === 'Delete' || event.key === 'Backspace')) {
4091
4152
  this.options.onSearch('')
4092
4153
  }
4093
4154
  }