2015-03-13 00:38:37 +01:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2015-03-13 00:38:37 +01: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.clipboard = (function() {
|
2015-05-26 22:52:23 +02:00
|
|
|
|
|
|
|
var dialog;
|
|
|
|
var dialogContainer;
|
|
|
|
var exportNodesDialog;
|
|
|
|
var importNodesDialog;
|
2017-01-11 15:15:14 +01:00
|
|
|
var disabled = false;
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2016-01-08 20:54:16 +01:00
|
|
|
function setupDialogs() {
|
2016-05-03 22:36:22 +02:00
|
|
|
dialog = $('<div id="clipboard-dialog" class="hide node-red-dialog"><form class="dialog-form form-horizontal"></form></div>')
|
2015-05-26 22:52:23 +02:00
|
|
|
.appendTo("body")
|
|
|
|
.dialog({
|
|
|
|
modal: true,
|
|
|
|
autoOpen: false,
|
|
|
|
width: 500,
|
|
|
|
resizable: false,
|
|
|
|
buttons: [
|
|
|
|
{
|
2016-05-03 22:36:22 +02:00
|
|
|
id: "clipboard-dialog-cancel",
|
|
|
|
text: RED._("common.label.cancel"),
|
2015-05-26 22:52:23 +02:00
|
|
|
click: function() {
|
|
|
|
$( this ).dialog( "close" );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2016-05-03 22:36:22 +02:00
|
|
|
id: "clipboard-dialog-close",
|
|
|
|
class: "primary",
|
|
|
|
text: RED._("common.label.close"),
|
2015-05-26 22:52:23 +02:00
|
|
|
click: function() {
|
|
|
|
$( this ).dialog( "close" );
|
|
|
|
}
|
|
|
|
},
|
2016-09-19 14:54:23 +02:00
|
|
|
{
|
|
|
|
id: "clipboard-dialog-copy",
|
|
|
|
class: "primary",
|
|
|
|
text: RED._("clipboard.export.copy"),
|
|
|
|
click: function() {
|
|
|
|
$("#clipboard-export").select();
|
|
|
|
document.execCommand("copy");
|
|
|
|
document.getSelection().removeAllRanges();
|
|
|
|
RED.notify(RED._("clipboard.nodesExported"));
|
|
|
|
$( this ).dialog( "close" );
|
|
|
|
}
|
|
|
|
},
|
2015-05-26 22:52:23 +02:00
|
|
|
{
|
2016-05-03 22:36:22 +02:00
|
|
|
id: "clipboard-dialog-ok",
|
|
|
|
class: "primary",
|
|
|
|
text: RED._("common.label.import"),
|
2015-05-26 22:52:23 +02:00
|
|
|
click: function() {
|
2016-09-23 23:02:12 +02:00
|
|
|
RED.view.importNodes($("#clipboard-import").val(),$("#import-tab > a.selected").attr('id') === 'import-tab-new');
|
2015-05-26 22:52:23 +02:00
|
|
|
$( this ).dialog( "close" );
|
2015-03-13 00:38:37 +01:00
|
|
|
}
|
|
|
|
}
|
2015-05-26 22:52:23 +02:00
|
|
|
],
|
|
|
|
open: function(e) {
|
|
|
|
$(this).parent().find(".ui-dialog-titlebar-close").hide();
|
2015-03-13 00:38:37 +01:00
|
|
|
},
|
2015-05-26 22:52:23 +02:00
|
|
|
close: function(e) {
|
2015-03-13 00:38:37 +01:00
|
|
|
}
|
2016-01-08 20:54:16 +01:00
|
|
|
});
|
2015-03-13 00:38:37 +01:00
|
|
|
|
2015-05-26 22:52:23 +02:00
|
|
|
dialogContainer = dialog.children(".dialog-form");
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2016-09-19 14:54:23 +02:00
|
|
|
exportNodesDialog =
|
|
|
|
'<div class="form-row">'+
|
2016-09-23 23:02:12 +02:00
|
|
|
'<label style="width:auto;margin-right: 10px;" data-i18n="clipboard.export.copy"></label>'+
|
2016-09-19 14:54:23 +02:00
|
|
|
'<span id="export-range-group" class="button-group">'+
|
|
|
|
'<a id="export-range-selected" class="editor-button toggle" href="#" data-i18n="clipboard.export.selected"></a>'+
|
|
|
|
'<a id="export-range-flow" class="editor-button toggle" href="#" data-i18n="clipboard.export.current"></a>'+
|
|
|
|
'<a id="export-range-full" class="editor-button toggle" href="#" data-i18n="clipboard.export.all"></a>'+
|
|
|
|
'</span>'+
|
|
|
|
'</div>'+
|
|
|
|
'<div class="form-row">'+
|
|
|
|
'<textarea readonly style="resize: none; width: 100%; border-radius: 4px;font-family: monospace; font-size: 12px; background:#f3f3f3; padding-left: 0.5em; box-sizing:border-box;" id="clipboard-export" rows="5"></textarea>'+
|
2015-05-26 22:52:23 +02:00
|
|
|
'</div>'+
|
2016-09-19 14:54:23 +02:00
|
|
|
'<div class="form-row" style="text-align: right;">'+
|
|
|
|
'<span id="export-format-group" class="button-group">'+
|
|
|
|
'<a id="export-format-mini" class="editor-button editor-button-small toggle" href="#" data-i18n="clipboard.export.compact"></a>'+
|
|
|
|
'<a id="export-format-full" class="editor-button editor-button-small toggle" href="#" data-i18n="clipboard.export.formatted"></a>'+
|
|
|
|
'</span>'+
|
2015-05-26 22:52:23 +02:00
|
|
|
'</div>';
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-05-26 22:52:23 +02:00
|
|
|
importNodesDialog = '<div class="form-row">'+
|
2015-07-01 00:42:03 +02:00
|
|
|
'<textarea style="resize: none; width: 100%; border-radius: 0px;font-family: monospace; font-size: 12px; background:#eee; padding-left: 0.5em; box-sizing:border-box;" id="clipboard-import" rows="5" placeholder="'+
|
|
|
|
RED._("clipboard.pasteNodes")+
|
|
|
|
'"></textarea>'+
|
2016-09-23 23:02:12 +02:00
|
|
|
'</div>'+
|
|
|
|
'<div class="form-row">'+
|
|
|
|
'<label style="width:auto;margin-right: 10px;" data-i18n="clipboard.import.import"></label>'+
|
|
|
|
'<span id="import-tab" class="button-group">'+
|
|
|
|
'<a id="import-tab-current" class="editor-button toggle selected" href="#" data-i18n="clipboard.export.current"></a>'+
|
|
|
|
'<a id="import-tab-new" class="editor-button toggle" href="#" data-i18n="clipboard.import.newFlow"></a>'+
|
|
|
|
'</span>'+
|
2015-05-26 22:52:23 +02:00
|
|
|
'</div>';
|
|
|
|
}
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-06-17 21:48:56 +02:00
|
|
|
function validateImport() {
|
|
|
|
var importInput = $("#clipboard-import");
|
|
|
|
var v = importInput.val();
|
2016-01-08 20:54:16 +01:00
|
|
|
v = v.substring(v.indexOf('['),v.lastIndexOf(']')+1);
|
2015-06-17 21:48:56 +02:00
|
|
|
try {
|
|
|
|
JSON.parse(v);
|
|
|
|
importInput.removeClass("input-error");
|
2016-01-08 20:54:16 +01:00
|
|
|
importInput.val(v);
|
2015-06-17 21:48:56 +02:00
|
|
|
$("#clipboard-dialog-ok").button("enable");
|
|
|
|
} catch(err) {
|
|
|
|
if (v !== "") {
|
|
|
|
importInput.addClass("input-error");
|
|
|
|
}
|
|
|
|
$("#clipboard-dialog-ok").button("disable");
|
|
|
|
}
|
|
|
|
}
|
2015-05-26 22:52:23 +02:00
|
|
|
|
2015-03-13 00:38:37 +01:00
|
|
|
function importNodes() {
|
2017-01-11 15:15:14 +01:00
|
|
|
if (disabled) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-13 00:38:37 +01:00
|
|
|
dialogContainer.empty();
|
|
|
|
dialogContainer.append($(importNodesDialog));
|
2016-09-23 23:02:12 +02:00
|
|
|
dialogContainer.i18n();
|
|
|
|
|
2015-03-13 00:38:37 +01:00
|
|
|
$("#clipboard-dialog-ok").show();
|
|
|
|
$("#clipboard-dialog-cancel").show();
|
|
|
|
$("#clipboard-dialog-close").hide();
|
2016-09-19 14:54:23 +02:00
|
|
|
$("#clipboard-dialog-copy").hide();
|
2015-03-13 00:38:37 +01:00
|
|
|
$("#clipboard-dialog-ok").button("disable");
|
2015-06-17 21:48:56 +02:00
|
|
|
$("#clipboard-import").keyup(validateImport);
|
|
|
|
$("#clipboard-import").on('paste',function() { setTimeout(validateImport,10)});
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2016-09-23 23:02:12 +02:00
|
|
|
$("#import-tab > a").click(function(evt) {
|
|
|
|
evt.preventDefault();
|
|
|
|
if ($(this).hasClass('disabled') || $(this).hasClass('selected')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$(this).parent().children().removeClass('selected');
|
|
|
|
$(this).addClass('selected');
|
|
|
|
});
|
|
|
|
|
2015-07-01 00:42:03 +02:00
|
|
|
dialog.dialog("option","title",RED._("clipboard.importNodes")).dialog("open");
|
2015-03-13 00:38:37 +01:00
|
|
|
}
|
2015-06-17 21:48:56 +02:00
|
|
|
|
2015-03-13 00:38:37 +01:00
|
|
|
function exportNodes() {
|
2017-01-11 15:15:14 +01:00
|
|
|
if (disabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-13 00:38:37 +01:00
|
|
|
dialogContainer.empty();
|
|
|
|
dialogContainer.append($(exportNodesDialog));
|
2016-09-19 14:54:23 +02:00
|
|
|
dialogContainer.i18n();
|
|
|
|
|
|
|
|
$("#export-format-group > a").click(function(evt) {
|
|
|
|
evt.preventDefault();
|
|
|
|
if ($(this).hasClass('disabled') || $(this).hasClass('selected')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$(this).parent().children().removeClass('selected');
|
|
|
|
$(this).addClass('selected');
|
|
|
|
|
|
|
|
var flow = $("#clipboard-export").val();
|
|
|
|
if (flow.length > 0) {
|
|
|
|
var nodes = JSON.parse(flow);
|
|
|
|
|
|
|
|
var format = $(this).attr('id');
|
|
|
|
if (format === 'export-format-full') {
|
|
|
|
flow = JSON.stringify(nodes,null,4);
|
|
|
|
} else {
|
|
|
|
flow = JSON.stringify(nodes);
|
|
|
|
}
|
|
|
|
$("#clipboard-export").val(flow);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$("#export-range-group > a").click(function(evt) {
|
|
|
|
evt.preventDefault();
|
|
|
|
if ($(this).hasClass('disabled') || $(this).hasClass('selected')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$(this).parent().children().removeClass('selected');
|
|
|
|
$(this).addClass('selected');
|
|
|
|
var type = $(this).attr('id');
|
|
|
|
var flow = "";
|
|
|
|
var nodes = null;
|
|
|
|
if (type === 'export-range-selected') {
|
|
|
|
var selection = RED.view.selection();
|
|
|
|
nodes = RED.nodes.createExportableNodeSet(selection.nodes);
|
|
|
|
} else if (type === 'export-range-flow') {
|
|
|
|
var activeWorkspace = RED.workspaces.active();
|
|
|
|
nodes = RED.nodes.filterNodes({z:activeWorkspace});
|
|
|
|
var parentNode = RED.nodes.workspace(activeWorkspace)||RED.nodes.subflow(activeWorkspace);
|
|
|
|
nodes.unshift(parentNode);
|
|
|
|
nodes = RED.nodes.createExportableNodeSet(nodes);
|
|
|
|
} else if (type === 'export-range-full') {
|
|
|
|
nodes = RED.nodes.createCompleteNodeSet(false);
|
|
|
|
}
|
|
|
|
if (nodes !== null) {
|
|
|
|
if (RED.settings.flowFilePretty) {
|
|
|
|
flow = JSON.stringify(nodes,null,4);
|
|
|
|
} else {
|
|
|
|
flow = JSON.stringify(nodes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (flow.length > 0) {
|
|
|
|
$("#export-copy").removeClass('disabled');
|
|
|
|
} else {
|
|
|
|
$("#export-copy").addClass('disabled');
|
|
|
|
}
|
|
|
|
$("#clipboard-export").val(flow);
|
|
|
|
})
|
|
|
|
|
2015-03-13 00:38:37 +01:00
|
|
|
$("#clipboard-dialog-ok").hide();
|
|
|
|
$("#clipboard-dialog-cancel").hide();
|
2016-09-19 14:54:23 +02:00
|
|
|
$("#clipboard-dialog-copy").hide();
|
|
|
|
$("#clipboard-dialog-close").hide();
|
2015-03-13 00:38:37 +01:00
|
|
|
var selection = RED.view.selection();
|
|
|
|
if (selection.nodes) {
|
2016-09-19 14:54:23 +02:00
|
|
|
$("#export-range-selected").click();
|
|
|
|
} else {
|
|
|
|
$("#export-range-selected").addClass('disabled').removeClass('selected');
|
|
|
|
$("#export-range-flow").click();
|
|
|
|
}
|
|
|
|
if (RED.settings.flowFilePretty) {
|
|
|
|
$("#export-format-full").click();
|
|
|
|
} else {
|
|
|
|
$("#export-format-mini").click();
|
|
|
|
}
|
|
|
|
$("#clipboard-export")
|
|
|
|
.focus(function() {
|
|
|
|
var textarea = $(this);
|
|
|
|
textarea.select();
|
|
|
|
textarea.mouseup(function() {
|
|
|
|
textarea.unbind("mouseup");
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
});
|
|
|
|
dialog.dialog("option","title",RED._("clipboard.exportNodes")).dialog( "open" );
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
$("#clipboard-export").focus();
|
|
|
|
if (!document.queryCommandEnabled("copy")) {
|
|
|
|
$("#clipboard-dialog-cancel").hide();
|
|
|
|
$("#clipboard-dialog-close").show();
|
2016-05-17 23:09:57 +02:00
|
|
|
} else {
|
2016-09-19 14:54:23 +02:00
|
|
|
$("#clipboard-dialog-cancel").show();
|
|
|
|
$("#clipboard-dialog-copy").show();
|
2016-05-17 23:09:57 +02:00
|
|
|
}
|
2016-09-19 14:54:23 +02:00
|
|
|
|
|
|
|
},0);
|
2015-03-13 00:38:37 +01:00
|
|
|
}
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-14 23:53:31 +01:00
|
|
|
function hideDropTarget() {
|
|
|
|
$("#dropTarget").hide();
|
2016-12-04 23:59:43 +01:00
|
|
|
RED.keyboard.remove("escape");
|
2015-03-14 23:53:31 +01:00
|
|
|
}
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-13 00:38:37 +01:00
|
|
|
return {
|
|
|
|
init: function() {
|
2015-05-26 22:52:23 +02:00
|
|
|
setupDialogs();
|
2015-07-10 20:49:31 +02:00
|
|
|
RED.events.on("view:selection-changed",function(selection) {
|
2015-03-13 00:38:37 +01:00
|
|
|
if (!selection.nodes) {
|
2015-04-13 17:48:38 +02:00
|
|
|
RED.menu.setDisabled("menu-item-export",true);
|
|
|
|
RED.menu.setDisabled("menu-item-export-clipboard",true);
|
|
|
|
RED.menu.setDisabled("menu-item-export-library",true);
|
2015-03-13 00:38:37 +01:00
|
|
|
} else {
|
2015-04-13 17:48:38 +02:00
|
|
|
RED.menu.setDisabled("menu-item-export",false);
|
|
|
|
RED.menu.setDisabled("menu-item-export-clipboard",false);
|
|
|
|
RED.menu.setDisabled("menu-item-export-library",false);
|
2015-03-13 00:38:37 +01:00
|
|
|
}
|
|
|
|
});
|
2016-12-04 23:59:43 +01:00
|
|
|
|
2017-01-11 12:35:48 +01:00
|
|
|
RED.actions.add("core:show-export-dialog",exportNodes);
|
|
|
|
RED.actions.add("core:show-import-dialog",importNodes);
|
2016-12-04 23:59:43 +01:00
|
|
|
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2017-01-11 15:15:14 +01:00
|
|
|
RED.events.on("editor:open",function() { disabled = true; });
|
|
|
|
RED.events.on("editor:close",function() { disabled = false; });
|
|
|
|
RED.events.on("search:open",function() { disabled = true; });
|
|
|
|
RED.events.on("search:close",function() { disabled = false; });
|
|
|
|
RED.events.on("type-search:open",function() { disabled = true; });
|
|
|
|
RED.events.on("type-search:close",function() { disabled = false; });
|
|
|
|
RED.events.on("palette-editor:open",function() { disabled = true; });
|
|
|
|
RED.events.on("palette-editor:close",function() { disabled = false; });
|
|
|
|
|
|
|
|
|
2015-03-14 23:53:31 +01:00
|
|
|
$('#chart').on("dragenter",function(event) {
|
|
|
|
if ($.inArray("text/plain",event.originalEvent.dataTransfer.types) != -1) {
|
|
|
|
$("#dropTarget").css({display:'table'});
|
2016-12-04 23:59:43 +01:00
|
|
|
RED.keyboard.add("*", "escape" ,hideDropTarget);
|
2015-03-14 23:53:31 +01:00
|
|
|
}
|
|
|
|
});
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-14 23:53:31 +01:00
|
|
|
$('#dropTarget').on("dragover",function(event) {
|
|
|
|
if ($.inArray("text/plain",event.originalEvent.dataTransfer.types) != -1) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.on("dragleave",function(event) {
|
|
|
|
hideDropTarget();
|
|
|
|
})
|
|
|
|
.on("drop",function(event) {
|
|
|
|
var data = event.originalEvent.dataTransfer.getData("text/plain");
|
|
|
|
hideDropTarget();
|
2016-01-08 20:54:16 +01:00
|
|
|
data = data.substring(data.indexOf('['),data.lastIndexOf(']')+1);
|
2015-03-14 23:53:31 +01:00
|
|
|
RED.view.importNodes(data);
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-13 00:38:37 +01:00
|
|
|
},
|
|
|
|
import: importNodes,
|
|
|
|
export: exportNodes
|
|
|
|
}
|
|
|
|
})();
|