|
| 1 | +/** |
| 2 | + * The encoder is used to configure the dial and display segment on an SD+. |
| 3 | + * It is completely optional. |
| 4 | + */ |
| 5 | +export class Encoder { |
| 6 | + /** |
| 7 | + * Default background for the touch display slot |
| 8 | + * @type {string} |
| 9 | + */ |
| 10 | + public background?: string; |
| 11 | + |
| 12 | + /** |
| 13 | + * Default icon for the Property Inspector, dial stack and layout. Falls back |
| 14 | + * to the Action List Icon |
| 15 | + * |
| 16 | + * @type {string} |
| 17 | + */ |
| 18 | + public icon?: string; |
| 19 | + |
| 20 | + /** |
| 21 | + * Either a built-in layout (string) or a path to a JSON file that describes |
| 22 | + * a custom layout. Can be changed with `setFeedbackLayout` event. |
| 23 | + * @see Layout |
| 24 | + * @type {string} |
| 25 | + */ |
| 26 | + public layout?: string; |
| 27 | + |
| 28 | + /** |
| 29 | + * The color used as the background of the Dial Stack |
| 30 | + * @type {string} |
| 31 | + */ |
| 32 | + public stackColor?: string; |
| 33 | + |
| 34 | + /** |
| 35 | + * Part of TriggerDescription. Describes the action performed on rotation of |
| 36 | + * the dial |
| 37 | + * @type {string} |
| 38 | + */ |
| 39 | + public onRotateDescription?: string; |
| 40 | + |
| 41 | + /** |
| 42 | + * Part of TriggerDescription. Describes the action performed on pressing of |
| 43 | + * the dial |
| 44 | + * @type {string} |
| 45 | + */ |
| 46 | + public onPushDescription?: string; |
| 47 | + |
| 48 | + /** |
| 49 | + * Part of TriggerDescription. Describes the action performed on touching the |
| 50 | + * display pad |
| 51 | + * @type {string} |
| 52 | + */ |
| 53 | + public onTouchDescription?: string; |
| 54 | + |
| 55 | + /** |
| 56 | + * Part of TriggerDescription. Describes the action performed on long pressing |
| 57 | + * the display pad |
| 58 | + * @type {string} |
| 59 | + */ |
| 60 | + public onLongTouchDescription?: string; |
| 61 | + |
| 62 | + public toManifest(): Record<string, unknown> { |
| 63 | + const snippet: Record<string, unknown> = { |
| 64 | + background: this.background, |
| 65 | + Icon: this.icon, |
| 66 | + layout: this.layout, |
| 67 | + StackColor: this.stackColor, |
| 68 | + TriggerDescription: { |
| 69 | + Rotate: this.onRotateDescription, |
| 70 | + Push: this.onPushDescription, |
| 71 | + Touch: this.onTouchDescription, |
| 72 | + LongTouch: this.onLongTouchDescription, |
| 73 | + }, |
| 74 | + }; |
| 75 | + |
| 76 | + const optionals: [string, unknown, unknown][] = [ |
| 77 | + ["background", this.background, this.background], |
| 78 | + ["Icon", this.icon, this.icon], |
| 79 | + ["layout", this.layout, this.layout], |
| 80 | + ["StackColor", this.stackColor, this.stackColor], |
| 81 | + ["StackColor", this.stackColor, this.stackColor], |
| 82 | + [ |
| 83 | + "TriggerDescription", |
| 84 | + this.onRotateDescription || |
| 85 | + this.onPushDescription || |
| 86 | + this.onTouchDescription || |
| 87 | + this.onLongTouchDescription, |
| 88 | + this.buildTriggerDescription(), |
| 89 | + ], |
| 90 | + ]; |
| 91 | + |
| 92 | + this.evaluateOptionalValues(optionals, snippet); |
| 93 | + return snippet; |
| 94 | + } |
| 95 | + |
| 96 | + private buildTriggerDescription() { |
| 97 | + const snippet: Record<string, unknown> = {}; |
| 98 | + |
| 99 | + const optionals: [string, unknown, unknown][] = [ |
| 100 | + ["Rotate", this.onRotateDescription, this.onRotateDescription], |
| 101 | + ["Push", this.onPushDescription, this.onPushDescription], |
| 102 | + ["Touch", this.onTouchDescription, this.onTouchDescription], |
| 103 | + ["LongTouch", this.onLongTouchDescription, this.onLongTouchDescription], |
| 104 | + ]; |
| 105 | + |
| 106 | + this.evaluateOptionalValues(optionals, snippet); |
| 107 | + return snippet; |
| 108 | + } |
| 109 | + |
| 110 | + private evaluateOptionalValues( |
| 111 | + optionals: [string, unknown, unknown][], |
| 112 | + object: Record<string, unknown>, |
| 113 | + ): Record<string, unknown> { |
| 114 | + optionals.forEach(([prop, condition, value]) => { |
| 115 | + if (condition) { |
| 116 | + object[prop] = value; |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + return object; |
| 121 | + } |
| 122 | +} |
0 commit comments