Skip to content

Commit

Permalink
feat: Web Storage API (closes #30)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkruse committed Jun 2, 2024
1 parent d66448e commit 33f674e
Show file tree
Hide file tree
Showing 7 changed files with 462 additions and 1 deletion.
13 changes: 13 additions & 0 deletions AppSrc/Dashboard.wo
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,19 @@ Object oDashboard is a cWebView
End_Procedure
End_Object

Object oTileWebStorage is a cWebHtmlBox
Set pbServerOnClick to True
Set psCSSClass to "Tile"
Set psHtml to '<div class="WebCon_Sizer" data-ServerOnClick="openview"><div class="Tile_Title">Web Storage API</div><div Class="Tile_Subtitle">store data in the browser</div></div>'
Set piColumnIndex to 2

WebRegisterPath ntNavigateForward oWebStorageDemo

Procedure OnClick
Send NavigatePath
End_Procedure
End_Object

Object oWebSpacer is a cWebSpacer
Set piColumnSpan to 3
Set piHeight to 20
Expand Down
10 changes: 10 additions & 0 deletions AppSrc/WebApp.src
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ Object oWebApp is a cWebApp
Send NavigatePath
End_Procedure
End_Object

Object oWebMenuItemWebStorage is a cWebMenuItem
Set psCaption to "Web Storage Demo"
WebRegisterPath ntNavigateBegin oWebStorageDemo

Procedure OnClick
Send NavigatePath
End_Procedure
End_Object
End_Object

Object oBackButton is a cWebButton
Expand Down Expand Up @@ -265,6 +274,7 @@ Object oWebApp is a cWebApp
Use CredentialManagementDemo.wo
Use SensorDemo.wo
Use ScreenOrientationDemo.wo
Use WebStorageDemo.wo
Set phoDefaultView to oDashboard

End_Object
Expand Down
231 changes: 231 additions & 0 deletions AppSrc/WebStorageDemo.wo
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
Use cWebView.pkg
Use cWebPanel.pkg
Use cWebLabel.pkg
Use cWebHtmlBox.pkg
Use cWebEdit.pkg
Use cWebButton.pkg

Use cWebStorageAPI.pkg

Object oWebStorageDemo is a cWebView
Set peWebViewStyle to wvsDrillDown
Set peViewType to vtUndefined
Set pbShowCaption to False
Set Verify_Save_msg to 0 // don't confirm saves

Set psCaption to "Web Storage Demo"

Set piMaxWidth to 1024
Set piColumnCount to 12
Set pbServerOnShow to True

Object oLocalStorage is a cLocalStorageAPI
Set pbServerOnStorage to True

Procedure OnStorage String sKeyName String sNewValue String sOldValue String sURL
If (sKeyName = "") Begin
Send Log of oWebEdit (SFormat("LocalStorage was cleared by page at %1", sURL))
End
Else Begin
Send Log of oWebEdit (SFormat("Key '%1' was changed from '%2' to '%3' by page at %4", sKeyName, sOldValue, sNewValue, sURL))
End
End_Procedure
End_Object

Object oSessionStorage is a cSessionStorageAPI
End_Object

Object oWebMainPanel is a cWebPanel
Set piColumnCount to 12
WebSetResponsive piColumnCount rmMobile to 6

Object oWebLabel is a cWebLabel
Set psCaption to "Supported?"
Set piColumnSpan to 0
End_Object

Object oWebHtmlBox is a cWebHtmlBox
Set piColumnSpan to 0
Set psHtml to '<div class="as-label"><a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API#browser_compatibility" target="_blank">See browser compatibility</a></div>'
End_Object

Object oWebLabel2 is a cWebLabel
Set psCaption to "Web Storage lets you store and retrieve string values in the browser, local to the current site, either permanently (localStorage) or for the current session (sessionStorage). To test the 'storage' event, try opening this demo in a new tab, and manipulating localStorage from there."
Set piColumnSpan to 12
End_Object

Object oWebButton1 is a cWebButton
Set piColumnSpan to 6
Set psCaption to "Enumerate localStorage"

Procedure OnKey String sName
Send Log of oWebEdit (SFormat("Found key '%1' in localStorage", sName))
End_Procedure

WebPublishProcedure OnKey

Procedure OnClick
Integer iCount i
WebGet piLength of oLocalStorage to iCount
If (iCount = 0) Begin
Send Log of oWebEdit "No keys in localStorage"
End
Else Begin
For i from 0 to (iCount - 1)
Send GetKey of oLocalStorage Self (RefProc(OnKey)) i
Loop
End
End_Procedure
End_Object

Object oWebButton2 is a cWebButton
Set piColumnSpan to 6
Set psCaption to "Enumerate sessionStorage"
Set piColumnIndex to 6

Procedure OnKey String sName
Send Log of oWebEdit (SFormat("Found key '%1' in sessionStorage", sName))
End_Procedure

WebPublishProcedure OnKey

Procedure OnClick
Integer iCount i
WebGet piLength of oSessionStorage to iCount
If (iCount = 0) Begin
Send Log of oWebEdit "No keys in sessionStorage"
End
Else Begin
For i from 0 to (iCount - 1)
Send GetKey of oSessionStorage Self (RefProc(OnKey)) i
Loop
End
End_Procedure
End_Object

Object oWebButton3 is a cWebButton
Set piColumnSpan to 6
Set psCaption to "Save value in localStorage"

Procedure OnClick
Send SetItem of oLocalStorage "DemoKey" "Example local value set by demo"
Send Log of oWebEdit "Value saved in localStorage key 'DemoKey'"
End_Procedure
End_Object

Object oWebButton4 is a cWebButton
Set piColumnSpan to 6
Set psCaption to "Save value in sessionStorage"
Set piColumnIndex to 6

Procedure OnClick
Send SetItem of oSessionStorage "DemoKey" "Example session value set by demo"
Send Log of oWebEdit "Value saved in sessionStorage key 'DemoKey'"
End_Procedure
End_Object

Object oWebButton5 is a cWebButton
Set piColumnSpan to 6
Set psCaption to "Retrieve value from localStorage"
Set piColumnIndex to 0

Procedure OnValue String sValue
Send Log of oWebEdit (SFormat("Got value '%1' from localStorage", sValue))
End_Procedure

WebPublishProcedure OnValue

Procedure OnClick
Send GetItem of oLocalStorage Self (RefProc(OnValue)) "DemoKey"
End_Procedure
End_Object

Object oWebButton6 is a cWebButton
Set piColumnSpan to 6
Set psCaption to "Retrieve value from sessionStorage"
Set piColumnIndex to 6

Procedure OnValue String sValue
Send Log of oWebEdit (SFormat("Got value '%1' from sessionStorage", sValue))
End_Procedure

WebPublishProcedure OnValue

Procedure OnClick
Send GetItem of oSessionStorage Self (RefProc(OnValue)) "DemoKey"
End_Procedure
End_Object

Object oWebButton7 is a cWebButton
Set piColumnSpan to 6
Set psCaption to "Remove value from localStorage"

Procedure OnClick
Send RemoveItem of oLocalStorage "DemoKey"
End_Procedure
End_Object

Object oWebButton8 is a cWebButton
Set piColumnSpan to 6
Set psCaption to "Remove value from sessionStorage"
Set piColumnIndex to 6

Procedure OnClick
Send RemoveItem of oSessionStorage "DemoKey"
End_Procedure
End_Object

Object oWebButton9 is a cWebButton
Set piColumnSpan to 6
Set psCaption to "Clear localStorage"

Procedure OnClick
Send Clear of oLocalStorage
End_Procedure
End_Object

Object oWebButton10 is a cWebButton
Set piColumnSpan to 6
Set psCaption to "Clear sessionStorage"
Set piColumnIndex to 6

Procedure OnClick
Send Clear of oSessionStorage
End_Procedure
End_Object

Object oWebEdit is a cWebEdit
Set piColumnSpan to 0
Set pbShowLabel to False
Set pbReadOnly to True
Set pbFillHeight to True

Procedure Log String sText
String sValue
WebGet psValue to sValue
If (sValue <> "") Begin
Move (sValue + Character(13) + Character(10)) to sValue
End
Move (SFormat("%1%2: %3", sValue, CurrentDateTime(), sText)) to sValue
WebSet psValue to sValue
End_Procedure
End_Object
End_Object

Procedure OnShow
Boolean bLocalIsSupported bSessionIsSupported

Forward Send OnShow

WebGet pbIsSupported of oLocalStorage to bLocalIsSupported
WebGet pbIsSupported of oSessionStorage to bSessionIsSupported
If (bLocalIsSupported and bSessionIsSupported) Begin
WebSet psCaption of oWebLabel to "Web Storage API is supported"
WebSet psTextColor of oWebLabel to "green"
End
Else Begin
WebSet psCaption of oWebLabel to "Web Storage API is NOT supported (try any real browser)"
WebSet psTextColor of oWebLabel to "red"
End
End_Procedure
End_Object
91 changes: 91 additions & 0 deletions AppSrc/cWebStorageAPI.pkg
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
Use cWebObject.pkg

Class cWebStorage_Mixin is a Mixin
Procedure Define_cWebStorage_Mixin
{ WebProperty=Client DesignTime=False }
Property Boolean pbIsSupported False
{ WebProperty=Client DesignTime=False }
Property Integer piLength 0
End_Procedure

Procedure GetKey Handle hoReturnObj Handle hoReturnMsg Integer iIndex
String sReturnMessage
String[] aParams

Get WebMethodName of hoReturnObj hoReturnMsg to sReturnMessage
If (sReturnMessage <> "") Begin
Get WebObjectName of hoReturnObj to aParams[0]
Move sReturnMessage to aParams[1]
Move iIndex to aParams[2]
Send ClientAction "key" aParams
End
End_Procedure

Procedure GetItem Handle hoReturnObj Handle hoReturnMsg String sKeyName
String sReturnMessage
String[] aParams

Get WebMethodName of hoReturnObj hoReturnMsg to sReturnMessage
If (sReturnMessage <> "") Begin
Get WebObjectName of hoReturnObj to aParams[0]
Move sReturnMessage to aParams[1]
Move sKeyName to aParams[2]
Send ClientAction "getItem" aParams
End
End_Procedure

Procedure SetItem String sKeyName String sKeyValue
String[] aParams
Move sKeyName to aParams[0]
Move sKeyValue to aParams[1]
Send ClientAction "setItem" aParams
End_Procedure

Procedure RemoveItem String sKeyName
String[] aParams
Move sKeyName to aParams[0]
Send ClientAction "removeItem" aParams
End_Procedure

Procedure Clear
Send ClientAction "clear"
End_Procedure
End_Class

Class cLocalStorageAPI is a cWebObject
Import_Class_Protocol cWebStorage_Mixin

Procedure Construct_Object
Forward Send Construct_Object
Send Define_cWebStorage_Mixin

{ WebProperty=Client }
Property Boolean pbServerOnStorage False
{ WebProperty=Client }
Property String psClientOnStorage ""

Set psJSClass to "WebAPIs.LocalStorage"
End_Procedure

Procedure End_Construct_Object
Forward Send End_Construct_Object

WebPublishProcedure OnStorage
End_Procedure

// Triggers when *another* window changes localStorage only
{ MethodType=Event }
Procedure OnStorage String sKeyName String sNewValue String sOldValue String sURL
End_Procedure
End_Class

Class cSessionStorageAPI is a cWebObject
Import_Class_Protocol cWebStorage_Mixin

Procedure Construct_Object
Forward Send Construct_Object
Send Define_cWebStorage_Mixin

Set psJSClass to "WebAPIs.SessionStorage"
End_Procedure
End_Class
2 changes: 1 addition & 1 deletion AppSrc/config/classlist.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<classList xmlns="http://www.dataaccess.com/VisualDataFlex/2006/classlist/">
<VDFLibraryTimeStamp>2023-07-24T17:47:23</VDFLibraryTimeStamp><libraries></libraries>
<VDFLibraryTimeStamp>2024-03-11T18:26:21</VDFLibraryTimeStamp><libraries></libraries>
<classes>
<class>
<className>cWebButton</className>
Expand Down
Loading

0 comments on commit 33f674e

Please sign in to comment.