1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00
node-red/editor/js/ui/palette-editor.js

968 lines
40 KiB
JavaScript
Raw Permalink Normal View History

2016-08-04 17:49:36 +02:00
/**
* Copyright JS Foundation and other contributors, http://js.foundation
2016-08-04 17:49:36 +02:00
*
* 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.palette.editor = (function() {
var disabled = false;
2016-08-09 11:43:03 +02:00
var editorTabs;
var filterInput;
var searchInput;
2016-08-04 17:49:36 +02:00
var nodeList;
2016-08-09 11:43:03 +02:00
var packageList;
var loadedList = [];
var filteredList = [];
var loadedIndex = {};
2016-08-09 11:43:03 +02:00
2016-08-04 17:49:36 +02:00
var typesInUse = {};
var nodeEntries = {};
var eventTimers = {};
2016-08-05 17:39:41 +02:00
var activeFilter = "";
function semVerCompare(A,B) {
var aParts = A.split(".").map(function(m) { return parseInt(m);});
var bParts = B.split(".").map(function(m) { return parseInt(m);});
for (var i=0;i<3;i++) {
var j = aParts[i]-bParts[i];
if (j<0) { return -1 }
if (j>0) { return 1 }
}
return 0;
}
2016-08-05 14:39:14 +02:00
function delayCallback(start,callback) {
var delta = Date.now() - start;
if (delta < 300) {
delta = 300;
} else {
delta = 0;
}
setTimeout(function() {
callback();
},delta);
}
function changeNodeState(id,state,shade,callback) {
shade.show();
var start = Date.now();
2016-08-04 17:49:36 +02:00
$.ajax({
url:"nodes/"+id,
type: "PUT",
data: JSON.stringify({
enabled: state
}),
contentType: "application/json; charset=utf-8"
2016-08-05 14:39:14 +02:00
}).done(function(data,textStatus,xhr) {
delayCallback(start,function() {
shade.hide();
callback();
});
}).fail(function(xhr,textStatus,err) {
delayCallback(start,function() {
shade.hide();
callback(xhr);
});
})
}
function installNodeModule(id,version,callback) {
var requestBody = {
module: id
};
if (version) {
requestBody.version = version;
}
2016-08-09 11:43:03 +02:00
$.ajax({
url:"nodes",
type: "POST",
data: JSON.stringify(requestBody),
2016-08-09 11:43:03 +02:00
contentType: "application/json; charset=utf-8"
}).done(function(data,textStatus,xhr) {
callback();
}).fail(function(xhr,textStatus,err) {
callback(xhr);
});
}
2016-08-05 14:39:14 +02:00
function removeNodeModule(id,callback) {
$.ajax({
url:"nodes/"+id,
type: "DELETE"
2016-08-04 17:49:36 +02:00
}).done(function(data,textStatus,xhr) {
callback();
}).fail(function(xhr,textStatus,err) {
callback(xhr);
})
}
function refreshNodeModuleList() {
for (var id in nodeEntries) {
if (nodeEntries.hasOwnProperty(id)) {
_refreshNodeModule(id);
}
}
}
2016-08-04 17:49:36 +02:00
function refreshNodeModule(module) {
if (!eventTimers.hasOwnProperty(module)) {
eventTimers[module] = setTimeout(function() {
delete eventTimers[module];
_refreshNodeModule(module);
},100);
}
}
function getContrastingBorder(rgbColor){
var parts = /^rgba?\(\s*(\d+),\s*(\d+),\s*(\d+)[,)]/.exec(rgbColor);
if (parts) {
var r = parseInt(parts[1]);
var g = parseInt(parts[2]);
var b = parseInt(parts[3]);
var yiq = ((r*299)+(g*587)+(b*114))/1000;
if (yiq > 160) {
r = Math.floor(r*0.8);
g = Math.floor(g*0.8);
b = Math.floor(b*0.8);
return "rgb("+r+","+g+","+b+")";
}
}
return rgbColor;
}
2016-08-09 11:43:03 +02:00
function formatUpdatedAt(dateString) {
var now = new Date();
var d = new Date(dateString);
2016-08-15 00:08:37 +02:00
var delta = (Date.now() - new Date(dateString).getTime())/1000;
2016-08-09 11:43:03 +02:00
if (delta < 60) {
2016-08-15 00:08:37 +02:00
return RED._('palette.editor.times.seconds');
2016-08-09 11:43:03 +02:00
}
delta = Math.floor(delta/60);
if (delta < 10) {
2016-08-15 00:08:37 +02:00
return RED._('palette.editor.times.minutes');
2016-08-09 11:43:03 +02:00
}
if (delta < 60) {
2016-08-15 00:08:37 +02:00
return RED._('palette.editor.times.minutesV',{count:delta});
2016-08-09 11:43:03 +02:00
}
delta = Math.floor(delta/60);
if (delta < 24) {
2016-08-15 00:08:37 +02:00
return RED._('palette.editor.times.hoursV',{count:delta});
2016-08-09 11:43:03 +02:00
}
delta = Math.floor(delta/24);
if (delta < 7) {
2016-08-15 00:08:37 +02:00
return RED._('palette.editor.times.daysV',{count:delta})
2016-08-09 11:43:03 +02:00
}
var weeks = Math.floor(delta/7);
var days = delta%7;
if (weeks < 4) {
2016-08-15 00:08:37 +02:00
return RED._('palette.editor.times.weeksV',{count:weeks})
2016-08-09 11:43:03 +02:00
}
var months = Math.floor(weeks/4);
weeks = weeks%4;
if (months < 12) {
2016-08-15 00:08:37 +02:00
return RED._('palette.editor.times.monthsV',{count:months})
2016-08-09 11:43:03 +02:00
}
var years = Math.floor(months/12);
months = months%12;
if (months === 0) {
2016-08-15 00:08:37 +02:00
return RED._('palette.editor.times.yearsV',{count:years})
2016-08-09 11:43:03 +02:00
} else {
2016-08-15 00:08:37 +02:00
return RED._('palette.editor.times.year'+(years>1?'s':'')+'MonthsV',{y:years,count:months})
2016-08-09 11:43:03 +02:00
}
}
2016-08-04 17:49:36 +02:00
function _refreshNodeModule(module) {
if (!nodeEntries.hasOwnProperty(module)) {
nodeEntries[module] = {info:RED.nodes.registry.getModule(module)};
2016-08-05 17:39:41 +02:00
var index = [module];
for (var s in nodeEntries[module].info.sets) {
if (nodeEntries[module].info.sets.hasOwnProperty(s)) {
index.push(s);
index = index.concat(nodeEntries[module].info.sets[s].types)
}
}
nodeEntries[module].index = index.join(",").toLowerCase();
2016-08-04 17:49:36 +02:00
nodeList.editableList('addItem', nodeEntries[module]);
} else {
var moduleInfo = nodeEntries[module].info;
var nodeEntry = nodeEntries[module].elements;
if (nodeEntry) {
var activeTypeCount = 0;
var typeCount = 0;
var errorCount = 0;
nodeEntry.errorList.empty();
2016-08-04 17:49:36 +02:00
nodeEntries[module].totalUseCount = 0;
nodeEntries[module].setUseCount = {};
for (var setName in moduleInfo.sets) {
if (moduleInfo.sets.hasOwnProperty(setName)) {
var inUseCount = 0;
var set = moduleInfo.sets[setName];
var setElements = nodeEntry.sets[setName];
if (set.err) {
errorCount++;
$("<li>").text(set.err).appendTo(nodeEntry.errorList);
}
2016-08-04 17:49:36 +02:00
if (set.enabled) {
activeTypeCount += set.types.length;
}
typeCount += set.types.length;
for (var i=0;i<moduleInfo.sets[setName].types.length;i++) {
var t = moduleInfo.sets[setName].types[i];
inUseCount += (typesInUse[t]||0);
var swatch = setElements.swatches[t];
if (set.enabled) {
var def = RED.nodes.getType(t);
if (def && def.color) {
swatch.css({background:RED.utils.getNodeColor(t,def)});
2016-08-04 17:49:36 +02:00
swatch.css({border: "1px solid "+getContrastingBorder(swatch.css('backgroundColor'))})
} else {
swatch.css({background:"#eee",border:"1px dashed #999"})
}
} else {
swatch.css({background:"#eee",border:"1px dashed #999"})
}
}
nodeEntries[module].setUseCount[setName] = inUseCount;
nodeEntries[module].totalUseCount += inUseCount;
if (inUseCount > 0) {
setElements.enableButton.text(RED._('palette.editor.inuse'));
2016-08-04 17:49:36 +02:00
setElements.enableButton.addClass('disabled');
} else {
setElements.enableButton.removeClass('disabled');
if (set.enabled) {
setElements.enableButton.text(RED._('palette.editor.disable'));
2016-08-04 17:49:36 +02:00
} else {
setElements.enableButton.text(RED._('palette.editor.enable'));
2016-08-04 17:49:36 +02:00
}
}
setElements.setRow.toggleClass("palette-module-set-disabled",!set.enabled);
}
}
if (errorCount === 0) {
nodeEntry.errorRow.hide()
} else {
nodeEntry.errorRow.show();
}
2016-08-04 17:49:36 +02:00
var nodeCount = (activeTypeCount === typeCount)?typeCount:activeTypeCount+" / "+typeCount;
nodeEntry.setCount.text(RED._('palette.editor.nodeCount',{count:typeCount,label:nodeCount}));
2016-08-04 17:49:36 +02:00
if (nodeEntries[module].totalUseCount > 0) {
nodeEntry.enableButton.text(RED._('palette.editor.inuse'));
2016-08-04 17:49:36 +02:00
nodeEntry.enableButton.addClass('disabled');
nodeEntry.removeButton.hide();
} else {
nodeEntry.enableButton.removeClass('disabled');
if (moduleInfo.local) {
2017-05-03 12:37:42 +02:00
nodeEntry.removeButton.css('display', 'inline-block');
}
2016-08-04 17:49:36 +02:00
if (activeTypeCount === 0) {
nodeEntry.enableButton.text(RED._('palette.editor.enableall'));
2016-08-04 17:49:36 +02:00
} else {
nodeEntry.enableButton.text(RED._('palette.editor.disableall'));
2016-08-04 17:49:36 +02:00
}
nodeEntry.container.toggleClass("disabled",(activeTypeCount === 0));
}
}
if (moduleInfo.pending_version) {
nodeEntry.versionSpan.html(moduleInfo.version+' <i class="fa fa-long-arrow-right"></i> '+moduleInfo.pending_version).appendTo(nodeEntry.metaRow)
nodeEntry.updateButton.text(RED._('palette.editor.updated')).addClass('disabled').show();
} else if (loadedIndex.hasOwnProperty(module)) {
if (semVerCompare(loadedIndex[module].version,moduleInfo.version) === 1) {
nodeEntry.updateButton.show();
nodeEntry.updateButton.text(RED._('palette.editor.update',{version:loadedIndex[module].version}));
} else {
nodeEntry.updateButton.hide();
}
} else {
nodeEntry.updateButton.hide();
}
2016-08-04 17:49:36 +02:00
}
2016-08-05 17:39:41 +02:00
}
function filterChange(val) {
2016-08-09 11:43:03 +02:00
activeFilter = val.toLowerCase();
var visible = nodeList.editableList('filter');
var size = nodeList.editableList('length');
2016-08-05 17:39:41 +02:00
if (val === "") {
2016-08-09 11:43:03 +02:00
filterInput.searchBox('count');
2016-08-05 17:39:41 +02:00
} else {
2016-08-09 11:43:03 +02:00
filterInput.searchBox('count',visible+" / "+size);
2016-08-05 17:39:41 +02:00
}
2016-08-04 17:49:36 +02:00
}
2016-08-10 22:59:31 +02:00
var catalogueCount;
var catalogueLoadStatus = [];
var catalogueLoadStart;
var catalogueLoadErrors = false;
var activeSort = sortModulesAZ;
function handleCatalogResponse(err,catalog,index,v) {
catalogueLoadStatus.push(err||v);
if (!err) {
if (v.modules) {
v.modules.forEach(function(m) {
loadedIndex[m.id] = m;
m.index = [m.id];
if (m.keywords) {
m.index = m.index.concat(m.keywords);
}
if (m.updated_at) {
m.timestamp = new Date(m.updated_at).getTime();
} else {
m.timestamp = 0;
}
m.index = m.index.join(",").toLowerCase();
})
loadedList = loadedList.concat(v.modules);
}
searchInput.searchBox('count',loadedList.length);
} else {
catalogueLoadErrors = true;
}
if (catalogueCount > 1) {
2016-08-15 00:08:37 +02:00
$(".palette-module-shade-status").html(RED._('palette.editor.loading')+"<br>"+catalogueLoadStatus.length+"/"+catalogueCount);
}
if (catalogueLoadStatus.length === catalogueCount) {
if (catalogueLoadErrors) {
RED.notify(RED._('palette.editor.errors.catalogLoadFailed',{url: catalog}),"error",false,8000);
}
var delta = 250-(Date.now() - catalogueLoadStart);
setTimeout(function() {
$("#palette-module-install-shade").hide();
},Math.max(delta,0));
}
}
2016-08-09 11:43:03 +02:00
function initInstallTab() {
2016-08-13 01:33:41 +02:00
if (loadedList.length === 0) {
loadedList = [];
loadedIndex = {};
2016-08-13 01:33:41 +02:00
packageList.editableList('empty');
2017-05-03 12:37:42 +02:00
$(".palette-module-shade-status").text(RED._('palette.editor.loading'));
var catalogues = RED.settings.theme('palette.catalogues')||['https://catalogue.nodered.org/catalogue.json'];
2016-08-13 01:33:41 +02:00
catalogueLoadStatus = [];
catalogueLoadErrors = false;
2016-08-13 01:33:41 +02:00
catalogueCount = catalogues.length;
if (catalogues.length > 1) {
2016-08-15 00:08:37 +02:00
$(".palette-module-shade-status").html(RED._('palette.editor.loading')+"<br>0/"+catalogues.length);
2016-08-13 01:33:41 +02:00
}
$("#palette-module-install-shade").show();
catalogueLoadStart = Date.now();
2017-05-03 12:37:42 +02:00
var handled = 0;
2016-08-13 01:33:41 +02:00
catalogues.forEach(function(catalog,index) {
$.getJSON(catalog, {_: new Date().getTime()},function(v) {
handleCatalogResponse(null,catalog,index,v);
refreshNodeModuleList();
}).fail(function(jqxhr, textStatus, error) {
handleCatalogResponse(jqxhr,catalog,index);
2017-05-03 12:37:42 +02:00
}).always(function() {
handled++;
if (handled === catalogueCount) {
searchInput.searchBox('change');
}
2016-08-13 01:33:41 +02:00
})
});
}
}
2016-08-09 11:43:03 +02:00
function refreshFilteredItems() {
packageList.editableList('empty');
2017-05-03 12:37:42 +02:00
var currentFilter = searchInput.searchBox('value').trim();
if (currentFilter === ""){
packageList.editableList('addItem',{count:loadedList.length})
return;
}
filteredList.sort(activeSort);
for (var i=0;i<Math.min(10,filteredList.length);i++) {
packageList.editableList('addItem',filteredList[i]);
}
if (filteredList.length === 0) {
packageList.editableList('addItem',{});
}
if (filteredList.length > 10) {
packageList.editableList('addItem',{start:10,more:filteredList.length-10})
}
2016-08-09 11:43:03 +02:00
}
function sortModulesAZ(A,B) {
return A.info.id.localeCompare(B.info.id);
}
function sortModulesRecent(A,B) {
return -1 * (A.info.timestamp-B.info.timestamp);
}
2016-08-04 17:49:36 +02:00
function init() {
if (RED.settings.theme('palette.editable') === false) {
return;
}
createSettingsPane();
RED.userSettings.add({
id:'palette',
title: RED._("palette.editor.palette"),
get: getSettingsPane,
close: function() {
settingsPane.detach();
2017-05-03 12:37:42 +02:00
},
focus: function() {
editorTabs.resize();
setTimeout(function() {
filterInput.focus();
},200);
}
})
2017-05-03 12:37:42 +02:00
RED.actions.add("core:manage-palette",function() {
RED.userSettings.show('palette');
});
RED.events.on('registry:module-updated', function(ns) {
refreshNodeModule(ns.module);
});
RED.events.on('registry:node-set-enabled', function(ns) {
refreshNodeModule(ns.module);
});
RED.events.on('registry:node-set-disabled', function(ns) {
refreshNodeModule(ns.module);
});
RED.events.on('registry:node-type-added', function(nodeType) {
if (!/^subflow:/.test(nodeType)) {
var ns = RED.nodes.registry.getNodeSetForType(nodeType);
refreshNodeModule(ns.module);
}
});
RED.events.on('registry:node-type-removed', function(nodeType) {
if (!/^subflow:/.test(nodeType)) {
var ns = RED.nodes.registry.getNodeSetForType(nodeType);
refreshNodeModule(ns.module);
}
});
RED.events.on('registry:node-set-added', function(ns) {
refreshNodeModule(ns.module);
for (var i=0;i<filteredList.length;i++) {
if (filteredList[i].info.id === ns.module) {
var installButton = filteredList[i].elements.installButton;
installButton.addClass('disabled');
installButton.text(RED._('palette.editor.installed'));
break;
}
}
});
RED.events.on('registry:node-set-removed', function(ns) {
var module = RED.nodes.registry.getModule(ns.module);
if (!module) {
var entry = nodeEntries[ns.module];
if (entry) {
nodeList.editableList('removeItem', entry);
delete nodeEntries[ns.module];
for (var i=0;i<filteredList.length;i++) {
if (filteredList[i].info.id === ns.module) {
var installButton = filteredList[i].elements.installButton;
installButton.removeClass('disabled');
installButton.text(RED._('palette.editor.install'));
break;
}
}
}
}
});
RED.events.on('nodes:add', function(n) {
if (!/^subflow:/.test(n.type)) {
typesInUse[n.type] = (typesInUse[n.type]||0)+1;
if (typesInUse[n.type] === 1) {
var ns = RED.nodes.registry.getNodeSetForType(n.type);
refreshNodeModule(ns.module);
}
}
})
RED.events.on('nodes:remove', function(n) {
if (typesInUse.hasOwnProperty(n.type)) {
typesInUse[n.type]--;
if (typesInUse[n.type] === 0) {
delete typesInUse[n.type];
var ns = RED.nodes.registry.getNodeSetForType(n.type);
refreshNodeModule(ns.module);
}
}
})
}
var settingsPane;
2017-05-03 12:37:42 +02:00
function getSettingsPane() {
2017-05-03 12:37:42 +02:00
initInstallTab();
editorTabs.activateTab('nodes');
return settingsPane;
}
2017-05-03 12:37:42 +02:00
function createSettingsPane() {
settingsPane = $('<div id="user-settings-tab-palette"></div>');
var content = $('<div id="palette-editor">'+
'<ul id="palette-editor-tabs"></ul>'+
'</div>').appendTo(settingsPane);
2016-08-09 11:43:03 +02:00
editorTabs = RED.tabs.create({
element: settingsPane.find('#palette-editor-tabs'),
2016-08-09 11:43:03 +02:00
onchange:function(tab) {
content.find(".palette-editor-tab").hide();
2016-08-09 11:43:03 +02:00
tab.content.show();
if (filterInput) {
filterInput.searchBox('value',"");
}
if (searchInput) {
searchInput.searchBox('value',"");
}
2016-08-09 11:43:03 +02:00
if (tab.id === 'install') {
if (searchInput) {
searchInput.focus();
}
} else {
if (filterInput) {
filterInput.focus();
}
}
},
minimumActiveTabWidth: 110
});
var modulesTab = $('<div>',{class:"palette-editor-tab"}).appendTo(content);
2016-08-05 17:39:41 +02:00
2016-08-09 11:43:03 +02:00
editorTabs.addTab({
id: 'nodes',
2016-08-15 00:08:37 +02:00
label: RED._('palette.editor.tab-nodes'),
2016-08-09 11:43:03 +02:00
content: modulesTab
})
2016-08-05 17:39:41 +02:00
2016-08-09 11:43:03 +02:00
var filterDiv = $('<div>',{class:"palette-search"}).appendTo(modulesTab);
filterInput = $('<input type="text" data-i18n="[placeholder]palette.filter"></input>')
.appendTo(filterDiv)
.searchBox({
delay: 200,
change: function() {
filterChange($(this).val());
}
2016-08-05 17:39:41 +02:00
});
2016-08-09 11:43:03 +02:00
nodeList = $('<ol>',{id:"palette-module-list", style:"position: absolute;top: 35px;bottom: 0;left: 0;right: 0px;"}).appendTo(modulesTab).editableList({
2016-08-04 17:49:36 +02:00
addButton: false,
scrollOnAdd: false,
2016-08-04 17:49:36 +02:00
sort: function(A,B) {
return A.info.name.localeCompare(B.info.name);
},
2016-08-05 17:39:41 +02:00
filter: function(data) {
if (activeFilter === "" ) {
return true;
}
return (activeFilter==="")||(data.index.indexOf(activeFilter) > -1);
},
2016-08-04 17:49:36 +02:00
addItem: function(container,i,object) {
var entry = object.info;
if (entry) {
var headerRow = $('<div>',{class:"palette-module-header"}).appendTo(container);
var titleRow = $('<div class="palette-module-meta palette-module-name"><i class="fa fa-cube"></i></div>').appendTo(headerRow);
$('<span>').text(entry.name).appendTo(titleRow);
var metaRow = $('<div class="palette-module-meta palette-module-version"><i class="fa fa-tag"></i></div>').appendTo(headerRow);
var versionSpan = $('<span>').text(entry.version).appendTo(metaRow);
var errorRow = $('<div class="palette-module-meta palette-module-errors"><i class="fa fa-warning"></i></div>').hide().appendTo(headerRow);
var errorList = $('<ul class="palette-module-error-list"></ul>').appendTo(errorRow);
var buttonRow = $('<div>',{class:"palette-module-meta"}).appendTo(headerRow);
var setButton = $('<a href="#" class="editor-button editor-button-small palette-module-set-button"><i class="fa fa-angle-right palette-module-node-chevron"></i> </a>').appendTo(buttonRow);
var setCount = $('<span>').appendTo(setButton);
var buttonGroup = $('<div>',{class:"palette-module-button-group"}).appendTo(buttonRow);
var updateButton = $('<a href="#" class="editor-button editor-button-small"></a>').text(RED._('palette.editor.update')).appendTo(buttonGroup);
2017-05-03 12:37:42 +02:00
updateButton.attr('id','up_'+Math.floor(Math.random()*1000000000));
updateButton.click(function(evt) {
evt.preventDefault();
if ($(this).hasClass('disabled')) {
return;
}
update(entry,loadedIndex[entry.name].version,container,function(err){});
})
var removeButton = $('<a href="#" class="editor-button editor-button-small"></a>').text(RED._('palette.editor.remove')).appendTo(buttonGroup);
2017-05-03 12:37:42 +02:00
removeButton.attr('id','up_'+Math.floor(Math.random()*1000000000));
removeButton.click(function(evt) {
evt.preventDefault();
remove(entry,container,function(err){});
2016-08-09 11:43:03 +02:00
})
if (!entry.local) {
removeButton.hide();
2016-08-04 17:49:36 +02:00
}
var enableButton = $('<a href="#" class="editor-button editor-button-small"></a>').text(RED._('palette.editor.disableall')).appendTo(buttonGroup);
2016-08-04 17:49:36 +02:00
var contentRow = $('<div>',{class:"palette-module-content"}).appendTo(container);
var shade = $('<div class="palette-module-shade hide"><img src="red/images/spin.svg" class="palette-spinner"/></div>').appendTo(container);
object.elements = {
updateButton: updateButton,
removeButton: removeButton,
enableButton: enableButton,
errorRow: errorRow,
errorList: errorList,
setCount: setCount,
container: container,
shade: shade,
versionSpan: versionSpan,
sets: {}
}
setButton.click(function(evt) {
evt.preventDefault();
if (container.hasClass('expanded')) {
container.removeClass('expanded');
contentRow.slideUp();
} else {
container.addClass('expanded');
contentRow.slideDown();
}
2016-08-04 17:49:36 +02:00
})
var setList = Object.keys(entry.sets)
setList.sort(function(A,B) {
return A.toLowerCase().localeCompare(B.toLowerCase());
});
setList.forEach(function(setName) {
var set = entry.sets[setName];
var setRow = $('<div>',{class:"palette-module-set"}).appendTo(contentRow);
var buttonGroup = $('<div>',{class:"palette-module-set-button-group"}).appendTo(setRow);
var typeSwatches = {};
set.types.forEach(function(t) {
var typeDiv = $('<div>',{class:"palette-module-type"}).appendTo(setRow);
typeSwatches[t] = $('<span>',{class:"palette-module-type-swatch"}).appendTo(typeDiv);
$('<span>',{class:"palette-module-type-node"}).text(t).appendTo(typeDiv);
})
var enableButton = $('<a href="#" class="editor-button editor-button-small"></a>').appendTo(buttonGroup);
enableButton.click(function(evt) {
evt.preventDefault();
if (object.setUseCount[setName] === 0) {
var currentSet = RED.nodes.registry.getNodeSet(set.id);
shade.show();
var newState = !currentSet.enabled
changeNodeState(set.id,newState,shade,function(xhr){
if (xhr) {
if (xhr.responseJSON) {
RED.notify(RED._('palette.editor.errors.'+(newState?'enable':'disable')+'Failed',{module: id,message:xhr.responseJSON.message}));
}
}
});
}
})
object.elements.sets[set.name] = {
setRow: setRow,
enableButton: enableButton,
swatches: typeSwatches
};
});
2016-08-04 17:49:36 +02:00
enableButton.click(function(evt) {
evt.preventDefault();
if (object.totalUseCount === 0) {
changeNodeState(entry.name,(container.hasClass('disabled')),shade,function(xhr){
if (xhr) {
if (xhr.responseJSON) {
RED.notify(RED._('palette.editor.errors.installFailed',{module: id,message:xhr.responseJSON.message}));
}
}
2016-08-04 17:49:36 +02:00
});
}
})
refreshNodeModule(entry.name);
} else {
$('<div>',{class:"red-ui-search-empty"}).text(RED._('search.empty')).appendTo(container);
}
2016-08-04 17:49:36 +02:00
}
});
2016-08-09 11:43:03 +02:00
var installTab = $('<div>',{class:"palette-editor-tab hide"}).appendTo(content);
2016-08-09 11:43:03 +02:00
editorTabs.addTab({
id: 'install',
2016-08-15 00:08:37 +02:00
label: RED._('palette.editor.tab-install'),
2016-08-09 11:43:03 +02:00
content: installTab
})
var toolBar = $('<div>',{class:"palette-editor-toolbar"}).appendTo(installTab);
2016-08-09 11:43:03 +02:00
var searchDiv = $('<div>',{class:"palette-search"}).appendTo(installTab);
searchInput = $('<input type="text" data-i18n="[placeholder]palette.search"></input>')
.appendTo(searchDiv)
.searchBox({
delay: 300,
change: function() {
2017-05-03 12:37:42 +02:00
var searchTerm = $(this).val().trim().toLowerCase();
if (searchTerm.length > 0) {
2016-08-09 11:43:03 +02:00
filteredList = loadedList.filter(function(m) {
return (m.index.indexOf(searchTerm) > -1);
}).map(function(f) { return {info:f}});
refreshFilteredItems();
2016-08-09 11:43:03 +02:00
searchInput.searchBox('count',filteredList.length+" / "+loadedList.length);
} else {
searchInput.searchBox('count',loadedList.length);
packageList.editableList('empty');
2017-05-03 12:37:42 +02:00
packageList.editableList('addItem',{count:loadedList.length});
2016-08-09 11:43:03 +02:00
}
}
});
$('<span>').text(RED._("palette.editor.sort")+' ').appendTo(toolBar);
2016-10-25 22:15:06 +02:00
var sortGroup = $('<span class="button-group"></span>').appendTo(toolBar);
2016-08-15 00:08:37 +02:00
var sortAZ = $('<a href="#" class="sidebar-header-button-toggle selected" data-i18n="palette.editor.sortAZ"></a>').appendTo(sortGroup);
var sortRecent = $('<a href="#" class="sidebar-header-button-toggle" data-i18n="palette.editor.sortRecent"></a>').appendTo(sortGroup);
sortAZ.click(function(e) {
e.preventDefault();
if ($(this).hasClass("selected")) {
return;
}
$(this).addClass("selected");
sortRecent.removeClass("selected");
activeSort = sortModulesAZ;
refreshFilteredItems();
});
sortRecent.click(function(e) {
e.preventDefault();
if ($(this).hasClass("selected")) {
return;
}
$(this).addClass("selected");
sortAZ.removeClass("selected");
activeSort = sortModulesRecent;
refreshFilteredItems();
});
2016-08-09 11:43:03 +02:00
var refreshSpan = $('<span>').appendTo(toolBar);
var refreshButton = $('<a href="#" class="sidebar-header-button"><i class="fa fa-refresh"></i></a>').appendTo(refreshSpan);
refreshButton.click(function(e) {
e.preventDefault();
2016-08-13 01:33:41 +02:00
loadedList = [];
loadedIndex = {};
initInstallTab();
})
2016-09-30 11:46:13 +02:00
packageList = $('<ol>',{style:"position: absolute;top: 78px;bottom: 0;left: 0;right: 0px;"}).appendTo(installTab).editableList({
addButton: false,
scrollOnAdd: false,
addItem: function(container,i,object) {
2017-05-03 12:37:42 +02:00
if (object.count) {
$('<div>',{class:"red-ui-search-empty"}).text(RED._('palette.editor.moduleCount',{count:object.count})).appendTo(container);
2017-05-03 12:37:42 +02:00
return
}
if (object.more) {
container.addClass('palette-module-more');
var moreRow = $('<div>',{class:"palette-module-header palette-module"}).appendTo(container);
var moreLink = $('<a href="#"></a>').text(RED._('palette.editor.more',{count:object.more})).appendTo(moreRow);
moreLink.click(function(e) {
e.preventDefault();
packageList.editableList('removeItem',object);
for (var i=object.start;i<Math.min(object.start+10,object.start+object.more);i++) {
packageList.editableList('addItem',filteredList[i]);
}
if (object.more > 10) {
packageList.editableList('addItem',{start:object.start+10, more:object.more-10})
}
})
return;
}
if (object.info) {
var entry = object.info;
var headerRow = $('<div>',{class:"palette-module-header"}).appendTo(container);
var titleRow = $('<div class="palette-module-meta"><i class="fa fa-cube"></i></div>').appendTo(headerRow);
$('<span>',{class:"palette-module-name"}).text(entry.name||entry.id).appendTo(titleRow);
$('<a target="_blank" class="palette-module-link"><i class="fa fa-external-link"></i></a>').attr('href',entry.url).appendTo(titleRow);
var descRow = $('<div class="palette-module-meta"></div>').appendTo(headerRow);
$('<div>',{class:"palette-module-description"}).text(entry.description).appendTo(descRow);
var metaRow = $('<div class="palette-module-meta"></div>').appendTo(headerRow);
$('<span class="palette-module-version"><i class="fa fa-tag"></i> '+entry.version+'</span>').appendTo(metaRow);
$('<span class="palette-module-updated"><i class="fa fa-calendar"></i> '+formatUpdatedAt(entry.updated_at)+'</span>').appendTo(metaRow);
var buttonRow = $('<div>',{class:"palette-module-meta"}).appendTo(headerRow);
var buttonGroup = $('<div>',{class:"palette-module-button-group"}).appendTo(buttonRow);
var installButton = $('<a href="#" class="editor-button editor-button-small"></a>').text(RED._('palette.editor.install')).appendTo(buttonGroup);
installButton.click(function(e) {
e.preventDefault();
if (!$(this).hasClass('disabled')) {
install(entry,container,function(xhr) {});
}
})
if (nodeEntries.hasOwnProperty(entry.id)) {
installButton.addClass('disabled');
installButton.text(RED._('palette.editor.installed'));
}
object.elements = {
installButton:installButton
}
} else {
$('<div>',{class:"red-ui-search-empty"}).text(RED._('search.empty')).appendTo(container);
}
}
});
$('<div id="palette-module-install-shade" class="palette-module-shade hide"><div class="palette-module-shade-status"></div><img src="red/images/spin.svg" class="palette-spinner"/></div>').appendTo(installTab);
}
function update(entry,version,container,done) {
if (RED.settings.theme('palette.editable') === false) {
done(new Error('Palette not editable'));
return;
}
var notification = RED.notify(RED._("palette.editor.confirm.update.body",{module:entry.name}),{
modal: true,
fixed: true,
buttons: [
{
text: RED._("common.label.cancel"),
click: function() {
notification.close();
}
},
{
text: RED._("palette.editor.confirm.button.update"),
class: "primary palette-module-install-confirm-button-update",
click: function() {
var spinner = RED.utils.addSpinnerOverlay(container, true);
installNodeModule(entry.name,version,function(xhr) {
spinner.remove();
if (xhr) {
if (xhr.responseJSON) {
RED.notify(RED._('palette.editor.errors.updateFailed',{module: entry.name,message:xhr.responseJSON.message}));
}
}
done(xhr);
});
notification.close();
}
}
]
})
}
function remove(entry,container,done) {
if (RED.settings.theme('palette.editable') === false) {
done(new Error('Palette not editable'));
return;
}
var notification = RED.notify(RED._("palette.editor.confirm.remove.body",{module:entry.name}),{
modal: true,
fixed: true,
buttons: [
{
text: RED._("common.label.cancel"),
click: function() {
notification.close();
}
},
{
text: RED._("palette.editor.confirm.button.remove"),
class: "primary palette-module-install-confirm-button-remove",
click: function() {
var spinner = RED.utils.addSpinnerOverlay(container, true);
removeNodeModule(entry.name, function(xhr) {
spinner.remove();
if (xhr) {
if (xhr.responseJSON) {
RED.notify(RED._('palette.editor.errors.removeFailed',{module: entry.name,message:xhr.responseJSON.message}));
}
}
})
notification.close();
}
}
]
})
2016-08-04 17:49:36 +02:00
}
function install(entry,container,done) {
if (RED.settings.theme('palette.editable') === false) {
done(new Error('Palette not editable'));
return;
}
var buttons = [
{
text: RED._("common.label.cancel"),
click: function() {
notification.close();
}
}
];
if (entry.url) {
buttons.push({
text: RED._("palette.editor.confirm.button.review"),
class: "primary palette-module-install-confirm-button-install",
click: function() {
var url = entry.url||"";
window.open(url);
}
});
}
buttons.push({
text: RED._("palette.editor.confirm.button.install"),
class: "primary palette-module-install-confirm-button-install",
click: function() {
var spinner = RED.utils.addSpinnerOverlay(container, true);
installNodeModule(entry.id,entry.version,function(xhr) {
spinner.remove();
if (xhr) {
if (xhr.responseJSON) {
RED.notify(RED._('palette.editor.errors.installFailed',{module: entry.id,message:xhr.responseJSON.message}));
}
}
done(xhr);
});
notification.close();
}
});
var notification = RED.notify(RED._("palette.editor.confirm.install.body",{module:entry.id}),{
modal: true,
fixed: true,
buttons: buttons
})
}
2016-08-04 17:49:36 +02:00
return {
init: init,
install: install
2016-08-04 17:49:36 +02:00
}
})();