Created
January 30, 2014 20:00
-
-
Save miketaylr/8717560 to your computer and use it in GitHub Desktop.
mqm.Util
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function() { | |
| Util = { | |
| isDialogShowing: false, | |
| isInterstitialShowing: false, | |
| navigate: function(b, a) { | |
| a = a || {}; | |
| if (a.newTab) { | |
| window.open(b); | |
| } else { | |
| document.location.href = b; | |
| } | |
| }, | |
| capitalize: function(a) { | |
| return a.substr(0, 1).toUpperCase() + a.substr(1); | |
| }, | |
| hashToParams: function(d) { | |
| var b = {}; | |
| for (var a in d) { | |
| if (typeof d[a] !== "undefined") { | |
| b[a] = d[a]; | |
| } | |
| } | |
| var c = $.param(b); | |
| c = c.replace(/'/, "%27"); | |
| c = c.replace(/\(/, "%28"); | |
| c = c.replace(/\)/, "%29"); | |
| return c; | |
| }, | |
| getFriendlyDistance: function(d, b) { | |
| if (b) { | |
| if (b.toUpperCase() == "M") { | |
| b = "mi"; | |
| } | |
| if (b.toUpperCase() == "K") { | |
| b = "km"; | |
| } | |
| } else { | |
| b = ""; | |
| } | |
| var a = d + b; | |
| if (d) { | |
| var c = (d + "").split("."); | |
| if (c.length != 2) { | |
| return d; | |
| } | |
| if (c[1].length > 1) { | |
| a = Number("" + c[0] + "." + c[1].substr(0, 2)) + " " + b; | |
| } else { | |
| return a; | |
| } | |
| } | |
| return a; | |
| }, | |
| getFriendlyTime: function(e) { | |
| var f = [ "" ]; | |
| if (e) { | |
| var d = e.split(":"); | |
| if (d.length != 3) { | |
| return e; | |
| } | |
| if (d[0] != "00") { | |
| var b = Number(d[0]) + ""; | |
| f.push(b); | |
| f.push(b == "1" ? " " + messages.hourAbbreviated + " " : " " + messages.hoursAbbreviated + " "); | |
| } | |
| if (d[1] != "00") { | |
| var c = Number(d[1]) + ""; | |
| f.push(c); | |
| f.push(c == "1" ? " " + messages.minuteAbbreviated : " " + messages.minutesAbbreviated); | |
| } | |
| if (f.length == 1) { | |
| var a = Number(d[2]) + ""; | |
| f.push(a); | |
| f.push(a == "1" ? " " + messages.secondAbbreviated : " " + messages.secondsAbbreviated); | |
| } | |
| } | |
| return f.join(""); | |
| }, | |
| setCookie: function(b, d, f) { | |
| var c = new Date(), e = 1e3 * 60 * 60 * 24, a = "" + b + "=" + escape(d); | |
| if (f == null || f == 0) { | |
| f = 365 * 5; | |
| } | |
| if (f) { | |
| c.setTime(c.getTime() + e * f); | |
| a += ";expires=" + c.toGMTString(); | |
| } | |
| document.cookie = a; | |
| }, | |
| getCookie: function(a) { | |
| var b = null; | |
| $(document.cookie.split(";")).each(function(d, e) { | |
| e = e.split("="); | |
| if (e.length == 2) { | |
| var c = e[0].replace(/^\s+|\s+$/g, ""); | |
| if (a == c) { | |
| b = e[1]; | |
| return; | |
| } | |
| } | |
| }); | |
| return unescape(b); | |
| }, | |
| getSingleLineAddress: function(f) { | |
| if (!f || !f.address) { | |
| return ""; | |
| } | |
| var g = f.address.street, e = f.address.locality, b = f.address.region, d = f.address.country, a = ""; | |
| if (g != null && g != "") { | |
| a = g; | |
| if (e != null && e != "") { | |
| a += ", "; | |
| a += e; | |
| } | |
| if (b != null && b != "") { | |
| a += ", "; | |
| a += b; | |
| } | |
| } else { | |
| if (e != null && e != "") { | |
| a = e; | |
| if (b != null && b != "") { | |
| a += ", " + b; | |
| } | |
| } else { | |
| if (b != null && b != "") { | |
| a = b; | |
| } | |
| } | |
| } | |
| var c = f.address.postalCode; | |
| if (c != null && c != "") { | |
| if (a != "") { | |
| a += " "; | |
| } | |
| a += c; | |
| } | |
| if (a == "") { | |
| if (d != null && d != "") { | |
| a += d; | |
| } else { | |
| if (f.userInput != null && f.userInput != "") { | |
| a += f.userInput; | |
| } | |
| } | |
| } | |
| return a; | |
| }, | |
| setPrimaryAndSecondaryStrings: function(c) { | |
| var b, a; | |
| c.singleLine = this.getSingleLineAddress(c); | |
| if (c.name) { | |
| b = c.name; | |
| if (c.address) { | |
| if (c.address.street) { | |
| a = c.address.street; | |
| } else { | |
| if (c.address.locality) { | |
| a = c.address.locality; | |
| if (c.address.region) { | |
| a += ", " + c.address.region; | |
| } | |
| } else { | |
| if (c.address.region) { | |
| a = c.address.region; | |
| } else { | |
| if (c.address.postalCode) { | |
| a = c.address.postalCode; | |
| } else { | |
| if (c.address.country) { | |
| a = c.address.country; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } else { | |
| if (c.address) { | |
| if (c.address.street) { | |
| b = c.address.street; | |
| if (c.address.locality) { | |
| a = c.address.locality; | |
| if (c.address.region) { | |
| a += ", " + c.address.region; | |
| } | |
| } else { | |
| if (c.address.state) { | |
| a = c.address.state; | |
| if (c.address.postalCode) { | |
| a += ", " + c.address.postalCode; | |
| } | |
| } else { | |
| if (c.address.region) { | |
| a = c.address.region; | |
| } else { | |
| if (c.address.postalCode) { | |
| a = c.address.postalCode; | |
| } else { | |
| if (c.address.country) { | |
| a = c.address.country; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } else { | |
| if (c.address.locality) { | |
| b = c.address.locality; | |
| if (c.address.region) { | |
| b += ", " + c.address.region; | |
| } | |
| if (c.address.country) { | |
| a = c.address.country; | |
| } | |
| } else { | |
| if (c.address.region) { | |
| b = c.address.region; | |
| if (c.address.country) { | |
| a = c.address.country; | |
| } | |
| } else { | |
| if (c.address.postalCode) { | |
| b = c.address.postalCode; | |
| if (c.address.country) { | |
| a = c.address.country; | |
| } | |
| } else { | |
| if (c.address.country) { | |
| b = c.address.country; | |
| } else { | |
| if (c.address.latLng) { | |
| b = c.address.latLng.lat + ", " + c.address.latLng.lng; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| c.primaryString = b; | |
| c.secondaryString = a; | |
| }, | |
| showDialog: function(d) { | |
| var b = this, e = $("#modalDialog .messageText"), a = $("#modalDialog .positive"), c = $("#modalDialog .negative"); | |
| if (!d) { | |
| d = { | |
| showNegative: true, | |
| showPositive: true | |
| }; | |
| } | |
| $("#modalDialog .positive").unbind("click"); | |
| $("#modalDialog .negative").unbind("click"); | |
| e.html(d.message ? d.message : ""); | |
| a.html(d.positive ? d.positive : messages.ok); | |
| c.html(d.negative ? d.negative : messages.cancel); | |
| if (d.positiveCallback) { | |
| a.click(d.positiveCallback); | |
| } | |
| if (d.negativeCallback) { | |
| c.click(d.negativeCallback); | |
| } | |
| if (!d.showNegative) { | |
| c.hide(); | |
| } else { | |
| c.show(); | |
| } | |
| if (!d.showPositive) { | |
| a.hide(); | |
| } else { | |
| a.show(); | |
| } | |
| $("#modalDialog").modal({ | |
| onClose: function() { | |
| b.isDialogShowing = false; | |
| $.modal.close(); | |
| } | |
| }); | |
| if (d.height) { | |
| $("#simplemodal-container").css("height", d.height); | |
| } | |
| if (d.width) { | |
| $("#simplemodal-container").css("width", d.width); | |
| } | |
| b.isDialogShowing = true; | |
| }, | |
| refreshDialog: function(c, d) { | |
| var b = $("#simplemodal-container"), a = d ? d : window.innerHeight ? window.innerHeight : $(window).height(), g = c ? c : window.innerWidth ? window.innerWidth : $(window).width(), f = (a - b.height()) / 2, e = (g - b.width()) / 2; | |
| b.css({ | |
| top: f, | |
| left: e | |
| }); | |
| $("#simplemodal-overlay").css({ | |
| width: g, | |
| height: a | |
| }); | |
| }, | |
| retryDialog: function(a, b) { | |
| Util.showDialog({ | |
| message: a, | |
| positive: messages.retry, | |
| negative: messages.cancel, | |
| positiveCallback: b, | |
| showPositive: true, | |
| showNegative: true, | |
| height: "100px" | |
| }); | |
| }, | |
| closeDialog: function() { | |
| $.modal.close(); | |
| }, | |
| calculateRouteIndex: function(h, g) { | |
| var a = 0, k = 0, f = 0, d = 0, e = g.length - 1; | |
| for (var c = 0; c < e; c++) { | |
| f += Util.distanceTo(g[c].address.latLng, g[c + 1].address.latLng); | |
| } | |
| k = f + Util.distanceTo(h.address.latLng, g[0].address.latLng); | |
| for (var b = 0; b < e; b++) { | |
| d = f; | |
| d -= Util.distanceTo(g[b].address.latLng, g[b + 1].address.latLng); | |
| d += Util.distanceTo(g[b].address.latLng, h.address.latLng); | |
| d += Util.distanceTo(h.address.latLng, g[b + 1].address.latLng); | |
| if (d < k) { | |
| a = b + 1; | |
| k = d; | |
| } | |
| } | |
| d = f; | |
| d += Util.distanceTo(h.address.latLng, g[e].address.latLng); | |
| if (d < k) { | |
| a = g.length; | |
| } | |
| return a; | |
| }, | |
| distanceTo: function(j, i) { | |
| var k, h, g, b, d = .01745329251994, e = 3.141592653589793, f = 3963.205; | |
| if (!j || !i) { | |
| return 0; | |
| } | |
| if (j.lat == i.lat && j.lng == i.lng) { | |
| return 0; | |
| } | |
| k = i.lng - j.lng; | |
| h = d * (90 - j.lat); | |
| g = d * (90 - i.lat); | |
| b = Math.cos(h) * Math.cos(g) + Math.sin(h) * Math.sin(g) * Math.cos(d * k); | |
| if (b < -1) { | |
| return e * f; | |
| } else { | |
| if (b >= 1) { | |
| return 0; | |
| } | |
| } | |
| return Math.acos(b) * f; | |
| }, | |
| bearing: function(g, e) { | |
| var f = g.lat * (Math.PI / 180), d = e.lat * (Math.PI / 180), c = e.lng * (Math.PI / 180) - g.lng * (Math.PI / 180), h = Math.sin(c) * Math.cos(d), a = Math.cos(f) * Math.sin(d) - Math.sin(f) * Math.cos(d) * Math.cos(c), b = Math.atan2(h, a) * (180 / Math.PI); | |
| if (b < 0) { | |
| b += 360; | |
| } | |
| return b; | |
| }, | |
| formatPoiList: function(e, d) { | |
| var b = [], c, a; | |
| b.push('<div class="poiPrimary">'); | |
| b.push(e.primaryString); | |
| b.push('</div><div class="poiSecondary">'); | |
| if (d) { | |
| b.push(e.address.county); | |
| } else { | |
| b.push(e.address.singleLineAddress); | |
| } | |
| b.push("</div>"); | |
| if (e.gasPrices && e.gasPrices.length > 0) { | |
| b.push(this.formatGasPrice(e.lowGasPrice)); | |
| } | |
| if (e.rating) { | |
| b.push(this.formatStars(e.rating)); | |
| } | |
| return b.join(""); | |
| }, | |
| formatStars: function(b) { | |
| var a = []; | |
| if (b) { | |
| stars = 76 * b / 10; | |
| a.push('<div class="sprite ratingBackground"></div>'); | |
| a.push('<div class="sprite rating" style="width:' + stars + 'px"></div>'); | |
| } | |
| return a.join(""); | |
| }, | |
| formatGasPrice: function(b) { | |
| var a = []; | |
| a.push('<div class="poiGas">'); | |
| a.push(b.type + ': <b><span class="poiGasPrice">$'); | |
| a.push((b.amount - .005).toFixed(2)); | |
| a.push('</span><span class="poiGasSuper">9</span></b>'); | |
| a.push("</div>"); | |
| return a.join(""); | |
| }, | |
| formatGasPriceDetails: function(c) { | |
| var b = [], a = this; | |
| b.push("<table><tbody>"); | |
| b.push("<tr><td><b>" + messages.gasPrices + '</b></td><td class="detailsGasDate">' + messages.lastUpdated + "</td></tr>"); | |
| $.each(c.gasPrices, function(d, e) { | |
| b.push("<tr><td>"); | |
| b.push(a.formatGasPrice(e)); | |
| b.push('</td><td class="detailsGasDate">'); | |
| b.push(e.updatedDate); | |
| b.push("</td></tr>"); | |
| }); | |
| b.push("</tbody></table>"); | |
| return b.join(""); | |
| }, | |
| formatPoiBubble: function(d, b) { | |
| var c = [], a; | |
| if (!b) { | |
| c.push('<table class="poiTable"><tbody><tr><td>'); | |
| } | |
| c.push("<div>"); | |
| c.push('<div><div class="poiPrimary">'); | |
| if (d.primaryString && d.primaryString.length > 20) { | |
| c.push(d.primaryString.substring(0, 19) + "..."); | |
| } else { | |
| c.push(d.primaryString); | |
| } | |
| c.push('</div><div class="poiSecondary">'); | |
| c.push(d.secondaryString); | |
| c.push("</div>"); | |
| if (d.gasPrices && d.gasPrices.length > 0) { | |
| c.push(this.formatGasPrice(d.lowGasPrice)); | |
| } | |
| if (d.rating) { | |
| a = 76 * d.rating / 10; | |
| c.push('<div class="sprite ratingBackground"></div>'); | |
| c.push('<div class="sprite rating" style="width:' + a + 'px"></div>'); | |
| } | |
| c.push("</div>"); | |
| if (!b) { | |
| c.push('</div></td><td class="poiGoButton"><span>GO</span></td></tr></tbody></table>'); | |
| } else { | |
| c.push("</div>"); | |
| } | |
| return c.join(""); | |
| }, | |
| randomBetween: function(b, a) { | |
| return Math.floor(Math.random() * (a - b + 1) + b); | |
| }, | |
| rotateImage: function(a, c) { | |
| var b = { | |
| "-moz-transform": "rotate(" + c + "deg)", | |
| "-webkit-transform": "rotate(" + c + "deg)", | |
| "-o-transform": "rotate(" + c + "deg)" | |
| }; | |
| $(a).css(b); | |
| }, | |
| checkForLatLngInput: function(a) { | |
| if (!a) { | |
| return; | |
| } | |
| var f = null, e, c, d, b, g = /^([+-]?(((\d+(\.)?)|(\d*\.\d+))([eE][+-]?\d+)?))$/; | |
| if (a.indexOf(",") != -1) { | |
| if (a.match("^loc:")) { | |
| a = a.substr(4); | |
| } | |
| d = a.split(","); | |
| if (d != null && d.length == 2) { | |
| e = d[0].trim(); | |
| c = d[1].trim(); | |
| if (c.indexOf("(")) { | |
| b = c.substr(c.indexOf("(")); | |
| c.substr(0, c.indexOf("(")); | |
| } | |
| if (!e || !c) { | |
| return; | |
| } | |
| if (!g.test(e + "") || !g.test(c)) { | |
| return; | |
| } | |
| if (e > 90 || e < -90 || c > 180 || c < -180) { | |
| return; | |
| } | |
| if (!b) { | |
| b = e + "," + c; | |
| } | |
| f = { | |
| primaryString: b, | |
| status: "RESOLVED", | |
| address: { | |
| quality: "ZIP", | |
| latLng: { | |
| lat: e, | |
| lng: c | |
| } | |
| } | |
| }; | |
| } | |
| } | |
| return f; | |
| }, | |
| isEmpty: function(a) { | |
| if (a && a !== "") { | |
| return false; | |
| } | |
| return true; | |
| }, | |
| isNotEmpty: function(a) { | |
| return !this.isEmpty(a); | |
| }, | |
| supportsLocalStorage: function() { | |
| try { | |
| return "localStorage" in window && window.localStorage !== null; | |
| } catch (a) { | |
| return false; | |
| } | |
| }, | |
| getPixelDpi: function() { | |
| if (window.matchMedia) { | |
| if (window.matchMedia("-webkit-min-device-pixel-ratio: 2")) { | |
| return "xhdpi"; | |
| } else { | |
| if (window.matchMedia("(-webkit-min-device-pixel-ratio: 1.5) and (-webkit-max-device-pixel-ratio: 1.99)")) { | |
| return "hdpi"; | |
| } | |
| } | |
| } | |
| return "mdpi"; | |
| }, | |
| userAgent: function() { | |
| return navigator.userAgent; | |
| }, | |
| isIOS: function() { | |
| var a = mqm.Util.userAgent().toLowerCase(); | |
| return a.indexOf("iphone") > -1 || a.indexOf("ipad") > -1 || a.indexOf("ipod") > -1; | |
| }, | |
| isAndroid: function() { | |
| var a = mqm.Util.userAgent().toLowerCase(); | |
| return a.indexOf("android") > -1; | |
| }, | |
| isIOS5: function() { | |
| return this.isIOS() && this.iosVersion().major == 5; | |
| }, | |
| iosVersion: function() { | |
| var b = mqm.Util.userAgent().toLowerCase(); | |
| var a = b.match(/os ([0-9])_([0-9])/); | |
| return { | |
| major: parseInt(a[1], 10), | |
| minor: parseInt(a[2], 10) | |
| }; | |
| }, | |
| deviceId: _.once(function() { | |
| return mqm.Util.userAgent().match(/\((.*?)\)/)[1]; | |
| }), | |
| findPoi: function(a) { | |
| var c = mqm.mapController.map, b, d; | |
| if (c) { | |
| b = c.getShapeCollection(a.overlayKey); | |
| } | |
| if (b && b.items) { | |
| $(b.items).each(function(e, f) { | |
| if (f && f.location && f.location.id) { | |
| if (f.location.id == a.id) { | |
| d = f; | |
| return; | |
| } | |
| } | |
| }); | |
| } | |
| return d; | |
| }, | |
| track: function(a) { | |
| if (a === undefined) { | |
| return; | |
| } | |
| new EventTracker(a).record(); | |
| }, | |
| impression: function(a) { | |
| new EventTracker(a).record(); | |
| }, | |
| getCarouselItemLabel: function(c) { | |
| if (this.isCarouselItem(c)) { | |
| var b = mqm.mapController.carouselManager.getItemByKey(c.overlayKey.replace("_OVERLAY", "").toLowerCase()).carouselLabel; | |
| if (b) { | |
| var a = b.split("_"); | |
| return "_" + a[a.length - 1]; | |
| } | |
| } | |
| return ""; | |
| }, | |
| isCarouselItem: function(a) { | |
| return !!a.overlayKey && a.overlayKey !== "SEARCH_RESULTS_OVERLAY"; | |
| }, | |
| getCarouselItem: function(a) { | |
| if (this.isCarouselItem(a)) { | |
| return mqm.mapController.carouselManager.getItemByKey(a.overlayKey.replace("_OVERLAY", "").toLowerCase()); | |
| } else { | |
| return null; | |
| } | |
| }, | |
| validateDate: function(c) { | |
| var a, d, b = new Date(); | |
| if (c.startDate) { | |
| a = new Date(c.startDate); | |
| } else { | |
| a = new Date(); | |
| a.setDate(b.getDate() - 1); | |
| } | |
| if (c.endDate) { | |
| d = new Date(c.endDate); | |
| } else { | |
| d = new Date(); | |
| d.setDate(b.getDate() + 1); | |
| } | |
| return a < b && d > b ? true : false; | |
| }, | |
| getUID: function() { | |
| var a, b = this.getCookie("dev_id"); | |
| if (b && b != "null") { | |
| return b; | |
| } else { | |
| a = this.getNewUID(); | |
| this.setCookie("dev_id", a); | |
| return a; | |
| } | |
| }, | |
| getNewUID: function() { | |
| var a = "", b; | |
| for (b = 0; b < 32; b++) { | |
| if (b == 8 || b == 12 || b == 16 || b == 20) { | |
| a = a + "-"; | |
| } | |
| a = a + Math.floor(Math.random() * 16).toString(16).toUpperCase(); | |
| } | |
| return a; | |
| }, | |
| isTablet: function() { | |
| if (!_USE_TABLET_VIEW) { | |
| return false; | |
| } | |
| var a = window.innerWidth ? window.innerWidth : $(window).width(); | |
| if (a > 640) { | |
| return true; | |
| } else { | |
| return false; | |
| } | |
| }, | |
| getParameterByName: function(a) { | |
| var b = RegExp("[?&]" + a + "=([^&]*)").exec(window.location.search); | |
| return b && decodeURIComponent(b[1].replace(/\+/g, " ")); | |
| }, | |
| isSendToMobile: function() { | |
| var b = navigator.userAgent.toLowerCase(), a = this.getParameterByName("hk"), c = location.pathname != null && location.pathname.length >= 3 && location.pathname.substring(0, 3) == "/h/"; | |
| if (!_USE_SEND_TO_APP) { | |
| return false; | |
| } | |
| if ((c || a != null && a.length > 0) && (b.indexOf("android") > -1 || b.indexOf("iphone") > -1 || b.indexOf("ipad") > -1 || b.indexOf("ipod") > -1)) { | |
| return true; | |
| } | |
| return false; | |
| }, | |
| showSendToApp: function(b, a) { | |
| var d = [], c = false; | |
| $(window).on("pagehide", function() { | |
| d.map(window.clearTimeout); | |
| }); | |
| window.location = b; | |
| d.push(window.setTimeout(function() { | |
| d.splice(0); | |
| c = true; | |
| window.location = a; | |
| }, 250)); | |
| d.push(window.setTimeout(function() { | |
| c && window.location.reload(); | |
| }, 500)); | |
| }, | |
| tryCall: function(c, b, a) { | |
| if (_.isFunction(c[b])) { | |
| return c[b].apply(c, a); | |
| } | |
| }, | |
| locationHref: function() { | |
| return window.location.href; | |
| }, | |
| isBlank: function(a) { | |
| return !(a && a != ""); | |
| }, | |
| htmlDecode: function(a) { | |
| if (a.indexOf("<") > -1 && a.indexOf(">") > -1) { | |
| a = $("<div/>").html(a).text(); | |
| } | |
| return a; | |
| } | |
| }; | |
| window.mqm.Util = Util; | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment