|
| 1 | +/* |
| 2 | + * GNU AGPL-3.0 License |
| 3 | + * |
| 4 | + * Copyright (c) 2021 - present core.ai . All rights reserved. |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU Affero General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License |
| 14 | + * for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see https://opensource.org/licenses/AGPL-3.0. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +/*global newProjectExtension, Strings, Metrics*/ |
| 22 | +/*eslint no-console: 0*/ |
| 23 | +/*eslint strict: ["error", "global"]*/ |
| 24 | +/* jshint ignore:start */ |
| 25 | + |
| 26 | +function desktopInit() { |
| 27 | + const LAST_GIT_CLONE_BASE_DIR = "PH_LAST_GIT_CLONE_BASE_DIR"; |
| 28 | + let createProjectBtn, websiteURLInput, locationInput; |
| 29 | + |
| 30 | + function _validateGitURL(errors) { |
| 31 | + let gitURL = websiteURLInput.value; |
| 32 | + if(gitURL){ |
| 33 | + $(websiteURLInput).removeClass("error-border"); |
| 34 | + return true; |
| 35 | + } |
| 36 | + $(websiteURLInput).addClass("error-border"); |
| 37 | + errors.push(`<span><i class="fas fa-exclamation-triangle" style="color: #f89406"></i> ${Strings.ERROR_GIT_URL_INVALID}</span>`); |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + function _validateProjectLocation(errors) { |
| 42 | + let location = locationInput.value; |
| 43 | + if( location === Strings.PLEASE_SELECT_A_FOLDER){ |
| 44 | + $(locationInput).addClass("error-border"); |
| 45 | + return false; |
| 46 | + } |
| 47 | + if(locationInput.error){ |
| 48 | + errors.push(`<span><i class="fas fa-exclamation-triangle" style="color: #f89406"></i> ${locationInput.error}</span>`); |
| 49 | + $(locationInput).addClass("error-border"); |
| 50 | + return false; |
| 51 | + } |
| 52 | + $(locationInput).removeClass("error-border"); |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + function _validate() { |
| 57 | + const errors = []; |
| 58 | + let isValid = _validateGitURL(errors); |
| 59 | + isValid = _validateProjectLocation(errors) && isValid; |
| 60 | + $(createProjectBtn).prop('disabled', !isValid); |
| 61 | + const $messageDisplay = $("#messageDisplay"); |
| 62 | + $messageDisplay.html(""); |
| 63 | + if(!isValid) { |
| 64 | + $messageDisplay.html(errors.join("<br>")); |
| 65 | + } |
| 66 | + return isValid; |
| 67 | + } |
| 68 | + |
| 69 | + async function _deduceClonePath(newPath) { |
| 70 | + if(!newPath){ |
| 71 | + newPath = locationInput.originalPath; |
| 72 | + } |
| 73 | + if(!newPath){ |
| 74 | + return; |
| 75 | + } |
| 76 | + const {clonePath, error} = await newProjectExtension.getGitCloneDir(newPath, websiteURLInput.value); |
| 77 | + locationInput.fullPath = clonePath; |
| 78 | + locationInput.value = window.top.Phoenix.fs.getTauriPlatformPath(clonePath); |
| 79 | + locationInput.error = error; |
| 80 | + locationInput.originalPath = newPath; |
| 81 | + } |
| 82 | + |
| 83 | + function _selectFolder() { |
| 84 | + newProjectExtension.showFolderSelect(locationInput.originalPath || "") |
| 85 | + .then((newPath)=>{ |
| 86 | + _deduceClonePath(newPath).then(_validate); |
| 87 | + }).catch((err)=>{ |
| 88 | + console.error("user cancelled or error", err); |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + function _createProjectClicked() { |
| 93 | + localStorage.setItem(LAST_GIT_CLONE_BASE_DIR, locationInput.originalPath); |
| 94 | + //newProjectExtension.gitClone(websiteURLInput.value, locationInput.value); |
| 95 | + Metrics.countEvent(Metrics.EVENT_TYPE.NEW_PROJECT, "git.Click", "create"); |
| 96 | + newProjectExtension.closeDialogue(); |
| 97 | + } |
| 98 | + |
| 99 | + function initGitProject() { |
| 100 | + $(".label-clone").text(Strings.GIT_CLONE_URL); |
| 101 | + createProjectBtn = document.getElementById("createProjectBtn"); |
| 102 | + websiteURLInput = document.getElementById("websiteURLInput"); |
| 103 | + locationInput = document.getElementById("locationInput"); |
| 104 | + createProjectBtn.onclick = _createProjectClicked; |
| 105 | + $(websiteURLInput).keyup(()=>{ |
| 106 | + _deduceClonePath().then(_validate); |
| 107 | + }); |
| 108 | + locationInput.value = Strings.PLEASE_SELECT_A_FOLDER; |
| 109 | + locationInput.onclick = _selectFolder; |
| 110 | + websiteURLInput.value = "https://github.com/phcode-dev/HTML-Starter-Templates.git"; |
| 111 | + _deduceClonePath(localStorage.getItem(LAST_GIT_CLONE_BASE_DIR)).then(_validate); |
| 112 | + } |
| 113 | + window.initGitProject = initGitProject; |
| 114 | +} |
| 115 | + |
| 116 | +if(window.top.Phoenix.isNativeApp){ |
| 117 | + desktopInit(); |
| 118 | +} |
0 commit comments