Skip to content

Commit

Permalink
Pass view options to script
Browse files Browse the repository at this point in the history
  • Loading branch information
javalikescript committed Jan 5, 2025
1 parent ee77ccd commit 4e793ce
Showing 1 changed file with 76 additions and 75 deletions.
151 changes: 76 additions & 75 deletions extensions/web-scripts/view-init.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,5 @@
define(['./config.json', './view.xml'], function(config, viewXml) {

function chainInitFn(f1, f2) {
return function(a) { f1(a); f2(a); };
}

var initFn = function() {};
for(;;) {
var i = viewXml.indexOf('<script>');
if (i < 0) {
break;
}
var j = viewXml.indexOf('</script>', i);
if (j < 0) {
break;
}
var src = viewXml.substring(i + 8, j);
viewXml = viewXml.substring(0, i) + viewXml.substring(j + 9);
var fn = Function('view', '"use strict"; ' + src);
initFn = chainInitFn(initFn, fn);
}

var viewTemplate = [
'<app-page id="' + config.id + '" title="' + config.title + '">',
viewXml,
'</app-page>'
].join('\n');

function getPropertyValue(things, properties, path) {
var parts = path.split('/', 2);
var thingId = parts[0];
Expand Down Expand Up @@ -56,61 +30,88 @@ define(['./config.json', './view.xml'], function(config, viewXml) {
return undefined;
}

var viewVue = new Vue({
template: viewTemplate,
data: {
property: {}
},
methods: {
onShow: function() {
this.onDataChange();
var script = '';
for(;;) {
// type="text/javascript"
var i = viewXml.indexOf('<script>');
if (i < 0) {
break;
}
var j = viewXml.indexOf('</script>', i);
if (j < 0) {
break;
}
script += '\n' + viewXml.substring(i + 8, j);
viewXml = viewXml.substring(0, i) + viewXml.substring(j + 9);
}

var options = {
template: [
'<app-page id="' + config.id + '" title="' + config.title + '">',
viewXml,
'</app-page>'
].join('\n')
};

if (config.properties && config.properties.length > 0) {
assignMap(options, {
data: {
property: {}
},
onDataChange: function() {
Promise.all([
app.getThings(),
app.getPropertiesByThingId()
]).then(apply(this, function(things, properties) {
var propertyMap = {};
methods: {
onShow: function() {
this.onDataChange();
},
onDataChange: function() {
Promise.all([
app.getThings(),
app.getPropertiesByThingId()
]).then(apply(this, function(things, properties) {
var propertyMap = {};
for (var i = 0; i < config.properties.length; i++) {
var cfgProp = config.properties[i];
propertyMap[cfgProp.name] = getPropertyValue(things, properties, cfgProp.path);
}
this.property = propertyMap;
}));
},
getPropertyByPath: function(path) {
return Promise.all([
app.getThings(),
app.getPropertiesByThingId()
]).then(apply(this, function(things, properties) {
return getPropertyValue(things, properties, path);
}));
},
setPropertyByPath: function(path, value) {
var parts = path.split('/', 2);
var thingId = parts[0];
var propName = parts[1];
var valueByName = {};
valueByName[propName] = value;
return fetch('/things/' + thingId + '/properties', {
method: 'PUT',
body: JSON.stringify(valueByName)
}).then(rejectIfNotOk);
},
setProperty: function(name, value) {
for (var i = 0; i < config.properties.length; i++) {
var cfgProp = config.properties[i];
propertyMap[cfgProp.name] = getPropertyValue(things, properties, cfgProp.path);
}
this.property = propertyMap;
}));
},
getPropertyByPath: function(path) {
return Promise.all([
app.getThings(),
app.getPropertiesByThingId()
]).then(apply(this, function(things, properties) {
return getPropertyValue(things, properties, path);
}));
},
setPropertyByPath: function(path, value) {
var parts = path.split('/', 2);
var thingId = parts[0];
var propName = parts[1];
var valueByName = {};
valueByName[propName] = value;
return fetch('/things/' + thingId + '/properties', {
method: 'PUT',
body: JSON.stringify(valueByName)
}).then(rejectIfNotOk);
},
setProperty: function(name, value) {
for (var i = 0; i < config.properties.length; i++) {
var cfgProp = config.properties[i];
if (cfgProp.name === name) {
return this.setPropertyByPath(cfgProp.path, value);
if (cfgProp.name === name) {
return this.setPropertyByPath(cfgProp.path, value);
}
}
return Promise.reject('not found');
}
return Promise.reject('not found');
}
}
});

addPageComponent(viewVue, config.icon);
});
}

initFn(viewVue);
if (script.length > 0) {
var fn = Function('options', 'config', '"use strict";' + script);
fn.call(this, options, config);
}
var vue = new Vue(options);
addPageComponent(vue, config.icon);

});

0 comments on commit 4e793ce

Please sign in to comment.