|
| 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 path, newProjectExtension, recentProjectExtension, Strings, Metrics*/ |
| 22 | +/*eslint no-console: 0*/ |
| 23 | +/*eslint strict: ["error", "global"]*/ |
| 24 | +/* jshint ignore:start */ |
| 25 | + |
| 26 | +function _createRecentProjectCard(projectName, fullPath, nodeId, tabIndex) { |
| 27 | + let removeBtnDisableStyle = ""; |
| 28 | + if(path.normalize(fullPath) === path.normalize(newProjectExtension.getWelcomeProjectPath())){ |
| 29 | + removeBtnDisableStyle = "display: none;"; |
| 30 | + } |
| 31 | + return $(`<li> |
| 32 | + <a id="${nodeId}" href="#" |
| 33 | + class="d-flex align-items-center justify-content-between tabable" |
| 34 | + tabindex="${tabIndex}" |
| 35 | + onclick="openProject('${fullPath}');_recentProjectMetric('open');"> |
| 36 | + <div class="project-name"> |
| 37 | + ${projectName} |
| 38 | + </div> |
| 39 | + <button class="remove-btn" onclick="removeProject('${fullPath}');_recentProjectMetric('remove');" |
| 40 | + style="${removeBtnDisableStyle}"> |
| 41 | + <svg width="16" height="16" viewBox="0 0 14 14" fill="none" |
| 42 | + xmlns="http://www.w3.org/2000/svg"> |
| 43 | + <path d="M1.75 3.5H2.91667H12.25" stroke="#D0D0D0" stroke-linecap="round" |
| 44 | + stroke-linejoin="round"/> |
| 45 | + <path d="M4.6665 3.50008V2.33341C4.6665 2.024 4.78942 1.72725 5.00821 1.50846C5.22701 1.28966 5.52375 1.16675 5.83317 1.16675H8.1665C8.47592 1.16675 8.77267 1.28966 8.99146 1.50846C9.21026 1.72725 9.33317 2.024 9.33317 2.33341V3.50008M11.0832 3.50008V11.6667C11.0832 11.9762 10.9603 12.2729 10.7415 12.4917C10.5227 12.7105 10.2259 12.8334 9.91651 12.8334H4.08317C3.77375 12.8334 3.47701 12.7105 3.25821 12.4917C3.03942 12.2729 2.9165 11.9762 2.9165 11.6667V3.50008H11.0832Z" |
| 46 | + stroke="#D0D0D0" stroke-linecap="round" stroke-linejoin="round"/> |
| 47 | + <path d="M5.8335 6.41675V9.91675" stroke="#D0D0D0" stroke-linecap="round" |
| 48 | + stroke-linejoin="round"/> |
| 49 | + <path d="M8.1665 6.41675V9.91675" stroke="#D0D0D0" stroke-linecap="round" |
| 50 | + stroke-linejoin="round"/> |
| 51 | + </svg> |
| 52 | + </button> |
| 53 | + </a> |
| 54 | + </li>`); |
| 55 | +} |
| 56 | + |
| 57 | +function _recentProjectMetric(type) { |
| 58 | + Metrics.countEvent(Metrics.EVENT_TYPE.NEW_PROJECT, "recentProject.btnClick", type); |
| 59 | +} |
| 60 | + |
| 61 | +function getDisplayName(projectPath) { |
| 62 | + const prefixRemove = [newProjectExtension.getLocalProjectsPath(), newProjectExtension.getMountDir()]; |
| 63 | + for(let prefix of prefixRemove){ |
| 64 | + if(projectPath.startsWith(prefix)){ |
| 65 | + return projectPath.replace(prefix, ''); |
| 66 | + } |
| 67 | + } |
| 68 | + return projectPath; |
| 69 | +} |
| 70 | + |
| 71 | +function _updateProjectCards() { |
| 72 | + let recentProjectList = $(document.getElementById('recentProjectList')); |
| 73 | + recentProjectList.empty(); |
| 74 | + let recentProjects = recentProjectExtension.getRecentProjects(); |
| 75 | + let tabIndex = 20; |
| 76 | + for(let recentProject of recentProjects){ |
| 77 | + recentProjectList.append(_createRecentProjectCard(getDisplayName(recentProject), |
| 78 | + recentProject, `recent-prj-list-${tabIndex}`, tabIndex++)); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +function openProject(fullPath) { |
| 83 | + recentProjectExtension.openProjectWithPath(fullPath) |
| 84 | + .then(()=>{ |
| 85 | + newProjectExtension.closeDialogue(); |
| 86 | + }) |
| 87 | + .catch(()=>{ |
| 88 | + _updateProjectCards(); |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +function removeProject(fullPath) { |
| 93 | + recentProjectExtension.removeFromRecentProject(fullPath); |
| 94 | + _updateProjectCards(); |
| 95 | + event.stopPropagation(); |
| 96 | +} |
| 97 | + |
| 98 | +function getPhoenixAbsURL(relativePath) { |
| 99 | + return `${window.parent.Phoenix.baseURL}${relativePath}`; |
| 100 | +} |
| 101 | + |
| 102 | +function newProject(url, suggestedProjectName, title, license, licenseURL, credits, creditsURL) { |
| 103 | + let href = `new-project-from-url.html?url=${url}&suggestedName=${suggestedProjectName}&title=${title}`; |
| 104 | + if(license){ |
| 105 | + href=`${href}&license=${license}`; |
| 106 | + } |
| 107 | + if(licenseURL){ |
| 108 | + href=`${href}&licenseURL=${licenseURL}`; |
| 109 | + } |
| 110 | + if(credits){ |
| 111 | + href=`${href}&credits=${credits}`; |
| 112 | + } |
| 113 | + if(creditsURL){ |
| 114 | + href=`${href}&creditsURL=${creditsURL}`; |
| 115 | + } |
| 116 | + window.location.href = href; |
| 117 | +} |
| 118 | + |
| 119 | +function initCodeEditor() { |
| 120 | + document.getElementById("openFolderBtn").onclick = function() { |
| 121 | + newProjectExtension.openFolder(); |
| 122 | + Metrics.countEvent(Metrics.EVENT_TYPE.NEW_PROJECT, "mainPage.btnClick", "open-folder"); |
| 123 | + }; |
| 124 | + document.getElementById("newGitHubProject").onclick = function() { |
| 125 | + window.location.href = 'new-project-github.html'; |
| 126 | + Metrics.countEvent(Metrics.EVENT_TYPE.NEW_PROJECT, "mainPage.btnClick", "github-project"); |
| 127 | + }; |
| 128 | + document.getElementById("exploreBtn").onclick = function() { |
| 129 | + openProject(newProjectExtension.getExploreProjectPath()); |
| 130 | + Metrics.countEvent(Metrics.EVENT_TYPE.NEW_PROJECT, "mainPage.btnClick", "explore"); |
| 131 | + }; |
| 132 | + document.getElementById("newBootstrapBlogBtn").onclick = function() { |
| 133 | + newProject(getPhoenixAbsURL("assets/sample-projects/bootstrap-blog.zip"), |
| 134 | + "bootstrap-blog", Strings.NEW_BOOTSTRAP_BLOG, |
| 135 | + "MIT", "https://github.com/twbs/bootstrap/blob/main/LICENSE", |
| 136 | + "https://getbootstrap.com", "https://getbootstrap.com"); |
| 137 | + Metrics.countEvent(Metrics.EVENT_TYPE.NEW_PROJECT, "mainPage.btnClick", "bootstrap-blog"); |
| 138 | + }; |
| 139 | + document.getElementById("newHTMLBtn").onclick = function() { |
| 140 | + newProject(getPhoenixAbsURL("assets/sample-projects/HTML5.zip"), |
| 141 | + "html project", Strings.NEW_HTML); |
| 142 | + Metrics.countEvent(Metrics.EVENT_TYPE.NEW_PROJECT, "mainPage.btnClick", "html5"); |
| 143 | + }; |
| 144 | + _updateProjectCards(); |
| 145 | +} |
0 commit comments