Skip to content

Commit

Permalink
Fix issues #24 & 28
Browse files Browse the repository at this point in the history
  • Loading branch information
slowe committed Jan 1, 2016
1 parent 1f9911b commit c6d0baa
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 92 deletions.
95 changes: 51 additions & 44 deletions tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
padding: 0px;
font-size: 1em;
}
#starmapper { height: 400px; width:500px; position: relative; }
#starmapper, #starmapper2 { height: 400px; width:500px; position: relative; }
#virtualskyinfobox{
font-size: 12px;
display:none;
Expand Down Expand Up @@ -48,6 +48,7 @@
<script src="virtualsky.js" type="text/javascript"></script>
<script>
$.noConflict();
var sky;
jQuery(document).ready(function($) {
function assert_equal(actual,expected,message){
equal(actual,expected,message);
Expand All @@ -67,9 +68,10 @@
test("Initiation",function(){

jQuery('#starmapper').remove();
jQuery('#starmapper2').remove();

// Try setting up a planetarium with no ID provided
var sky = jQuery.virtualsky({});
sky = jQuery.virtualsky({});
assert_equal(sky.id,"","No ID should be set if none is provided");

var match = 0;
Expand All @@ -85,6 +87,7 @@
// We will add a DOM element then assign it
// Remove the existing DOM element
jQuery('#starmapper').remove();
jQuery('#starmapper2').remove();
jQuery('body').append('<div id="starmapper"><\/div>');

sky = jQuery.virtualsky({id:'starmapper',projection:'stereo'});
Expand All @@ -106,9 +109,25 @@

var d2r = Math.PI/180;

test("Times and Locations",function(){
test("Angles",function(){

sky = jQuery.virtualsky({id:'angletest','projection':'polar','width':700,'height':700});

assert_almost_equal(sky.greatCircle(180*d2r,0,180*d2r,0),0*d2r,8,"Great Circle distance for same point is zero");
assert_almost_equal(sky.greatCircle(180*d2r,0,90*d2r,0),90*d2r,8,"Great Circle distance for points separated around the equator");
assert_almost_equal(sky.greatCircle(180*d2r,0,180*d2r,45*d2r),45*d2r,8,"Great Circle distance for points separated in declination");
assert_almost_equal(sky.greatCircle(0,0,360*d2r,45*d2r),45*d2r,8,"Great Circle distance for points separated in declination and 360 degrees different");
assert_almost_equal(sky.greatCircle(10*d2r,0,10*d2r,45*d2r),45*d2r,8,"Great Circle distance for points separated in declination but at the same non-zero RA");
assert_almost_equal(sky.greatCircle(10*d2r,45*d2r,10*d2r,-45*d2r),90*d2r,8,"Great Circle distance for points separated in declination but at the same non-zero RA");
assert_almost_equal(sky.greatCircle(-180*d2r,0,180*d2r,45*d2r),45*d2r,8,"Great Circle distance for points separated in declination and 360 degrees different (one negative)");
assert_almost_equal(sky.greatCircle(190*d2r,20*d2r,10*d2r,20*d2r),140*d2r,8,"Great Circle distance for points at the same non-zero declination but 360 degrees appart in RA");

});

sky = jQuery.virtualsky({id:'starmapper'});

var sky = jQuery.virtualsky({id:'starmapper'});

test("Times and Locations",function(){

var str = 'October 25, 1985 01:21:00'
var clock = new Date(str);
Expand Down Expand Up @@ -151,22 +170,6 @@

});

test("Angles",function(){

var sky = jQuery.virtualsky({id:'angletest','projection':'polar','width':700,'height':700});

assert_almost_equal(sky.greatCircle(180*d2r,0,180*d2r,0),0*d2r,8,"Great Circle distance for same point is zero");
assert_almost_equal(sky.greatCircle(180*d2r,0,90*d2r,0),90*d2r,8,"Great Circle distance for points separated around the equator");
assert_almost_equal(sky.greatCircle(180*d2r,0,180*d2r,45*d2r),45*d2r,8,"Great Circle distance for points separated in declination");
assert_almost_equal(sky.greatCircle(0,0,360*d2r,45*d2r),45*d2r,8,"Great Circle distance for points separated in declination and 360 degrees different");
assert_almost_equal(sky.greatCircle(10*d2r,0,10*d2r,45*d2r),45*d2r,8,"Great Circle distance for points separated in declination but at the same non-zero RA");
assert_almost_equal(sky.greatCircle(10*d2r,45*d2r,10*d2r,-45*d2r),90*d2r,8,"Great Circle distance for points separated in declination but at the same non-zero RA");
assert_almost_equal(sky.greatCircle(-180*d2r,0,180*d2r,45*d2r),45*d2r,8,"Great Circle distance for points separated in declination and 360 degrees different (one negative)");
assert_almost_equal(sky.greatCircle(190*d2r,20*d2r,10*d2r,20*d2r),140*d2r,8,"Great Circle distance for points at the same non-zero declination but 360 degrees appart in RA");

});


test("Keyboard input",function(){

function keypress(key,sky){
Expand All @@ -177,7 +180,6 @@
sky.mouseover = mouse;
}

var sky = jQuery.virtualsky({id:'starmapper'});
var mag = sky.magnitude;

keypress(38,sky);
Expand All @@ -199,36 +201,30 @@

var clock = new Date();
keypress('8',sky);
ok(sky.clock-clock < 2,"Keypress (8): Time is set to now");
var diff = sky.clock-clock;
ok(diff < 10,"Keypress (8): Time is set to now ("+clock+", "+sky.clock+", "+(diff)+")");

});

test("Custom input",function(){

var sky = jQuery.virtualsky({id:'starmapper',constellationboundaries: true,
boundaries: [
["And",343,34.5,343,52.5,350,52.5,350,50,353.75,50,353.75,48,2.5,48,2.5,46,13,46,13,48,16.75,48,16.75,50,20.5,50,25,50,25,47,30.625,47,30.625,50.5,37.75,50.5,37.75,36.75,30,36.75,30,35,21.125,35,21.125,33,10.75,33,10.75,23.75,12.75,23.75,12.75,21,2.125,21,2.125,22,1,22,1,28,0,28,0,31.33333,356.25,31.33333,356.25,32.08333,352.5,32.08333,352.5,34.5,343,34.5],
["Ori",69.25,0,69.25,15.5,74.5,15.5,74.5,16,80,16,80,15.5,84,15.5,84,12.5,86.5,12.5,86.5,18,85.5,18,85.5,22.83333,88.25,22.83333,88.25,21.5,93.25,21.5,93.25,17.5,94.625,17.5,94.625,12,94.625,10,93.625,10,93.625,0,93.625,-4,87.5,-4,87.5,-11,76.25,-11,76.25,-4,70,-4,70,0,69.25,0]
]
});
assert_equal(sky.boundaries.length,2,"Provided boundaries are used")


});

test("Languages",function(){
var sky = jQuery.virtualsky({id:'starmapper',lang:'en'});
assert_equal(sky.getPhrase("sun"),"Sun","Check English Sun exists");
sky.changeLanguage('es');
assert_equal(sky.lang.code,"es","Change language to Spanish");
assert_equal(sky.getPhrase("sun"),"Sol","Check Spanish Sol exists");
function testLang(code,sun){
stop();
sky.changeLanguage(code,function(e){
assert_equal(sky.langcode,code,"Change language to "+this.htmlDecode(this.langs[code].language.name));
assert_equal(sky.getPhrase("sun"),sun,"Check "+sun+" is translation of 'Sun'");
start();
});

}

testLang('en','Sun');
testLang('es','Sol');
testLang('fr','Soleil');
});


test("JSON callbacks",function(){

var sky = jQuery.virtualsky({id:'starmapper'});

// We need to stop qunit until we get the ajax callback
stop();
sky.load('stars',sky.file.stars,function(e){
Expand Down Expand Up @@ -269,13 +265,24 @@

test("Add Pointers",function(){

var sky = jQuery.virtualsky({id:'starmapper'});

assert_equal(1,sky.addPointer({'ra':83.6330833,'dec':22.0145,'label':'M1 - Crab Nebula','html':'Blah blah','url':'http://www.strudel.org.uk/lookUP/?name=M1',colour:'rgb(220,220,255)'}),"Add a first pointer");
assert_equal(2,sky.addPointer({'ra':148.9684583,'dec':69.6797028,'label':'M82','url':'http://www.strudel.org.uk/lookUP/?name=M82',colour:'rgb(220,220,255)'}),"Add a 2nd pointer");

});

test("Custom input",function(){

jQuery('#starmapper2').remove();
var sky2 = jQuery.virtualsky({id:'starmapper2',constellationboundaries: true,
boundaries: [
["And",343,34.5,343,52.5,350,52.5,350,50,353.75,50,353.75,48,2.5,48,2.5,46,13,46,13,48,16.75,48,16.75,50,20.5,50,25,50,25,47,30.625,47,30.625,50.5,37.75,50.5,37.75,36.75,30,36.75,30,35,21.125,35,21.125,33,10.75,33,10.75,23.75,12.75,23.75,12.75,21,2.125,21,2.125,22,1,22,1,28,0,28,0,31.33333,356.25,31.33333,356.25,32.08333,352.5,32.08333,352.5,34.5,343,34.5],
["Ori",69.25,0,69.25,15.5,74.5,15.5,74.5,16,80,16,80,15.5,84,15.5,84,12.5,86.5,12.5,86.5,18,85.5,18,85.5,22.83333,88.25,22.83333,88.25,21.5,93.25,21.5,93.25,17.5,94.625,17.5,94.625,12,94.625,10,93.625,10,93.625,0,93.625,-4,87.5,-4,87.5,-11,76.25,-11,76.25,-4,70,-4,70,0,69.25,0]
]
});
assert_equal(sky2.boundaries.length,2,"Provided boundaries are used")

});


});

Expand Down
96 changes: 48 additions & 48 deletions virtualsky.js
Original file line number Diff line number Diff line change
Expand Up @@ -782,10 +782,14 @@ function VirtualSky(input){

// Country codes at http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
this.language = (typeof this.q.lang==="string") ? this.q.lang : (typeof this.setlang==="string" ? this.setlang : (navigator) ? (navigator.userLanguage||navigator.systemLanguage||navigator.language||browser.language) : "");
this.langshort = (this.language.indexOf('-') > 0 ? this.language.substring(0,this.language.indexOf('-')) : this.language.substring(0,2));
this.langs = [{"language": {"code": "en","name": "English","alignment": "left" }}]; // The contents of the language will be loaded from the JSON language file
this.lang = this.langs[0];

this.langs = {
'ar': { "language": {"name": "&#1575;&#1604;&#1593;&#1585;&#1576;&#1610;&#1577;","alignment": "right" } },
'cs': { "language": {"name": "Čeština","alignment": "left" } },
'en': { "language": {"name": "English","alignment": "left" } },
'es': { "language": {"name": "Espa&#241;ol","alignment": "left" } },
'fr': { "language": {"name": "Fran&#231;ais","alignment": "left" } },
}; // The contents of the language will be loaded from the JSON language file
this.lang = this.langs['en']; // default

if(typeof this.polartype=="undefined") this.selectProjection('polar'); // Set the default projection

Expand Down Expand Up @@ -953,43 +957,50 @@ VirtualSky.prototype.init = function(d){
// If it fails and this was the long variation of the language (e.g. "en-gb" or "zh-yue"), try the short version (e.g. "en" or "zh")
VirtualSky.prototype.loadLanguage = function(l,fn){
l = l || this.language;
var lang = "";
if(this.langs[l]) lang = l;
if(!lang){
// Try loading a short version of the language code
l = (l.indexOf('-') > 0 ? l.substring(0,l.indexOf('-')) : l.substring(0,2));
if(this.langs[l]) lang = l;
}
l = lang;
if(!l) return;
var url = this.langurl.replace('%LANG%',l);
this.loadJSON(
url,
function(data){
this.langcode = l;
var found = -1;
for(var i = 0 ; i < this.langs.length ; i++){
if(this.langs[i].language && this.langs[i].language.code==l) found = i;
}
if(found < 0)
this.langs.push(data);
else
this.langs[found] = data;
this.langs[l] = data;
this.langs[l].loaded = true;

// Update any starnames
if(data.starnames){
for(var n in data.starnames) this.starnames[n] = data.starnames[n];
}
if(data.starnames) for(var n in data.starnames) this.starnames[n] = data.starnames[n];

this.changeLanguage(l).draw();
this.changeLanguage(l);
if(typeof fn==="function") fn.call(this);
},
function(){},
function(){
function(data){ },
function(e){
// If we tried to load the short version of the language and it failed, default to English
if(this.langshort==l){ this.loadLanguage('en',fn); }
if(url.indexOf(this.language) > 0) this.loadLanguage(this.langshort,fn);
this.loadLanguage('en',fn);
}
);
return this;
}
// Change the active language
VirtualSky.prototype.changeLanguage = function(code){
for(var i = 0; i < this.langs.length ; i++){
if(this.langs[i].language.code==code){ this.lang = this.langs[i]; return this; }
VirtualSky.prototype.changeLanguage = function(code,fn){
if(this.langs[code]){
if(!this.langs[code].loaded) this.loadLanguage(code,fn);
else {
this.lang = this.langs[code];
this.langcode = code;
this.draw();
if(typeof fn==="function") fn.call(this);
}
return this;
}
this.lang = this.langs[0];
this.lang = this.langs['en'];
return this;
}
VirtualSky.prototype.htmlDecode = function(input){
Expand All @@ -999,18 +1010,14 @@ VirtualSky.prototype.htmlDecode = function(input){
return e.childNodes[0].nodeValue;
}
VirtualSky.prototype.getPhrase = function(key,key2){
if(key===undefined)
return undefined;
if(key===undefined) return undefined;
if(key==="constellations"){
if(key2 && is(this.lang.constellations[key2],"string"))
return this.htmlDecode(this.lang.constellations[key2]);
}else if(key==="planets"){
if(this.lang.planets && this.lang.planets[key2])
return this.htmlDecode(this.lang.planets[key2]);
else
return this.htmlDecode(this.lang[key2]);
}else
return this.htmlDecode(this.lang[key]) || this.htmlDecode(this.langs[0][key]) || "";
if(this.lang.planets && this.lang.planets[key2]) return this.htmlDecode(this.lang.planets[key2]);
else return this.htmlDecode(this.lang[key2]);
}else return this.htmlDecode(this.lang[key]) || this.htmlDecode(this.langs['en'][key]) || "";
}
VirtualSky.prototype.resize = function(w,h){
if(!this.canvas) return;
Expand Down Expand Up @@ -1325,7 +1332,7 @@ VirtualSky.prototype.toggleHelp = function(){
'<strong class="'+v+'_help_key '+v+'_'+this.keys[i].txt+'">'+this.keys[i].str+'</strong> &rarr; <a href="#" class="'+v+'_'+this.keys[i].txt+'" style="text-decoration:none;">'+this.getPhrase(this.keys[i].txt)+'</a>'+
'</li>'; }
$('<div class="'+v+'_help">'+
'<div class="'+v+'_dismiss" title="close">&times;</div>'+
'<div class="'+v+'_dismiss" title="'+this.getPhrase('close')+'">&times;</div>'+
'<span>'+this.getPhrase('keyboard')+'</span>'+
'<div class="'+v+'_helpinner"><ul></ul></div>'+
'</div>').appendTo(this.container);
Expand Down Expand Up @@ -1392,8 +1399,7 @@ VirtualSky.prototype.registerKey = function(charCode,fn,txt){
}
// Work out if the keypress has a function that needs to be called.
VirtualSky.prototype.keypress = function(charCode,event){
if(!event)
event = { altKey: false };
if(!event) event = { altKey: false };
if(this.mouseover && this.keyboard){
for(var i = 0 ; i < this.keys.length ; i++){
if(this.keys[i].charCode == charCode && event.altKey == this.keys[i].altKey){
Expand Down Expand Up @@ -1923,8 +1929,7 @@ VirtualSky.prototype.draw = function(proj){
var credit = this.getPhrase('power');
var metric_credit = this.drawText(credit,5,this.tall-5);
// Float a transparent link on top of the credit text
if(d.find('.'+this.id+'_credit').length == 0)
d.append('<div class="'+this.id+'_credit"><a href="http://lcogt.net/virtualsky" target="_parent" title="Created by the Las Cumbres Observatory Global Telescope">'+this.getPhrase('powered')+'</a></div>');
if(d.find('.'+this.id+'_credit').length == 0) d.append('<div class="'+this.id+'_credit"><a href="http://lcogt.net/virtualsky" target="_parent" title="Las Cumbres Observatory Global Telescope">'+this.getPhrase('powered')+'</a></div>');
d.find('.'+this.id+'_credit').css({padding:0,zIndex:20,display:'block',overflow:'hidden',backgroundColor:'transparent'});
d.find('.'+this.id+'_credit a').css({display:'block',width:Math.ceil(metric_credit)+'px',height:fontsize+'px','font-size':fontsize+'px'});
this.positionCredit();
Expand All @@ -1950,8 +1955,7 @@ VirtualSky.prototype.draw = function(proj){
}).on('click',{me:this},function(e){ e.data.me.toggleHelp(); });
d.find('.'+this.id+'_help').css({'font-size':fontsize}).find('a').css({color:txtcolour});
}
if(this.container.find('.'+this.id+'_clock').length == 0)
this.container.append('<div class="'+this.id+'_clock" title="'+this.getPhrase('datechange')+'">'+clockstring+'</div>');
if(this.container.find('.'+this.id+'_clock').length == 0) this.container.append('<div class="'+this.id+'_clock" title="'+this.getPhrase('datechange')+'">'+clockstring+'</div>');
var off = $('#'+this.idinner).position();
this.container.find('.'+this.id+'_clock').css({
position:'absolute',
Expand All @@ -1978,12 +1982,8 @@ VirtualSky.prototype.draw = function(proj){
if(s.wide < w) w = s.wide;
s.container.append(
'<div id="'+id+'_calendar" class="'+v+'form">'+
'<div style="" id="'+id+'_calendar_close" class="'+v+'_dismiss" title="close">'+
'&times;'+
'</div>'+
'<div style="text-align:center;margin:2px;">'+
e.data.sky.getPhrase('date')+
'</div>'+
'<div style="" id="'+id+'_calendar_close" class="'+v+'_dismiss" title="'+e.data.sky.getPhrase('close')+'">&times;</div>'+
'<div style="text-align:center;margin:2px;">'+e.data.sky.getPhrase('date')+'</div>'+
'<div style="text-align:center;">'+
'<input type="text" id="'+id+'_year" style="width:3.2em;" value="" />'+
'<div class="divider">/</div>'+
Expand Down Expand Up @@ -2041,7 +2041,7 @@ VirtualSky.prototype.draw = function(proj){
}
s.container.append(
'<div id="'+id+'_geo" class="'+v+'form">'+
'<div id="'+id+'_geo_close" class="'+v+'_dismiss" title="close">&times;</div>'+
'<div id="'+id+'_geo_close" class="'+v+'_dismiss" title="'+s.getPhrase('close')+'">&times;</div>'+
'<div style="text-align:center;margin:2px;">'+s.getPhrase('position')+'</div>'+
'<div style="text-align:center;">'+
'<input type="text" id="'+id+'_lat" value="" style="padding-right:10px!important;">'+
Expand Down Expand Up @@ -2851,8 +2851,8 @@ VirtualSky.prototype.updateClock = function(d){
}
// Call any calendar-based events
VirtualSky.prototype.calendarUpdate = function(){
for(var e = 0; e < sky.calendarevents.length; e++){
if(is(sky.calendarevents[e],"function")) sky.calendarevents[e].call(sky);
for(var e = 0; e < this.calendarevents.length; e++){
if(is(this.calendarevents[e],"function")) this.calendarevents[e].call(this);
}
return this;
}
Expand Down

0 comments on commit c6d0baa

Please sign in to comment.