Skip to content

Commit 3b44134

Browse files
Add login dialog
1 parent 19033cc commit 3b44134

File tree

3 files changed

+70
-52
lines changed

3 files changed

+70
-52
lines changed

GitHubAccess/GitHubManager.js

Lines changed: 67 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,23 @@
2121
*
2222
*/
2323

24-
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 100 */
24+
/*jslint sloppy: true, vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 100 */
2525
/*global define, $, brackets, Mustache*/
2626

2727
define(function (require, exports, module) {
28-
"use strict";
2928
var Dialogs = brackets.getModule("widgets/Dialogs"),
3029
FileSystem = brackets.getModule("filesystem/FileSystem"),
3130
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
31+
PromiseQueue = brackets.getModule("utils/Async").PromiseQueue,
3232
ProjectManager = brackets.getModule("project/ProjectManager"),
3333
Strings = brackets.getModule("strings"),
3434
StringUtils = brackets.getModule("utils/StringUtils"),
3535
_ = brackets.getModule("thirdparty/lodash"),
36-
Octokit = require('octokit'),
37-
cloneDialog = require("text!templates/clone-dialog.html");
36+
Octokit = require("octokit"),
37+
cloneDialog = require("text!templates/clone-dialog.html"),
38+
loginDialog = require("text!templates/login-dialog.html"),
39+
cloneDialogData = require("text!json/clone-dialog.json"),
40+
loginDialogData = require("text!json/login-dialog.json");
3841

3942
function GitHubManager() {
4043
}
@@ -43,11 +46,12 @@ define(function (require, exports, module) {
4346

4447
};
4548

46-
GitHubManager.prototype.cloneRepo = function (rootPath) {
47-
};
48-
49-
GitHubManager.prototype.selectFilePathHandler = function (event) {
50-
49+
GitHubManager.prototype.cloneRepo = function (repo, branch, targetPath) {
50+
repo.git.getTree("master", {
51+
recursive: true
52+
}).then(function (tree) {
53+
console.log(tree);
54+
});
5155
};
5256

5357
GitHubManager.prototype.addSelectMenueForArray = function ($element, array) {
@@ -64,36 +68,19 @@ define(function (require, exports, module) {
6468

6569
GitHubManager.prototype.buildBranchArray = function (rawBranches) {
6670
var branches = [];
71+
72+
branches = _.flatten(rawBranches);
6773

68-
_.forEach(rawBranches, function (subBranches) {
69-
subBranches.splice(0, 2);
70-
branches = branches.concat(subBranches);
71-
});
72-
73-
return branches;
74+
return _.difference(_.unique(branches.sort(), true), ["refs", "heads"]);
7475
};
7576

76-
GitHubManager.prototype.init = function () {
77-
var gh = new Octokit(),
78-
self = new GitHubManager(),
79-
repo,
80-
templateVars,
77+
GitHubManager.prototype.openCloneDialog = function (gh, self) {
78+
var templateVars,
8179
dlg,
80+
repo,
8281
$dlg;
8382

84-
templateVars = {
85-
"title": "Clone Dialog",
86-
"buttons": [{
87-
"className": "left cancel",
88-
"id": "cancel",
89-
"text": "Cancel"
90-
}, {
91-
"className": "primary githubaccess-clone",
92-
"disabled": "disabled",
93-
"id": "clone",
94-
"text": "Clone Repository"
95-
}]
96-
};
83+
templateVars = JSON.parse(cloneDialogData);
9784

9885
dlg = Dialogs.showModalDialogUsingTemplate(Mustache.render(cloneDialog, templateVars), false);
9986

@@ -104,15 +91,18 @@ define(function (require, exports, module) {
10491

10592
if (value !== "") {
10693
repo = gh.getRepo(value.split("/")[0], value.split("/")[1]);
107-
repo.getBranches().then(function (branches) {
108-
branches = self.buildBranchArray(branches);
94+
95+
repo.getBranches().then($.proxy(function (branches) {
96+
branches = this.buildBranchArray(branches);
10997

11098
var $step = $dlg.find("div.step1"),
11199
$input;
112-
$step.html("<label>Branch to Clone:</label><select style='margin-left: 0;' class='branch-selection'></select><br><input type='text' placeholder='Folder to clone repo to'><button class='btn select-folder' data-button-id='select-folder'>Select Folder</button>");
113100

114-
self.addSelectMenueForArray($step.find("select.branch-selection"), branches);
115-
$input = $step.find("input");
101+
this.addSelectMenueForArray($step.find("select.branch-selection"), branches);
102+
$step.css("display", "block");
103+
$step.find("select.branch-selection").chosen();
104+
105+
$input = $step.find("input.target-path");
116106

117107
$step.find("button.select-folder").on("click", function () {
118108
FileSystem.showOpenDialog(false, true, "Choose target path", null, [], function (error, dir) {
@@ -131,7 +121,7 @@ define(function (require, exports, module) {
131121
}
132122
});
133123

134-
}, function (answer) {
124+
}, self), function (answer) {
135125
var message;
136126

137127
try {
@@ -149,14 +139,48 @@ define(function (require, exports, module) {
149139
if (id === "cancel") {
150140
Dialogs.cancelModalDialogIfOpen("github-access", "cancel");
151141
} else if (id === "clone") {
152-
console.log("Cloning...");
153-
console.log(repo);
154-
console.log("To: " + $dlg.find("div.step1").find("input").val());
142+
self.cloneRepo(repo, $dlg.find(".branch-selection").val(), $dlg.find("div.step1").find("input").val());
155143
Dialogs.cancelModalDialogIfOpen("github-access", "clone");
156144
}
157145
});
146+
};
147+
148+
GitHubManager.prototype.checkLoginData = function ($element) {
149+
//TODO: Actual validity checking
150+
151+
return true;
152+
};
153+
154+
GitHubManager.prototype.init = function () {
155+
var gh,
156+
self = new GitHubManager(),
157+
repo,
158+
templateVars,
159+
dlg,
160+
$dlg;
161+
162+
templateVars = JSON.parse(loginDialogData);
163+
164+
dlg = Dialogs.showModalDialogUsingTemplate(Mustache.render(loginDialog, templateVars), false);
158165

159-
dlg.done(function (id) {
166+
$dlg = dlg.getElement();
167+
168+
$dlg.on("buttonClick", function (event, id) {
169+
if (id === "cancel") {
170+
Dialogs.cancelModalDialogIfOpen("github-access-login", "cancel");
171+
} else if (id === "login") {
172+
var valid = self.checkLoginData($dlg);
173+
174+
if (valid) {
175+
gh = new Octokit({
176+
token: $dlg.find("input.oauth-token").val()
177+
});
178+
179+
180+
Dialogs.cancelModalDialogIfOpen("github-access-login", "login");
181+
self.openCloneDialog(gh, self);
182+
}
183+
}
160184
});
161185
};
162186

GitHubAccess/templates/clone-dialog.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ <h1 class="dialog-title">{{title}}</h1>
77
<div class="errors">
88
</div>
99
<div class="step0 form-horizontal">
10-
<label for="repo">Repository to clone:</label><input class="repo" placeholder="User/Repository" type="text" pattern="(\w)+/(\w)+" required><br>
10+
<label for="repo">Repository to clone:</label><input class="repo" placeholder="User/Repository" type="text" pattern="(\w)+/(\w)+" required>
1111
<button class="btn get-branches" data-button-id="get-branches">Get branches for Repository</button>
1212
</div><br/>
1313
<div class="step1 form-horizontal">
1414
<label>Branch to Clone:</label>
15-
<select class='branch-selection'></select><br>
15+
<select class='branch-selection'></select>
1616
<input class="target-path" type='text' placeholder='Folder to clone repo to'>
1717
<button class='btn select-folder' data-button-id='select-folder'>Select Folder</button>
1818
</div><br/>

GitHubAccess/templates/login-dialog.html

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@ <h1 class="dialog-title">{{title}}</h1>
1111
<label for="repo">Oauth Token:</label>
1212
<input class="oauth-token" type='text' placeholder='Token'>
1313
</div>
14-
<div class="basic-auth form-horizontal">
15-
<label for="repo">Username:</label>
16-
<input class="user" type='text' placeholder='Username'>
17-
<label for="repo">Password:</label>
18-
<input class="password" type='password' placeholder='Password'>
19-
</div>
20-
<input type="checkbox" name="remember" value="Remember login">
14+
<label for="remember">Remember Login:<input type="checkbox" name="remember" value="Remember login"></label>
2115
</div>
2216
</div>
2317
<div class="modal-footer">

0 commit comments

Comments
 (0)