Skip to content

Commit

Permalink
A huge refactor to split a lot of initialization into their own files.
Browse files Browse the repository at this point in the history
* Also make sure to refetch the issue list every time the panel is opened.

close #22
  • Loading branch information
stomlinson committed Apr 9, 2012
1 parent ef1dee0 commit cc55d4c
Show file tree
Hide file tree
Showing 10 changed files with 412 additions and 220 deletions.
6 changes: 6 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

exports.config = {
name: "GitHub Issues Addon",
favicon: "https://github.com/favicon.ico",
Expand Down
28 changes: 28 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const { Cc, Ci } = require("chrome");

exports.Helpers = {
getAnchorElement: function(name) {
let browserWindow = Cc["@mozilla.org/appshell/window-mediator;1"].
getService(Ci.nsIWindowMediator).
getMostRecentWindow("navigator:browser");
let anchors = browserWindow.document.querySelectorAll('#addon-bar > toolbaritem'),
len = anchors.length;
let anchor = null;

for(let index = 0, anc; index < len, anc = anchors[index]; index++) {
if(anc.getAttribute("label") === name) {
anchor = anc;
}
}

return anchor;

}
};

106 changes: 106 additions & 0 deletions lib/issues.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const {Request} = require("request");
const {Repos} = require("./repos");
const {Labels} = require("./labels");
const {prefs} = require("simple-prefs");

exports.Issues = {
get: getIssues,
search: search,
getCount: getCount
};

function getIssues(opts, callback) {
opts = opts || {};
opts.state = opts.state || "open";
opts.per_page = opts.per_page || prefs.page_size;

let url = Repos.BASE_URL + "/issues" + getURLString(opts);

console.log("requesting: " + url);
Request({
url: url,
onComplete: function(response) {
callback && callback(response.json);
}
}).get();
}

function getCount(callback) {
let issuesRequest = Request({
url: Repos.BASE_URL,
onComplete: function(response) {
let count = response.json.open_issues;
callback && callback(count);
}
}).get();
}

function search(search_term, callback) {
let searchTerm = search_term.replace(/[\t ]+/, " ").replace(" ", "+");

let url = "http://github.com/api/v2/json/issues/search/" + prefs.repo_url + "/open/" + searchTerm;
console.log(url);
Request({
url: url,
onComplete: function(response) {
convertV2toV3(response.json.issues, callback);
}
}).get();
}

function convertV2toV3(issues, callback) {
Labels.get(function(labels) {
let labelsHash = getLabelsHash(labels);
// Issues by default come back in chronological order
issues = issues.reverse();
issues.forEach(function(issue) {
issue.assignee = {
avatar_url: "http://www.gravatar.com/avatar/" + issue.gravatar_id,
login: issue.user
};

let labels = issue.labels || [],
newLabels = [];

issue.labels = newLabels;

labels.forEach(function(label) {
newLabels.push(labelsHash[label]);
});

callback && callback(issues);
});
});

}

function getLabelsHash(labels) {
let labelsHash = {};
labels = labels || [];

labels.forEach(function(label) {
labelsHash[label.name] = label;
});

return labelsHash;
}

function getURLString(opts) {
let urlString = "";
let optsArr = [];
for(let key in opts) {
optsArr.push(encodeURIComponent(key) + "=" + encodeURIComponent(opts[key]));
}

if(optsArr.length) {
urlString += "?" + optsArr.join("&");
}
return urlString;
}

18 changes: 18 additions & 0 deletions lib/issues_panel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const {data} = require("self");
const {Panel} = require("panel");

exports.get = function() {
return Panel({
width: 400,
height: 700,
contentURL: data.url("issues.html"),
contentScriptFile: [ data.url("mustache.js"), data.url("shared.js"), data.url("issues.js") ]
});
}

25 changes: 25 additions & 0 deletions lib/labels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const {Request} = require("request");
const {Repos} = require("./repos");

exports.Labels = {
get: function(callback) {
let url = Repos.BASE_URL + "/labels";
Request({
url: url,
onComplete: function(response) {
let labels = response.json;
labels.splice(0, 0, {
name: "all"
});

callback(labels);
}
}).get();
}
};
Loading

0 comments on commit cc55d4c

Please sign in to comment.