Skip to content

Commit

Permalink
Merge pull request #493 from aarranz/feature/duplicate-workspace
Browse files Browse the repository at this point in the history
Feature/duplicate workspace
  • Loading branch information
aarranz committed Nov 25, 2020
2 parents 6ed42ef + d0b3c68 commit 00f77c8
Show file tree
Hide file tree
Showing 10 changed files with 304 additions and 56 deletions.
1 change: 1 addition & 0 deletions setup.cfg
3 changes: 3 additions & 0 deletions src/GruntFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ var WirecloudFiles = [
'wirecloud/commons/static/js/wirecloud/ui/MACSearch.js',
'wirecloud/commons/static/js/wirecloud/ui/WindowMenu.js',
'wirecloud/commons/static/js/wirecloud/ui/AlertWindowMenu.js',
'wirecloud/commons/static/js/wirecloud/ui/FormWindowMenu.js',
//'wirecloud/commons/static/js/wirecloud/ui/InputInterfaceFactory.js',
'wirecloud/commons/static/js/wirecloud/ui/MessageWindowMenu.js',
'wirecloud/catalogue/static/js/wirecloud/WirecloudCatalogue.js',
'wirecloud/catalogue/static/js/wirecloud/ui/ResourcePainter.js',
Expand All @@ -126,6 +128,7 @@ var WirecloudFiles = [
'wirecloud/platform/static/js/wirecloud/Workspace.js',
'wirecloud/platform/static/js/wirecloud/wiring/KeywordSuggestion.js',
'wirecloud/platform/static/js/wirecloud/wiring/Operator.js',
'wirecloud/platform/static/js/wirecloud/ui/NewWorkspaceWindowMenu.js',
'wirecloud/platform/static/js/wirecloud/ui/WidgetViewDraggable.js',
'wirecloud/platform/static/js/wirecloud/ui/WidgetViewResizeHandle.js',
'wirecloud/platform/static/js/wirecloud/ui/WidgetView.js',
Expand Down
198 changes: 198 additions & 0 deletions src/js_tests/wirecloud/ui/NewWorkspaceWindowMenuSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/*
* Copyright (c) 2020 Future Internet Consulting and Development Solutions S.L.
*
* This file is part of Wirecloud Platform.
*
* Wirecloud Platform is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Wirecloud is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Wirecloud Platform. If not, see
* <http://www.gnu.org/licenses/>.
*
*/

/* globals StyledElements, Wirecloud */

(function (ns, se) {

"use strict";

describe("NewWorkspaceWindowMenu", () => {

beforeAll(() => {
Wirecloud.ui.InputInterfaceFactory = new StyledElements.InputInterfaceFactory();
Wirecloud.ui.InputInterfaceFactory.addFieldType("mac", StyledElements.TextInputInterface);

Wirecloud.contextManager = {
"get": jasmine.createSpy("get").and.callFake((name) => {
switch (name) {
case "username":
return "currentuser";
};
})
};
});

beforeEach(() => {
spyOn(Wirecloud.UserInterfaceManager, "monitorTask");
spyOn(Wirecloud, "createWorkspace");
spyOn(Wirecloud, "changeActiveWorkspace");
Wirecloud.ui.MissingDependenciesWindowMenu = jasmine.createSpy("MissingDependenciesWindowMenu");
Wirecloud.ui.MessageWindowMenu = jasmine.createSpy("MessageWindowMenu");
});

afterAll(() => {
delete Wirecloud.ui.InputInterfaceFactory;
delete Wirecloud.contextManager;
delete Wirecloud.ui.MissingDependenciesWindowMenu;
delete Wirecloud.ui.MessageWindowMenu;
});

describe("new Wirecloud.ui.NewWorkspaceWindowMenu(options)", () => {

it("should work without providing any option", () => {
const dialog = new ns.NewWorkspaceWindowMenu();

expect(dialog.form.fields.workspace).toBe(undefined);
expect(dialog.form.fields.mashup).not.toBe(undefined);
expect(dialog.form.fields.title).not.toBe(undefined);
});

it("should allow to pass an initial title by using the title option", () => {
const dialog = new ns.NewWorkspaceWindowMenu({"title": "New workspace"});

expect(dialog.form.fields.workspace).toBe(undefined);
expect(dialog.form.fields.mashup).not.toBe(undefined);
expect(dialog.form.fields.title.initialValue).toBe("New workspace");
});

it("should allow to duplicate workspaces by using the workspace option", () => {
const dialog = new ns.NewWorkspaceWindowMenu({"workspace": "4"});

expect(dialog.form.fields.mashup).toBe(undefined);
expect(dialog.form.fields.workspace.initialValue).toBe("4");
});

});

describe("executeOperation(data)", () => {

it("should work when the title is empty", () => {
const dialog = new ns.NewWorkspaceWindowMenu();
const workspace = {};
Wirecloud.createWorkspace.and.returnValue(
new Wirecloud.Task("Sending request", (resolve) => {resolve(workspace);})
);

dialog.executeOperation({
title: "",
mashup: null
});
expect(Wirecloud.changeActiveWorkspace).toHaveBeenCalledWith(workspace);
});

it("should work when a title is provided", () => {
const dialog = new ns.NewWorkspaceWindowMenu();
const workspace = {};
Wirecloud.createWorkspace.and.returnValue(
new Wirecloud.Task("Sending request", (resolve) => {resolve(workspace);})
);

dialog.executeOperation({
title: "New Workspace",
mashup: null
});
expect(Wirecloud.changeActiveWorkspace).toHaveBeenCalledWith(workspace);
});

it("should manage generic errors", () => {
const dialog = new ns.NewWorkspaceWindowMenu();
Wirecloud.createWorkspace.and.returnValue(
new Wirecloud.Task("Sending request", (resolve, reject) => {reject({});})
);

dialog.executeOperation({
title: "New Workspace",
mashup: null
});
expect(Wirecloud.changeActiveWorkspace).not.toHaveBeenCalled();
});

it("should manage missingDependencies error", () => {
const dialog = new ns.NewWorkspaceWindowMenu();
Wirecloud.createWorkspace.and.returnValue(
new Wirecloud.Task("Sending request", (resolve, reject) => {reject({details: {missingDependencies: []}});})
);

dialog.executeOperation({
title: "My Marketplace",
mashup: "CoNWeT/bae-marketplace/0.1.1"
});

expect(Wirecloud.changeActiveWorkspace).not.toHaveBeenCalled();
expect(Wirecloud.ui.MessageWindowMenu).not.toHaveBeenCalled();
expect(Wirecloud.ui.MissingDependenciesWindowMenu).toHaveBeenCalled();
});

it("should manage missingDependencies error (retry - success)", () => {
const dialog = new ns.NewWorkspaceWindowMenu();
Wirecloud.createWorkspace.and.returnValue(
new Wirecloud.Task("Sending request", (resolve, reject) => {reject({details: {missingDependencies: []}});})
);

dialog.executeOperation({
title: "",
mashup: "CoNWeT/bae-marketplace/0.1.1"
});
expect(Wirecloud.ui.MissingDependenciesWindowMenu).toHaveBeenCalled();

const retry = Wirecloud.ui.MissingDependenciesWindowMenu.calls.argsFor(0)[0];

const workspace = {};
Wirecloud.createWorkspace.and.returnValue(
new Wirecloud.Task("Sending request", (resolve) => {resolve(workspace);})
);

retry();

expect(Wirecloud.ui.MessageWindowMenu).not.toHaveBeenCalled();
expect(Wirecloud.changeActiveWorkspace).toHaveBeenCalledWith(workspace);
});

it("should manage missingDependencies error (retry - error)", () => {
// Error retry
const dialog = new ns.NewWorkspaceWindowMenu();
Wirecloud.createWorkspace.and.returnValue(
new Wirecloud.Task("Sending request", (resolve, reject) => {reject({details: {missingDependencies: []}});})
);

dialog.executeOperation({
title: "",
mashup: "CoNWeT/bae-marketplace/0.1.1"
});
expect(Wirecloud.ui.MissingDependenciesWindowMenu).toHaveBeenCalled();

const retry = Wirecloud.ui.MissingDependenciesWindowMenu.calls.argsFor(0)[0];
Wirecloud.createWorkspace.and.returnValue(
new Wirecloud.Task("Sending request", (resolve, reject) => {reject({});})
);

retry();

expect(Wirecloud.changeActiveWorkspace).not.toHaveBeenCalled();
expect(Wirecloud.ui.MessageWindowMenu).toHaveBeenCalled();
});

});

});

})(Wirecloud.ui, StyledElements);
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
var mapping = StyledElements.Utils.clone(default_mapping);

this.createInterface = function createInterface(fieldId, fieldDesc) {
var Class_ = mapping[fieldDesc.type];
var Class_ = mapping[fieldDesc.type != null ? fieldDesc.type : 'text'];
if (Class_ == null) {
throw new Error(fieldDesc.type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@
var HiddenInputInterface = function HiddenInputInterface(fieldId, options) {
StyledElements.InputInterface.call(this, fieldId, options);

this.inputElement = new StyledElements.StyledHiddenField(options);
this.inputElement = new StyledElements.HiddenField(options);
};
HiddenInputInterface.prototype = new StyledElements.InputInterface();

Expand Down
8 changes: 6 additions & 2 deletions src/wirecloud/platform/static/js/wirecloud/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,12 @@
* @param {Object} options
* - `allow_renaming` (Boolean, default: `true`)
* - `mashup` (String): Mashup reference to use as template.
* - `name` (String): This options is required if the `mashup` and
* `workspace` options are not used and optional in any other case.
* - `name` (String): This options is required if neither the `mashup` nor
* the `workspace` options are used and the `title` option is also not
* used. This parameter is optional in any other case.
* - `title` (String): This options is required if neither the `mashup` nor
* the `workspace` options are used and the `name` option is also not
* used. This parameter is optional in any other case.
* - `workspace` (String): id of the workspace to clone.
*
* @returns {Wirecloud.Task}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2012-2017 CoNWeT Lab., Universidad Politécnica de Madrid
* Copyright (c) 2020 Future Internet Consulting and Development Solutions S.L.
*
* This file is part of Wirecloud Platform.
*
Expand All @@ -26,76 +27,85 @@

"use strict";

var retry = function retry(data) {
const retry = function retry(data) {
Wirecloud.UserInterfaceManager.monitorTask(
Wirecloud.createWorkspace({
title: data.title,
mashup: data.mashup
}).then(
function (workspace) {
Wirecloud.createWorkspace(data).then(
(workspace) => {
Wirecloud.changeActiveWorkspace(workspace);
},
function (msg, details) {
var dialog = new Wirecloud.ui.MessageWindowMenu(msg, Wirecloud.constants.LOGGING.ERROR_MSG);
(msg, details) => {
const dialog = new Wirecloud.ui.MessageWindowMenu(msg, Wirecloud.constants.LOGGING.ERROR_MSG);
dialog.show();
}
)
);
};

var NewWorkspaceWindowMenu = function NewWorkspaceWindowMenu() {
var fields = {
'title': {
label: utils.gettext('Name'),
type: 'text'
},
'mashup': {
label: utils.gettext('Template'),
type: 'mac',
scope: 'mashup',
dialog_title: utils.gettext('Select a mashup template'),
required: false,
parent_dialog: this
const NewWorkspaceWindowMenu = function NewWorkspaceWindowMenu(options) {
if (options == null) {
options = {};
}
const fields = {
"title": {
label: utils.gettext("Name"),
type: "text",
initialValue: options.title
}
};
Wirecloud.ui.FormWindowMenu.call(this, fields, utils.gettext('Create Workspace'), 'wc-new-workspace-modal');
const title = options.workspace == null ? utils.gettext("Create Workspace") : utils.gettext("Copy Workspace");
if (options.workspace == null) {
fields.mashup = {
label: utils.gettext("Template"),
type: "mac",
scope: "mashup",
dialog_title: utils.gettext("Select a mashup template"),
required: false,
parent_dialog: this
};
} else {
fields.workspace = {
type: "hidden",
initialValue: options.workspace
};
}
Wirecloud.ui.FormWindowMenu.call(this, fields, title, "wc-new-workspace-modal");
};
NewWorkspaceWindowMenu.prototype = new Wirecloud.ui.FormWindowMenu();

NewWorkspaceWindowMenu.prototype.executeOperation = function executeOperation(data) {
var task_title;
let task_title;

if (data.name) {
if (data.title) {
if (data.mashup) {
task_title = utils.gettext("Creating a %(owner)s/%(name)s workspace using %(mashup)s as template");
task_title = utils.gettext("Creating a %(owner)s/%(title)s workspace using %(mashup)s as template");
} else {
task_title = utils.gettext("Creating a %(owner)s/%(name)s workspace");
task_title = utils.gettext("Creating a %(owner)s/%(title)s workspace");
}
} else {
task_title = utils.gettext("Creating a new workspace using %(mashup)s as template");
}
task_title = utils.interpolate(task_title, {
owner: Wirecloud.contextManager.get('username'),
name: data.name,
owner: Wirecloud.contextManager.get("username"),
title: data.title,
mashup: data.mashup
});

Wirecloud.UserInterfaceManager.monitorTask(
Wirecloud.createWorkspace({
title: data.title,
mashup: data.mashup
}).then(function (workspace) {
return Wirecloud.changeActiveWorkspace(workspace);
}, function (error) {
var dialog;
if (error.details != null && 'missingDependencies' in error.details) {
// Show missing dependencies
dialog = new Wirecloud.ui.MissingDependenciesWindowMenu(retry.bind(null, data), error.details);
} else {
dialog = new Wirecloud.ui.MessageWindowMenu(error, Wirecloud.constants.LOGGING.ERROR_MSG);
Wirecloud.createWorkspace(data).then(
(workspace) => {
return Wirecloud.changeActiveWorkspace(workspace);
},
(error) => {
let dialog;
if (error.details != null && "missingDependencies" in error.details) {
// Show missing dependencies
dialog = new Wirecloud.ui.MissingDependenciesWindowMenu(retry.bind(null, data), error.details);
} else {
dialog = new Wirecloud.ui.MessageWindowMenu(error, Wirecloud.constants.LOGGING.ERROR_MSG);
}
dialog.show();
}
dialog.show();
}).toTask(task_title)
).toTask(task_title)
);
};

Expand Down
Loading

0 comments on commit 00f77c8

Please sign in to comment.