1010 </f7-nav-right >
1111 </f7-navbar >
1212 <f7-toolbar tabbar position =" top" >
13- <f7-link @click =" switchTab('design', fromYaml )" :tab-link-active =" currentTab === 'design'" class =" tab-link" >
13+ <f7-link @click =" switchTab('design')" :tab-link-active =" currentTab === 'design'" class =" tab-link" >
1414 Design
1515 </f7-link >
16- <f7-link @click =" switchTab('code', toYaml )" :tab-link-active =" currentTab === 'code'" class =" tab-link" >
16+ <f7-link @click =" switchTab('code')" :tab-link-active =" currentTab === 'code'" class =" tab-link" >
1717 Code
1818 </f7-link >
1919 </f7-toolbar >
2020
2121 <f7-tabs v-if =" ready" >
22- <f7-tab id =" design" @tab:show = " () => this.currentTab = 'design' " :tab-active =" currentTab === 'design'" >
22+ <f7-tab id =" design" :tab-active =" currentTab === 'design'" >
2323 <f7-block class =" block-narrow" v-if =" item.name || item.created === false" >
2424 <f7-col v-if =" !editable" >
2525 <div class =" padding-left" >
3838 </f7-block >
3939 </f7-tab >
4040
41- <f7-tab id =" code" @tab:show =" () => { this.currentTab = 'code'; toYaml() }" :tab-active =" currentTab === 'code'" >
42- <f7-icon v-if =" !editable" f7 =" lock" class =" float-right margin" style =" opacity :0.5 ; z-index : 4000 ; user-select : none ;" size =" 50" color =" gray" :tooltip =" notEditableMsg" />
43- <editor class =" item-code-editor" mode =" application/vnd.openhab.item+yaml" :value =" itemYaml" @input =" onEditorInput" :readOnly =" !editable" />
41+ <f7-tab id =" code" :tab-active =" currentTab === 'code'" >
42+ <code-editor ref =" codeEditor"
43+ object-type =" items"
44+ :object =" item"
45+ :object-id =" item.name"
46+ :read-only =" !editable"
47+ :read-only-msg =" notEditableMsg"
48+ @updated =" updateItem"
49+ @changed =" onCodeChanged" />
4450 </f7-tab >
4551 </f7-tabs >
4652 </f7-page >
4753</template >
4854
49- <style lang="stylus">
50- .item-code-editor.vue-codemirror
51- display block
52- top calc (var (-- f7-navbar-height ) + var (-- f7-tabbar-height ))
53- height calc (100% - 2 * var (-- f7-navbar-height ))
54- width 100%
55- .yaml-message
56- display block
57- position absolute
58- top 80%
59- white-space pre-wrap
60- </style >
61-
6255<script >
6356import cloneDeep from ' lodash/cloneDeep'
6457import fastDeepEqual from ' fast-deep-equal/es6'
6558
6659import * as Types from ' @/assets/item-types.js'
67- import YAML from ' yaml'
6860
6961import ItemForm from ' @/components/item/item-form.vue'
7062
7163import DirtyMixin from ' ../dirty-mixin'
7264import ItemMixin from ' @/components/item/item-mixin'
65+ import CodeEditor from ' @/components/config/controls/code-editor.vue'
7366
7467export default {
7568 mixins: [DirtyMixin, ItemMixin],
7669 props: [' itemName' , ' createMode' , ' itemCopy' ],
7770 components: {
7871 ItemForm,
79- ' editor ' : () => import ( /* webpackChunkName: "script-editor" */ ' @/components/config/controls/script-editor.vue ' )
72+ CodeEditor
8073 },
8174 data () {
8275 return {
8376 ready: false ,
8477 loading: false ,
8578 item: {},
8679 savedItem: {},
87- itemYaml: ' ' ,
80+ itemDirty: false ,
81+ codeDirty: false ,
8882 items: [],
8983 types: Types,
9084 semanticClasses: this .$store .getters .semanticClasses ,
@@ -110,12 +104,14 @@ export default {
110104 }
111105 },
112106 watch: {
107+ itemDirty : function () { this .dirty = this .itemDirty || this .codeDirty },
108+ codeDirty : function () { this .dirty = this .itemDirty || this .codeDirty },
113109 item: {
114110 handler : function () {
115111 if (! this .loading ) { // ignore changes during loading
116112 const itemClone = cloneDeep (this .item )
117113 delete itemClone .functionKey
118- this .dirty = ! fastDeepEqual (itemClone, this .savedItem )
114+ this .itemDirty = ! fastDeepEqual (itemClone, this .savedItem )
119115 }
120116 },
121117 deep: true
@@ -140,6 +136,19 @@ export default {
140136 ev .preventDefault ()
141137 }
142138 },
139+ switchTab (tab ) {
140+ if (this .currentTab === tab) return
141+ if (this .currentTab === ' code' && this .codeDirty ) {
142+ this .$refs .codeEditor .parseCode (() => { this .codeDirty = false })
143+ }
144+ this .currentTab = tab
145+ if (this .currentTab === ' code' ) {
146+ this .$refs .codeEditor .generateCode ()
147+ }
148+ },
149+ onCodeChanged (codeDirty ) {
150+ this .codeDirty = codeDirty
151+ },
143152 load () {
144153 if (this .loading ) return
145154 this .loading = true
@@ -173,9 +182,15 @@ export default {
173182 },
174183 save () {
175184 if (! this .editable ) return
176- if (this .currentTab === ' code' ) {
177- if (! this .fromYaml ()) return
185+
186+ if (this .currentTab === ' code' && this .codeDirty ) {
187+ this .$refs .codeEditor .parseCode (() => {
188+ this .codeDirty = false
189+ this .save ()
190+ })
191+ return
178192 }
193+
179194 if (this .validateItemName (this .item .name ) !== ' ' ) return this .$f7 .dialog .alert (' Please give the Item a valid name: ' + this .validateItemName (this .item .name )).open ()
180195 if (! this .item .type || ! this .types .ItemTypes .includes (this .item .type .split (' :' )[0 ])) return this .$f7 .dialog .alert (' Please give Item a valid type' ).open ()
181196
@@ -216,7 +231,7 @@ export default {
216231 }).open ()
217232 }
218233
219- this .dirty = false
234+ this .itemDirty = this . codeDirty = false
220235 if (this .createMode ) {
221236 this .$f7router .navigate (' /settings/items/' + this .item .name )
222237 } else {
@@ -230,34 +245,15 @@ export default {
230245 }).open ()
231246 })
232247 },
233- onEditorInput (value ) {
234- this .itemYaml = value
235- },
236- toYaml () {
237- const yamlObj = {
238- label: this .item .label ,
239- type: this .item .type ,
240- icon: this .item .category || ' ' ,
241- groupNames: this .item .groupNames || [],
242- tags: this .item .tags
243- // metadata: this.item.metadata
244- }
245- if (this .item .type === ' Group' ) {
246- yamlObj .groupType = this .item .groupType || ' None'
247- yamlObj .function = this .item .function || ' None'
248- }
249- this .itemYaml = YAML .stringify (yamlObj)
250- },
251- fromYaml () {
248+ updateItem (updatedItem ) {
252249 if (! this .editable ) return false
253250 try {
254- const updatedItem = YAML .parse (this .itemYaml )
255251 if (updatedItem === null ) return false
256252 if (updatedItem .groupNames == null ) updatedItem .groupNames = []
257253 if (updatedItem .tags == null ) updatedItem .tags = []
258254 this .$set (this .item , ' label' , updatedItem .label )
259255 this .$set (this .item , ' type' , updatedItem .type )
260- this .$set (this .item , ' category' , updatedItem .icon )
256+ this .$set (this .item , ' category' , updatedItem .category )
261257 this .$set (this .item , ' groupNames' , updatedItem .groupNames )
262258 this .$set (this .item , ' groupType' , updatedItem .groupType )
263259 this .$set (this .item , ' function' , updatedItem .function )
0 commit comments