@cocreate/render 1.33.0 → 1.35.0

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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,23 @@
1
+ # [1.35.0](https://github.com/CoCreate-app/CoCreate-render/compare/v1.34.0...v1.35.0) (2023-11-25)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * pass attributes renamed to state ([c3c96d7](https://github.com/CoCreate-app/CoCreate-render/commit/c3c96d795b405054a8669b4d05cd69b89d103cc6))
7
+ * update nav, edit button and css path ([5b6cad4](https://github.com/CoCreate-app/CoCreate-render/commit/5b6cad4d112d736b1fec04a7bc897f22b961e086))
8
+
9
+
10
+ ### Features
11
+
12
+ * upgrade dependencies for latest features and fixes ([18468c3](https://github.com/CoCreate-app/CoCreate-render/commit/18468c3992d47414e1ffa9ae27a60abed0cf9a8e))
13
+
14
+ # [1.34.0](https://github.com/CoCreate-app/CoCreate-render/compare/v1.33.0...v1.34.0) (2023-11-22)
15
+
16
+
17
+ ### Features
18
+
19
+ * improved placeholder support ([165c450](https://github.com/CoCreate-app/CoCreate-render/commit/165c450b833c317ce5b55f9d2fc9d9de16d68f69))
20
+
1
21
  # [1.33.0](https://github.com/CoCreate-app/CoCreate-render/compare/v1.32.0...v1.33.0) (2023-11-19)
2
22
 
3
23
 
package/docs/index.html CHANGED
@@ -28,7 +28,7 @@
28
28
  type="text/css" />
29
29
  <link
30
30
  rel="stylesheet"
31
- href="/docs/index.css"
31
+ href="../index.css"
32
32
  array="files"
33
33
  object="60888216117c640e7596303f"
34
34
  key="src"
@@ -38,15 +38,15 @@
38
38
  </head>
39
39
 
40
40
  <body>
41
+ <!-- Navbar -->
41
42
  <nav
42
- class="nav display:flex align-items:center left:0px background:whitesmoke padding-top:10px padding-bottom:10px"
43
+ class="width:100% display:flex align-items:center background:transparent padding-top:10px padding-bottom:10px"
43
44
  content_id="content"
44
- scroll="sticky-nav,hide-nav"
45
- scroll-up="10"
46
- scroll-down="10"
47
- array="files"
48
- object="60395ef42b3ac232657040fd"
49
- key="src"></nav>
45
+ scroll="sticky-nav,"
46
+ scroll-up="5"
47
+ scroll-down="5"
48
+ path="../"
49
+ src="../components/navbar.html"></nav>
50
50
  <sidenav
51
51
  id="menuL"
52
52
  class="position:fixed top:0px left:0px overflow:hidden background:whitesmoke height:100vh width:0px width:300px@xl"
@@ -333,7 +333,7 @@
333
333
  <li
334
334
  class="padding:15px_0px border-bottom:1px_solid_lightgrey">
335
335
  <h4>
336
- <span>setValue(elements, data, [passTo])</span>
336
+ <span>setValue(elements, data, [stateTo])</span>
337
337
  </h4>
338
338
  <p>This is main function.</p>
339
339
  <div>
@@ -360,7 +360,7 @@
360
360
  </div>
361
361
  <div>
362
362
  <p>
363
- <strong>passTo</strong
363
+ <strong>stateTo</strong
364
364
  ><span class="cocreate-badge warning"
365
365
  >Type:string</span
366
366
  ><span class="cocreate-badge warning"
@@ -566,10 +566,8 @@
566
566
  <button
567
567
  href="https://github.com/CoCreate-app/CoCreate-render/tree/master/docs/index.html?message=docs%3A%20describe%20your%20change..."
568
568
  target="_blank"
569
- class="position:fixed bottom:15px right:15px padding:15px background:dodgerblue color:#fff font-size:1.5rem grow-hover border-radius:50% border-width:0 box-shadow:0px_2px_10px_0px_rgba(0,_0,_0,_0.4)">
570
- <i
571
- class="height:20px fill:#505050"
572
- src="/assets/svg/pencil-alt.svg"></i>
569
+ class="display:flex justify-content:center align-items:center position:fixed bottom:15px right:15px height:50px width:50px border-radius:50% box-shadow:0px_2px_10px_0px_rgba(0,_0,_0,_0.4)">
570
+ <i class="height:20px" src="../assets/svg/pencil-alt.svg"></i>
573
571
  </button>
574
572
  </main>
575
573
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/render",
3
- "version": "1.33.0",
3
+ "version": "1.35.0",
4
4
  "description": "A simple render component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "render",
@@ -58,10 +58,10 @@
58
58
  "webpack-log": "^3.0.1"
59
59
  },
60
60
  "dependencies": {
61
- "@cocreate/actions": "^1.12.4",
62
- "@cocreate/api": "^1.15.12",
63
- "@cocreate/element-prototype": "^1.13.2",
64
- "@cocreate/observer": "^1.12.0",
65
- "@cocreate/utils": "^1.27.4"
61
+ "@cocreate/actions": "^1.13.0",
62
+ "@cocreate/api": "^1.16.0",
63
+ "@cocreate/element-prototype": "^1.14.1",
64
+ "@cocreate/observer": "^1.13.0",
65
+ "@cocreate/utils": "^1.28.0"
66
66
  }
67
67
  }
package/src/index.js CHANGED
@@ -32,6 +32,23 @@ const sources = new Map()
32
32
  const renderedNodes = new Map()
33
33
  const elementSelector = '[render-selector], [render-closest], [render-parent], [render-next], [render-previous]'
34
34
 
35
+
36
+ /**
37
+ * Initializes elements based on specified rendering attributes. If a specific element is provided, it initializes that element.
38
+ * If no element is provided, it queries and initializes all elements matching the defined selector criteria for rendering attributes.
39
+ *
40
+ * Supported rendering attributes:
41
+ * - render-selector: [Describe what this attribute does]
42
+ * - render-closest: [Describe what this attribute does]
43
+ * - render-parent: [Describe what this attribute does]
44
+ * - render-next: [Describe what this attribute does]
45
+ * - render-previous: [Describe what this attribute does]
46
+ *
47
+ * @param {(Element|Element[]|HTMLCollection|null)} [element] - Optional. A single element, an array of elements, an HTMLCollection, or null.
48
+ * - If an HTMLCollection or an array of elements is provided, each element in the collection/array is initialized.
49
+ * - If a single element is provided, only that element is initialized.
50
+ * - If null or omitted, the function queries and initializes all elements matching the 'elementSelector'.
51
+ */
35
52
  function init(element) {
36
53
  if (element && !(element instanceof HTMLCollection) && !Array.isArray(element))
37
54
  element = [element]
@@ -48,6 +65,21 @@ function init(element) {
48
65
  }
49
66
  }
50
67
 
68
+ /**
69
+ * Asynchronously renders content based on the provided parameters. This function handles the rendering logic for elements
70
+ * based on data sources, selectors, and other options.
71
+ *
72
+ * @param {Object} options - An object containing the parameters for rendering.
73
+ * @param {Object} options.source - The data source used for rendering.
74
+ * @param {Element} options.element - The DOM element where content will be rendered.
75
+ * @param {string} options.selector - CSS selector to specify target elements for rendering.
76
+ * @param {Object} options.data - Data to be used in rendering.
77
+ * @param {string} [options.key] - Optional key to reference specific parts of the data.
78
+ * @param {number} [options.index] - Optional index, typically used when rendering lists or arrays.
79
+ * @param {number} [options.currentIndex] - The current index in a list or array rendering process.
80
+ * @param {boolean} [options.update] - Flag to indicate if the rendering should update existing content.
81
+ * @param {boolean} [options.remove] - Flag to indicate if the rendering should remove elements instead of updating or appending.
82
+ */
51
83
  async function render({ source, element, selector, data, key, index, currentIndex, update, remove }) {
52
84
  if (!element) {
53
85
  if (source) {
@@ -453,9 +485,9 @@ async function renderValues(node, data, key, renderAs, keyPath, parent) {
453
485
  }
454
486
  // });
455
487
 
456
- if (CoCreate.pass) {
457
- if (node.hasAttribute('[pass_id]'))
458
- CoCreate.pass.initElement(node)
488
+ if (CoCreate.state) {
489
+ if (node.hasAttribute('[state_id]'))
490
+ CoCreate.state.initElement(node)
459
491
  }
460
492
 
461
493
  if (node.tagName === 'SCRIPT' && node.src) {
@@ -534,52 +566,74 @@ async function renderValues(node, data, key, renderAs, keyPath, parent) {
534
566
 
535
567
  async function renderValue(node, data, placeholder, renderAs, renderedNode) {
536
568
  let output = placeholder;
537
-
538
- if (placeholder.match(/{{(.*?)}}/)) {
539
- if (renderedNode) {
540
- renderedNodes.set(node, renderedNode)
541
- }
542
-
543
- let match
544
- do {
545
- match = output.match(/{{([A-Za-z0-9_.,\[\]\- ]*)}}/); // works by getting inner matches first
546
- // match = output.match(/{{([^}]*)}}/);
547
- // match = output.match(/{{(.*?)}}/);
548
- if (match) {
549
- let value
550
- try {
551
- let Data = JSON.parse('{' + match[1].replace(/'/g, '"') + '}');
552
- if (Data.storage || Data.database || Data.array || Data.object || Data.index) {
553
- Data.method = 'object.read'
554
- value = await CoCreate.crud.send(Data)
555
- value = value.object[0][Data.key]
569
+ let regex = /\{([^{}]+)\}/
570
+ let match;
571
+ do {
572
+ match = output.match(regex);
573
+ if (match) {
574
+ let value;
575
+ try {
576
+ if (match[1].startsWith('[') && match[1].endsWith(']')) { // {[]} - Dot-notation
577
+ match[1] = match[1].slice(1, -1);
578
+ value = getRenderValue(node, data, match[1], renderAs);
579
+ } else if (match[1].startsWith('(') && match[1].endsWith(')')) { // {()} - CSS Selector and JSON Structure
580
+ match[1] = match[1].slice(1, -1);
581
+ // TODO: utils.queryElements(match[1])
582
+ // element.getValue()
583
+ } else if (output.includes('{(' + match[0] + ')}')) { // {()} - JSON Structure
584
+ match[0] = '{(' + match[0] + ')}'
585
+ try {
586
+ let Data = JSON.parse('{' + match[1].replace(/'/g, '"') + '}');
587
+ if (Data.storage || Data.database || Data.array || Data.object || Data.index) {
588
+ Data.method = 'object.read'
589
+ value = await CoCreate.crud.send(Data)
590
+ value = value.object[0][Data.key]
591
+ }
592
+ } catch (error) {
593
+ value = getRenderValue(node, data, match[1], renderAs)
594
+ }
595
+ } else if (output.includes('{{' + match[1] + '}}')) { // {{}} - Dot-notation && JSON Structure
596
+ match[0] = '{{' + match[1] + '}}'
597
+ try {
598
+ let Data = JSON.parse('{' + match[1].replace(/'/g, '"') + '}');
599
+ if (Data.storage || Data.database || Data.array || Data.object || Data.index) {
600
+ Data.method = 'object.read'
601
+ value = await CoCreate.crud.send(Data)
602
+ value = value.object[0][Data.key]
603
+ }
604
+ } catch (error) {
605
+ value = getRenderValue(node, data, match[1], renderAs)
556
606
  }
557
- } catch (error) {
558
- value = getRenderValue(node, data, match[1], renderAs)
607
+ } else {
608
+ // Otherwise, retun original ouptut
609
+ return output
559
610
  }
560
611
 
561
- if (value || value === "") {
562
- if (typeof value === "object")
563
- value = JSON.stringify(value, null, 2)
612
+ } catch (error) {
613
+ console.error(error)
614
+ }
564
615
 
565
- output = output.replace(match[0], value);
566
- } else if (renderAs) {
567
- if (match[0].startsWith(`{{${renderAs}.`)) {
568
- output = output.replace(match[0], "");
569
- } else {
570
- match = null
571
- }
616
+ if (value || value === "") {
617
+ if (typeof value === "object") {
618
+ value = JSON.stringify(value, null, 2);
619
+ }
620
+ output = output.replace(match[0], value);
621
+ } else if (renderAs) {
622
+ if (match[0].startsWith(`{{${renderAs}.`)) {
623
+ output = output.replace(match[0], "");
572
624
  } else {
573
- match = null
625
+ match = null;
574
626
  }
575
-
627
+ } else {
628
+ match = null;
576
629
  }
630
+ }
631
+ } while (match);
577
632
 
578
- } while (match)
579
- }
580
633
  return output;
581
634
  }
582
635
 
636
+
583
637
  function getRenderValue(node, data, key, renderAs) {
584
638
  let value = getValueFromObject(data, key);
585
639
 
@@ -777,4 +831,4 @@ Observer.init({
777
831
 
778
832
  init()
779
833
 
780
- export default { render, sources, renderedNodes }
834
+ export default { render, renderValue, sources, renderedNodes }