|
23 | 23 | /*eslint strict: ["error", "global"]*/ |
24 | 24 | /* jshint ignore:start */ |
25 | 25 |
|
26 | | -let createProjectBtn, websiteURLInput, locationInput; |
27 | | -const FLATTEN_ZIP_FIRST_LEVEL_DIR = true; |
| 26 | +function browserInit() { |
| 27 | + let createProjectBtn, websiteURLInput, locationInput; |
| 28 | + const FLATTEN_ZIP_FIRST_LEVEL_DIR = true; |
28 | 29 |
|
29 | | -function _isValidGitHubURL(url) { |
30 | | - // strip trailing slash |
31 | | - url = url.replace(/\/$/, ""); |
32 | | - let githubPrefix = "https://github.com/"; |
33 | | - let components = url.replace("https://github.com/", '').split('/'); |
34 | | - if(!url.startsWith(githubPrefix) || components.length !== 2){ |
35 | | - return false; |
| 30 | + function _isValidGitHubURL(url) { |
| 31 | + // strip trailing slash |
| 32 | + url = url.replace(/\/$/, ""); |
| 33 | + let githubPrefix = "https://github.com/"; |
| 34 | + let components = url.replace("https://github.com/", '').split('/'); |
| 35 | + if(!url.startsWith(githubPrefix) || components.length !== 2){ |
| 36 | + return false; |
| 37 | + } |
| 38 | + return true; |
36 | 39 | } |
37 | | - return true; |
38 | | -} |
39 | 40 |
|
40 | | -function _fixGitHubBrokenURL() { |
41 | | - let githubPrefix = "https://github.com/", |
42 | | - gitSuffix = '.git'; |
43 | | - let githubURL = websiteURLInput.value; |
44 | | - if(githubURL.startsWith("http:")){ |
45 | | - githubURL = githubURL.replace("http:", "https:"); |
46 | | - } |
47 | | - if(!githubURL.startsWith(githubPrefix)){ |
48 | | - return; |
49 | | - } |
50 | | - // strip any query string params if present |
51 | | - let queryParamTrimIndex = githubURL.indexOf('?') >= 0 ? githubURL.indexOf('?') : githubURL.length; |
52 | | - githubURL = githubURL.substring(0, queryParamTrimIndex); |
53 | | - // trim everything after https://github.com/orgname/repo/... to https://github.com/orgname/repo |
54 | | - let components = githubURL.replace("https://github.com/", '').split('/'); |
55 | | - // trim .git at the end of the name |
56 | | - if(githubURL.endsWith(gitSuffix)){ |
57 | | - githubURL = githubURL.replace(new RegExp(gitSuffix + '$'), ''); |
58 | | - } |
59 | | - if(components.length > 2){ |
60 | | - githubURL = `https://github.com/${components[0]}/${components[1]}`; |
| 41 | + function _fixGitHubBrokenURL() { |
| 42 | + let githubPrefix = "https://github.com/", |
| 43 | + gitSuffix = '.git'; |
| 44 | + let githubURL = websiteURLInput.value; |
| 45 | + if(githubURL.startsWith("http:")){ |
| 46 | + githubURL = githubURL.replace("http:", "https:"); |
| 47 | + } |
| 48 | + if(!githubURL.startsWith(githubPrefix)){ |
| 49 | + return; |
| 50 | + } |
| 51 | + // strip any query string params if present |
| 52 | + let queryParamTrimIndex = githubURL.indexOf('?') >= 0 ? githubURL.indexOf('?') : githubURL.length; |
| 53 | + githubURL = githubURL.substring(0, queryParamTrimIndex); |
| 54 | + // trim everything after https://github.com/orgname/repo/... to https://github.com/orgname/repo |
| 55 | + let components = githubURL.replace("https://github.com/", '').split('/'); |
| 56 | + // trim .git at the end of the name |
| 57 | + if(githubURL.endsWith(gitSuffix)){ |
| 58 | + githubURL = githubURL.replace(new RegExp(gitSuffix + '$'), ''); |
| 59 | + } |
| 60 | + if(components.length > 2){ |
| 61 | + githubURL = `https://github.com/${components[0]}/${components[1]}`; |
| 62 | + } |
| 63 | + websiteURLInput.value = githubURL; |
61 | 64 | } |
62 | | - websiteURLInput.value = githubURL; |
63 | | -} |
64 | 65 |
|
65 | | -function _validateGitHubURL() { |
66 | | - _fixGitHubBrokenURL(); |
67 | | - let githubURL = websiteURLInput.value; |
68 | | - if(_isValidGitHubURL(githubURL)){ |
69 | | - $(websiteURLInput).removeClass("error-border"); |
70 | | - return true; |
| 66 | + function _validateGitHubURL() { |
| 67 | + _fixGitHubBrokenURL(); |
| 68 | + let githubURL = websiteURLInput.value; |
| 69 | + const $messageDisplay = $("#messageDisplay"); |
| 70 | + if(_isValidGitHubURL(githubURL)){ |
| 71 | + $(websiteURLInput).removeClass("error-border"); |
| 72 | + $messageDisplay.html(""); |
| 73 | + return true; |
| 74 | + } |
| 75 | + $(websiteURLInput).addClass("error-border"); |
| 76 | + $messageDisplay.html(`<span><i class="fas fa-exclamation-triangle" style="color: #f89406"></i> ${Strings.ERROR_ONLY_GITHUB}</span>`); |
| 77 | + return false; |
71 | 78 | } |
72 | | - $(websiteURLInput).addClass("error-border"); |
73 | | - return false; |
74 | | -} |
75 | 79 |
|
76 | | -function _validateProjectLocation() { |
77 | | - if(!window.showDirectoryPicker){ |
78 | | - // fs access apis not present, so we will give phoenix empty location to figure out a suitable location |
| 80 | + function _validateProjectLocation() { |
| 81 | + if(!window.showDirectoryPicker){ |
| 82 | + // fs access apis not present, so we will give phoenix empty location to figure out a suitable location |
| 83 | + return true; |
| 84 | + } |
| 85 | + let location = locationInput.value; |
| 86 | + if( location === Strings.PLEASE_SELECT_A_FOLDER){ |
| 87 | + $(locationInput).addClass("error-border"); |
| 88 | + return false; |
| 89 | + } |
| 90 | + $(locationInput).removeClass("error-border"); |
79 | 91 | return true; |
80 | 92 | } |
81 | | - let location = locationInput.value; |
82 | | - if( location === Strings.PLEASE_SELECT_A_FOLDER){ |
83 | | - $(locationInput).addClass("error-border"); |
84 | | - return false; |
| 93 | + |
| 94 | + function _validate() { |
| 95 | + return _validateGitHubURL() |
| 96 | + && _validateProjectLocation(); |
85 | 97 | } |
86 | | - $(locationInput).removeClass("error-border"); |
87 | | - return true; |
88 | | -} |
89 | 98 |
|
90 | | -function _validate() { |
91 | | - return _validateGitHubURL() |
92 | | - && _validateProjectLocation(); |
93 | | -} |
| 99 | + function _selectFolder() { |
| 100 | + newProjectExtension.showFolderSelect() |
| 101 | + .then(file =>{ |
| 102 | + locationInput.fullPath = file; |
| 103 | + locationInput.value = file.replace(newProjectExtension.getMountDir(), ""); |
| 104 | + _validateProjectLocation(); |
| 105 | + }); |
| 106 | + } |
94 | 107 |
|
95 | | -function _selectFolder() { |
96 | | - newProjectExtension.showFolderSelect() |
97 | | - .then(file =>{ |
98 | | - locationInput.fullPath = file; |
99 | | - locationInput.value = file.replace(newProjectExtension.getMountDir(), ""); |
100 | | - _validateProjectLocation(); |
101 | | - }); |
102 | | -} |
| 108 | + function _createProjectClicked() { |
| 109 | + if(_validate()){ |
| 110 | + let githubURL = websiteURLInput.value; |
| 111 | + let components = githubURL.replace("https://github.com/", '').split('/'); |
| 112 | + let zipURL = `https://phcode.site/getGitHubZip?org=${components[0]}&repo=${components[1]}`; |
| 113 | + let suggestedProjectName = `${components[0]}-${components[1]}`; |
| 114 | + newProjectExtension.downloadAndOpenProject( |
| 115 | + zipURL, |
| 116 | + locationInput.fullPath, suggestedProjectName, FLATTEN_ZIP_FIRST_LEVEL_DIR) |
| 117 | + .then(()=>{ |
| 118 | + Metrics.countEvent(Metrics.EVENT_TYPE.NEW_PROJECT, "github.Click", "create.success"); |
| 119 | + newProjectExtension.closeDialogue(); |
| 120 | + }); |
| 121 | + } else { |
| 122 | + newProjectExtension.showErrorDialogue( |
| 123 | + Strings.MISSING_FIELDS, |
| 124 | + Strings.PLEASE_FILL_ALL_REQUIRED); |
| 125 | + } |
| 126 | + Metrics.countEvent(Metrics.EVENT_TYPE.NEW_PROJECT, "github.Click", "create"); |
| 127 | + } |
103 | 128 |
|
104 | | -function _createProjectClicked() { |
105 | | - if(_validate()){ |
106 | | - let githubURL = websiteURLInput.value; |
107 | | - let components = githubURL.replace("https://github.com/", '').split('/'); |
108 | | - let zipURL = `https://phcode.site/getGitHubZip?org=${components[0]}&repo=${components[1]}`; |
109 | | - let suggestedProjectName = `${components[0]}-${components[1]}`; |
110 | | - newProjectExtension.downloadAndOpenProject( |
111 | | - zipURL, |
112 | | - locationInput.fullPath, suggestedProjectName, FLATTEN_ZIP_FIRST_LEVEL_DIR) |
113 | | - .then(()=>{ |
114 | | - Metrics.countEvent(Metrics.EVENT_TYPE.NEW_PROJECT, "github.Click", "create.success"); |
115 | | - newProjectExtension.closeDialogue(); |
116 | | - }); |
117 | | - } else { |
118 | | - newProjectExtension.showErrorDialogue( |
119 | | - Strings.MISSING_FIELDS, |
120 | | - Strings.PLEASE_FILL_ALL_REQUIRED); |
| 129 | + function initGitProject() { |
| 130 | + if(!window.showDirectoryPicker){ // fs access apis not present |
| 131 | + $(document.getElementById("projectLocation")).addClass("forced-hidden"); |
| 132 | + } |
| 133 | + $(".label-clone").text(Strings.GIT_REPO_URL); |
| 134 | + createProjectBtn = document.getElementById("createProjectBtn"); |
| 135 | + websiteURLInput = document.getElementById("websiteURLInput"); |
| 136 | + locationInput = document.getElementById("locationInput"); |
| 137 | + createProjectBtn.onclick = _createProjectClicked; |
| 138 | + $(websiteURLInput).keyup(_validate); |
| 139 | + locationInput.value = Strings.PLEASE_SELECT_A_FOLDER; |
| 140 | + locationInput.onclick = _selectFolder; |
| 141 | + websiteURLInput.value = "https://github.com/phcode-dev/HTML-Starter-Templates"; |
| 142 | + _validate(); |
121 | 143 | } |
122 | | - Metrics.countEvent(Metrics.EVENT_TYPE.NEW_PROJECT, "github.Click", "create"); |
| 144 | + window.initGitProject = initGitProject; |
123 | 145 | } |
124 | 146 |
|
125 | | -function initGithubProject() { |
126 | | - if(!window.showDirectoryPicker){ // fs access apis not present |
127 | | - $(document.getElementById("projectLocation")).addClass("forced-hidden"); |
128 | | - } |
129 | | - createProjectBtn = document.getElementById("createProjectBtn"); |
130 | | - websiteURLInput = document.getElementById("websiteURLInput"); |
131 | | - locationInput = document.getElementById("locationInput"); |
132 | | - createProjectBtn.onclick = _createProjectClicked; |
133 | | - $(websiteURLInput).keyup(_validate); |
134 | | - locationInput.value = Strings.PLEASE_SELECT_A_FOLDER; |
135 | | - locationInput.onclick = _selectFolder; |
136 | | - _validate(); |
| 147 | +if(!window.top.Phoenix.isNativeApp){ |
| 148 | + browserInit(); |
137 | 149 | } |
0 commit comments