2015-03-13 00:38:37 +01:00
|
|
|
/**
|
|
|
|
* 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.clipboard = (function() {
|
2015-05-26 22:52:23 +02:00
|
|
|
|
|
|
|
var dialog;
|
|
|
|
var dialogContainer;
|
|
|
|
var exportNodesDialog;
|
|
|
|
var importNodesDialog;
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-05-26 22:52:23 +02:00
|
|
|
function setupDialogs(){
|
|
|
|
dialog = $('<div id="clipboard-dialog" class="hide"><form class="dialog-form form-horizontal"></form></div>')
|
|
|
|
.appendTo("body")
|
|
|
|
.dialog({
|
|
|
|
modal: true,
|
|
|
|
autoOpen: false,
|
|
|
|
width: 500,
|
|
|
|
resizable: false,
|
|
|
|
buttons: [
|
|
|
|
{
|
|
|
|
id: "clipboard-dialog-ok",
|
2015-07-01 00:42:03 +02:00
|
|
|
text: RED._("common.label.ok"),
|
2015-05-26 22:52:23 +02:00
|
|
|
click: function() {
|
|
|
|
if (/Import/.test(dialog.dialog("option","title"))) {
|
|
|
|
RED.view.importNodes($("#clipboard-import").val());
|
|
|
|
}
|
|
|
|
$( this ).dialog( "close" );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "clipboard-dialog-cancel",
|
2015-07-01 00:42:03 +02:00
|
|
|
text: RED._("common.label.cancel"),
|
2015-05-26 22:52:23 +02:00
|
|
|
click: function() {
|
|
|
|
$( this ).dialog( "close" );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "clipboard-dialog-close",
|
2015-07-01 00:42:03 +02:00
|
|
|
text: RED._("common.label.close"),
|
2015-05-26 22:52:23 +02:00
|
|
|
click: function() {
|
|
|
|
$( 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();
|
|
|
|
RED.keyboard.disable();
|
2015-03-13 00:38:37 +01:00
|
|
|
},
|
2015-05-26 22:52:23 +02:00
|
|
|
close: function(e) {
|
|
|
|
RED.keyboard.enable();
|
2015-03-13 00:38:37 +01:00
|
|
|
}
|
2015-05-26 22:52:23 +02: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
|
|
|
|
2015-05-26 22:52:23 +02:00
|
|
|
exportNodesDialog = '<div class="form-row">'+
|
2015-07-01 00:42:03 +02:00
|
|
|
'<label for="node-input-export" style="display: block; width:100%;"><i class="fa fa-clipboard"></i> '+RED._("clipboard.nodes")+'</label>'+
|
2015-05-26 22:52:23 +02:00
|
|
|
'<textarea readonly 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-export" rows="5"></textarea>'+
|
|
|
|
'</div>'+
|
|
|
|
'<div class="form-tips">'+
|
2015-07-01 00:42:03 +02:00
|
|
|
RED._("clipboard.selectNodes")+
|
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>'+
|
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();
|
|
|
|
try {
|
|
|
|
JSON.parse(v);
|
|
|
|
importInput.removeClass("input-error");
|
|
|
|
$("#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() {
|
|
|
|
dialogContainer.empty();
|
|
|
|
dialogContainer.append($(importNodesDialog));
|
|
|
|
$("#clipboard-dialog-ok").show();
|
|
|
|
$("#clipboard-dialog-cancel").show();
|
|
|
|
$("#clipboard-dialog-close").hide();
|
|
|
|
$("#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
|
|
|
|
|
|
|
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() {
|
|
|
|
dialogContainer.empty();
|
|
|
|
dialogContainer.append($(exportNodesDialog));
|
|
|
|
$("#clipboard-dialog-ok").hide();
|
|
|
|
$("#clipboard-dialog-cancel").hide();
|
|
|
|
$("#clipboard-dialog-close").show();
|
|
|
|
var selection = RED.view.selection();
|
|
|
|
if (selection.nodes) {
|
|
|
|
var nns = RED.nodes.createExportableNodeSet(selection.nodes);
|
|
|
|
$("#clipboard-export")
|
|
|
|
.val(JSON.stringify(nns))
|
|
|
|
.focus(function() {
|
|
|
|
var textarea = $(this);
|
|
|
|
textarea.select();
|
|
|
|
textarea.mouseup(function() {
|
|
|
|
textarea.unbind("mouseup");
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
});
|
2015-07-01 00:42:03 +02:00
|
|
|
dialog.dialog("option","title",RED._("clipboard.exportNodes")).dialog( "open" );
|
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();
|
|
|
|
RED.keyboard.remove(/* ESCAPE */ 27);
|
|
|
|
}
|
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-03-13 00:38:37 +01:00
|
|
|
RED.view.on("selection-changed",function(selection) {
|
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
RED.keyboard.add(/* e */ 69,{ctrl:true},function(){exportNodes();d3.event.preventDefault();});
|
|
|
|
RED.keyboard.add(/* i */ 73,{ctrl:true},function(){importNodes();d3.event.preventDefault();});
|
2015-07-01 00:42:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
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'});
|
|
|
|
RED.keyboard.add(/* ESCAPE */ 27,hideDropTarget);
|
|
|
|
}
|
|
|
|
});
|
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();
|
|
|
|
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
|
|
|
|
}
|
2015-07-01 00:42:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-03-13 00:38:37 +01:00
|
|
|
})();
|