@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.
@@ -11,7 +11,8 @@ const sql = {
11
11
  id SERIAL PRIMARY KEY,
12
12
  userid integer,
13
13
  items text DEFAULT ''::text,
14
- meta text DEFAULT ''::text
14
+ meta text DEFAULT ''::text,
15
+ complete boolean
15
16
  );
16
17
  `,
17
18
  compare: `
@@ -3062,6 +3062,7 @@ class WebsyResultList {
3062
3062
  this.rows = []
3063
3063
  this.apiService = new WebsyDesigns.APIService('/api')
3064
3064
  this.templateService = new WebsyDesigns.APIService('')
3065
+ this.activeTemplate = ''
3065
3066
  if (!elementId) {
3066
3067
  console.log('No element Id provided for Websy Search List')
3067
3068
  return
@@ -3083,16 +3084,17 @@ class WebsyResultList {
3083
3084
  appendData (d) {
3084
3085
  let startIndex = this.rows.length
3085
3086
  this.rows = this.rows.concat(d)
3087
+ this.activeTemplate = this.options.template
3086
3088
  const html = this.buildHTML(d, startIndex)
3087
3089
  const el = document.getElementById(this.elementId)
3088
3090
  el.innerHTML += html.replace(/\n/g, '')
3089
3091
  }
3090
- buildHTML (d, startIndex = 0) {
3092
+ buildHTML (d, startIndex = 0, inputTemplate, locator = []) {
3091
3093
  let html = ``
3092
3094
  if (this.options.template) {
3093
3095
  if (d.length > 0) {
3094
3096
  d.forEach((row, ix) => {
3095
- let template = `${ix > 0 ? '-->' : ''}${this.options.template}${ix < d.length - 1 ? '<!--' : ''}`
3097
+ let template = `${ix > 0 ? '-->' : ''}${inputTemplate || this.options.template}${ix < d.length - 1 ? '<!--' : ''}`
3096
3098
  // find conditional elements
3097
3099
  let ifMatches = [...template.matchAll(/<\s*if[^>]*>([\s\S]*?)<\s*\/\s*if>/g)]
3098
3100
  ifMatches.forEach(m => {
@@ -3171,16 +3173,39 @@ class WebsyResultList {
3171
3173
  }
3172
3174
  }
3173
3175
  })
3176
+ let forMatches = [...template.matchAll(/<\s*for[^>]*>([\s\S]*?)<\s*\/\s*for>/g)]
3177
+ forMatches.forEach(m => {
3178
+ let itemsMatch = m[0].match(/(items=["|']\w.+)["|']/g)
3179
+ let forMarkup = m[0].match(/<\s*for[^>]*>/)
3180
+ let withoutFor = m[0].replace(forMarkup, '').replace('</for>', '').replace(/<\s*for[^>]*>/g, '')
3181
+ if (itemsMatch && itemsMatch[0]) {
3182
+ let c = itemsMatch[0].trim().replace('items=', '')
3183
+ if (c.split('')[0] === '"') {
3184
+ c = c.replace(/"/g, '')
3185
+ }
3186
+ else if (c.split('')[0] === '\'') {
3187
+ c = c.replace(/'/g, '')
3188
+ }
3189
+ let items = row
3190
+ let parts = c.split('.')
3191
+ parts.forEach(p => {
3192
+ items = items[p]
3193
+ })
3194
+ template = template.replace(m[0], this.buildHTML(items, 0, withoutFor, [...locator, `${startIndex + ix}:${c}`]))
3195
+ }
3196
+ })
3174
3197
  let tagMatches = [...template.matchAll(/(\sdata-event=["|']\w.+)["|']/g)]
3175
3198
  tagMatches.forEach(m => {
3176
3199
  if (m[0] && m.index > -1) {
3177
- template = template.replace(m[0], `${m[0]} data-id=${startIndex + ix}`)
3200
+ template = template.replace(m[0], `${m[0]} data-id=${startIndex + ix} data-locator='${locator.join(';')}'`)
3178
3201
  }
3179
- })
3180
- for (let key in row) {
3202
+ })
3203
+ let flatRow = this.flattenObject(row)
3204
+ for (let key in flatRow) {
3181
3205
  let rg = new RegExp(`{${key}}`, 'gm')
3182
- template = template.replace(rg, row[key])
3206
+ template = template.replace(rg, flatRow[key] || '')
3183
3207
  }
3208
+ template = template.replace(/\{(.*?)\}/g, '')
3184
3209
  html += template
3185
3210
  })
3186
3211
  }
@@ -3205,6 +3230,27 @@ class WebsyResultList {
3205
3230
  }
3206
3231
  return null
3207
3232
  }
3233
+ flattenObject (obj) {
3234
+ const toReturn = {}
3235
+ for (const i in obj) {
3236
+ if (!obj.hasOwnProperty(i)) {
3237
+ continue
3238
+ }
3239
+ if (typeof obj[i] === 'object') {
3240
+ const flatObject = this.flattenObject(obj[i])
3241
+ for (const x in flatObject) {
3242
+ if (!flatObject.hasOwnProperty(x)) {
3243
+ continue
3244
+ }
3245
+ toReturn[i + '.' + x] = flatObject[x]
3246
+ }
3247
+ }
3248
+ else {
3249
+ toReturn[i] = obj[i]
3250
+ }
3251
+ }
3252
+ return JSON.parse(JSON.stringify(toReturn))
3253
+ }
3208
3254
  handleClick (event) {
3209
3255
  if (event.target.classList.contains('clickable')) {
3210
3256
  let l = event.target.getAttribute('data-event')
@@ -3212,15 +3258,30 @@ class WebsyResultList {
3212
3258
  l = l.split('(')
3213
3259
  let params = []
3214
3260
  const id = event.target.getAttribute('data-id')
3261
+ const locator = event.target.getAttribute('data-locator')
3215
3262
  if (l[1]) {
3216
3263
  l[1] = l[1].replace(')', '')
3217
3264
  params = l[1].split(',')
3218
3265
  }
3219
3266
  l = l[0]
3267
+ let data = this.rows
3268
+ if (locator !== '') {
3269
+ let locatorItems = locator.split(';')
3270
+ locatorItems.forEach(loc => {
3271
+ let locatorParts = loc.split(':')
3272
+ if (data[locatorParts[0]]) {
3273
+ data = data[locatorParts[0]]
3274
+ let parts = locatorParts[1].split('.')
3275
+ parts.forEach(p => {
3276
+ data = data[p]
3277
+ })
3278
+ }
3279
+ })
3280
+ }
3220
3281
  params = params.map(p => {
3221
3282
  if (typeof p !== 'string' && typeof p !== 'number') {
3222
- if (this.rows[+id]) {
3223
- p = this.rows[+id][p]
3283
+ if (data[+id]) {
3284
+ p = data[+id][p]
3224
3285
  }
3225
3286
  }
3226
3287
  else if (typeof p === 'string') {
@@ -3230,7 +3291,7 @@ class WebsyResultList {
3230
3291
  })
3231
3292
  if (event.target.classList.contains('clickable') && this.options.listeners.click[l]) {
3232
3293
  event.stopPropagation()
3233
- this.options.listeners.click[l].call(this, event, this.rows[+id], ...params)
3294
+ this.options.listeners.click[l].call(this, event, data[+id], ...params)
3234
3295
  }
3235
3296
  }
3236
3297
  }
@@ -3290,7 +3351,7 @@ class WebsyRouter {
3290
3351
  if (this.options.onHide) {
3291
3352
  this.on('hide', this.options.onHide)
3292
3353
  }
3293
- this.init()
3354
+ // this.init()
3294
3355
  }
3295
3356
  addGroup (group) {
3296
3357
  if (!this.groups[group]) {