Skip to content

Commit

Permalink
Merge pull request #145 from cs-education/github_ui_to_contextmenu
Browse files Browse the repository at this point in the history
Move GitHub UI to the Context Menu
  • Loading branch information
coltonmercurio committed Apr 20, 2016
2 parents 17fc1a5 + beda87b commit ca59022
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 118 deletions.
41 changes: 29 additions & 12 deletions src/app/github-int/github-int.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class GithubInt {
* Note: unauthenticated users are limited to 60 github api calls per hour
* and each file is an api call.
*/
cloneRepo(repoUrl) {
cloneRepo(repoUrl, destPath) {

var tokens = repoUrl.split('/', 2);
if(tokens.length<2)
Expand All @@ -57,7 +57,9 @@ class GithubInt {
return;
}

fs.makeDirectory(reponame);
var parentPath = destPath + '/' + reponame;

fs.makeDirectory(parentPath);

var loaded = 0;
var showErrors = true;
Expand All @@ -82,7 +84,7 @@ class GithubInt {
return;
}

fs.writeFile(reponame+'/'+tree[this].path, new Buffer(data,'binary'));
fs.writeFile(parentPath+'/'+tree[this].path, new Buffer(data,'binary'));

loaded += 1;

Expand All @@ -94,7 +96,7 @@ class GithubInt {
if(tree[i].type == 'tree'){

loaded += 1;
fs.makeDirectory(reponame+'/'+tree[i].path);
fs.makeDirectory(parentPath+'/'+tree[i].path);
}
}
}.bind(this));
Expand All @@ -105,14 +107,16 @@ class GithubInt {
* If the repo already exists, it deletes it and creates a new repo.
* If needed this can be extended to modify current existing repo rather than deleting, but this is non-trivial.
*/
saveAll() {
saveAll(saveRepoName, srcPath) {
var fs = SysFileSystem;
this.saveRepoName = saveRepoName;
this.sourcePath = srcPath;

if(!this.authenticated)
{
notify('Must be authenticated...', 'red');
}
var repo = this.hub.getRepo(this.username, 'saved-jor1k-workspace');
var repo = this.hub.getRepo(this.username, saveRepoName);
repo.show(function(err, repo_info){
if(err){
if(err.error==404)
Expand All @@ -138,7 +142,7 @@ class GithubInt {
* Helper for saveAll.
*/
createSaveRepo(err, res) {
this.user.createRepo({'name': 'saved-jor1k-workspace'}, function(err, res) {
this.user.createRepo({'name': this.saveRepoName}, function(err, res) {

if(err)
{
Expand All @@ -150,8 +154,8 @@ class GithubInt {
return;
}

var repo = this.hub.getRepo(this.username, 'saved-jor1k-workspace');
this.pushToRepo(repo);
var repo = this.hub.getRepo(this.username, this.saveRepoName);
this.pushToRepo(repo, this.sourcePath);
}.bind(this));
}

Expand All @@ -160,9 +164,22 @@ class GithubInt {
*
* This is very hacky... the api is very limited and doesn't allow parallel writes
*/
pushToRepo(repo) {
pushToRepo(repo, sourcePath) {
var fs = SysFileSystem;
var tree = fs.getDirectoryTree();

var tree = [];
if(sourcePath === ''){
tree = fs.getDirectoryTreeOfDir('/');
}
else{
tree = fs.getDirectoryTreeOfDir(sourcePath);
}

//trim source path from tree
var pathLength = sourcePath.length;
var trimPath = function(fullPath){
return fullPath.substring(pathLength, fullPath.length);
};

var readFile = function(err){

Expand Down Expand Up @@ -206,7 +223,7 @@ class GithubInt {

var i = this;
tree[this].path = tree[this].path.substring(1,tree[this].path.length);
repo.write('master', tree[i].path, buf.toString('binary'), 'save', readFile.bind(i+1));
repo.write('master', trimPath(tree[i].path), buf.toString('binary'), 'save', readFile.bind(i+1));
};

readFile();
Expand Down
6 changes: 3 additions & 3 deletions src/app/sys-filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ class SysFileSystem {
* of all nodes within /home/user. Partially ordered from root -> leafs
*/
getDirectoryTree(){
return this.getDirectoryTreeHelper('/');
return this.getDirectoryTreeOfDir('/');
}

/*
* Helper for getDirectoryTree
*/
getDirectoryTreeHelper(path){
getDirectoryTreeOfDir(path){
var children = this.localFS.readdirSync(path);

if(path=='/')
Expand All @@ -200,7 +200,7 @@ class SysFileSystem {

for(var a=0; a<dirs.length; a++)
{
ret.push.apply(ret, this.getDirectoryTreeHelper(dirs[a]));
ret.push.apply(ret, this.getDirectoryTreeOfDir(dirs[a]));
}

return ret;
Expand Down
4 changes: 0 additions & 4 deletions src/app/sys-global-observables.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ export var compileStatus = ko.observable('');
export var focusTerm = ko.observable((tty) => {});
export var runCode = ko.observable((gccOptions) => {});

export var githubUsername = ko.observable('');
export var githubPassword = ko.observable('');
export var githubRepo = ko.observable('');

export var buildCmd = ko.observable('');
export var execCmd = ko.observable('');

Expand Down
39 changes: 0 additions & 39 deletions src/components/file-browser/file-browser.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,4 @@
</ul>
</div>
<div id="file-browser-body"><span class="loading">Loading...</span></div>
<div id="github-opts-container">
<div class="row">
<div class="col-sm-9">
<div class="row">
<label class="col-sm-2 control-label"><small>username</small></label>
<div class="col-sm-4">
<input class="form-control input-sm" type="text" maxlen=256
title="Github username" data-bind="textInput: githubUsername">
</div>
<label class="col-sm-2 control-label"><small>password</small></label>
<div class="col-sm-4">
<input id="githubPassword" class="form-control input-sm" type="password" maxlen=256
title="Github password" data-bind="textInput: githubPassword">
</div>
</div>
<div class="row">
<label class="col-sm-2 control-label"><small>repo&nbsp;url</small></label>
<div class="col-sm-10">
<input id="githubRepo" class="form-control input-sm" type="text" maxlen=256 title="URL of repo to clone into workspace" placeholder="username/saved-jor1k-workspace"
data-bind="textInput: githubRepo">
</div>
</div>
</div>
<div>
<button id="save-workspace-btn"
data-bind="enable: compileBtnEnable"
title="push workspace to 'saved-jor1k-workspace' repo"
type="button" class="btn btn-xs pull-right col-sm-3 btn-default">
Save Workspace<br>
</button>
<button id="clone-repo-btn"
data-bind="enable: compileBtnEnable"
title="clone repo into workspace"
type="button" class="btn btn-xs pull-right col-sm-3 btn-default">
Clone Repo<br>
</button>
</div>
</div>
</div>
</div>
157 changes: 97 additions & 60 deletions src/components/file-browser/file-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,65 +30,6 @@ var confirmNotific8Options = {

class Filebrowser {
constructor() {
this.githubUsername = SysGlobalObservables.githubUsername;
this.githubPassword = SysGlobalObservables.githubPassword;
this.githubRepo = SysGlobalObservables.githubRepo;
this.compileBtnEnable = SysGlobalObservables.compileBtnEnable;

var githubOptsContainer = $('#github-opts-container');
$('span:contains("File Browser")').click(() => {
githubOptsContainer.show();
});

$('span:contains("Code")').click(() => {
githubOptsContainer.hide();
});

$('span:contains("Video Search")').click(() => {
githubOptsContainer.hide();
});

$('span:contains("Man page search")').click(() => {
githubOptsContainer.hide();
});

githubOptsContainer.css('width', $('#code-container').width() + 'px');

const $saveReop = $('#save-workspace-btn');
$saveReop.click(() => {
var username = this.githubUsername();
var password = this.githubPassword();
console.log('Save Repo');
this.githubPassword('');

if (!username || !password)
return;

var github = new GithubInt(username, password);
github.saveAll();
});

const $cloneRepo = $('#clone-repo-btn');
$cloneRepo.click(() => {
var username = this.githubUsername();
var password = this.githubPassword();
var repo = this.githubRepo();
console.log('Clone Repo');
if (!repo)
return;

this.githubPassword('');

var github;

if(username && password)
github = new GithubInt(username, password);
else
github = new GithubInt();

github.cloneRepo(repo);
});

var readyCallback = () => {
this.id = '#file-browser-body';
var fs = this.fs = SysFileSystem;
Expand Down Expand Up @@ -169,6 +110,10 @@ class Filebrowser {
}
menuHtml += '<li><a data-action="rename">Rename</a></li>';
menuHtml += '<li><a data-action="delete">Delete</a></li>';
if (self.metaData[itemId].isDirectory) {
menuHtml += '<li><a data-action="clone">Clone a repo into \'' + self.metaData[itemId].name + '\'...</a></li>';
menuHtml += '<li><a data-action="push">Push \''+ self.metaData[itemId].name + '\' to a repo...</a></li>';
}

menuContainer.html(menuHtml);
},
Expand All @@ -182,7 +127,7 @@ class Filebrowser {
var index;

if (action === 'delete') {
bootbox.confirm('Are you sure you want to delete "' + itemName + '" ?', function (result) {
bootbox.confirm('Are you sure you want to delete the directory \'' + itemName + '\'?', function (result) {
if (result) {
if (self.metaData[itemId].isDirectory) {
self.fs.removeDirectory(itemPath);
Expand Down Expand Up @@ -235,6 +180,98 @@ class Filebrowser {
}
});
}
else if (action === 'clone') {
bootbox.dialog({
title: 'Clone a GitHub repo into \'' + itemName + '\'...',
message: '<div>'
+ '<div class="row">'
+ '<label class="col-sm-2 control-label"><small>Username (optional)</small></label>'
+ '<div class="col-sm-4">'
+ '<input id="githubUsername" class="form-control input-sm" type="text" maxlen=256>'
+ '</div>'
+ '<label class="col-sm-2 control-label"><small>Password</small></label>'
+ '<div class="col-sm-4">'
+ '<input id="githubPassword" class="form-control input-sm" type="password" maxlen=256>'
+ '</div>'
+ '</div>'
+ '<div class="row">'
+ '<label class="col-sm-2 control-label"><small>Repo URI</small></label>'
+ '<div class="col-sm-10">'
+ '<input id="githubRepo" class="form-control input-sm" type="text" maxlen=256 placeholder="username/repo-name">'
+ '</div>'
+ '</div>'
+'</div>',
buttons: {
success: {
label: 'Clone',
className: 'btn-clone',
callback: function () {
var username = $('#githubUsername').val();
var password = $('#githubPassword').val();
var repoName = $('#githubRepo').val();

console.log('Clone Repo');

if (repoName.trim().length === 0)
return;

var github;

if((username.trim().length !== 0) && (password.trim().length !== 0))
github = new GithubInt(username, password);
else
github = new GithubInt();

github.cloneRepo(repoName, itemPath);
}
}
}
});
}
else if (action === 'push') {
bootbox.dialog({
title: 'Push \''+ itemName + '\' to a GitHub repo...',
message: '<div>'
+ '<div class="row">'
+ '<label class="col-sm-2 control-label"><small>Username</small></label>'
+ '<div class="col-sm-4">'
+ '<input id="githubUsername" class="form-control input-sm" type="text" maxlen=256>'
+ '</div>'
+ '<label class="col-sm-2 control-label"><small>Password</small></label>'
+ '<div class="col-sm-4">'
+ '<input id="githubPassword" class="form-control input-sm" type="password" maxlen=256>'
+ '</div>'
+ '</div>'
+ '<div class="row">'
+ '<label class="col-sm-2 control-label"><small>Repo Name</small></label>'
+ '<div class="col-sm-10">'
+ '<input id="githubSaveRepo" class="form-control input-sm" type="text" maxlen=256 placeholder="sysprog-save">'
+ '</div>'
+ '</div>'
+'</div>',
buttons: {
success: {
label: 'Push',
className: 'btn-push',
callback: function () {
var username = $('#githubUsername').val();
var password = $('#githubPassword').val();
var saveRepoName = $('#githubSaveRepo').val();

console.log('Save Repo');

if ((username.trim().length === 0) || (password.trim().length === 0)
|| (saveRepoName.trim().length === 0))
return;

var github = new GithubInt(username, password);

github.saveAll(saveRepoName, itemPath);
}
}
}
});
}
}
});

Expand Down
1 change: 1 addition & 0 deletions src/styles/_file-browser.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
user-select: none;
width: 100%;
height: 100%;
overflow: auto;

.loading {
font-size: 16px;
Expand Down

0 comments on commit ca59022

Please sign in to comment.