@shopgate/engage
Version:
Shopgate's ENGAGE library.
58 lines • 9.72 kB
JavaScript
import _regeneratorRuntime from"@babel/runtime/regenerator";function _extends(){_extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};return _extends.apply(this,arguments);}function asyncGeneratorStep(gen,resolve,reject,_next,_throw,key,arg){try{var info=gen[key](arg);var value=info.value;}catch(error){reject(error);return;}if(info.done){resolve(value);}else{Promise.resolve(value).then(_next,_throw);}}function _asyncToGenerator(fn){return function(){var self=this,args=arguments;return new Promise(function(resolve,reject){var gen=fn.apply(self,args);function _next(value){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"next",value);}function _throw(err){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"throw",err);}_next(undefined);});};}import{COMMAND_GET_APP_PERMISSIONS,COMMAND_REQUEST_APP_PERMISSIONS}from'@shopgate/pwa-core/constants/AppCommands';import{PERMISSION_STATUS_NOT_DETERMINED,PERMISSION_STATUS_NOT_SUPPORTED,PERMISSION_STATUS_GRANTED,PERMISSION_STATUS_DENIED,PERMISSION_ID_LOCATION,PERMISSION_ID_PUSH,PERMISSION_ID_CAMERA}from'@shopgate/pwa-core/constants/AppPermissions';import geolocationRequestBrowser from"../classes/GeolocationRequestBrowser";var MOCK_PERMISSIONS_KEY='sg-mocked-app-permissions';var getMockedAppPermissions;/**
* In development within the browser, app permissions can be mocked by calling a global functions
* on the MockedPermissions object. The values are persisted in local storage, so that they survive
* reload of the PWA.
*
* Permissions are stored "globally" and will affect every shop.
*
* Example:
* MockedAppPermissions.setGetAppPermissionsResponse('push', 'notDetermined')
* MockedAppPermissions.setRequestAppPermissionsResponse('push', 'denied')
*
* The object also provides access to the relevant constants.
*
* Example:
* MockedAppPermissions.ID_PUSH
* MockedAppPermissions.STATUS_DENIED
*
* Complete Example:
* MockedAppPermissions.setRequestAppPermissionsResponse(
* MockedAppPermissions.ID_PUSH,
* MockedAppPermissions.STATUS_DENIED
* )
*/if(process.env.NODE_ENV==='development'){/**
* Retrieves mocked app permissions from the local storage
* @returns {Object|null}
*/getMockedAppPermissions=function getMockedAppPermissions(){return JSON.parse(localStorage.getItem(MOCK_PERMISSIONS_KEY));};/**
* Sets a single mocked app permission status from the local storage
* @param {string} commandType Command type for which the status is set (get / request)
* @param {string} permissionId The permissionId for which the status is set
* @param {string} status The mocked permission status
*/var setMockedAppPermission=function setMockedAppPermission(commandType,permissionId,status){var _current;var current=getMockedAppPermissions();if(current===null){current={};}if(!((_current=current)===null||_current===void 0?void 0:_current[permissionId])){// Add defaults for the permissionId when not set yet
current[permissionId]={get:PERMISSION_STATUS_GRANTED,request:PERMISSION_STATUS_GRANTED};}// Add the status to the mocked app permissions
current[permissionId][commandType]=status;// Update local storage
localStorage.setItem(MOCK_PERMISSIONS_KEY,JSON.stringify(current));};/**
* Prepare global object in DEV environment to mock app permissions
*/var obj={setGetAppPermissionsResponse:function setGetAppPermissionsResponse(permissionId){var status=arguments.length>1&&arguments[1]!==undefined?arguments[1]:PERMISSION_STATUS_GRANTED;setMockedAppPermission('get',permissionId,status);},setRequestAppPermissionsResponse:function setRequestAppPermissionsResponse(permissionId){var status=arguments.length>1&&arguments[1]!==undefined?arguments[1]:PERMISSION_STATUS_GRANTED;setMockedAppPermission('request',permissionId,status);}};// Populate useful constants
obj.STATUS_GRANTED=PERMISSION_STATUS_GRANTED;obj.STATUS_DENIED=PERMISSION_STATUS_DENIED;obj.STATUS_NOT_DETERMINED=PERMISSION_STATUS_NOT_DETERMINED;obj.STATUS_NOT_SUPPORTED=PERMISSION_STATUS_NOT_SUPPORTED;obj.ID_PUSH=PERMISSION_ID_PUSH;obj.ID_CAMERA=PERMISSION_ID_CAMERA;obj.ID_LOCATION=PERMISSION_ID_LOCATION;window.MockedAppPermissions=obj;}/**
* Mapping of states from the browser permissions API to app permissions.
*/var permissionsApiStateMapping={prompt:PERMISSION_STATUS_NOT_DETERMINED,granted:PERMISSION_STATUS_GRANTED,denied:PERMISSION_STATUS_DENIED};/**
* Determines geolocation permissions from the browser permissions API.
* @see https://developer.mozilla.org/en-US/docs/Web/API/Permissions_API
* @returns {string|null}
*/var getGeolocationPermissionsFromPermissionsApi=/*#__PURE__*/function(){var _ref=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(){var state,_ref2;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:_context.prev=0;_context.next=3;return navigator.permissions.query({name:'geolocation'});case 3:_ref2=_context.sent;state=_ref2.state;_context.next=9;break;case 7:_context.prev=7;_context.t0=_context["catch"](0);case 9:return _context.abrupt("return",{state:permissionsApiStateMapping[state]||null});case 10:case"end":return _context.stop();}},_callee,null,[[0,7]]);}));return function getGeolocationPermissionsFromPermissionsApi(){return _ref.apply(this,arguments);};}();/**
* Determines geolocation permissions from the browser geolocation API.
* @see https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API
* @returns {string}
*/var getGeolocationPermissionsFromGeolocationApi=/*#__PURE__*/function(){var _ref3=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(){var state,geolocation,_ref4$code,code;return _regeneratorRuntime.wrap(function _callee2$(_context2){while(1)switch(_context2.prev=_context2.next){case 0:state=PERMISSION_STATUS_GRANTED;_context2.prev=1;_context2.next=4;return geolocationRequestBrowser.dispatch();case 4:geolocation=_context2.sent;_context2.next=12;break;case 7:_context2.prev=7;_context2.t0=_context2["catch"](1);_ref4$code=_context2.t0.code;code=_ref4$code===void 0?null:_ref4$code;if(code!==null){state=PERMISSION_STATUS_DENIED;}case 12:return _context2.abrupt("return",{state:state,geolocation:geolocation});case 13:case"end":return _context2.stop();}},_callee2,null,[[1,7]]);}));return function getGeolocationPermissionsFromGeolocationApi(){return _ref3.apply(this,arguments);};}();/**
* Creates mocked app permissions to support development within browser environments.
* @param {string} commandName The name of the command for which the permissions are created
* @param {Object} [commandParams={}] The command params.
* @param {string} [fallbackStatus] The status that's returned when no status could be determined.
* @returns {Array}
*/var mockedPermissions=/*#__PURE__*/function(){var _ref5=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3(commandName){var commandParams,fallbackStatus,permissionIds,permissions,geolocationState,result,_args3=arguments;return _regeneratorRuntime.wrap(function _callee3$(_context3){while(1)switch(_context3.prev=_context3.next){case 0:commandParams=_args3.length>1&&_args3[1]!==undefined?_args3[1]:{};fallbackStatus=_args3.length>2&&_args3[2]!==undefined?_args3[2]:PERMISSION_STATUS_GRANTED;permissionIds=commandParams.permissionIds;permissions=commandParams.permissions;if(permissions){permissionIds=permissions.map(function(permission){return permission.permissionId;});}if(!permissionIds.includes(PERMISSION_ID_LOCATION)){_context3.next=14;break;}if(!(commandName===COMMAND_GET_APP_PERMISSIONS)){_context3.next=10;break;}_context3.next=9;return getGeolocationPermissionsFromPermissionsApi();case 9:geolocationState=_context3.sent;case 10:if(!(commandName===COMMAND_REQUEST_APP_PERMISSIONS)){_context3.next=14;break;}_context3.next=13;return getGeolocationPermissionsFromGeolocationApi();case 13:geolocationState=_context3.sent;case 14:result=permissionIds.map(function(permissionId){var _geolocationState;var state=fallbackStatus;var geolocation;var useLocalStorageMock=false;// Use mocked permissions from local storage if available
if(typeof getMockedAppPermissions==='function'){var _permissionMock$permi;var permissionMock=getMockedAppPermissions();var type=commandName===COMMAND_GET_APP_PERMISSIONS?'get':'request';var mockValue=permissionMock===null||permissionMock===void 0?void 0:(_permissionMock$permi=permissionMock[permissionId])===null||_permissionMock$permi===void 0?void 0:_permissionMock$permi[type];if(mockValue){useLocalStorageMock=true;state=mockValue||PERMISSION_STATUS_GRANTED;}}if(!useLocalStorageMock&&permissionId===PERMISSION_ID_LOCATION&&((_geolocationState=geolocationState)===null||_geolocationState===void 0?void 0:_geolocationState.state)){var _geolocationState2=geolocationState;state=_geolocationState2.state;geolocation=_geolocationState2.geolocation;}return _extends({permissionId:permissionId,status:state},geolocation?{data:geolocation}:{});});return _context3.abrupt("return",result);case 16:case"end":return _context3.stop();}},_callee3);}));return function mockedPermissions(_x){return _ref5.apply(this,arguments);};}();/**
* Creates a function to mock app permissions.
* @param {string} [fallbackStatus] The status that's returned when no status could be determined.
* @returns {Function}
*/export var createMockedPermissions=function createMockedPermissions(){var fallbackStatus=arguments.length>0&&arguments[0]!==undefined?arguments[0]:PERMISSION_STATUS_GRANTED;return function(commandName,commandParams){return mockedPermissions(commandName,commandParams,fallbackStatus);};};