mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-11-12 06:08:24 +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
120 lines
3.4 KiB
JavaScript
120 lines
3.4 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.models = (function() {
|
|
"use strict";
|
|
|
|
/**
|
|
* Conversation model.
|
|
*/
|
|
var ConversationModel = Backbone.Model.extend({
|
|
defaults: {
|
|
callerId: undefined, // Loop caller id
|
|
loopToken: undefined, // Loop conversation token
|
|
sessionId: undefined, // TB session id
|
|
sessionToken: undefined, // TB session token
|
|
apiKey: undefined // TB api key
|
|
},
|
|
|
|
/**
|
|
* Initiates a conversation, requesting call session information to the Loop
|
|
* server and updates appropriately the current model attributes with the
|
|
* data.
|
|
*
|
|
* Triggered events:
|
|
*
|
|
* - `session:ready` when the session information have been successfully
|
|
* retrieved from the server;
|
|
* - `session:error` when the request failed.
|
|
*
|
|
* @param {String} baseServerUrl The server URL
|
|
* @throws {Error} If no baseServerUrl is given
|
|
* @throws {Error} If no conversation token is set
|
|
*/
|
|
initiate: function(baseServerUrl) {
|
|
if (!baseServerUrl) {
|
|
throw new Error("baseServerUrl arg must be passed to initiate()");
|
|
}
|
|
|
|
if (!this.get("loopToken")) {
|
|
throw new Error("missing required attribute loopToken");
|
|
}
|
|
|
|
// Check if the session is already set
|
|
if (this.isSessionReady()) {
|
|
return this.trigger("session:ready", this);
|
|
}
|
|
|
|
var request = $.ajax({
|
|
url: baseServerUrl + "/calls/" + this.get("loopToken"),
|
|
method: "POST",
|
|
contentType: "application/json",
|
|
data: JSON.stringify({}),
|
|
dataType: "json"
|
|
});
|
|
|
|
request.done(this.setReady.bind(this));
|
|
|
|
request.fail(function(xhr, _, statusText) {
|
|
var serverError = xhr.status + " " + statusText;
|
|
if (typeof xhr.responseJSON === "object" && xhr.responseJSON.error) {
|
|
serverError += "; " + xhr.responseJSON.error;
|
|
}
|
|
this.trigger("session:error", new Error(
|
|
"Retrieval of session information failed: HTTP " + serverError));
|
|
}.bind(this));
|
|
},
|
|
|
|
/**
|
|
* Checks that the session is ready.
|
|
*
|
|
* @return {Boolean}
|
|
*/
|
|
isSessionReady: function() {
|
|
return !!this.get("sessionId");
|
|
},
|
|
|
|
/**
|
|
* Sets session information and triggers the `session:ready` event.
|
|
*
|
|
* @param {Object} sessionData Conversation session information.
|
|
*/
|
|
setReady: function(sessionData) {
|
|
// Explicit property assignment to prevent later "surprises"
|
|
this.set({
|
|
sessionId: sessionData.sessionId,
|
|
sessionToken: sessionData.sessionToken,
|
|
apiKey: sessionData.apiKey
|
|
}).trigger("session:ready", this);
|
|
return this;
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Notification model.
|
|
*/
|
|
var NotificationModel = Backbone.Model.extend({
|
|
defaults: {
|
|
level: "info",
|
|
message: ""
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Notification collection
|
|
*/
|
|
var NotificationCollection = Backbone.Collection.extend({
|
|
model: NotificationModel
|
|
});
|
|
|
|
return {
|
|
ConversationModel: ConversationModel,
|
|
NotificationCollection: NotificationCollection,
|
|
NotificationModel: NotificationModel
|
|
};
|
|
})();
|