2017-04-25 14:43:59 +02:00
|
|
|
/**
|
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
RED.userSettings = (function() {
|
|
|
|
|
2017-04-28 21:49:01 +02:00
|
|
|
var trayWidth = 700;
|
2017-04-25 14:43:59 +02:00
|
|
|
var settingsVisible = false;
|
|
|
|
|
2017-04-30 00:10:55 +02:00
|
|
|
var panes = [];
|
|
|
|
|
|
|
|
function addPane(options) {
|
|
|
|
panes.push(options);
|
|
|
|
}
|
|
|
|
|
2017-04-28 21:49:01 +02:00
|
|
|
function show(initialTab) {
|
2017-04-25 14:43:59 +02:00
|
|
|
if (settingsVisible) {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-12 22:00:11 +01:00
|
|
|
if (!RED.user.hasPermission("settings.write")) {
|
|
|
|
RED.notify(RED._("user.errors.settings"),"error");
|
|
|
|
return;
|
|
|
|
}
|
2017-04-25 14:43:59 +02:00
|
|
|
settingsVisible = true;
|
2017-04-28 21:49:01 +02:00
|
|
|
var tabContainer;
|
2017-04-25 14:43:59 +02:00
|
|
|
|
|
|
|
var trayOptions = {
|
2017-08-07 11:00:28 +02:00
|
|
|
title: RED._("menu.label.userSettings"),
|
2017-04-25 14:43:59 +02:00
|
|
|
buttons: [
|
|
|
|
{
|
|
|
|
id: "node-dialog-ok",
|
|
|
|
text: RED._("common.label.close"),
|
|
|
|
class: "primary",
|
|
|
|
click: function() {
|
|
|
|
RED.tray.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
resize: function(dimensions) {
|
|
|
|
trayWidth = dimensions.width;
|
|
|
|
},
|
|
|
|
open: function(tray) {
|
|
|
|
var trayBody = tray.find('.editor-tray-body');
|
2017-04-30 00:10:55 +02:00
|
|
|
var settingsContent = $('<div></div>').appendTo(trayBody);
|
|
|
|
var tabContainer = $('<div></div>',{id:"user-settings-tabs-container"}).appendTo(settingsContent);
|
2017-04-25 14:43:59 +02:00
|
|
|
|
2017-04-28 21:49:01 +02:00
|
|
|
$('<ul></ul>',{id:"user-settings-tabs"}).appendTo(tabContainer);
|
2017-04-30 00:10:55 +02:00
|
|
|
var settingsTabs = RED.tabs.create({
|
2017-04-25 14:43:59 +02:00
|
|
|
id: "user-settings-tabs",
|
2017-04-28 21:49:01 +02:00
|
|
|
vertical: true,
|
2017-04-25 14:43:59 +02:00
|
|
|
onchange: function(tab) {
|
2017-04-30 00:10:55 +02:00
|
|
|
setTimeout(function() {
|
|
|
|
$("#user-settings-tabs-content").children().hide();
|
|
|
|
$("#" + tab.id).show();
|
2017-05-03 12:37:42 +02:00
|
|
|
if (tab.pane.focus) {
|
|
|
|
tab.pane.focus();
|
|
|
|
}
|
2017-04-30 00:10:55 +02:00
|
|
|
},50);
|
2017-04-25 14:43:59 +02:00
|
|
|
}
|
|
|
|
});
|
2017-04-30 00:10:55 +02:00
|
|
|
var tabContents = $('<div></div>',{id:"user-settings-tabs-content"}).appendTo(settingsContent);
|
|
|
|
|
|
|
|
panes.forEach(function(pane) {
|
|
|
|
settingsTabs.addTab({
|
|
|
|
id: "user-settings-tab-"+pane.id,
|
2017-05-03 12:37:42 +02:00
|
|
|
label: pane.title,
|
|
|
|
pane: pane
|
2017-04-30 00:10:55 +02:00
|
|
|
});
|
|
|
|
pane.get().hide().appendTo(tabContents);
|
2017-04-25 14:43:59 +02:00
|
|
|
});
|
2017-04-30 00:10:55 +02:00
|
|
|
settingsContent.i18n();
|
|
|
|
settingsTabs.activateTab("user-settings-tab-"+(initialTab||'view'))
|
2017-05-03 12:37:42 +02:00
|
|
|
$("#sidebar-shade").show();
|
2017-04-25 14:43:59 +02:00
|
|
|
},
|
|
|
|
close: function() {
|
|
|
|
settingsVisible = false;
|
2017-04-30 00:10:55 +02:00
|
|
|
panes.forEach(function(pane) {
|
|
|
|
if (pane.close) {
|
|
|
|
pane.close();
|
|
|
|
}
|
|
|
|
});
|
2017-05-03 12:37:42 +02:00
|
|
|
$("#sidebar-shade").hide();
|
|
|
|
|
2017-04-25 14:43:59 +02:00
|
|
|
},
|
|
|
|
show: function() {}
|
|
|
|
}
|
|
|
|
if (trayWidth !== null) {
|
|
|
|
trayOptions.width = trayWidth;
|
|
|
|
}
|
|
|
|
RED.tray.show(trayOptions);
|
|
|
|
}
|
|
|
|
|
2017-04-28 21:49:01 +02:00
|
|
|
var viewSettings = [
|
|
|
|
{
|
2017-08-07 11:00:28 +02:00
|
|
|
title: "menu.label.view.grid",
|
2017-04-28 21:49:01 +02:00
|
|
|
options: [
|
2017-06-28 23:15:30 +02:00
|
|
|
{setting:"view-show-grid",oldSetting:"menu-menu-item-view-show-grid",label:"menu.label.view.showGrid",toggle:true,onchange:"core:toggle-show-grid"},
|
|
|
|
{setting:"view-snap-grid",oldSetting:"menu-menu-item-view-snap-grid",label:"menu.label.view.snapGrid",toggle:true,onchange:"core:toggle-snap-grid"},
|
2017-04-28 21:49:01 +02:00
|
|
|
{setting:"view-grid-size",label:"menu.label.view.gridSize",type:"number",default: 20, onchange:RED.view.gridSize}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
2017-08-07 11:00:28 +02:00
|
|
|
title: "menu.label.nodes",
|
2017-04-28 21:49:01 +02:00
|
|
|
options: [
|
2017-06-28 23:15:30 +02:00
|
|
|
{setting:"view-node-status",oldSetting:"menu-menu-item-status",label:"menu.label.displayStatus",default: true, toggle:true,onchange:"core:toggle-status"}
|
2017-04-28 21:49:01 +02:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
2017-08-07 11:00:28 +02:00
|
|
|
title: "menu.label.other",
|
2017-04-28 21:49:01 +02:00
|
|
|
options: [
|
2018-12-01 14:54:14 +01:00
|
|
|
{setting:"view-show-tips",oldSettings:"menu-menu-item-show-tips",label:"menu.label.showTips",toggle:true,default:true,onchange:"core:toggle-show-tips"},
|
2018-12-03 12:30:29 +01:00
|
|
|
{setting:"view-flow-vertical",label:"menu.label.view.flowVertical",toggle:true,default:true,onchange:RED.view.vertical}
|
2017-04-28 21:49:01 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
var allSettings = {};
|
|
|
|
|
|
|
|
function createViewPane() {
|
|
|
|
|
|
|
|
var pane = $('<div id="user-settings-tab-view" class="node-help"></div>');
|
|
|
|
|
2017-12-06 00:50:32 +01:00
|
|
|
var currentEditorSettings = RED.settings.get('editor') || {};
|
|
|
|
currentEditorSettings.view = currentEditorSettings.view || {};
|
|
|
|
|
2017-04-28 21:49:01 +02:00
|
|
|
viewSettings.forEach(function(section) {
|
2017-08-07 11:00:28 +02:00
|
|
|
$('<h3></h3>').text(RED._(section.title)).appendTo(pane);
|
2017-04-28 21:49:01 +02:00
|
|
|
section.options.forEach(function(opt) {
|
2017-12-06 00:50:32 +01:00
|
|
|
var initialState = currentEditorSettings.view[opt.setting];
|
2017-04-28 21:49:01 +02:00
|
|
|
var row = $('<div class="user-settings-row"></div>').appendTo(pane);
|
|
|
|
var input;
|
|
|
|
if (opt.toggle) {
|
|
|
|
input = $('<label for="user-settings-'+opt.setting+'"><input id="user-settings-'+opt.setting+'" type="checkbox"> '+RED._(opt.label)+'</label>').appendTo(row).find("input");
|
|
|
|
input.prop('checked',initialState);
|
|
|
|
} else {
|
|
|
|
$('<label for="user-settings-'+opt.setting+'">'+RED._(opt.label)+'</label>').appendTo(row);
|
|
|
|
$('<input id="user-settings-'+opt.setting+'" type="'+(opt.type||"text")+'">').appendTo(row).val(initialState);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
return pane;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setSelected(id, value) {
|
|
|
|
var opt = allSettings[id];
|
2017-12-06 00:50:32 +01:00
|
|
|
var currentEditorSettings = RED.settings.get('editor') || {};
|
|
|
|
currentEditorSettings.view = currentEditorSettings.view || {};
|
|
|
|
currentEditorSettings.view[opt.setting] = value;
|
|
|
|
RED.settings.set('editor', currentEditorSettings);
|
2017-04-28 21:49:01 +02:00
|
|
|
var callback = opt.onchange;
|
|
|
|
if (typeof callback === 'string') {
|
|
|
|
callback = RED.actions.get(callback);
|
|
|
|
}
|
|
|
|
if (callback) {
|
|
|
|
callback.call(opt,value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function toggle(id) {
|
|
|
|
var opt = allSettings[id];
|
2017-12-06 00:50:32 +01:00
|
|
|
var currentEditorSettings = RED.settings.get('editor') || {};
|
|
|
|
currentEditorSettings.view = currentEditorSettings.view || {};
|
|
|
|
setSelected(id,!currentEditorSettings.view[opt.setting]);
|
2017-04-28 21:49:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-25 14:43:59 +02:00
|
|
|
function init() {
|
|
|
|
RED.actions.add("core:show-user-settings",show);
|
2017-04-28 21:49:01 +02:00
|
|
|
RED.actions.add("core:show-help", function() { show('keyboard')});
|
|
|
|
|
2017-04-30 00:10:55 +02:00
|
|
|
addPane({
|
|
|
|
id:'view',
|
2017-08-07 11:00:28 +02:00
|
|
|
title: RED._("menu.label.view.view"),
|
2017-04-30 00:10:55 +02:00
|
|
|
get: createViewPane,
|
|
|
|
close: function() {
|
|
|
|
viewSettings.forEach(function(section) {
|
|
|
|
section.options.forEach(function(opt) {
|
|
|
|
var input = $("#user-settings-"+opt.setting);
|
|
|
|
if (opt.toggle) {
|
|
|
|
setSelected(opt.setting,input.prop('checked'));
|
|
|
|
} else {
|
|
|
|
setSelected(opt.setting,input.val());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-12-06 00:50:32 +01:00
|
|
|
var currentEditorSettings = RED.settings.get('editor') || {};
|
|
|
|
currentEditorSettings.view = currentEditorSettings.view || {};
|
|
|
|
var editorSettingsChanged = false;
|
2017-04-28 21:49:01 +02:00
|
|
|
viewSettings.forEach(function(section) {
|
|
|
|
section.options.forEach(function(opt) {
|
2017-06-28 23:15:30 +02:00
|
|
|
if (opt.oldSetting) {
|
|
|
|
var oldValue = RED.settings.get(opt.oldSetting);
|
|
|
|
if (oldValue !== undefined && oldValue !== null) {
|
2017-12-06 00:50:32 +01:00
|
|
|
currentEditorSettings.view[opt.setting] = oldValue;
|
|
|
|
editorSettingsChanged = true;
|
2017-06-28 23:15:30 +02:00
|
|
|
RED.settings.remove(opt.oldSetting);
|
|
|
|
}
|
|
|
|
}
|
2017-04-28 21:49:01 +02:00
|
|
|
allSettings[opt.setting] = opt;
|
|
|
|
if (opt.onchange) {
|
2017-12-06 00:50:32 +01:00
|
|
|
var value = currentEditorSettings.view[opt.setting];
|
2017-12-05 11:20:13 +01:00
|
|
|
if ((value === null || value === undefined) && opt.hasOwnProperty('default')) {
|
2017-04-28 21:49:01 +02:00
|
|
|
value = opt.default;
|
2017-12-06 00:50:32 +01:00
|
|
|
currentEditorSettings.view[opt.setting] = value;
|
|
|
|
editorSettingsChanged = true;
|
2017-04-28 21:49:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var callback = opt.onchange;
|
|
|
|
if (typeof callback === 'string') {
|
|
|
|
callback = RED.actions.get(callback);
|
|
|
|
}
|
|
|
|
if (callback) {
|
|
|
|
callback.call(opt,value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-12-06 00:50:32 +01:00
|
|
|
if (editorSettingsChanged) {
|
|
|
|
RED.settings.set('editor',currentEditorSettings);
|
|
|
|
}
|
2017-04-28 21:49:01 +02:00
|
|
|
|
2017-04-25 14:43:59 +02:00
|
|
|
}
|
|
|
|
return {
|
2017-04-28 21:49:01 +02:00
|
|
|
init: init,
|
2017-04-30 00:10:55 +02:00
|
|
|
toggle: toggle,
|
|
|
|
show: show,
|
|
|
|
add: addPane
|
2017-04-25 14:43:59 +02:00
|
|
|
};
|
|
|
|
})();
|