mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-11-11 05:39:41 +02:00
--HG-- rename : static/shared/README.md => browser/components/loop/content/shared/README.md rename : static/shared/css/common.css => browser/components/loop/content/shared/css/common.css rename : static/shared/css/conversation.css => browser/components/loop/content/shared/css/conversation.css rename : static/shared/css/panel.css => browser/components/loop/content/shared/css/panel.css rename : static/shared/css/readme.html => browser/components/loop/content/shared/css/readme.html rename : static/shared/img/icon_32.png => browser/components/loop/content/shared/img/icon_32.png rename : static/shared/img/icon_64.png => browser/components/loop/content/shared/img/icon_64.png rename : static/shared/js/models.js => browser/components/loop/content/shared/js/models.js rename : static/shared/js/views.js => browser/components/loop/content/shared/js/views.js rename : static/shared/libs/backbone-1.1.2.js => browser/components/loop/content/shared/libs/backbone-1.1.2.js rename : static/shared/libs/jquery-2.1.0.js => browser/components/loop/content/shared/libs/jquery-2.1.0.js rename : static/shared/libs/lodash-2.4.1.js => browser/components/loop/content/shared/libs/lodash-2.4.1.js rename : static/shared/libs/sdk.js => browser/components/loop/content/shared/libs/sdk.js rename : static/shared/libs/webl10n-20130617.js => browser/components/loop/content/shared/libs/webl10n-20130617.js rename : static/css/webapp.css => browser/components/loop/standalone/content/css/webapp.css rename : static/index.html => browser/components/loop/standalone/content/index.html rename : static/js/webapp.js => browser/components/loop/standalone/content/js/webapp.js rename : static/l10n/data.ini => browser/components/loop/standalone/content/l10n/data.ini rename : test/webapp_test.js => browser/components/loop/test/standalone/webapp_test.js extra : transplant_source : %DA%D9%3A%E9%C6%E0d%13%84%C1%BEps%C8b%F09o%D7m
149 lines
3.9 KiB
JavaScript
149 lines
3.9 KiB
JavaScript
/* 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:true */
|
|
|
|
var loop = loop || {};
|
|
loop.shared = loop.shared || {};
|
|
loop.shared.views = (function(TB) {
|
|
"use strict";
|
|
|
|
/**
|
|
* Base Backbone view.
|
|
*/
|
|
var BaseView = Backbone.View.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;
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Conversation view.
|
|
*/
|
|
var ConversationView = BaseView.extend({
|
|
el: "#conversation",
|
|
|
|
videoStyles: { width: "100%", height: "100%" },
|
|
|
|
/**
|
|
* Establishes webrtc communication using TB sdk.
|
|
*/
|
|
initialize: function(options) {
|
|
options = options || {};
|
|
if (!options.sdk) {
|
|
throw new Error("missing required sdk");
|
|
}
|
|
this.sdk = options.sdk;
|
|
// XXX: this feels like to be moved to the ConversationModel, but as it's
|
|
// tighly coupled with the DOM (element ids to receive streams), we'd need
|
|
// an abstraction we probably don't want yet.
|
|
this.session = this.sdk.initSession(this.model.get("sessionId"));
|
|
this.publisher = this.sdk.initPublisher(this.model.get("apiKey"),
|
|
"outgoing", this.videoStyles);
|
|
|
|
this.session.connect(this.model.get("apiKey"),
|
|
this.model.get("sessionToken"));
|
|
|
|
this.listenTo(this.session, "sessionConnected", this._sessionConnected);
|
|
this.listenTo(this.session, "streamCreated", this._streamCreated);
|
|
this.listenTo(this.session, "connectionDestroyed", this._sessionEnded);
|
|
},
|
|
|
|
_sessionConnected: function(event) {
|
|
this.session.publish(this.publisher);
|
|
this._subscribeToStreams(event.streams);
|
|
},
|
|
|
|
_streamCreated: function(event) {
|
|
this._subscribeToStreams(event.streams);
|
|
},
|
|
|
|
_sessionEnded: function(event) {
|
|
// XXX: better end user notification
|
|
alert("Your session has ended. Reason: " + event.reason);
|
|
this.model.trigger("session:ended");
|
|
},
|
|
|
|
_subscribeToStreams: function(streams) {
|
|
streams.forEach(function(stream) {
|
|
if (stream.connection.connectionId !==
|
|
this.session.connection.connectionId) {
|
|
this.session.subscribe(stream, "incoming", this.videoStyles);
|
|
}
|
|
}.bind(this));
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Notification view.
|
|
*/
|
|
var NotificationView = Backbone.View.extend({
|
|
template: _.template([
|
|
'<div class="alert alert-<%- level %>">',
|
|
' <button class="close"></button>',
|
|
' <p class="message"><%- message %></p>',
|
|
'</div>'
|
|
].join("")),
|
|
|
|
events: {
|
|
"click .close": "dismiss"
|
|
},
|
|
|
|
dismiss: function(event) {
|
|
event.preventDefault();
|
|
this.$el.addClass("fade-out");
|
|
setTimeout(function() {
|
|
this.collection.remove(this.model);
|
|
this.remove();
|
|
}.bind(this), 500); // XXX make timeout value configurable
|
|
},
|
|
|
|
render: function() {
|
|
this.$el.html(this.template(this.model.toJSON()));
|
|
return this;
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Notification list view.
|
|
*/
|
|
var NotificationListView = Backbone.View.extend({
|
|
initialize: function() {
|
|
this.listenTo(this.collection, "reset add remove", this.render);
|
|
},
|
|
|
|
render: function() {
|
|
this.$el.html(this.collection.map(function(notification) {
|
|
return new NotificationView({
|
|
model: notification,
|
|
collection: this.collection
|
|
}).render().$el;
|
|
}.bind(this)));
|
|
return this;
|
|
}
|
|
});
|
|
|
|
return {
|
|
BaseView: BaseView,
|
|
ConversationView: ConversationView,
|
|
NotificationListView: NotificationListView,
|
|
NotificationView: NotificationView
|
|
};
|
|
})(window.TB);
|