mirror of https://github.com/anoshenko/rui.git
Adde binding parameter to CreateView functions
This commit is contained in:
parent
bbbaf28aba
commit
73b14ed78a
|
@ -92,7 +92,7 @@ func (detailsView *detailsViewData) setFunc(tag PropertyName, value any) []Prope
|
||||||
value.setParentID(detailsView.htmlID())
|
value.setParentID(detailsView.htmlID())
|
||||||
|
|
||||||
case DataObject:
|
case DataObject:
|
||||||
if view := CreateViewFromObject(detailsView.Session(), value); view != nil {
|
if view := CreateViewFromObject(detailsView.Session(), value, nil); view != nil {
|
||||||
detailsView.setRaw(Summary, view)
|
detailsView.setRaw(Summary, view)
|
||||||
view.setParentID(detailsView.htmlID())
|
view.setParentID(detailsView.htmlID())
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -336,7 +336,7 @@ func (listView *listViewData) setItems(value any) []PropertyName {
|
||||||
items := make([]View, len(value))
|
items := make([]View, len(value))
|
||||||
for i, val := range value {
|
for i, val := range value {
|
||||||
if val.IsObject() {
|
if val.IsObject() {
|
||||||
if view := CreateViewFromObject(session, val.Object()); view != nil {
|
if view := CreateViewFromObject(session, val.Object(), nil); view != nil {
|
||||||
items[i] = view
|
items[i] = view
|
||||||
} else {
|
} else {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -118,7 +118,7 @@ func (resizable *resizableData) setFunc(tag PropertyName, value any) []PropertyN
|
||||||
newContent = value
|
newContent = value
|
||||||
|
|
||||||
case DataObject:
|
case DataObject:
|
||||||
if newContent = CreateViewFromObject(resizable.Session(), value); newContent == nil {
|
if newContent = CreateViewFromObject(resizable.Session(), value, nil); newContent == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
114
viewFactory.go
114
viewFactory.go
|
@ -6,7 +6,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var viewCreators = map[string]func(Session) View{
|
var systemViewCreators = map[string]func(Session) View{
|
||||||
"View": newView,
|
"View": newView,
|
||||||
"ColumnLayout": newColumnLayout,
|
"ColumnLayout": newColumnLayout,
|
||||||
"ListLayout": newListLayout,
|
"ListLayout": newListLayout,
|
||||||
|
@ -36,54 +36,56 @@ var viewCreators = map[string]func(Session) View{
|
||||||
"VideoPlayer": newVideoPlayer,
|
"VideoPlayer": newVideoPlayer,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var viewCreate map[string]func(Session) View = nil
|
||||||
|
|
||||||
|
func viewCreators() map[string]func(Session) View {
|
||||||
|
if viewCreate == nil {
|
||||||
|
viewCreate = map[string]func(Session) View{}
|
||||||
|
for tag, fn := range systemViewCreators {
|
||||||
|
viewCreate[strings.ToLower(tag)] = fn
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return viewCreate
|
||||||
|
}
|
||||||
|
|
||||||
// RegisterViewCreator register function of creating view
|
// RegisterViewCreator register function of creating view
|
||||||
func RegisterViewCreator(tag string, creator func(Session) View) bool {
|
func RegisterViewCreator(tag string, creator func(Session) View) bool {
|
||||||
builtinViews := []string{
|
loTag := strings.ToLower(tag)
|
||||||
"View",
|
for name := range systemViewCreators {
|
||||||
"ViewsContainer",
|
if name == loTag {
|
||||||
"ColumnLayout",
|
ErrorLog(`It is forbidden to override the function of ` + tag + ` creating`)
|
||||||
"ListLayout",
|
|
||||||
"GridLayout",
|
|
||||||
"StackLayout",
|
|
||||||
"TabsLayout",
|
|
||||||
"AbsoluteLayout",
|
|
||||||
"Resizable",
|
|
||||||
"DetailsView",
|
|
||||||
"TextView",
|
|
||||||
"Button",
|
|
||||||
"Checkbox",
|
|
||||||
"DropDownList",
|
|
||||||
"ProgressBar",
|
|
||||||
"NumberPicker",
|
|
||||||
"ColorPicker",
|
|
||||||
"DatePicker",
|
|
||||||
"TimePicker",
|
|
||||||
"EditView",
|
|
||||||
"ListView",
|
|
||||||
"CanvasView",
|
|
||||||
"ImageView",
|
|
||||||
"TableView",
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, name := range builtinViews {
|
|
||||||
if name == tag {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
viewCreators[tag] = creator
|
viewCreators()[loTag] = creator
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateViewFromObject create new View and initialize it by Node data
|
// CreateViewFromObject create new View and initialize it by DataObject data. Parameters:
|
||||||
func CreateViewFromObject(session Session, object DataObject) View {
|
// - session - the session to which the view will be attached (should not be nil);
|
||||||
tag := object.Tag()
|
// - object - datas describing View;
|
||||||
|
// - binding - object assigned to the Binding property (may be nil).
|
||||||
|
//
|
||||||
|
// If the function fails, it returns nil and an error message is written to the log.
|
||||||
|
func CreateViewFromObject(session Session, object DataObject, binding any) View {
|
||||||
|
if session == nil {
|
||||||
|
ErrorLog(`Session must not be nil`)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
tag := object.Tag()
|
||||||
|
creator, ok := viewCreators()[strings.ToLower(tag)]
|
||||||
|
if !ok {
|
||||||
|
ErrorLog(`Unknown view type "` + tag + `"`)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
if creator, ok := viewCreators[tag]; ok {
|
|
||||||
if !session.ignoreViewUpdates() {
|
if !session.ignoreViewUpdates() {
|
||||||
session.setIgnoreViewUpdates(true)
|
session.setIgnoreViewUpdates(true)
|
||||||
defer session.setIgnoreViewUpdates(false)
|
defer session.setIgnoreViewUpdates(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
view := creator(session)
|
view := creator(session)
|
||||||
view.init(session)
|
view.init(session)
|
||||||
if customView, ok := view.(CustomView); ok {
|
if customView, ok := view.(CustomView); ok {
|
||||||
|
@ -92,28 +94,46 @@ func CreateViewFromObject(session Session, object DataObject) View {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
parseProperties(view, object)
|
parseProperties(view, object)
|
||||||
return view
|
if binding != nil {
|
||||||
|
view.setRaw(Binding, binding)
|
||||||
}
|
}
|
||||||
|
return view
|
||||||
ErrorLog(`Unknown view type "` + object.Tag() + `"`)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateViewFromText create new View and initialize it by content of text
|
// CreateViewFromText create new View and initialize it by content of text. Parameters:
|
||||||
func CreateViewFromText(session Session, text string) View {
|
// - session - the session to which the view will be attached (should not be nil);
|
||||||
|
// - text - text describing View;
|
||||||
|
// - binding - object assigned to the Binding property (optional parameter).
|
||||||
|
//
|
||||||
|
// If the function fails, it returns nil and an error message is written to the log.
|
||||||
|
func CreateViewFromText(session Session, text string, binding ...any) View {
|
||||||
if data := ParseDataText(text); data != nil {
|
if data := ParseDataText(text); data != nil {
|
||||||
return CreateViewFromObject(session, data)
|
var b any = nil
|
||||||
|
if len(binding) > 0 {
|
||||||
|
b = binding[0]
|
||||||
|
}
|
||||||
|
return CreateViewFromObject(session, data, b)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateViewFromResources create new View and initialize it by the content of
|
// CreateViewFromResources create new View and initialize it by the content of
|
||||||
// the resource file from "views" directory
|
// the resource file from "views" directory. Parameters:
|
||||||
func CreateViewFromResources(session Session, name string) View {
|
// - session - the session to which the view will be attached (should not be nil);
|
||||||
|
// - name - file name in the views folder of the application resources (it is not necessary to specify the .rui extension, it is added automatically);
|
||||||
|
// - binding - object assigned to the Binding property (optional parameter).
|
||||||
|
//
|
||||||
|
// If the function fails, it returns nil and an error message is written to the log.
|
||||||
|
func CreateViewFromResources(session Session, name string, binding ...any) View {
|
||||||
if strings.ToLower(filepath.Ext(name)) != ".rui" {
|
if strings.ToLower(filepath.Ext(name)) != ".rui" {
|
||||||
name += ".rui"
|
name += ".rui"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var b any = nil
|
||||||
|
if len(binding) > 0 {
|
||||||
|
b = binding[0]
|
||||||
|
}
|
||||||
|
|
||||||
for _, fs := range resources.embedFS {
|
for _, fs := range resources.embedFS {
|
||||||
rootDirs := resources.embedRootDirs(fs)
|
rootDirs := resources.embedRootDirs(fs)
|
||||||
for _, dir := range rootDirs {
|
for _, dir := range rootDirs {
|
||||||
|
@ -124,14 +144,14 @@ func CreateViewFromResources(session Session, name string) View {
|
||||||
case viewDir:
|
case viewDir:
|
||||||
if data, err := fs.ReadFile(dir + "/" + name); err == nil {
|
if data, err := fs.ReadFile(dir + "/" + name); err == nil {
|
||||||
if data := ParseDataText(string(data)); data != nil {
|
if data := ParseDataText(string(data)); data != nil {
|
||||||
return CreateViewFromObject(session, data)
|
return CreateViewFromObject(session, data, b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if data, err := fs.ReadFile(dir + "/" + viewDir + "/" + name); err == nil {
|
if data, err := fs.ReadFile(dir + "/" + viewDir + "/" + name); err == nil {
|
||||||
if data := ParseDataText(string(data)); data != nil {
|
if data := ParseDataText(string(data)); data != nil {
|
||||||
return CreateViewFromObject(session, data)
|
return CreateViewFromObject(session, data, b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -141,7 +161,7 @@ func CreateViewFromResources(session Session, name string) View {
|
||||||
if resources.path != "" {
|
if resources.path != "" {
|
||||||
if data, err := os.ReadFile(resources.path + viewDir + "/" + name); err == nil {
|
if data, err := os.ReadFile(resources.path + viewDir + "/" + name); err == nil {
|
||||||
if data := ParseDataText(string(data)); data != nil {
|
if data := ParseDataText(string(data)); data != nil {
|
||||||
return CreateViewFromObject(session, data)
|
return CreateViewFromObject(session, data, b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -174,9 +174,8 @@ func (container *viewsContainerData) htmlSubviews(self View, buffer *strings.Bui
|
||||||
|
|
||||||
func viewFromTextValue(text string, session Session) View {
|
func viewFromTextValue(text string, session Session) View {
|
||||||
if strings.Contains(text, "{") && strings.Contains(text, "}") {
|
if strings.Contains(text, "{") && strings.Contains(text, "}") {
|
||||||
data := ParseDataText(text)
|
if data := ParseDataText(text); data != nil {
|
||||||
if data != nil {
|
if view := CreateViewFromObject(session, data, nil); view != nil {
|
||||||
if view := CreateViewFromObject(session, data); view != nil {
|
|
||||||
return view
|
return view
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -277,7 +276,7 @@ func (container *viewsContainerData) setContent(value any) bool {
|
||||||
container.views = views
|
container.views = views
|
||||||
|
|
||||||
case DataObject:
|
case DataObject:
|
||||||
if view := CreateViewFromObject(session, value); view != nil {
|
if view := CreateViewFromObject(session, value, nil); view != nil {
|
||||||
container.views = []View{view}
|
container.views = []View{view}
|
||||||
} else {
|
} else {
|
||||||
return false
|
return false
|
||||||
|
@ -287,7 +286,7 @@ func (container *viewsContainerData) setContent(value any) bool {
|
||||||
views := []View{}
|
views := []View{}
|
||||||
for _, data := range value {
|
for _, data := range value {
|
||||||
if data.IsObject() {
|
if data.IsObject() {
|
||||||
if view := CreateViewFromObject(session, data.Object()); view != nil {
|
if view := CreateViewFromObject(session, data.Object(), nil); view != nil {
|
||||||
views = append(views, view)
|
views = append(views, view)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue