Skip to content

Commit

Permalink
Add set property on view
Browse files Browse the repository at this point in the history
  • Loading branch information
javalikescript committed Dec 26, 2024
1 parent 7c351b9 commit e763219
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 49 deletions.
6 changes: 3 additions & 3 deletions extensions/web-scripts/scripts-add.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
</template>
<article>
<section>
<button v-on:click="newBlocks"><i class="fa fa-plus"></i>&nbsp;Add Blocks</button>
<button v-on:click="newView"><i class="fa fa-plus"></i>&nbsp;Add View</button>
<button v-on:click="newScript"><i class="fa fa-plus"></i>&nbsp;Add Script</button>
<button v-on:click="newBlocks"><i class="fa fa-shapes"></i>&nbsp;Add Blocks</button>
<button v-on:click="newView"><i class="fa fa-code"></i>&nbsp;Add View</button>
<button v-on:click="newScript"><i class="fa fa-moon"></i>&nbsp;Add Lua</button>
</section>
</article>
</app-page>
5 changes: 4 additions & 1 deletion extensions/web-scripts/scripts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
<article class="cards">
<div class="card" v-for="script in scripts">
<div class="bar">
<p>{{ script.name }}</p>
<p>
<i :class="['fas', script.hasBlocks ? 'fa-shapes' : (script.hasView ? 'fa-code' : 'fa-moon')]"></i>
{{ script.name }}
</p>
<div>
<button v-on:click="reloadScript(script)"><i class="fas fa-redo"></i>&nbsp;Reload</button>
<button v-on:click="pollScript(script)"><i class="far fa-bell"></i>&nbsp;Poll</button>
Expand Down
94 changes: 59 additions & 35 deletions extensions/web-scripts/view-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,36 @@ define(['./config.json', './view.xml'], function(config, viewXml) {
'</app-page>'
].join('\n');

function getPropertyValue(things, properties, path) {
var parts = path.split('/', 2);
var thingId = parts[0];
var propName = parts[1];
var props = properties[thingId];
if (props) {
var value = props[propName];
if (value !== undefined) {
return value;
}
}
var thing = things[thingId];
if (thing) {
var thgProp = thing.properties[propName];
if (thgProp) {
var propType = thgProp['@type'];
switch (propType) {
case 'integer':
case 'number':
return 0;
case 'string':
return '';
case 'boolean':
return false;
}
}
}
return undefined;
}

var viewVue = new Vue({
template: viewTemplate,
data: {
Expand All @@ -20,47 +50,41 @@ define(['./config.json', './view.xml'], function(config, viewXml) {
app.getThings(),
app.getPropertiesByThingId()
]).then(apply(this, function(things, properties) {
this.refreshProperties(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);
}));
},
refreshProperties: function(things, properties) {
var propertyMap = {};
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];
var parts = cfgProp.path.split('/', 2);
var thingId = parts[0];
var propName = parts[1];
var props = properties[thingId];
var value;
if (props) {
value = props[propName];
if (value !== undefined) {
propertyMap[cfgProp.name] = value;
continue;
}
}
var thing = things[thingId];
if (thing) {
var thgProp = thing.properties[propName];
if (thgProp) {
var propType = thgProp['@type'];
switch (propType) {
case 'integer':
case 'number':
value = 0;
break;
case 'string':
value = '';
break;
case 'boolean':
value = false;
break;
}
propertyMap[cfgProp.name] = value;
}
if (cfgProp.name === name) {
return this.setPropertyByPath(cfgProp.path, value);
}
}
this.property = propertyMap;
return Promise.reject('not found');
}
}
});
Expand Down
12 changes: 3 additions & 9 deletions extensions/web-scripts/web-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,16 +437,12 @@ define(['./scripts.xml', './scripts-add.xml',
template: scriptsAddTemplate,
methods: {
newScript: function () {
fetch(scriptPath, {
method: 'PUT'
}).then(assertIsOk).then(function() {
fetch(scriptPath, {method: 'PUT'}).then(assertIsOk).then(function() {
app.replacePage('scripts');
});
},
newBlocks: function () {
fetch(scriptPath, {
method: 'PUT'
}).then(assertIsOk).then(getResponseText).then(function(scriptId) {
fetch(scriptPath, {method: 'PUT', body: 'New Blocks'}).then(assertIsOk).then(getResponseText).then(function(scriptId) {
return fetch(scriptFilesPath + scriptId + '/blocks.xml', {
method: 'PUT',
body: '<xml xmlns="http://www.w3.org/1999/xhtml"></xml>'
Expand All @@ -456,9 +452,7 @@ define(['./scripts.xml', './scripts-add.xml',
});
},
newView: function () {
fetch(scriptPath, {
method: 'PUT'
}).then(assertIsOk).then(getResponseText).then(function(scriptId) {
fetch(scriptPath, {method: 'PUT', body: 'New View'}).then(assertIsOk).then(getResponseText).then(function(scriptId) {
return Promise.all([
fetch(scriptFilesPath + scriptId + '/view.xml', {method: 'PUT', body: '<!-- View content -->'}),
fetch(scriptFilesPath + scriptId + '/config.json', {method: 'PUT', body: '{"id": "view-' + scriptId + '", "title": "View ' + scriptId + '"}'}),
Expand Down
6 changes: 5 additions & 1 deletion extensions/web-scripts/web-scripts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ local REST_SCRIPTS = {
return list
end,
['(engine)?method=PUT'] = function(exchange, engine)
local name = exchange:getRequest():getBody()
if not name or name == '' then
name = 'New script'
end
local dir = engine:getScriptsDirectory()
local scriptName = 'script.lua'
local extId = engine:generateId()
Expand All @@ -34,7 +38,7 @@ local REST_SCRIPTS = {
scriptFile:write('local script = ...\n\n')
local manifestFile = File:new(extDir, 'manifest.json')
local manifest = {
name = 'New script',
name = name,
version = '1.0',
script = scriptName
}
Expand Down

0 comments on commit e763219

Please sign in to comment.