Skip to content

Commit 6a2bfef

Browse files
committed
Refactor JS code.
1 parent 897faa5 commit 6a2bfef

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

js/upriver.js

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
'use strict';
2+
13
function Upriver(token) {
24
$.ajaxSetup({
35
headers: {
4-
'Authorization': 'token ' + sessionStorage.getItem('token')
6+
Authorization: 'token ' + token
57
}
68
});
79

@@ -23,38 +25,45 @@ function Upriver(token) {
2325
self.loadBranches(selected.text(), '#parentBranch');
2426
});
2527

26-
$('#pull').click(this.pull);
28+
$('#pull').click(function() {
29+
var repo = $('#forkRepo').children('option:selected').text();
30+
var branch = $('#forkBranch').children('option:selected').text();
31+
var hash = $('#parentBranch').children('option:selected').attr('data-sha');
32+
var force = $('#force').prop('checked');
33+
this.setCommit(repo, branch, hash, force);
34+
}.bind(this));
2735

2836
this.loadRepos(function() {
2937
$('#forkRepo').change();
3038
$('#parentRepo').change();
3139
});
3240
}
3341

34-
Upriver.prototype.getRepo = function(repo, callback) {
42+
Upriver.prototype.getRepo = function getRepo(repo, callback) {
3543
$.getJSON('https://api.github.com/repos/' + repo, callback);
3644
};
3745

38-
Upriver.prototype.getRepos = function(callback) {
46+
Upriver.prototype.getRepos = function getRepos(callback) {
3947
$.getJSON('https://api.github.com/user', function(user) {
4048
$.getJSON(user.repos_url, callback);
4149
});
4250
};
4351

44-
Upriver.prototype.addRepo = function(repo) {
45-
$('<option></option>')
52+
Upriver.prototype.addRepo = function addRepo(repo) {
53+
$(document.createElement('option'))
4654
.text(repo.full_name)
4755
.attr('data-name', repo.full_name)
48-
.attr('data-parent', (repo.parent) ? repo.parent.full_name : '')
56+
.attr('data-parent', repo.parent.full_name)
57+
.attr('data-default-branch', repo.default_branch)
4958
.appendTo('#forkRepo');
5059

51-
$('<option></option>')
60+
$(document.createElement('option'))
5261
.text(repo.parent.full_name)
5362
.attr('data-name', repo.parent.full_name)
5463
.appendTo('#parentRepo');
5564
};
5665

57-
Upriver.prototype.loadRepos = function(callback) {
66+
Upriver.prototype.loadRepos = function loadRepos(callback) {
5867
this.getRepos(function(repos) {
5968
var queue = async.queue(function(repo, callback) {
6069
this.getRepo(repo.full_name, function(repo) {
@@ -73,45 +82,38 @@ Upriver.prototype.loadRepos = function(callback) {
7382
}.bind(this));
7483
};
7584

76-
Upriver.prototype.getBranches = function(repo, callback) {
85+
Upriver.prototype.getBranches = function getBranches(repo, callback) {
7786
$.getJSON('https://api.github.com/repos/' + repo + '/branches', callback);
7887
};
7988

80-
Upriver.prototype.addBranch = function(element, branch) {
81-
$('<option></option>')
89+
Upriver.prototype.addBranch = function addBranch(element, branch) {
90+
$(document.createElement('option'))
8291
.text(branch.name)
8392
.attr('data-name', branch.name)
8493
.attr('data-sha', branch.commit.sha)
94+
.prop('selected', branch.name === 'master')
8595
.appendTo(element);
8696
};
8797

88-
Upriver.prototype.loadBranches = function(repo, element) {
98+
Upriver.prototype.loadBranches = function loadBranches(repo, element) {
8999
this.getBranches(repo, function(branches) {
90-
$(element).html('');
91-
branches.forEach(this.addBranch.bind(this, element));
92-
$(element).children('option[data-name=\'master\']').prop('selected', true).change();
100+
$(element).empty();
101+
branches.forEach(function(branch) {
102+
this.addBranch(element, branch);
103+
}.bind(this));
93104
}.bind(this));
94105
};
95106

96-
Upriver.prototype.pull = function() {
97-
var forkRepo = $('#forkRepo').children('option:selected').text();
98-
var forkBranch = $('#forkBranch').children('option:selected').text();
99-
var parentSha = $('#parentBranch').children('option:selected').attr('data-sha');
100-
var force = $('#force').prop('checked');
101-
107+
Upriver.prototype.setCommit = function setCommit(repo, branch, sha, force) {
102108
$.ajax({
103109
type: 'PATCH',
104110
dataType: "json",
105-
url: 'https://api.github.com/repos/' + forkRepo + '/git/refs/heads/' + forkBranch,
106-
data: JSON.stringify({
107-
sha: parentSha,
108-
force: force
109-
}),
110-
complete: function(xhr) {
111-
var data = JSON.parse(xhr.responseText);
112-
$('#modal pre').text(JSON.stringify(data, undefined, 2));
113-
$('#modal').modal('show');
114-
}
111+
url: 'https://api.github.com/repos/' + repo + '/git/refs/heads/' + branch,
112+
data: JSON.stringify({ sha: sha, force: force })
113+
}).done(function(body) {
114+
var data = JSON.parse(data);
115+
$('#modal pre').text(JSON.stringify(data, undefined, 2));
116+
$('#modal').modal('show');
115117
});
116118
};
117119

0 commit comments

Comments
 (0)