-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.js
118 lines (87 loc) · 2.78 KB
/
load.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
106
107
108
109
110
111
112
113
114
115
116
117
118
clippy.BASE_PATH = '//s3.amazonaws.com/clippy.js/Agents/';
clippy.load = function (name, successCb, failCb) {
var path = clippy.BASE_PATH + name;
var mapDfd = clippy.load._loadMap(path);
var agentDfd = clippy.load._loadAgent(name, path);
var soundsDfd = clippy.load._loadSounds(name, path);
var data;
agentDfd.done(function (d) {
data = d;
});
var sounds;
soundsDfd.done(function (d) {
sounds = d;
});
// wrapper to the success callback
var cb = function () {
var a = new clippy.Agent(path, data,sounds);
successCb(a);
};
$.when(mapDfd, agentDfd, soundsDfd).done(cb).fail(failCb);
};
clippy.load._maps = {};
clippy.load._loadMap = function (path) {
var dfd = clippy.load._maps[path];
if (dfd) return dfd;
// set dfd if not defined
dfd = clippy.load._maps[path] = $.Deferred();
var src = path + '/map.png';
var img = new Image();
img.onload = dfd.resolve;
img.onerror = dfd.reject;
// start loading the map;
img.setAttribute('src', src);
return dfd.promise();
};
clippy.load._sounds = {};
clippy.load._loadSounds = function (name, path) {
var dfd = clippy.load._sounds[name];
if (dfd) return dfd;
// set dfd if not defined
dfd = clippy.load._sounds[name] = $.Deferred();
var audio = document.createElement('audio');
var canPlayMp3 = !!audio.canPlayType && "" != audio.canPlayType('audio/mpeg');
var canPlayOgg = !!audio.canPlayType && "" != audio.canPlayType('audio/ogg; codecs="vorbis"');
if (!canPlayMp3 && !canPlayOgg) {
dfd.resolve({});
} else {
var src = path + (canPlayMp3 ? '/sounds-mp3.js' : '/sounds-ogg.js');
// load
clippy.load._loadScript(src);
}
return dfd.promise()
};
clippy.load._data = {};
clippy.load._loadAgent = function (name, path) {
var dfd = clippy.load._data[name];
if (dfd) return dfd;
dfd = clippy.load._getAgentDfd(name);
var src = path + '/agent.js';
clippy.load._loadScript(src);
return dfd.promise();
};
clippy.load._loadScript = function (src) {
var script = document.createElement('script');
script.setAttribute('src', src);
script.setAttribute('async', 'async');
script.setAttribute('type', 'text/javascript');
document.head.appendChild(script);
};
clippy.load._getAgentDfd = function (name) {
var dfd = clippy.load._data[name];
if (!dfd) {
dfd = clippy.load._data[name] = $.Deferred();
}
return dfd;
};
clippy.ready = function (name, data) {
var dfd = clippy.load._getAgentDfd(name);
dfd.resolve(data);
};
clippy.soundsReady = function (name, data) {
var dfd = clippy.load._sounds[name];
if (!dfd) {
dfd = clippy.load._sounds[name] = $.Deferred();
}
dfd.resolve(data);
};