action

package
v0.21.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package action defines actions that you can trigger using events. Normally you would do this with the On() function that all GoRADD controls have.

For example:

  button := NewButton(p, "okButton").SetText("OK")
	 button.On(event.Click(), action.Message(javascript.JsCode("event.target.value") + "was clicked"))

will create a Message action that responds to a button click.

There are three types of actions:

Javascript Actions execute javascript code that is handled immediately by the browser. Goradd provides a number of standard actions, like Redirect. However, the Javascript action can execute any javascript.

Server and Ajax actions call the control.Action() function of a GoRADD control when the event is triggered. You specify the control id of the receiving control, and an integer representing the action. The Action() function can then do whatever is needed.

Server actions cause a page to completely refresh, whereas Ajax actions use the javascript XMLHttpRequest mechanism to pass data without a complete refresh. In response to an Ajax action, a control can call its Refresh() function to redraw just that control.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionGroup added in v0.2.3

type ActionGroup struct {
	Actions []ActionI
}

ActionGroup groups multiple actions as a single action. To use it, call Group() and pass a list of actions.

func Group added in v0.2.3

func Group(actions ...ActionI) ActionGroup

Group joins multiple actions into a single action.

func (ActionGroup) GetCallbackAction added in v0.2.3

func (g ActionGroup) GetCallbackAction() FrameworkCallbackActionI

GetCallbackAction returns the embedded callback action in the group, if one exists. Note that you can only have at most one callback action in a group

func (ActionGroup) HasCallbackAction added in v0.3.0

func (g ActionGroup) HasCallbackAction() bool

HasCallbackAction is called by the framework to determine if the action group has a callback action in it.

func (ActionGroup) HasServerAction added in v0.2.3

func (g ActionGroup) HasServerAction() bool

HasServerAction is called by the framework to determine if the action group has a server action in it.

func (ActionGroup) RenderScript added in v0.4.0

func (g ActionGroup) RenderScript(params RenderParams) (s string)

RenderScript renders the group of actions as a single action.

type ActionI

type ActionI interface {
	// RenderScript is called by the framework to return the action's javascript.
	RenderScript(params RenderParams) string
}

ActionI is an interface that defines actions that can be triggered by events

func AddClass

func AddClass(controlID string, classes string) ActionI

AddClass will add the given class, or space separated classes, to the html object specified by the id.

func Blur

func Blur(controlID string) ActionI

Blur will blur the html object specified by the id.

func Confirm

func Confirm(m interface{}) ActionI

Confirm will put up a standard browser confirmation dialog box, and will cancel any following actions if the user does not agree.

func Css added in v0.2.0

func Css(controlID string, property string, value interface{}) ActionI

Css will set the css property to the given value on the given html object.

func Focus

func Focus(controlID string) ActionI

Focus will focus the html object specified by the id.

func GoraddFunction added in v0.2.0

func GoraddFunction(operation string, arguments ...interface{}) ActionI

GoraddFunction calls a function defined on the global goradd object that is in goradd.js.

func Hide added in v0.5.0

func Hide(controlID string) ActionI

Hide will hide the given control

func Javascript

func Javascript(js string) ActionI

Javascript will execute the given javascript

func Message

func Message(m interface{}) ActionI

Message returns an action that will display a standard browser alert message. Specify a string, or one of the javascript.* types.

func Redirect

func Redirect(url string) ActionI

Redirect will navigate to the given page.

func Refresh added in v0.12.0

func Refresh(id string) ActionI

Refresh will cause the given control to redraw

func RemoveClass added in v0.2.0

func RemoveClass(controlID string, classes string) ActionI

RemoveClass will turn off the given space separated classes in the html object specified by the id.

func Select

func Select(controlID string) ActionI

Select will select all the text in the html object specified by the id. The object should be a text box.

func SetControlValue

func SetControlValue(id string, key string, value interface{}) ActionI

SetControlValue is primarily used by custom controls to set a value that eventually can get picked up by the control in the UpdateFormValues function. It is an aid to tying javascript powered widgets together with the go version of the control. Value gets converted to a javascript value, so use the javascript.* helpers if you want to interpret a javascript value and pass it on. For example:

action.SetControlValue(myControl.ID(), "myKey", javascript.NewJsCode("event.target.id"))

will pass the id of the target of an event to the receiver of the action.

func Show added in v0.5.0

func Show(controlID string) ActionI

Show will show the given control if it is hidden

func ToggleClass

func ToggleClass(controlID string, classes string) ActionI

ToggleClass will turn on or off the given space separated classes in the html object specified by the id.

func Trigger

func Trigger(controlID string, event string, data interface{}) ActionI

Trigger will trigger a javascript event on a control

func WidgetFunction added in v0.2.0

func WidgetFunction(controlID string, operation string, arguments ...interface{}) ActionI

WidgetFunction calls a goradd widget function in javascript on an HTML control with the given id. Available functions are defined by the widget object in goradd.js

type CallbackAction added in v0.3.0

type CallbackAction struct {
	ActionID           int
	DestControlID      string
	SubID              string
	Value              interface{}
	ValidationOverride int // overrides the validation setting that is on the control
	CallAsync          bool
}

CallbackAction is a kind of superclass for Ajax and Server actions. Do not use this class directly.

This class is exported so that the encoder can see its fields.

func (*CallbackAction) GetActionValue added in v0.3.0

func (a *CallbackAction) GetActionValue() interface{}

GetActionValue returns the action value given to the action when it was created.

func (*CallbackAction) GetDestinationControlID added in v0.3.0

func (a *CallbackAction) GetDestinationControlID() string

GetDestinationControlID returns the control that the action will operate on.

func (*CallbackAction) GetDestinationControlSubID added in v0.3.0

func (a *CallbackAction) GetDestinationControlSubID() string

GetDestinationControlSubID returns the sub id so that a composite control can send the action to a sub control.

func (*CallbackAction) ID added in v0.3.0

func (a *CallbackAction) ID() int

ID returns the action id that was defined when the action was created.

type CallbackActionI

type CallbackActionI interface {
	ActionI
	ActionValue(v interface{}) CallbackActionI
	Validator(v int) CallbackActionI
	Async() CallbackActionI
	DestinationControlID(id string) CallbackActionI
}

CallbackActionI defines actions that result in a callback to the server. Specifically Server and Ajax actions are defined for now. There is potential for a Message action, like through WebSocket, PubHub, etc.

func Ajax

func Ajax(destControlId string, actionID int) CallbackActionI

Ajax creates an ajax action. When the action fires, the Action() function of the Goradd control identified by the destControlId will be called, and the given actionID will be the ID passed in the Params of the call. You can specify a sub id which indicates that the action should be sent to something inside the main control by concatenating the control's id with another id that indicates the internal destination, separated with an underscore.

The returned action uses a Builder pattern to add options, so for example you might call:

myControl.On(event.Click(), action.Ajax("myControl", MyActionIdConst).ActionValue("myActionValue").Async())

func Server

func Server(destControlId string, actionId int) CallbackActionI

Server creates a server action, which is an action that will use a POST submission mechanism to trigger the action. Generally, with modern browsers, server actions are not that useful, since they cause an entire page to reload, while Ajax actions do not, and so are Ajax actions are quicker to process. However, there are special cases where a server action might be useful, like: - You are moving to a new page anyway. - You are having trouble making an Ajax action work for some reason, and a Server action might get around the problem. - You are submitting a multi-part form, like when uploading a file. When the action fires, the Action() function of the Goradd control identified by the destControlId will be called, and the given actionID will be the ID passed in the Params of the call. You can specify a sub id which indicates that the action should be sent to something inside the main control by concatenating the control's id with another id that indicates the internal destination, separated with an underscore.

The returned action uses a Builder pattern to add options, so for example you might call:

myControl.On(event.Click(), action.Server("myControl", MyActionIdConst).ActionValue("myActionValue").Async())

type FrameworkCallbackActionI added in v0.19.0

type FrameworkCallbackActionI interface {
	// ID returns the id assigned to the action when the action was created.
	ID() int
	// GetDestinationControlID returns the id that the action was sent to.
	GetDestinationControlID() string
	// GetDestinationControlSubID returns the id of the sub-control that is the destination of the action, if one was assigned.
	GetDestinationControlSubID() string
	// GetActionValue returns the action value that was assigned to the action when the action was fired.
	GetActionValue() interface{}
	// IsServerAction returns true if this is a server action.
	IsServerAction() bool
}

FrameworkCallbackActionI is the interface the framework uses to access actions. Callback actions must satisfy this interface, as well as the CallbackActionI interface.

This is separated here so that IDEs will not pick up these functions for framework users.

type Params added in v0.21.0

type Params struct {
	// Id is the id assigned when the action is created
	ID int
	// Action is an interface to the action itself
	Action ActionI
	// ControlID is the control that originated the action
	ControlId string
	// contains filtered or unexported fields
}

Params are sent to the control.Action() function in response to a user action. Each action might contain one of three kinds of values:

  • Action value, a value assigned to the action when it is created,
  • Control value, a value returned by certain custom controls
  • Event value, a value assigned by certain event types

Use the accessor functions like Params.ActionValueInt or Params.ControlValueString to extract an appropriately typed value from the action.

func NewActionParams added in v0.21.0

func NewActionParams(id int, action ActionI, controlId string, rawValues RawActionValues) Params

NewActionParams is used internally by the framework to create new action parameters. You should not normally need to call this funtion.

func (*Params) ActionValue added in v0.21.0

func (a *Params) ActionValue(i interface{}) (ok bool, err error)

ActionValue will return the action value into the given object using json.Unmarshal. You should primarily use it to get object or array values out of the action value. If you are expecting a basic type, use one of the ActionValue* helper functions instead. The given value should be a pointer to an object or variable that is the expected type for the data. ok will be false if no data was found. It will return an error if the data was found, but could not be converted to the given type.

func (*Params) ActionValueBool added in v0.21.0

func (a *Params) ActionValueBool() bool

ActionValueBool returns the action value as a bool.

func (*Params) ActionValueFloat added in v0.21.0

func (a *Params) ActionValueFloat() float64

ActionValueFloat returns the action value as a float64.

func (*Params) ActionValueInt added in v0.21.0

func (a *Params) ActionValueInt() int

ActionValueInt returns the action value as an integer.

func (*Params) ActionValueString added in v0.21.0

func (a *Params) ActionValueString() string

ActionValueString returns the action value as a string. It will convert to a string, even if the value is not a string.

func (*Params) ControlValue added in v0.21.0

func (a *Params) ControlValue(i interface{}) (ok bool, err error)

ControlValue will return the control value into the given object using json.Unmarshal. You should primarily use it to get object or array values out of the control value. If you are expecting a basic type, use one of the ControlValue* helper functions instead. The given value should be a pointer to an object or variable that is the expected type for the data. ok will be false if no data was found. It will return an error if the data was found, but could not be converted to the given type.

func (*Params) ControlValueBool added in v0.21.0

func (a *Params) ControlValueBool() (ret bool)

ControlValueBool returns the control value as a bool.

func (*Params) ControlValueFloat added in v0.21.0

func (a *Params) ControlValueFloat() float64

ControlValueFloat returns the control value as a float64.

func (*Params) ControlValueInt added in v0.21.0

func (a *Params) ControlValueInt() int

ControlValueInt returns the control value as an int.

func (*Params) ControlValueString added in v0.21.0

func (a *Params) ControlValueString() string

ControlValueString returns the control value as a string. It will convert to a string, even if the value is not a string.

func (*Params) EventValue added in v0.21.0

func (a *Params) EventValue(i interface{}) (ok bool, err error)

EventValue will attempt to put the Event value into the given object using json.Unmarshal. You should primarily use it to get object or array values out of the Action value. If you are expecting a basic type, use one of the EventValue* helper functions instead. The given value should be a pointer to an object or variable that is the expected type for the data. ok will be false if no data was found. It will return an error if the data was found, but could not be converted to the given type.

func (*Params) EventValueBool added in v0.21.0

func (a *Params) EventValueBool() bool

EventValueBool returns the event value as a bool. To be false, the value should be a boolean false, a numeric 0, an empty string, null or undefined on the client side. Otherwise, will return true.

func (*Params) EventValueFloat added in v0.21.0

func (a *Params) EventValueFloat() float64

EventValueFloat returns the event value as a float64. If the value was not numeric, it will return 0.

func (*Params) EventValueInt added in v0.21.0

func (a *Params) EventValueInt() int

EventValueInt returns the event value as an integer. If the value was a floating point value at the client, it will be truncated to an integer. If the value is not numeric, will return 0.

func (*Params) EventValueString added in v0.21.0

func (a *Params) EventValueString() string

EventValueString returns the event value as a string. It will convert to a string, even if the value is not a string.

func (*Params) EventValueStringMap added in v0.21.0

func (a *Params) EventValueStringMap() (m map[string]string)

EventValueStringMap returns the event value as a map[string]string.

type RawActionValues added in v0.21.0

type RawActionValues struct {
	Event   json.RawMessage `json:"event"`
	Control json.RawMessage `json:"control"`
	Action  json.RawMessage `json:"action"`
}

RawActionValues is the structure representing the json values sent in an ActionParam. Use the helper functions to get to the values.

type RenderParams added in v0.4.0

type RenderParams struct {
	// TriggeringControlID is the id of the control that triggered the action
	TriggeringControlID string
	// ControlActionValue is the control action value that will be received by the Action() function.
	ControlActionValue interface{}
	// EventID is the event that triggered the action
	EventID uint16
	// EventActionValue is the event action value that will be received by the Action() function.
	EventActionValue interface{}
}

RenderParams is used by the framework to give information that helps with rendering actions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL