aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/examples/youtube-channel-example/youtube-channel.reel/youtube-channel.js
blob: 1f5aa2d19ff04eefe15180b0f34fc2dddd20ee0e (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
/* <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*/
var Montage = require("montage").Montage,
    Component = require("montage/ui/component").Component,
    Uuid = require("montage/core/uuid").Uuid;


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

    _userRe: {
        enumerable: false,
        value: /youtube.com\/(user\/)?([a-z0-9]+)/i
    },

    _channelUrl: {
        enumerable: false,
        value: null
    },
    channelUrl: {
        depends: ["channel"],
        get: function() {
            return this._channelUrl;
        },
        set: function(value, fromChannel) {
            if (this._channelUrl !== value) {
                this._channelUrl = value;

                // prevent infinite loop
                if (!fromChannel) {
                    var match = this._userRe.exec(value);
                    if (match && match[2]) {
                        Object.getPropertyDescriptor(this, "channel").set.call(this, match[2], true);
                    }
                }
            }
        }
    },

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

                // prevent infinite loop
                if (!fromUrl) {
                    Object.getPropertyDescriptor(this, "channelUrl").set.call(this, "http://www.youtube.com/user/" + value, true);
                }

                this._loadChannel();
            }
        }
    },

    _loadChannel: {
        enumerable: false,
        value: function() {
            var self = this;

            var callbackName = "scriptCallback" + Uuid.generate().replace(/-/g, "_");

            window[callbackName] = function(data) {
                self.handleData(data);
                delete window[callbackName];
            };

            // create url
            var url = "http://gdata.youtube.com/feeds/api/users/" + this._channel + "/uploads?v=2&alt=json-in-script&callback=" + callbackName;

            var script = document.createElement("script");
            script.src = url;
            this._element.appendChild(script);
        }
    },

    handleData: {
        value: function(data) {
            var entries = data.feed.entry || [];

            var playlist = [];

            for (var i = 0, len = entries.length; i < len; i++) {
                var id = entries[i]["media$group"]["yt$videoid"]["$t"];
                playlist.push(id);
            }

            this.player.playlist = playlist;
        }
    }

});