Skip to content

Commit c6d0baa

Browse files
committed
Fix issues #24 & 28
1 parent 1f9911b commit c6d0baa

File tree

2 files changed

+99
-92
lines changed

2 files changed

+99
-92
lines changed

tests.html

+51-44
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
padding: 0px;
1919
font-size: 1em;
2020
}
21-
#starmapper { height: 400px; width:500px; position: relative; }
21+
#starmapper, #starmapper2 { height: 400px; width:500px; position: relative; }
2222
#virtualskyinfobox{
2323
font-size: 12px;
2424
display:none;
@@ -48,6 +48,7 @@
4848
<script src="virtualsky.js" type="text/javascript"></script>
4949
<script>
5050
$.noConflict();
51+
var sky;
5152
jQuery(document).ready(function($) {
5253
function assert_equal(actual,expected,message){
5354
equal(actual,expected,message);
@@ -67,9 +68,10 @@
6768
test("Initiation",function(){
6869

6970
jQuery('#starmapper').remove();
71+
jQuery('#starmapper2').remove();
7072

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

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

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

107110
var d2r = Math.PI/180;
108111

109-
test("Times and Locations",function(){
112+
test("Angles",function(){
113+
114+
sky = jQuery.virtualsky({id:'angletest','projection':'polar','width':700,'height':700});
115+
116+
assert_almost_equal(sky.greatCircle(180*d2r,0,180*d2r,0),0*d2r,8,"Great Circle distance for same point is zero");
117+
assert_almost_equal(sky.greatCircle(180*d2r,0,90*d2r,0),90*d2r,8,"Great Circle distance for points separated around the equator");
118+
assert_almost_equal(sky.greatCircle(180*d2r,0,180*d2r,45*d2r),45*d2r,8,"Great Circle distance for points separated in declination");
119+
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");
120+
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");
121+
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");
122+
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)");
123+
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");
124+
125+
});
126+
127+
sky = jQuery.virtualsky({id:'starmapper'});
110128

111-
var sky = jQuery.virtualsky({id:'starmapper'});
129+
130+
test("Times and Locations",function(){
112131

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

152171
});
153172

154-
test("Angles",function(){
155-
156-
var sky = jQuery.virtualsky({id:'angletest','projection':'polar','width':700,'height':700});
157-
158-
assert_almost_equal(sky.greatCircle(180*d2r,0,180*d2r,0),0*d2r,8,"Great Circle distance for same point is zero");
159-
assert_almost_equal(sky.greatCircle(180*d2r,0,90*d2r,0),90*d2r,8,"Great Circle distance for points separated around the equator");
160-
assert_almost_equal(sky.greatCircle(180*d2r,0,180*d2r,45*d2r),45*d2r,8,"Great Circle distance for points separated in declination");
161-
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");
162-
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");
163-
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");
164-
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)");
165-
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");
166-
167-
});
168-
169-
170173
test("Keyboard input",function(){
171174

172175
function keypress(key,sky){
@@ -177,7 +180,6 @@
177180
sky.mouseover = mouse;
178181
}
179182

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

183185
keypress(38,sky);
@@ -199,36 +201,30 @@
199201

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

204207
});
205208

206-
test("Custom input",function(){
207-
208-
var sky = jQuery.virtualsky({id:'starmapper',constellationboundaries: true,
209-
boundaries: [
210-
["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],
211-
["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]
212-
]
213-
});
214-
assert_equal(sky.boundaries.length,2,"Provided boundaries are used")
215-
216-
217-
});
218-
219209
test("Languages",function(){
220-
var sky = jQuery.virtualsky({id:'starmapper',lang:'en'});
221-
assert_equal(sky.getPhrase("sun"),"Sun","Check English Sun exists");
222-
sky.changeLanguage('es');
223-
assert_equal(sky.lang.code,"es","Change language to Spanish");
224-
assert_equal(sky.getPhrase("sun"),"Sol","Check Spanish Sol exists");
210+
function testLang(code,sun){
211+
stop();
212+
sky.changeLanguage(code,function(e){
213+
assert_equal(sky.langcode,code,"Change language to "+this.htmlDecode(this.langs[code].language.name));
214+
assert_equal(sky.getPhrase("sun"),sun,"Check "+sun+" is translation of 'Sun'");
215+
start();
216+
});
217+
218+
}
225219

220+
testLang('en','Sun');
221+
testLang('es','Sol');
222+
testLang('fr','Soleil');
226223
});
227224

225+
228226
test("JSON callbacks",function(){
229227

230-
var sky = jQuery.virtualsky({id:'starmapper'});
231-
232228
// We need to stop qunit until we get the ajax callback
233229
stop();
234230
sky.load('stars',sky.file.stars,function(e){
@@ -269,13 +265,24 @@
269265

270266
test("Add Pointers",function(){
271267

272-
var sky = jQuery.virtualsky({id:'starmapper'});
273-
274268
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");
275269
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");
276270

277271
});
278272

273+
test("Custom input",function(){
274+
275+
jQuery('#starmapper2').remove();
276+
var sky2 = jQuery.virtualsky({id:'starmapper2',constellationboundaries: true,
277+
boundaries: [
278+
["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],
279+
["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]
280+
]
281+
});
282+
assert_equal(sky2.boundaries.length,2,"Provided boundaries are used")
283+
284+
});
285+
279286

280287
});
281288

virtualsky.js

+48-48
Original file line numberDiff line numberDiff line change
@@ -782,10 +782,14 @@ function VirtualSky(input){
782782

783783
// Country codes at http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
784784
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) : "");
785-
this.langshort = (this.language.indexOf('-') > 0 ? this.language.substring(0,this.language.indexOf('-')) : this.language.substring(0,2));
786-
this.langs = [{"language": {"code": "en","name": "English","alignment": "left" }}]; // The contents of the language will be loaded from the JSON language file
787-
this.lang = this.langs[0];
788-
785+
this.langs = {
786+
'ar': { "language": {"name": "&#1575;&#1604;&#1593;&#1585;&#1576;&#1610;&#1577;","alignment": "right" } },
787+
'cs': { "language": {"name": "Čeština","alignment": "left" } },
788+
'en': { "language": {"name": "English","alignment": "left" } },
789+
'es': { "language": {"name": "Espa&#241;ol","alignment": "left" } },
790+
'fr': { "language": {"name": "Fran&#231;ais","alignment": "left" } },
791+
}; // The contents of the language will be loaded from the JSON language file
792+
this.lang = this.langs['en']; // default
789793

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

@@ -953,43 +957,50 @@ VirtualSky.prototype.init = function(d){
953957
// 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")
954958
VirtualSky.prototype.loadLanguage = function(l,fn){
955959
l = l || this.language;
960+
var lang = "";
961+
if(this.langs[l]) lang = l;
962+
if(!lang){
963+
// Try loading a short version of the language code
964+
l = (l.indexOf('-') > 0 ? l.substring(0,l.indexOf('-')) : l.substring(0,2));
965+
if(this.langs[l]) lang = l;
966+
}
967+
l = lang;
956968
if(!l) return;
957969
var url = this.langurl.replace('%LANG%',l);
958970
this.loadJSON(
959971
url,
960972
function(data){
961973
this.langcode = l;
962-
var found = -1;
963-
for(var i = 0 ; i < this.langs.length ; i++){
964-
if(this.langs[i].language && this.langs[i].language.code==l) found = i;
965-
}
966-
if(found < 0)
967-
this.langs.push(data);
968-
else
969-
this.langs[found] = data;
974+
this.langs[l] = data;
975+
this.langs[l].loaded = true;
970976

971977
// Update any starnames
972-
if(data.starnames){
973-
for(var n in data.starnames) this.starnames[n] = data.starnames[n];
974-
}
978+
if(data.starnames) for(var n in data.starnames) this.starnames[n] = data.starnames[n];
975979

976-
this.changeLanguage(l).draw();
980+
this.changeLanguage(l);
981+
if(typeof fn==="function") fn.call(this);
977982
},
978-
function(){},
979-
function(){
983+
function(data){ },
984+
function(e){
980985
// If we tried to load the short version of the language and it failed, default to English
981-
if(this.langshort==l){ this.loadLanguage('en',fn); }
982-
if(url.indexOf(this.language) > 0) this.loadLanguage(this.langshort,fn);
986+
this.loadLanguage('en',fn);
983987
}
984988
);
985989
return this;
986990
}
987991
// Change the active language
988-
VirtualSky.prototype.changeLanguage = function(code){
989-
for(var i = 0; i < this.langs.length ; i++){
990-
if(this.langs[i].language.code==code){ this.lang = this.langs[i]; return this; }
992+
VirtualSky.prototype.changeLanguage = function(code,fn){
993+
if(this.langs[code]){
994+
if(!this.langs[code].loaded) this.loadLanguage(code,fn);
995+
else {
996+
this.lang = this.langs[code];
997+
this.langcode = code;
998+
this.draw();
999+
if(typeof fn==="function") fn.call(this);
1000+
}
1001+
return this;
9911002
}
992-
this.lang = this.langs[0];
1003+
this.lang = this.langs['en'];
9931004
return this;
9941005
}
9951006
VirtualSky.prototype.htmlDecode = function(input){
@@ -999,18 +1010,14 @@ VirtualSky.prototype.htmlDecode = function(input){
9991010
return e.childNodes[0].nodeValue;
10001011
}
10011012
VirtualSky.prototype.getPhrase = function(key,key2){
1002-
if(key===undefined)
1003-
return undefined;
1013+
if(key===undefined) return undefined;
10041014
if(key==="constellations"){
10051015
if(key2 && is(this.lang.constellations[key2],"string"))
10061016
return this.htmlDecode(this.lang.constellations[key2]);
10071017
}else if(key==="planets"){
1008-
if(this.lang.planets && this.lang.planets[key2])
1009-
return this.htmlDecode(this.lang.planets[key2]);
1010-
else
1011-
return this.htmlDecode(this.lang[key2]);
1012-
}else
1013-
return this.htmlDecode(this.lang[key]) || this.htmlDecode(this.langs[0][key]) || "";
1018+
if(this.lang.planets && this.lang.planets[key2]) return this.htmlDecode(this.lang.planets[key2]);
1019+
else return this.htmlDecode(this.lang[key2]);
1020+
}else return this.htmlDecode(this.lang[key]) || this.htmlDecode(this.langs['en'][key]) || "";
10141021
}
10151022
VirtualSky.prototype.resize = function(w,h){
10161023
if(!this.canvas) return;
@@ -1325,7 +1332,7 @@ VirtualSky.prototype.toggleHelp = function(){
13251332
'<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>'+
13261333
'</li>'; }
13271334
$('<div class="'+v+'_help">'+
1328-
'<div class="'+v+'_dismiss" title="close">&times;</div>'+
1335+
'<div class="'+v+'_dismiss" title="'+this.getPhrase('close')+'">&times;</div>'+
13291336
'<span>'+this.getPhrase('keyboard')+'</span>'+
13301337
'<div class="'+v+'_helpinner"><ul></ul></div>'+
13311338
'</div>').appendTo(this.container);
@@ -1392,8 +1399,7 @@ VirtualSky.prototype.registerKey = function(charCode,fn,txt){
13921399
}
13931400
// Work out if the keypress has a function that needs to be called.
13941401
VirtualSky.prototype.keypress = function(charCode,event){
1395-
if(!event)
1396-
event = { altKey: false };
1402+
if(!event) event = { altKey: false };
13971403
if(this.mouseover && this.keyboard){
13981404
for(var i = 0 ; i < this.keys.length ; i++){
13991405
if(this.keys[i].charCode == charCode && event.altKey == this.keys[i].altKey){
@@ -1923,8 +1929,7 @@ VirtualSky.prototype.draw = function(proj){
19231929
var credit = this.getPhrase('power');
19241930
var metric_credit = this.drawText(credit,5,this.tall-5);
19251931
// Float a transparent link on top of the credit text
1926-
if(d.find('.'+this.id+'_credit').length == 0)
1927-
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>');
1932+
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>');
19281933
d.find('.'+this.id+'_credit').css({padding:0,zIndex:20,display:'block',overflow:'hidden',backgroundColor:'transparent'});
19291934
d.find('.'+this.id+'_credit a').css({display:'block',width:Math.ceil(metric_credit)+'px',height:fontsize+'px','font-size':fontsize+'px'});
19301935
this.positionCredit();
@@ -1950,8 +1955,7 @@ VirtualSky.prototype.draw = function(proj){
19501955
}).on('click',{me:this},function(e){ e.data.me.toggleHelp(); });
19511956
d.find('.'+this.id+'_help').css({'font-size':fontsize}).find('a').css({color:txtcolour});
19521957
}
1953-
if(this.container.find('.'+this.id+'_clock').length == 0)
1954-
this.container.append('<div class="'+this.id+'_clock" title="'+this.getPhrase('datechange')+'">'+clockstring+'</div>');
1958+
if(this.container.find('.'+this.id+'_clock').length == 0) this.container.append('<div class="'+this.id+'_clock" title="'+this.getPhrase('datechange')+'">'+clockstring+'</div>');
19551959
var off = $('#'+this.idinner).position();
19561960
this.container.find('.'+this.id+'_clock').css({
19571961
position:'absolute',
@@ -1978,12 +1982,8 @@ VirtualSky.prototype.draw = function(proj){
19781982
if(s.wide < w) w = s.wide;
19791983
s.container.append(
19801984
'<div id="'+id+'_calendar" class="'+v+'form">'+
1981-
'<div style="" id="'+id+'_calendar_close" class="'+v+'_dismiss" title="close">'+
1982-
'&times;'+
1983-
'</div>'+
1984-
'<div style="text-align:center;margin:2px;">'+
1985-
e.data.sky.getPhrase('date')+
1986-
'</div>'+
1985+
'<div style="" id="'+id+'_calendar_close" class="'+v+'_dismiss" title="'+e.data.sky.getPhrase('close')+'">&times;</div>'+
1986+
'<div style="text-align:center;margin:2px;">'+e.data.sky.getPhrase('date')+'</div>'+
19871987
'<div style="text-align:center;">'+
19881988
'<input type="text" id="'+id+'_year" style="width:3.2em;" value="" />'+
19891989
'<div class="divider">/</div>'+
@@ -2041,7 +2041,7 @@ VirtualSky.prototype.draw = function(proj){
20412041
}
20422042
s.container.append(
20432043
'<div id="'+id+'_geo" class="'+v+'form">'+
2044-
'<div id="'+id+'_geo_close" class="'+v+'_dismiss" title="close">&times;</div>'+
2044+
'<div id="'+id+'_geo_close" class="'+v+'_dismiss" title="'+s.getPhrase('close')+'">&times;</div>'+
20452045
'<div style="text-align:center;margin:2px;">'+s.getPhrase('position')+'</div>'+
20462046
'<div style="text-align:center;">'+
20472047
'<input type="text" id="'+id+'_lat" value="" style="padding-right:10px!important;">'+
@@ -2851,8 +2851,8 @@ VirtualSky.prototype.updateClock = function(d){
28512851
}
28522852
// Call any calendar-based events
28532853
VirtualSky.prototype.calendarUpdate = function(){
2854-
for(var e = 0; e < sky.calendarevents.length; e++){
2855-
if(is(sky.calendarevents[e],"function")) sky.calendarevents[e].call(sky);
2854+
for(var e = 0; e < this.calendarevents.length; e++){
2855+
if(is(this.calendarevents[e],"function")) this.calendarevents[e].call(this);
28562856
}
28572857
return this;
28582858
}

0 commit comments

Comments
 (0)