root/src/public/js/ThumbnailCache.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
function ThumbnailCache(duration) {
    this.thumbnails = {};
    this.openRequests = {};
    this.spacing = 5;
    if (duration > 0) {
        this.loadAll(duration);
    }
}

ThumbnailCache.prototype.get = function(second, callback) {
    second = this.secondToSpacing(second);
    if (this.thumbnails[second]) {
        if (callback) {
            callback(this.thumbnails[second]);
        }
        return;
    }
    if (this.openRequests[second]) {
        //this.openRequests.callbacks.push(callback);
        return;
    }
    var self = this;
    this.openRequests[second] = true;
    var img = new Image();
    img.onload = function(data) {
        delete self.openRequests[second];
        self.thumbnails[second] = img;
        if (callback) {
            callback(img);
        }
    };
    img.src ="api/player/thumbnail?" + second;
}

ThumbnailCache.prototype.loadAll = function(duration) {
    for (var i = 0; i < duration; i += this.spacing) {
        this.get(i);
    }
}

ThumbnailCache.prototype.isLoading = function() {
    for (var key in this.openRequests) {
        return true;
    }
    return false;
}

ThumbnailCache.prototype.secondToSpacing = function(second) {
    return second - (second % this.spacing);
}