Jon Atkins 9f08b1d977 mobile: work around leaflet not seeing the WebView size in some cases, causing odd map issues at startup
not 100% certain it fixes it yet, as it doesn't happen every single time...
2014-12-06 07:23:54 +00:00

224 lines
7.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

window.isSmartphone = function() {
// this check is also used in main.js. Note it should not detect
// tablets because their display is large enough to use the desktop
// version.
// The stock intel site allows forcing mobile/full sites with a vp=m or vp=f
// parameter - let's support the same. (stock only allows this for some
// browsers - e.g. android phone/tablet. let's allow it for all, but
// no promises it'll work right)
var viewParam = getURLParam('vp');
if (viewParam == 'm') return true;
if (viewParam == 'f') return false;
return navigator.userAgent.match(/Android.*Mobile/);
}
window.smartphone = function() {};
window.runOnSmartphonesBeforeBoot = function() {
if(!isSmartphone()) return;
console.warn('running smartphone pre boot stuff');
// add smartphone stylesheet
headHTML = document.getElementsByTagName('head')[0].innerHTML;
headHTML += '<style>@@INCLUDESTRING:mobile/smartphone.css@@</style>';
document.getElementsByTagName('head')[0].innerHTML = headHTML;
// dont need many of those
window.setupStyles = function() {
$('head').append('<style>' +
[ '#largepreview.enl img { border:2px solid '+COLORS[TEAM_ENL]+'; } ',
'#largepreview.res img { border:2px solid '+COLORS[TEAM_RES]+'; } ',
'#largepreview.none img { border:2px solid '+COLORS[TEAM_NONE]+'; } '].join("\n")
+ '</style>');
}
window.smartphone.mapButton = $('<a>map</a>').click(function() {
$('#map').css('visibility', 'visible');
$('#updatestatus').show();
$('#chatcontrols a .active').removeClass('active');
$("#chatcontrols a:contains('map')").addClass('active');
});
window.smartphone.sideButton = $('<a>info</a>').click(function() {
$('#scrollwrapper').show();
$('.active').removeClass('active');
$("#chatcontrols a:contains('info')").addClass('active');
});
$('#chatcontrols').append(smartphone.mapButton).append(smartphone.sideButton);
window.addHook('portalDetailsUpdated', function(data) {
var x = $('.imgpreview img').removeClass('hide');
if(!x.length) {
$('.fullimg').remove();
return;
}
if($('.fullimg').length) {
$('.fullimg').replaceWith(x.addClass('fullimg'));
} else {
x.addClass('fullimg').appendTo('#sidebar');
}
});
}
window.smartphoneInfo = function(data) {
var guid = data.selectedPortalGuid;
if(!window.portals[guid]) return;
var data = window.portals[selectedPortal].options.data;
var details = window.portalDetail.get(guid);
var lvl = data.level;
if(data.team === "NEUTRAL")
var t = '<span class="portallevel">L0</span>';
else
var t = '<span class="portallevel" style="background: '+COLORS_LVL[lvl]+';">L' + lvl + '</span>';
var percentage = data.health;
if(details) {
var totalEnergy = getTotalPortalEnergy(details);
if(getTotalPortalEnergy(details) > 0) {
percentage = Math.floor(getCurrentPortalEnergy(details) / totalEnergy * 100);
}
}
t += ' ' + percentage + '% ';
t += data.title;
if(details) {
var l,v,max,perc;
var eastAnticlockwiseToNorthClockwise = [2,1,0,7,6,5,4,3];
for(var ind=0;ind<8;ind++)
{
if (details.resonators.length == 8) {
var slot = eastAnticlockwiseToNorthClockwise[ind];
var reso = details.resonators[slot];
} else {
var slot = null;
var reso = ind < details.resonators.length ? details.resonators[ind] : null;
}
var className = TEAM_TO_CSS[getTeam(details)];
if(slot !== null && OCTANTS[slot] === 'N')
className += ' north'
if(reso) {
l = parseInt(reso.level);
v = parseInt(reso.energy);
max = RESO_NRG[l];
perc = v/max*100;
} else {
l = 0;
v = 0;
max = 0;
perc = 0;
}
t += '<div class="resonator '+className+'" style="border-top-color: '+COLORS_LVL[l]+';left: '+(100*ind/8.0)+'%;">';
t += '<div class="filllevel" style="width:'+perc+'%;"></div>';
t += '</div>'
}
}
$('#mobileinfo').html(t);
}
window.runOnSmartphonesAfterBoot = function() {
if(!isSmartphone()) return;
console.warn('running smartphone post boot stuff');
window.show('map');
// add a div/hook for updating mobile info
$('#updatestatus').prepend('<div id="mobileinfo" onclick="show(\'info\')"></div>');
window.addHook('portalSelected', window.smartphoneInfo);
// init msg of status bar. hint for the user that a tap leads to the info screen
$('#mobileinfo').html('<div style="text-align: center"><b>tap here for info screen</b></div>');
// disable img full view
$('#portaldetails').off('click', '**');
// make buttons in action bar flexible
var l = $('#chatcontrols a:visible');
l.css('width', 100/l.length + '%');
// notify android that a select spinner is enabled.
// this disables javascript injection on android side.
// if android is not notified, the spinner closes on the next JS call
if (typeof android !== 'undefined' && android && android.spinnerEnabled) {
$("body").on("click", "select", function() {
android.spinnerEnabled(true);
});
}
// add event to portals that allows long press to switch to sidebar
window.addHook('portalAdded', function(data) {
data.portal.on('add', function() {
if(!this._container || this.options.addedTapHoldHandler) return;
this.options.addedTapHoldHandler = true;
var guid = this.options.guid;
// this is a hack, accessing Leaflets private _container is evil
$(this._container).on('taphold', function() {
window.renderPortalDetails(guid);
window.show('info');
});
});
});
if(typeof android !== 'undefined' && android && android.setPermalink) {
window.map.on('moveend', window.setAndroidPermalink);
addHook('portalSelected', window.setAndroidPermalink);
}
// for some reason, leaflet misses the WebView size being set at startup on IITC Mobile
// create a short timer that checks for this issue
setTimeout (function() { map.invalidateSize(); }, 0.2*1000);
}
window.setAndroidPermalink = function() {
var c = window.map.getCenter();
var lat = Math.round(c.lat*1E6)/1E6;
var lng = Math.round(c.lng*1E6)/1E6;
var href = '/intel?ll='+lat+','+lng+'&z=' + map.getZoom();
if(window.selectedPortal && window.portals[window.selectedPortal]) {
var p = window.portals[window.selectedPortal].getLatLng();
lat = Math.round(p.lat*1E6)/1E6;
lng = Math.round(p.lng*1E6)/1E6;
href += '&pll='+lat+','+lng;
}
href = $('<a>').prop('href', href).prop('href'); // to get absolute URI
android.setPermalink(href);
}
window.useAndroidPanes = function() {
// isSmartphone is important to disable panes in desktop mode
return (typeof android !== 'undefined' && android && android.addPane && window.isSmartphone());
}
if(typeof android !== 'undefined' && android && android.getFileRequestUrlPrefix) {
window.requestFile = function(callback) {
do {
var funcName = "onFileSelected" + parseInt(Math.random()*0xFFFF).toString(16);
} while(window[funcName] !== undefined)
window[funcName] = function(filename, content) {
callback(decodeURIComponent(filename), atob(content));
};
var script = document.createElement('script');
script.src = android.getFileRequestUrlPrefix() + funcName;
(document.body || document.head || document.documentElement).appendChild(script);
};
}