aboutsummaryrefslogtreecommitdiffstats
path: root/public/js/controllers.js
blob: e5d56c5dae2e049768a128aa26bd97d5993a46bf (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
function PlayerCtrl($scope, $http) {
    function bufferImage(url) {
        console.log('buffering ' + url);
        var img = new Image();
        img.src = url;
    }
    
    var m = window.location.pathname.match(/\/song\/([^\/]+)\/([^\/]+)/);
    if (m.length < 2) {
        alert('Invalid song path')
        return;
    }
    var user = m[1];
    var songname = m[2];

    // Get GIF data for the song
    song_api = 'http://' + appConfig.context + '/1/song/' + 
        user + "/" + songname + "/";
    $http.get(song_api).success(function(data) {
        $scope.song = data;
    });

    // Get GIF data for the song
    suggest_api = 'http://' + appConfig.context + '/1/suggest/' + 
        user + "/" + songname + "/";
    var available_images = [];
    $http.get(suggest_api).success(function(data) {
        $scope.base_url = data.base_url;

        //for (gif in data.images) {
        //    bufferImage(data.base_url + gif.filename);
        //}

        image_slots = Math.floor(($('#side_bottom').width() / 140) - 2);
        // image_slots = 3;
        console.log('image_slots ' + image_slots + ' ' + $('#side_bottom').width());

        $scope.images = data.images.slice(0, image_slots);
        available_images.push.apply(available_images, data.images.slice(image_slots));

        setTimeout(onReadyCallback, 0);
    });

    console.log('available images init')
    console.log(available_images)

    $('#suggest_more').click(function() {
        console.log('available images on suggest more');
        console.log(available_images);
        $('.gif_box').each(function(index) {
            new_image_obj = available_images.pop()
            if (!new_image_obj) {
                $http.get(suggest_api).success(function(data) {
                    $('.gif_box').each(function(index) {
                        if ($(this).attr('style').indexOf('loading.gif') != -1) {
                            new_image = $scope.base_url + data.images.pop().filename;
                            $(this).attr('style', 'background-image: url(' + new_image + ');');
                        };
                    });

                    available_images.push.apply(available_images, data.images);
                });
                new_image = '/img/loading.gif';
            } else {
                new_image = $scope.base_url + new_image_obj.filename;
            }
            $(this).attr('style', 'background-image: url(' + new_image + ');');
        });

        return false;
    });

    player.init(currentSong);

    $('#gif_block').click(function() {
        var $this = $(this);
        if ($this.hasClass('playing')) {
            player.pause();
            $this.removeClass('playing');
            $('#gif_inner').stop();
        } else {
            player.play(triggerTime);
            $this.addClass('playing');
            var inner_width = -(currentSong.duration * .2);
            $('#gif_inner').animate({
                'left' : inner_width
            }, currentSong.duration, 'linear');
        }
    });

    var nextImageInStrip = 0;
    function triggerTime(ms) {
        renderNewImages(ms);
        removePastImages(ms);
    }

    var imageBuffer = [];

    function renderNewImages(ms) {
        var offset = position_to_timestamp($('#gif_inner').width());
        var loadBuffer = 8000 + offset; //msec
        var renderBuffer = 1000 + offset; //msec
        var max = ms + loadBuffer;
        var gifs = $scope.song.gifs;
        for(var i=nextImageInStrip; i < gifs.length && gifs[i].timestamp < max; i++) {
            (function() {
                var gif = gifs[i];
                var img = bufferImage(gif.gif);
                var insertDelay = gif.timestamp < ms + renderBuffer ? 0 : 
                                    ( gif.timestamp < ms + loadBuffer ? (loadBuffer - renderBuffer - gif.timestamp) : (loadBuffer - renderBuffer) );
                setTimeout(function() {
                    addImage(gif.gif, gif.timestamp);
                }, insertDelay);
                nextImageInStrip++;
            }());
        }
    }



    function addImage(url, ms) {
        var pos = timestamp_to_position(ms);
        
        place_gif({
            left: pos,
            background_url: url
        });
        console.log(url, pos);
    }

    function removePastImages(ms) {
        $('.gif_placed_box').each(function() {
            var $item = $(this);
            var rightPosition = parseInt($item.css('left')) - timestamp_to_position(ms) + parseInt($item.css('width'));
            if (rightPosition < 0) {
                $item.remove();
            }
        });
        
    }

    
    function onReadyCallback() {
        setUpDrag();
        var inner_width = (currentSong.duration * .2);
        $('#gif_inner').width(inner_width);
        setTimeout(function() {
            $('#gif_block').trigger('click');
        }, 1000);
    }
    
    var position_to_timestamp = function(position) {
        return (position / $('#gif_inner').width()) * currentSong.duration;
    };
    
    var timestamp_to_position = function(timestamp) {
        return (timestamp / currentSong.duration) * $('#gif_inner').width();
    };
    
    // Post gif timestamp
    $scope.$on('gmbomt:gif_dropped', function(e, args) {
        post_gif_timestamp({
            gif_url: args.gif_url,
            position: args.position
        });
    });
    var post_gif_timestamp = function(args) {
        var url = '/1/dropgif/' + user + '/' + songname;
        $.post(url,
            {
                user: user,
                gif: args.gif_url,
                timestamp: position_to_timestamp(args.position),
                row: 1
            },
            function(data) {
                if (data.match(/true/)) {
                    console.log('POSTED TIMESTAMP YO');
            }
        });
    };
}