// UTILS + MISC /////////////////////////////////////////////////////// window.aboutIITC = function() { var v = (script_info.script && script_info.script.version || script_info.dateTimeVersion) + ' ['+script_info.buildName+']'; if (typeof android !== 'undefined' && android && android.getVersionName) { v += '[IITC Mobile '+android.getVersionName()+']'; } var plugins = ''; var attrib = '@@INCLUDEMD:ATTRIBUTION.md@@'; var contrib = '@@INCLUDEMD:CONTRIBS.md@@' var a = '' + '
About IITC
' + '
Ingress Intel Total Conversion
' + '
' + '
' + ' IITC Homepage
' + ' On the script’s homepage you can:' + ' ' + '
' + '
' + ' MapQuest OSM tiles Courtesy of MapQuest ' + '
' + '
' + '
Version: ' + v + '
' + '
Plugins: ' + plugins + '
' + '
' + '
' + attrib + '
' + '
' + '
' + contrib + '
'; dialog({ title: 'IITC ' + v, html: a, dialogClass: 'ui-dialog-aboutIITC' }); } window.layerGroupLength = function(layerGroup) { var layersCount = 0; var layers = layerGroup._layers; if (layers) layersCount = Object.keys(layers).length; return layersCount; } // retrieves parameter from the URL?query=string. window.getURLParam = function(param) { var items = window.location.search.substr(1).split('&'); if (items == "") return ""; for (var i=0; i=0; i--){ C = c[i].split('='); cookies[C[0]] = unescape(C[1]); } return cookies[name]; } window.writeCookie = function(name, val) { var d = new Date(Date.now() + 10 * 365 * 24 * 60 * 60 * 1000).toUTCString(); document.cookie = name + "=" + val + '; expires='+d+'; path=/'; } window.eraseCookie = function(name) { document.cookie = name + '=; expires=Thu, 1 Jan 1970 00:00:00 GMT; path=/'; } //certain values were stored in cookies, but we're better off using localStorage instead - make it easy to convert window.convertCookieToLocalStorage = function(name) { var cookie=readCookie(name); if(cookie !== undefined) { console.log('converting cookie '+name+' to localStorage'); if(localStorage[name] === undefined) { localStorage[name] = cookie; } eraseCookie(name); } } // add thousand separators to given number. // http://stackoverflow.com/a/1990590/1684530 by Doug Neiner. window.digits = function(d) { // U+2009 - Thin Space. Recommended for use as a thousands separator... // https://en.wikipedia.org/wiki/Space_(punctuation)#Table_of_spaces return (d+"").replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1 "); } window.zeroPad = function(number,pad) { number = number.toString(); var zeros = pad - number.length; return Array(zeros>0?zeros+1:0).join("0") + number; } // converts javascript timestamps to HH:mm:ss format if it was today; // otherwise it returns YYYY-MM-DD window.unixTimeToString = function(time, full) { if(!time) return null; var d = new Date(typeof time === 'string' ? parseInt(time) : time); var time = d.toLocaleTimeString(); // var time = zeroPad(d.getHours(),2)+':'+zeroPad(d.getMinutes(),2)+':'+zeroPad(d.getSeconds(),2); var date = d.getFullYear()+'-'+zeroPad(d.getMonth()+1,2)+'-'+zeroPad(d.getDate(),2); if(typeof full !== 'undefined' && full) return date + ' ' + time; if(d.toDateString() == new Date().toDateString()) return time; else return date; } // converts a javascript time to a precise date and time (optionally with millisecond precision) // formatted in ISO-style YYYY-MM-DD hh:mm:ss.mmm - but using local timezone window.unixTimeToDateTimeString = function(time, millisecond) { if(!time) return null; var d = new Date(typeof time === 'string' ? parseInt(time) : time); return d.getFullYear()+'-'+zeroPad(d.getMonth()+1,2)+'-'+zeroPad(d.getDate(),2) +' '+zeroPad(d.getHours(),2)+':'+zeroPad(d.getMinutes(),2)+':'+zeroPad(d.getSeconds(),2)+(millisecond?'.'+zeroPad(d.getMilliseconds(),3):''); } window.unixTimeToHHmm = function(time) { if(!time) return null; var d = new Date(typeof time === 'string' ? parseInt(time) : time); var h = '' + d.getHours(); h = h.length === 1 ? '0' + h : h; var s = '' + d.getMinutes(); s = s.length === 1 ? '0' + s : s; return h + ':' + s; } window.formatInterval = function(seconds,maxTerms) { var d = Math.floor(seconds / 86400); var h = Math.floor((seconds % 86400) / 3600); var m = Math.floor((seconds % 3600) / 60); var s = seconds % 60; var terms = []; if (d > 0) terms.push(d+'d'); if (h > 0) terms.push(h+'h'); if (m > 0) terms.push(m+'m'); if (s > 0 || terms.length==0) terms.push(s+'s'); if (maxTerms) terms = terms.slice(0,maxTerms); return terms.join(' '); } window.rangeLinkClick = function() { if(window.portalRangeIndicator) window.map.fitBounds(window.portalRangeIndicator.getBounds()); if(window.isSmartphone()) window.show('map'); } window.showPortalPosLinks = function(lat, lng, name) { var encoded_name = 'undefined'; if(name !== undefined) { encoded_name = encodeURIComponent(name); } if (typeof android !== 'undefined' && android && android.intentPosLink) { android.intentPosLink(lat, lng, map.getZoom(), name, true); } else { var qrcode = '
'; var script = ''; var gmaps = 'Google Maps'; var bingmaps = 'Bing Maps'; var osm = 'OpenStreetMap'; var latLng = '<' + lat + ',' + lng +'>'; dialog({ html: '
' + qrcode + script + gmaps + '; ' + bingmaps + '; ' + osm + '
' + latLng + '
', title: name, id: 'poslinks' }); } } window.isTouchDevice = function() { return 'ontouchstart' in window // works on most browsers || 'onmsgesturechange' in window; // works on ie10 }; window.androidCopy = function(text) { if(typeof android === 'undefined' || !android || !android.copy) return true; // i.e. execute other actions else android.copy(text); return false; } window.androidPermalink = function() { if(typeof android === 'undefined' || !android || !android.intentPosLink) return true; // i.e. execute other actions var center = map.getCenter(); android.intentPosLink(center.lat, center.lng, map.getZoom(), "Selected map view", false); return false; } window.getMinPortalLevel = function() { var z = map.getZoom(); z = getDataZoomForMapZoom(z); return getMapZoomTileParameters(z).level; } // returns number of pixels left to scroll down before reaching the // bottom. Works similar to the native scrollTop function. window.scrollBottom = function(elm) { if(typeof elm === 'string') elm = $(elm); return elm.get(0).scrollHeight - elm.innerHeight() - elm.scrollTop(); } window.zoomToAndShowPortal = function(guid, latlng) { map.setView(latlng, 17); // if the data is available, render it immediately. Otherwise defer // until it becomes available. if(window.portals[guid]) renderPortalDetails(guid); else urlPortal = guid; } window.selectPortalByLatLng = function(lat, lng) { if(lng === undefined && lat instanceof Array) { lng = lat[1]; lat = lat[0]; } else if(lng === undefined && lat instanceof L.LatLng) { lng = lat.lng; lat = lat.lat; } for(var guid in window.portals) { var latlng = window.portals[guid].getLatLng(); if(latlng.lat == lat && latlng.lng == lng) { renderPortalDetails(guid); return; } } // not currently visible urlPortalLL = [lat, lng]; map.setView(urlPortalLL, 17); }; String.prototype.capitalize = function() { return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase(); } // http://stackoverflow.com/a/646643/1684530 by Bergi and CMS if (typeof String.prototype.startsWith !== 'function') { String.prototype.startsWith = function (str){ return this.slice(0, str.length) === str; }; } // escape a javascript string, so quotes and backslashes are escaped with a backslash // (for strings passed as parameters to html onclick="..." for example) window.escapeJavascriptString = function(str) { return (str+'').replace(/[\\"']/g,'\\$&'); } //escape special characters, such as tags window.escapeHtmlSpecialChars = function(str) { var div = document.createElement(div); var text = document.createTextNode(str); div.appendChild(text); return div.innerHTML; } window.prettyEnergy = function(nrg) { return nrg> 1000 ? Math.round(nrg/1000) + ' k': nrg; } window.setPermaLink = function(elm) { var c = map.getCenter(); var lat = Math.round(c.lat*1E6)/1E6; var lng = Math.round(c.lng*1E6)/1E6; var qry = 'll='+lat+','+lng+'&z=' + map.getZoom(); $(elm).attr('href', '/intel?' + qry); } window.uniqueArray = function(arr) { return $.grep(arr, function(v, i) { return $.inArray(v, arr) === i; }); } window.genFourColumnTable = function(blocks) { var t = $.map(blocks, function(detail, index) { if(!detail) return ''; var title = detail[2] ? ' title="'+escapeHtmlSpecialChars(detail[2]) + '"' : ''; if(index % 2 === 0) return ''+detail[1]+''+detail[0]+''; else return ' '+detail[0]+''+detail[1]+''; }).join(''); if(t.length % 2 === 1) t + ''; return t; } // converts given text with newlines (\n) and tabs (\t) to a HTML // table automatically. window.convertTextToTableMagic = function(text) { // check if it should be converted to a table if(!text.match(/\t/)) return text.replace(/\n/g, '
'); var data = []; var columnCount = 0; // parse data var rows = text.split('\n'); $.each(rows, function(i, row) { data[i] = row.split('\t'); if(data[i].length > columnCount) columnCount = data[i].length; }); // build the table var table = ''; $.each(data, function(i, row) { table += ''; $.each(data[i], function(k, cell) { var attributes = ''; if(k === 0 && data[i].length < columnCount) { attributes = ' colspan="'+(columnCount - data[i].length + 1)+'"'; } table += ''+cell+''; }); table += ''; }); table += '
'; return table; } // Given 3 sets of points in an array[3]{lat, lng} returns the area of the triangle window.calcTriArea = function(p) { return Math.abs((p[0].lat*(p[1].lng-p[2].lng)+p[1].lat*(p[2].lng-p[0].lng)+p[2].lat*(p[0].lng-p[1].lng))/2); } // Update layerGroups display status to window.overlayStatus and localStorage 'ingress.intelmap.layergroupdisplayed' window.updateDisplayedLayerGroup = function(name, display) { overlayStatus[name] = display; localStorage['ingress.intelmap.layergroupdisplayed'] = JSON.stringify(overlayStatus); } // Read layerGroup status from window.overlayStatus if it was added to map, // read from cookie if it has not added to map yet. // return 'defaultDisplay' if both overlayStatus and cookie didn't have the record window.isLayerGroupDisplayed = function(name, defaultDisplay) { if(typeof(overlayStatus[name]) !== 'undefined') return overlayStatus[name]; convertCookieToLocalStorage('ingress.intelmap.layergroupdisplayed'); var layersJSON = localStorage['ingress.intelmap.layergroupdisplayed']; if(!layersJSON) return defaultDisplay; var layers = JSON.parse(layersJSON); // keep latest overlayStatus overlayStatus = $.extend(layers, overlayStatus); if(typeof(overlayStatus[name]) === 'undefined') return defaultDisplay; return overlayStatus[name]; } window.addLayerGroup = function(name, layerGroup, defaultDisplay) { if (defaultDisplay === undefined) defaultDisplay = true; if(isLayerGroupDisplayed(name, defaultDisplay)) map.addLayer(layerGroup); layerChooser.addOverlay(layerGroup, name); } window.clampLat = function(lat) { // the map projection used does not handle above approx +- 85 degrees north/south of the equator if (lat > 85.051128) lat = 85.051128; else if (lat < -85.051128) lat = -85.051128; return lat; } window.clampLng = function(lng) { if (lng > 179.999999) lng = 179.999999; else if (lng < -180.0) lng = -180.0; return lng; } window.clampLatLng = function(latlng) { return new L.LatLng ( clampLat(latlng.lat), clampLng(latlng.lng) ); } window.clampLatLngBounds = function(bounds) { return new L.LatLngBounds ( clampLatLng(bounds.getSouthWest()), clampLatLng(bounds.getNorthEast()) ); } window.getGenericMarkerSvg = function(color) { var markerTemplate = '@@INCLUDESTRING:images/marker-icon.svg.template@@'; return markerTemplate.replace(/%COLOR%/g, color); } window.getGenericMarkerIcon = function(color,className) { return L.divIcon({ iconSize: new L.Point(25, 41), iconAnchor: new L.Point(12, 41), html: getGenericMarkerSvg(color), className: className || 'leaflet-iitc-divicon-generic-marker' }); } window.createGenericMarker = function(ll,color,options) { options = options || {}; var markerOpt = $.extend({ icon: getGenericMarkerIcon(color || '#a24ac3') }, options); return L.marker(ll, markerOpt); } // Fix Leaflet: handle touchcancel events in Draggable L.Draggable.prototype._onDownOrig = L.Draggable.prototype._onDown; L.Draggable.prototype._onDown = function(e) { L.Draggable.prototype._onDownOrig.apply(this, arguments); if(e.type === "touchstart") { L.DomEvent.on(document, "touchcancel", this._onUp, this); } }