Skip to content

Commit

Permalink
Merge pull request #541 from oxixes/workspace-params
Browse files Browse the repository at this point in the history
Adds parameters to workspaces
  • Loading branch information
aarranz authored Jun 27, 2024
2 parents 590a739 + aa132cf commit 2a25766
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 17 deletions.
9 changes: 6 additions & 3 deletions src/js_tests/wirecloud/coreSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
name: "home",
title: "Home",
contextManager: {
addCallback: jasmine.createSpy()
addCallback: jasmine.createSpy(),
modify: jasmine.createSpy()
}
};
Wirecloud.workspaceInstances = {1: initworkspace};
Expand Down Expand Up @@ -170,7 +171,8 @@
workspace_name: "home",
workspace_title: "Home",
view: "workspace",
tab: "MyTab"
tab: "MyTab",
params: {}
});
expect(Wirecloud.activeWorkspace).toBe(initworkspace);
expect(workspace).toBe(initworkspace);
Expand All @@ -195,7 +197,8 @@
workspace_owner: "wirecloud",
workspace_name: "home",
workspace_title: "Home",
view: "workspace"
view: "workspace",
params: {}
});
expect(Wirecloud.activeWorkspace).toBe(initworkspace);
expect(workspace).toBe(initworkspace);
Expand Down
10 changes: 8 additions & 2 deletions src/js_tests/wirecloud/ui/WiringEditorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1687,7 +1687,10 @@
spyOn(Wirecloud.HistoryManager, "getCurrentState").and.returnValue({
"workspace_owner": "workspace_owner",
"workspace_name": "workspace_name",
"view": "workspace"
"view": "workspace",
"params": {
"testParam": "testValue"
}
});
const editor = new ns.WiringEditor(1);

Expand All @@ -1696,7 +1699,10 @@
expect(state).toEqual({
"workspace_owner": "workspace_owner",
"workspace_name": "workspace_name",
"view": "wiring"
"view": "wiring",
"params": {
"testParam": "testValue"
}
});
expect(Wirecloud.HistoryManager.getCurrentState).toHaveBeenCalledWith();
});
Expand Down
7 changes: 5 additions & 2 deletions src/js_tests/wirecloud/ui/WorkspaceViewSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1015,10 +1015,13 @@

view.onHistoryChange({
workspace_owner: "user",
workspace_name: "dashboard"
workspace_name: "dashboard",
params: {
test: "test"
}
});

expect(Wirecloud.changeActiveWorkspace).toHaveBeenCalledWith(workspace, {initialtab: undefined, history: 'ignore'});
expect(Wirecloud.changeActiveWorkspace).toHaveBeenCalledWith(workspace, {initialtab: undefined, history: 'ignore', params: {test: "test"}});
});

it("should handle tab changes", () => {
Expand Down
6 changes: 5 additions & 1 deletion src/wirecloud/platform/core/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,11 @@ def get_workspace_context_definitions(self):
'longdescription': {
'label': _('Long description'),
'description': _("Detailed workspace's description. This description can contain formatting."),
}
},
"params": {
"description": _("Dictionary with the parameters of the workspace"),
"label": _("Params"),
},
}

def get_workspace_preferences(self):
Expand Down
11 changes: 10 additions & 1 deletion src/wirecloud/platform/static/js/wirecloud/HistoryManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
data.title = document.title;
}
for (const key in data) {
if (typeof data[key] === 'object') {
continue;
}
data[key] = "" + data[key];
}
return data;
Expand All @@ -66,11 +69,17 @@
url.search = window.location.search;

for (key in data) {
if (['workspace_name', 'workspace_owner', 'workspace_title', 'tab_id', 'title'].indexOf(key) !== -1) {
if (['workspace_name', 'workspace_owner', 'workspace_title', 'tab_id', 'title', 'params'].indexOf(key) !== -1) {
continue;
}
hash += '&' + encodeURIComponent(key) + '=' + encodeURIComponent(data[key]);
}
if ("params" in data) {
for (key in data.params) {
hash += '&' + encodeURIComponent(key) + '=' + encodeURIComponent(data.params[key]);
}
}

url.hash = '#' + hash.substr(1);

return url;
Expand Down
38 changes: 36 additions & 2 deletions src/wirecloud/platform/static/js/wirecloud/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,30 @@
});
};

const get_hash_parameters = function get_hash_parameters() {

const hash = window.location.hash;
const hash_parameters = {};

if (hash.length > 1) {
hash.substring(1).split('&').forEach(function (param) {
const parts = param.split('=');
hash_parameters[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
});
}

if ('view' in hash_parameters) {
delete hash_parameters.view;
}

if ('tab' in hash_parameters) {
delete hash_parameters.tab;
}

return hash_parameters;

};

const switch_active_workspace = function switch_active_workspace(options, workspace) {

return new Wirecloud.Task(gettext("Switching active workspace"), (resolve, reject) => {
Expand All @@ -58,9 +82,14 @@
workspace_owner: workspace.owner,
workspace_name: workspace.name,
workspace_title: workspace.title,
view: "workspace"
view: "workspace",
params: {}
};

if ('params' in options) {
state.params = options.params;
}

if (options.initialtab != null) {
state.tab = options.initialtab;
}
Expand All @@ -73,6 +102,10 @@
Wirecloud.HistoryManager.replaceState(state);
}

workspace.contextManager.modify({
params: state.params
});

if (this.activeWorkspace) {
this.activeWorkspace.unload();
this.activeWorkspace = null;
Expand Down Expand Up @@ -316,7 +349,8 @@
name: state.workspace_name
}, {
initialtab: state.tab,
history: "replace"
history: "replace",
params: get_hash_parameters()
}
);
}, (error) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@
const data = {
workspace_owner: currentState.workspace_owner,
workspace_name: currentState.workspace_name,
view: 'marketplace'
view: 'marketplace',
params: currentState.params
};

if (this.loading === false && this.error === false && this.alternatives.getCurrentAlternative() !== this.emptyAlternative) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@
workspace_owner: currentState.workspace_owner,
workspace_name: currentState.workspace_name,
view: 'myresources',
subview: 'search'
subview: 'search',
params: currentState.params
};

const subview = this.alternatives.getCurrentAlternative();
Expand Down
4 changes: 2 additions & 2 deletions src/wirecloud/platform/static/js/wirecloud/ui/WiringEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -760,11 +760,11 @@ Wirecloud.ui = Wirecloud.ui || {};

buildStateData() {
const currentState = Wirecloud.HistoryManager.getCurrentState();

return {
workspace_owner: currentState.workspace_owner,
workspace_name: currentState.workspace_name,
view: this.view_name
view: this.view_name,
params: currentState.params
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,8 @@
workspace_owner: currentState.workspace_owner,
workspace_name: currentState.workspace_name,
workspace_title: currentState.workspace_title,
view: 'workspace'
view: 'workspace',
params: currentState.params
};
}

Expand Down Expand Up @@ -540,7 +541,7 @@
this.layout.slideOut().content.clear().appendChild(alert_msg);
Wirecloud.dispatchEvent('viewcontextchanged');
} else if (Wirecloud.activeWorkspace == null || (nextWorkspace.id !== Wirecloud.activeWorkspace.id)) {
Wirecloud.changeActiveWorkspace(nextWorkspace, {initialtab: newState.tab, history: 'ignore'});
Wirecloud.changeActiveWorkspace(nextWorkspace, {initialtab: newState.tab, history: 'ignore', params: newState.params});
} else if (newState.tab != null) {
target_tab = this.findTab(newState.tab_id);
this.notebook.goToTab(target_tab);
Expand Down

0 comments on commit 2a25766

Please sign in to comment.