@operato/input 1.0.0-alpha.22 → 1.0.0-alpha.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,560 @@
1
+ import '@material/mwc-button'
2
+
3
+ import { PropertyValues, css, html } from 'lit'
4
+ import { customElement, property, state } from 'lit/decorators.js'
5
+
6
+ import { OxFormField } from './ox-form-field'
7
+ import { i18next } from '@operato/i18n'
8
+
9
+ function createCronRegex(type: 'sec' | 'min' | 'hour' | 'day' | 'month' | 'dayOfWeek') {
10
+ // https://gist.github.com/dkandalov/a2aed17cfdeb65243022
11
+ var regexByField = {} as any
12
+ regexByField['sec'] = '[0-5]?\\d'
13
+ regexByField['min'] = '[0-5]?\\d'
14
+ regexByField['hour'] = '[01]?\\d|2[0-3]'
15
+ regexByField['day'] = '0?[1-9]|[12]\\d|3[01]'
16
+ regexByField['month'] = '[1-9]|1[012]'
17
+ regexByField['dayOfWeek'] = '[0-7]'
18
+
19
+ var crontabFields = [type]
20
+ if (!type) crontabFields = ['sec', 'min', 'hour', 'day', 'month', 'dayOfWeek']
21
+
22
+ crontabFields.forEach(field => {
23
+ var number = regexByField[field]
24
+ var range =
25
+ '(?:' +
26
+ number +
27
+ ')' +
28
+ '(?:' +
29
+ '(?:-|/|,' +
30
+ ('dayOfWeek' === field ? '|#' : '') +
31
+ ')' +
32
+ '(?:' +
33
+ number +
34
+ ')' +
35
+ ')?'
36
+ if (field === 'dayOfWeek') range += '(?:L)?'
37
+ if (field === 'month') range += '(?:L|W)?'
38
+ regexByField[field] = '\\?|\\*|' + range + '(?:,' + range + ')*'
39
+ })
40
+
41
+ var monthValues = 'JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC'
42
+ var monthRange = '(?:' + monthValues + ')(?:(?:-)(?:' + monthValues + '))?'
43
+ regexByField['month'] += '|\\?|\\*|' + monthRange + '(?:,' + monthRange + ')*'
44
+
45
+ var dayOfWeekValues = 'MON|TUE|WED|THU|FRI|SAT|SUN'
46
+ var dayOfWeekRange = '(?:' + dayOfWeekValues + ')(?:(?:-)(?:' + dayOfWeekValues + '))?'
47
+ regexByField['dayOfWeek'] += '|\\?|\\*|' + dayOfWeekRange + '(?:,' + dayOfWeekRange + ')*'
48
+
49
+ if (!type)
50
+ return (
51
+ '^\\s*($' +
52
+ '|#' +
53
+ '|\\w+\\s*=' +
54
+ '|' +
55
+ '(' +
56
+ regexByField['sec'] +
57
+ ')\\s+' +
58
+ '(' +
59
+ regexByField['min'] +
60
+ ')\\s+' +
61
+ '(' +
62
+ regexByField['hour'] +
63
+ ')\\s+' +
64
+ '(' +
65
+ regexByField['day'] +
66
+ ')\\s+' +
67
+ '(' +
68
+ regexByField['month'] +
69
+ ')\\s+' +
70
+ '(' +
71
+ regexByField['dayOfWeek'] +
72
+ ')(|\\s)+' +
73
+ ')$'
74
+ )
75
+ else return `^${regexByField[type]}$`
76
+ }
77
+
78
+ @customElement('ox-input-crontab')
79
+ export class OxInputCrontab extends OxFormField {
80
+ static styles = css`
81
+ :host {
82
+ display: block;
83
+ width: 100%;
84
+ height: 100%;
85
+ border: 0;
86
+ background-color: var(--main-section-background-color);
87
+ }
88
+
89
+ :host * {
90
+ box-sizing: border-box;
91
+ }
92
+ :host *:focus {
93
+ outline: none;
94
+ }
95
+
96
+ form {
97
+ display: grid;
98
+ width: 100%;
99
+ height: 100%;
100
+ padding: 1rem;
101
+ grid-template: auto auto 1fr auto / repeat(6, 1fr);
102
+ grid-gap: 0.5rem;
103
+ justify-content: center;
104
+ align-items: center;
105
+ overflow: auto;
106
+ }
107
+
108
+ label[for='example'] {
109
+ text-align: right;
110
+ grid-column: 3;
111
+ font: normal 1em var(--theme-font);
112
+ color: var(--secondary-color);
113
+ text-transform: capitalize;
114
+ }
115
+
116
+ #example {
117
+ grid-column: 4 / span 3;
118
+ width: 100%;
119
+ height: 100%;
120
+ font: normal 0.9em var(--theme-font);
121
+ text-transform: capitalize;
122
+ border-radius: var(--border-radius);
123
+ border: var(--border-dark-color);
124
+ }
125
+
126
+ input {
127
+ width: 100%;
128
+ margin-top: var(--margin-default);
129
+ padding: 5px;
130
+ border-radius: var(--border-radius);
131
+ border: var(--border-dark-color);
132
+ font: normal 1em var(--theme-font);
133
+ }
134
+ input:focus {
135
+ border: 1px solid var(--primary-color);
136
+ }
137
+
138
+ input:invalid {
139
+ border: 1px solid var(--status-danger-color);
140
+ color: var(--status-danger-color);
141
+ }
142
+
143
+ label {
144
+ width: 100%;
145
+ height: 100%;
146
+ font: normal 0.8em var(--theme-font);
147
+ color: var(--primary-color);
148
+ }
149
+
150
+ label:not([for='example']) {
151
+ text-align: center;
152
+ }
153
+
154
+ #input-wrapper {
155
+ grid-column: span 6;
156
+ display: flex;
157
+ flex-wrap: wrap;
158
+ margin: 0 -0.25rem;
159
+ }
160
+
161
+ #input-wrapper > div {
162
+ flex: 1;
163
+ display: grid;
164
+ grid-template-rows: 1fr auto;
165
+ grid-gap: 0.5rem;
166
+ min-width: 60px;
167
+ max-width: 33%;
168
+ margin: 0.25rem;
169
+ }
170
+
171
+ #tooltip {
172
+ grid-column: span 6;
173
+ display: grid;
174
+ grid-template-columns: auto 1fr;
175
+ grid-gap: 0;
176
+ margin: auto;
177
+ grid-auto-rows: min-content;
178
+ align-self: center;
179
+ align-items: center;
180
+ }
181
+
182
+ #tooltip > div {
183
+ padding: 0.25rem 0.5rem;
184
+ border-bottom: #ccc 1px dashed;
185
+ font: normal 0.9em var(--theme-font);
186
+ }
187
+
188
+ #tooltip > .crontab-value {
189
+ text-align: right;
190
+ color: var(--secondary-color);
191
+ }
192
+
193
+ #tooltip > .crontab-description {
194
+ text-align: left;
195
+ color: #585858;
196
+ }
197
+
198
+ #button-wrapper {
199
+ grid-column: 1 / span 6;
200
+ display: flex;
201
+ flex-wrap: wrap-reverse;
202
+ flex-direction: row-reverse;
203
+ margin: -0.25rem;
204
+ }
205
+
206
+ mwc-button {
207
+ background-color: var(--secondary-color);
208
+ border-radius: var(--button-border-radius);
209
+ --mdc-theme-primary: #fff;
210
+ margin: 0.25rem;
211
+ }
212
+ mwc-button:hover,
213
+ mwc-button:active {
214
+ background-color: var(--primary-color);
215
+ }
216
+ `
217
+
218
+ @property({ type: String }) value?: string
219
+ @property({ type: String }) second?: string
220
+ @property({ type: String }) minute?: string
221
+ @property({ type: String }) hour?: string
222
+ @property({ type: String }) dayOfMonth?: string
223
+ @property({ type: String }) month?: string
224
+ @property({ type: String }) dayOfWeek?: string
225
+
226
+ @state() tooltip: { value: string; description: string }[] = []
227
+
228
+ render() {
229
+ return html`
230
+ <form>
231
+ <label for="example">${i18next.t('label.examples')}</label>
232
+ <select
233
+ id="example"
234
+ @change=${(e: Event) => (this.value = (e.currentTarget as HTMLInputElement).value)}
235
+ .value=${this.value}
236
+ >
237
+ <optgroup label="${i18next.t('label.second by second')}">
238
+ <option value="* * * * * *">${i18next.t('text.every second')}</option>
239
+ <option value="0/2 * * * * *">${i18next.t('text.every 2 seconds')}</option>
240
+ <option value="0/15 * * * * *">${i18next.t('text.every 15 seconds')}</option>
241
+ <option value="0/30 * * * * *">${i18next.t('text.every 30 seconds')}</option>
242
+ </optgroup>
243
+ <optgroup label="${i18next.t('label.minute by minute')}">
244
+ <option value="0 * * * * *">${i18next.t('text.every minute')}</option>
245
+ <option value="0 0/2 * * * *">${i18next.t('text.every 2 minutes')}</option>
246
+ <option value="0 0/15 * * * *">${i18next.t('text.every 15 minutes')}</option>
247
+ <option value="0 0/30 * * * *">${i18next.t('text.every half hour')}</option>
248
+ </optgroup>
249
+ <optgroup label="${i18next.t('label.hourly')}">
250
+ <option value="0 0 * * * *">${i18next.t('text.every hour')}</option>
251
+ <option value="0 0 0/2 * * *">${i18next.t('text.every 2 hours')}</option>
252
+ <option value="0 0 0/12 * * *">${i18next.t('text.every 12 hours')}</option>
253
+ <option value="0 0 10-19 * * *">${i18next.t('text.every hour during working time')}</option>
254
+ </optgroup>
255
+ <optgroup label="${i18next.t('label.daily')}">
256
+ <option value="0 0 0 * * *">${i18next.t('text.every day')}</option>
257
+ </optgroup>
258
+ <optgroup label="${i18next.t('label.weekly')}">
259
+ <option value="0 0 0 * * SUN">${i18next.t('text.every sunday')}</option>
260
+ <option value="0 0 0 * * 0">${i18next.t('text.every sunday(2)')}</option>
261
+ <option value="0 0 0 * * 1-5">${i18next.t('text.every weekday')}</option>
262
+ </optgroup>
263
+ <optgroup label="${i18next.t('label.monthly')}">
264
+ <option value="0 0 0 1 * *">${i18next.t('text.the first day of every month')}</option>
265
+ <option value="0 0 10 21 * *">${i18next.t('text.10 am on every payday')}</option>
266
+ </optgroup>
267
+ <optgroup label="${i18next.t('label.yearly')}">
268
+ <option value="0 0 0 1 1 *">${i18next.t('text.the first day of every year')}</option>
269
+ <option value="0 0 0 25 12 *">${i18next.t('text.every christmas')}</option>
270
+ </optgroup>
271
+ </select>
272
+ <div id="input-wrapper">
273
+ <div>
274
+ <input
275
+ id="second-input"
276
+ class="second"
277
+ type="text"
278
+ .value=${this.second || ''}
279
+ @input=${(e: Event) => (this.second = (e.currentTarget as HTMLInputElement).value)}
280
+ @focus=${(e: Event) => {
281
+ this.showTooltip('second')
282
+ }}
283
+ pattern=${createCronRegex('sec')}
284
+ required
285
+ />
286
+ <label for="second-input" class="second">${i18next.t('label.second')}</label>
287
+ </div>
288
+ <div>
289
+ <input
290
+ id="minute-input"
291
+ class="minute"
292
+ type="text"
293
+ .value=${this.minute || ''}
294
+ @input=${(e: Event) => (this.minute = (e.currentTarget as HTMLInputElement).value)}
295
+ @focus=${(e: Event) => {
296
+ this.showTooltip('minute')
297
+ }}
298
+ pattern=${createCronRegex('min')}
299
+ required
300
+ />
301
+ <label for="minute-input" class="minute">${i18next.t('label.minute')}</label>
302
+ </div>
303
+ <div>
304
+ <input
305
+ id="hour-input"
306
+ class="hour"
307
+ type="text"
308
+ .value=${this.hour || ''}
309
+ @input=${(e: Event) => (this.hour = (e.currentTarget as HTMLInputElement).value)}
310
+ @focus=${(e: Event) => {
311
+ this.showTooltip('hour')
312
+ }}
313
+ pattern=${createCronRegex('hour')}
314
+ required
315
+ />
316
+ <label for="hour-input" class="hour">${i18next.t('label.hour')}</label>
317
+ </div>
318
+ <div>
319
+ <input
320
+ id="day-of-month-input"
321
+ class="day-of-month"
322
+ type="text"
323
+ .value=${this.dayOfMonth || ''}
324
+ @input=${(e: Event) => (this.dayOfMonth = (e.currentTarget as HTMLInputElement).value)}
325
+ @focus=${(e: Event) => {
326
+ this.showTooltip('dayOfMonth')
327
+ }}
328
+ pattern=${createCronRegex('day')}
329
+ required
330
+ />
331
+ <label for="day-of-month-input" class="day-of-month">${i18next.t('label.day-of-month')}</label>
332
+ </div>
333
+ <div>
334
+ <input
335
+ id="month-input"
336
+ class="month"
337
+ type="text"
338
+ .value=${this.month || ''}
339
+ @input=${(e: Event) => (this.month = (e.currentTarget as HTMLInputElement).value)}
340
+ @focus=${(e: Event) => {
341
+ this.showTooltip('month')
342
+ }}
343
+ pattern=${createCronRegex('month')}
344
+ required
345
+ />
346
+ <label for="month-input" class="month">${i18next.t('label.month')}</label>
347
+ </div>
348
+ <div>
349
+ <input
350
+ id="day-of-week-input"
351
+ class="day-of-week"
352
+ type="text"
353
+ .value=${this.dayOfWeek || ''}
354
+ @input=${(e: Event) => (this.dayOfWeek = (e.currentTarget as HTMLInputElement).value)}
355
+ @focus=${(e: Event) => {
356
+ this.showTooltip('dayOfWeek')
357
+ }}
358
+ pattern=${createCronRegex('dayOfWeek')}
359
+ required
360
+ />
361
+ <label for="day-of-week-input" class="day-of-week">${i18next.t('label.day-of-week')}</label>
362
+ </div>
363
+ </div>
364
+ <div id="tooltip">
365
+ ${this.tooltip.map(
366
+ tip => html`
367
+ <div class="crontab-value">${tip.value}</div>
368
+ <div class="crontab-description">${i18next.t(`text.${tip.description}`)}</div>
369
+ `
370
+ )}
371
+ </div>
372
+ </form>
373
+ `
374
+ }
375
+
376
+ get focusableElements(): HTMLElement[] {
377
+ return Array.from(this.renderRoot.querySelectorAll('select, input, mwc-button'))
378
+ }
379
+
380
+ firstUpdated() {
381
+ ;(this.renderRoot.querySelector('input') as HTMLInputElement).focus()
382
+ this.renderRoot.addEventListener('change', this.onChange.bind(this))
383
+ }
384
+
385
+ updated(changes: PropertyValues<this>) {
386
+ if (changes.has('value')) {
387
+ var values = (this.value || '').split(' ')
388
+
389
+ if (values.length == 1) values = ['*', '*', '*', '*', '*', '*']
390
+ else if (values.length == 5) values = ['0'].concat(values)
391
+
392
+ this.second = values[0]
393
+ this.minute = values[1]
394
+ this.hour = values[2]
395
+ this.dayOfMonth = values[3]
396
+ this.month = values[4]
397
+ this.dayOfWeek = values[5]
398
+ }
399
+ }
400
+
401
+ showTooltip(type: 'second' | 'minute' | 'hour' | 'dayOfMonth' | 'month' | 'dayOfWeek') {
402
+ switch (type) {
403
+ case 'second':
404
+ case 'minute':
405
+ this.tooltip = [
406
+ {
407
+ value: '*',
408
+ description: 'any value'
409
+ },
410
+ {
411
+ value: ',',
412
+ description: 'value list separator'
413
+ },
414
+ {
415
+ value: '-',
416
+ description: 'range of values'
417
+ },
418
+ {
419
+ value: '/',
420
+ description: 'step values'
421
+ },
422
+ {
423
+ value: '0-59',
424
+ description: 'allowed values'
425
+ }
426
+ ]
427
+ break
428
+ case 'hour':
429
+ this.tooltip = [
430
+ {
431
+ value: '*',
432
+ description: 'any value'
433
+ },
434
+ {
435
+ value: ',',
436
+ description: 'value list separator'
437
+ },
438
+ {
439
+ value: '-',
440
+ description: 'range of values'
441
+ },
442
+ {
443
+ value: '/',
444
+ description: 'step values'
445
+ },
446
+ {
447
+ value: '0-23',
448
+ description: 'allowed values'
449
+ }
450
+ ]
451
+ break
452
+
453
+ case 'dayOfMonth':
454
+ this.tooltip = [
455
+ {
456
+ value: '*',
457
+ description: 'any value'
458
+ },
459
+ {
460
+ value: ',',
461
+ description: 'value list separator'
462
+ },
463
+ {
464
+ value: '-',
465
+ description: 'range of values'
466
+ },
467
+ {
468
+ value: '/',
469
+ description: 'step values'
470
+ },
471
+ {
472
+ value: '1-31',
473
+ description: 'allowed values'
474
+ }
475
+ ]
476
+ break
477
+
478
+ case 'month':
479
+ this.tooltip = [
480
+ {
481
+ value: '*',
482
+ description: 'any value'
483
+ },
484
+ {
485
+ value: ',',
486
+ description: 'value list separator'
487
+ },
488
+ {
489
+ value: '-',
490
+ description: 'range of values'
491
+ },
492
+ {
493
+ value: '/',
494
+ description: 'step values'
495
+ },
496
+ {
497
+ value: '1-12',
498
+ description: 'allowed values'
499
+ },
500
+ {
501
+ value: 'JAN-DEC',
502
+ description: 'alternative single values'
503
+ }
504
+ ]
505
+ break
506
+
507
+ case 'dayOfWeek':
508
+ this.tooltip = [
509
+ {
510
+ value: '*',
511
+ description: 'any value'
512
+ },
513
+ {
514
+ value: ',',
515
+ description: 'value list separator'
516
+ },
517
+ {
518
+ value: '-',
519
+ description: 'range of values'
520
+ },
521
+ {
522
+ value: '/',
523
+ description: 'step values'
524
+ },
525
+ {
526
+ value: '0-6',
527
+ description: 'allowed values'
528
+ },
529
+ {
530
+ value: 'SUN-SAT',
531
+ description: 'alternative single values'
532
+ }
533
+ ]
534
+ break
535
+
536
+ default:
537
+ this.tooltip = []
538
+ break
539
+ }
540
+ }
541
+
542
+ onChange() {
543
+ var form = this.renderRoot.querySelector('form') as HTMLFormElement
544
+ var valid = form.checkValidity()
545
+ if (!valid) {
546
+ form.reportValidity()
547
+ return
548
+ }
549
+
550
+ this.value = `${this.second} ${this.minute} ${this.hour} ${this.dayOfMonth} ${this.month} ${this.dayOfWeek}`
551
+
552
+ this.dispatchEvent(
553
+ new CustomEvent('change', {
554
+ bubbles: true,
555
+ composed: true,
556
+ detail: this.value
557
+ })
558
+ )
559
+ }
560
+ }