/** * 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 = (function() { var dialog; var dialogBody; var activeProject; var screens = {}; function initScreens() { screens = { 'welcome': { content: function() { var container = $('
'); var buttons = $('').appendTo(container); var createNew = $('').appendTo(buttons); createNew.click(function(e) { e.preventDefault(); show('create'); }); var openExisting = $('').appendTo(buttons); openExisting.click(function(e) { e.preventDefault(); show('open') }); return container; }, buttons: [ ] }, 'create': (function() { var projectNameInput; var projectSummaryEditor; var projectSecretInput; var projectSecretSelect; var copyProject; var projectRepoInput; return { title: "Create a new project", // TODO: NLS content: function() { var container = $(''); var row; var validateForm = function() { var projectName = projectNameInput.val(); var valid = true; if (!/^[a-zA-Z0-9\-_]+$/.test(projectName)) { if (projectNameInputChanged) { projectNameInput.addClass("input-error"); } valid = false; } else { projectNameInput.removeClass("input-error"); } var projectType = $(".projects-dialog-screen-create-type.selected").data('type'); if (projectType === 'copy') { if (!copyProject) { valid = false; } } else if (projectType === 'clone') { var repo = projectRepoInput.val(); if (repo.trim() === '') { // TODO: could do more url regex checking... if (projectRepoChanged) { projectRepoInput.addClass("input-error"); } valid = false; } else { projectRepoInput.removeClass("input-error"); } } $("#projects-dialog-create").prop('disabled',!valid).toggleClass('disabled ui-button-disabled ui-state-disabled',!valid); } row = $('').appendTo(container); var createAsEmpty = $('').appendTo(row); var createAsCopy = $('').appendTo(row); var createAsClone = $('').appendTo(row); row.find(".projects-dialog-screen-create-type").click(function(evt) { evt.preventDefault(); $(".projects-dialog-screen-create-type").removeClass('selected'); $(this).addClass('selected'); $(".projects-dialog-screen-create-row").hide(); $(".projects-dialog-screen-create-row-"+$(this).data('type')).show(); validateForm(); }) row = $('').appendTo(container); $('').appendTo(row); projectNameInput = $('').appendTo(row); var projectNameInputChanged = false; projectNameInput.on("change keyup paste",function() { validateForm(); }); // Empty Project row = $('').appendTo(container); $('').appendTo(row); projectSummaryEditor = $('').appendTo(row); // Copy Project row = $('').appendTo(container); $('').appendTo(row); var autoInsertedName = ""; createProjectList({ height: "250px", small: true, select: function(project) { copyProject = project; var projectName = projectNameInput.val(); if (projectName === "" || projectName === autoInsertedName) { autoInsertedName = project.name+"-copy"; projectNameInput.val(autoInsertedName); } validateForm(); } }).appendTo(row); // Clone Project row = $('').appendTo(container); $('').appendTo(row); projectRepoInput = $('').appendTo(row); var projectRepoChanged = false; projectRepoInput.on("change keyup paste",function() { var repo = $(this).val(); var m = /\/([^/]+)\.git/.exec(repo); if (m) { var projectName = projectNameInput.val(); if (projectName === "" || projectName === autoInsertedName) { autoInsertedName = m[1]; projectNameInput.val(autoInsertedName); } } validateForm(); }); // Secret - empty/clone row = $('').appendTo(container); $('').appendTo(row); projectSecretInput = $('').appendTo(row); return container; }, buttons: [ { // id: "clipboard-dialog-cancel", text: RED._("common.label.cancel"), click: function() { $( this ).dialog( "close" ); } }, { id: "projects-dialog-create", text: "Create project", // TODO: nls class: "primary disabled", disabled: true, click: function() { var projectType = $(".projects-dialog-screen-create-type.selected").data('type'); var projectData = { name: projectNameInput.val(), } if (projectType === 'empty') { projectData.summary = projectSummaryEditor.val(); projectData.credentialSecret = projectSecretInput.val(); } else if (projectType === 'copy') { projectData.copy = copyProject.name; } else if (projectType === 'clone') { projectData.credentialSecret = projectSecretInput.val(); projectData.remote = { url: projectRepoInput.val() } } sendRequest({ url: "projects", type: "POST", responses: { 200: function(data) { switchProject(projectData.name,function(err,data) { if (err) { console.log("unexpected_error",error) } else { dialog.dialog( "close" ); } }) }, 400: { 'project_exists': function(error) { console.log("already exists"); }, 'git_error': function(error) { console.log("git error",error); }, 'git_auth_failed': function(error) { // getRepoAuthDetails(req); console.log("git auth error",error); }, 'unexpected_error': function(error) { console.log("unexpected_error",error) } } } },projectData) // if (projectType === 'empty') { // show('credentialSecret'); // } else if (projectType === 'copy') { // show('copy'); // } else if (projectType === 'clone') { // show('clone'); // } // var projectName = projectNameInput.val().trim(); // var projectRepoEnabled = projectRepoEnabledInput.prop('checked'); // var projectRepo = projectRepoInput.val().trim(); // if (projectName !== '') { // var req = { // name:projectName // }; // if (projectRepoEnabled && projectRepo !== '') { // req.remote = projectRepo; // } // console.log(req); // sendRequest({ // url: "projects", // type: "POST", // responses: { // 200: function(data) { // console.log("Success!",data); // }, // 400: { // 'project_exists': function(error) { // console.log("already exists"); // }, // 'git_error': function(error) { // console.log("git error",error); // }, // 'git_auth_failed': function(error) { // // getRepoAuthDetails(req); // console.log("git auth error",error); // }, // 'unexpected_error': function(error) { // console.log("unexpected_error",error) // } // } // } // },req) // } // $( this ).dialog( "close" ); } } ] } })(), 'open': { content: function() { return createProjectList({ canSelectActive: false, dblclick: function() { $("#projects-dialog-open").click(); } }) }, buttons: [ { // id: "clipboard-dialog-cancel", text: RED._("common.label.cancel"), click: function() { $( this ).dialog( "close" ); } }, { id: "projects-dialog-open", text: "Open project", // TODO: nls class: "primary disabled", disabled: true, click: function() { switchProject(selectedProject.name,function(err,data) { if (err) { if (err.error === 'credentials_load_failed') { dialog.dialog( "close" ); } else { console.log("unexpected_error",err) } } else { dialog.dialog( "close" ); } }) } } ] } } } function switchProject(name,done) { RED.deploy.setDeployInflight(true); RED.projects.settings.switchProject(name); sendRequest({ url: "projects/"+name, type: "PUT", responses: { 200: function(data) { done(null,data); }, 400: { 'credentials_load_failed': function(error) { done(error,null); }, 'unexpected_error': function(error) { done(error,null); } }, } },{active:true}).always(function() { RED.deploy.setDeployInflight(false); }) } function show(s,options) { if (!dialog) { RED.projects.init(); } var screen = screens[s]; var container = screen.content(); dialogBody.empty(); dialog.dialog('option','buttons',screen.buttons); dialogBody.append(container); dialog.dialog('option','title',screen.title||""); dialog.dialog("open"); dialog.dialog({position: { 'my': 'center', 'at': 'center', 'of': window }}); } var selectedProject = null; function createProjectList(options) { options = options||{}; var height = options.height || "300px"; selectedProject = null; var container = $('',{style:"min-height: "+height+"; height: "+height+";"}); var list = $('