forked from mozilla/gecko-dev
/
conversation.js
132 lines (113 loc) · 3.46 KB
/
conversation.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* 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/. */
/*global loop*/
Components.utils.import("resource://gre/modules/Services.jsm");
var loop = loop || {};
loop.conversation = (function(TB) {
"use strict";
var baseServerUrl = Services.prefs.getCharPref("loop.server");
/**
* App router.
* @type {loop.webapp.Router}
*/
var router;
/**
* Current conversation model instance.
* @type {loop.webapp.ConversationModel}
*/
var conversation;
var ConversationRouter = loop.shared.router.BaseRouter.extend({
_conversation: undefined,
activeView: undefined,
routes: {
"start/:version": "start",
"call/ongoing": "conversation",
"call/ended": "ended"
},
/**
* Loads and render current active view.
*
* @param {loop.shared.BaseView} view View.
*/
loadView : function(view) {
if (this.activeView) {
this.activeView.hide();
}
this.activeView = view.render().show();
},
initialize: function(options) {
options = options || {};
if (!options.conversation) {
throw new Error("missing required conversation");
}
this._conversation = options.conversation;
this.listenTo(this._conversation, "session:ready", this._onSessionReady);
this.listenTo(this._conversation, "session:ended", this._onSessionEnded);
},
/**
* Navigates to conversation when the call session is ready.
*/
_onSessionReady: function() {
this.navigate("call/ongoing", {trigger: true});
},
/**
* Navigates to ended state when the call has ended
*/
_onSessionEnded: function() {
this.navigate("call/ended", {trigger: true});
},
/**
* start is the initial route that does any necessary prompting and set
* up for the call.
*
* @param {String} loopVersion The version from the push notification, set
* by the router from the URL.
*/
start: function(loopVersion) {
// XXX For now, we just kick the conversation straight away, bug 990678
// will implement the follow-ups.
this._conversation.set({loopVersion: loopVersion});
this._conversation.initiate({
baseServerUrl: baseServerUrl,
outgoing: false
});
},
/**
* conversation is the route when the conversation is active. The start
* route should be navigated to first.
*/
conversation: function() {
if (!this._conversation.isSessionReady()) {
// XXX: notify user that something has gone wrong.
console.error("Error: navigated to conversation route without " +
"the start route to initialise the call first");
return;
}
this.loadView(
new loop.shared.views.ConversationView({
sdk: TB,
model: this._conversation
}));
},
/**
* ended does any necessary work to end the call.
*/
ended: function() {
// XXX Later we implement the end-of call here (bug 974873)
window.close();
}
});
/**
* Panel initialisation.
*/
function init() {
conversation = new loop.shared.models.ConversationModel();
router = new ConversationRouter({conversation: conversation});
Backbone.history.start();
}
return {
ConversationRouter: ConversationRouter,
init: init
};
})(window.TB);