Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions jstests/tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture">
<div id="applist"></div>
</div>
</body>
</html>
20 changes: 20 additions & 0 deletions jstests/tests/home/test_models.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
define(function (require) {
"use strict";
var models = require("home/models");

var MockApi = function () {
this.available_applications_info = function() {
return [{}, {}];
};
};

QUnit.module("home.models");
QUnit.test("instantiation", function (assert) {
var mock_api = new MockApi();
var model = new models.ApplicationListModel(mock_api);
assert.equal(model.data.length, 0);
model.update().done(function() {
assert.equal(model.data.length, 2);
});
});
});
44 changes: 44 additions & 0 deletions jstests/tests/home/test_views.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
define(function (require) {
"use strict";
var models = require("home/models");
var views = require("home/views");
var $ = require("jquery");

var MockApi = function () {
this.available_applications_info = function() {
return [
{
image : {
ui_name : "foo",
policy : {
allow_home: true
}
}
},
{
image: {
ui_name : "bar",
policy : {
allow_home: true
}
}
}];
};
};

QUnit.module("home.views");
QUnit.test("rendering", function (assert) {
var mock_api = new MockApi();
var model = new models.ApplicationListModel(mock_api);
var view = new views.ApplicationListView(model);
model.update()
.done(function() { view.render(); } )
.done(function() {
var applist = $("#applist");
assert.equal(applist.children().length, 2);
assert.equal($("#applist > div:nth-child(1) > div > h4").text(), "foo");
assert.equal($("#applist > div:nth-child(2) > div > h4").text(), "bar");
});

});
});
2 changes: 1 addition & 1 deletion jstests/tests/test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ define(function (require) {

QUnit.module("Utils");
QUnit.test("url_path_join", function (assert) {
assert.equal(utils.url_path_join("foo", "bar", "baz"), "foo/bar/baz")
assert.equal(utils.url_path_join("foo", "bar", "baz"), "foo/bar/baz");
});
});
10 changes: 8 additions & 2 deletions jstests/testsuite.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
});

require([
"tests/test_remoteappapi.js",
"tests/test_utils.js"
"tests/home/test_models.js",
"tests/home/test_views.js",
"tests/test_remoteappapi.js",
"tests/test_utils.js"
], function() {
window.apidata = {
base_url: "/",
prefix: "/"
};
QUnit.load();
QUnit.start();
});
Expand Down
183 changes: 0 additions & 183 deletions remoteappmanager/static/js/home.js

This file was deleted.

72 changes: 72 additions & 0 deletions remoteappmanager/static/js/home/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*globals: require, console*/
require(
["jquery", "utils", "remoteappapi", "home/models", "home/views"],
function($, utils, RemoteAppAPI, models, views) {
"use strict";

var base_url = window.apidata.base_url;
var appapi = new RemoteAppAPI(base_url);

// This model keeps the retrieved content from the REST query locally.
// It is only synchronized at initial load.
var model = new models.ApplicationListModel(appapi);
var view = new views.ApplicationListView(model);

var report_error = function (jqXHR, status, error) {
// Writes an error message resulting from an incorrect
// ajax operation. Parameters are from the resulting ajax.
var msg = utils.log_ajax_error(jqXHR, status, error);
$(".spawn-error-msg").text(msg).show();
};

view.view_button_clicked = function (index) {
var app_info = model.data[index];

window.location = utils.url_path_join(
base_url,
"containers",
app_info.container.url_id);
};

view.stop_button_clicked = function (index) {
var app_info = model.data[index];

var url_id = app_info.container.url_id;
return appapi.stop_application(url_id, {
success: function () {
view.reset_buttons_to_start(index);
app_info.container = null;
},
error: function (jqXHR, status, error) {
report_error(jqXHR, status, error);
}});
};

view.start_button_clicked = function (index) {
// The container is not running. This is a start button.
var mapping_id = model.data[index].mapping_id;
return appapi.start_application(mapping_id, {
error: function(jqXHR, status, error) {
report_error(jqXHR, status, error);
},
statusCode: {
201: function (data, textStatus, request) {
var location = request.getResponseHeader('Location');
var url = utils.parse_url(location);
var arr = url.pathname.replace(/\/$/, "").split('/');
var url_id = arr[arr.length-1];

window.location = utils.url_path_join(
base_url,
"containers",
url_id
);
}
}
});
};

$.when(model.update()).done(function () { view.render(); });

}
);
32 changes: 32 additions & 0 deletions remoteappmanager/static/js/home/models.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
define(['jquery'], function ($) {
"use strict";

var ApplicationListModel = function(remote_app_api) {
// (constructor) Model for the application list.
// Parameters
// remote_app_api : RemoteAppAPI
// The remote application API stub object
this._appapi = remote_app_api;

// Contains the data retrieved from the remote API
this.data = [];
};

ApplicationListModel.prototype.update = function() {
// Requests an update of the object internal data.
// This method returns a jQuery Promise object.
// When resolved, this.data will contain a list of the retrieved
// data. Note that, in error conditions, this routine resolves
// successfully in any case, and the data is set to empty list
var self = this;
return $.when(
self._appapi.available_applications_info()
).done(function (app_data) {
self.data = app_data;
});
};

return {
ApplicationListModel: ApplicationListModel
};
});
Loading