/** @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.panel = (function(_, mozL10n) {
"use strict";
var sharedViews = loop.shared.views;
var sharedModels = loop.shared.models;
var sharedMixins = loop.shared.mixins;
var sharedActions = loop.shared.actions;
var Button = sharedViews.Button;
var ButtonGroup = sharedViews.ButtonGroup;
var ContactsList = loop.contacts.ContactsList;
var ContactDetailsForm = loop.contacts.ContactDetailsForm;
var __ = mozL10n.get; // aliasing translation function as __ for concision
var TabView = React.createClass({
propTypes: {
buttonsHidden: React.PropTypes.bool,
// The selectedTab prop is used by the UI showcase.
selectedTab: React.PropTypes.string
},
getDefaultProps: function() {
return {
buttonsHidden: false,
selectedTab: "call"
};
},
getInitialState: function() {
return {selectedTab: this.props.selectedTab};
},
handleSelectTab: function(event) {
var tabName = event.target.dataset.tabName;
this.setState({selectedTab: tabName});
},
render: function() {
var cx = React.addons.classSet;
var tabButtons = [];
var tabs = [];
React.Children.forEach(this.props.children, function(tab, i) {
// Filter out null tabs (eg. rooms when the feature is disabled)
if (!tab) {
return;
}
var tabName = tab.props.name;
var isSelected = (this.state.selectedTab == tabName);
if (!tab.props.hidden) {
tabButtons.push(
);
}
tabs.push(
{tab.props.children}
);
}, this);
return (
{!this.props.buttonsHidden
?
{tabButtons}
: null}
{tabs}
);
}
});
var Tab = React.createClass({
render: function() {
return null;
}
});
/**
* Availability drop down menu subview.
*/
var AvailabilityDropdown = React.createClass({
mixins: [sharedMixins.DropdownMenuMixin],
getInitialState: function() {
return {
doNotDisturb: navigator.mozLoop.doNotDisturb
};
},
// XXX target event can either be the li, the span or the i tag
// this makes it easier to figure out the target by making a
// closure with the desired status already passed in.
changeAvailability: function(newAvailabilty) {
return function(event) {
// Note: side effect!
switch (newAvailabilty) {
case 'available':
this.setState({doNotDisturb: false});
navigator.mozLoop.doNotDisturb = false;
break;
case 'do-not-disturb':
this.setState({doNotDisturb: true});
navigator.mozLoop.doNotDisturb = true;
break;
}
this.hideDropdownMenu();
}.bind(this);
},
render: function() {
// XXX https://github.com/facebook/react/issues/310 for === htmlFor
var cx = React.addons.classSet;
var availabilityStatus = cx({
'status': true,
'status-dnd': this.state.doNotDisturb,
'status-available': !this.state.doNotDisturb
});
var availabilityDropdown = cx({
'dropdown-menu': true,
'hide': !this.state.showMenu
});
var availabilityText = this.state.doNotDisturb ?
__("display_name_dnd_status") :
__("display_name_available_status");
return (
);
}
});
/**
* Panel settings (gear) menu.
*/
var SettingsDropdown = React.createClass({
mixins: [sharedMixins.DropdownMenuMixin],
handleClickSettingsEntry: function() {
// XXX to be implemented at the same time as unhiding the entry
},
handleClickAccountEntry: function() {
navigator.mozLoop.openFxASettings();
},
handleClickAuthEntry: function() {
if (this._isSignedIn()) {
navigator.mozLoop.logOutFromFxA();
} else {
navigator.mozLoop.logInToFxA();
}
},
_isSignedIn: function() {
return !!navigator.mozLoop.userProfile;
},
render: function() {
var cx = React.addons.classSet;
// For now all of the menu entries require FxA so hide the whole gear if FxA is disabled.
if (!navigator.mozLoop.fxAEnabled) {
return null;
}
return (
);
}
});
/**
* Call url result view.
*/
var CallUrlResult = React.createClass({
mixins: [sharedMixins.DocumentVisibilityMixin],
propTypes: {
callUrl: React.PropTypes.string,
callUrlExpiry: React.PropTypes.number,
notifications: React.PropTypes.object.isRequired,
client: React.PropTypes.object.isRequired
},
getInitialState: function() {
return {
pending: false,
copied: false,
callUrl: this.props.callUrl || "",
callUrlExpiry: 0
};
},
/**
* Provided by DocumentVisibilityMixin. Schedules retrieval of a new call
* URL everytime the panel is reopened.
*/
onDocumentVisible: function() {
this._fetchCallUrl();
},
/**
* Returns a random 5 character string used to identify
* the conversation.
* XXX this will go away once the backend changes
*/
conversationIdentifier: function() {
return Math.random().toString(36).substring(5);
},
componentDidMount: function() {
// If we've already got a callURL, don't bother requesting a new one.
// As of this writing, only used for visual testing in the UI showcase.
if (this.state.callUrl.length) {
return;
}
this._fetchCallUrl();
},
/**
* Fetches a call URL.
*/
_fetchCallUrl: function() {
this.setState({pending: true});
this.props.client.requestCallUrl(this.conversationIdentifier(),
this._onCallUrlReceived);
},
_onCallUrlReceived: function(err, callUrlData) {
if (err) {
if (err.code != 401) {
// 401 errors are already handled in hawkRequest and show an error
// message about the session.
this.props.notifications.errorL10n("unable_retrieve_url");
}
this.setState(this.getInitialState());
} else {
try {
var callUrl = new window.URL(callUrlData.callUrl);
// XXX the current server vers does not implement the callToken field
// but it exists in the API. This workaround should be removed in the future
var token = callUrlData.callToken ||
callUrl.pathname.split('/').pop();
this.setState({pending: false, copied: false,
callUrl: callUrl.href,
callUrlExpiry: callUrlData.expiresAt});
} catch(e) {
console.log(e);
this.props.notifications.errorL10n("unable_retrieve_url");
this.setState(this.getInitialState());
}
}
},
handleEmailButtonClick: function(event) {
this.handleLinkExfiltration(event);
navigator.mozLoop.composeEmail(
__("share_email_subject4", { clientShortname: __("clientShortname2")}),
__("share_email_body4", { callUrl: this.state.callUrl,
clientShortname: __("clientShortname2"),
learnMoreUrl: navigator.mozLoop.getLoopCharPref("learnMoreUrl") }));
},
handleCopyButtonClick: function(event) {
this.handleLinkExfiltration(event);
// XXX the mozLoop object should be passed as a prop, to ease testing and
// using a fake implementation in UI components showcase.
navigator.mozLoop.copyString(this.state.callUrl);
this.setState({copied: true});
},
handleLinkExfiltration: function(event) {
try {
navigator.mozLoop.telemetryAdd("LOOP_CLIENT_CALL_URL_SHARED", true);
} catch (err) {
console.error("Error recording telemetry", err);
}
if (this.state.callUrlExpiry) {
navigator.mozLoop.noteCallUrlExpiry(this.state.callUrlExpiry);
}
},
render: function() {
// XXX setting elem value from a state (in the callUrl input)
// makes it immutable ie read only but that is fine in our case.
// readOnly attr will suppress a warning regarding this issue
// from the react lib.
var cx = React.addons.classSet;
var inputCSSClass = cx({
"pending": this.state.pending,
// Used in functional testing, signals that
// call url was received from loop server
"callUrl": !this.state.pending
});
return (
);
}
});
/**
* Panel view.
*/
var PanelView = React.createClass({
propTypes: {
notifications: React.PropTypes.object.isRequired,
client: React.PropTypes.object.isRequired,
// Mostly used for UI components showcase and unit tests
callUrl: React.PropTypes.string,
userProfile: React.PropTypes.object,
showTabButtons: React.PropTypes.bool,
selectedTab: React.PropTypes.string,
dispatcher: React.PropTypes.instanceOf(loop.Dispatcher).isRequired,
roomListStore:
React.PropTypes.instanceOf(loop.store.RoomListStore).isRequired
},
getInitialState: function() {
return {
userProfile: this.props.userProfile || navigator.mozLoop.userProfile,
};
},
_serviceErrorToShow: function() {
if (!navigator.mozLoop.errors || !Object.keys(navigator.mozLoop.errors).length) {
return null;
}
// Just get the first error for now since more than one should be rare.
var firstErrorKey = Object.keys(navigator.mozLoop.errors)[0];
return {
type: firstErrorKey,
error: navigator.mozLoop.errors[firstErrorKey],
};
},
updateServiceErrors: function() {
var serviceError = this._serviceErrorToShow();
if (serviceError) {
this.props.notifications.set({
id: "service-error",
level: "error",
message: serviceError.error.friendlyMessage,
details: serviceError.error.friendlyDetails,
detailsButtonLabel: serviceError.error.friendlyDetailsButtonLabel,
});
} else {
this.props.notifications.remove(this.props.notifications.get("service-error"));
}
},
_onStatusChanged: function() {
var profile = navigator.mozLoop.userProfile;
var currUid = this.state.userProfile ? this.state.userProfile.uid : null;
var newUid = profile ? profile.uid : null;
if (currUid != newUid) {
// On profile change (login, logout), switch back to the default tab.
this.selectTab("call");
}
this.setState({userProfile: profile});
this.updateServiceErrors();
},
/**
* The rooms feature is hidden by default for now. Once it gets mainstream,
* this method can be safely removed.
*/
_renderRoomsTab: function() {
if (!navigator.mozLoop.getLoopBoolPref("rooms.enabled")) {
return null;
}
return (
);
},
startForm: function(name, contact) {
this.refs[name].initForm(contact);
this.selectTab(name);
},
selectTab: function(name) {
this.refs.tabView.setState({ selectedTab: name });
},
componentWillMount: function() {
this.updateServiceErrors();
},
componentDidMount: function() {
window.addEventListener("LoopStatusChanged", this._onStatusChanged);
},
componentWillUnmount: function() {
window.removeEventListener("LoopStatusChanged", this._onStatusChanged);
},
render: function() {
var NotificationListView = sharedViews.NotificationListView;
var displayName = this.state.userProfile && this.state.userProfile.email ||
__("display_name_guest");
return (
{this._renderRoomsTab()}
);
}
});
/**
* Panel initialisation.
*/
function init() {
// Do the initial L10n setup, we do this before anything
// else to ensure the L10n environment is setup correctly.
mozL10n.initialize(navigator.mozLoop);
var client = new loop.Client();
var notifications = new sharedModels.NotificationCollection();
var dispatcher = new loop.Dispatcher();
var roomListStore = new loop.store.RoomListStore({
mozLoop: navigator.mozLoop,
dispatcher: dispatcher
});
React.renderComponent(, document.querySelector("#main"));
document.body.classList.add(loop.shared.utils.getTargetPlatform());
document.body.setAttribute("dir", mozL10n.getDirection());
// Notify the window that we've finished initalization and initial layout
var evtObject = document.createEvent('Event');
evtObject.initEvent('loopPanelInitialized', true, false);
window.dispatchEvent(evtObject);
}
return {
init: init,
UserIdentity: UserIdentity,
AuthLink: AuthLink,
AvailabilityDropdown: AvailabilityDropdown,
CallUrlResult: CallUrlResult,
PanelView: PanelView,
RoomList: RoomList,
SettingsDropdown: SettingsDropdown,
ToSView: ToSView
};
})(_, document.mozL10n);
document.addEventListener('DOMContentLoaded', loop.panel.init);