-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwebclient_utils.js
105 lines (97 loc) · 2.64 KB
/
webclient_utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return unescape(results[1]);
}
function playSfx(tagId) {
filename = $("#" + tagId).attr("src");
var snd = new Audio(filename);
snd.play();
}
// shim layer with setTimeout fallback
// Written by Paul Irish
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element){
window.setTimeout(callback, 1000 / 60);
};
})();
var localized_strings = null;
function getLocalString(key) {
// TODO How do we preload strings.json only once?
// method 1 - XHR it when loading page (before asset loader?),
// keep in memory
// method 2 - embed strings.json in the page using templating,
// read it out here.
if (!localized_strings) {
localized_strings = $("#localized_strings");
if (!localized_strings) {
return "";
}
localized_strings = JSON.parse(localized_strings.html());
}
var str = localized_strings[key];
if (str) {
return str;
} else {
return "";
}
}
function AssetLoader() {
this._things = [];
this._thingsToLoad = 0;
this._thingsLoaded = 0;
}
AssetLoader.prototype = {
getImgFor: function(url) {
for (var i = 0; i < this._things.length; i++) {
if (this._things[i].url == url) {
return this._things[i].tag;
}
}
return null;
},
add: function(url, type) {
var tag = this.getImgFor(url);
if (!tag) {
this._thingsToLoad++;
var tag = new Image();
var thing = { url: url,
tag: tag };
this._things.push(thing);
}
return tag;
},
loadThemAll: function(callback, updateFunc) {
var self = this;
// Edge case where nothing has been added - call callback immediately:
if (this._thingsToLoad == 0) {
callback();
return;
}
for (var t = 0; t < this._thingsToLoad; t++) {
(function(thing) {
thing.tag.onload = function() {
self._thingsLoaded ++;
if (updateFunc) {
updateFunc( self._thingsLoaded / self._thingsToLoad );
}
if (self._thingsLoaded == self._thingsToLoad) {
callback();
}
};
thing.tag.src = thing.url;
})(this._things[t]);
}
}
};