PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.4
Booking for Appointments and Events Calendar – Amelia v2.4.4
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / v3 / src / assets / js / common / date.js
ameliabooking / v3 / src / assets / js / common Last commit date
appointments.js 4 days ago attendees.js 4 days ago colorManipulation.js 4 days ago customer.js 4 days ago date.js 4 days ago defaultCustomize.js 4 days ago employee.js 4 days ago events.js 4 days ago formFieldsTemplates.js 4 days ago formatting.js 4 days ago helper.js 4 days ago image.js 4 days ago integrationApple.js 4 days ago integrationGoogle.js 4 days ago integrationOutlook.js 4 days ago integrationStripe.js 4 days ago integrationZoom.js 4 days ago licence.js 4 days ago phone.js 4 days ago pricing.js 4 days ago recurring.js 4 days ago responsive.js 4 days ago scrollElements.js 4 days ago settings.js 4 days ago translationsElementPlus.js 4 days ago utilHeaderHeight.js 4 days ago
date.js
378 lines
1 import moment from 'moment'
2 import { DateTime, Settings, Info } from 'luxon'
3 import { settings, shortLocale } from '../../../plugins/settings.js'
4 import { useCookies } from 'vue3-cookies'
5 import { useUrlQueryParams } from './helper'
6
7 Settings.defaultLocale = shortLocale
8
9 const months = []
10 const weekDaysLocale = []
11 const weekDaysShortLocale = []
12
13 for (let i = 1; i <= 12; i++) {
14 months.push(DateTime.local(2022, i, 1).monthLong)
15 if (i <= 7) {
16 weekDaysLocale.push(Info.weekdays('long')[i - 1])
17 weekDaysShortLocale.push(Info.weekdays('short')[i - 1])
18 }
19 }
20
21 const formatRegex = {
22 formatPHPtoJsMap: {
23 d: 'dd',
24 D: 'ccc',
25 j: 'd',
26 l: 'cccc',
27 N: 'c',
28 w: 'c',
29 W: 'W',
30 F: 'MMMM',
31 m: 'MM',
32 M: 'MMM',
33 n: 'M',
34 o: 'kkkk',
35 Y: 'yyyy',
36 y: 'yy',
37 a: 'a',
38 A: 'a',
39 g: 'h',
40 G: 'H',
41 h: 'hh',
42 H: 'HH',
43 i: 'mm',
44 s: 'ss',
45 O: 'ZZZ',
46 P: 'ZZ',
47 c: 'yyyy-MM-dd[T]HH:mm:ssZZ',
48 r: 'ccc, dd MMM yyyy HH:mm:ss ZZZ',
49 U: 'X',
50 T: '',
51 S: 'o',
52 },
53
54 formatPHPtoMomentMap: {
55 d: 'DD',
56 D: 'ddd',
57 j: 'D',
58 l: 'dddd',
59 N: 'E',
60 w: 'd',
61 W: 'W',
62 F: 'MMMM',
63 m: 'MM',
64 M: 'MMM',
65 n: 'M',
66 o: 'GGGG',
67 Y: 'YYYY',
68 y: 'YY',
69 a: 'a',
70 A: 'A',
71 g: 'h',
72 G: 'H',
73 h: 'hh',
74 H: 'HH',
75 i: 'mm',
76 s: 'ss',
77 O: 'ZZ',
78 P: 'Z',
79 c: 'YYYY-MM-DD[T]HH:mm:ssZ',
80 r: 'ddd, DD MMM YYYY HH:mm:ss ZZ',
81 U: 'X',
82 T: '',
83 S: 'o',
84 },
85
86 formatEx: /[dDjlNwWFmMntoYyaAgGhHisOPcrUTS]/g,
87 }
88
89 function useLocalValue(value) {
90 return moment.utc(value, 'YYYY-MM-DD HH:mm').local().format('YYYY-MM-DD HH:mm')
91 }
92
93 function useUtcValue(value) {
94 return moment(value, 'YYYY-MM-DD HH:mm').utc().format('YYYY-MM-DD HH:mm')
95 }
96
97 function useUtcValueOffset(value) {
98 return value ? moment(value, 'YYYY-MM-DD HH:mm:ss').utcOffset() : moment().utcOffset()
99 }
100
101 function useStringFromDate(d) {
102 return (
103 d.getFullYear() +
104 '-' +
105 '0'.concat(d.getMonth() + 1).slice(-2) +
106 '-' +
107 '0'.concat(d.getDate()).slice(-2)
108 )
109 }
110
111 function useFormatTime(t) {
112 return t.split(':')[0] + ':' + t.split(':')[1]
113 }
114
115 function useTimeInSeconds(time) {
116 return moment(time, 'HH:mm').diff(moment().startOf('day'), 'seconds')
117 }
118
119 function useSecondsInTime(seconds) {
120 return moment.utc(seconds * 1000).format('HH:mm')
121 }
122
123 function addSeconds(time, seconds) {
124 return moment(time, 'HH:mm').add(seconds, 'seconds').format('HH:mm')
125 }
126
127 // * Luxon time format
128 function jsTimeFormat() {
129 // Fix for French "G \h i \m\i\n" and "G\hi" format
130 if (
131 settings.wordpress.timeFormat === 'G \\h i \\m\\i\\n' ||
132 settings.wordpress.timeFormat === 'G\\hi'
133 ) {
134 return 'HH:mm'
135 }
136
137 return settings.wordpress.timeFormat.replace(formatRegex.formatEx, function (phpStr) {
138 return formatRegex.formatPHPtoJsMap[phpStr]
139 })
140 }
141
142 // * Moment time format
143 function momentTimeFormat() {
144 // Fix for French "G \h i \m\i\n" and "G\hi" format
145 if (
146 settings.wordpress.timeFormat === 'G \\h i \\m\\i\\n' ||
147 settings.wordpress.timeFormat === 'G\\hi'
148 ) {
149 return 'HH:mm'
150 }
151
152 return settings.wordpress.timeFormat.replace(formatRegex.formatEx, function (phpStr) {
153 return formatRegex.formatPHPtoMomentMap[phpStr]
154 })
155 }
156
157 function jsDateFormat() {
158 // Fix for Portuguese "j \d\e F, Y" format
159 if (settings.wordpress.dateFormat === 'j \\d\\e F, Y') {
160 return 'd MMMM, yyyy'
161 }
162
163 // Fix for Spanish/Catalan "j \d\e F \d\e Y" format
164 if (settings.wordpress.dateFormat === 'j \\d\\e F \\d\\e Y') {
165 return 'd MMMM yyyy'
166 }
167
168 // Remove ordinal suffix (S) as it's not supported in all languages
169 let format = settings.wordpress.dateFormat.replace(/S/g, '')
170
171 return format.replace(formatRegex.formatEx, function (phpStr) {
172 return formatRegex.formatPHPtoJsMap[phpStr]
173 })
174 }
175
176 function momentDateFormat() {
177 // Fix for Portuguese "j \d\e F, Y" format
178 if (settings.wordpress.dateFormat === 'j \\d\\e F, Y') {
179 return 'D MMMM, YYYY'
180 }
181
182 // Fix for Spanish/Catalan "j \d\e F \d\e Y" format
183 if (settings.wordpress.dateFormat === 'j \\d\\e F \\d\\e Y') {
184 return 'D MMMM YYYY'
185 }
186
187 return settings.wordpress.dateFormat.replace(formatRegex.formatEx, function (phpStr) {
188 return formatRegex.formatPHPtoMomentMap[phpStr]
189 })
190 }
191
192 function getFrontedFormattedDateTime(dateTime) {
193 let data = dateTime.split(' ')
194
195 return getFrontedFormattedDate(data[0]) + ' ' + getFrontedFormattedTime(data[1].substring(0, 5))
196 }
197
198 function getFrontedFormattedTime(time, zone = '') {
199 return zone
200 ? DateTime.fromFormat(time, 'HH:mm', { zone: zone }).toFormat(jsTimeFormat())
201 : DateTime.fromFormat(time, 'HH:mm').toFormat(jsTimeFormat())
202 }
203
204 function getFrontedFormattedDate(date) {
205 let dateString = DateTime.fromFormat(date, 'yyyy-MM-dd').toFormat(jsDateFormat())
206
207 // Fix for Portuguese "j \d\e F, Y" format
208 if (settings.wordpress.dateFormat === 'j \\d\\e F, Y') {
209 let result = ''
210
211 dateString.split(' ').forEach(function (value, index) {
212 if (index === 1) {
213 value = value.charAt(0).toUpperCase() + value.slice(1)
214 }
215
216 result = result + value + ' '
217
218 if (index === 0) {
219 result += 'de '
220 }
221 })
222
223 return result
224 }
225
226 // Fix for Spanish/Catalan "j \d\e F \d\e Y" format
227 if (settings.wordpress.dateFormat === 'j \\d\\e F \\d\\e Y') {
228 let result = ''
229
230 let format = !dateString.includes(' de ') ? dateString : dateString.replace('de ', '')
231
232 format.split(' ').forEach(function (value, index) {
233 if (index === 1 || index === 3) {
234 value = value.charAt(0).toUpperCase() + value.slice(1)
235 }
236
237 result = result + value + ' '
238
239 if (index === 0 || index === 1) {
240 result += 'de '
241 }
242 })
243
244 return result
245 }
246
247 return dateString
248 }
249
250 function getFirstDayOfWeek() {
251 return settings.wordpress.startOfWeek
252 }
253
254 function useSecondsToDuration(seconds, hourLabel, minuteLabel) {
255 let hours = Math.floor(seconds / 3600)
256 let minutes = (seconds / 60) % 60
257
258 return (hours ? hours + hourLabel + ' ' : '') + ' ' + (minutes ? minutes + minuteLabel : '')
259 }
260
261 function useConvertedUtcToLocalDateTime(period) {
262 let utcOffset = moment(period, 'YYYY-MM-DD HH:mm:ss').toDate().getTimezoneOffset()
263
264 if (utcOffset > 0) {
265 return moment
266 .utc(period, 'YYYY-MM-DD HH:mm:ss')
267 .subtract(utcOffset, 'minutes')
268 .format('YYYY-MM-DD HH:mm:ss')
269 } else {
270 return moment
271 .utc(period, 'YYYY-MM-DD HH:mm:ss')
272 .add(-1 * utcOffset, 'minutes')
273 .format('YYYY-MM-DD HH:mm:ss')
274 }
275 }
276
277 function getEventFrontedFormattedDateDay(date) {
278 return DateTime.fromFormat(date, 'yyyy-MM-dd').toFormat('dd')
279 }
280
281 function getEventFrontedFormattedDateMonth(date) {
282 return DateTime.fromFormat(date, 'yyyy-MM-dd').toFormat('LLL')
283 }
284
285 function getEventFrontedFormattedTime(time) {
286 return moment(time, 'HH:mm:ss').format(momentTimeFormat())
287 }
288
289 function getDatePickerInitRange() {
290 const vueCookies = useCookies()['cookies']
291
292 let ameliaRangePast = vueCookies.get('ameliaRangePast')
293 let ameliaRangeFuture = vueCookies.get('ameliaRangeFuture')
294
295 if (ameliaRangePast !== null && ameliaRangeFuture !== null) {
296 return [
297 moment().subtract(ameliaRangePast, 'days').toDate(),
298 moment().add(ameliaRangeFuture, 'days').toDate(),
299 ]
300 }
301
302 return [moment().toDate(), moment().add(6, 'days').toDate()]
303 }
304
305 function setDatePickerSelectedDaysCount(start, end) {
306 const vueCookies = useCookies()['cookies']
307 let currentDate = moment().format('YYYY-MM-DD')
308
309 vueCookies.set(
310 'ameliaRangePast',
311 moment(currentDate, 'YYYY-MM-DD').diff(moment(start, 'YYYY-MM-DD'), 'days'),
312 )
313 vueCookies.set(
314 'ameliaRangeFuture',
315 moment(end, 'YYYY-MM-DD').diff(moment(currentDate, 'YYYY-MM-DD'), 'days'),
316 )
317 }
318
319 function getDateRange(cabinetType) {
320 let queryParams = useUrlQueryParams(window.location.href)
321
322 let start = 'start' in queryParams ? queryParams['start'] : null
323 let end = 'end' in queryParams ? queryParams['end'] : null
324
325 if (start && end) {
326 return [moment(start).toDate(), moment(end).toDate()]
327 }
328
329 if (
330 'ameliaBooking' in window &&
331 'cabinet' in window['ameliaBooking'] &&
332 'pastDays' in window['ameliaBooking']['cabinet'] &&
333 'futureDays' in window['ameliaBooking']['cabinet']
334 ) {
335 return [
336 moment().subtract(window['ameliaBooking']['cabinet']['pastDays'], 'days').toDate(),
337 moment().add(window['ameliaBooking']['cabinet']['futureDays'], 'days').toDate(),
338 ]
339 }
340 // if it's customer set date range to numberOfDaysAvailableForBooking
341 if (cabinetType === 'customer') {
342 return [
343 moment().toDate(),
344 moment().add(settings.general.numberOfDaysAvailableForBooking, 'days').toDate(),
345 ]
346 }
347
348 return getDatePickerInitRange()
349 }
350
351 export {
352 weekDaysLocale,
353 weekDaysShortLocale,
354 months,
355 useUtcValue,
356 useLocalValue,
357 useUtcValueOffset,
358 useStringFromDate,
359 useFormatTime,
360 useTimeInSeconds,
361 useSecondsInTime,
362 getFrontedFormattedTime,
363 getFrontedFormattedDate,
364 getFrontedFormattedDateTime,
365 addSeconds,
366 getFirstDayOfWeek,
367 useSecondsToDuration,
368 useConvertedUtcToLocalDateTime,
369 getEventFrontedFormattedDateDay,
370 getEventFrontedFormattedDateMonth,
371 getEventFrontedFormattedTime,
372 getDatePickerInitRange,
373 jsDateFormat,
374 momentDateFormat,
375 setDatePickerSelectedDaysCount,
376 getDateRange,
377 }
378