-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtangy-form.js
825 lines (744 loc) · 24.5 KB
/
tangy-form.js
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';
import { afterNextRender } from '@polymer/polymer/lib/utils/render-status.js';
import './util/html-element-props.js'
import './style/tangy-common-styles.js'
import { t } from './util/t.js'
import { tangyFormReducer } from './tangy-form-reducer.js'
import { TangyFormResponseModel } from './tangy-form-response-model.js';
import { TangyFormItemHelpers } from './tangy-form-item-callback-helpers.js'
// Core elements.
import './tangy-form-item.js'
import './tangy-complete-button.js'
import './tangy-overlay.js'
import './tangy-input-groups.js'
import './tangy-input-group.js'
import './tangy-list.js'
import './tangy-template.js'
// <!-- Dependencies -->
import '@polymer/paper-fab/paper-fab.js';
import '@polymer/paper-icon-button/paper-icon-button.js';
import '@polymer/paper-tabs/paper-tab.js';
import '@polymer/paper-tabs/paper-tabs.js';
import 'translation-web-component/t-lang.js'
import 'translation-web-component/t-translate.js'
import 'translation-web-component/t-t.js'
import 'translation-web-component/t-number.js'
/**
* `tangy-form`
*
* @customElement
* @polymer
* @demo demo/index.html
*/
export class TangyForm extends PolymerElement {
/*
* Public API
*/
getMeta() {
return {
...this._meta,
items: this._meta.items.map(metaItem => {
const item = this.querySelector(`tangy-form-item#${metaItem.id}`)
return {
...metaItem,
inputs: item.getInputsMeta()
}
})
}
}
// For creating a new response. Call it directly to force a new response when working programatically otherwise
// this will get called later if no response has been assigned by the time afterNextRender is called.
newResponse() {
let initialResponse = new TangyFormResponseModel()
initialResponse.form = this.getProps()
this.querySelectorAll('tangy-form-item').forEach((item) => {
initialResponse.items.push(item.getProps())
})
this.response = initialResponse
}
// Set a form response to this property to resume a form response.
set response(value) {
this._responseHasBeenSet = true
this.store.dispatch({ type: 'FORM_OPEN', response: value, itemsInDom: [...this.querySelectorAll('tangy-form-item')].map(itemEl => itemEl.getProps())})
this.fireHook('on-open')
}
// Get the current form response.
get response() {
return (this._responseHasBeenSet) ? this.store.getState() : null
}
// Get an array of all inputs across items.
get inputs() {
return this.response.items.reduce((acc, item) => [...acc, ...item.inputs], [])
}
// Get an object where object properties are input names and object property values are corresponding input values.
get values() {
return this.inputs.reduce((acc, input) => Object.assign({}, acc, {[input.name]: input.value}), {})
}
inject(name, value) {
this._injected[name] = value
this.querySelectorAll('tangy-form-item').forEach(item => {
item.inject(name, value)
})
}
// Get the value of a single input by name.
getValue(name) {
let state = this.store.getState()
let inputs = []
state.items.forEach(item => inputs = [...inputs, ...item.inputs])
//return (inputs[name]) ? inputs[name].value : undefined
let foundInput = inputs.find(input => (input.name === name) ? input.value : false)
if (foundInput && typeof foundInput.value === 'object') {
let values = []
foundInput.value.forEach(subInput => {
if (subInput.value) {
values.push(subInput.name)
}
})
return values
} else if (foundInput && foundInput.hasOwnProperty('value')) {
return foundInput.value
} else {
return ''
}
}
// Go to next item. Note, if validation fails, you will not go to the next item.
next() {
// Note that next needs to be called on the item level so that it can determine if it is valid and can actually close.
this.querySelector('tangy-form-item[open=""]').next()
}
// Go to previous item. Note, if validation fails, you will not go to the previous item.
back() {
// Note that back needs to be called on the item level so that it can determine if it is valid and can actually close.
this.querySelector('tangy-form-item[open=""]').back()
}
// Disable an item so it is skipped.
itemDisable(itemId) {
let state = this.store.getState()
let item = state.items.find(item => itemId === item.id)
if (item && !item.disabled) this.store.dispatch({ type: 'ITEM_DISABLE', itemId: itemId })
}
// Enable an item so it is not skipped.
itemEnable(itemId) {
let state = this.store.getState()
let item = state.items.find(item => itemId === item.id)
if (item && item.disabled) this.store.dispatch({ type: 'ITEM_ENABLE', itemId: itemId })
}
enableItemReadOnly() {
this.store.dispatch({
type: 'ENABLE_ITEM_READONLY'
})
}
disableItemReadOnly() {
this.store.dispatch({
type: 'DISABLE_ITEM_READONLY'
})
}
// Hides Open/Close buttons
hideItemButtons() {
this.store.dispatch({
type: 'HIDE_ITEM_BUTTONS'
})
}
// Shows Open/Close buttons
showItemButtons() {
this.store.dispatch({
type: 'SHOW_ITEM_BUTTONS'
})
}
/*
* Private.
*/
static get template() {
return html`
<style include="tangy-common-styles"></style>
<style>
:host {
width: 100%;
display: block;
margin: 0px;
padding: 0px;
}
#previousItemButton,
#nextItemButton {
position: relative;
color: #ffffff;
}
#previousItemButton[disabled],
#nextItemButton[disabled] {
color: #979797;
}
#previousItemButton {
float: left;
}
#nextItemButton {
float: right;
}
#markCompleteFab, #lockedFab {
position: fixed;
right: 7px;
top: 24px;
}
#markCompleteButton {
position: fixed;
right: 7px;
bottom: 2px;
color: var(--accent-text-color);
}
#markCompleteFab {}
:host(:not([linear-mode])) #nextItemButton,
:host(:not([linear-mode])) #previousItemButton
{
display: none;
}
:host([hide-complete-fab]) #markCompleteFab {
display: none !important;
}
#progress {
position: fixed;
bottom: 0px;
}
paper-progress {
width: 100%;
}
:host(:not([has-summary])) #bar {
display:none;
}
#bar {
width:100%;
background-color: var(--primary-color);
color: var(--accent-color);
}
#bar-filler {
height: 45px;
}
#markCompleteButton,
#previousItemButton,
#nextItemButton {
padding: 0;
color: var(--accent-color);
--paper-fab-iron-icon: {
color: var(--accent-color);
height: 75px;
width: 75px;
};
}
#markCompleteButton paper-icon-button,
#previousItemButton paper-icon-button,
#nextItemButton paper-icon-button, paper-icon-button {
width: 75px;
height: 75px;
}
paper-fab[disabled] {
background-color: #cccccc !important;
}
paper-fab.pressed {
background-color: #3c5b8d !important;
}
paper-fab.keyboard-focus {
background-color: #1976d2;
}
paper-tab[aria-selected=true] {
background: #f26f10;
color: #FFF
}
:host(:not[error-logging]) #errors {
display: none;
}
.error {
background: white;
border: solid 5px red;
padding: 5px;
}
#close-all-items{
display:none;
}
</style>
<div id="nav"></div>
<template is="dom-if" if="{{complete}}">
<div style="text-align:right">
<paper-button id="open-all-items" on-click="openAllItems" >
[[t.openAllItems]]
</paper-button>
<paper-button id="close-all-items" on-click="closeAllItems" >
[[t.closeAllItems]]
</paper-button>
</div>
<div id="bar">
<paper-tabs selected="[[tabIndex]]" scrollable>
<template is="dom-if" if="{{hasSummary}}">
<paper-tab id="summary-button" on-click="onClickSummaryTab">[[t.summary]]</paper-tab>
</template>
<paper-tab id="response-button" on-click="onClickResponseTab">[[t.response]]</paper-tab>
</paper-tabs>
</div>
</template>
<div id="errors"></div>
<div id="items"><slot></slot></div>
`;
}
onClickSummaryTab() {
this.store.dispatch({ type: 'SHOW_SUMMARY' })
setTimeout(() => {
this.querySelector('[summary]').scrollIntoView({ behavior: 'smooth', block: 'start' })
}, 200)
}
onClickResponseTab() {
this.store.dispatch({ type: 'SHOW_RESPONSE' })
//this.querySelectorAll('tangy-form-item').forEach(el => el.hidden = false)
this.querySelectorAll('tangy-form-item')[0].scrollIntoView({ behavior: 'smooth', block: 'center' })
}
openAllItems(){
this.store.dispatch({ type: 'OPEN_ALL_ITEMS' })
this.shadowRoot.querySelector('#open-all-items').style.display = 'none';
this.shadowRoot.querySelector('#close-all-items').style.display = 'initial';
}
closeAllItems(){
this.store.dispatch({ type: 'CLOSE_ALL_ITEMS' })
this.shadowRoot.querySelector('#open-all-items').style.display = 'initial';
this.shadowRoot.querySelector('#close-all-items').style.display = 'none';
}
static get is() { return 'tangy-form'; }
static get properties() {
return {
fullscreenNavAlign: {
type: String,
// Value of 'top' or 'bottom'
value: 'top',
reflectToAttribute: true
},
fullscreenInline: {
type: Boolean,
value: false,
reflectToAttribute: true
},
openInFullscreen: {
type: Boolean,
value: false,
reflectToAttribute: true
},
fullscreen: {
type: Boolean,
value: false,
reflectToAttribute: true
},
title: {
type: String,
value: ''
},
complete: {
type: Boolean,
value: false,
reflectToAttribute: true
},
// Set linear-mode to turn on navigation and turn off item action buttons.
linearMode: {
type: Boolean,
value: false,
reflectToAttribute: true
},
// Hide closed items to focus user on current item.
hideClosedItems: {
type: Boolean,
value: false,
reflectToAttribute: true
},
hideCompleteFab: {
type: Boolean,
value: false,
reflectToAttribute: true
},
tabIndex: {
type: Number,
value: 0,
reflectToAttribute: true
},
showResponse: {
type: Boolean,
value: false,
reflectToAttribute: true
},
showSummary: {
type: Boolean,
value: false,
reflectToAttribute: true
},
hasSummary: {
type: Boolean,
value: false,
reflectToAttribute: true
},
fullScreenGranted: {
type: Boolean,
value: false,
},
exitClicks: {
type: Number,
value: undefined,
reflectToAttribute: true
},
cycleSequences: {
type: String,
value: undefined,
reflectToAttribute: true
},
recordItemFirstOpenTimes: {
type: Boolean,
value: false,
reflectToAttribute: true
},
endUnixtime: {
type: Number,
value: undefined,
reflectToAttribute: true
},
lastSaveUnixtime: {
type: Number,
value: undefined,
reflectToAttribute: true
}
}
}
constructor() {
super()
this._injected = {}
this.t = {
summary: 'summary',
response: 'response',
openAllItems: 'Open All Items',
closeAllItems: 'Close All Items',
}
this._responseHasBeenSet = false
// Set up the store.
this.store = Redux.createStore(
tangyFormReducer,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
)
}
ready() {
super.ready()
// Stash original meta state.
this._meta = {
form: this.getProps(),
items: [...this.querySelectorAll('tangy-form-item')].map(itemEl => {
return {
...itemEl.getProps(),
// Skip because tangy-form-item may not be initialized.
// inputs: itemEl.getInputsMeta()
}
})
}
// Pass events of items to the reducer.
this.hasLazyItems = false
this.querySelectorAll('tangy-form-item').forEach((item) => {
if (item.getAttribute('src')) this.hasLazyItems = true
// Pass in the store so on-change and on-open logic can access it.
item.store = this.store
if (this.linearMode) item.noButtons = true
item.addEventListener('change', this.onItemChange.bind(this))
item.addEventListener('ITEM_NEXT', this.onItemNext.bind(this))
item.addEventListener('ITEM_BACK', this.onItemBack.bind(this))
item.addEventListener('ITEM_CLOSED', this.onItemClosed.bind(this))
item.addEventListener('ITEM_OPENED', this.onItemOpened.bind(this))
item.addEventListener('FORM_RESPONSE_COMPLETE', this.onFormResponseComplete.bind(this))
item.addEventListener('FORM_RESPONSE_NO_CONSENT', this.onFormResponseNoConsent.bind(this))
item.addEventListener('logic-error', this.onItemError.bind(this))
item.addEventListener('go-to', event => this.onItemGoTo(event))
})
// Subscribe to the store to reflect changes.
if (this.hasLazyItems) {
// Because items open asynchronously, we may need to throttle render to prevent two renders happening at the same time.
this.unsubscribe = this.store.subscribe(this.throttledReflect.bind(this))
} else {
// We're synchronous. Much simpler!
this.unsubscribe = this.store.subscribe(this.reflect.bind(this))
}
// Dispatch events out when state changes.
this.store.subscribe(state => {
this.dispatchEvent(new CustomEvent('TANGY_FORM_UPDATE'))
})
if (this.hasAttribute('on-submit')) {
this.addEventListener('submit', (event) => {
let form = this
this.fireHook('on-submit')
})
}
if (this.hasAttribute('on-resubmit')) {
this.addEventListener('resubmit', (event) => {
let form = this
this.fireHook('on-resubmit')
})
}
afterNextRender(this, function() {
if (this._responseHasBeenSet === false) {
this.newResponse()
}
})
this.addEventListener('enter-fullscreen', () => {
this.store.dispatch({type: 'ENTER_FULLSCREEN'})
})
this.addEventListener('exit-fullscreen', () => {
this.store.dispatch({type: 'EXIT_FULLSCREEN'})
})
}
disconnectedCallback() {
if (this.unsubscribe) this.unsubscribe()
}
onFormResponseComplete(event) {
this.store.dispatch({
type: 'ITEM_SAVE',
item: event.target.getProps()
})
const cancelledBeforeSubmit = !this.dispatchEvent(new CustomEvent('before-submit', {cancelable: true}))
if (cancelledBeforeSubmit) return
let cancelledSubmit
if (!this.response.hasUnlocked) {
cancelledSubmit = !this.dispatchEvent(new CustomEvent('submit', {cancelable: true}))
} else {
cancelledSubmit = !this.dispatchEvent(new CustomEvent('resubmit', {cancelable: true}))
}
if (cancelledSubmit) return
this.store.dispatch({
type: 'FORM_RESPONSE_COMPLETE'
})
const cancelledComplete = !this.dispatchEvent(new CustomEvent('tangy-form-complete', {cancelable: true}))
if (cancelledComplete) return
if (this.hasSummary) {
this.store.dispatch({ type: "SHOW_SUMMARY" })
} else {
this.store.dispatch({ type: "SHOW_RESPONSE" })
}
if (!this.response.hasUnlocked) {
!this.dispatchEvent(new CustomEvent('after-submit', {cancelable: true})) || !this.dispatchEvent(new CustomEvent('tangy-form-complete', {cancelable: true}))
} else {
!this.dispatchEvent(new CustomEvent('after-resubmit', {cancelable: true})) || !this.dispatchEvent(new CustomEvent('tangy-form-complete', {cancelable: true}))
}
}
onFormResponseNoConsent(event) {
this.store.dispatch({
type: 'ITEM_SAVE',
item: event.target.getProps()
})
const cancelledSubmit = !this.dispatchEvent(new CustomEvent('submit', {cancelable: true}))
if (cancelledSubmit) return
this.store.dispatch({
type: 'FORM_RESPONSE_COMPLETE'
})
// const cancelledComplete = !this.dispatchEvent(new CustomEvent('tangy-form-complete', {cancelable: true}))
// if (cancelledComplete) return
if (this.hasSummary) {
this.store.dispatch({ type: "SHOW_SUMMARY" })
} else {
this.store.dispatch({ type: "SHOW_RESPONSE" })
}
}
onItemChange(event) {
this.store.dispatch({
type: 'ITEM_CHANGE',
itemId: event.target.id
})
}
onItemNext(event) {
this.store.dispatch({
type: 'ITEM_SAVE',
item: event.target.getProps()
})
this.fireHook('on-change')
this.fireHook('custom-scoring-logic')
this.focusOnNextItem()
}
onItemBack(event) {
this.store.dispatch({
type: 'ITEM_SAVE',
item: event.target.getProps()
})
this.fireHook('on-change')
this.fireHook('custom-scoring-logic')
this.focusOnPreviousItem()
}
onItemGoTo(event) {
this.store.dispatch({
type: 'ITEM_SAVE',
item: event.target.getProps()
})
this.fireHook('on-change')
this.fireHook('custom-scoring-logic')
this.store.dispatch({
type: 'ITEM_GO_TO',
itemId: event.detail
})
}
onItemOpened(event) {
this.store.dispatch({
type: 'ITEM_SAVE',
item: event.target.getProps()
})
}
onItemClosed(event) {
this.store.dispatch({
type: 'ITEM_SAVE',
item: event.target.getProps()
})
}
onItemError(event) {
this.errorMessage(event.detail)
}
// Prevent parallel reflects, leads to race conditions.
throttledReflect(iAmQueued = false) {
// If there is an reflect already queued, we can quit.
if (this.reflectQueued && !iAmQueued) return
if (this.reflectRunning) {
this.reflectQueued = true
setTimeout(() => this.throttledReflect(true), 200)
} else {
this.reflectRunning = true
this.reflect()
this.reflectRunning = false
if (iAmQueued) this.reflectQueued = false
}
}
// Apply state in the store to the DOM.
reflect() {
let state = this.store.getState()
// Set initial this.previousState
let firstState = false
if (!this.previousState) {
this.previousState = state
firstState = true
}
this.setProps(state.form)
// Tabs
if (state.form && state.form.complete) {
//this.shadowRoot.querySelector('paper-tabs').selected = (state.form.showSummary) ? '0' : '1'
}
// Set state in tangy-form-item elements.
let items = [].slice.call(this.querySelectorAll('tangy-form-item'))
items.forEach((item) => {
let index = state.items.findIndex((itemState) => item.id == itemState.id)
if (index !== -1) item.setProps(state.items[index])
})
// Find item to scroll to.
if (state.focusIndex !== this.previousState.focusIndex) {
if (items[state.focusIndex]) items[state.focusIndex].scrollIntoView({ behavior: 'smooth', block: 'start' })
}
// Dispatch ALL_ITEMS_CLOSED if all items are now closed.
let previouslyClosedItemCount = (this.previousState.items.filter(item => !item.open)).length
let currentlyClosedItemCount = (state.items.filter(item => !item.open)).length
if (previouslyClosedItemCount !== currentlyClosedItemCount && currentlyClosedItemCount === state.items.length) {
this.dispatchEvent(new CustomEvent('ALL_ITEMS_CLOSED'))
}
if ((!this.previousState.form.fullscreenEnabled && state.form.fullscreenEnabled) || (firstState && state.form.fullscreenEnabled)) {
this.enableFullscreen()
}
else if (this.previousState.form.fullscreenEnabled && !state.form.fullscreenEnabled) {
this.disableFullscreen()
}
// Stash as previous state.
this.previousState = Object.assign({}, state)
}
fireHook(hook, event) {
// If locked, bail.
if (this.locked) return
// If no hook, bail.
if (!this.getAttribute(hook)) return
// Prepare some helper variables.
let state = this.store.getState()
// Inputs.
let inputsArray = []
state.items.forEach(item => inputsArray = [...inputsArray, ...item.inputs])
let inputsKeyedByName = {}
inputsArray.forEach(input => inputsKeyedByName[input.name] = input)
let inputs = inputsKeyedByName
// Items.
let items = {}
state.items.forEach(item => items[item.name] = item)
let inputEls = this.shadowRoot.querySelectorAll('[name]')
let tangyFormStore = this.store
let itemEnable = name => this.itemEnable(name)
let itemDisable = name => this.itemDisable(name)
let skip = name => this.itemDisable(name)
let unskip = name => this.itemEnable(name)
let sectionEnable = name => this.itemEnable(name)
let sectionDisable = name => this.itemDisable(name)
let helpers = new TangyFormItemHelpers(this)
let getValue = (name) => this.getValue(name)
let inputHide = (name) => helpers.inputHide(name)
let inputShow = (name) => helpers.inputShow(name)
let inputDisable = (name) => helpers.inputDisable(name)
let inputEnable = (name) => helpers.inputEnable(name)
let itemsPerMinute = (input) => helpers.itemsPerMinute(input)
let numberOfItemsAttempted = (input) => helpers.numberOfItemsAttempted(input)
let numberOfCorrectItems = (input) => helpers.numberOfCorrectItems(input)
let numberOfIncorrectItems = (input) => helpers.numberOfIncorrectItems(input)
let gridAutoStopped = (input) => helpers.gridAutoStopped(input)
// Use itemInputs instead of inputs in modules such as Class in order to summon only the inputs on-screen/in the currently active form.
let itemInputs = [...this.shadowRoot.querySelectorAll('[name]')].reduce((acc, input) => Object.assign({}, acc, {[input.name]: input}), {})
try {
eval(`
${Object.keys(this._injected).map(name => `var ${name} = this._injected['${name}']`).join('\n')}
${this.getAttribute(hook)}
`)
} catch (e) {
const message = `${t(`Error detected in the form's logic:`)} ${hook}`
console.log(message)
console.log(e)
this.errorMessage(message)
}
}
errorMessage(message) {
if (!this.hasAttribute('error-logging')) return
const errorEl = document.createElement('div')
errorEl.innerHTML = message
errorEl.classList.add('error')
this.shadowRoot.querySelector('#errors').appendChild(errorEl)
this.style.background = 'red'
setTimeout(() => {
this.style.background = 'transparent'
}, 400)
}
focusOnPreviousItem(event) {
// Dispatch action.
let state = this.store.getState()
let item = state.items.find(item => item.open)
this.store.dispatch({ type: 'ITEM_BACK', itemId: item.id })
}
focusOnNextItem(event) {
// Dispatch action.
let state = this.store.getState()
let item = state.items.find(item => item.open)
this.store.dispatch({ type: 'ITEM_NEXT', itemId: item.id })
}
disableFullscreen() {
if(document.webkitExitFullscreen) document.webkitExitFullscreen()
if(document.exitFullscreen) document.exitFullscreen()
this.removeEventListener('click', this.enableFullscreen, true)
}
enableFullscreen() {
// If fullscreen inline is enabled, don't use Fulscreen API.
if (this.fullscreenInline) return
if(this.requestFullscreen) {
this.requestFullscreen()
.then(message => {
this.fullScreenGranted = true;
})
.catch(err => {
console.log(`Error attempting to enable full-screen mode: ${err.message} (${err.name})`);
this.fullScreenGranted = false;
this.dispatchEvent(new CustomEvent('fullscreen-rejected'))
});
} else if(this.mozRequestFullScreen) {
this.mozRequestFullScreen();
} else if(this.webkitRequestFullscreen) {
this.webkitRequestFullscreen();
} else if(this.msRequestFullscreen) {
this.msRequestFullscreen();
}
}
unlock(options ={}) {
const meta = {...this.getMeta(), ...options}
this.store.dispatch({
type: 'UNLOCK',
meta
})
}
}
window.customElements.define(TangyForm.is, TangyForm);