-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·122 lines (95 loc) · 2.88 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
module.exports = function() {
// Require all the node packages we depend on.
var Promise = require("bluebird");
var hbs = require("hbs");
var $ = require("jquery");
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
// These settings are required to activate jquery's CORS compatibility for cross-site AJAX requests.
$.support.cors = true;
$.ajaxSettings.xhr = function() {
return new XMLHttpRequest();
}
// Load and compile the plugin's hbs template.
var toolbarTemplate = require("./templates/toolbar.hbs");
var GithubToolbar = hbs.compile(toolbarTemplate);
return {
initialize: function (args) {
var self = this;
if (_.has(args, "channelAPI")) {
self.channelAPI = args.channelAPI;
}
self.metadataURL = "";
self.toolbar = null;
self.repo = {
metadata: {}
};
// hook into the channel's topic change event
self.channelAPI.onChannelTopicChange(function (topic) {
self.consumeTopic(topic);
});
},
consumeTopic: function (topic) {
var self = this;
self.topic = topic;
if (topic) {
var match = topic.match(/http(s)?:\/\/.*\.?github.com\/(.[\w|\-|\/]+)/);
if (match && match[2]) {
var key = match[2];
var newMetadataURL = "";
if (/\/$/.test(key)) {
key = key.replace(/\/$/, "");
}
if (/\//.test(key)) {
newMetadataURL = "https://api.github.com/repos/" + key;
} else {
newMetadataURL = "https://api.github.com/orgs/" + key;
}
// if the metadata url has changed, do an ajax call to retrieve new metadata
if (self.metadataURL !== newMetadataURL) {
self.metadataURL = newMetadataURL;
self.updateAndRender();
}
return;
}
}
self.channelAPI.removeToolbar();
},
pluginToolbar: function (repo) {
var self = this;
var params = {
repo: repo,
isOrg: false
};
if (_.has(repo.metadata, "type")) {
if (repo.metadata.type === "Organization") {
params.isOrg = true;
}
}
var html = GithubToolbar(params);
self.channelAPI.setToolbar(html);
},
updateAndRender: function () {
var self = this;
Promise.resolve($.ajax({
url: self.metadataURL,
dataType: "json",
type: "get",
ifModified: true
}))
.then(function (metadata) {
if (metadata && !_.isEmpty(metadata)) {
self.repo.metadata = metadata;
self.pluginToolbar(self.repo);
}
return self.repo;
})
.catch(function (error) {
});
},
close: function () {
var self = this;
// if (self.githubUpdateCheck) clearInterval(self.githubUpdateCheck);
self.channelAPI.removeToolbar();
}
}
}