-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataview_paginated_scroll.livecodescript
289 lines (214 loc) · 6.51 KB
/
dataview_paginated_scroll.livecodescript
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
script "DataView Paginated Scroll Behavior" with behavior "DataView Array Controller Behavior"
local sLocalsA
local sViewsRequiresRendering
/**
Summary: Stores certain properties required for this behavior.
Parameters:
pProp: The property to set.
pValue: The value to assign to the property.
Description:
Pagination requires knowledge of how many pages there are total as well
as how many results are on each page. Set the `number of rows` property
as well as the `rows per page` property to store this information.
Returns: nothing
*/
setProp viewProp[pProp] pValue
switch pProp
case "number of rows"
put pValue into sLocalsA[pProp]
# View needs to be fully rendered after setting `number of rows`
# as the content height will need updating.
put true into sViewsRequiresRendering
break
case "rows per page"
put pValue into sLocalsA[pProp]
break
default
pass viewProp
break
end switch
end viewProp
/**
Summary: Returns properties stored or synthesized by this behavior.
Returns: nothing
*/
getProp viewProp[pProp]
switch pProp
case "rows per page"
case "number of rows"
return max(0, sLocalsA[pProp])
default
pass viewProp
break
end switch
end viewProp
/**
Summary: Turn off `sViewsRequiresRendering` flag when called.
Returns: nothing
*/
after RenderView
put false into sViewsRequiresRendering
end RenderView
/**
Summary: Turn off `sViewsRequiresRendering` flag when called.
Returns: nothing
*/
after RefreshViewRows
put false into sViewsRequiresRendering
end RefreshViewRows
/**
Summary: When resetting the view clear out `rows per page` and `number of rows`.
Returns: nothing
*/
after ResetView
put empty into sLocalsA
end ResetView
/**
Summary: Looks for pages that need to be loaded.
Parameters:
pRow: See DataView docs.
rDataA: See DataView docs.
rTemplateStyle: See DataView docs.
Description:
This handler inspects `rData` to see if the magic key property is empty. That magic
key property is the property assigned to the `viewProp["magic key"]` custom property
of the DataView. If it is empty then the page of results that `pRow` is associated
with is requested by sending the `LoadDataForPage` message.
Returns: nothing
*/
after DataForRow pRow, @rDataA, @rTemplateStyle
if rDataA[the viewProp["magic key"] of me] is empty then
_AddCurrentPageOfDataToQueue pRow
_StartRequestingPages
end if
end DataForRow
/**
Summary: Returns the stored number of rows.
Description:
While this function is not used explicitly, it is provided in cases the
developer tries to call this function in their own code.
Returns: Integer
*/
function NumberOfRows
return max(0, sLocalsA["number of rows"])
end NumberOfRows
/**
Summary: Returns the row that a page starts on.
Parameters:
pPage: The page number.
Returns: Integer
*/
getProp dvFirstRowOfPage[pPage]
return ((pPage-1) * sLocalsA["rows per page"]) + 1
end dvFirstRowOfPage
/**
Summary: Returns the row that a page ends on.
Parameters:
pPage: The page number.
Returns: Integer
*/
getProp dvLastRowOfPage[pPage]
return min(pPage * sLocalsA["rows per page"], sLocalsA["number of rows"])
end dvLastRowOfPage
/**
Summary: Update the "rows per page" and then pass to parent behavior.
Returns: nothing
*/
setProp dvData pDataA
local tRowCount
put the number of elements of pDataA into tRowCount
put tRowCount into sLocalsA["number of rows"]
pass dvData
end dvData
/**
Summary: Adds data to a page in the DataView's data source.
Description:
This will be called after `LoadDataForPage` is processed by the data provider
and there are records to insert into the page.
If `pPage` is the page that triggered `LoadDataForPage` then the next
page in the queue will be loaded.
Returns: nothing
*/
setProp dvDataForPage[pPage] pDataA
local tRow, i
_ValidateMagicKey
put ((pPage-1) * sLocalsA["rows per page"]) into tRow
repeat with i = 1 to the number of elements of pDataA
add 1 to tRow
set the dvRowData[tRow] of me to pDataA[i]
set the dvRowIsDirty[tRow] of me to true
end repeat
if sViewsRequiresRendering then
RenderView
else
RenderVisibleRows
end if
if pPage is sLocalsA["waiting on page"] then
put empty into sLocalsA["waiting on page"]
_RequestNextPageInQueue
end if
end dvDataForPage
/**
Summary: Starts downloading pages in the queue if a request is not already running.
Returns: nothing
*/
private command _StartRequestingPages
if sLocalsA["waiting on page"] is empty then
_RequestNextPageInQueue
end if
return empty
end _StartRequestingPages
/**
Summary: Adds the current page that is visible in the DataView to the queue of pages to load.
Description:
When the DataView scrolls to a record that has no `id` the page that the
row is on is added to a queue of pages whose data needs to be requested. The page
passed in will be placed at the front of the queue as it is currently
visible to the user.
Returns: nothing
*/
private command _AddCurrentPageOfDataToQueue pRowWithMissingId
local tPageToLoad, tItemNo
if sLocalsA["rows per page"] < 1 then
return empty
end if
set the wholematches to true
put (pRowWithMissingId + (sLocalsA["rows per page"]-1)) \
div sLocalsA["rows per page"] into tPageToLoad
# Ignore the page if it is currently being waited on
if tPageToLoad is not sLocalsA["waiting on page"] then
# Add tPageToLoad to front of pages to load.
put itemoffset(tPageToLoad, sLocalsA["page queue"]) into tItemNo
if tItemNo > 0 then
delete item tItemNo of sLocalsA["page queue"]
end if
if sLocalsA["page queue"] is not empty then
put tPageToLoad & "," before sLocalsA["page queue"]
else
put tPageToLoad into sLocalsA["page queue"]
end if
end if
return empty
end _AddCurrentPageOfDataToQueue
/**
Summary: Dispatches the `LoadDataForPage` message for the next page in the queue.
Returns: nothing
*/
private command _RequestNextPageInQueue
local tLoadPage
if sLocalsA["waiting on page"] is empty and sLocalsA["page queue"] is not empty then
put item 1 of sLocalsA["page queue"] into tLoadPage
delete item 1 of sLocalsA["page queue"]
put tLoadPage into sLocalsA["waiting on page"]
dispatch "LoadDataForPage" with tLoadPage
end if
return empty
end _RequestNextPageInQueue
/**
Summary: Throws an error if the "magic key" prop isn't set.
*/
private command _ValidateMagicKey
if the viewProp["magic key"] of me is empty then
throw "DataView Paginated Error: viewProp[" & quote & "magic key" & quote & "] is not defined."
end if
end _ValidateMagicKey