-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBBjRouter.bbj
297 lines (293 loc) · 9.19 KB
/
BBjRouter.bbj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use java.util.HashMap
use com.google.gson.Gson
use com.google.gson.JsonParser
use com.google.gson.JsonObject
rem /**
rem * BBjRouterEvent is the matched event payload for matched routes.
rem * It contains all the necessary data about the current matched route
rem *
rem * @author Hyyan Abo Fakher
rem */
class public BBjRouterEvent
rem /**
rem * The route data
rem */
field public HashMap Data! = new HashMap()
rem /**
rem * The route query params
rem */
field public HashMap Params! = new HashMap()
rem /**
rem * The route query as string
rem */
field public BBjString QueryString! = ""
rem /**
rem * The route path
rem */
field public BBjString Path! = ""
field protected BBjRouter Router!
rem /**
rem * Construct a new BBjRouterEvent
rem *
rem * @param router! The instance of the BBjRouter object
rem */
method public BBjRouterEvent(BBjRouter router!)
#Router! = router!
methodend
rem /**
rem *
rem /**
rem * Get the BBjRouter instance
rem *
rem * @return BBjRouter
rem */
method public BBjRouter getWidget()
methodret #Router!
methodend
rem /**
rem * Alias for `BBjRouter.getWidget()`
rem *
rem * @return BBjRouter
rem */
method public BBjRouter getControl()
methodret #getWidget()
methodend
classend
rem /**
rem * BBjRouter is a minimalistic client side router.
rem *
rem * At the application level there should be one and only one router .
rem * Having multi router instances is not supported and it does not have
rem * a valid use case.
rem *
rem * @author Hyyan Abo Fakher
rem */
class public BBjRouter
rem /**
rem * A constant to use for the not found event
rem */
field public static BBjNumber ON_NOT_FOUND = 2
field protected BBjChildWindow Canvas!
field protected BBjHtmlView HTMLView!
rem /**
rem * Construct new BBjRouter for the given BBjWindow instance
rem *
rem * @param wnd! The BBjWindow instance
rem * @param hash! When true , then hash based routing will be used
rem * @param id! The id of the created child window
rem */
method public BBjRouter(BBjWindow wnd!, Boolean hash!, BBjNumber id!)
#Canvas! = wnd!.addChildWindow(id!, 0, 0, 0, 0, "", $00008010$, BBjAPI().getSysGui().getAvailableContext())
rem setup a hidden HTMLView to handle events
#HTMLView! = #Canvas!.addHtmlView(101, 0, 0, 0, 0, "", $0000$)
#HTMLView!.setOpaque(0)
#HTMLView!.setNoEdge(1)
#HTMLView!.setTabTraversable(0)
#HTMLView!.setFocusable(0)
#HTMLView!.setAttribute("data-bbj-router", "")
#HTMLView!.setCallback(BBjAPI.ON_NATIVE_JAVASCRIPT, #this!, "handleJavascriptEvents")
#HTMLView!.injectScript(#readFile("BBjRouter/dist/bbj-router.min.js"), 1)
base! = "/"
if(hash! = 0)
base! = "/webapp/" + BBjAPI().getBuiManager().getApplicationName() + "/"
fi
#HTMLView!.executeAsyncScript("BBjRouter.init(" + str(hash!)+ ", '" + str(base!) + "')")
methodend
rem /**
rem * Construct new BBjRouter for the given BBjWindow instance
rem *
rem * @param wnd! The BBjWindow instance
rem * @param id! The id of the created child window
rem */
method public BBjRouter(BBjWindow wnd!, BBjNumber id!)
#this!(wnd!, BBjAPI.FALSE, id!)
methodend
rem /**
rem * Construct new BBjRouter for the given BBjWindow instance
rem *
rem * @param wnd! The BBjWindow instance
rem * @param hash! When true , then hash based routing will be used
rem */
method public BBjRouter(BBjWindow wnd!, Boolean hash!)
#this!(wnd!, hash!, wnd!.getAvailableControlID())
methodend
rem /**
rem * Construct new BBjRouter for the given BBjWindow instance
rem *
rem * @param wnd! The BBjWindow instance
rem */
method public BBjRouter(BBjWindow wnd!)
#this!(wnd!, wnd!.getAvailableControlID())
methodend
rem /**
rem * Get the unique auto generated id of the widget.
rem *
rem * @return BBjString
rem */
method public BBjString getUUID()
methodret #UUID!
methodend
rem /**
rem * Get the canvas's id
rem *
rem * @return BBjNumber
rem */
method public BBjNumber getID()
methodret #getCanvas().getID()
methodend
rem /**
rem * Navigates to a route but it doesn't change the browser URL.
rem * You should fire this at least once in the beginning.
rem *
rem * @return The router instance.
rem */
method public BBjRouter resolve()
#getHTMLView().executeAsyncScript("BBjRouter.resolve()")
methodret #this!
methodend
rem /**
rem * Navigate to the given route.
rem *
rem * @param path! The path to navigate to.
rem * @param silent! When true , then the router will not fire events
rem * for this change
rem *
rem * @return The router instance.
rem */
method public BBjRouter navigate(BBjString path!, BBjNumber silent!)
#getHTMLView().executeAsyncScript("BBjRouter.navigate('" + path! + "'," + str(silent!)+ ")")
methodret #this!
methodend
rem /**
rem * Navigate to the given route and fire change events
rem *
rem * @param path! The path to navigate to.
rem *
rem * @return The router instance.
rem */
method public BBjRouter navigate(BBjString path!)
#navigate(path!, 0)
methodret #this!
methodend
rem /**
rem * Register a new route
rem *
rem * @param path! The route's path.
rem * @param callback! The route's callback's name to execute when
rem * the route is matched
rem *
rem * @return The router instance.
rem */
method public BBjRouter register(BBjString path!, BBjString callback!)
#getHTMLView().executeAsyncScript("BBjRouter.on('" + path! + "')")
BBjAPI().setCustomEventCallback(str(#this!) + str(path!), callback!)
methodret #this!
methodend
rem /**
rem * Register a new route
rem *
rem * @param path! The route's path.
rem * @param obj! A class instance
rem * @param method! A method in the given class instance to execute when
rem * the route is matched
rem *
rem * @return The router instance.
rem */
method public BBjRouter register(BBjString path!, CustomObject obj!, BBjString method!)
#getHTMLView().executeAsyncScript("BBjRouter.on('" + path! + "')")
BBjAPI().setCustomEventCallback(str(#this!) + str(path!), obj!, method!)
methodret #this!
methodend
rem /**
rem * Unregister an already registered route
rem *
rem * @param path! The route's path.
rem *
rem * @return The router instance.
rem */
method public BBjRouter unregister(BBjString path!)
#getHTMLView().executeAsyncScript("BBjRouter.off('" + path! + "')")
methodret #this!
methodend
rem /**
rem * Set a router event listener
rem *
rem * @see BBjMediaQuery.ON_NOT_FOUND
rem *
rem * @param event! The modal event
rem * @param callback! The event callback
rem */
method public void setCallback(int event!, String callback!)
BBjAPI().setCustomEventCallback(str(#this!) + str(event!), callback!)
methodend
rem /**
rem * Set a router event listener
rem *
rem * @see BBjMediaQuery.ON_NOT_FOUND
rem *
rem * @param event! The modal event
rem * @param callback! A class instance
rem * @param method! The method name in the passed class
rem */
method public void setCallback(int event!, CustomObject instance!, String method!)
BBjAPI().setCustomEventCallback(str(#this!) + str(event!), instance!, method!)
methodend
rem /**
rem * Clear an event callback
rem *
rem * @param event! The widget event
rem */
method public void clearCallback(BBjNumber event!)
BBjAPI().clearCustomEventCallback(str(#this!) + str(event!))
methodend
rem /**
rem * Destroy the widget
rem */
method public void destroy()
#getHTMLView().executeAsyncScript("BBjRouter.destroy()")
#getCanvas().destroy()
methodend
rem /**
rem * Handle the router client events.
rem *
rem * @param ev! The Javascript event
rem * @ignore
rem */
method public void handleJavascriptEvents(BBjNativeJavaScriptEvent ev!)
map! = ev!.getEventMap()
type$ = str(map!.get("type"))
detail$ = str(map!.get("detail"))
event! = new BBjRouterEvent(#this!)
parser! = new JsonParser()
gson! = new Gson()
detailsAsJson! = parser!.parse(detail$).getAsJsonObject()
path! = detailsAsJson!.get("path", err=*next).getAsString()
event!.setData(gson!.fromJson(detailsAsJson!.get("data").toString(), HashMap.class), err=*next)
event!.setParams(gson!.fromJson(detailsAsJson!.get("params").toString(), HashMap.class), err=*next)
event!.setQueryString(detailsAsJson!.get("queryString").getAsString(), err=*next)
event!.setPath(path!)
switch type$
case "bbj-router-matched"
BBjAPI().postPriorityCustomEvent(str(#this!) + str(path!), event!)
break
case "bbj-router-notfound"
BBjAPI().postPriorityCustomEvent(str(#this!) + str(BBjRouter.ON_NOT_FOUND), event!)
break
swend
methodend
rem /**
rem * Read the content of the given file as a String.
rem *
rem * @param file! The file's path
rem *
rem * @return The file's content
rem */
method protected BBjString readFile(BBjString file$)
content$ = ""
ch=unt
open (ch)file$
read record (ch,siz=5512000)content$
close (ch)
methodret content$
methodend
classend