| 
									
										
										
										
											2020-08-31 02:59:47 +02:00
										 |  |  | import {InputElement} from "./InputElement"; | 
					
						
							|  |  |  | import {UIEventSource} from "../../Logic/UIEventSource"; | 
					
						
							|  |  |  | import {DropDown} from "./DropDown"; | 
					
						
							|  |  |  | import {TextField} from "./TextField"; | 
					
						
							|  |  |  | import Combine from "../Base/Combine"; | 
					
						
							|  |  |  | import {Utils} from "../../Utils"; | 
					
						
							| 
									
										
										
										
											2020-09-08 00:33:05 +02:00
										 |  |  | import {UIElement} from "../UIElement"; | 
					
						
							|  |  |  | import {VariableUiElement} from "../Base/VariableUIElement"; | 
					
						
							|  |  |  | import {FromJSON} from "../../Customizations/JSON/FromJSON"; | 
					
						
							| 
									
										
										
										
											2020-08-31 02:59:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default class SingleTagInput extends InputElement<string> { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private readonly _value: UIEventSource<string>; | 
					
						
							|  |  |  |     IsSelected: UIEventSource<boolean>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private key: InputElement<string>; | 
					
						
							|  |  |  |     private value: InputElement<string>; | 
					
						
							|  |  |  |     private operator: DropDown<string> | 
					
						
							| 
									
										
										
										
											2020-09-20 20:34:56 +02:00
										 |  |  |     private readonly helpMessage: UIElement; | 
					
						
							| 
									
										
										
										
											2020-08-31 02:59:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     constructor(value: UIEventSource<string> = undefined) { | 
					
						
							|  |  |  |         super(undefined); | 
					
						
							| 
									
										
										
										
											2020-09-03 19:05:18 +02:00
										 |  |  |         this._value = value ?? new UIEventSource<string>(""); | 
					
						
							| 
									
										
										
										
											2020-09-08 00:33:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-20 20:34:56 +02:00
										 |  |  |         this.helpMessage = new VariableUiElement(this._value.map(tagDef => { | 
					
						
							| 
									
										
										
										
											2020-09-08 00:33:05 +02:00
										 |  |  |                 try { | 
					
						
							|  |  |  |                     FromJSON.Tag(tagDef, ""); | 
					
						
							|  |  |  |                     return ""; | 
					
						
							|  |  |  |                 } catch (e) { | 
					
						
							|  |  |  |                     return `<br/><span class='alert'>${e}</span>` | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         )); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-02 11:37:34 +02:00
										 |  |  |         this.key = TextField.KeyInput(); | 
					
						
							| 
									
										
										
										
											2020-08-31 02:59:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         this.value = new TextField<string>({ | 
					
						
							|  |  |  |                 placeholder: "value - if blank, matches if key is NOT present", | 
					
						
							|  |  |  |                 fromString: str => str, | 
					
						
							| 
									
										
										
										
											2020-09-03 00:00:37 +02:00
										 |  |  |                 toString: str => str, | 
					
						
							| 
									
										
										
										
											2020-09-08 00:33:05 +02:00
										 |  |  |                 value: new UIEventSource<string>("") | 
					
						
							| 
									
										
										
										
											2020-08-31 02:59:47 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2020-09-20 20:34:56 +02:00
										 |  |  |          | 
					
						
							| 
									
										
										
										
											2020-08-31 02:59:47 +02:00
										 |  |  |         this.operator = new DropDown<string>("", [ | 
					
						
							|  |  |  |             {value: "=", shown: "="}, | 
					
						
							|  |  |  |             {value: "~", shown: "~"}, | 
					
						
							|  |  |  |             {value: "!~", shown: "!~"} | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |         this.operator.GetValue().setData("="); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const self = this; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         function updateValue() { | 
					
						
							|  |  |  |             if (self.key.GetValue().data === undefined || | 
					
						
							|  |  |  |                 self.value.GetValue().data === undefined || | 
					
						
							|  |  |  |                 self.operator.GetValue().data === undefined) { | 
					
						
							|  |  |  |                 return undefined; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             self._value.setData(self.key.GetValue().data + self.operator.GetValue().data + self.value.GetValue().data); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.key.GetValue().addCallback(() => updateValue()); | 
					
						
							|  |  |  |         this.operator.GetValue().addCallback(() => updateValue()); | 
					
						
							|  |  |  |         this.value.GetValue().addCallback(() => updateValue()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         function loadValue(value: string) { | 
					
						
							|  |  |  |             if (value === undefined) { | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             let parts: string[]; | 
					
						
							|  |  |  |             if (value.indexOf("=") >= 0) { | 
					
						
							|  |  |  |                 parts = Utils.SplitFirst(value, "="); | 
					
						
							|  |  |  |                 self.operator.GetValue().setData("="); | 
					
						
							|  |  |  |             } else if (value.indexOf("!~") > 0) { | 
					
						
							|  |  |  |                 parts = Utils.SplitFirst(value, "!~"); | 
					
						
							|  |  |  |                 self.operator.GetValue().setData("!~"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             } else if (value.indexOf("~") > 0) { | 
					
						
							|  |  |  |                 parts = Utils.SplitFirst(value, "~"); | 
					
						
							|  |  |  |                 self.operator.GetValue().setData("~"); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 console.warn("Invalid value for tag: ", value) | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             self.key.GetValue().setData(parts[0]); | 
					
						
							|  |  |  |             self.value.GetValue().setData(parts[1]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self._value.addCallback(loadValue); | 
					
						
							|  |  |  |         loadValue(self._value.data); | 
					
						
							|  |  |  |         this.IsSelected = this.key.IsSelected.map( | 
					
						
							|  |  |  |             isSelected => isSelected || this.value.IsSelected.data, [this.value.IsSelected] | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     IsValid(t: string): boolean { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     InnerRender(): string { | 
					
						
							|  |  |  |         return new Combine([ | 
					
						
							| 
									
										
										
										
											2020-09-08 00:33:05 +02:00
										 |  |  |             this.key, this.operator, this.value, | 
					
						
							| 
									
										
										
										
											2020-09-20 20:34:56 +02:00
										 |  |  |             this.helpMessage | 
					
						
							| 
									
										
										
										
											2020-09-08 00:33:05 +02:00
										 |  |  |         ]).Render(); | 
					
						
							| 
									
										
										
										
											2020-08-31 02:59:47 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     GetValue(): UIEventSource<string> { | 
					
						
							|  |  |  |         return this._value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |