Skip to content

Commit 3a59fde

Browse files
committed
Clean-up, fix web component script props
1 parent f8380ba commit 3a59fde

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

components/JsonSchema.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export default Utils.enableHtmlProps({
100100
name: 'JsonSchema',
101101
props: {
102102
schema: {
103-
type: Object | Array,
103+
type: [Object, Array],
104104
default: () => ({})
105105
},
106106
initShown: {

components/ObjectTree.vue

+1-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default Utils.enableHtmlProps({
3232
name: 'ObjectTree',
3333
props: {
3434
data: {
35-
type: Array | Object,
35+
type: [Object, Array],
3636
default: null
3737
},
3838
// Set to null to disable collapsing
@@ -68,9 +68,6 @@ export default Utils.enableHtmlProps({
6868
arr = Array(this.collapseAfter);
6969
}
7070
return [...arr.keys()];
71-
},
72-
collapse() {
73-
return (Array.isArray(this.data) && this.data.length > 50);
7471
}
7572
},
7673
methods: {

components/SearchableList.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default {
4545
name: 'SearchableList',
4646
props: {
4747
data: {
48-
type: Array | Object,
48+
type: [Array, Object],
4949
default: () => ([])
5050
},
5151
identifierKey: {

utils.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Utils as CommonUtils } from '@openeo/js-commons';
2+
import { types } from 'util';
23

34
class Utils extends CommonUtils {
45

@@ -11,9 +12,15 @@ class Utils extends CommonUtils {
1112
default: undefined
1213
}
1314
}
14-
if (vue.props[key].type === Function) {
15+
let types = Array.isArray(vue.props[key].type) ? vue.props[key].type : [vue.props[key].type];
16+
types = types.filter(t => typeof t === 'function').map(t => t.name);
17+
if (types.includes('Function')) {
1518
continue;
1619
}
20+
else if (types.includes('Boolean')) {
21+
// Functions for default are not executed for Boolean, so remove the type
22+
delete vue.props[key].type;
23+
}
1724
let defaultValue = vue.props[key].default;
1825
if (typeof defaultValue === 'function') {
1926
defaultValue = defaultValue();
@@ -28,6 +35,7 @@ class Utils extends CommonUtils {
2835

2936
static readHtmlProp(prop, component, defaultValue = undefined) {
3037
if (Utils.isObject(component) && Utils.isObject(component.$slots) && Array.isArray(component.$slots.default)) {
38+
// Read script tag for specific prop
3139
let el = component.$slots.default.find(slot => typeof slot.tag === 'string' && slot.tag.toUpperCase() === 'SCRIPT' && slot.data.attrs.prop === prop && slot.data.attrs.type === 'application/json');
3240
if (el) {
3341
try {
@@ -37,6 +45,19 @@ class Utils extends CommonUtils {
3745
console.error(`Data passed to prop '${prop}' via script tag is invalid: ${error.message}`);
3846
}
3947
}
48+
// Read script tag containing all props as JSON
49+
let elAll = component.$slots.default.find(slot => typeof slot.tag === 'string' && slot.tag.toUpperCase() === 'SCRIPT' && slot.data.attrs.type === 'application/json');
50+
if (elAll) {
51+
try {
52+
let data = JSON.parse(elAll.data.domProps.innerHTML);
53+
if (Utils.isObject(data) && typeof data[prop] !== 'undefined') {
54+
return data[prop];
55+
}
56+
}
57+
catch (error) {
58+
console.error(`Data passed via script tag is invalid: ${error.message}`);
59+
}
60+
}
4061
}
4162
return defaultValue;
4263
}

0 commit comments

Comments
 (0)