/* 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/. */ var loop = loop || {}; loop.roomViews = (function(mozL10n) { "use strict"; var ROOM_STATES = loop.store.ROOM_STATES; var SCREEN_SHARE_STATES = loop.shared.utils.SCREEN_SHARE_STATES; var FAILURE_DETAILS = loop.shared.utils.FAILURE_DETAILS; var sharedActions = loop.shared.actions; var sharedMixins = loop.shared.mixins; var sharedUtils = loop.shared.utils; var sharedViews = loop.shared.views; /** * ActiveRoomStore mixin. * @type {Object} */ var ActiveRoomStoreMixin = { mixins: [Backbone.Events], propTypes: { roomStore: React.PropTypes.instanceOf(loop.store.RoomStore).isRequired }, componentWillMount: function() { this.listenTo(this.props.roomStore, "change:activeRoom", this._onActiveRoomStateChanged); this.listenTo(this.props.roomStore, "change:error", this._onRoomError); this.listenTo(this.props.roomStore, "change:savingContext", this._onRoomSavingContext); }, componentWillUnmount: function() { this.stopListening(this.props.roomStore); }, _onActiveRoomStateChanged: function() { // Only update the state if we're mounted, to avoid the problem where // stopListening doesn't nuke the active listeners during a event // processing. if (this.isMounted()) { this.setState(this.props.roomStore.getStoreState("activeRoom")); } }, _onRoomError: function() { // Only update the state if we're mounted, to avoid the problem where // stopListening doesn't nuke the active listeners during a event // processing. if (this.isMounted()) { this.setState({error: this.props.roomStore.getStoreState("error")}); } }, _onRoomSavingContext: function() { // Only update the state if we're mounted, to avoid the problem where // stopListening doesn't nuke the active listeners during a event // processing. if (this.isMounted()) { this.setState({savingContext: this.props.roomStore.getStoreState("savingContext")}); } }, getInitialState: function() { var storeState = this.props.roomStore.getStoreState("activeRoom"); return _.extend({ // Used by the UI showcase. roomState: this.props.roomState || storeState.roomState, savingContext: false }, storeState); } }; /** * Used to display errors in direct calls and rooms to the user. */ var FailureInfoView = React.createClass({ propTypes: { failureReason: React.PropTypes.string.isRequired }, /** * Returns the translated message appropraite to the failure reason. * * @return {String} The translated message for the failure reason. */ _getMessage: function() { switch (this.props.failureReason) { case FAILURE_DETAILS.NO_MEDIA: case FAILURE_DETAILS.UNABLE_TO_PUBLISH_MEDIA: return mozL10n.get("no_media_failure_message"); case FAILURE_DETAILS.TOS_FAILURE: return mozL10n.get("tos_failure_message", { clientShortname: mozL10n.get("clientShortname2") }); case FAILURE_DETAILS.ICE_FAILED: return mozL10n.get("ice_failure_message"); default: return mozL10n.get("generic_failure_message"); } }, render: function() { return (

{this._getMessage()}

); } }); /** * Something went wrong view. Displayed when there's a big problem. */ var RoomFailureView = React.createClass({ mixins: [ sharedMixins.AudioMixin ], propTypes: { dispatcher: React.PropTypes.instanceOf(loop.Dispatcher).isRequired, failureReason: React.PropTypes.string, mozLoop: React.PropTypes.object.isRequired }, componentDidMount: function() { this.play("failure"); }, handleRejoinCall: function() { this.props.dispatcher.dispatch(new sharedActions.JoinRoom()); }, render: function() { var settingsMenuItems = [ { id: "feedback" }, { id: "help" } ]; var btnTitle; if (this.props.failureReason === FAILURE_DETAILS.ICE_FAILED) { btnTitle = mozL10n.get("retry_call_button"); } else { btnTitle = mozL10n.get("rejoin_button"); } return (
); } }); var SocialShareDropdown = React.createClass({ propTypes: { dispatcher: React.PropTypes.instanceOf(loop.Dispatcher).isRequired, roomUrl: React.PropTypes.string, show: React.PropTypes.bool.isRequired, socialShareProviders: React.PropTypes.array }, handleAddServiceClick: function(event) { event.preventDefault(); this.props.dispatcher.dispatch(new sharedActions.AddSocialShareProvider()); }, handleProviderClick: function(event) { event.preventDefault(); var origin = event.currentTarget.dataset.provider; var provider = this.props.socialShareProviders .filter(function(socialProvider) { return socialProvider.origin === origin; })[0]; this.props.dispatcher.dispatch(new sharedActions.ShareRoomUrl({ provider: provider, roomUrl: this.props.roomUrl, previews: [] })); }, render: function() { // Don't render a thing when no data has been fetched yet. if (!this.props.socialShareProviders) { return null; } var cx = React.addons.classSet; var shareDropdown = cx({ "share-service-dropdown": true, "dropdown-menu": true, "visually-hidden": true, "hide": !this.props.show }); return ( ); } }); /** * Desktop room invitation view (overlay). */ var DesktopRoomInvitationView = React.createClass({ statics: { TRIGGERED_RESET_DELAY: 2000 }, mixins: [sharedMixins.DropdownMenuMixin(".room-invitation-overlay")], propTypes: { dispatcher: React.PropTypes.instanceOf(loop.Dispatcher).isRequired, error: React.PropTypes.object, mozLoop: React.PropTypes.object.isRequired, onAddContextClick: React.PropTypes.func, onEditContextClose: React.PropTypes.func, // This data is supplied by the activeRoomStore. roomData: React.PropTypes.object.isRequired, savingContext: React.PropTypes.bool, show: React.PropTypes.bool.isRequired, showEditContext: React.PropTypes.bool.isRequired, socialShareProviders: React.PropTypes.array }, getInitialState: function() { return { copiedUrl: false, newRoomName: "" }; }, handleEmailButtonClick: function(event) { event.preventDefault(); var roomData = this.props.roomData; var contextURL = roomData.roomContextUrls && roomData.roomContextUrls[0]; this.props.dispatcher.dispatch( new sharedActions.EmailRoomUrl({ roomUrl: roomData.roomUrl, roomDescription: contextURL && contextURL.description, from: "conversation" })); }, handleCopyButtonClick: function(event) { event.preventDefault(); this.props.dispatcher.dispatch(new sharedActions.CopyRoomUrl({ roomUrl: this.props.roomData.roomUrl, from: "conversation" })); this.setState({copiedUrl: true}); setTimeout(this.resetTriggeredButtons, this.constructor.TRIGGERED_RESET_DELAY); }, /** * Reset state of triggered buttons if necessary */ resetTriggeredButtons: function() { if (this.state.copiedUrl) { this.setState({copiedUrl: false}); } }, handleShareButtonClick: function(event) { event.preventDefault(); var providers = this.props.socialShareProviders; // If there are no providers available currently, save a click by dispatching // the 'AddSocialShareProvider' right away. if (!providers || !providers.length) { this.props.dispatcher.dispatch(new sharedActions.AddSocialShareProvider()); return; } this.toggleDropdownMenu(); }, handleEditContextClose: function() { if (this.props.onEditContextClose) { this.props.onEditContextClose(); } }, render: function() { if (!this.props.show) { return null; } var cx = React.addons.classSet; return (

{mozL10n.get("invite_header_text2")}

{mozL10n.get(this.state.copiedUrl ? "invite_copied_link_button" : "invite_copy_link_button")}

{mozL10n.get("invite_email_link_button")}

); } }); var DesktopRoomEditContextView = React.createClass({ mixins: [React.addons.LinkedStateMixin], maxRoomNameLength: 124, propTypes: { dispatcher: React.PropTypes.instanceOf(loop.Dispatcher).isRequired, error: React.PropTypes.object, mozLoop: React.PropTypes.object.isRequired, onClose: React.PropTypes.func, // This data is supplied by the activeRoomStore. roomData: React.PropTypes.object.isRequired, savingContext: React.PropTypes.bool.isRequired, show: React.PropTypes.bool.isRequired }, componentWillMount: function() { this._fetchMetadata(); }, componentWillReceiveProps: function(nextProps) { var newState = {}; // When the 'show' prop is changed from outside this component, we do need // to update the state. if (("show" in nextProps) && nextProps.show !== this.props.show) { newState.show = nextProps.show; if (nextProps.show) { this._fetchMetadata(); } } // When we receive an update for the `roomData` property, make sure that // the current form fields reflect reality. This is necessary, because the // form state is maintained in the components' state. if (nextProps.roomData) { // Right now it's only necessary to update the form input states when // they contain no text yet. if (!this.state.newRoomName && nextProps.roomData.roomName) { newState.newRoomName = nextProps.roomData.roomName; } var url = this._getURL(nextProps.roomData); if (url) { if (!this.state.newRoomURL && url.location) { newState.newRoomURL = url.location; } if (!this.state.newRoomDescription && url.description) { newState.newRoomDescription = url.description; } if (!this.state.newRoomThumbnail && url.thumbnail) { newState.newRoomThumbnail = url.thumbnail; } } } // Feature support: when a context save completed without error, we can // close the context edit form. if (("savingContext" in nextProps) && this.props.savingContext && this.props.savingContext !== nextProps.savingContext && this.state.show && !this.props.error && !nextProps.error) { newState.show = false; if (this.props.onClose) { this.props.onClose(); } } if (Object.getOwnPropertyNames(newState).length) { this.setState(newState); } }, getInitialState: function() { var url = this._getURL(); return { // `availableContext` prop only used in tests. availableContext: null, show: this.props.show, newRoomName: this.props.roomData.roomName || "", newRoomURL: url && url.location || "", newRoomDescription: url && url.description || "", newRoomThumbnail: url && url.thumbnail || "" }; }, _fetchMetadata: function() { this.props.mozLoop.getSelectedTabMetadata(function(metadata) { var previewImage = metadata.favicon || ""; var description = metadata.title || metadata.description; var metaUrl = metadata.url; this.setState({ availableContext: { previewImage: previewImage, description: description, url: metaUrl } }); }.bind(this)); }, handleCloseClick: function(event) { event.stopPropagation(); event.preventDefault(); this.setState({ show: false }); if (this.props.onClose) { this.props.onClose(); } }, handleContextClick: function(event) { event.stopPropagation(); event.preventDefault(); var url = this._getURL(); if (!url || !url.location) { return; } var mozLoop = this.props.mozLoop; mozLoop.openURL(url.location); mozLoop.telemetryAddValue("LOOP_ROOM_CONTEXT_CLICK", 1); }, handleFormSubmit: function(event) { event && event.preventDefault(); this.props.dispatcher.dispatch(new sharedActions.UpdateRoomContext({ roomToken: this.props.roomData.roomToken, newRoomName: this.state.newRoomName, newRoomURL: this.state.newRoomURL, newRoomDescription: this.state.newRoomDescription, newRoomThumbnail: this.state.newRoomThumbnail })); }, handleTextareaKeyDown: function(event) { // Submit the form as soon as the user press Enter in that field // Note: We're using a textarea instead of a simple text input to display // placeholder and entered text on two lines, to circumvent l10n // rendering/UX issues for some locales. if (event.which === 13) { this.handleFormSubmit(event); } }, /** * Utility function to extract URL context data from the `roomData` property * that can also be supplied as an argument. * * @param {Object} roomData Optional room data object to use, equivalent to * the activeRoomStore state. * @return {Object} The first context URL found on the `roomData` object. */ _getURL: function(roomData) { roomData = roomData || this.props.roomData; return this.props.roomData.roomContextUrls && this.props.roomData.roomContextUrls[0]; }, render: function() { if (!this.state.show) { return null; } var url = this._getURL(); var thumbnail = url && url.thumbnail || "loop/shared/img/icons-16x16.svg#globe"; var urlDescription = url && url.description || ""; var location = url && url.location || ""; var cx = React.addons.classSet; var availableContext = this.state.availableContext; return (

{mozL10n.get("rooms_change_failed_label")}

{mozL10n.get("context_inroom_header")}