Dynamically load javascript files of components.

This commit is contained in:
FrozenCow 2011-03-31 13:06:34 +02:00
parent 6622db1b2c
commit 1cda538eef
2 changed files with 29 additions and 8 deletions

View File

@ -26,10 +26,6 @@
<script type="text/javascript" src="js/flatmap.js"></script> <script type="text/javascript" src="js/flatmap.js"></script>
<script type="text/javascript" src="js/clock.timeofday.js"></script> <script type="text/javascript" src="js/clock.timeofday.js"></script>
<script type="text/javascript" src="js/clock.digital.js"></script> <script type="text/javascript" src="js/clock.digital.js"></script>
<script type="text/javascript" src="js/chat.js"></script>
<script type="text/javascript" src="js/chatballoon.js"></script>
<script type="text/javascript" src="js/chatbox.js"></script>
<script type="text/javascript" src="js/playermarkers.js"></script>
<script type="text/javascript" src="config.js"></script> <script type="text/javascript" src="config.js"></script>
<script type="text/javascript"> <script type="text/javascript">

View File

@ -24,6 +24,24 @@ componentconstructors['testcomponent'] = function(dynmap, configuration) {
}; };
}; };
function loadjs(url, completed) {
var script = document.createElement('script');
script.setAttribute('src', url);
script.setAttribute('type', 'text/javascript');
var isloaded = false;
script.onload = function() {
if (isloaded) { return; }
isloaded = true;
completed();
};
// Hack for IE, don't know whether this still applies to IE9.
script.onreadystatechange = function() {
script.onload();
};
document.head.appendChild(script);
}
function splitArgs(s) { function splitArgs(s) {
var r = s.split(' '); var r = s.split(' ');
delete arguments[0]; delete arguments[0];
@ -279,12 +297,19 @@ DynMap.prototype = {
me.selectMap(me.defaultworld.defaultmap); me.selectMap(me.defaultworld.defaultmap);
var componentstoload = me.options.components.length;
$.each(me.options.components, function(index, configuration) { $.each(me.options.components, function(index, configuration) {
var componentconstructor = componentconstructors[configuration.type]; loadjs('js/' + configuration.type + '.js', function() {
me.components.push(new componentconstructor(me, configuration)); var componentconstructor = componentconstructors[configuration.type];
me.components.push(new componentconstructor(me, configuration));
componentstoload--;
if (componentstoload == 0) {
// Actually start updating once all components are loaded.
setTimeout(function() { me.update(); }, me.options.updaterate);
}
});
}); });
setTimeout(function() { me.update(); }, me.options.updaterate);
}, },
selectMap: function(map, completed) { selectMap: function(map, completed) {
if (!map) { throw "Cannot select map " + map; } if (!map) { throw "Cannot select map " + map; }