ngx-favorite-pages
Version:
Stencil Component Starter
321 lines (316 loc) • 21.1 kB
JavaScript
import { p as proxyCustomElement, H, h, c as Host } from './p-eb4e379c.js';
const favoritePagesCss = ":host{display:block;width:100%}.kt-subheader__main{display:flex;justify-content:flex-start;align-items:center;flex-wrap:wrap;padding:0.25rem 0}.kt-subheader__breadcrumbs{display:flex;align-items:center;list-style-type:none;flex-wrap:wrap;margin:0;padding:0}.kt-subheader__breadcrumbs-link{padding:0 0.5rem 0 0;font-size:1rem;font-weight:500;color:#6f6f6f;transition:all 0.3s;text-decoration:none}.kt-subheader__breadcrumbs-link{color:#6f6f6f}.kt-svg-icon.fav-icon{width:17px;height:17px;transform:scaleX(-1)}.kt-svg-icon.inactive{opacity:0.35}.kt-svg-icon g [fill]{fill:#008fc7}.kt-subheader__breadcrumbs-separator:after{display:block;width:1px;height:16px;border-radius:0;content:\" \";background:#e7e8ef}.kt-subheader__breadcrumbs-separator:after{background:#dbdce7}.kt-subheader__breadcrumbs-separator{display:flex;justify-content:content;align-items:center;padding:0 0.5rem 0 0}.kt-subheader__toolbar{display:flex;justify-content:flex-end;align-items:center;padding:0}.btn{display:inline-flex;height:32px !important;padding-top:0;padding-bottom:0;margin-top:0.25rem;margin-bottom:0.25rem}.btn.btn-icon{width:32px !important;align-items:center}.btn:not(.dropdown-toggle-split){margin-left:0.5rem}.btn:not(.dropdown-toggle-split){margin-left:0.25rem}.kt-svg-icon.inactive{opacity:0.35}.kt-svg-icon{height:23px;width:23px}.fav-cont{display:flex;justify-content:space-between;align-items:stretch;flex-wrap:wrap}.kt-subheader__breadcrumbs-item:after{display:block;width:1px;height:16px;border-radius:0;content:\" \";background:#dbdce7;top:3px;right:0}.kt-subheader__breadcrumbs-link{position:relative;display:inline-flex;justify-content:space-between}.kt-subheader__breadcrumbs-item:after{top:3px;position:absolute;right:12px}.kt-subheader__breadcrumbs-link i{font-size:14px}span.kt-subheader__breadcrumbs-link.kt-subheader__breadcrumbs-item{padding-right:20px}input[type=\"text\"]{border:1px solid #e2e5ec;border-radius:4px}.kt-subheader__breadcrumbs-link:hover{-webkit-transition:all 0.3s;transition:all 0.3s;color:#008fc7}.kt-subheader__breadcrumbs-item:last-child:after{display:none}.no-items{font-weight:bold}.toast{position:fixed;top:20px;right:20px;padding:12px 20px;border-radius:5px;color:white;font-size:14px;z-index:9999;animation:fadeInOut 3s ease-in-out;box-shadow:0 2px 10px rgba(0, 0, 0, 0.2)}.toast.success{background-color:#2ecc71}.toast.error{background-color:#e74c3c}@keyframes fadeInOut{0%{opacity:0;transform:translateY(-20%)}10%{opacity:1;transform:translateY(0%)}90%{opacity:1;transform:translateY(0%)}100%{opacity:0;transform:translateY(-20%)}}";
const FavoritePagesStyle0 = favoritePagesCss;
const FavoritePages$1 = /*@__PURE__*/ proxyCustomElement(class FavoritePages extends H {
constructor() {
super();
this.__registerHost();
this.__attachShadow();
}
get host() { return this; }
showModal = false;
slug;
path;
currentRouteSlug;
favorites = []; // Store favorite pages
isFavorite = false; // State to track favorite status
pageId;
pageTitle;
apiUrl;
cultureCode;
applicationId;
isCultureLoaded = false;
selectedTabName;
modalType = 'add'; // To distinguish between add and edit modal
editingPage = null; // Holds page data when editing;
isEditMode = false; // Initialize this variable
isShowEditIcons = false;
authToken;
isFetching = false;
showToastFlag;
toastType;
toastMessage;
// handlePropertyChange() {
// this.fetchInitiateValue();
// }
componentWillLoad() {
this.fetchInitiateValue();
// this.overrideHistoryMethods();
// window.addEventListener('popstate', this.handleRouteChange); // Handles back/forward navigation
}
componentDidLoad() {
// this.fetchInitiateValue();
// if (this.host.shadowRoot) {
// const existingLink = this.host.shadowRoot.querySelector('link[rel="stylesheet"]');
// if (!existingLink) {
// const link = document.createElement('link');
// link.rel = 'stylesheet';
// link.href = 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css';
// this.host.shadowRoot.appendChild(link);
// // console.log('FontAwesome added to shadow DOM');
// } else {
// console.log('FontAwesome already added to shadow DOM');
// }
// } else {
// console.warn('shadowRoot is null');
// }
}
// disconnectedCallback() {
// window.removeEventListener('popstate', this.handleRouteChange);
// }
overrideHistoryMethods() {
const pushState = history.pushState;
const replaceState = history.replaceState;
history.pushState = (...args) => {
pushState.apply(history, args);
this.handleRouteChange(); // Trigger update when `pushState` is called
};
history.replaceState = (...args) => {
replaceState.apply(history, args);
this.handleRouteChange(); // Trigger update when `replaceState` is called
};
}
handleRouteChange = () => {
this.fetchInitiateValue();
};
async fetchInitiateValue() {
if (!this.cultureCode || !this.applicationId || !this.apiUrl) {
this.showToast('Something went wrong', 'error');
return;
}
await this.getFavoritePages();
}
async getFavoritePages() {
try {
const response = await fetch(`${this.apiUrl}/api/Pages/FavoritePages`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'IntegrationPermission': '0f5d983d203189bbffc5f686d01f6680bc6a83718a515fe42639347efc92478e',
'CultureCode': this.cultureCode,
'Source': '2',
'applicationId': this.applicationId,
},
credentials: 'include',
});
if (!response.ok) {
this.showToast('Something went wrong', 'error');
return;
}
if (response.ok) {
const data = await response.json();
this.favorites = data.content;
if (this.favorites.some(fav => fav.pageId === this.pageId)) {
this.isFavorite = true;
}
else {
this.isFavorite = false;
}
}
else {
this.showToast('Something went wrong', 'error');
}
}
catch (error) {
this.showToast('Something went wrong', 'error');
// console.error('Error fetching favorite pages:', error);
}
}
// Add a favorite
async addFavorite() {
const pageTitle = this.pageTitle;
const pageId = this.pageId;
if (!pageId || !this.pageTitle) {
// console.error('Page ID is missing.');
this.showToast('Something went wrong', 'error');
return;
}
if (this.favorites.some(fav => fav.pageId === pageId)) {
// console.log('Page is already in favorites.');
this.isFavorite = true;
return;
}
const requestBody = { name: pageTitle };
try {
const response = await fetch(`${this.apiUrl}/api/Pages/AddFavoritePages/${pageId}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'IntegrationPermission': '0f5d983d203189bbffc5f686d01f6680bc6a83718a515fe42639347efc92478e',
'CultureCode': this.cultureCode,
'Source': '2',
'applicationId': this.applicationId,
},
credentials: 'include',
body: JSON.stringify(requestBody),
});
const data = await response.json();
this.getFavoritePages();
console.log('Page added to favorites successfully:', data);
}
catch (error) {
// console.error('Error adding favorite page:', error);
this.showToast('Something went wrong', 'error');
}
}
showToast(message, type = 'success') {
this.toastMessage = message;
this.toastType = type;
this.showToastFlag = true;
setTimeout(() => {
this.showToastFlag = false;
}, 3000);
}
// Delete a favorite
async deleteFavorite(pageId) {
if (!pageId) {
this.showToast('Something went wrong', 'error');
return;
}
try {
await fetch(`${this.apiUrl}/api/Pages/RemoveFavouritePage/${pageId}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'IntegrationPermission': '0f5d983d203189bbffc5f686d01f6680bc6a83718a515fe42639347efc92478e',
'CultureCode': this.cultureCode,
'Source': '2',
'applicationid': this.applicationId,
},
credentials: 'include',
});
// this.favorites = this.favorites.filter(fav => fav.pageId !== pageId);
this.getFavoritePages();
this.isFavorite = false;
this.isEditMode = false;
// console.log('Favorite page removed successfully.');
}
catch (error) {
// console.error('Error removing favorite page:', error);
this.showToast('Something went wrong', 'error');
}
}
showEditIcons() {
this.isEditMode = true;
this.editingPage = null;
}
editFavorite(fav, index) {
const favorite = this.favorites[index];
this.editingPage = favorite; // Set page for editing
this.modalType = 'edit'; // Set modal type to 'edit'
this.pageId = fav.pageId;
this.isShowEditIcons = true;
}
doneEditing() {
this.saveEditedFavorite();
}
async saveEditedFavorite() {
if (!this.editingPage) {
return;
}
const { pageId, name } = this.editingPage;
const requestBody = { name };
try {
await fetch(`${this.apiUrl}/api/Pages/EditFavouritePage/${pageId}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'IntegrationPermission': '0f5d983d203189bbffc5f686d01f6680bc6a83718a515fe42639347efc92478e',
'CultureCode': this.cultureCode,
'Source': '2',
'applicationId': this.applicationId,
},
credentials: 'include',
body: JSON.stringify(requestBody),
});
this.getFavoritePages();
this.isEditMode = false;
// console.log('Favorite page name updated successfully.');
}
catch (error) {
// console.error('Error updating favorite page:', error);
this.showToast('Something went wrong', 'error');
}
}
get favoriteForCurrentPage() {
return this.favorites.find(fav => fav.pageId === this.pageId);
}
toggleModal() {
this.showModal = !this.showModal;
this.modalType = 'add'; // Reset to 'add' when modal is closed
this.editingPage = null; // Clear any selected page for editing
}
async onDone() {
if (this.modalType === 'edit' && this.editingPage?.name) {
await this.saveEditedFavorite();
}
this.showModal = false;
}
async handleEdit(fav) {
if (this.isEditMode && this.editingPage?.pageId === fav.pageId) {
this.saveEditedFavorite();
}
else {
this.isEditMode = true;
this.editingPage = { ...fav };
}
}
render() {
return (h(Host, { key: 'e092e934ec8d51ce76ce1043c2cdc24e860efeb0' }, h("div", { key: '113e6f9135764ee9a7c8aed76f892c181186cecc', class: "fav-cont" }, h("div", { key: 'ff0f66101ad8e27d3489a5798cbabde7311df78f', class: "kt-subheader__main dina" }, h("div", { key: 'bc91486a9250ede1d3124350253a6c34806ba4c9', class: "kt-subheader__breadcrumbs" }, this.favorites.map(fav => (h("span", { class: "kt-subheader__breadcrumbs-link kt-subheader__breadcrumbs-item" }, h("a", { class: "kt-subheader__breadcrumbs-link" }, this.isEditMode && this.editingPage?.pageId == fav.pageId ? (h("input", { type: "text", value: this.editingPage?.name, onInput: e => (this.editingPage.name = e.target.value) })) : (h("a", { href: fav.url, target: "_self", class: "kt-subheader__breadcrumbs-link" }, fav.name))), this.isEditMode && (h(h.Fragment, null, h("a", { class: "kt-subheader__breadcrumbs-link", title: "Edit", onClick: () => this.handleEdit(fav) }, h("svg", { xmlns: "http://www.w3.org/2000/svg", width: "15px", height: "15px", viewBox: "0 0 24 24", fill: "none" }, h("path", { d: "M18.3282 8.32837L15.8939 5.89405C14.7058 4.706 14.1118 4.11198 13.4268 3.88941C12.8243 3.69364 12.1752 3.69364 11.5727 3.88941C10.8877 4.11198 10.2937 4.706 9.10564 5.89405L7.49975 7.49994M3 20.9997L3.04745 20.6675C3.21536 19.4922 3.29932 18.9045 3.49029 18.3558C3.65975 17.8689 3.89124 17.4059 4.17906 16.9783C4.50341 16.4963 4.92319 16.0765 5.76274 15.237L17.4107 3.58896C18.1918 2.80791 19.4581 2.80791 20.2392 3.58896C21.0202 4.37001 21.0202 5.63634 20.2392 6.41739L8.37744 18.2791C7.61579 19.0408 7.23497 19.4216 6.8012 19.7244C6.41618 19.9932 6.00093 20.2159 5.56398 20.3879C5.07171 20.5817 4.54375 20.6882 3.48793 20.9012L3 20.9997Z", stroke: "#000000", "stroke-width": "2", "stroke-linecap": "round", "stroke-linejoin": "round" }))), h("a", { class: "kt-subheader__breadcrumbs-link", title: "Remove", onClick: () => this.deleteFavorite(fav.pageId) }, h("svg", { xmlns: "http://www.w3.org/2000/svg", width: "15px", height: "15px", viewBox: "0 0 24 24", fill: "none" }, h("path", { d: "M18 6L17.1991 18.0129C17.129 19.065 17.0939 19.5911 16.8667 19.99C16.6666 20.3412 16.3648 20.6235 16.0011 20.7998C15.588 21 15.0607 21 14.0062 21H9.99377C8.93927 21 8.41202 21 7.99889 20.7998C7.63517 20.6235 7.33339 20.3412 7.13332 19.99C6.90607 19.5911 6.871 19.065 6.80086 18.0129L6 6M4 6H20M16 6L15.7294 5.18807C15.4671 4.40125 15.3359 4.00784 15.0927 3.71698C14.8779 3.46013 14.6021 3.26132 14.2905 3.13878C13.9376 3 13.523 3 12.6936 3H11.3064C10.477 3 10.0624 3 9.70951 3.13878C9.39792 3.26132 9.12208 3.46013 8.90729 3.71698C8.66405 4.00784 8.53292 4.40125 8.27064 5.18807L8 6", stroke: "#000000", "stroke-width": "2", "stroke-linecap": "round", "stroke-linejoin": "round" })), ' ')))))))), h("div", { key: 'b679d2af402bceafa38a289be6fc575878286f06', class: "kt-subheader__toolbar" }, h("div", { key: '07e5e6ce7ec9e48383de51379c5ec28304633dc4', class: "kt-subheader__wrapper" }, this.favorites.length === 0 || !this.isFavorite ? (h("a", { class: "btn btn-icon", title: "Add to Favorite", onClick: () => this.addFavorite() }, h("svg", { xmlns: "http://www.w3.org/2000/svg", width: "24px", height: "24px", viewBox: "0 0 24 24", version: "1.1", class: "kt-svg-icon inactive" }, h("g", { stroke: "none", "stroke-width": "1", fill: "none", "fill-rule": "evenodd" }, h("polygon", { points: "0 0 24 0 24 24 0 24" }), h("path", { d: "M12,18 L7.91561963,20.1472858 C7.42677504,20.4042866 6.82214789,20.2163401 6.56514708,19.7274955 C6.46280801,19.5328351 6.42749334,19.309867 6.46467018,19.0931094 L7.24471742,14.545085 L3.94038429,11.3241562 C3.54490071,10.938655 3.5368084,10.3055417 3.92230962,9.91005817 C4.07581822,9.75257453 4.27696063,9.65008735 4.49459766,9.61846284 L9.06107374,8.95491503 L11.1032639,4.81698575 C11.3476862,4.32173209 11.9473121,4.11839309 12.4425657,4.36281539 C12.6397783,4.46014562 12.7994058,4.61977315 12.8967361,4.81698575 L14.9389263,8.95491503 L19.5054023,9.61846284 C20.0519472,9.69788046 20.4306287,10.2053233 20.351211,10.7518682 C20.3195865,10.9695052 20.2170993,11.1706476 20.0596157,11.3241562 L16.7552826,14.545085 L17.5353298,19.0931094 C17.6286908,19.6374458 17.263103,20.1544017 16.7187666,20.2477627 C16.5020089,20.2849396 16.2790408,20.2496249 16.0843804,20.1472858 L12,18 Z", fill: "#000000" }))))) : (h("a", { class: "btn btn-icon", title: "Favorite", onClick: () => this.addFavorite() }, h("svg", { xmlns: "http://www.w3.org/2000/svg", width: "24px", height: "24px", viewBox: "0 0 24 24", version: "1.1", class: "kt-svg-icon" }, h("g", { stroke: "none", "stroke-width": "1", fill: "none", "fill-rule": "evenodd" }, h("polygon", { points: "0 0 24 0 24 24 0 24" }), h("path", { d: "M12,18 L7.91561963,20.1472858 C7.42677504,20.4042866 6.82214789,20.2163401 6.56514708,19.7274955 C6.46280801,19.5328351 6.42749334,19.309867 6.46467018,19.0931094 L7.24471742,14.545085 L3.94038429,11.3241562 C3.54490071,10.938655 3.5368084,10.3055417 3.92230962,9.91005817 C4.07581822,9.75257453 4.27696063,9.65008735 4.49459766,9.61846284 L9.06107374,8.95491503 L11.1032639,4.81698575 C11.3476862,4.32173209 11.9473121,4.11839309 12.4425657,4.36281539 C12.6397783,4.46014562 12.7994058,4.61977315 12.8967361,4.81698575 L14.9389263,8.95491503 L19.5054023,9.61846284 C20.0519472,9.69788046 20.4306287,10.2053233 20.351211,10.7518682 C20.3195865,10.9695052 20.2170993,11.1706476 20.0596157,11.3241562 L16.7552826,14.545085 L17.5353298,19.0931094 C17.6286908,19.6374458 17.263103,20.1544017 16.7187666,20.2477627 C16.5020089,20.2849396 16.2790408,20.2496249 16.0843804,20.1472858 L12,18 Z", fill: "#000000" }))))), this.favorites.length > 0 && (h("a", { key: '2b91b9e37ea65668cb00025acae7f502c7398e21', class: "btn btn-icon", title: "Edit", onClick: () => this.showEditIcons() }, h("svg", { key: '64ad840c066acd0e20dff146523091966b4dbfc6', xmlns: "http://www.w3.org/2000/svg", width: "24px", height: "24px", viewBox: "0 0 24 24", version: "1.1", class: "kt-svg-icon" }, h("g", { key: '0b52263f9c5436ce481b88fbbffc2ce160299760', stroke: "none", "stroke-width": "1", fill: "none", "fill-rule": "evenodd" }, h("rect", { key: '53adc3b8b815fa509bbdd07d498815cb779e22bb', x: "0", y: "0", width: "24", height: "24" }), h("path", { key: '5848dfab604c126efcb01cc7c2cd2933795ff3e0', d: "M8,17.9148182 L8,5.96685884 C8,5.56391781 8.16211443,5.17792052 8.44982609,4.89581508 L10.965708,2.42895648 C11.5426798,1.86322723 12.4640974,1.85620921 13.0496196,2.41308426 L15.5337377,4.77566479 C15.8314604,5.0588212 16,5.45170806 16,5.86258077 L16,17.9148182 C16,18.7432453 15.3284271,19.4148182 14.5,19.4148182 L9.5,19.4148182 C8.67157288,19.4148182 8,18.7432453 8,17.9148182 Z", fill: "#000000", "fill-rule": "nonzero", transform: "translate(12.000000, 10.707409) rotate(-135.000000) translate(-12.000000, -10.707409) " }), h("rect", { key: '309972499ee9e7b0d47f7fb6390f93d7262da48e', fill: "#000000", opacity: "0.3", x: "5", y: "20", width: "15", height: "2", rx: "1" }))))), this.showToastFlag && h("div", { key: '030b9ddf10a7be9b87c024867988d8f7ae820a23', class: `toast ${this.toastType}` }, this.toastMessage))))));
}
static get watchers() { return {
"cultureCode": ["componentWillLoad"],
"applicationId": ["componentWillLoad"],
"pageId": ["componentWillLoad"],
"apiUrl": ["componentWillLoad"],
"pageTitle": ["componentWillLoad"]
}; }
static get style() { return FavoritePagesStyle0; }
}, [1, "favorite-pages", {
"pageId": [8, "page-id"],
"pageTitle": [8, "page-title"],
"apiUrl": [1, "api-url"],
"cultureCode": [1, "culture-code"],
"applicationId": [1, "application-id"],
"showModal": [32],
"slug": [32],
"path": [32],
"currentRouteSlug": [32],
"favorites": [32],
"isFavorite": [32],
"isCultureLoaded": [32],
"selectedTabName": [32],
"modalType": [32],
"editingPage": [32],
"isEditMode": [32],
"isShowEditIcons": [32],
"authToken": [32],
"isFetching": [32],
"showToastFlag": [32],
"toastType": [32],
"toastMessage": [32]
}, undefined, {
"cultureCode": ["componentWillLoad"],
"applicationId": ["componentWillLoad"],
"pageId": ["componentWillLoad"],
"apiUrl": ["componentWillLoad"],
"pageTitle": ["componentWillLoad"]
}]);
function defineCustomElement$1() {
if (typeof customElements === "undefined") {
return;
}
const components = ["favorite-pages"];
components.forEach(tagName => { switch (tagName) {
case "favorite-pages":
if (!customElements.get(tagName)) {
customElements.define(tagName, FavoritePages$1);
}
break;
} });
}
defineCustomElement$1();
const FavoritePages = FavoritePages$1;
const defineCustomElement = defineCustomElement$1;
export { FavoritePages, defineCustomElement };
//# sourceMappingURL=favorite-pages.js.map