/** * 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.projects.userSettings = (function() { var gitconfigContainer; var gitUsernameInput; var gitEmailInput; function createRemoteRepositorySection(pane) { var currentGitSettings = RED.settings.get('git') || {}; currentGitSettings.user = currentGitSettings.user || {}; var title = $('

').text("Committer Details").appendTo(pane); gitconfigContainer = $('
').appendTo(pane); $('
').appendTo(gitconfigContainer).text("Leave blank to use system default"); var row = $('
').appendTo(gitconfigContainer); $('').text('Username').appendTo(row); gitUsernameInput = $('').appendTo(row); gitUsernameInput.val(currentGitSettings.user.name||""); row = $('
').appendTo(gitconfigContainer); $('').text('Email').appendTo(row); gitEmailInput = $('').appendTo(row); gitEmailInput.val(currentGitSettings.user.email||""); var sshkeyTitle = $('

').text("SSH Keys").appendTo(gitconfigContainer); var editSshKeyListButton = $('') .appendTo(sshkeyTitle) .click(function(evt) { editSshKeyListButton.hide(); formButtons.show(); sshkeyInputRow.show(); $(".projects-dialog-sshkey-list-button-remove").css('display', 'inline-block'); }); var sshkeyListOptions = { height: "300px", deleteAction: function(entry, header) { var spinner = utils.addSpinnerOverlay(header).addClass('projects-dialog-spinner-contain'); var notification = RED.notify("Are you sure you want to delete the SSH Keys '"+entry.name+"'? This cannot be undone.", { type: 'warning', modal: true, fixed: true, buttons: [ { text: RED._("common.label.cancel"), click: function() { spinner.remove(); notification.close(); } }, { text: "Delete SSH Keys", click: function() { notification.close(); sendSSHKeyManagementAPI("DELETE_KEY", entry.name, null, function(data) { spinner.remove(); hideSSHKeyGenerateForm(); utils.refreshSSHKeyList(sshkeysList); }, function(err) { spinner.remove(); console.log('Delete error! error:', err); notification = RED.notify("Failed to delete the SSH Keys '"+entry.name+"'.", { type: "error", modal: true, fixed: false }); }); } } ] }); }, selectAction: function(entry, header) { var spinner = utils.addSpinnerOverlay(header).addClass('projects-dialog-spinner-contain'); sendSSHKeyManagementAPI("GET_KEY_DETAIL", entry.name, null, function(data) { spinner.remove(); setDialogContext(entry.name, data.publickey); dialog.dialog("open"); }, function(err) { console.log('Get SSH Key detail error! error:', err); spinner.remove(); notification = RED.notify("Failed to get the SSH Key detail '"+entry.name+"'.", { type: "error", modal: true, fixed: false }); }); } }; var sshkeysListRow = $('
').appendTo(gitconfigContainer); var sshkeysList = utils.createSSHKeyList(sshkeyListOptions).appendTo(sshkeysListRow); var sshkeyInputRow = $('
').hide().appendTo(gitconfigContainer); var sshkeyNameLabel = $('').text('Key Name').appendTo(sshkeyInputRow); var sshkeyNameInput = $('').appendTo(sshkeyInputRow); var sshkeyPassphraseLabel = $('').text('Passphrase').appendTo(sshkeyInputRow); var sshkeyPassphraseInput = $('').appendTo(sshkeyInputRow); var sshkeySamePassphraseLabel = $('').text('Same Passphrase').appendTo(sshkeyInputRow); var sshkeySamePassphraseInput = $('').appendTo(sshkeyInputRow); var formButtonArea = $('
').appendTo(gitconfigContainer); var formButtons = $('') .hide().appendTo(formButtonArea); function hideSSHKeyGenerateForm() { editSshKeyListButton.show(); formButtons.hide(); sshkeyInputRow.hide(); sshkeyNameInput.val(""); sshkeyPassphraseInput.val(""); sshkeySamePassphraseInput.val(""); if ( sshkeyNameInput.hasClass('input-error') ) { sshkeyNameInput.removeClass('input-error'); } if ( sshkeyPassphraseInput.hasClass('input-error') ) { sshkeyPassphraseInput.removeClass('input-error'); } if ( sshkeySamePassphraseInput.hasClass('input-error') ) { sshkeySamePassphraseInput.removeClass('input-error'); } $(".projects-dialog-sshkey-list-button-remove").hide(); } $('') .appendTo(formButtons) .click(function(evt) { evt.preventDefault(); hideSSHKeyGenerateForm(); }); var generateButton = $('') .appendTo(formButtons) .click(function(evt) { evt.preventDefault(); if ( sshkeyNameInput.hasClass('input-error') ) { sshkeyNameInput.removeClass('input-error'); } if ( sshkeyPassphraseInput.hasClass('input-error') ) { sshkeyPassphraseInput.removeClass('input-error'); } if ( sshkeySamePassphraseInput.hasClass('input-error') ) { sshkeySamePassphraseInput.removeClass('input-error'); } var valid = true; if ( sshkeyNameInput.val() === "" ) { sshkeyNameInput.addClass('input-error'); valid = false; } if ( sshkeyPassphraseInput.val() !== sshkeySamePassphraseInput.val() ) { sshkeySamePassphraseInput.addClass('input-error'); valid = false; } if ( valid ) { sendSSHKeyManagementAPI("GENERATE_KEY", { name: sshkeyNameInput.val(), email: gitEmailInput.val(), password: sshkeyPassphraseInput.val(), size: 4096 }, gitconfigContainer, function() { hideSSHKeyGenerateForm(); utils.refreshSSHKeyList(sshkeysList); }, function(err) { console.log('err message:', err.message); if ( err.message.includes('Some SSH Keyfile exists') ) { sshkeyNameInput.addClass('input-error'); } else if ( err.message.includes('Failed to generate ssh key files') ) { sshkeyPassphraseInput.addClass('input-error'); sshkeySamePassphraseInput.addClass('input-error'); } } ); } }); } function sendSSHKeyManagementAPI(type, param, overlay, successCallback, failCallback) { var url; var method; var payload; switch(type) { case 'GET_KEY_LIST': method = 'GET'; url = "settings/user/keys"; break; case 'GET_KEY_DETAIL': method = 'GET'; url = "settings/user/keys/" + param; break; case 'GENERATE_KEY': method = 'POST'; url = "settings/user/keys"; payload= param; break; case 'DELETE_KEY': method = 'DELETE'; url = "settings/user/keys/" + param; break; default: console.error('Unexpected type....'); return; } // var spinner = utils.addSpinnerOverlay(gitconfigContainer); var spinner = overlay ? utils.addSpinnerOverlay(overlay) : null; var done = function(err) { if ( spinner ) { spinner.remove(); } if (err) { console.log(err); return; } }; console.log('method:', method); console.log('url:', url); utils.sendRequest({ url: url, type: method, responses: { 0: function(error) { if ( failCallback ) { failCallback(error); } done(error); }, 200: function(data) { if ( successCallback ) { successCallback(data); } done(); }, 400: { 'unexpected_error': function(error) { console.log(error); if ( failCallback ) { failCallback(error); } done(error); } }, } },payload); } var dialog; var dialogBody; function createPublicKeyDialog() { dialog = $('
') .appendTo("body") .dialog({ modal: true, autoOpen: false, width: 600, resize: false, open: function(e) { $(this).parent().find(".ui-dialog-titlebar-close").hide(); }, close: function(e) { } }); dialogBody = dialog.find("form"); dialog.dialog('option', 'title', 'SSH public key'); dialog.dialog('option', 'buttons', [ { text: RED._("common.label.close"), click: function() { $( this ).dialog( "close" ); } }, { text: "Copy to Clipboard", click: function() { var target = document.getElementById('public-key-data'); document.getSelection().selectAllChildren(target); var ret = document.execCommand('copy'); var msg = ret ? 'successful' : 'unsuccessful'; console.log('Copy text command was ' + msg); $( this ).dialog("close"); } } ]); dialog.dialog({position: { 'my': 'center', 'at': 'center', 'of': window }}); var container = $('
'); $('
').appendTo(container); $('
').appendTo(container); dialogBody.append(container); } function setDialogContext(name, data) { var title = dialog.find("div.projects-dialog-ssh-public-key-name"); title.text(name); var context = dialog.find("div.projects-dialog-ssh-public-key>pre"); context.text(data); } function createSettingsPane(activeProject) { var pane = $('
'); createRemoteRepositorySection(pane); createPublicKeyDialog(); return pane; } var utils; function init(_utils) { utils = _utils; RED.userSettings.add({ id:'gitconfig', title: "Git config", // TODO: nls get: createSettingsPane, close: function() { var currentGitSettings = RED.settings.get('git') || {}; currentGitSettings.user = currentGitSettings.user || {}; currentGitSettings.user.name = gitUsernameInput.val(); currentGitSettings.user.email = gitEmailInput.val(); RED.settings.set('git', currentGitSettings); } }); } return { init: init, }; })();