-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportal.js
251 lines (224 loc) · 10.4 KB
/
portal.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
(() => {
const modal = (id) => `
<div id="${id}" class="modal pr-0" tabindex="-1" role="dialog">
<div class="modal-dialog modal-dialog-centered modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close fullscreen" style="font-size:1.3rem" data-bs-toggle="false">
<span>⛶</span>
</button>
<button type="button" class="close refresh ml-0" style="font-size:1.3rem">
<span>↻</span>
</button>
<button type="button" class="close ml-0" data-bs-dismiss="modal">
<span>×</span>
</button>
</div>
<div class="modal-body" style="padding:0;display:flex;justify-content:center;">
<div class="iframeLoading" style="align-self:center;color:grey;font-size:16px;">Loading...</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-success saveButton">Save & Close</button>
</div>
</div>
</div>
</div>`
const tr = () => `
<tr id="ProjectPortal-tr" sq_id="ProjectPortal">
<td class="labelrc col-12" colspan="2" style="padding:0;border:0">
</td>
</tr>`
const iframe = (link) => `<iframe style="display:none;width:100%;border:none" src="${link}"></iframe>`
const em = ExternalModules.UWMadison.ProjectPortal
const insideModal = window.self !== window.top
let widthCache = 'auto'
let loaded = false
$(document).ready(() => {
$('form tr').last().after(tr())
if (require_change_reason && insideModal) {
Swal.fire({
icon: 'error',
title: 'Incompatible Project',
text: 'Project portal cannot function when using "change reasons" on the target project. Please disable this feature.',
})
}
if (typeof Shazam !== "object") {
loadPortals()
return
}
let oldCallback = Shazam.beforeDisplayCallback
Shazam.beforeDisplayCallback = () => {
if (typeof oldCallback == "function")
oldCallback()
loadPortals()
}
setTimeout(loadPortals, 1000)
})
const loadPortals = () => {
if (loaded) return
loaded = true
// Prevent google chrome bug with hidden forms
$(window).bind('beforeunload', () => {
if ($('.modal').is(':visible') === false)
$(".modal").remove()
})
$.each(em.config, (name, config) => {
if ($(`.${name}`).length == 0) return
$(`.${name}`).off() // Remove all Redcap events
config.url = customPipes(config.url)
// Not a modal, just a boring link
if (!config.modal) {
$(`.${name}`).attr('href', config.url)
$(`.${name}`).on('click', function () {
window.location = $(this).attr('href')
})
return
}
// Stop that pop-up from happening
if (insideModal) require_change_reason = 0
// Insert and modify the modal HTML
$("#ProjectPortal-tr td").append(modal(name))
if (config.hideClose)
$(`#${name} .btn-danger`).hide()
if (config.hideSave)
$(`#${name} .btn-success`).hide()
$(`#${name}`).modal({
show: false,
backdrop: 'static'
})
// Setup our click and events
$(`.${name}`).on('click', () => $(`#${name}`).modal('show'))
$(`#${name}`).on('shown.bs.modal', () => $(".modal-open").css("overflow-y", 'auto'))
$(`#${name}`).on('show.bs.modal', () => showModal(name, config))
})
}
const showModal = (name, config) => {
const $modal = $(`#${name}`)
$modal.find('.modal-dialog').css('max-width', parseCSS(config.width, $modal))
$(window).on('resize', () => {
let t = $(`#${name} .fullscreen`).data('toggle')
let h = t ? window.innerHeight : parseCSS(config.height)
$(`#${name} .modal-dialog`).css('margin-top', t ? 0 : '1.75rem')
$(`#${name} .modal-dialog`).css('margin-bottom', t ? 0 : '1.75rem')
$(`#${name} .modal-content`).css('height', h)
})
$(window).resize()
if ($modal.find('iframe').length != 0) return
$modal.find('.refresh').on('click', function () {
$(`#${name} iframe`).get(0).contentWindow.location.reload()
})
$modal.find('.fullscreen').on('click', () => {
let t = $(`#${name} .fullscreen`).data('toggle')
$(`#${name} .fullscreen`).data('toggle', !t)
if (t) {
widthCache = $(`#${name} .modal-dialog`).width()
if (config.hide != 'all')//Only change width if we aren't showing a normal form
$(`#${name} .modal-dialog`).css('max-width', `${window.innerWidth}px`)
} else {
$(`#${name} .modal-dialog`).css('max-width', parseCSS(config.width, $(`#${name}`)))
}
$(`#${name} .modal-dialog`).css('width', t ? "auto" : widthCache)
$(window).resize()
})
$modal.find('.modal-body').append(iframe(config.url))
$modal.find('iframe').on('load', function () {
let content = $(this).contents()
if ($(content).find("#header").text() == "Server Error") return
if (config.hide == 'all') {
$(content).find('body > :not(#form)').not('.ui-dialog').hide()
$(content).find('#form').appendTo($(content).find('body'))
$(content).find('tr[id$=__-tr]').hide()
$(content).find('html').css('overflow-x', 'hidden')
} else if (config.hide == 'nav') {
$(content).find("#west, #south, #subheader, #fade").remove()
}
$(`#${name} .iframeLoading`).hide()
$(this).show()
enableRedcapSaveButtons()
$(content).find('input.rc-autocomplete').css('width', 'auto')
setTimeout(() => {
$(`#${name}`).find('.modal-dialog').css('max-width', parseCSS(config.width, $(`#${name}`)))
}, 500)
})
$modal.find('.saveButton').on('click', function () {
let content = $(`#${name} iframe`).contents()
// Check for required feilds
let reqFieldMissing = false
$(content).find('*[req]:visible').each(function () {
if (reqFieldMissing) return false
let $input = $(this).find('select,input,textarea')
if ($input.length == 1) // Select, input, or textarea
reqFieldMissing = $input.val() == ""
else // checkbox
reqFieldMissing = $input.not('*[id]').get().map(x => x.value).every(x => !x)
})
if (reqFieldMissing) {
Swal.fire({
icon: 'error',
title: 'Missing Required Fields',
text: 'You did not provide a value for some fields that require a value. Please enter a value for the fields marked as required on this page.',
})
return
}
disableRedcapSaveButtons()
$(`#${name}`).modal('hide')
if ($(content).find("#saveButton, .saveButton").not('.modal #saveButton, .modal .saveButton').length != 0) {
$(`#${name} iframe`).get(0).contentWindow.jQuery("#saveButton, .saveButton").not('.modal #saveButton, .modal .saveButton').click()
} else {
$(content).find("#submit-btn-savecontinue").click()
}
})
}
const enableRedcapSaveButtons = () => {
$("#__SUBMITBUTTONS__-tr button").css('pointer-events', '')
$(".modalTempDisableSave").last().remove()
}
const disableRedcapSaveButtons = () => {
$("#__SUBMITBUTTONS__-tr button").css('pointer-events', 'none')
if ($(".modalTempDisableSave").length > 0)
$("#questiontable").after(`<div class='modalTempDisableSave d-none'></div>`)
else
$("#__SUBMITBUTTONS__-tr button").last().after(`<span class='text-bold text-danger modalTempDisableSave'><br>* Form saving disabled while modal is saved</span>`)
setTimeout(enableRedcapSaveButtons, 3000)
}
const customPipes = (input) => {
const ymd = 'y-MM-dd'
const config = {
"today": (date, days) => {
let result = new Date(date)
result.setDate(result.getDate() + days)
return result
},
"work": (date, days) => {
let result = new Date(date)
result.setDate(result.getDate() + days + (result.getDay() === 6 ? 2 : +!result.getDay()) +
(Math.floor((days - 1 + (result.getDay() % 6 || 1)) / 5) * 2))
return result
}
}
input = input.replaceAll("[current-url]", encodeURIComponent(window.location.href))
input = input.replaceAll("[today]", formatDate(new Date(), ymd))
$.each(config, (pipeName, func) => {
if (!input.includes(`[${pipeName}`)) return
let pipes = input.match(new RegExp(`(?:${pipeName}){1}([+-]([0-9]*))+`, 'g'))
pipes.forEach((pipe) => {
if (!pipe) return
let number = parseInt(pipe.split(pipeName)[1])
let text = formatDate(func(new Date(), number), ymd)
input = input.replace(`[${pipe}]`, text)
})
})
return input
}
const parseCSS = (setting, $modalWidth) => {
if (!setting && $modalWidth) {
const scrollable = $modalWidth.find('iframe').contents().height() > ($modalWidth.find('iframe').height() + 1)
return scrollable ? '822px' : '806px' // default blank Widths
}
setting = setting || '90%' // Default blank Height
if (setting.includes('%'))
return Math.floor(($modalWidth ? window.innerWidth : window.innerHeight) * (parseInt(setting.replace('%', '')) / 100)) + "px"
return setting
}
})()