PropertyWithTag method of DataObject renamed to PropertyByTag

This commit is contained in:
anoshenko 2022-12-18 18:22:58 +03:00
parent d3002ced0e
commit c31b2f9d8c
6 changed files with 82 additions and 12 deletions

View File

@ -52,6 +52,24 @@ function sessionInfo() {
message += ",pixel-ratio=" + pixelRatio;
}
if (localStorage.length > 0) {
message += ",storage="
lead = "_{"
for (var i = 0; i < localStorage.length; i++) {
var key = localStorage.key(i)
var value = localStorage.getItem(key)
key = key.replaceAll(/\\/g, "\\\\")
key = key.replaceAll(/\"/g, "\\\"")
key = key.replaceAll(/\'/g, "\\\'")
value = value.replaceAll(/\\/g, "\\\\")
value = value.replaceAll(/\"/g, "\\\"")
value = value.replaceAll(/\'/g, "\\\'")
message += lead + "\"" + key + "\"=\"" + value + "\""
lead = ","
}
message += "}"
}
return message + "}";
}
@ -940,8 +958,8 @@ function radioButtonKeyClickEvent(element, event) {
function editViewInputEvent(element) {
var text = element.value
text = text.replace(/\\/g, "\\\\")
text = text.replace(/\"/g, "\\\"")
text = text.replaceAll(/\\/g, "\\\\")
text = text.replaceAll(/\"/g, "\\\"")
var message = "textChanged{session=" + sessionID + ",id=" + element.id + ",text=\"" + text + "\"}"
sendMessage(message);
}
@ -1147,7 +1165,7 @@ function loadImage(url) {
img.addEventListener("error", function(event) {
var message = "imageError{session=" + sessionID + ",url=\"" + url + "\"";
if (event && event.message) {
var text = event.message.replace(new RegExp("\"", 'g'), "\\\"")
var text = event.message.replaceAll(new RegExp("\"", 'g'), "\\\"")
message += ",message=\"" + text + "\"";
}
sendMessage(message + "}")
@ -1178,7 +1196,7 @@ function loadInlineImage(url, content) {
img.addEventListener("error", function(event) {
var message = "imageError{session=" + sessionID + ",url=\"" + url + "\"";
if (event && event.message) {
var text = event.message.replace(new RegExp("\"", 'g'), "\\\"")
var text = event.message.replaceAll(new RegExp("\"", 'g'), "\\\"")
message += ",message=\"" + text + "\"";
}
sendMessage(message + "}")
@ -1842,3 +1860,19 @@ function getCanvasContext(elementId) {
}
return null;
}
function localStorageSet(key, value) {
try {
localStorage.setItem(key, value)
} catch (err) {
sendMessage("storageError{session=" + sessionID + ", error=`" + err + "`}")
}
}
function localStorageClear() {
try {
localStorage.setItem(key, value)
} catch (err) {
sendMessage("storageError{session=" + sessionID + ", error=`" + err + "`}")
}
}

View File

@ -286,7 +286,7 @@ func (border *borderProperty) setBorderObject(obj DataObject) bool {
result := true
for _, side := range []string{Top, Right, Bottom, Left} {
if node := obj.PropertyWithTag(side); node != nil {
if node := obj.PropertyByTag(side); node != nil {
if node.Type() == ObjectNode {
if !border.setSingleBorderObject(side, node.Object()) {
result = false

View File

@ -18,7 +18,7 @@ type DataObject interface {
Tag() string
PropertyCount() int
Property(index int) DataNode
PropertyWithTag(tag string) DataNode
PropertyByTag(tag string) DataNode
PropertyValue(tag string) (string, bool)
PropertyObject(tag string) DataObject
SetPropertyValue(tag, value string)
@ -106,7 +106,7 @@ func (object *dataObject) Property(index int) DataNode {
return object.property[index]
}
func (object *dataObject) PropertyWithTag(tag string) DataNode {
func (object *dataObject) PropertyByTag(tag string) DataNode {
if object.property != nil {
for _, node := range object.property {
if node.Tag() == tag {
@ -118,14 +118,14 @@ func (object *dataObject) PropertyWithTag(tag string) DataNode {
}
func (object *dataObject) PropertyValue(tag string) (string, bool) {
if node := object.PropertyWithTag(tag); node != nil && node.Type() == TextNode {
if node := object.PropertyByTag(tag); node != nil && node.Type() == TextNode {
return node.Text(), true
}
return "", false
}
func (object *dataObject) PropertyObject(tag string) DataObject {
if node := object.PropertyWithTag(tag); node != nil && node.Type() == ObjectNode {
if node := object.PropertyByTag(tag); node != nil && node.Type() == ObjectNode {
return node.Object()
}
return nil

View File

@ -270,7 +270,7 @@ func (picker *filePickerData) htmlDisabledProperties(self View, buffer *strings.
func (picker *filePickerData) handleCommand(self View, command string, data DataObject) bool {
switch command {
case "fileSelected":
if node := data.PropertyWithTag("files"); node != nil && node.Type() == ArrayNode {
if node := data.PropertyByTag("files"); node != nil && node.Type() == ArrayNode {
count := node.ArraySize()
files := make([]FileInfo, count)
for i := 0; i < count; i++ {

View File

@ -100,6 +100,10 @@ type Session interface {
// OpenURL opens the url in the new browser tab
OpenURL(url string)
ClientItem(key string) (string, bool)
SetClientItem(key, value string)
RemoveAllClientItems()
getCurrentTheme() Theme
registerAnimation(props []AnimatedProperty) string
@ -183,6 +187,7 @@ type sessionData struct {
animationCounter int
animationCSS string
updateScripts map[string]*strings.Builder
clientStorage map[string]string
}
func newSession(app Application, id int, customTheme string, params DataObject) Session {
@ -199,6 +204,7 @@ func newSession(app Application, id int, customTheme string, params DataObject)
session.animationCounter = 0
session.animationCSS = ""
session.updateScripts = map[string]*strings.Builder{}
session.clientStorage = map[string]string{}
if customTheme != "" {
if theme, ok := CreateThemeFromText(customTheme); ok {
@ -516,7 +522,7 @@ func (session *sessionData) handleRootSize(data DataObject) {
}
func (session *sessionData) handleResize(data DataObject) {
if node := data.PropertyWithTag("views"); node != nil && node.Type() == ArrayNode {
if node := data.PropertyByTag("views"); node != nil && node.Type() == ArrayNode {
for _, el := range node.ArrayElements() {
if el.IsObject() {
obj := el.Object()
@ -591,6 +597,16 @@ func (session *sessionData) handleSessionInfo(params DataObject) {
session.pixelRatio = f
}
}
if node := params.PropertyByTag("storage"); node != nil && node.Type() == ObjectNode {
if obj := node.Object(); obj != nil {
for i := 0; i < obj.PropertyCount(); i++ {
if element := obj.Property(i); element.Type() == TextNode {
session.clientStorage[element.Tag()] = element.Text()
}
}
}
}
}
func (session *sessionData) handleEvent(command string, data DataObject) {
@ -610,6 +626,11 @@ func (session *sessionData) handleEvent(command string, data DataObject) {
case "sessionInfo":
session.handleSessionInfo(data)
case "storageError":
if text, ok := data.PropertyValue("error"); ok {
ErrorLog(text)
}
default:
if viewID, ok := data.PropertyValue("id"); ok {
if view := session.viewByHTMLID(viewID); view != nil {
@ -641,3 +662,18 @@ func (session *sessionData) OpenURL(urlStr string) {
}
session.callFunc("openURL", urlStr)
}
func (session *sessionData) ClientItem(key string) (string, bool) {
value, ok := session.clientStorage[key]
return value, ok
}
func (session *sessionData) SetClientItem(key, value string) {
session.clientStorage[key] = value
session.bridge.callFunc("localStorageSet", key, value)
}
func (session *sessionData) RemoveAllClientItems() {
session.clientStorage = map[string]string{}
session.bridge.callFunc("localStorageClear")
}

View File

@ -140,7 +140,7 @@ func (event *TouchEvent) init(data DataObject) {
event.Touches = []Touch{}
event.TimeStamp = getTimeStamp(data)
if node := data.PropertyWithTag("touches"); node != nil && node.Type() == ArrayNode {
if node := data.PropertyByTag("touches"); node != nil && node.Type() == ArrayNode {
for i := 0; i < node.ArraySize(); i++ {
if element := node.ArrayElement(i); element != nil && element.IsObject() {
if obj := element.Object(); obj != nil {