@agility/app-sdk
Advanced tools
Comparing version
@@ -1,1 +0,1 @@ | ||
!function(e,o){"object"==typeof exports&&"object"==typeof module?module.exports=o():"function"==typeof define&&define.amd?define("agilityAppSDK",[],o):"object"==typeof exports?exports.agilityAppSDK=o():e.agilityAppSDK=o()}("undefined"!=typeof self?self:this,(function(){return(()=>{"use strict";var e={d:(o,t)=>{for(var n in t)e.o(t,n)&&!e.o(o,n)&&Object.defineProperty(o,n,{enumerable:!0,get:t[n]})},o:(e,o)=>Object.prototype.hasOwnProperty.call(e,o),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},o={};e.d(o,{default:()=>_});var t={};e.r(t),e.d(t,{APP_LOCATION_APP_CONFIG:()=>i,APP_LOCATION_CUSTOM_FIELD:()=>n,APP_LOCATION_FLYOUT:()=>l,APP_LOCATION_UNKNOWN:()=>a,closeFlyout:()=>u,getAppLocation:()=>g,initializeField:()=>s,openFlyout:()=>m,resolveAppComponent:()=>y,setAppConfig:()=>r,updateFieldHeight:()=>f,updateFieldValue:()=>d});var n="CustomField",a="Unknown",i="AppConfig",l="Flyout",r=function(e){window.parent&&window.parent.postMessage({message:e,type:"setAppConfig_for_".concat(e.name)},"*")},s=function(e){var o=e.location,t=e.containerRef,n=e.onReady,a=w("fieldID"),i=w("fieldName"),l=c({location:o,fieldName:i,fieldID:a});p({containerRef:t,messageID:l}),console.log("".concat(l," => Waiting for message from Agility CMS")),window.addEventListener("message",(function(e){e.data.type==="setInitialProps_for_".concat(l)?(console.log("".concat(l," => auth, fieldValue received from Agility CMS, setting up field...")),n(e.data.message)):console.log("".concat(l," => IGNORING MESSAGE FROM PARENT: "),e.data)}),!1),window.parent?(console.log("".concat(l," => 😀 Notifying Agility CMS this field is ready to receive messages...")),window.parent.postMessage({message:"ready",type:"ready_for_".concat(l)},"*")):console.log("".concat(l," => 😞 Parent window not found. You must load this within Agility CMS as an iFrame."))},c=function(e){return e.location+"_"+e.fieldName+"_"+e.fieldID},d=function(e){var o=e.value,t=e.location,n=e.fieldName,a=e.fieldID,i=c({location:t,fieldName:n,fieldID:a});window.parent?window.parent.postMessage({message:o,type:"setNewValue_for_".concat(i)},"*"):console.log("".concat(i," => 😞 Can't post message to parent."))},f=function(e){var o=e.height,t=e.messageID;window.parent&&window.parent.postMessage({message:o,type:"setHeight_for_".concat(t)},"*")},p=function(e){var o=e.containerRef,t=e.messageID;setInterval((function(){f({height:o.current?o.current.offsetHeight:o.offsetHeight,messageID:t})}),500)},m=function(e){var o=e.title,t=e.size,a=e.appLocationName,i=e.fieldName,r=e.fieldID,s=e.onClose,d=e.params,f=c({location:n,fieldID:r,fieldName:i});window.parent&&(window.parent.postMessage({message:{title:o,size:t,appLocation:l,appLocationName:a,params:d},type:"openFlyout_for_".concat(f)},"*"),window.addEventListener("message",(function e(o){o.data.type==="closeFlyoutCallback_for_".concat(f)&&s(o.data.message),window.removeEventListener("message",e,!1)}),!1))},u=function(e){var o=e.fieldName,t=e.fieldID,a=e.params,i=n,l=c({location:i,fieldID:t,fieldName:o});window.parent&&window.parent.postMessage({message:{location:i,fieldName:o,fieldID:t,params:a},type:"closeFlyout_for_".concat(l)},"*")},g=function(){var e=w("location");return e===n?{location:e,name:w("fieldTypeName")}:e===i||e===l?{location:e}:{location:a,name:null}},y=function(e){var o=g(),t=e.appComponents.find((function(e){return e.location===o.location&&(!o.name||e.name===o.name)}));if(t)return t.componentToRender;console.error("Could not render the '"+e.name+"' component for '"+o.location+"' with the name of '"+o.name+"'")},w=function(e){e=e.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");var o=new RegExp("[\\?&]"+e+"=([^&#]*)").exec(window.location.search);return null===o?"":decodeURIComponent(o[1].replace(/\+/g," "))};const _=t;return o.default})()})); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("agilityAppSDK",[],t):"object"==typeof exports?exports.agilityAppSDK=t():e.agilityAppSDK=t()}("undefined"!=typeof self?self:this,(function(){return(()=>{"use strict";var e={d:(t,n)=>{for(var o in n)e.o(n,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:n[o]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.d(t,{default:()=>h});var n={};e.r(n),e.d(n,{closeFlyout:()=>f,openFlyout:()=>l,subscribeToFieldValueChanges:()=>d,updateFieldValue:()=>c});var o={};e.r(o),e.d(o,{initializeAppConfig:()=>w,initializeField:()=>y,locations:()=>a,resolveAppComponent:()=>O});const a={APP_LOCATION_CUSTOM_FIELD:"CustomField",APP_LOCATION_FLYOUT:"Flyout",APP_LOCATION_UNKNOWN:"Unknown",APP_LOCATION_APP_CONFIG:"AppConfig"};var i=function(e){e=e.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");var t=new RegExp("[\\?&]"+e+"=([^&#]*)").exec(window.location.search);return null===t?"":decodeURIComponent(t[1].replace(/\+/g," "))},r=function(e){return e.location+"_"+e.fieldName+"_"+e.fieldID},s=function(e){var t=e.containerRef,n=e.messageID;setInterval((function(){!function(e){var t=e.height,n=e.messageID;window.parent&&window.parent.postMessage({message:t,type:"setHeight_for_".concat(n)},"*")}({height:t.current?t.current.offsetHeight:t.offsetHeight,messageID:n})}),500)},c=function(e){var t=e.fieldName,n=e.fieldValue,o=r({location:this.location,fieldName:this.fieldName,fieldID:this.fieldID});t||(t=this.fieldName),window.parent?window.parent.postMessage({message:{fieldName:t,fieldValue:n},type:"setNewValue_for_".concat(o)},"*"):console.log("".concat(o," => 😞 Can't post message to parent."))},l=function(e){var t=e.title,n=e.size,o=e.name,i=e.onClose,s=e.params,c=r({location:a.APP_LOCATION_CUSTOM_FIELD,fieldID:this.fieldID,fieldName:this.fieldName});window.parent&&(window.parent.postMessage({message:{title:t,size:n,name:o,params:s},type:"openFlyout_for_".concat(c)},"*"),window.addEventListener("message",(function e(t){t.data.type==="closeFlyoutCallback_for_".concat(c)&&i(t.data.message),window.removeEventListener("message",e,!1)}),!1))},f=function(e){var t=e.params,n=a.APP_LOCATION_CUSTOM_FIELD,o=r({location:n,fieldID:this.fieldID,fieldName:this.fieldName});window.parent&&window.parent.postMessage({message:{location:n,fieldName:this.fieldName,fieldID:this.fieldID,params:t},type:"closeFlyout_for_".concat(o)},"*")},d=function(e){var t=e.fieldName,n=e.onChange,o=r({fieldID:this.fieldID,fieldName:this.fieldName,location:this.location});window.addEventListener("message",(function(e){e.data.type==="otherValueChanged_".concat(t,"_for_").concat(o)&&n(e.data.message)}),!1),window.parent?window.parent.postMessage({message:t,type:"subscribeToOtherValueChanges_".concat(o)},"*"):console.log("".concat(o," => 😞 Parent window not found. You must load this within Agility CMS as an iFrame."))};function u(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);t&&(o=o.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,o)}return n}function p(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?u(Object(n),!0).forEach((function(t){m(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):u(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function m(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function g(e,t,n,o,a,i,r){try{var s=e[i](r),c=s.value}catch(e){return void n(e)}s.done?t(c):Promise.resolve(c).then(o,a)}var w=function(e){window.parent&&window.parent.postMessage({message:e,type:"setAppConfig_for_".concat(e.name)},"*")},y=function(){var e,t=(e=regeneratorRuntime.mark((function e(t){var o;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.containerRef,e.abrupt("return",new Promise((function(e){var t=i("fieldID"),a=i("fieldName"),c=i("location"),l=r({location:c,fieldName:a,fieldID:t});s({containerRef:o,messageID:l}),console.log("".concat(l," => Waiting for message from Agility CMS")),window.addEventListener("message",(function(t){if(t.data.type==="setInitialProps_for_".concat(l)){console.log("".concat(l," => auth, fieldValue received from Agility CMS, setting up field..."));var o=t.data.message;o.location=c,e(new function(){return p(p({},o),n)})}else console.log("".concat(l," => IGNORING MESSAGE FROM PARENT: "),t.data)}),!1),window.parent?(console.log("".concat(l," => 😀 Notifying Agility CMS this field is ready to receive messages...")),window.parent.postMessage({message:"ready",type:"ready_for_".concat(l)},"*")):console.log("".concat(l," => 😞 Parent window not found. You must load this within Agility CMS as an iFrame."))})));case 2:case"end":return e.stop()}}),e)})),function(){var t=this,n=arguments;return new Promise((function(o,a){var i=e.apply(t,n);function r(e){g(i,o,a,r,s,"next",e)}function s(e){g(i,o,a,r,s,"throw",e)}r(void 0)}))});return function(e){return t.apply(this,arguments)}}(),O=function(e){var t=_(),n=e.appComponents.find((function(e){return e.location===t.location&&(!t.name||e.name===t.name)}));if(n)return n.componentToRender;console.error("Could not render the '"+e.name+"' component for '"+t.location+"' with the name of '"+t.name+"'")},_=function(){var e=i("location");return e===a.APP_LOCATION_CUSTOM_FIELD?{location:e,name:i("fieldTypeName")}:e===a.APP_LOCATION_APP_CONFIG?{location:e}:e===a.APP_LOCATION_FLYOUT?{location:e,name:i("flyoutName")}:{location:a.APP_LOCATION_UNKNOWN,name:null}};const h=o;return t.default})()})); |
{ | ||
"name": "@agility/app-sdk", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "JavaScript library for building Agility CMS apps.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
206
src/sdk.js
@@ -1,8 +0,6 @@ | ||
const APP_LOCATION_CUSTOM_FIELD = 'CustomField'; | ||
const APP_LOCATION_UNKNOWN = 'Unknown'; | ||
const APP_LOCATION_APP_CONFIG = 'AppConfig'; | ||
const APP_LOCATION_FLYOUT = 'Flyout'; | ||
import * as fieldMethods from './fields' | ||
import locations from './locations' | ||
import { getUrlParameter, getMessageID, autoSyncFieldHeight } from './utils' | ||
const setAppConfig = (appConfig) => { | ||
const initializeAppConfig = (appConfig) => { | ||
if (window.parent) { | ||
@@ -16,113 +14,69 @@ window.parent.postMessage({ | ||
const initializeField = ({ location, containerRef, onReady }) => { | ||
const initializeField = async ({ containerRef }) => { | ||
const fieldID = getUrlParameter('fieldID'); | ||
const fieldName = getUrlParameter('fieldName'); | ||
var messageID = getMessageID({location, fieldName, fieldID}); | ||
return new Promise(resolve => { | ||
const fieldID = getUrlParameter('fieldID'); | ||
const fieldName = getUrlParameter('fieldName'); | ||
const location = getUrlParameter('location'); | ||
autoSyncFieldHeight({ containerRef, messageID }); | ||
var messageID = getMessageID({location, fieldName, fieldID}); | ||
//get the field ready to wait for messages from the parent | ||
console.log(`${messageID} => Waiting for message from Agility CMS`) | ||
autoSyncFieldHeight({ containerRef, messageID }); | ||
//open a channel to listen to messages from the CMS | ||
window.addEventListener("message", function (e) { | ||
//only care about these messages | ||
if(e.data.type === `setInitialProps_for_${messageID}`) { | ||
console.log(`${messageID} => auth, fieldValue received from Agility CMS, setting up field...`) | ||
onReady(e.data.message); | ||
} else { | ||
//show us the unhandled message... | ||
console.log(`${messageID} => IGNORING MESSAGE FROM PARENT: `, e.data) | ||
} | ||
}, false); | ||
//get the field ready to wait for messages from the parent | ||
console.log(`${messageID} => Waiting for message from Agility CMS`) | ||
//let the CMS know we are NOW ready to receive messages | ||
if (window.parent) { | ||
console.log(`${messageID} => 😀 Notifying Agility CMS this field is ready to receive messages...`) | ||
window.parent.postMessage({ | ||
message: "ready", | ||
type: `ready_for_${messageID}` | ||
}, "*") | ||
} else { | ||
console.log(`${messageID} => 😞 Parent window not found. You must load this within Agility CMS as an iFrame.`) | ||
} | ||
} | ||
//open a channel to listen to messages from the CMS | ||
window.addEventListener("message", function (e) { | ||
//only care about these messages | ||
if(e.data.type === `setInitialProps_for_${messageID}`) { | ||
console.log(`${messageID} => auth, fieldValue received from Agility CMS, setting up field...`) | ||
const getMessageID = ({ location, fieldName, fieldID }) => { | ||
return location + '_' + fieldName + '_' + fieldID; | ||
} | ||
//set field info that we can re-use later | ||
const fieldInfo = e.data.message; | ||
fieldInfo.location = location; | ||
const updateFieldValue = ({ value, location, fieldName, fieldID }) => { | ||
var messageID = getMessageID({location, fieldName, fieldID}); | ||
if (window.parent) { | ||
window.parent.postMessage({ | ||
message: value, | ||
type: `setNewValue_for_${messageID}` | ||
}, "*") | ||
} else { | ||
console.log(`${messageID} => 😞 Can't post message to parent.`) | ||
} | ||
} | ||
resolve(new function() { | ||
return { | ||
...fieldInfo, | ||
...fieldMethods | ||
} | ||
}()); | ||
const updateFieldHeight = ({ height, messageID }) => { | ||
if (window.parent) { | ||
window.parent.postMessage({ | ||
message: height, | ||
type: `setHeight_for_${messageID}` | ||
}, "*") | ||
} | ||
} | ||
} else { | ||
//show us the unhandled message... | ||
console.log(`${messageID} => IGNORING MESSAGE FROM PARENT: `, e.data) | ||
} | ||
}, false); | ||
const autoSyncFieldHeight = ({ containerRef, messageID }) => { | ||
setInterval(function() { | ||
updateFieldHeight({ | ||
height: containerRef.current ? containerRef.current.offsetHeight : containerRef.offsetHeight, | ||
messageID | ||
}); | ||
}, 500) | ||
//let the CMS know we are NOW ready to receive messages | ||
if (window.parent) { | ||
console.log(`${messageID} => 😀 Notifying Agility CMS this field is ready to receive messages...`) | ||
window.parent.postMessage({ | ||
message: "ready", | ||
type: `ready_for_${messageID}` | ||
}, "*") | ||
} else { | ||
console.log(`${messageID} => 😞 Parent window not found. You must load this within Agility CMS as an iFrame.`) | ||
} | ||
}) | ||
} | ||
const openFlyout = ({title, size, appLocationName, fieldName, fieldID, onClose, params }) => { | ||
const messageID = getMessageID({location:APP_LOCATION_CUSTOM_FIELD, fieldID, fieldName }); | ||
if (window.parent) { | ||
window.parent.postMessage({ | ||
message: { | ||
title, | ||
size, | ||
appLocation: APP_LOCATION_FLYOUT, | ||
appLocationName, | ||
params | ||
}, | ||
type: `openFlyout_for_${messageID}` | ||
}, "*") | ||
var listener = function(e) { | ||
if(e.data.type === `closeFlyoutCallback_for_${messageID}`) { | ||
onClose(e.data.message); | ||
} | ||
window.removeEventListener("message", listener, false); | ||
}; | ||
window.addEventListener("message", listener, false) | ||
} | ||
} | ||
const resolveAppComponent = (appConfig) => { | ||
const appLocation = getAppLocation(); | ||
const currentAppComponent = appConfig.appComponents.find((appComponent) => { | ||
return appComponent.location === appLocation.location && (!appLocation.name || appComponent.name === appLocation.name); | ||
}); | ||
const closeFlyout = ({ fieldName, fieldID, params }) => { | ||
const location = APP_LOCATION_CUSTOM_FIELD; | ||
const messageID = getMessageID({ location, fieldID, fieldName}) | ||
if (window.parent) { | ||
window.parent.postMessage({ | ||
message: { | ||
location, | ||
fieldName, | ||
fieldID, | ||
params | ||
}, | ||
type: `closeFlyout_for_${messageID}` | ||
}, "*") | ||
if(currentAppComponent) { | ||
return currentAppComponent.componentToRender; | ||
} else { | ||
console.error("Could not render the '" + appConfig.name + "' component for '" + appLocation.location + "' with the name of '" + appLocation.name + "'"); | ||
} | ||
@@ -133,3 +87,3 @@ } | ||
const location = getUrlParameter('location'); | ||
if(location === APP_LOCATION_CUSTOM_FIELD) { | ||
if(location === locations.APP_LOCATION_CUSTOM_FIELD) { | ||
const fieldTypeName = getUrlParameter('fieldTypeName'); | ||
@@ -140,13 +94,15 @@ return { | ||
} | ||
} else if(location === APP_LOCATION_APP_CONFIG) { | ||
} else if(location === locations.APP_LOCATION_APP_CONFIG) { | ||
return { | ||
location | ||
} | ||
} else if(location === APP_LOCATION_FLYOUT) { | ||
} else if(location === locations.APP_LOCATION_FLYOUT) { | ||
const flyoutName = getUrlParameter('flyoutName'); | ||
return { | ||
location | ||
location, | ||
name: flyoutName | ||
} | ||
} else { | ||
return { | ||
location: APP_LOCATION_UNKNOWN, | ||
location: locations.APP_LOCATION_UNKNOWN, | ||
name: null | ||
@@ -157,38 +113,8 @@ }; | ||
const resolveAppComponent = (appConfig) => { | ||
const appLocation = getAppLocation(); | ||
const currentAppComponent = appConfig.appComponents.find((appComponent) => { | ||
return appComponent.location === appLocation.location && (!appLocation.name || appComponent.name === appLocation.name); | ||
}); | ||
if(currentAppComponent) { | ||
return currentAppComponent.componentToRender; | ||
} else { | ||
console.error("Could not render the '" + appConfig.name + "' component for '" + appLocation.location + "' with the name of '" + appLocation.name + "'"); | ||
} | ||
} | ||
const getUrlParameter = (name) => { | ||
//eslint-disable-next-line | ||
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); | ||
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'); | ||
var results = regex.exec(window.location.search); | ||
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' ')); | ||
}; | ||
export { | ||
setAppConfig, | ||
initializeAppConfig, | ||
initializeField, | ||
updateFieldValue, | ||
updateFieldHeight, | ||
getAppLocation, | ||
resolveAppComponent, | ||
openFlyout, | ||
closeFlyout, | ||
APP_LOCATION_CUSTOM_FIELD, | ||
APP_LOCATION_UNKNOWN, | ||
APP_LOCATION_APP_CONFIG, | ||
APP_LOCATION_FLYOUT | ||
locations | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
17073
33.12%11
37.5%245
29.63%1
Infinity%