@@ -2,8 +2,11 @@ import { html, TemplateResult } from 'lit-element';
2
2
import { get , translate } from 'lit-translate' ;
3
3
4
4
import {
5
+ cloneElement ,
5
6
createElement ,
7
+ getChildElementsByTagName ,
6
8
getValue ,
9
+ SimpleAction ,
7
10
Wizard ,
8
11
WizardActor ,
9
12
WizardInputElement ,
@@ -44,6 +47,61 @@ export function contentFunctionWizard(
44
47
] ;
45
48
}
46
49
50
+ function updateFunctionAction ( element : Element ) : WizardActor {
51
+ return ( inputs : WizardInputElement [ ] ) : SimpleAction [ ] => {
52
+ const functionAttrs : Record < string , string | null > = { } ;
53
+ const functionKeys = [ 'name' , 'desc' , 'type' ] ;
54
+ functionKeys . forEach ( key => {
55
+ functionAttrs [ key ] = getValue ( inputs . find ( i => i . label === key ) ! ) ;
56
+ } ) ;
57
+
58
+ if (
59
+ functionKeys . some ( key => functionAttrs [ key ] !== element . getAttribute ( key ) )
60
+ ) {
61
+ const newElement = cloneElement ( element , functionAttrs ) ;
62
+ return [
63
+ {
64
+ old : { element } ,
65
+ new : { element : newElement } ,
66
+ } ,
67
+ ] ;
68
+ }
69
+
70
+ return [ ] ;
71
+ } ;
72
+ }
73
+
74
+ export function editFunctionWizard ( element : Element ) : Wizard {
75
+ const name = element . getAttribute ( 'name' ) ;
76
+ const desc = element . getAttribute ( 'desc' ) ;
77
+ const type = element . getAttribute ( 'type' ) ;
78
+ const reservedNames : string [ ] = getChildElementsByTagName (
79
+ element . parentElement ! ,
80
+ 'Function'
81
+ )
82
+ . filter ( sibling => sibling !== element )
83
+ . map ( sibling => sibling . getAttribute ( 'name' ) ! ) ;
84
+
85
+ return [
86
+ {
87
+ title : get ( 'wizard.title.edit' , { tagName : 'Function' } ) ,
88
+ primary : {
89
+ icon : 'save' ,
90
+ label : get ( 'save' ) ,
91
+ action : updateFunctionAction ( element ) ,
92
+ } ,
93
+ content : [
94
+ ...contentFunctionWizard ( {
95
+ name,
96
+ desc,
97
+ type,
98
+ reservedNames,
99
+ } ) ,
100
+ ] ,
101
+ } ,
102
+ ] ;
103
+ }
104
+
47
105
function createFunctionAction ( parent : Element ) : WizardActor {
48
106
return ( inputs : WizardInputElement [ ] ) => {
49
107
const functionAttrs : Record < string , string | null > = { } ;
0 commit comments