@adobe/aem-spa-page-model-manager
Advanced tools
Comparing version 1.3.11 to 1.3.12
@@ -0,1 +1,8 @@ | ||
## [1.3.12](https://github.com/adobe/aem-spa-page-model-manager/compare/v1.3.11...v1.3.12) (2021-07-14) | ||
### Bug Fixes | ||
* fix html extension regex ([ee2f71a](https://github.com/adobe/aem-spa-page-model-manager/commit/ee2f71aefc5328685e85e607712cf2a5491370da)), closes [#64](https://github.com/adobe/aem-spa-page-model-manager/issues/64) | ||
## [1.3.11](https://github.com/adobe/aem-spa-page-model-manager/compare/v1.3.10...v1.3.11) (2021-04-15) | ||
@@ -2,0 +9,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.aemSpaPageModelManager=e():t.aemSpaPageModelManager=e()}(function(){try{return"undefined"!=typeof self}catch(t){return!1}}()?self:this,(function(){return(()=>{"use strict";var t={470:t=>{function e(t){if("string"!=typeof t)throw new TypeError("Path must be a string. Received "+JSON.stringify(t))}function r(t,e){for(var r,i="",n=0,o=-1,s=0,a=0;a<=t.length;++a){if(a<t.length)r=t.charCodeAt(a);else{if(47===r)break;r=47}if(47===r){if(o===a-1||1===s);else if(o!==a-1&&2===s){if(i.length<2||2!==n||46!==i.charCodeAt(i.length-1)||46!==i.charCodeAt(i.length-2))if(i.length>2){var l=i.lastIndexOf("/");if(l!==i.length-1){-1===l?(i="",n=0):n=(i=i.slice(0,l)).length-1-i.lastIndexOf("/"),o=a,s=0;continue}}else if(2===i.length||1===i.length){i="",n=0,o=a,s=0;continue}e&&(i.length>0?i+="/..":i="..",n=2)}else i.length>0?i+="/"+t.slice(o+1,a):i=t.slice(o+1,a),n=a-o-1;o=a,s=0}else 46===r&&-1!==s?++s:s=-1}return i}var i={resolve:function(){for(var t,i="",n=!1,o=arguments.length-1;o>=-1&&!n;o--){var s;o>=0?s=arguments[o]:(void 0===t&&(t=process.cwd()),s=t),e(s),0!==s.length&&(i=s+"/"+i,n=47===s.charCodeAt(0))}return i=r(i,!n),n?i.length>0?"/"+i:"/":i.length>0?i:"."},normalize:function(t){if(e(t),0===t.length)return".";var i=47===t.charCodeAt(0),n=47===t.charCodeAt(t.length-1);return 0!==(t=r(t,!i)).length||i||(t="."),t.length>0&&n&&(t+="/"),i?"/"+t:t},isAbsolute:function(t){return e(t),t.length>0&&47===t.charCodeAt(0)},join:function(){if(0===arguments.length)return".";for(var t,r=0;r<arguments.length;++r){var n=arguments[r];e(n),n.length>0&&(void 0===t?t=n:t+="/"+n)}return void 0===t?".":i.normalize(t)},relative:function(t,r){if(e(t),e(r),t===r)return"";if((t=i.resolve(t))===(r=i.resolve(r)))return"";for(var n=1;n<t.length&&47===t.charCodeAt(n);++n);for(var o=t.length,s=o-n,a=1;a<r.length&&47===r.charCodeAt(a);++a);for(var l=r.length-a,h=s<l?s:l,c=-1,d=0;d<=h;++d){if(d===h){if(l>h){if(47===r.charCodeAt(a+d))return r.slice(a+d+1);if(0===d)return r.slice(a+d)}else s>h&&(47===t.charCodeAt(n+d)?c=d:0===d&&(c=0));break}var u=t.charCodeAt(n+d);if(u!==r.charCodeAt(a+d))break;47===u&&(c=d)}var p="";for(d=n+c+1;d<=o;++d)d!==o&&47!==t.charCodeAt(d)||(0===p.length?p+="..":p+="/..");return p.length>0?p+r.slice(a+c):(a+=c,47===r.charCodeAt(a)&&++a,r.slice(a))},_makeLong:function(t){return t},dirname:function(t){if(e(t),0===t.length)return".";for(var r=t.charCodeAt(0),i=47===r,n=-1,o=!0,s=t.length-1;s>=1;--s)if(47===(r=t.charCodeAt(s))){if(!o){n=s;break}}else o=!1;return-1===n?i?"/":".":i&&1===n?"//":t.slice(0,n)},basename:function(t,r){if(void 0!==r&&"string"!=typeof r)throw new TypeError('"ext" argument must be a string');e(t);var i,n=0,o=-1,s=!0;if(void 0!==r&&r.length>0&&r.length<=t.length){if(r.length===t.length&&r===t)return"";var a=r.length-1,l=-1;for(i=t.length-1;i>=0;--i){var h=t.charCodeAt(i);if(47===h){if(!s){n=i+1;break}}else-1===l&&(s=!1,l=i+1),a>=0&&(h===r.charCodeAt(a)?-1==--a&&(o=i):(a=-1,o=l))}return n===o?o=l:-1===o&&(o=t.length),t.slice(n,o)}for(i=t.length-1;i>=0;--i)if(47===t.charCodeAt(i)){if(!s){n=i+1;break}}else-1===o&&(s=!1,o=i+1);return-1===o?"":t.slice(n,o)},extname:function(t){e(t);for(var r=-1,i=0,n=-1,o=!0,s=0,a=t.length-1;a>=0;--a){var l=t.charCodeAt(a);if(47!==l)-1===n&&(o=!1,n=a+1),46===l?-1===r?r=a:1!==s&&(s=1):-1!==r&&(s=-1);else if(!o){i=a+1;break}}return-1===r||-1===n||0===s||1===s&&r===n-1&&r===i+1?"":t.slice(r,n)},format:function(t){if(null===t||"object"!=typeof t)throw new TypeError('The "pathObject" argument must be of type Object. Received type '+typeof t);return function(t,e){var r=e.dir||e.root,i=e.base||(e.name||"")+(e.ext||"");return r?r===e.root?r+i:r+"/"+i:i}(0,t)},parse:function(t){e(t);var r={root:"",dir:"",base:"",ext:"",name:""};if(0===t.length)return r;var i,n=t.charCodeAt(0),o=47===n;o?(r.root="/",i=1):i=0;for(var s=-1,a=0,l=-1,h=!0,c=t.length-1,d=0;c>=i;--c)if(47!==(n=t.charCodeAt(c)))-1===l&&(h=!1,l=c+1),46===n?-1===s?s=c:1!==d&&(d=1):-1!==s&&(d=-1);else if(!h){a=c+1;break}return-1===s||-1===l||0===d||1===d&&s===l-1&&s===a+1?-1!==l&&(r.base=r.name=0===a&&o?t.slice(1,l):t.slice(a,l)):(0===a&&o?(r.name=t.slice(1,s),r.base=t.slice(1,l)):(r.name=t.slice(a,s),r.base=t.slice(a,l)),r.ext=t.slice(s,l)),a>0?r.dir=t.slice(0,a-1):o&&(r.dir="/"),r},sep:"/",delimiter:":",win32:null,posix:null};i.posix=i,t.exports=i},873:(t,e,r)=>{r.r(e),r.d(e,{AEM_MODE:()=>l,AuthoringUtils:()=>y,Constants:()=>c,ModelClient:()=>R,ModelManager:()=>A,PathUtils:()=>C});class i{constructor(){}}i.PAGE_MODEL_INIT="cq-pagemodel-init",i.PAGE_MODEL_LOADED="cq-pagemodel-loaded",i.PAGE_MODEL_UPDATE="cq-pagemodel-update",i.PAGE_MODEL_ROUTE_CHANGED="cq-pagemodel-route-changed";const n=i;class o{constructor(){}}o.PAGE_MODEL_ROOT_URL="cq:pagemodel_root_url",o.PAGE_MODEL_ROUTE_FILTERS="cq:pagemodel_route_filters",o.PAGE_MODEL_ROUTER="cq:pagemodel_router",o.WCM_MODE="cq:wcmmode",o.WCM_DATA_TYPE="cq:datatype";const s=o;class a{constructor(){}}var l,h;a.TYPE_PROP=":type",a.ITEMS_PROP=":items",a.ITEMS_ORDER_PROP=":itemsOrder",a.PATH_PROP=":path",a.CHILDREN_PROP=":children",a.HIERARCHY_TYPE_PROP=":hierarchyType",a.JCR_CONTENT="jcr:content",function(t){t.EDIT="edit",t.PREVIEW="preview",t.DISABLED="disabled"}(l||(l={})),function(t){t.JS="script",t.STYLESHEET="stylesheet"}(h||(h={}));const c=a,d=require("clone");var u=r.n(d),p=r(470);const f=require("url");var g=r.n(f);class _{constructor(){}}_.DEFAULT_SLING_MODEL_SELECTOR="model",_.DEFAULT_MODEL_JSON_EXTENSION=`.${_.DEFAULT_SLING_MODEL_SELECTOR}.json`;const m=_,P=/(?:\/)(?:content|apps|libs|etc|etc.clientlibs|conf|mnt\/overlay)(?:\/)/,E=`(.+)/${c.JCR_CONTENT}/(.+)`,M="http://dummy";class C{static isBrowser(){return"undefined"!=typeof window}static getContextPath(t){const e=t||this.getCurrentPathname();if(!e)return"";const r=e.match(P),i=null===r?-1:r.index||-1;return i>0?e.slice(0,i):""}static adaptPagePath(t,e){if(!t)return"";const r=C.internalize(t);return e&&r===C.sanitize(e)?"":r}static externalize(t){const e=this.getContextPath();return t.startsWith(e)?t:`${e}${t}`}static internalize(t){if(!t||"string"!=typeof t)return"";const e=this.getContextPath();return t.replace(new RegExp(`^${e}/`),"/")}static getMetaPropertyValue(t){let e=null;if(this.isBrowser()){const r=document.head.querySelector(`meta[property="${t}"]`);e=r?r.getAttribute("content"):null}return e}static convertToModelUrl(t){return t&&t.replace&&t.replace(/\.htm(l)?$/,m.DEFAULT_MODEL_JSON_EXTENSION)}static getCurrentPageModelUrl(){const t=this.getCurrentPathname();let e=null;return t&&(e=this.convertToModelUrl(t)||null),e}static getModelUrl(t){if(t&&t.replace)return this.convertToModelUrl(t);return this.getMetaPropertyValue(s.PAGE_MODEL_ROOT_URL)||this.getCurrentPageModelUrl()}static sanitize(t){if(!t||"string"!=typeof t)return null;let e=g().parse(t,!1,!0).pathname;if(e){e=this.internalize(e);const t=e.indexOf(".");t>-1&&(e=e.substr(0,t)),e=(0,p.normalize)(e)}return e}static addExtension(t,e){if(!e||e.length<1)return t;if(e.startsWith(".")||(e="."+e),!t||t.length<1||t.indexOf(e)>-1)return t;let r=this.normalize(t);const i=new URL(r,M);let n=this.sanitize(i.pathname);n=i.origin===M?n:i.origin+n;let o=this._extractPathWithoutResource(i.pathname);return o=this._replaceExtension(o,e),r=(n+"."+o+i.search).replace(/\.\./g,"."),r}static _extractPathWithoutResource(t){const e=t.split(".");return e.shift(),e.join(".")}static _replaceExtension(t,e){if(t.length<1)return e;const r=t.split("/"),i=r[0].split(".");let n=i.pop();n=n?n.replace(/\.htm(l)?/,""):"";let o=i.join(".")+"."+n+e;return r.shift(),r.length>0&&(o+=r.join("/")),o}static addSelector(t,e){if(!e||e.length<1)return t;if(e.startsWith(".")||(e="."+e),!t||t.length<1||t.indexOf(e)>-1)return t;const r=t.indexOf(".")||t.length;return r<0?t+e:t.slice(0,r)+e+t.slice(r,t.length)}static getCurrentPathname(){return this.isBrowser()?window.location.pathname:null}static getCurrentURL(){return this.isBrowser()?window.location.href:""}static dispatchGlobalCustomEvent(t,e){this.isBrowser()&&window.dispatchEvent(new CustomEvent(t,e))}static join(t){return t?this.normalize(t.filter((t=>t)).join("/")):""}static normalize(t){return t?t.replace(/\/+/g,"/"):""}static makeAbsolute(t){return t&&"string"==typeof t?t.startsWith("/")?t:"/"+t:""}static makeRelative(t){return t&&"string"==typeof t?t.startsWith("/")?t.slice(1):t:""}static getParentNodePath(t){if(t&&t.length>0){const e=t.lastIndexOf("/")+1;if(e<t.length)return t.substring(0,e-1)}return null}static isItem(t){return new RegExp(E).test(t)}static getNodeName(t){return("string"==typeof t?t.replace(/\/+/g,"/").split(/\//).filter(Boolean):[]).pop()||null}static subpath(t,e){if(!t)return"";const r=C.makeRelative(t).split("/"),i=C.makeRelative(e).split("/");if(r.length<i.length)return t;let n;for(n=0;n<i.length&&r[n]===i[n];++n);return n===i.length?r.slice(n).join("/"):t}static splitByDelimitators(t,e){let r=[t];return e.forEach((t=>{let e=[];const i=C.normalize(C.makeAbsolute(t)+"/");r.forEach((r=>{if(e=e.concat(r.split(i)),r.endsWith(t)){const r=e.splice(e.length-1,1)[0];r!==t&&(e=e.concat(r.split(C.makeAbsolute(t))))}e=e.filter((t=>t))})),r=e})),r}static _getJCRPath(t,e){return[t,c.JCR_CONTENT,e].join("/")}static splitPageContentPaths(t){if(!t&&"string"!=typeof t)return;const e=t.split(`/${c.JCR_CONTENT}/`),r={pagePath:e[0]};return e.length>1&&(r.itemPath=e[1]),r}static trimStrings(t,e){return e.forEach((e=>{for(;t.startsWith(e);)t=C.makeRelative(t.slice(e.length));for(;t.endsWith(e);)(t=t.slice(0,t.length-e.length)).endsWith("/")&&(t=t.slice(0,t.length-1))})),t}static _getStartStrings(t,e){let r="";return e.forEach((e=>{for(;t.startsWith(e);)t=C.makeRelative(t.slice(e.length)),r=`${r}/${e}`})),C.makeRelative(r)}static toAEMPath(t,e,r){if(window.location.origin===e){const e=`(/editor.html)?(/content/${r=r.replace(/^\/|\/$/g,"")})?`;if(t.indexOf(e)<0)return`${e}${t}(.html)?`}return t}}function O(t){C.dispatchGlobalCustomEvent(n.PAGE_MODEL_LOADED,{detail:{model:u()(t)}})}class D{constructor(t){this._modelManager=t,this._windowListener=t=>{t&&t.detail&&t.detail.msg?this._updateModel(t.detail.msg):console.error("EditorService.js","No message passed to cq-pagemodel-update",t)},C.isBrowser()&&window.addEventListener(n.PAGE_MODEL_UPDATE,this._windowListener)}_updateModel(t){if(!t||!t.cmd||!t.path)return void console.error("PageModelManager.js","Not enough data received to update the page model");const e=t.path,r=t.cmd,i=u()(t.data);let n,o,s;const a=C.getParentNodePath(e);switch(r){case"replace":this._modelManager.modelStore.setData(e,i),this._modelManager._notifyListeners(e);break;case"delete":this._modelManager.modelStore.removeData(e),a&&this._modelManager._notifyListeners(a);break;case"insertBefore":s=!0;case"insertAfter":n=C.getNodeName(e),a&&(o=a+"/"+i.key,this._modelManager.modelStore.insertData(o,i.value,n,s),this._modelManager._notifyListeners(a));break;default:console.log("EditorClient","unsupported command:",r)}O(this._modelManager.modelStore.dataMap)}destroy(){C.isBrowser()&&window.removeEventListener(n.PAGE_MODEL_UPDATE,this._windowListener)}}class R{constructor(t){this._apiHost=t||null}get apiHost(){return this._apiHost}fetch(t){if(!t){const e="Fetching model rejected for path: "+t;return Promise.reject(new Error(e))}const e=this._apiHost||"";return fetch(`${e}${t}`,{credentials:"same-origin"}).then((t=>{if(t.status>=200&&t.status<300)return t.json();throw{response:t}})).catch((t=>Promise.reject(t)))}destroy(){this._apiHost=null}}class v{constructor(t,e){this._data=null,this._rootPath=null,this._data={},t&&this.initialize(t,e||{}),this._pageContentDelimiter=[c.JCR_CONTENT]}initialize(t,e){e&&(this._data=e),this._rootPath=t}get rootPath(){return this._rootPath||""}get dataMap(){return this._data}setData(t,e={}){const r=C.getNodeName(t);if(r){const i=this.getData(C.getParentNodePath(t),!1);if(i&&i[c.ITEMS_PROP]){const t=u()(e),n=i[c.ITEMS_PROP]||{};n[r]&&Object.keys(n[r]).forEach((e=>t.value[e]=t.value[e]||"")),n[r]=t.value,i[c.ITEMS_PROP]=n}}}getData(t,e=!0){if(!t&&"string"!=typeof t)return e?u()(this._data):this._data;if(t===this._rootPath||t===`${this._rootPath}/${c.JCR_CONTENT}`)return e?u()(this._data):this._data;const r=C.splitPageContentPaths(t);if(r){const t=this._getPageData(r.pagePath);if(!t||!r.itemPath)return e?u()(t):t;const i=this._findItemData(r.itemPath,t);if(i)return e?u()(i.data):i.data}}insertData(t,e,r,i=!1){if(e=u()(e),!t)return void console.warn("No path provided for data: "+e);if(!C.isItem(t)&&this._data)return this._data[c.CHILDREN_PROP]||(this._data[c.CHILDREN_PROP]={}),void(this._data[c.CHILDREN_PROP][t]=e);const n=C.splitPageContentPaths(t);if(n&&n.itemPath){const t=this._getPageData(n.pagePath),o=this._findItemData(n.itemPath,t).parent||t||this._data,s=C.getNodeName(n.itemPath);if(null!=s&&o&&Object.prototype.hasOwnProperty.call(o,c.ITEMS_PROP)){const t=o[c.ITEMS_PROP];if(t){t[s]=e;const n=o[c.ITEMS_ORDER_PROP];if(null!=n&&n.length>0&&null!=r){const t=n.indexOf(r);t>-1?n.splice(i?t:t+1,0,s):n.push(s)}}}}}removeData(t){if(!t)return null;if(!C.isItem(t)&&this._data&&this._data[c.CHILDREN_PROP])return delete this._data[c.CHILDREN_PROP][t],null;const e=C.splitPageContentPaths(t);if(e&&e.itemPath){const t=this._getPageData(e.pagePath),r=this._findItemData(e.itemPath,t);if(r.data&&r&&r.parent&&Object.prototype.hasOwnProperty.call(r.parent,c.ITEMS_PROP)){const{parent:t}=r,i=t[c.ITEMS_PROP],n=C.getNodeName(e.itemPath);if(n){i&&delete i[n],delete r.data,delete r.parent;const e=t[c.ITEMS_ORDER_PROP];if(e&&e.length>0){const t=e.indexOf(n);e.splice(t,1)}return r.parentPath?r.parentPath:null}}}return console.warn(`Item for path ${t} was not found! Nothing to remove then.`),null}destroy(){this._data=null,this._rootPath=null,this._pageContentDelimiter=null}_findItemData(t,e=this._data,r=null,i=""){const n={parent:r,parentPath:i};if(!e)throw new Error("Assertion error: No data provided. This should never happen.");const o=e[c.ITEMS_PROP];if(!o)return n;for(const e in o){if(!Object.prototype.hasOwnProperty.call(o,e))continue;const r=o[e];if(e===t)return n.data=o[e],n.key=e,n;{let n=C.subpath(t,e);if(!this._pageContentDelimiter)throw new Error("_pageContentDelimiter not set. this should never happen as its set in constructor.");{const o=C._getStartStrings(n,this._pageContentDelimiter),s=C.join([i,e,o]);if(n=C.trimStrings(n,this._pageContentDelimiter),n!==t){const t=this._findItemData(n,r,r,s);if(t)return t}}}}return n}_getPageData(t){if(!this._data)return;if(""===t||t===this._data[c.PATH_PROP]||t===this.rootPath)return this._data;const e=this._data[c.CHILDREN_PROP];return e&&e[t]}}class y{constructor(t){this._apiDomain=t}getApiDomain(){return this._apiDomain}getAemLibraries(){const t=document.createDocumentFragment();if(!y.isRemoteApp()||!y.isEditMode())return t;const e=this.prependDomain(y.AUTHORING_LIBRARIES.JS),r=this.prependDomain(y.AUTHORING_LIBRARIES.CSS),i=y.AUTHORING_LIBRARIES.META;return t.append(this.generateScriptElements(e)),t.append(this.generateLinkElements(r)),t.append(this.generateMetaElements(i)),t}setOnLoadCallback(t,e){const r=t.querySelectorAll("script");r.length?r[r.length-1].onload=()=>{e()}:e()}generateMetaElements(t){const e=document.createDocumentFragment();return Object.entries(t).forEach((t=>{const[r,i]=t,n=document.createElement("meta");n.setAttribute("property",r),n.content=i,e.appendChild(n)})),e}generateLinkElements(t){const e=document.createDocumentFragment();return t.forEach((t=>{const r=document.createElement("link");r.type="text/css",r.rel="stylesheet",r.href=t,e.appendChild(r)})),e}generateScriptElements(t){const e=document.createDocumentFragment();return t.forEach((t=>{const r=document.createElement("script");r.type="text/javascript",r.src=t,r.async=!1,e.appendChild(r)})),e}static isMode(t){const e=C.getMetaPropertyValue(s.WCM_MODE)===t,r=C.isBrowser()&&y.getWCMModeFromURL()===t;return e||r}static isEditMode(){return y.isMode(l.EDIT)}static isPreviewMode(){return y.isMode(l.PREVIEW)}static isRemoteApp(){try{return!!new URL(C.getCurrentURL()).searchParams.get(s.WCM_MODE)}catch(t){}return!1}static getWCMModeFromURL(){let t;try{return t=new URL(C.getCurrentURL()),t.searchParams.get(s.WCM_MODE)||""}catch(t){}return""}prependDomain(t){const e=[],r=this.getApiDomain();return t.forEach((t=>{e.push(`${r||""}${t}`)})),e}static isInEditor(){return y.isEditMode()||y.isPreviewMode()||y.isRemoteApp()}}y.EDITOR_CLIENTLIB_PATH="/etc.clientlibs/cq/gui/components/authoring/editors/clientlibs/",y.AUTHORING_LIBRARIES={JS:[y.EDITOR_CLIENTLIB_PATH+"internal/messaging.js",y.EDITOR_CLIENTLIB_PATH+"utils.js",y.EDITOR_CLIENTLIB_PATH+"internal/page.js",y.EDITOR_CLIENTLIB_PATH+"internal/pagemodel/messaging.js"],CSS:[y.EDITOR_CLIENTLIB_PATH+"internal/page.css"],META:{[s.WCM_DATA_TYPE]:"JSON"}};const A=new class{constructor(){this._listenersMap={},this._fetchPromises={},this._modelPaths={}}get modelClient(){if(!this._modelClient)throw new Error("ModelClient is undefined. Call initialize first!");return this._modelClient}get modelStore(){if(!this._modelStore)throw new Error("ModelStore is undefined. Call initialize first!");return this._modelStore}get clientlibUtil(){if(!this._clientlibUtil)throw new Error("AuthoringUtils is undefined. Call initialize first!");return this._clientlibUtil}initialize(t){this.initializeAsync(t);const{rootModelURL:e,rootModelPath:r}=this._modelPaths;if(!e)throw new Error("Provide root model url to initialize ModelManager.");if(!r)throw new Error("No root modelpath resolved! This should never happen.");return this._initPromise}initializeAsync(t){this.destroy();const e=this._toModelConfig(t),r=e&&e.model;this._initializeFields(e),this._initPromise=this._attachAEMLibraries();const{rootModelPath:i}=this._modelPaths;this._modelStore=new v(i,r),i&&this._setInitializationPromise(i)}_attachAEMLibraries(){if(!C.isBrowser())return Promise.resolve();const t=this.clientlibUtil.getAemLibraries();if(!t.hasChildNodes())return Promise.resolve();let e;const r=new Promise((t=>{e=t}));return this.clientlibUtil.setOnLoadCallback(t,e),window.document.head.appendChild(t),r}_initializeFields(t){this._listenersMap={},this._fetchPromises={},this._initPromise=null,this._modelClient=t&&t.modelClient||new R,this._editorClient=new D(this),this._clientlibUtil=new y(this.modelClient.apiHost),this._modelPaths=this._getPathsForModel(t)}_getPathsForModel(t){const e=null==t?void 0:t.path,r=C.getMetaPropertyValue(s.PAGE_MODEL_ROOT_URL),i=C.internalize(r),n=this._isRemoteApp()?"":C.getCurrentPathname(),o=n&&C.sanitize(n)||"",a=e||i||o;return{currentPathname:n,metaPropertyModelURL:i,rootModelURL:a,rootModelPath:C.sanitize(a)||""}}_fetchPageModelFromStore(){const t=this.modelStore.getData();return O(t),t}_setInitializationPromise(t){const{rootModelURL:e}=this._modelPaths;this._initPromise=this._initPromise.then((()=>this._checkDependencies())).then((()=>{const r=this.modelStore.getData(t);return r&&Object.keys(r).length>0?(O(r),r):e?this._fetchData(e).then((e=>{try{return this.modelStore.initialize(t,e),this._fetchActivePageModel(e)||this._fetchPageModelFromStore()}catch(t){console.error("Error on initialization - "+t)}})):void 0}))}_fetchActivePageModel(t){const{currentPathname:e,metaPropertyModelURL:r}=this._modelPaths,i=e&&C.sanitize(e)||"";if(e&&i&&!L(e)&&(o=r,(n=e)&&o&&C.sanitize(n)!==C.sanitize(o))&&!function(t,e){const r=C.sanitize(e);return!!r&&!!(t&&e&&t[c.CHILDREN_PROP]&&t[c.CHILDREN_PROP][r])}(t,e))return this._fetchData(e).then((t=>(this.modelStore.insertData(i,t),this._fetchPageModelFromStore())));if(e&&L(e))return this._fetchPageModelFromStore();if(!C.isBrowser())throw new Error("Attempting to retrieve model data from a non-browser.\n Please provide the initial data with the property key model");var n,o}get rootPath(){return this.modelStore.rootPath}getData(t){let e="",r=!1;return"string"==typeof t?e=t:t&&(e=t.path||"",r=!!t.forceReload),(this._initPromise||Promise.resolve()).then((()=>this._checkDependencies())).then((()=>{if(!r){const t=this.modelStore.getData(e);if(t)return Promise.resolve(t)}if(C.isItem(e)){const{pageData:t,pagePath:r}=this._getParentPage(e);if(!t)return this._fetchData(r).then((t=>(this._storeData(r,t),this.modelStore.getData(e))))}return this._fetchData(e).then((t=>this._storeData(e,t)))}))}_fetchData(t){if(Object.prototype.hasOwnProperty.call(this._fetchPromises,t))return this._fetchPromises[t];if(this.modelClient){const e=this.modelClient.fetch(this._toModelPath(t));return this._fetchPromises[t]=e,e.then((e=>(delete this._fetchPromises[t],this._isRemoteApp()&&O(e),e))).catch((e=>(delete this._fetchPromises[t],e))),e}throw new Error("ModelClient not initialized!")}_notifyListeners(t){if(t=C.adaptPagePath.call(this,t),!this._listenersMap)throw new Error("ListenersMap is undefined.");const e=this._listenersMap[t];e&&e.length&&e.forEach((r=>{try{r()}catch(r){console.error(`Error in listener ${e} at path ${t}: ${r}`)}}))}addListener(t,e){var r;if(!t||"string"!=typeof t||"function"!=typeof e)return;const i=C.adaptPagePath(t,null===(r=this.modelStore)||void 0===r?void 0:r.rootPath);this._listenersMap[i]=this._listenersMap[t]||[],this._listenersMap[i].push(e)}removeListener(t,e){var r;if(!t||"string"!=typeof t||"function"!=typeof e)return;const i=C.adaptPagePath(t,null===(r=this.modelStore)||void 0===r?void 0:r.rootPath),n=this._listenersMap[i];if(n){const t=n.indexOf(e);-1!==t&&n.splice(t,1)}}destroy(){this._modelClient&&this._modelClient.destroy&&this._modelClient.destroy(),this._modelStore&&this._modelStore.destroy&&this._modelStore.destroy(),this._editorClient&&this._editorClient.destroy&&this._editorClient.destroy()}_storeData(t,e){let r=!1;return this._modelStore&&(r=C.isItem(t)),e&&Object.keys(e).length>0&&(this.modelStore.insertData(t,e),this._notifyListeners(t)),r||this._notifyListeners(""),e}_toModelPath(t){let e=C.addSelector(t,"model");return e=C.addExtension(e,"json"),e=C.externalize(e),C.makeAbsolute(e)}_toModelConfig(t){return t&&"string"==typeof t?{path:t}:t||{}}_checkDependencies(){return this.modelClient?this.modelStore?Promise.resolve():Promise.reject("No ModelManager registered."):Promise.reject("No ModelClient registered.")}_getParentPage(t){const e=C.splitPageContentPaths(t),r=(null==e?void 0:e.pagePath)||"";return{pageData:this.modelStore.getData(r),pagePath:r}}_isRemoteApp(){const t=this.modelClient.apiHost||"";return C.isBrowser()&&t.length>0&&C.getCurrentURL()!==t}};class w{constructor(){}}function L(t){const e=function(){const t=C.getMetaPropertyValue(s.PAGE_MODEL_ROUTE_FILTERS);return t?t.split(","):[]}();for(let r=0,i=e.length;r<i;r++)if(new RegExp(e[r]).test(t))return!0;return!1}function T(){if(!C.isBrowser())return!1;const t=C.getMetaPropertyValue(s.PAGE_MODEL_ROUTER);return!t||w.DISABLED!==t}function S(t){if(!T())return;const e=function(t){const e=t||window.location.pathname;return C.sanitize(e)}(t);e&&"/"!==e&&!L(e)&&function(t){A.getData({path:t}).then((t=>{C.dispatchGlobalCustomEvent(n.PAGE_MODEL_ROUTE_CHANGED,{detail:{model:t}})}))}(e)}if(w.DISABLED="disabled",w.CONTENT_PATH="path",T()){const t=window.history.pushState,e=window.history.replaceState;window.history.pushState=(e,r,i)=>(S(i||null),t.apply(history,[e,r,i])),window.history.replaceState=(t,r,i)=>(S(i||null),e.apply(history,[t,r,i])),window.onpopstate=t=>{var e,r;S((null===(r=null===(e=null==t?void 0:t.target)||void 0===e?void 0:e.location)||void 0===r?void 0:r.pathname)||null)}}}},e={};function r(i){if(e[i])return e[i].exports;var n=e[i]={exports:{}};return t[i](n,n.exports,r),n.exports}return r.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return r.d(e,{a:e}),e},r.d=(t,e)=>{for(var i in e)r.o(e,i)&&!r.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},r.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r(873)})()})); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.aemSpaPageModelManager=e():t.aemSpaPageModelManager=e()}(function(){try{return"undefined"!=typeof self}catch(t){return!1}}()?self:this,(function(){return(()=>{"use strict";var t={470:t=>{function e(t){if("string"!=typeof t)throw new TypeError("Path must be a string. Received "+JSON.stringify(t))}function r(t,e){for(var r,i="",n=0,o=-1,s=0,a=0;a<=t.length;++a){if(a<t.length)r=t.charCodeAt(a);else{if(47===r)break;r=47}if(47===r){if(o===a-1||1===s);else if(o!==a-1&&2===s){if(i.length<2||2!==n||46!==i.charCodeAt(i.length-1)||46!==i.charCodeAt(i.length-2))if(i.length>2){var l=i.lastIndexOf("/");if(l!==i.length-1){-1===l?(i="",n=0):n=(i=i.slice(0,l)).length-1-i.lastIndexOf("/"),o=a,s=0;continue}}else if(2===i.length||1===i.length){i="",n=0,o=a,s=0;continue}e&&(i.length>0?i+="/..":i="..",n=2)}else i.length>0?i+="/"+t.slice(o+1,a):i=t.slice(o+1,a),n=a-o-1;o=a,s=0}else 46===r&&-1!==s?++s:s=-1}return i}var i={resolve:function(){for(var t,i="",n=!1,o=arguments.length-1;o>=-1&&!n;o--){var s;o>=0?s=arguments[o]:(void 0===t&&(t=process.cwd()),s=t),e(s),0!==s.length&&(i=s+"/"+i,n=47===s.charCodeAt(0))}return i=r(i,!n),n?i.length>0?"/"+i:"/":i.length>0?i:"."},normalize:function(t){if(e(t),0===t.length)return".";var i=47===t.charCodeAt(0),n=47===t.charCodeAt(t.length-1);return 0!==(t=r(t,!i)).length||i||(t="."),t.length>0&&n&&(t+="/"),i?"/"+t:t},isAbsolute:function(t){return e(t),t.length>0&&47===t.charCodeAt(0)},join:function(){if(0===arguments.length)return".";for(var t,r=0;r<arguments.length;++r){var n=arguments[r];e(n),n.length>0&&(void 0===t?t=n:t+="/"+n)}return void 0===t?".":i.normalize(t)},relative:function(t,r){if(e(t),e(r),t===r)return"";if((t=i.resolve(t))===(r=i.resolve(r)))return"";for(var n=1;n<t.length&&47===t.charCodeAt(n);++n);for(var o=t.length,s=o-n,a=1;a<r.length&&47===r.charCodeAt(a);++a);for(var l=r.length-a,h=s<l?s:l,c=-1,d=0;d<=h;++d){if(d===h){if(l>h){if(47===r.charCodeAt(a+d))return r.slice(a+d+1);if(0===d)return r.slice(a+d)}else s>h&&(47===t.charCodeAt(n+d)?c=d:0===d&&(c=0));break}var u=t.charCodeAt(n+d);if(u!==r.charCodeAt(a+d))break;47===u&&(c=d)}var p="";for(d=n+c+1;d<=o;++d)d!==o&&47!==t.charCodeAt(d)||(0===p.length?p+="..":p+="/..");return p.length>0?p+r.slice(a+c):(a+=c,47===r.charCodeAt(a)&&++a,r.slice(a))},_makeLong:function(t){return t},dirname:function(t){if(e(t),0===t.length)return".";for(var r=t.charCodeAt(0),i=47===r,n=-1,o=!0,s=t.length-1;s>=1;--s)if(47===(r=t.charCodeAt(s))){if(!o){n=s;break}}else o=!1;return-1===n?i?"/":".":i&&1===n?"//":t.slice(0,n)},basename:function(t,r){if(void 0!==r&&"string"!=typeof r)throw new TypeError('"ext" argument must be a string');e(t);var i,n=0,o=-1,s=!0;if(void 0!==r&&r.length>0&&r.length<=t.length){if(r.length===t.length&&r===t)return"";var a=r.length-1,l=-1;for(i=t.length-1;i>=0;--i){var h=t.charCodeAt(i);if(47===h){if(!s){n=i+1;break}}else-1===l&&(s=!1,l=i+1),a>=0&&(h===r.charCodeAt(a)?-1==--a&&(o=i):(a=-1,o=l))}return n===o?o=l:-1===o&&(o=t.length),t.slice(n,o)}for(i=t.length-1;i>=0;--i)if(47===t.charCodeAt(i)){if(!s){n=i+1;break}}else-1===o&&(s=!1,o=i+1);return-1===o?"":t.slice(n,o)},extname:function(t){e(t);for(var r=-1,i=0,n=-1,o=!0,s=0,a=t.length-1;a>=0;--a){var l=t.charCodeAt(a);if(47!==l)-1===n&&(o=!1,n=a+1),46===l?-1===r?r=a:1!==s&&(s=1):-1!==r&&(s=-1);else if(!o){i=a+1;break}}return-1===r||-1===n||0===s||1===s&&r===n-1&&r===i+1?"":t.slice(r,n)},format:function(t){if(null===t||"object"!=typeof t)throw new TypeError('The "pathObject" argument must be of type Object. Received type '+typeof t);return function(t,e){var r=e.dir||e.root,i=e.base||(e.name||"")+(e.ext||"");return r?r===e.root?r+i:r+"/"+i:i}(0,t)},parse:function(t){e(t);var r={root:"",dir:"",base:"",ext:"",name:""};if(0===t.length)return r;var i,n=t.charCodeAt(0),o=47===n;o?(r.root="/",i=1):i=0;for(var s=-1,a=0,l=-1,h=!0,c=t.length-1,d=0;c>=i;--c)if(47!==(n=t.charCodeAt(c)))-1===l&&(h=!1,l=c+1),46===n?-1===s?s=c:1!==d&&(d=1):-1!==s&&(d=-1);else if(!h){a=c+1;break}return-1===s||-1===l||0===d||1===d&&s===l-1&&s===a+1?-1!==l&&(r.base=r.name=0===a&&o?t.slice(1,l):t.slice(a,l)):(0===a&&o?(r.name=t.slice(1,s),r.base=t.slice(1,l)):(r.name=t.slice(a,s),r.base=t.slice(a,l)),r.ext=t.slice(s,l)),a>0?r.dir=t.slice(0,a-1):o&&(r.dir="/"),r},sep:"/",delimiter:":",win32:null,posix:null};i.posix=i,t.exports=i},873:(t,e,r)=>{r.r(e),r.d(e,{AEM_MODE:()=>l,AuthoringUtils:()=>y,Constants:()=>c,ModelClient:()=>R,ModelManager:()=>A,PathUtils:()=>C});class i{constructor(){}}i.PAGE_MODEL_INIT="cq-pagemodel-init",i.PAGE_MODEL_LOADED="cq-pagemodel-loaded",i.PAGE_MODEL_UPDATE="cq-pagemodel-update",i.PAGE_MODEL_ROUTE_CHANGED="cq-pagemodel-route-changed";const n=i;class o{constructor(){}}o.PAGE_MODEL_ROOT_URL="cq:pagemodel_root_url",o.PAGE_MODEL_ROUTE_FILTERS="cq:pagemodel_route_filters",o.PAGE_MODEL_ROUTER="cq:pagemodel_router",o.WCM_MODE="cq:wcmmode",o.WCM_DATA_TYPE="cq:datatype";const s=o;class a{constructor(){}}var l,h;a.TYPE_PROP=":type",a.ITEMS_PROP=":items",a.ITEMS_ORDER_PROP=":itemsOrder",a.PATH_PROP=":path",a.CHILDREN_PROP=":children",a.HIERARCHY_TYPE_PROP=":hierarchyType",a.JCR_CONTENT="jcr:content",function(t){t.EDIT="edit",t.PREVIEW="preview",t.DISABLED="disabled"}(l||(l={})),function(t){t.JS="script",t.STYLESHEET="stylesheet"}(h||(h={}));const c=a,d=require("clone");var u=r.n(d),p=r(470);const f=require("url");var g=r.n(f);class _{constructor(){}}_.DEFAULT_SLING_MODEL_SELECTOR="model",_.DEFAULT_MODEL_JSON_EXTENSION=`.${_.DEFAULT_SLING_MODEL_SELECTOR}.json`;const m=_,P=/(?:\/)(?:content|apps|libs|etc|etc.clientlibs|conf|mnt\/overlay)(?:\/)/,E=`(.+)/${c.JCR_CONTENT}/(.+)`,M="http://dummy";class C{static isBrowser(){return"undefined"!=typeof window}static getContextPath(t){const e=t||this.getCurrentPathname();if(!e)return"";const r=e.match(P),i=null===r?-1:r.index||-1;return i>0?e.slice(0,i):""}static adaptPagePath(t,e){if(!t)return"";const r=C.internalize(t);return e&&r===C.sanitize(e)?"":r}static externalize(t){const e=this.getContextPath();return t.startsWith(e)?t:`${e}${t}`}static internalize(t){if(!t||"string"!=typeof t)return"";const e=this.getContextPath();return t.replace(new RegExp(`^${e}/`),"/")}static getMetaPropertyValue(t){let e=null;if(this.isBrowser()){const r=document.head.querySelector(`meta[property="${t}"]`);e=r?r.getAttribute("content"):null}return e}static convertToModelUrl(t){return t&&t.replace&&t.replace(/\.htm(l)?$/,m.DEFAULT_MODEL_JSON_EXTENSION)}static getCurrentPageModelUrl(){const t=this.getCurrentPathname();let e=null;return t&&(e=this.convertToModelUrl(t)||null),e}static getModelUrl(t){if(t&&t.replace)return this.convertToModelUrl(t);return this.getMetaPropertyValue(s.PAGE_MODEL_ROOT_URL)||this.getCurrentPageModelUrl()}static sanitize(t){if(!t||"string"!=typeof t)return null;let e=g().parse(t,!1,!0).pathname;if(e){e=this.internalize(e);const t=e.indexOf(".");t>-1&&(e=e.substr(0,t)),e=(0,p.normalize)(e)}return e}static addExtension(t,e){if(!e||e.length<1)return t;if(e.startsWith(".")||(e="."+e),!t||t.length<1||t.indexOf(e)>-1)return t;let r=this.normalize(t);const i=new URL(r,M);let n=this.sanitize(i.pathname);n=i.origin===M?n:i.origin+n;let o=this._extractPathWithoutResource(i.pathname);return o=this._replaceExtension(o,e),r=(n+"."+o+i.search).replace(/\.\./g,"."),r}static _extractPathWithoutResource(t){const e=t.split(".");return e.shift(),e.join(".")}static _replaceExtension(t,e){if(t.length<1)return e;const r=t.split("/"),i=r[0].split(".");let n=i.pop();n=n?n.replace(/htm(l)?/,""):"";let o=i.join(".")+"."+n+e;return r.shift(),r.length>0&&(o+=r.join("/")),o}static addSelector(t,e){if(!e||e.length<1)return t;if(e.startsWith(".")||(e="."+e),!t||t.length<1||t.indexOf(e)>-1)return t;const r=t.indexOf(".")||t.length;return r<0?t+e:t.slice(0,r)+e+t.slice(r,t.length)}static getCurrentPathname(){return this.isBrowser()?window.location.pathname:null}static getCurrentURL(){return this.isBrowser()?window.location.href:""}static dispatchGlobalCustomEvent(t,e){this.isBrowser()&&window.dispatchEvent(new CustomEvent(t,e))}static join(t){return t?this.normalize(t.filter((t=>t)).join("/")):""}static normalize(t){return t?t.replace(/\/+/g,"/"):""}static makeAbsolute(t){return t&&"string"==typeof t?t.startsWith("/")?t:"/"+t:""}static makeRelative(t){return t&&"string"==typeof t?t.startsWith("/")?t.slice(1):t:""}static getParentNodePath(t){if(t&&t.length>0){const e=t.lastIndexOf("/")+1;if(e<t.length)return t.substring(0,e-1)}return null}static isItem(t){return new RegExp(E).test(t)}static getNodeName(t){return("string"==typeof t?t.replace(/\/+/g,"/").split(/\//).filter(Boolean):[]).pop()||null}static subpath(t,e){if(!t)return"";const r=C.makeRelative(t).split("/"),i=C.makeRelative(e).split("/");if(r.length<i.length)return t;let n;for(n=0;n<i.length&&r[n]===i[n];++n);return n===i.length?r.slice(n).join("/"):t}static splitByDelimitators(t,e){let r=[t];return e.forEach((t=>{let e=[];const i=C.normalize(C.makeAbsolute(t)+"/");r.forEach((r=>{if(e=e.concat(r.split(i)),r.endsWith(t)){const r=e.splice(e.length-1,1)[0];r!==t&&(e=e.concat(r.split(C.makeAbsolute(t))))}e=e.filter((t=>t))})),r=e})),r}static _getJCRPath(t,e){return[t,c.JCR_CONTENT,e].join("/")}static splitPageContentPaths(t){if(!t&&"string"!=typeof t)return;const e=t.split(`/${c.JCR_CONTENT}/`),r={pagePath:e[0]};return e.length>1&&(r.itemPath=e[1]),r}static trimStrings(t,e){return e.forEach((e=>{for(;t.startsWith(e);)t=C.makeRelative(t.slice(e.length));for(;t.endsWith(e);)(t=t.slice(0,t.length-e.length)).endsWith("/")&&(t=t.slice(0,t.length-1))})),t}static _getStartStrings(t,e){let r="";return e.forEach((e=>{for(;t.startsWith(e);)t=C.makeRelative(t.slice(e.length)),r=`${r}/${e}`})),C.makeRelative(r)}static toAEMPath(t,e,r){if(window.location.origin===e){const e=`(/editor.html)?(/content/${r=r.replace(/^\/|\/$/g,"")})?`;if(t.indexOf(e)<0)return`${e}${t}(.html)?`}return t}}function O(t){C.dispatchGlobalCustomEvent(n.PAGE_MODEL_LOADED,{detail:{model:u()(t)}})}class D{constructor(t){this._modelManager=t,this._windowListener=t=>{t&&t.detail&&t.detail.msg?this._updateModel(t.detail.msg):console.error("EditorService.js","No message passed to cq-pagemodel-update",t)},C.isBrowser()&&window.addEventListener(n.PAGE_MODEL_UPDATE,this._windowListener)}_updateModel(t){if(!t||!t.cmd||!t.path)return void console.error("PageModelManager.js","Not enough data received to update the page model");const e=t.path,r=t.cmd,i=u()(t.data);let n,o,s;const a=C.getParentNodePath(e);switch(r){case"replace":this._modelManager.modelStore.setData(e,i),this._modelManager._notifyListeners(e);break;case"delete":this._modelManager.modelStore.removeData(e),a&&this._modelManager._notifyListeners(a);break;case"insertBefore":s=!0;case"insertAfter":n=C.getNodeName(e),a&&(o=a+"/"+i.key,this._modelManager.modelStore.insertData(o,i.value,n,s),this._modelManager._notifyListeners(a));break;default:console.log("EditorClient","unsupported command:",r)}O(this._modelManager.modelStore.dataMap)}destroy(){C.isBrowser()&&window.removeEventListener(n.PAGE_MODEL_UPDATE,this._windowListener)}}class R{constructor(t){this._apiHost=t||null}get apiHost(){return this._apiHost}fetch(t){if(!t){const e="Fetching model rejected for path: "+t;return Promise.reject(new Error(e))}const e=this._apiHost||"";return fetch(`${e}${t}`,{credentials:"same-origin"}).then((t=>{if(t.status>=200&&t.status<300)return t.json();throw{response:t}})).catch((t=>Promise.reject(t)))}destroy(){this._apiHost=null}}class v{constructor(t,e){this._data=null,this._rootPath=null,this._data={},t&&this.initialize(t,e||{}),this._pageContentDelimiter=[c.JCR_CONTENT]}initialize(t,e){e&&(this._data=e),this._rootPath=t}get rootPath(){return this._rootPath||""}get dataMap(){return this._data}setData(t,e={}){const r=C.getNodeName(t);if(r){const i=this.getData(C.getParentNodePath(t),!1);if(i&&i[c.ITEMS_PROP]){const t=u()(e),n=i[c.ITEMS_PROP]||{};n[r]&&Object.keys(n[r]).forEach((e=>t.value[e]=t.value[e]||"")),n[r]=t.value,i[c.ITEMS_PROP]=n}}}getData(t,e=!0){if(!t&&"string"!=typeof t)return e?u()(this._data):this._data;if(t===this._rootPath||t===`${this._rootPath}/${c.JCR_CONTENT}`)return e?u()(this._data):this._data;const r=C.splitPageContentPaths(t);if(r){const t=this._getPageData(r.pagePath);if(!t||!r.itemPath)return e?u()(t):t;const i=this._findItemData(r.itemPath,t);if(i)return e?u()(i.data):i.data}}insertData(t,e,r,i=!1){if(e=u()(e),!t)return void console.warn("No path provided for data: "+e);if(!C.isItem(t)&&this._data)return this._data[c.CHILDREN_PROP]||(this._data[c.CHILDREN_PROP]={}),void(this._data[c.CHILDREN_PROP][t]=e);const n=C.splitPageContentPaths(t);if(n&&n.itemPath){const t=this._getPageData(n.pagePath),o=this._findItemData(n.itemPath,t).parent||t||this._data,s=C.getNodeName(n.itemPath);if(null!=s&&o&&Object.prototype.hasOwnProperty.call(o,c.ITEMS_PROP)){const t=o[c.ITEMS_PROP];if(t){t[s]=e;const n=o[c.ITEMS_ORDER_PROP];if(null!=n&&n.length>0&&null!=r){const t=n.indexOf(r);t>-1?n.splice(i?t:t+1,0,s):n.push(s)}}}}}removeData(t){if(!t)return null;if(!C.isItem(t)&&this._data&&this._data[c.CHILDREN_PROP])return delete this._data[c.CHILDREN_PROP][t],null;const e=C.splitPageContentPaths(t);if(e&&e.itemPath){const t=this._getPageData(e.pagePath),r=this._findItemData(e.itemPath,t);if(r.data&&r&&r.parent&&Object.prototype.hasOwnProperty.call(r.parent,c.ITEMS_PROP)){const{parent:t}=r,i=t[c.ITEMS_PROP],n=C.getNodeName(e.itemPath);if(n){i&&delete i[n],delete r.data,delete r.parent;const e=t[c.ITEMS_ORDER_PROP];if(e&&e.length>0){const t=e.indexOf(n);e.splice(t,1)}return r.parentPath?r.parentPath:null}}}return console.warn(`Item for path ${t} was not found! Nothing to remove then.`),null}destroy(){this._data=null,this._rootPath=null,this._pageContentDelimiter=null}_findItemData(t,e=this._data,r=null,i=""){const n={parent:r,parentPath:i};if(!e)throw new Error("Assertion error: No data provided. This should never happen.");const o=e[c.ITEMS_PROP];if(!o)return n;for(const e in o){if(!Object.prototype.hasOwnProperty.call(o,e))continue;const r=o[e];if(e===t)return n.data=o[e],n.key=e,n;{let n=C.subpath(t,e);if(!this._pageContentDelimiter)throw new Error("_pageContentDelimiter not set. this should never happen as its set in constructor.");{const o=C._getStartStrings(n,this._pageContentDelimiter),s=C.join([i,e,o]);if(n=C.trimStrings(n,this._pageContentDelimiter),n!==t){const t=this._findItemData(n,r,r,s);if(t)return t}}}}return n}_getPageData(t){if(!this._data)return;if(""===t||t===this._data[c.PATH_PROP]||t===this.rootPath)return this._data;const e=this._data[c.CHILDREN_PROP];return e&&e[t]}}class y{constructor(t){this._apiDomain=t}getApiDomain(){return this._apiDomain}getAemLibraries(){const t=document.createDocumentFragment();if(!y.isRemoteApp()||!y.isEditMode())return t;const e=this.prependDomain(y.AUTHORING_LIBRARIES.JS),r=this.prependDomain(y.AUTHORING_LIBRARIES.CSS),i=y.AUTHORING_LIBRARIES.META;return t.append(this.generateScriptElements(e)),t.append(this.generateLinkElements(r)),t.append(this.generateMetaElements(i)),t}setOnLoadCallback(t,e){const r=t.querySelectorAll("script");r.length?r[r.length-1].onload=()=>{e()}:e()}generateMetaElements(t){const e=document.createDocumentFragment();return Object.entries(t).forEach((t=>{const[r,i]=t,n=document.createElement("meta");n.setAttribute("property",r),n.content=i,e.appendChild(n)})),e}generateLinkElements(t){const e=document.createDocumentFragment();return t.forEach((t=>{const r=document.createElement("link");r.type="text/css",r.rel="stylesheet",r.href=t,e.appendChild(r)})),e}generateScriptElements(t){const e=document.createDocumentFragment();return t.forEach((t=>{const r=document.createElement("script");r.type="text/javascript",r.src=t,r.async=!1,e.appendChild(r)})),e}static isMode(t){const e=C.getMetaPropertyValue(s.WCM_MODE)===t,r=C.isBrowser()&&y.getWCMModeFromURL()===t;return e||r}static isEditMode(){return y.isMode(l.EDIT)}static isPreviewMode(){return y.isMode(l.PREVIEW)}static isRemoteApp(){try{return!!new URL(C.getCurrentURL()).searchParams.get(s.WCM_MODE)}catch(t){}return!1}static getWCMModeFromURL(){let t;try{return t=new URL(C.getCurrentURL()),t.searchParams.get(s.WCM_MODE)||""}catch(t){}return""}prependDomain(t){const e=[],r=this.getApiDomain();return t.forEach((t=>{e.push(`${r||""}${t}`)})),e}static isInEditor(){return y.isEditMode()||y.isPreviewMode()||y.isRemoteApp()}}y.EDITOR_CLIENTLIB_PATH="/etc.clientlibs/cq/gui/components/authoring/editors/clientlibs/",y.AUTHORING_LIBRARIES={JS:[y.EDITOR_CLIENTLIB_PATH+"internal/messaging.js",y.EDITOR_CLIENTLIB_PATH+"utils.js",y.EDITOR_CLIENTLIB_PATH+"internal/page.js",y.EDITOR_CLIENTLIB_PATH+"internal/pagemodel/messaging.js"],CSS:[y.EDITOR_CLIENTLIB_PATH+"internal/page.css"],META:{[s.WCM_DATA_TYPE]:"JSON"}};const A=new class{constructor(){this._listenersMap={},this._fetchPromises={},this._modelPaths={}}get modelClient(){if(!this._modelClient)throw new Error("ModelClient is undefined. Call initialize first!");return this._modelClient}get modelStore(){if(!this._modelStore)throw new Error("ModelStore is undefined. Call initialize first!");return this._modelStore}get clientlibUtil(){if(!this._clientlibUtil)throw new Error("AuthoringUtils is undefined. Call initialize first!");return this._clientlibUtil}initialize(t){this.initializeAsync(t);const{rootModelURL:e,rootModelPath:r}=this._modelPaths;if(!e)throw new Error("Provide root model url to initialize ModelManager.");if(!r)throw new Error("No root modelpath resolved! This should never happen.");return this._initPromise}initializeAsync(t){this.destroy();const e=this._toModelConfig(t),r=e&&e.model;this._initializeFields(e),this._initPromise=this._attachAEMLibraries();const{rootModelPath:i}=this._modelPaths;this._modelStore=new v(i,r),i&&this._setInitializationPromise(i)}_attachAEMLibraries(){if(!C.isBrowser())return Promise.resolve();const t=this.clientlibUtil.getAemLibraries();if(!t.hasChildNodes())return Promise.resolve();let e;const r=new Promise((t=>{e=t}));return this.clientlibUtil.setOnLoadCallback(t,e),window.document.head.appendChild(t),r}_initializeFields(t){this._listenersMap={},this._fetchPromises={},this._initPromise=null,this._modelClient=t&&t.modelClient||new R,this._editorClient=new D(this),this._clientlibUtil=new y(this.modelClient.apiHost),this._modelPaths=this._getPathsForModel(t)}_getPathsForModel(t){const e=null==t?void 0:t.path,r=C.getMetaPropertyValue(s.PAGE_MODEL_ROOT_URL),i=C.internalize(r),n=this._isRemoteApp()?"":C.getCurrentPathname(),o=n&&C.sanitize(n)||"",a=e||i||o;return{currentPathname:n,metaPropertyModelURL:i,rootModelURL:a,rootModelPath:C.sanitize(a)||""}}_fetchPageModelFromStore(){const t=this.modelStore.getData();return O(t),t}_setInitializationPromise(t){const{rootModelURL:e}=this._modelPaths;this._initPromise=this._initPromise.then((()=>this._checkDependencies())).then((()=>{const r=this.modelStore.getData(t);return r&&Object.keys(r).length>0?(O(r),r):e?this._fetchData(e).then((e=>{try{return this.modelStore.initialize(t,e),this._fetchActivePageModel(e)||this._fetchPageModelFromStore()}catch(t){console.error("Error on initialization - "+t)}})):void 0}))}_fetchActivePageModel(t){const{currentPathname:e,metaPropertyModelURL:r}=this._modelPaths,i=e&&C.sanitize(e)||"";if(e&&i&&!L(e)&&(o=r,(n=e)&&o&&C.sanitize(n)!==C.sanitize(o))&&!function(t,e){const r=C.sanitize(e);return!!r&&!!(t&&e&&t[c.CHILDREN_PROP]&&t[c.CHILDREN_PROP][r])}(t,e))return this._fetchData(e).then((t=>(this.modelStore.insertData(i,t),this._fetchPageModelFromStore())));if(e&&L(e))return this._fetchPageModelFromStore();if(!C.isBrowser())throw new Error("Attempting to retrieve model data from a non-browser.\n Please provide the initial data with the property key model");var n,o}get rootPath(){return this.modelStore.rootPath}getData(t){let e="",r=!1;return"string"==typeof t?e=t:t&&(e=t.path||"",r=!!t.forceReload),(this._initPromise||Promise.resolve()).then((()=>this._checkDependencies())).then((()=>{if(!r){const t=this.modelStore.getData(e);if(t)return Promise.resolve(t)}if(C.isItem(e)){const{pageData:t,pagePath:r}=this._getParentPage(e);if(!t)return this._fetchData(r).then((t=>(this._storeData(r,t),this.modelStore.getData(e))))}return this._fetchData(e).then((t=>this._storeData(e,t)))}))}_fetchData(t){if(Object.prototype.hasOwnProperty.call(this._fetchPromises,t))return this._fetchPromises[t];if(this.modelClient){const e=this.modelClient.fetch(this._toModelPath(t));return this._fetchPromises[t]=e,e.then((e=>(delete this._fetchPromises[t],this._isRemoteApp()&&O(e),e))).catch((e=>(delete this._fetchPromises[t],e))),e}throw new Error("ModelClient not initialized!")}_notifyListeners(t){if(t=C.adaptPagePath.call(this,t),!this._listenersMap)throw new Error("ListenersMap is undefined.");const e=this._listenersMap[t];e&&e.length&&e.forEach((r=>{try{r()}catch(r){console.error(`Error in listener ${e} at path ${t}: ${r}`)}}))}addListener(t,e){var r;if(!t||"string"!=typeof t||"function"!=typeof e)return;const i=C.adaptPagePath(t,null===(r=this.modelStore)||void 0===r?void 0:r.rootPath);this._listenersMap[i]=this._listenersMap[t]||[],this._listenersMap[i].push(e)}removeListener(t,e){var r;if(!t||"string"!=typeof t||"function"!=typeof e)return;const i=C.adaptPagePath(t,null===(r=this.modelStore)||void 0===r?void 0:r.rootPath),n=this._listenersMap[i];if(n){const t=n.indexOf(e);-1!==t&&n.splice(t,1)}}destroy(){this._modelClient&&this._modelClient.destroy&&this._modelClient.destroy(),this._modelStore&&this._modelStore.destroy&&this._modelStore.destroy(),this._editorClient&&this._editorClient.destroy&&this._editorClient.destroy()}_storeData(t,e){let r=!1;return this._modelStore&&(r=C.isItem(t)),e&&Object.keys(e).length>0&&(this.modelStore.insertData(t,e),this._notifyListeners(t)),r||this._notifyListeners(""),e}_toModelPath(t){let e=C.addSelector(t,"model");return e=C.addExtension(e,"json"),e=C.externalize(e),C.makeAbsolute(e)}_toModelConfig(t){return t&&"string"==typeof t?{path:t}:t||{}}_checkDependencies(){return this.modelClient?this.modelStore?Promise.resolve():Promise.reject("No ModelManager registered."):Promise.reject("No ModelClient registered.")}_getParentPage(t){const e=C.splitPageContentPaths(t),r=(null==e?void 0:e.pagePath)||"";return{pageData:this.modelStore.getData(r),pagePath:r}}_isRemoteApp(){const t=this.modelClient.apiHost||"";return C.isBrowser()&&t.length>0&&C.getCurrentURL()!==t}};class w{constructor(){}}function L(t){const e=function(){const t=C.getMetaPropertyValue(s.PAGE_MODEL_ROUTE_FILTERS);return t?t.split(","):[]}();for(let r=0,i=e.length;r<i;r++)if(new RegExp(e[r]).test(t))return!0;return!1}function T(){if(!C.isBrowser())return!1;const t=C.getMetaPropertyValue(s.PAGE_MODEL_ROUTER);return!t||w.DISABLED!==t}function S(t){if(!T())return;const e=function(t){const e=t||window.location.pathname;return C.sanitize(e)}(t);e&&"/"!==e&&!L(e)&&function(t){A.getData({path:t}).then((t=>{C.dispatchGlobalCustomEvent(n.PAGE_MODEL_ROUTE_CHANGED,{detail:{model:t}})}))}(e)}if(w.DISABLED="disabled",w.CONTENT_PATH="path",T()){const t=window.history.pushState,e=window.history.replaceState;window.history.pushState=(e,r,i)=>(S(i||null),t.apply(history,[e,r,i])),window.history.replaceState=(t,r,i)=>(S(i||null),e.apply(history,[t,r,i])),window.onpopstate=t=>{var e,r;S((null===(r=null===(e=null==t?void 0:t.target)||void 0===e?void 0:e.location)||void 0===r?void 0:r.pathname)||null)}}}},e={};function r(i){if(e[i])return e[i].exports;var n=e[i]={exports:{}};return t[i](n,n.exports,r),n.exports}return r.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return r.d(e,{a:e}),e},r.d=(t,e)=>{for(var i in e)r.o(e,i)&&!r.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},r.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r(873)})()})); |
{ | ||
"name": "@adobe/aem-spa-page-model-manager", | ||
"version": "1.3.11", | ||
"version": "1.3.12", | ||
"description": "An interpreter between AEM Editor and the AEM SPA Editor.", | ||
@@ -19,3 +19,3 @@ "keywords": [ | ||
"engines": { | ||
"npm": ">=6.14.4", | ||
"npm": "6.14.13", | ||
"node": ">=12.16.2" | ||
@@ -49,5 +49,5 @@ }, | ||
"@types/jest": "^26.0.14", | ||
"@types/node": "^14.11.2", | ||
"@typescript-eslint/eslint-plugin": "^4.2.0", | ||
"@typescript-eslint/parser": "^4.2.0", | ||
"@types/node": "^14.11.5", | ||
"@typescript-eslint/eslint-plugin": "^4.4.0", | ||
"@typescript-eslint/parser": "^4.4.0", | ||
"clean-webpack-plugin": "^3.0.0", | ||
@@ -59,7 +59,7 @@ "commitizen": "^4.2.1", | ||
"eslint-plugin-json": "^2.1.2", | ||
"jest": "^26.4.2", | ||
"jest": "^26.5.2", | ||
"jest-fetch-mock": "^3.0.3", | ||
"semantic-release": "^17.1.2", | ||
"ts-jest": "^26.4.0", | ||
"ts-loader": "^8.0.4", | ||
"ts-jest": "^26.4.1", | ||
"ts-loader": "^8.1.0", | ||
"ts-mockito": "^2.6.1", | ||
@@ -66,0 +66,0 @@ "typedoc": "^0.20.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
85723