aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/ui/youtube-player.reel/youtube-player.js
blob: 462dca586e339c5c2aae197d57780fe28be96746 (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
183
/* <copyright>
 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
 (c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
 </copyright> */
/*global require,exports,YT */
var Montage = require("montage").Montage,
    Component = require("ui/component").Component;

var YoutubePlayer = exports.YoutubePlayer = Montage.create(Component, {

    // Stores a queue of function calls to make on the player in the draw
    // function
    _playerQueue: {
        enumerable: false,
        value: []
    },

    _ready: {
        enumerable: false,
        value: false
    },

    _player: {
        enumerable: false,
        value: null
    },
    player: {
        get: function() {
            return this._player;
        },
        set: function(value) {
            if (this._player !== value) {
                this._player = value;
            }
        }
    },

    _autoplay: {
        enumerable: false,
        value: false
    },
    autoplay: {
        get: function() {
            return this._autoplay;
        },
        set: function(value) {
            if (this._autoplay !== value) {
                this._autoplay = value;
            }
        }
    },

    _videoId: {
        enumerable: false,
        value: null
    },
    videoId: {
        get: function() {
            return this._videoId;
        },
        set: function(value) {
            if (this._videoId !== value) {
                // TODO handle URLs as well
                this._videoId = value;

                // If the video isn't in the playlist, clear the playlist,
                if (this._playlist && this._playlist.indexOf(value) === -1) {
                    this._playlist = null;
                }
                // TODO if the video is in the playlist use playVideoAt

                if (this._autoplay) {
                    this._playerQueue.push(["loadVideoById", value]);
                } else {
                    this._playerQueue.push(["cueVideoById", value]);
                }
                this.needsDraw = true;
            }
        }
    },

    _playlist: {
        enumerable: false,
        value: null
    },
    playlist: {
        get: function() {
            return this._playlist;
        },
        set: function(value) {
            if (this._playlist !== value) {
                this._playlist = value;

                if (this._autoplay) {
                    this._playerQueue.push(["loadPlaylist", value]);
                } else {
                    this._playerQueue.push(["cuePlaylist", value]);
                }
                this.needsDraw = true;
            }
        }
    },

    play: {
        value: function() {
            this._playerQueue.push("playVideo");
            this.needsDraw = true;
        }
    },
    pause: {
        value: function() {
            this._playerQueue.push("pauseVideo");
            this.needsDraw = true;
        }
    },
    stop: {
        value: function() {
            this._playerQueue.push("stopVideo");
            this.needsDraw = true;
        }
    },

    prepareForDraw: {
        value: function() {
            // Create the callback if it doesn't exist, and make it dispatch
            // an event on the document instead
            if (!window.onYouTubePlayerAPIReady) {
                window.onYouTubePlayerAPIReady = function() {
                    var event = document.createEvent("CustomEvent");
                    event.initEvent("youtubePlayerApiReady", true, true);
                    document.dispatchEvent(event);
                };
            }

            this._element.src +=  (this._videoId ? "/" + this._videoId : "") + "?enablejsapi=1";

            if (typeof YT !== "undefined" && YT.Player) {
                this.handleYoutubePlayerApiReady();
            } else {
                document.addEventListener("youtubePlayerApiReady", this, false);
            }
        }
    },

    handleYoutubePlayerApiReady: {
        value: function(event) {
            console.log("handleYoutubePlayerApiReady");
            document.removeEventListener("youtubePlayerApiReady", this);

            var self = this;
            this.player = new YT.Player(this._element, { events: {
                onReady: function(event) {
                    self._ready = true;
                    self.needsDraw = true;
                }
            }});
        }
    },

    draw: {
        value: function() {
            if (!this._ready) {
                return;
            }

            for (var i = 0, len = this._playerQueue.length; i < len; i++) {
                var fnName, args;
                if (typeof this._playerQueue[i] === "string") {
                    fnName = this._playerQueue[i];
                    args = [];
                } else {
                    fnName = this._playerQueue[i].shift();
                    args = this._playerQueue[i];
                }

                this._player[fnName].apply(this._player, args);
            }

            this._playerQueue.length = 0;
        }
    }
});