UNPKG

@salla.sa/twilight-components

Version:
229 lines (222 loc) 12.3 kB
/*! * Crafted with ❤ by Salla */ import { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client'; import { d as defineCustomElement$3 } from './salla-button2.js'; import { d as defineCustomElement$2 } from './salla-loading2.js'; import { d as defineCustomElement$1 } from './salla-modal2.js'; const BookingTime = `<!-- Generated by IcoMoon.io --> <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32"> <title>calendar-time</title> <path d="M22.667 17.333c-0.737 0-1.333 0.596-1.333 1.333v2.667h-2.667c-0.737 0-1.333 0.596-1.333 1.333s0.596 1.333 1.333 1.333h4c0.737 0 1.333-0.596 1.333-1.333v-4c0-0.737-0.596-1.333-1.333-1.333zM28 2.667h-2.667v-1.333c0-0.736-0.596-1.333-1.333-1.333s-1.333 0.597-1.333 1.333v1.333h-13.333v-1.333c0-0.736-0.596-1.333-1.333-1.333s-1.333 0.597-1.333 1.333v1.333h-2.667c-2.205 0-4 1.795-4 4v21.333c0 2.205 1.795 4 4 4h5.363c0.737 0 1.333-0.596 1.333-1.333s-0.596-1.333-1.333-1.333h-5.363c-0.736 0-1.333-0.597-1.333-1.333v-21.333c0-0.736 0.597-1.333 1.333-1.333h2.667v2.667c0 0.736 0.596 1.333 1.333 1.333s1.333-0.597 1.333-1.333v-2.667h13.333v2.667c0 0.736 0.596 1.333 1.333 1.333s1.333-0.597 1.333-1.333v-2.667h2.667c0.736 0 1.333 0.599 1.333 1.333v2.696c0 0.736 0.596 1.333 1.333 1.333s1.333-0.597 1.333-1.333v-2.696c0-2.205-1.795-4-4-4zM22 12c-5.515 0-10 4.485-10 10s4.485 10 10 10 10-4.485 10-10-4.485-10-10-10zM22 29.333c-4.043 0-7.333-3.291-7.333-7.333s3.291-7.333 7.333-7.333 7.333 3.291 7.333 7.333-3.291 7.333-7.333 7.333z"></path> </svg> `; const Calendar = `<!-- Generated by IcoMoon.io --> <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32"> <title>calendar</title> <path d="M28 2.667h-2.667v-1.333c0-0.736-0.597-1.333-1.333-1.333s-1.333 0.597-1.333 1.333v1.333h-13.333v-1.333c0-0.736-0.597-1.333-1.333-1.333s-1.333 0.597-1.333 1.333v1.333h-2.667c-2.205 0-4 1.795-4 4v21.333c0 2.205 1.795 4 4 4h24c2.205 0 4-1.795 4-4v-21.333c0-2.205-1.795-4-4-4zM29.333 28c0 0.736-0.599 1.333-1.333 1.333h-24c-0.736 0-1.333-0.599-1.333-1.333v-13.333h26.667zM29.333 12h-26.667v-5.333c0-0.736 0.599-1.333 1.333-1.333h2.667v2.667c0 0.736 0.597 1.333 1.333 1.333s1.333-0.597 1.333-1.333v-2.667h13.333v2.667c0 0.736 0.597 1.333 1.333 1.333s1.333-0.597 1.333-1.333v-2.667h2.667c0.736 0 1.333 0.599 1.333 1.333z"></path> </svg> `; const TimeIcon = `<!-- Generated by IcoMoon.io --> <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32"> <title>time</title> <path d="M16 0c-8.823 0-16 7.177-16 16s7.177 16 16 16 16-7.177 16-16-7.177-16-16-16zM16 29.333c-7.352 0-13.333-5.981-13.333-13.333s5.981-13.333 13.333-13.333 13.333 5.981 13.333 13.333-5.981 13.333-13.333 13.333zM16 8c-0.736 0-1.333 0.596-1.333 1.333v6.667h-4c-0.736 0-1.333 0.596-1.333 1.333s0.597 1.333 1.333 1.333h5.333c0.736 0 1.333-0.596 1.333-1.333v-8c0-0.737-0.597-1.333-1.333-1.333z"></path> </svg> `; const sallaBookingFieldCss = ":host{display:block}"; const SallaBookingField = /*@__PURE__*/ proxyCustomElement(class SallaBookingField extends HTMLElement { constructor() { super(); this.__registerHost(); this.invalidInput = createEvent(this, "invalidInput", 7); this.option = undefined; this.productId = undefined; this.bookNowLabel = salla.lang.get('pages.cart.book_an_appointment', 'حجز موعد'); this.editLabel = salla.lang.get('pages.cart.edit_an_appointment', 'تعديل الموعد'); this.bookedLabel = salla.lang.get('pages.cart.booked_successfully', 'تمت اضافة الموعد بنجاح'); this.selectDate = salla.lang.get('pages.cart.select_appointment_date', 'حدد تاريخ الموعد'); this.bookingUrl = ''; this.iframeReady = false; this.reservations = []; this.reservationsInput = undefined; // Load translations salla.lang.onLoaded(() => this.setTranslations()); // Register event listeners Salla.event.on('booking::open', (data) => this.handleBookingOpen(data)); } async setTranslations() { const setNestedAsync = (lang, key, value) => { return new Promise((resolve) => { salla.helpers.setNested(salla.lang.messages[lang], key, value); resolve(true); }); }; await setNestedAsync('ar.trans', 'pages.cart.book_an_appointment', 'حجز موعد'); await setNestedAsync('en.trans', 'pages.cart.book_an_appointment', 'Book an Appointment'); await setNestedAsync('ar.trans', 'pages.cart.edit_an_appointment', 'تعديل الموعد'); await setNestedAsync('en.trans', 'pages.cart.edit_an_appointment', 'Edit an Appointment'); await setNestedAsync('ar.trans', 'pages.cart.booked_successfully', 'تمت اضافة الموعد بنجاح'); await setNestedAsync('en.trans', 'pages.cart.booked_successfully', 'Booked Successfully'); await setNestedAsync('ar.trans', 'pages.cart.select_appointment_date', 'حدد تاريخ الموعد'); await setNestedAsync('en.trans', 'pages.cart.select_appointment_date', 'Select appointment date'); this.bookNowLabel = salla.lang.get('pages.cart.book_an_appointment'); this.editLabel = salla.lang.get('pages.cart.edit_an_appointment'); this.bookedLabel = salla.lang.get('pages.cart.booked_successfully'); this.selectDate = salla.lang.get('pages.cart.select_appointment_date'); } openBookingModal(event, afterReload = false) { if (afterReload && (!event.detail || typeof event.detail !== 'number' || event.detail !== this.productId)) { return; } if (salla.config.isGuest()) { this.setAfterReloadEvent('booking::open-after-reload', this.productId); salla.event.dispatch('login::open'); return; } salla.booking.add(this.productId, false) .then((resp) => { if (resp.data.redirect.to !== 'booking') { throw new Error('Unexpected redirect!'); } salla.event.dispatch('booking::open', { url: resp.data.redirect.url, id: this.productId }); }) .catch((error) => { salla.error(salla.lang.get('common.errors.error_occurred')); salla.logger.error(error.response || error); }); } handleBookingOpen(data) { if (data.id !== this.productId) return; this.bookingUrl = salla.url.addParamToUrl('product_id', data.id, data.url); this.iframeReady = true; setTimeout(() => { this.modal.setTitle(this.selectDate); this.modal.open(); }, 100); } setAfterReloadEvent(event, payload) { salla.storage.set('afterReloadEvent', { event, payload }); } emitAfterReloadEvent() { const eventDetails = salla.storage.get('afterReloadEvent'); if (eventDetails && eventDetails.event) { const customEvent = new CustomEvent(eventDetails.event, { detail: eventDetails.payload }); window.dispatchEvent(customEvent); salla.storage.remove('afterReloadEvent'); } } componentWillLoad() { if (this.option && this.option.details.length) { this.reservations = this.option.details; } } componentDidLoad() { window.addEventListener('booking::open-after-reload', (event) => this.openBookingModal(event, true)); this.emitAfterReloadEvent(); window.addEventListener('message', this.handleMessageEvent.bind(this)); this.reservationsInput.addEventListener('invalid', e => this.invalidInput.emit(e)); this.reservationsInput.addEventListener('input', () => { this.reservationsInput.setCustomValidity(''); this.reservationsInput.reportValidity(); }); } handleMessageEvent(event) { var _a, _b; if (event.data.source !== 'booking') return; const action = event.data.type; const value = event.data.message; if (localStorage.getItem('debug')) console.log(`Received an action:${action}`, event.data); if (action === 'error') { if ((_a = value.fields) === null || _a === void 0 ? void 0 : _a.reservation) { salla.notify.error(value.fields.reservation[0]); return; } const errorList = Object.values(value.fields).flat().map(error => `<li>${error}</li>`).join(''); salla.notify.error(`<ul>${errorList}</ul>`); } if (action === 'success') { if (Number(value.productId) !== Number(this.productId)) return; this.reservations = value.data.reservations.map(schedule => { if (schedule.time && schedule.time.length > 0) { const timeSlot = schedule.time[0]; return { date: schedule.date, day: schedule.day, from_timestamp: timeSlot.from, to_timestamp: timeSlot.to, }; } return null; }).filter(item => item !== null); salla.notify.success(this.bookedLabel); (_b = this.modal) === null || _b === void 0 ? void 0 : _b.close(); setTimeout(() => window.location.reload()); } if (action === 'height') { this.iframe.height = (value === null || value === void 0 ? void 0 : value.height) + 'px'; } } bookingModal() { return (h("salla-modal", { class: "s-booking-field-modal", ref: modal => (this.modal = modal), width: "md", position: "middle", noPadding: true }, h("iframe", { ref: iframe => (this.iframe = iframe), src: this.bookingUrl, frameborder: "0" }))); } renderReservationDate(reservation) { return (h("span", { class: reservation.from_timestamp ? 's-booking-field-reservations-has-time' : '' }, h("i", { class: "s-booking-field-reservations-icon", innerHTML: Calendar }), reservation.date)); } renderReservationTime(reservation) { if (!reservation.from_timestamp) return ''; return (h("span", { class: "s-booking-field-reservations-time" }, h("i", { class: "s-booking-field-reservations-icon", innerHTML: TimeIcon }), h("span", null, reservation.from_timestamp, " - ", reservation.to_timestamp))); } render() { return (h(Host, null, h("div", { class: "s-booking-field-main" }, this.option.required || this.reservations.length > 0 ? h("div", { class: "s-booking-field-price" }, h("span", null, salla.money(this.option.price))) : '', h("salla-button", { class: "s-booking-field-book-now", size: "small", loaderPosition: "center", fill: "outline", onClick: event => this.openBookingModal(event, false) }, h("span", { class: "s-booking-field-book-now-content" }, h("span", { innerHTML: BookingTime }), this.reservations.length ? this.editLabel : this.bookNowLabel))), this.reservations.length > 0 && (h("div", { class: "s-booking-field-reservations" }, this.reservations.map((reservation, index) => (h("div", { key: index, class: "s-booking-field-reservations-item" }, this.renderReservationDate(reservation), this.renderReservationTime(reservation)))))), h("input", { class: "s-hidden", name: this.option.name, required: this.option.required, value: JSON.stringify(this.reservations) === '[]' ? '' : JSON.stringify(this.reservations), ref: reservations => this.reservationsInput = reservations }), this.iframeReady && this.bookingModal())); } get host() { return this; } static get style() { return sallaBookingFieldCss; } }, [0, "salla-booking-field", { "option": [16], "productId": [2, "product-id"], "bookNowLabel": [32], "editLabel": [32], "bookedLabel": [32], "selectDate": [32], "bookingUrl": [32], "iframeReady": [32], "reservations": [32], "reservationsInput": [32] }]); function defineCustomElement() { if (typeof customElements === "undefined") { return; } const components = ["salla-booking-field", "salla-button", "salla-loading", "salla-modal"]; components.forEach(tagName => { switch (tagName) { case "salla-booking-field": if (!customElements.get(tagName)) { customElements.define(tagName, SallaBookingField); } break; case "salla-button": if (!customElements.get(tagName)) { defineCustomElement$3(); } break; case "salla-loading": if (!customElements.get(tagName)) { defineCustomElement$2(); } break; case "salla-modal": if (!customElements.get(tagName)) { defineCustomElement$1(); } break; } }); } export { SallaBookingField as S, defineCustomElement as d }; //# sourceMappingURL=salla-booking-field2.js.map