/
bind-custom-events.js
116 lines (100 loc) · 3.99 KB
/
bind-custom-events.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
import $ from 'jquery'
import CanMap from 'can-map'
import _includes from 'lodash/includes'
import constants from '@caliorg/a2jdeps/models/constants'
import compute from 'can-compute'
// List of field types that can be filled with the `sample` property.
const canUseSampleValues = [
constants.ftText, constants.ftTextLong,
constants.ftTextPick, constants.ftNumber,
constants.ftNumberDollar, constants.ftNumberSSN,
constants.ftNumberPhone, constants.ftNumberZIP,
constants.ftNumberPick, constants.ftDateMDY
]
// This function sets some event handles for custom events used to communicate
// the parts of the author app that are outside of the scope of CanJS.
export default function bindCustomEvents (appState) {
let $authorApp = $('#author-app')
// user clicks the preview button in the edit page modal
$authorApp.on('edit-page:preview', function (evt, pageName) {
appState.attr('previewPageName', pageName)
appState.attr('page', 'preview')
appState.attr('previewMode', true)
})
// internal parts of the code call `window.traceAlert` which no longer
// updates the DOM manually but triggers this event
$authorApp.on('author:trace-alert', function (evt, data) {
let alertMessages = appState.attr('viewerAlertMessages')
let guideId = alertMessages.attr('guideId')
// we set a static `guideId` property to the alertMessages list to avoid
// mixing alert messages from different interviews when the selected
// interview changes.
if (!guideId || (guideId && guideId !== data.guideId)) {
alertMessages.replace([])
alertMessages.attr('guideId', data.guideId)
}
alertMessages.push({ message: data.message, open: true })
})
// user double clicks a guide in the interview tab or clicks the open guide
// button in the toolbar.
$authorApp.on('author:item-selected', function (evt, guideId) {
let alertMessages = appState.attr('viewerAlertMessages')
// this check is similar to the one made in the `author:trace-alert`,
// *this* covers the case where a selected interview does not generate
// alert messages (which causes `author:trace-alert` to not fire at all)
if (guideId !== alertMessages.attr('guideId')) {
alertMessages.replace([])
alertMessages.attr('guideId', guideId)
}
// this is work around for binding error in can-map
const gGuideMapData = {}
Object.keys(window.gGuide).forEach((key) => {
gGuideMapData[key] = window.gGuide[key]
})
const gGuideMap = new CanMap(gGuideMapData)
appState.attr({
guideId: guideId,
guidePath: window.gGuidePath,
guide: gGuideMap
})
})
// when window.gGuide is saved to the server successfully,
// sync the map reference in the appState.
$authorApp.on('author:guide-updated', function () {
// this is work around for binding error in can-map
const gGuideMapData = {}
Object.keys(window.gGuide).forEach((key) => {
gGuideMapData[key] = window.gGuide[key]
})
const gGuideMap = new CanMap(gGuideMapData)
appState.attr('guide', gGuideMap)
})
// TODO: Figure out a better way to do this.
$authorApp.on('author:fill-page-sample', function () {
let $fields = $('a2j-fields')
// do nothing if `a2j-fields` component not in the DOM.
if (!$fields.length) return
let pageFields = $fields[0].viewModel.attr('fields')
pageFields.forEach(function (field) {
let answerVm = field.attr('_answerVm')
let fieldType = field.attr('type')
let sampleValue = field.attr('sample')
if (_includes(canUseSampleValues, fieldType)) {
field.attr('hasError', false)
answerVm.attr('values', sampleValue)
}
})
})
// Updates legacy global window.gGuide with changes to CanJS guide.vars
var serializedGuideVars = compute(function () {
var guideVars = appState.attr('guide.vars')
if (guideVars) {
return guideVars.serialize()
}
})
serializedGuideVars.on('change', function (ev, newVars) {
if (newVars) {
window.gGuide.vars = newVars
}
})
}