Difference between revisions of "LGUI2:Item View Generators"

From Lavish Software Wiki
Jump to: navigation, search
(Examples)
 
Line 65: Line 65:
 
  joStackPanel[children]:Add["{\"type\":\"textblock\",\"_dock\":\"left\",\"margin\":[2,2,5,2],\"width\":100,\"text\":\"${Context.Args[partTitle].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}"]
+
  joListBoxItem:Set["content","${joStackPanel.AsJSON.Escape[0]}"]
 
   
 
   
  echo Context:SetView["${joListBoxItem.AsJSON.Escape}"]
+
  echo Context:SetView["${joListBoxItem.AsJSON.Escape[0]}"]
  Context:SetView["${joListBoxItem.AsJSON.Escape}"]
+
  Context:SetView["${joListBoxItem.AsJSON.Escape[0]}"]
 
  }
 
  }
  

Latest revision as of 11:27, 15 August 2019

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.

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

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