/** @jsx React.DOM */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ /* jshint newcap:false */ /* global loop:true, React */ var loop = loop || {}; loop.shared = loop.shared || {}; loop.shared.views = (function(_, OT, l10n) { "use strict"; var sharedModels = loop.shared.models; var __ = l10n.get; /** * L10n view. Translates resulting view DOM fragment once rendered. */ var L10nView = (function() { var L10nViewImpl = Backbone.View.extend(), // Original View constructor originalExtend = L10nViewImpl.extend; // Original static extend fn /** * Patches View extend() method so we can hook and patch any declared render * method. * * @return {Backbone.View} Extended view with patched render() method. */ L10nViewImpl.extend = function() { var ExtendedView = originalExtend.apply(this, arguments), originalRender = ExtendedView.prototype.render; /** * Wraps original render() method to translate contents once they're * rendered. * * @return {Backbone.View} Extended view instance. */ ExtendedView.prototype.render = function() { if (originalRender) { originalRender.apply(this, arguments); l10n.translate(this.el); } return this; }; return ExtendedView; }; return L10nViewImpl; })(); /** * Base view. */ var BaseView = L10nView.extend({ /** * Hides view element. * * @return {BaseView} */ hide: function() { this.$el.hide(); return this; }, /** * Shows view element. * * @return {BaseView} */ show: function() { this.$el.show(); return this; }, /** * Base render implementation: renders an attached template if available. * * Note: You need to override this if you want to do fancier stuff, eg. * rendering the template using model data. * * @return {BaseView} */ render: function() { if (this.template) { this.$el.html(this.template()); } return this; } }); /** * Media control button. * * Required props: * - {String} scope Media scope, can be "local" or "remote". * - {String} type Media type, can be "audio" or "video". * - {Function} action Function to be executed on click. * - {Enabled} enabled Stream activation status (default: true). */ var MediaControlButton = React.createClass({displayName: 'MediaControlButton', propTypes: { scope: React.PropTypes.string.isRequired, type: React.PropTypes.string.isRequired, action: React.PropTypes.func.isRequired, enabled: React.PropTypes.bool.isRequired }, getDefaultProps: function() { return {enabled: true}; }, handleClick: function() { this.props.action(); }, _getClasses: function() { var cx = React.addons.classSet; // classes var classesObj = { "btn": true, "media-control": true, "local-media": this.props.scope === "local", "muted": !this.props.enabled }; classesObj["btn-mute-" + this.props.type] = true; return cx(classesObj); }, _getTitle: function(enabled) { var prefix = this.props.enabled ? "mute" : "unmute"; var suffix = "button_title"; var msgId = [prefix, this.props.scope, this.props.type, suffix].join("_"); return __(msgId); }, render: function() { return ( /* jshint ignore:start */ React.DOM.button({className: this._getClasses(), title: this._getTitle(), onClick: this.handleClick}) /* jshint ignore:end */ ); } }); /** * Conversation controls. */ var ConversationToolbar = React.createClass({displayName: 'ConversationToolbar', getDefaultProps: function() { return { video: {enabled: true}, audio: {enabled: true} }; }, propTypes: { video: React.PropTypes.object.isRequired, audio: React.PropTypes.object.isRequired, hangup: React.PropTypes.func.isRequired, publishStream: React.PropTypes.func.isRequired }, handleClickHangup: function() { this.props.hangup(); }, handleToggleVideo: function() { this.props.publishStream("video", !this.props.video.enabled); }, handleToggleAudio: function() { this.props.publishStream("audio", !this.props.audio.enabled); }, render: function() { /* jshint ignore:start */ return ( React.DOM.ul({className: "conversation-toolbar"}, React.DOM.li(null, React.DOM.button({className: "btn btn-hangup", onClick: this.handleClickHangup, title: __("hangup_button_title")})), React.DOM.li(null, MediaControlButton({action: this.handleToggleVideo, enabled: this.props.video.enabled, scope: "local", type: "video"})), React.DOM.li(null, MediaControlButton({action: this.handleToggleAudio, enabled: this.props.audio.enabled, scope: "local", type: "audio"})) ) ); /* jshint ignore:end */ } }); var ConversationView = React.createClass({displayName: 'ConversationView', mixins: [Backbone.Events], propTypes: { sdk: React.PropTypes.object.isRequired, model: React.PropTypes.object.isRequired }, // height set to 100%" to fix video layout on Google Chrome // @see https://bugzilla.mozilla.org/show_bug.cgi?id=1020445 publisherConfig: { insertMode: "append", width: "100%", height: "100%", style: { bugDisplayMode: "off", buttonDisplayMode: "off", nameDisplayMode: "off" } }, getInitialState: function() { return { video: {enabled: false}, audio: {enabled: false} }; }, componentDidMount: function() { this.listenTo(this.props.model, "session:connected", this.startPublishing); this.listenTo(this.props.model, "session:stream-created", this._streamCreated); this.listenTo(this.props.model, ["session:peer-hungup", "session:network-disconnected", "session:ended"].join(" "), this.stopPublishing); this.props.model.startSession(); }, componentWillUnmount: function() { // Unregister all local event listeners this.stopListening(); this.hangup(); }, hangup: function() { this.stopPublishing(); this.props.model.endSession(); }, /** * Subscribes and attaches each created stream to a DOM element. * * XXX: for now we only support a single remote stream, hence a single DOM * element. * * http://tokbox.com/opentok/libraries/client/js/reference/StreamEvent.html * * @param {StreamEvent} event */ _streamCreated: function(event) { var incoming = this.getDOMNode().querySelector(".remote"); event.streams.forEach(function(stream) { if (stream.connection.connectionId !== this.props.model.session.connection.connectionId) { this.props.model.session.subscribe(stream, incoming, this.publisherConfig); } }, this); }, /** * Publishes remote streams available once a session is connected. * * http://tokbox.com/opentok/libraries/client/js/reference/SessionConnectEvent.html * * @param {SessionConnectEvent} event */ startPublishing: function(event) { var outgoing = this.getDOMNode().querySelector(".local"); // XXX move this into its StreamingVideo component? this.publisher = this.props.sdk.initPublisher( outgoing, this.publisherConfig); // Suppress OT GuM custom dialog, see bug 1018875 this.listenTo(this.publisher, "accessDialogOpened accessDenied", function(event) { event.preventDefault(); }); this.listenTo(this.publisher, "streamCreated", function(event) { this.setState({ audio: {enabled: event.stream.hasAudio}, video: {enabled: event.stream.hasVideo} }); }.bind(this)); this.listenTo(this.publisher, "streamDestroyed", function() { this.setState({ audio: {enabled: false}, video: {enabled: false} }); }.bind(this)); this.props.model.session.publish(this.publisher); }, /** * Toggles streaming status for a given stream type. * * @param {String} type Stream type ("audio" or "video"). * @param {Boolean} enabled Enabled stream flag. */ publishStream: function(type, enabled) { if (type === "audio") { this.publisher.publishAudio(enabled); this.setState({audio: {enabled: enabled}}); } else { this.publisher.publishVideo(enabled); this.setState({video: {enabled: enabled}}); } }, /** * Unpublishes local stream. */ stopPublishing: function() { // Unregister listeners for publisher events this.stopListening(this.publisher); this.props.model.session.unpublish(this.publisher); }, render: function() { /* jshint ignore:start */ return ( React.DOM.div({className: "conversation"}, ConversationToolbar({video: this.state.video, audio: this.state.audio, publishStream: this.publishStream, hangup: this.hangup}), React.DOM.div({className: "media nested"}, React.DOM.div({className: "video_wrapper remote_wrapper"}, React.DOM.div({className: "video_inner remote"}) ), React.DOM.div({className: "local"}) ) ) ); /* jshint ignore:end */ } }); /** * Notification view. */ var NotificationView = BaseView.extend({ template: _.template([ '
', '
', '