mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
254 lines
11 KiB
JavaScript
254 lines
11 KiB
JavaScript
/**
|
|
* Copyright 2015 IBM Corp.
|
|
*
|
|
* 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.deploy = (function() {
|
|
|
|
var deploymentTypes = {
|
|
"full":{img:"red/images/deploy-full-o.png"},
|
|
"nodes":{img:"red/images/deploy-nodes-o.png"},
|
|
"flows":{img:"red/images/deploy-flows-o.png"}
|
|
}
|
|
|
|
var ignoreDeployWarnings = {
|
|
unknown: false,
|
|
unusedConfig: false,
|
|
invalid: false
|
|
}
|
|
|
|
var deploymentType = "full";
|
|
|
|
function changeDeploymentType(type) {
|
|
deploymentType = type;
|
|
$("#btn-deploy img").attr("src",deploymentTypes[type].img);
|
|
}
|
|
|
|
|
|
/**
|
|
* options:
|
|
* type: "default" - Button with drop-down options - no further customisation available
|
|
* type: "simple" - Button without dropdown. Customisations:
|
|
* label: the text to display - default: "Deploy"
|
|
* icon : the icon to use. Null removes the icon. default: "red/images/deploy-full-o.png"
|
|
*/
|
|
function init(options) {
|
|
options = options || {};
|
|
var type = options.type || "default";
|
|
|
|
if (type == "default") {
|
|
$('<li><span class="deploy-button-group button-group">'+
|
|
'<a id="btn-deploy" class="deploy-button disabled" href="#"><img id="btn-deploy-icon" src="red/images/deploy-full-o.png"> <span>'+RED._("deploy.deploy")+'</span></a>'+
|
|
'<a id="btn-deploy-options" data-toggle="dropdown" class="deploy-button" href="#"><i class="fa fa-caret-down"></i></a>'+
|
|
'</span></li>').prependTo(".header-toolbar");
|
|
RED.menu.init({id:"btn-deploy-options",
|
|
options: [
|
|
{id:"deploymenu-item-full",toggle:"deploy-type",icon:"red/images/deploy-full.png",label:RED._("deploy.full"),sublabel:RED._("deploy.fullDesc"),selected: true, onselect:function(s) { if(s){changeDeploymentType("full")}}},
|
|
{id:"deploymenu-item-flow",toggle:"deploy-type",icon:"red/images/deploy-flows.png",label:RED._("deploy.modifiedFlows"),sublabel:RED._("deploy.modifiedFlowsDesc"), onselect:function(s) {if(s){changeDeploymentType("flows")}}},
|
|
{id:"deploymenu-item-node",toggle:"deploy-type",icon:"red/images/deploy-nodes.png",label:RED._("deploy.modifiedNodes"),sublabel:RED._("deploy.modifiedNodesDesc"),onselect:function(s) { if(s){changeDeploymentType("nodes")}}}
|
|
]
|
|
});
|
|
} else if (type == "simple") {
|
|
var label = options.label || RED._("deploy.deploy");
|
|
var icon = 'red/images/deploy-full-o.png';
|
|
if (options.hasOwnProperty('icon')) {
|
|
icon = options.icon;
|
|
}
|
|
|
|
$('<li><span class="deploy-button-group button-group">'+
|
|
'<a id="btn-deploy" class="deploy-button disabled" href="#">'+
|
|
(icon?'<img id="btn-deploy-icon" src="'+icon+'"> ':'')+
|
|
'<span>'+label+'</span></a>'+
|
|
'</span></li>').prependTo(".header-toolbar");
|
|
}
|
|
|
|
$('#btn-deploy').click(function() { save(); });
|
|
|
|
$( "#node-dialog-confirm-deploy" ).dialog({
|
|
title: "Confirm deploy",
|
|
modal: true,
|
|
autoOpen: false,
|
|
width: 550,
|
|
height: "auto",
|
|
buttons: [
|
|
{
|
|
text: RED._("deploy.confirmDeploy"),
|
|
click: function() {
|
|
|
|
var ignoreChecked = $( "#node-dialog-confirm-deploy-hide" ).prop("checked");
|
|
if (ignoreChecked) {
|
|
ignoreDeployWarnings[$( "#node-dialog-confirm-deploy-type" ).val()] = true;
|
|
}
|
|
save(true);
|
|
$( this ).dialog( "close" );
|
|
}
|
|
},
|
|
{
|
|
text: RED._("deploy.cancelDeploy"),
|
|
click: function() {
|
|
$( this ).dialog( "close" );
|
|
}
|
|
}
|
|
],
|
|
create: function() {
|
|
$("#node-dialog-confirm-deploy").parent().find("div.ui-dialog-buttonpane")
|
|
.append('<div style="height:0; vertical-align: middle; display:inline-block;">'+
|
|
'<input style="vertical-align:top;" type="checkbox" id="node-dialog-confirm-deploy-hide">'+
|
|
'<label style="display:inline;" for="node-dialog-confirm-deploy-hide"> do not warn about this again</label>'+
|
|
'<input type="hidden" id="node-dialog-confirm-deploy-type">'+
|
|
'</div>');
|
|
}
|
|
});
|
|
|
|
RED.nodes.on('change',function(state) {
|
|
if (state.dirty) {
|
|
window.onbeforeunload = function() {
|
|
return RED._("deploy.undeployedChanges");
|
|
}
|
|
$("#btn-deploy").removeClass("disabled");
|
|
} else {
|
|
window.onbeforeunload = null;
|
|
$("#btn-deploy").addClass("disabled");
|
|
}
|
|
});
|
|
}
|
|
|
|
function save(force) {
|
|
if (RED.nodes.dirty()) {
|
|
//$("#debug-tab-clear").click(); // uncomment this to auto clear debug on deploy
|
|
|
|
if (!force) {
|
|
var hasUnknown = false;
|
|
var hasInvalid = false;
|
|
var hasUnusedConfig = false;
|
|
|
|
var unknownNodes = [];
|
|
RED.nodes.eachNode(function(node) {
|
|
hasInvalid = hasInvalid || !node.valid;
|
|
if (node.type === "unknown") {
|
|
if (unknownNodes.indexOf(node.name) == -1) {
|
|
unknownNodes.push(node.name);
|
|
}
|
|
}
|
|
});
|
|
hasUnknown = unknownNodes.length > 0;
|
|
|
|
var unusedConfigNodes = {};
|
|
RED.nodes.eachConfig(function(node) {
|
|
if (node.users.length === 0) {
|
|
var label = "";
|
|
if (typeof node._def.label == "function") {
|
|
label = node._def.label.call(node);
|
|
} else {
|
|
label = node._def.label;
|
|
}
|
|
label = label || node.id;
|
|
unusedConfigNodes[node.type] = unusedConfigNodes[node.type] || [];
|
|
unusedConfigNodes[node.type].push(label);
|
|
hasUnusedConfig = true;
|
|
}
|
|
});
|
|
|
|
$( "#node-dialog-confirm-deploy-config" ).hide();
|
|
$( "#node-dialog-confirm-deploy-unknown" ).hide();
|
|
$( "#node-dialog-confirm-deploy-unused" ).hide();
|
|
|
|
var showWarning = false;
|
|
|
|
if (hasUnknown && !ignoreDeployWarnings.unknown) {
|
|
showWarning = true;
|
|
$( "#node-dialog-confirm-deploy-type" ).val("unknown");
|
|
$( "#node-dialog-confirm-deploy-unknown" ).show();
|
|
$( "#node-dialog-confirm-deploy-unknown-list" )
|
|
.html("<li>"+unknownNodes.join("</li><li>")+"</li>");
|
|
} else if (hasInvalid && !ignoreDeployWarnings.invalid) {
|
|
showWarning = true;
|
|
$( "#node-dialog-confirm-deploy-type" ).val("invalid");
|
|
$( "#node-dialog-confirm-deploy-config" ).show();
|
|
} else if (hasUnusedConfig && !ignoreDeployWarnings.unusedConfig) {
|
|
showWarning = true;
|
|
$( "#node-dialog-confirm-deploy-type" ).val("unusedConfig");
|
|
$( "#node-dialog-confirm-deploy-unused" ).show();
|
|
var unusedNodeLabels = [];
|
|
var unusedTypes = Object.keys(unusedConfigNodes).sort();
|
|
unusedTypes.forEach(function(type) {
|
|
unusedConfigNodes[type].forEach(function(label) {
|
|
unusedNodeLabels.push(type+": "+label);
|
|
});
|
|
});
|
|
$( "#node-dialog-confirm-deploy-unused-list" )
|
|
.html("<li>"+unusedNodeLabels.join("</li><li>")+"</li>");
|
|
}
|
|
if (showWarning) {
|
|
$( "#node-dialog-confirm-deploy-hide" ).prop("checked",false);
|
|
$( "#node-dialog-confirm-deploy" ).dialog( "open" );
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
var nns = RED.nodes.createCompleteNodeSet();
|
|
|
|
$("#btn-deploy-icon").removeClass('fa-download');
|
|
$("#btn-deploy-icon").addClass('spinner');
|
|
RED.nodes.dirty(false);
|
|
|
|
$.ajax({
|
|
url:"flows",
|
|
type: "POST",
|
|
data: JSON.stringify(nns),
|
|
contentType: "application/json; charset=utf-8",
|
|
headers: {
|
|
"Node-RED-Deployment-Type":deploymentType
|
|
}
|
|
}).done(function(data,textStatus,xhr) {
|
|
RED.notify(RED._("deploy.successfulDeploy"),"success");
|
|
RED.nodes.eachNode(function(node) {
|
|
if (node.changed) {
|
|
node.dirty = true;
|
|
node.changed = false;
|
|
}
|
|
if(node.credentials) {
|
|
delete node.credentials;
|
|
}
|
|
});
|
|
RED.nodes.eachConfig(function (confNode) {
|
|
if (confNode.credentials) {
|
|
delete confNode.credentials;
|
|
}
|
|
});
|
|
// Once deployed, cannot undo back to a clean state
|
|
RED.history.markAllDirty();
|
|
RED.view.redraw();
|
|
}).fail(function(xhr,textStatus,err) {
|
|
RED.nodes.dirty(true);
|
|
if (xhr.responseText) {
|
|
RED.notify(RED._("deploy.error")+xhr.responseJSON.message,"error");
|
|
} else {
|
|
RED.notify(RED._("deploy.error")+RED._("deploy.noResponseError"),"error");
|
|
}
|
|
}).always(function() {
|
|
$("#btn-deploy-icon").removeClass('spinner');
|
|
$("#btn-deploy-icon").addClass('fa-download');
|
|
});
|
|
}
|
|
}
|
|
|
|
return {
|
|
init: init
|
|
}
|
|
})();
|