UNPKG

@shopgate/engage

Version:
8 lines 2.2 kB
function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor;}import{GEOLOCATION_ERROR_UNKNOWN,GEOLOCATION_ERROR_DENIED,GEOLOCATION_ERROR_UNAVAILABLE,GEOLOCATION_ERROR_TIMEOUT,GEOLOCATION_DEFAULT_TIMEOUT}from"../constants/geolocationRequest";var errorMapping=[GEOLOCATION_ERROR_UNKNOWN,GEOLOCATION_ERROR_DENIED,GEOLOCATION_ERROR_UNAVAILABLE,GEOLOCATION_ERROR_TIMEOUT];/* eslint-disable class-methods-use-this */ /** * The GeolocationRequestBrowser class enables to retrieve the current geolocation of the device. */var GeolocationRequestBrowser=/*#__PURE__*/function(){function GeolocationRequestBrowser(){_classCallCheck(this,GeolocationRequestBrowser);}return _createClass(GeolocationRequestBrowser,[{key:"dispatch",value:/** * Dispatches the request. If no further provisions are made, it will trigger the geolocation * permission dialog if permissions are not already granted. * @param {number} [timeout=GEOLOCATION_DEFAULT_TIMEOUT] Timeout in ms for the request. * @returns {Promise} */function dispatch(){var timeout=arguments.length>0&&arguments[0]!==undefined?arguments[0]:GEOLOCATION_DEFAULT_TIMEOUT;return new Promise(function(resolve,reject){navigator.geolocation.getCurrentPosition(function(position){var coords=position.coords;resolve({accuracy:coords.accuracy,latitude:coords.latitude,longitude:coords.longitude});},function(_ref){var code=_ref.code,message=_ref.message;var error=new Error(message);error.code=errorMapping[code];reject(error);},{timeout:timeout});});}}]);}();export{GeolocationRequestBrowser};/* eslint-enable class-methods-use-this */export default new GeolocationRequestBrowser();