-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Server-demo-v2: Add UI for Create instance.
- Loading branch information
1 parent
b9c07df
commit efb109b
Showing
3 changed files
with
181 additions
and
7 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
leshan-server-demo/webapp2/src/components/InstanceCreateDialog.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<template> | ||
<v-dialog | ||
v-model="show" | ||
hide-overlay | ||
fullscreen | ||
transition="dialog-bottom-transition" | ||
> | ||
<v-card> | ||
<v-card-title class="headline grey lighten-2"> | ||
Create new Instance for Object {{ objectdef.name }} ({{ | ||
objectdef.id | ||
}}) | ||
</v-card-title> | ||
<v-card-text> | ||
<v-form ref="form" @submit.prevent="write"> | ||
<v-text-field | ||
v-model="instance.id" | ||
label="InstanceId" | ||
hint="Let this field empty to let the client choose the instance ID" | ||
></v-text-field> | ||
<v-text-field | ||
v-for="resourcedef in writableResourceDef" | ||
:key="resourcedef.id" | ||
v-model="instance.resources[resourcedef.id]" | ||
:label="resourcedef.name" | ||
></v-text-field> | ||
</v-form> | ||
</v-card-text> | ||
<v-card-actions> | ||
<v-spacer></v-spacer> | ||
<v-btn text @click="create"> | ||
Create | ||
</v-btn> | ||
</v-card-actions> | ||
</v-card> | ||
</v-dialog> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
props: { | ||
value: Boolean, | ||
objectdef: Object, | ||
}, | ||
data() { | ||
return { | ||
instance: { id:null,resources:{}}, | ||
}; | ||
}, | ||
watch: { | ||
value(v) { | ||
// reset local state when dialog is open | ||
if (v) { | ||
this.instanceValue = { id:null,resources:{}}, | ||
this.id = null | ||
} | ||
}, | ||
}, | ||
computed: { | ||
show: { | ||
get() { | ||
return this.value; | ||
}, | ||
set(value) { | ||
this.$emit("input", value); | ||
}, | ||
}, | ||
writableResourceDef() { | ||
return this.objectdef.resourcedefs.filter((def) => | ||
def.operations.includes("W") | ||
); | ||
}, | ||
}, | ||
methods: { | ||
create(){ | ||
this.show = false; | ||
this.$emit("create", this.instance); | ||
}, | ||
}, | ||
}; | ||
</script> |
81 changes: 81 additions & 0 deletions
81
leshan-server-demo/webapp2/src/components/ObjectControl.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<template> | ||
<span> | ||
<request-button | ||
@on-click="openCreateDialog" | ||
ref="C" | ||
>Create</request-button | ||
> | ||
<instance-create-dialog | ||
v-model="showDialog" | ||
:objectdef="objectdef" | ||
@create="create($event)" | ||
/> | ||
</span> | ||
</template> | ||
<script> | ||
import RequestButton from "./RequestButton.vue"; | ||
import InstanceCreateDialog from "./InstanceCreateDialog.vue"; | ||
import { preference } from "vue-preferences"; | ||
const timeout = preference("timeout", { defaultValue: 5 }); | ||
const format = preference("multiformat", { defaultValue: "TLV" }); | ||
export default { | ||
components: { RequestButton ,InstanceCreateDialog }, | ||
props: { objectdef: Object, endpoint: String, value: Object,}, | ||
data() { | ||
return { | ||
dialog: false, | ||
}; | ||
}, | ||
computed: { | ||
showDialog: { | ||
get() { | ||
return this.dialog; | ||
}, | ||
set(value) { | ||
this.dialog = value; | ||
this.$refs.C.resetState(); | ||
}, | ||
}, | ||
}, | ||
methods: { | ||
resourcePath(resourceId) { | ||
return this.path + "/" + resourceId; | ||
}, | ||
requestPath() { | ||
return `api/clients/${encodeURIComponent(this.endpoint)}/${this.objectdef.id}?timeout=${timeout.get()}&format=${format.get()}`; | ||
}, | ||
updateState(content, requestButton) { | ||
let state = !content.valid | ||
? "warning" | ||
: content.success | ||
? "success" | ||
: "error"; | ||
requestButton.changeState(state, content.status); | ||
}, | ||
openCreateDialog() { | ||
this.dialog = true; | ||
}, | ||
create(value) { | ||
let requestButton = this.$refs.C; | ||
let data = {resources:[]}; | ||
for (let id in value.resources){ | ||
data.resources.push({id:id,value:value.resources[id]}) | ||
} | ||
if (value.id) data.id = value.id; | ||
this.axios | ||
.post(this.requestPath(),data) | ||
.then((response) => { | ||
this.updateState(response.data, requestButton); | ||
}) | ||
.catch((error) => { | ||
console.log(error); | ||
requestButton.resetState(); | ||
alert(error); | ||
}); | ||
}, | ||
}, | ||
}; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters