-
Notifications
You must be signed in to change notification settings - Fork 5
/
App.vue
240 lines (213 loc) · 7.8 KB
/
App.vue
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
<template>
<div class="redirect-app" data-interrupt-submit :data-has-unsaved-changes="unsavedChanges">
<ElButton @click="newRedirect()" class="pra-add-redirect">{{ text('Add redirect') }}</ElButton>
<RedirectList/>
<section class="pra-default-redirect">
<label :for="'pra-default-redirect-destination-' + _uid">{{ text('Default redirect') }} <small>{{ text('type a node title or ID or paste a URL') }}</small></label>
<DestinationField
:id="'pra-default-redirect-destination-' + _uid"
:class="{'pra-has-error': showErrors && !destinationIsValid}"
:value="destination"
:placeholder="text('Type to search nodes')"
:show-dropdown-on-focus="true"
data-key="values"
label-key="label"
:getData="getNodes"
:url="$root.$options.settings.endpoints.nodes"
search-param="s"
:count="20"
@input="item => {destination = item}"
/>
<div v-if="showErrors && !destinationIsValid" class="pra-error-message">{{ text('destination error') }}</div>
</section>
<RedirectDialog/>
</div>
</template>
<script>
import { mapState } from 'vuex'
import isEqual from 'lodash/isEqual'
import { clone, dispatch, validateDestination } from '@/utils'
import api from '@/utils/api'
import RedirectList from './components/RedirectList.vue'
import RedirectDialog from './components/RedirectDialog.vue'
import DestinationField from './components/DestinationField.vue'
export default {
name: 'app',
components: {
RedirectList,
RedirectDialog,
DestinationField
},
data () {
return {
showErrors: false /** {boolean} Visibility of the default redirect’s error message. */
}
},
computed: {
/** Map `destination` to `value` and `prettyDestination` to `label` for the DestinationField component. */
destination: {
get () {
return {
value: this.defaultRedirect.destination,
label: this.defaultRedirect.prettyDestination
}
},
set (val) {
this.$store.commit({
type: 'updateDefaultRedirect',
destination: val.value,
prettyDestination: val.label
})
}
},
/**
* Validate the default redirect’s destination.
* @return {boolean} Is it valid?
*/
destinationIsValid () {
return validateDestination(this.defaultRedirect.destination)
},
/**
* Compare the redirects and the default redirect with initial state.
* @return {boolean} Is there a difference to initial state?
*/
unsavedChanges () {
if (this.redirects.length !== this.initialData.redirects.length) return true
if (!isEqual(this.defaultRedirect, this.initialData.defaultRedirect)) return true
for (let i = 0, j = this.redirects.length; i < j; i++) {
if (!isEqual(this.redirects[i], this.initialData.redirects[i])) return true
}
return false
},
...mapState([
'redirects', /** {Object[]} Collection of all the redirects with filters. */
'defaultRedirect', /** {Object} The default redirect. */
'initialData' /** {Object} The `defaultRedirect` and `redirects` that were initially loaded. */
])
},
created () {
// Provide a shortcut to the settings.
this.$root.$options.settings = Drupal.settings.campaignion_wizard[this.$root.$options.drupalContainer.id]
},
mounted () {
// Initialize data
this.$store.commit({
type: 'initData',
redirects: this.$root.$options.settings.redirects,
defaultRedirectUrl: this.$root.$options.settings.default_redirect_url
})
/**
* Handle events from campaignion_vue’s interrupt-submit.js
* @param {Event} e - The event.
*/
const listener = e => {
/** Answer back to interrupt-submit.js, it’s ok to leave the page. */
const leavePage = () => {
dispatch(this.$root.$el, 'resume-leave-page')
}
/** Answer back to interrupt-submit.js, don’t leave the page. */
const stayOnPage = () => {
dispatch(this.$root.$el, 'cancel-leave-page')
}
if (e.type === 'request-leave-page') {
// User clicked 'back' button.
// Forget about unsaved changes if the app is hidden.
if (this.appIsHidden()) {
leavePage()
return
}
if (this.unsavedChanges) {
this.$confirm(this.text('unsaved changes'), this.text('unsaved changes title'), {
confirmButtonText: this.text('Go back anyway'),
cancelButtonText: this.text('Stay on page'),
type: 'warning'
}).then(() => { leavePage() }, () => { stayOnPage() })
} else {
leavePage()
}
} else if (e.type === 'request-submit-page') {
// User clicked one of the submit buttons.
// If nothing has changed, just submit.
if (!this.unsavedChanges) {
leavePage()
return
}
// Validate destination field (only if the app is visible).
if (!this.destinationIsValid && !this.appIsHidden()) {
stayOnPage()
this.showErrors = true
return
}
// If we have arrived here, it’s fine to save.
this.persistData().then(() => {
leavePage()
}, () => {
stayOnPage()
this.$alert(this.text('service unavailable'), this.text('service unavailable title'), {
confirmButtonText: this.text('OK')
})
})
}
}
// Listen to events dispatched by campaignion_vue’s interrupt-submit.js
this.$root.$el.addEventListener('request-submit-page', listener)
this.$root.$el.addEventListener('request-leave-page', listener)
},
methods: {
/**
* Determine whether the app is hidden via the parent container controlled
* by Drupal.
* @return {boolean} Is the app hidden?
*/
appIsHidden () {
return this.$root.$options.drupalContainer.style.display === 'none'
},
/**
* Emit a `newRedirect` event on the global bus.
*/
newRedirect () {
this.$root.$emit('newRedirect')
},
/**
* Persist the redirects to the server.
* @return {Promise} Promise resolving when the request worked.
*/
persistData () {
return new Promise((resolve, reject) => {
// Append the default redirect to the redirects array.
const redirects = clone(this.redirects)
redirects.push(clone(this.defaultRedirect))
api.postData({
url: this.$root.$options.settings.endpoints.redirects,
data: { redirects },
headers: {}
}).then(() => {
resolve()
}, error => {
reject(error)
})
})
},
getNodes: api.getNodes,
text (text) {
switch (text) {
case 'Add redirect': return Drupal.t('Add personalized redirect')
case 'Default redirect': return Drupal.t('Default redirect')
case 'type a node title or ID or paste a URL': return Drupal.t('type a node title or ID or paste a URL')
case 'Type to search nodes': return Drupal.t('Type to search nodes')
case 'destination error': return Drupal.t('Please enter a valid URL or choose a node.')
case 'service unavailable title': return Drupal.t('Service unavailable')
case 'service unavailable': return Drupal.t('The service is temporarily unavailable.\rYour redirects could not be saved.\rPlease try again or contact support if the issue persists.')
case 'unsaved changes title': return Drupal.t('Unsaved changes')
case 'unsaved changes': return Drupal.t('You have unsaved changes!\rYou will lose your changes if you go back.')
case 'OK': return Drupal.t('OK')
case 'Cancel': return Drupal.t('Cancel')
case 'Go back anyway': return Drupal.t('Go back anyway')
case 'Stay on page': return Drupal.t('Stay on page')
}
}
}
}
</script>
<style>
</style>