1 2/** 3 * Service that provides wrappers for geolocation API 4 * @module locationService 5 * @param {jQuery} $ Instance of jQuery 6 * @return {Object} List of location service methods 7 */ 8XA.component.locationService = (function ($, document) { 9 10 "use strict"; 11 /** 12 * This object stores all public api methods. 13 * @type {Object.<Methods>} 14 * @memberOf module:locationService 15 */ 16 var api = {}, 17 getCurrentLocation, 18 getLocationOptions, 19 detectBrowser, 20 gettingLocation = false, 21 errorCallbacks = [], 22 successCallbacks = [], 23 reportError; 24 25 /** 26 * getCurrentLocation method call geolocation.getCurrentPosition API and call callback functions 27 * @memberOf module:locationService 28 * @param {Function} success function callback that calls in case of positive answer from geolocation API 29 * @param {Function} error function callback that calls in case of error answer from geolocation API 30 * @alias module:locationService.getCurrentLocation 31 * @private 32 */ 33 getCurrentLocation = function (success, error) { 34 var i; 35 36 errorCallbacks.push(error); 37 successCallbacks.push(success); 38 39 if (gettingLocation) { 40 //process for getting current location started already, just subscribe for proper event and wait 41 return; 42 } 43 gettingLocation = true; 44 if (navigator.geolocation) { 45 navigator.geolocation.getCurrentPosition( 46 function (position) { 47 for (i = 0; i < successCallbacks.length; i++) { 48 successCallbacks[i]([position.coords.latitude, position.coords.longitude]); 49 } 50 gettingLocation = false; 51 }, 52 function (error) { 53 reportError("Error while detecting user location"); 54 gettingLocation = false; 55 }, 56 getLocationOptions() 57 ); 58 } else { 59 reportError("Your browser does not support geolocation"); 60 gettingLocation = false; 61 } 62 }; 63 /** 64 * getLocationOptions method return option for location search based on browser 65 * @memberOf module:locationService 66 * @alias module:locationService.getLocationOptions 67 * @returns {Object} list of option for location search 68 * @private 69 */ 70 getLocationOptions = function () { 71 var browser = detectBrowser(); 72 if (browser.indexOf("Chrome") !== -1) { 73 return { 74 enableHighAccuracy: true, 75 timeout: 10000, 76 maximumAge: 0 77 }; 78 } else { 79 return { 80 timeout: 1000, 81 maximumAge: Infinity 82 }; 83 } 84 }; 85 /** 86 * detectBrowser method detects browser name and version 87 * @memberOf module:locationService 88 * @alias module:locationService.detectBrowser 89 * @returns {String} returns browser name and its version 90 * @private 91 */ 92 detectBrowser = function () { 93 var ua = navigator.userAgent, 94 tem, 95 M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || []; 96 97 if (/trident/i.test(M[1])) { 98 tem = /\brv[ :]+(\d+)/g.exec(ua) || []; 99 return "IE " + (tem[1] || ""); 100 } 101 if (M[1] === "Chrome") { 102 tem = ua.match(/\b(OPR|Edge)\/(\d+)/); 103 if (tem != null) { 104 return tem.slice(1).join(" ").replace("OPR", "Opera"); 105 } 106 } 107 M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, "-?"]; 108 if ((tem = ua.match(/version\/(\d+)/i)) != null) { 109 M.splice(1, 1, tem[1]); 110 } 111 return M.join(" "); 112 }; 113 /** 114 * reportError method adds error message to errorCallbacks array 115 * @memberOf module:locationService 116 * @param {String} errorMessage text of appeared error 117 * @alias module:locationService.reportError 118 * @private 119 */ 120 reportError = function (errorMessage) { 121 var i; 122 for (i = 0; i < errorCallbacks.length; i++) { 123 if (typeof errorCallbacks[i] === "function") { 124 errorCallbacks[i](errorMessage); 125 } 126 } 127 }; 128 /** 129 * detectLocation method call 130 * ["getCurrentLocation"]{@link module:locationService.getCurrentLocation} method 131 * to get user location. 132 * @memberOf module:locationService 133 * @param {Function} success function callback that calls in case of positive answer from geolocation API 134 * @param {Function} error function callback that calls in case of error answer from geolocation API 135 * @alias module:locationService.detectLocation 136 */ 137 api.detectLocation = function (success, error) { 138 getCurrentLocation(success, error); 139 }; 140 141 return api; 142 143}(jQuery, document)); 144 145XA.register("locationService", XA.component.locationService);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.