Difference between revisions of "LGUI2:Item View Generators"
(Created page with "An Item View Generator (also often referred to as a "factory") in LavishGUI 2 is responsible for creating an LGUI2:itemview when given an LGUI2:Item. This allows...") |
|||
(5 intermediate revisions by the same user not shown) | |||
Line 32: | Line 32: | ||
! code | ! code | ||
| [[LavishScript]] code to execute | | [[LavishScript]] code to execute | ||
+ | |- | ||
+ | !colspan="2"|"property" type | ||
+ | |- | ||
+ | ! template | ||
+ | | Name of the template to use to display the property | ||
+ | |- | ||
+ | ! propertyName | ||
+ | | Name of the JSON property | ||
+ | |- | ||
+ | ! templateProperty | ||
+ | | Name of a JSON property to set, within the specified template, to the value pulled from the specified property | ||
|} | |} | ||
+ | == Implementing the Item View Generator == | ||
+ | The Item View Generator code is expected either execute SetError, or SetView on the [[LGUI2:LS1:lgui2itemviewgeneratorargs|lgui2itemviewgeneratorargs]] that can be accessed as the "Context" Top-Level Object. We will therefore refer to properties/members and methods of this object as Context.Member and Context:Method. | ||
+ | |||
+ | The particular Item View Generator is executing because it was specified as handling the type (Context.ItemType) of item (Context.Item). Data representing the Item is available as Context.Args, and is usually used in the process of creating the View. | ||
+ | |||
+ | After determining if it can create a view, and what goes in the view, the Item View Generator should either perform Context:SetView[json], or Context:SetError[text] and then return. | ||
== Examples == | == Examples == | ||
+ | |||
+ | === Example 1 === | ||
+ | Given an [[LGUI2:Item|Item]] containing the values "partNumber", "partName", and "partTitle", this example generates a listboxitem containing a dockpanel providing a rendering of the 3 values in columns. | ||
+ | |||
+ | method GeneratePartView() | ||
+ | { | ||
+ | echo Controller:GeneratePartView ${Context(type)} ${Context.Args} | ||
+ | |||
+ | ; build an itemview lgui2element json | ||
+ | variable jsonvalue joListBoxItem={} | ||
+ | variable jsonvalue joStackPanel={} | ||
+ | |||
+ | joListBoxItem:Set["jsonTemplate","\"listboxitem\""] | ||
+ | joListBoxItem:Set["horizontalAlignment","\"stretch\""] | ||
+ | joListBoxItem:Set["height":22] | ||
+ | |||
+ | joStackPanel:Set["type","\"dockpanel\""] | ||
+ | joStackPanel:Set["horizontalAlignment","\"stretch\""] | ||
+ | joStackPanel:Set["children","[]"] | ||
+ | joStackPanel[children]:Add["{\"type\":\"textblock\",\"_dock\":\"left\",\"margin\":[2,2,5,2],\"width\":30,\"text\":\"${Context.Args[partNumber]}\"}"] | ||
+ | joStackPanel[children]:Add["{\"type\":\"textblock\",\"_dock\":\"right\",\"margin\":[2,2,5,2],\"text\":\"${Context.Args[partName].Escape}\"}"] | ||
+ | joStackPanel[children]:Add["{\"type\":\"textblock\",\"_dock\":\"left\",\"margin\":[2,2,5,2],\"width\":100,\"text\":\"${Context.Args[partTitle].Escape}\"}"] | ||
+ | |||
+ | joListBoxItem:Set["content","${joStackPanel.AsJSON.Escape[0]}"] | ||
+ | |||
+ | echo Context:SetView["${joListBoxItem.AsJSON.Escape[0]}"] | ||
+ | Context:SetView["${joListBoxItem.AsJSON.Escape[0]}"] | ||
+ | } | ||
+ | |||
+ | |||
+ | {{LGUI2:Topic}} | ||
+ | [[Category:LavishGUI 2]] | ||
+ | [[Category:LavishGUI 2 Items]] |
Latest revision as of 15:10, 7 October 2020
An Item View Generator (also often referred to as a "factory") in LavishGUI 2 is responsible for creating an itemview when given an Item. This allows different types of Items to be represented in different ways.
Contents
Defining an Item View Generator
Item View Generators are created much like Event Handlers, supporting "code" and "method" types.
As a JSON Array
When defining an Item View Generator with a JSON array, each item in the array is expected to be a string, and we will refer to them as parameters.
The first parameter is to be the type of Item View Generator, one of "code" or "method". Following the type parameter will be any type-specific parameters.
- Item View Generator types defined by JSON Arrays
- code: Code requires only one additional parameter, being LavishScript code to execute. Example: ["code","echo Hi mom!"]
- method: Method requires two parameters in this order. First, the LavishScript object (globally accessible) with the method to execute. Second, the name of the method to execute. Example: ["method","MyUIController","GenerateItemView"]. This case is also equivalent to ["code","MyUIController:GenerateItemView"].
As a JSON Object
Item View Generator properties | |
---|---|
type | One of "method" or "code", with additional properties below depending on the type |
"method" type | |
object | The LavishScript object (globally accessible) with the method to execute |
method | The method on the object to execute |
"code" type | |
code | LavishScript code to execute |
"property" type | |
template | Name of the template to use to display the property |
propertyName | Name of the JSON property |
templateProperty | Name of a JSON property to set, within the specified template, to the value pulled from the specified property |
Implementing the Item View Generator
The Item View Generator code is expected either execute SetError, or SetView on the lgui2itemviewgeneratorargs that can be accessed as the "Context" Top-Level Object. We will therefore refer to properties/members and methods of this object as Context.Member and Context:Method.
The particular Item View Generator is executing because it was specified as handling the type (Context.ItemType) of item (Context.Item). Data representing the Item is available as Context.Args, and is usually used in the process of creating the View.
After determining if it can create a view, and what goes in the view, the Item View Generator should either perform Context:SetView[json], or Context:SetError[text] and then return.
Examples
Example 1
Given an Item containing the values "partNumber", "partName", and "partTitle", this example generates a listboxitem containing a dockpanel providing a rendering of the 3 values in columns.
method GeneratePartView() { echo Controller:GeneratePartView ${Context(type)} ${Context.Args} ; build an itemview lgui2element json variable jsonvalue joListBoxItem={} variable jsonvalue joStackPanel={} joListBoxItem:Set["jsonTemplate","\"listboxitem\""] joListBoxItem:Set["horizontalAlignment","\"stretch\""] joListBoxItem:Set["height":22] joStackPanel:Set["type","\"dockpanel\""] joStackPanel:Set["horizontalAlignment","\"stretch\""] joStackPanel:Set["children","[]"] joStackPanel[children]:Add["{\"type\":\"textblock\",\"_dock\":\"left\",\"margin\":[2,2,5,2],\"width\":30,\"text\":\"${Context.Args[partNumber]}\"}"] joStackPanel[children]:Add["{\"type\":\"textblock\",\"_dock\":\"right\",\"margin\":[2,2,5,2],\"text\":\"${Context.Args[partName].Escape}\"}"] joStackPanel[children]:Add["{\"type\":\"textblock\",\"_dock\":\"left\",\"margin\":[2,2,5,2],\"width\":100,\"text\":\"${Context.Args[partTitle].Escape}\"}"] joListBoxItem:Set["content","${joStackPanel.AsJSON.Escape[0]}"] echo Context:SetView["${joListBoxItem.AsJSON.Escape[0]}"] Context:SetView["${joListBoxItem.AsJSON.Escape[0]}"] }
LavishGUI 2 Topics