|
| 1 | +import { html, TemplateResult } from 'lit-element'; |
| 2 | +import { get, translate } from 'lit-translate'; |
| 3 | + |
| 4 | +import { |
| 5 | + cloneElement, |
| 6 | + getValue, |
| 7 | + Wizard, |
| 8 | + WizardAction, |
| 9 | + WizardActor, |
| 10 | + WizardInput, |
| 11 | +} from '../foundation.js'; |
| 12 | + |
| 13 | +interface ContentOptions { |
| 14 | + refreshTime: string | null; |
| 15 | + sampleRate: string | null; |
| 16 | + dataSet: string | null; |
| 17 | + security: string | null; |
| 18 | + synchSourceId: string | null; |
| 19 | +} |
| 20 | + |
| 21 | +function contentSmvOptsWizard(option: ContentOptions): TemplateResult[] { |
| 22 | + return Object.entries(option).map( |
| 23 | + ([key, value]) => |
| 24 | + html`<wizard-checkbox |
| 25 | + label="${key}" |
| 26 | + .maybeValue=${value} |
| 27 | + nullable |
| 28 | + helper="${translate(`scl.${key}`)}" |
| 29 | + ></wizard-checkbox>` |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +function updateSmvOptsAction(element: Element): WizardActor { |
| 34 | + return (inputs: WizardInput[]): WizardAction[] => { |
| 35 | + const attributes: Record<string, string | null> = {}; |
| 36 | + const attributeKeys = [ |
| 37 | + 'refreshTime', |
| 38 | + 'sampleRate', |
| 39 | + 'dataSet', |
| 40 | + 'security', |
| 41 | + 'synchSourceId', |
| 42 | + ]; |
| 43 | + attributeKeys.forEach(key => { |
| 44 | + attributes[key] = getValue(inputs.find(i => i.label === key)!); |
| 45 | + }); |
| 46 | + |
| 47 | + if ( |
| 48 | + !attributeKeys.some(key => attributes[key] !== element.getAttribute(key)) |
| 49 | + ) |
| 50 | + return []; |
| 51 | + |
| 52 | + const newElement = cloneElement(element, attributes); |
| 53 | + return [{ old: { element }, new: { element: newElement } }]; |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +export function editSmvOptsWizard(element: Element): Wizard { |
| 58 | + const [refreshTime, sampleRate, dataSet, security, synchSourceId] = [ |
| 59 | + 'refreshTime', |
| 60 | + 'sampleRate', |
| 61 | + 'dataSet', |
| 62 | + 'security', |
| 63 | + 'synchSourceId', |
| 64 | + ].map(smvopt => element.getAttribute(smvopt)); |
| 65 | + |
| 66 | + return [ |
| 67 | + { |
| 68 | + title: get('wizard.title.edit', { tagName: element.tagName }), |
| 69 | + element, |
| 70 | + primary: { |
| 71 | + icon: 'save', |
| 72 | + label: get('save'), |
| 73 | + action: updateSmvOptsAction(element), |
| 74 | + }, |
| 75 | + content: [ |
| 76 | + ...contentSmvOptsWizard({ |
| 77 | + refreshTime, |
| 78 | + sampleRate, |
| 79 | + dataSet, |
| 80 | + security, |
| 81 | + synchSourceId, |
| 82 | + }), |
| 83 | + ], |
| 84 | + }, |
| 85 | + ]; |
| 86 | +} |
0 commit comments