-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGxExpressions.bbj
515 lines (495 loc) · 18.7 KB
/
GxExpressions.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
rem /**
rem * The package exports a collection of predefined expression which can be used directly in columns.
rem */
rem package GxExpressions
rem /**
rem * This file is part of the BBjGridExWidget plugin.
rem * (c) Basis Europe <eu@basis.cloud>
rem *
rem * For the full copyright and license information, please view the LICENSE
rem * file that was distributed with this source code.
rem */
use java.lang.StringBuilder
use ::BBjGridExWidget/GxLogger.bbj::GxLogger
REM /**
REM * GxExpressionInterface
REM *
REM * @author Hyyan Abo Fakher
REM */
interface public GxExpressionInterface
rem /**
rem * Get the expression as string
rem *
rem * @return BBjString The Javascript expression
rem */
method public String toString()
rem /**
rem * Compare two expressions
rem *
rem * @param expression! - Another expression instance to compare with
rem *
rem * @return bool - true when they are equal , false otherwise
rem */
method public boolean equals(GxExpressionInterface expression!)
interfaceend
rem /**
rem * Abstract implementation for GxExpressionInterface
rem *
rem * @author Hyyan Abo Fakher
rem */
class public GxExpressionAbstract implements GxExpressionInterface
rem /**
rem * {@inheritDoc}
rem */
method public boolean equals(GxExpressionInterface expression!)
methodret #toString().equals(expression!.toString())
methodend
classend
rem /**
rem * Expressions are Javascript functions which are written as strings. When you provide an expression to the grid,
rem * the grid converts the expression into a function for you and then executes the function on the client.
rem *
rem * Expressions can be used with the columns <i>ValueGetterExpression</i>, <i>ValueFormatterExpression</i>,
rem * <i>ValueSetterExpression</i>, <i>ValueParserExpression</i>.
rem *
rem * For example you can add a virtual column to the grid and define a value getter expression to
rem * get the value of this column based on other two columns.
rem *
rem * Expression can be as simple as <i>"data.CDNumber"</i> or more complex as the <i>GxExpressionNumbersFormatter</i>
rem * which can accepts parameters.
rem *
rem * When working with expression key the following points in mind:
rem *
rem * <ol>
rem * <li> If your expression has the word return in it, then the grid will assume it is a multi-line expression and will not wrap it
rem * <li> If a ValueGetterExpression does not have the word 'return' in it, then the grid will insert the 'return' statement and the ';' for you
rem * <li> f your expression has many lines, then you will need to provide the ';' at the end of each line and also provide the 'return' statement.
rem * </ol>
rem *
rem * Expression can access several predefined variables :<br><br>
rem *
rem * <table border="1" cellpadding="10">
rem * <tbody>
rem * <tr>
rem * <td><strong> x</strong></td>
rem * <td> Mapped from cell value</td>
rem * </tr>
rem * <tr>
rem * <td> <strong>value</strong></td>
rem * <td> Same as x</td>
rem * </tr>
rem * <tr>
rem * <td> <strong>oldValue</strong></td>
rem * <td> Mapped from the cell's old value, this is mapped in editing</td>
rem * </tr>
rem * <tr>
rem * <td> <strong>newValue</strong></td>
rem * <td> Mapped from the cell's new value, this is mapped in editing</td>
rem * </tr>
rem * <tr>
rem * <td> <strong>data</strong></td>
rem * <td> Mapped from the DataRow</td>
rem * </tr>
rem * <tr>
rem * <td> <strong>column</strong></td>
rem * <td> Current column</td>
rem * </tr>
rem * <tr>
rem * <td> <strong>columnGroup</strong></td>
rem * <td> Current column group</td>
rem * </tr>
rem * <tr>
rem * <td> <strong>getValue</strong></td>
rem * <td> The value after it is processed by the ValueGetterExpression</td>
rem * </tr>
rem * <tr>
rem * <td> <strong>ctx</strong></td>
rem * <td> The grid client context</td>
rem * </tr>
rem * </tbody>
rem *</table>
rem *
rem * @see GxExpressionNumbersFormatter
rem *
rem * @author Hyyan Abo Fakher
rem */
class public GxExpression extends GxExpressionAbstract
rem /**
rem * Expression string
rem */
field public BBjString Expression!
rem /**
rem * disable the default constructor
rem */
method private GxExpression()
methodend
rem /**
rem * @param BBjString expression!
rem */
method public GxExpression(BBjString expression!)
#super!()
#Expression! = expression!
methodend
rem /**
rem * @return BBjString
rem */
method public String toString()
methodret #Expression!
methodend
classend
rem /**
rem * The class defines a grid expression to format strings according to BBj supported masks
rem *
rem * @author Hyyan Abo Fakher
rem */
class public GxExpressionStringsFormatter extends GxExpressionAbstract
rem /**
rem * The String mask to use.
rem */
field public BBjString Mask! = null()
rem /**
rem * A prefix to append to the value after it is formatted
rem */
field public BBjString Prefix! = ""
rem /**
rem * A suffix to append to the value after it is formatted
rem */
field public BBjString Suffix! = ""
rem /**
rem * Disable the default constructor
rem */
method private GxExpressionStringsFormatter()
methodend
rem /**
rem * Construct a new GxExpressionNumbersFormatter
rem *
rem * @param BBjString mask! The number's mask
rem */
method public GxExpressionStringsFormatter(BBjString mask!)
#super!()
#Mask! = mask!
methodend
rem /**
rem * {@inheritDoc}
rem */
method public String toString()
declare StringBuilder ex!
if(#getMask() = null() ) then
methodret null()
FI
ex! = new StringBuilder()
ex!.append(String.format("var mask = '%s';" , #getMask()))
ex!.append("var theValue = getValue || value;")
ex!.append(String.format("if ((mask && theValue) && (theValue != null)) {return '%s' + BBj.Masks.Types.string(theValue, mask) + '%s'}",#getPrefix() , #getSuffix()))
ex!.append(String.format("return (theValue ? '%s' + theValue + '%s' : '');",#getPrefix() , #getSuffix()))
methodret ex!.toString()
methodend
classend
rem /**
rem * The class defines a grid expression to format numbers according to BBj supported masks
rem *
rem * @author Hyyan Abo Fakher
rem */
class public GxExpressionNumbersFormatter extends GxExpressionAbstract
rem /**
rem * The Number mask to use.
rem */
field public BBjString Mask! = null()
rem /**
rem * The number's group separator to use. by default the bbj group separator will be used
rem */
field public BBjString GroupSeparator! = ""
rem /**
rem * The number's decimal separator to use. by default the bbj decimal separator will be used
rem */
field public BBjString DecimalSeparator! = ""
rem /**
rem * A prefix to append to the value after it is formatted
rem */
field public BBjString Prefix! = ""
rem /**
rem * A suffix to append to the value after it is formatted
rem */
field public BBjString Suffix! = ""
rem /**
rem * Disable the default constructor
rem */
method private GxExpressionNumbersFormatter()
methodend
rem /**
rem * Construct a new GxExpressionNumbersFormatter
rem *
rem * @param BBjString mask! The number's mask
rem */
method public GxExpressionNumbersFormatter(BBjString mask!)
#super!()
#Mask! = mask!
methodend
rem /**
rem * Construct a new GxExpressionNumbersFormatter
rem *
rem * @param BBjString mask! The number's mask
rem * @param BBjString groupSeparator! The number's group separator to use
rem */
method public GxExpressionNumbersFormatter(BBjString mask!, BBjString groupSeparator!)
#this!(mask!)
#GroupSeparator! = groupSeparator!
methodend
rem /**
rem * Construct a new GxExpressionNumbersFormatter
rem *
rem * @param BBjString mask! The number's mask
rem * @param BBjString groupSeparator! The number's group separator to use
rem * @param BBjString decimalSeparator! The number's decimal separator to use
rem */
method public GxExpressionNumbersFormatter(BBjString mask!, BBjString groupSeparator!, BBjString decimalSeparator!)
#this!(mask!, groupSeparator!)
#DecimalSeparator! = decimalSeparator!
methodend
rem /**
rem * {@inheritDoc}
rem */
method public String toString()
declare StringBuilder ex!
if(#getMask() = null() ) then
methodret null()
FI
ex! = new StringBuilder()
ex!.append(String.format("var mask = '%s';" , #getMask()))
ex!.append(String.format("var groupSep = '%s' || ctx.numberGroupSep;" , #getGroupSeparator()))
ex!.append(String.format("var decimalSep = '%s' || ctx.numberDecimalSep;" , #getDecimalSeparator()))
ex!.append("var theValue = getValue || value;")
ex!.append(String.format("if (mask && (theValue != null && !isNaN(theValue))) {return '%s' + BBj.Masks.Types.number(theValue, mask, groupSep, decimalSep) + '%s'}",#getPrefix() , #getSuffix()))
ex!.append(String.format("return (theValue ? '%s' + theValue + '%s' : '');",#getPrefix() , #getSuffix()))
methodret ex!.toString()
methodend
classend
rem /**
rem * The class defines a grid expression to format bytes to one of the following measuring terms :
rem * <i>['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']</i>
rem *
rem * @author Hyyan Abo Fakher
rem */
class public GxExpressionBytesFormatter extends GxExpressionNumbersFormatter
rem /**
rem * Number of decimal to use when parsing the size. default is 2
rem */
field public BBjNumber Decimals! = 4
rem /**
rem * Disable the default constructor
rem */
method private GxExpressionBytesFormatter()
methodend
rem /**
rem * Construct a new GxExpressionNumbersFormatter
rem *
rem * @param BBjString mask! The number's mask
rem */
method public GxExpressionBytesFormatter(BBjString mask!)
#super!(mask!)
methodend
rem /**
rem * Construct a new GxExpressionNumbersFormatter
rem *
rem * @param BBjString mask! The number's mask
rem * @param BBjString groupSeparator! The number's group separator to use
rem */
method public GxExpressionBytesFormatter(BBjString mask!, BBjString groupSeparator!)
#super!(mask!,groupSeparator!)
methodend
rem /**
rem * Construct a new GxExpressionNumbersFormatter
rem *
rem * @param BBjString mask! The number's mask
rem * @param BBjString groupSeparator! The number's group separator to use
rem * @param BBjString decimalSeparator! The number's decimal separator to use
rem */
method public GxExpressionBytesFormatter(BBjString mask!, BBjString groupSeparator!, BBjString decimalSeparator!)
#super!(mask!,groupSeparator!,decimalSeparator!)
methodend
rem /**
rem * {@inheritDoc}
rem */
method public String toString()
declare StringBuilder ex!
if(#getMask() = null() ) then
methodret null()
FI
ex! = new StringBuilder()
ex!.append(String.format("var mask = '%s';" , #getMask()))
ex!.append(String.format("var groupSep = '%s' || ctx.numberGroupSep;" , #getGroupSeparator()))
ex!.append(String.format("var decimalSep = '%s' || ctx.numberDecimalSep;" , #getDecimalSeparator()))
ex!.append( "var theValue = getValue || value;")
ex!.append("if ((mask && theValue) && (theValue != null && !isNaN(theValue))) {")
ex!.append(String.format(" if (theValue === 0) return '%s' + '0 Bytes' + '%s';",#getPrefix() , #getSuffix()))
ex!.append(" var k = 1024;")
ex!.append(String.format("var dm = %1$s < 0 ? 0 : %1$s;" , #getDecimals()))
ex!.append(" var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];")
ex!.append(" var i = Math.floor(Math.log(theValue) / Math.log(k));")
ex!.append(" var r = parseFloat((theValue / Math.pow(k, i)).toFixed(dm));")
ex!.append(String.format(" return '%s' + BBj.Masks.Types.number(r, mask, groupSep, decimalSep) + ' ' + sizes[i] + '%s';",#getPrefix() , #getSuffix()))
ex!.append("}")
ex!.append(String.format("return (theValue ? '%s' + theValue + '%s' : '');",#getPrefix() , #getSuffix()))
methodret ex!.toString()
methodend
classend
rem /**
rem * The class defines a grid expression to format date/timestamps according to BBj supported masks
rem *
rem * @author Hyyan Abo Fakher
rem */
class public GxExpressionDateTimesFormatter extends GxExpressionAbstract
rem /**
rem * The date / timestamp mask to use.
rem */
field public BBjString Mask! = null()
rem /**
rem * The date / timestamp locale to use.
rem */
field public BBjString Locale! = null()
rem /**
rem * The date / timezone timezone to use.
rem */
field public BBjString Timezone! = null()
rem /**
rem * Disable the default constructor
rem */
method private GxExpressionDateTimesFormatter()
methodend
rem /**
rem * Construct a new GxExpressionDateTimesFormatter
rem *
rem * @param BBjString mask! The date/timestamp mask
rem */
method public GxExpressionDateTimesFormatter(BBjString mask!)
#super!()
#Mask! = mask!
methodend
rem /**
rem * Construct a new GxExpressionDateTimesFormatter
rem *
rem * @param BBjString mask! The date/timestamp mask
rem * @param BBjString locale! The date/timestamp locale
rem */
method public GxExpressionDateTimesFormatter(BBjString mask!, BBjString locale!)
#this!(mask!)
#Locale! = locale!
methodend
rem /**
rem * Construct a new GxExpressionDateTimesFormatter
rem *
rem * @param BBjString mask! The date/timestamp mask
rem * @param BBjString locale! The date/timestamp locale
rem * @param BBjString locale! The date/timestamp locale
rem */
method public GxExpressionDateTimesFormatter(BBjString mask!, BBjString locale!, BBjString timezone!)
#this!(mask!, locale!)
#Timezone! = timezone!
methodend
rem /**
rem * {@inheritDoc}
rem */
method public String toString()
declare StringBuilder ex!
if(#getMask() = null() ) then
methodret null()
FI
ex! = new StringBuilder()
ex!.append(String.format("var mask = '%s';" , #getMask()))
ex!.append(String.format("var locale = '%s' || null;" , #getLocale()), err=*next)
ex!.append(String.format("var timezone = '%s' || null;" , #getTimezone()), err=*next)
ex!.append("locale = typeof locale !== 'undefined' ? locale : ctx.locale ? ctx.locale : null;")
ex!.append("locale = locale ? locale.replace('_','-') : locale;")
ex!.append("timezone = typeof timezone !== 'undefined' ? timezone : ctx.timezone ? ctx.timezone : null;")
ex!.append("var theValue = getValue || value;")
ex!.append("if((mask && theValue) && theValue != null) {return BBj.Masks.Types.date(theValue, mask, locale, timezone)}")
ex!.append("return theValue || '';")
methodret ex!.toString()
methodend
classend
rem /**
rem * Alias for GxExpressionDateTimesFormatter
rem *
rem * @author Hyyan Abo Fakher
rem */
class public GxExpressionTimestampsFormatter extends GxExpressionDateTimesFormatter
rem /**
rem * Disable the default constructor
rem */
method private GxExpressionTimestampsFormatter()
methodend
rem /**
rem * Construct a new GxExpressionTimestampsFormatter
rem *
rem * @param BBjString mask! The date/timestamp mask
rem */
method public GxExpressionTimestampsFormatter(BBjString mask!)
#super!(mask!)
methodend
rem /**
rem * Construct a new GxExpressionTimestampsFormatter
rem *
rem * @param BBjString mask! The date/timestamp mask
rem * @param BBjString locale! The date/timestamp locale
rem */
method public GxExpressionTimestampsFormatter(BBjString mask!, BBjString locale!)
#super!(mask!, locale!)
methodend
rem /**
rem * Construct a new GxExpressionTimestampsFormatter
rem *
rem * @param BBjString mask! The date/timestamp mask
rem * @param BBjString locale! The date/timestamp locale
rem * @param BBjString locale! The date/timestamp locale
rem */
method public GxExpressionTimestampsFormatter(BBjString mask!, BBjString locale!, BBjString timezone!)
#super!(mask!, locale!, timezone!)
methodend
classend
rem /**
rem * The class defines a grid expression to format date/timestamps according to BBj supported masks
rem *
rem * @deprecated since version 0.101.0, GxExpressionDatesFormatter is deprecated. Use GxExpressionDateTimesFormatter instead.
rem *
rem * @author Hyyan Abo Fakher
rem */
class public GxExpressionDatesFormatter extends GxExpressionDateTimesFormatter
rem /**
rem * Disable the default constructor
rem */
method private GxExpressionDatesFormatter()
methodend
rem /**
rem * Construct a new GxExpressionDatesFormatter
rem *
rem * @param BBjString mask! The date/timestamp mask
rem */
method public GxExpressionDatesFormatter(BBjString mask!)
#super!(mask!)
#warn()
methodend
rem /**
rem * Construct a new GxExpressionDatesFormatter
rem *
rem * @param BBjString mask! The date/timestamp mask
rem * @param BBjString locale! The date/timestamp locale
rem */
method public GxExpressionDatesFormatter(BBjString mask!, BBjString locale!)
#super!(mask!, locale!)
#warn()
methodend
rem /**
rem * Construct a new GxExpressionDatesFormatter
rem *
rem * @param BBjString mask! The date/timestamp mask
rem * @param BBjString locale! The date/timestamp locale
rem * @param BBjString locale! The date/timestamp locale
rem */
method public GxExpressionDatesFormatter(BBjString mask!, BBjString locale!, BBjString timezone!)
#super!(mask!, locale!, timezone!)
#warn()
methodend
method private void warn()
GxLogger.warn("GxExpressions","since version 0.101.0, GxExpressionDatesFormatter is deprecated. Use GxExpressionDateTimesFormatter instead.")
methodend
classend