/* This file contains proprietary software owned by Motorola Mobility, Inc.
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
*/ /*global require,exports*/ var Montage = require("montage").Montage, Component = require("montage/ui/component").Component, Uuid = require("montage/core/uuid"); var YoutubeChannel = exports.YoutubeChannel = Montage.create(Component, { _savedReference: { value: null }, _userRe: { value: /youtube.com\/(user\/)?([a-z0-9]+)/i }, imageA: { value: null }, imageB: { value: null }, imageC: { value: null }, _popupElement: { value: null }, _shouldShowPopup: { value: false }, _channelUrl: { 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; this._script = document.createElement("script"); this._script.src = url; this.needsDraw = true; } }, _script: { enumerable: false, value: null }, prepareForDraw: { value: function() { this.imageA.element.addEventListener("click", this, false); this.imageB.element.addEventListener("click", this, false); this.imageC.element.addEventListener("click", this, false); this._positionPopup(); this._savedReference = this.element; } }, draw: { value: function() { var self = this; if (this._script) { if(this.element.childNodes.length < 1) { this._savedReference.appendChild(this._script); } else { this.element.appendChild(this._script); } this._script = null; } if (this._entries) { this.imageA.src = (this._entries[0]) ? this._entries[0]["media$group"]["media$thumbnail"][0].url : ""; this.imageB.src = (this._entries[1]) ? this._entries[1]["media$group"]["media$thumbnail"][0].url : ""; this.imageC.src = (this._entries[2]) ? this._entries[2]["media$group"]["media$thumbnail"][0].url : ""; } if (this._videoId) { this.player.videoId = this._videoId; this._videoId = null; } if (this._shouldShowPopup) { this._positionPopup(); // Need the video to be on top this.element.style.zIndex = 9000; // Fix for Canary where the thumbnail in the video doesn't // change until the CSS transition has finished, so wait for // it to change before starting the animation window.setTimeout(function() { self._element.classList.add("show"); self._popupElement.classList.add("show"); }, 50); if (window.Touch) { document.addEventListener('touchstart', this, false); } else { document.addEventListener('mousedown', this, false); document.addEventListener('keyup', this, false); } } else { this.element.classList.remove("show"); this._popupElement.classList.remove("show"); this.player.stop(); if (window.Touch) { document.removeEventListener('touchstart', this); } else { document.removeEventListener('mousedown', this); document.removeEventListener('keyup', this); } // take the video from the top only once it's stopped animating window.setTimeout(function() { self.element.style.zIndex = null; }, 500); } } }, _entries: { enumerable: false, value: null }, _positionPopup: { value: function() { var doc = this._element.ownerDocument; this.player.width = doc.width; this.player.height = doc.height; // // Chrome // viewport.insertBefore(this._popupElement, viewport.firstChild); // this._popupElement.style.top = viewport.offsetTop; // this._popupElement.style.left = viewport.offsetLeft; var computedStyle = window.getComputedStyle(doc.body); // Canary // remove the body margin this._popupElement.style.top = - (this._element.offsetTop || 0) - parseInt(computedStyle.marginTop, 10) + 'px'; this._popupElement.style.left = - (this._element.offsetLeft || 0) - parseInt(computedStyle.marginLeft, 10) + 'px'; } }, handleClick: { value: function(event) { switch(event.target.dataset.montageId) { case "imageA": this._videoId = this._entries[0]["media$group"]["yt$videoid"]["$t"]; break; case "imageB": this._videoId = this._entries[1]["media$group"]["yt$videoid"]["$t"]; break; case "imageC": this._videoId = this._entries[2]["media$group"]["yt$videoid"]["$t"]; break; } this._shouldShowPopup = true; this.needsDraw = true; } }, handleTouchStart: { value: function(event) { this._shouldShowPopup = false; this.needsDraw = true; } }, handleMousedown: { value: function(event) { if (event.button === 0) { this._shouldShowPopup = false; this.needsDraw = true; } } }, handleKeyup: { value: function(event) { this._shouldShowPopup = false; this.needsDraw = true; } }, handleData: { value: function(data) { this._entries = data.feed.entry || []; this.needsDraw = true; } }, serializeProperties: { value: function(serializer) { serializer.set("element", this.element); serializer.set("channelUrl", this.channelUrl); serializer.set("channel", this.channel); } } });