forked from stephanesan/api-designer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raml-designer-4-git.js
136 lines (120 loc) · 5.39 KB
/
raml-designer-4-git.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
(function(angular) {
'use strict';
angular
.module('headerApp', [])
.controller('headerController',
function ($scope, $http, $q, $sce) {
function updateHeader() {
var p1 = $http({method: "get", url: "https://"+gitParams.api + "repos/" + gitParams.repo, cache: true});
var p2 = $http({method: "get", url: "https://"+gitParams.api + "repos/" + gitParams.repo + "/branches", cache: true});
var p3 = $http({method: "get", url: "https://"+gitParams.api + "repos/" + gitParams.repo + "/tags", cache: true});
$q.all([p1, p2, p3]).then(function(responses) {
var resp1 = responses[0];
$scope.gitHubAvatar = resp1.data.owner.avatar_url;
$scope.gitHubOwnerHtmlURL = resp1.data.owner.html_url;
$scope.gitHubOwnerType = resp1.data.owner.type;
$scope.gitHubOwnerLogin = resp1.data.owner.login;
$scope.gitHubRepoName = resp1.data.name;
$scope.gitHubRepoPath = gitParams.path;
$scope.gitHubHtmlUrl = resp1.data.html_url;
// Build the select element
$scope.refs = {};
$scope.refs.availableOptions = [];
// Set the branch options
var branches = filterArray(responses[1].data, headerConfig.branches);
var found = false;
var replacement = null;
if(branches.length != 0) {
$scope.refs.availableOptions.push({id: "branches", name: "branches", disabled: true});
branches.forEach(function(branch){
$scope.refs.availableOptions.push({id: branch.name, name: branch.name});
if(branch.name === gitParams.ref) found=true;
else replacement = branch.name;
});
}
// Set the tag options
var tags = filterArray(responses[2].data, headerConfig.tags);
if(tags.length != 0) {
$scope.refs.availableOptions.push({id: "tags", name: "tags", disabled: true});
tags.forEach(function(tag){
$scope.refs.availableOptions.push({id: tag.name, name: tag.name});
if(tag.name === gitParams.ref) found=true;
else replacement = tag.name;
});
}
if(!found) {
// bad ref, force it to the last matching entry;
gitParams.ref = replacement;
console.log("Default ");
history.pushState(
null,
null,
window.location.search.replace(/^\?.*/, querryString(gitParams)));
}
// Set the selected option
$scope.refs.selectedOption= {id: gitParams.ref};
$scope.refs.changed = function() {
gitParams.ref = $scope.refs.selectedOption.id;
history.pushState(
null,
null,
window.location.search.replace(/^\?.*/, querryString(gitParams)));
updateIframe();
};
// We're done, display load the iframe and display the header
updateIframe();
$scope.gitHeader=true;
});
}
function filterArray(array, config) {
var out = [];
if(config == undefined) {
// all pass if no filter defined.
return array;
}
if(config.pattern == undefined ||
config.flags == undefined ) {
// all fail if no pattern or no flags defined.
return out;
}
var regExp = new RegExp(config.pattern, config.flags);
array.forEach(function(el){
if(regExp.test(el.name)) {
out.push(el);
}
});
return out;
}
function getHeaderConfig() {
var configFile = "config/"+gitParams.repo.replace(/\//, '_')+".json";
$http(
{
method: "get",
url: configFile,
cache: true
}
).then(
function(response)
{
console.log("Filter: "+JSON.stringify(response.data));
headerConfig= response.data;
if(undefined == gitParams.ref && undefined != headerConfig.ref) {
gitParams.ref = headerConfig.ref;
}
updateHeader();
},
function(response)
{
console.log("Default filter: "+JSON.stringify(headerConfig));
updateHeader();
}
);
}
function updateIframe() {
$scope.ramlLocation = $sce.trustAsResourceUrl("./api-designer-git-proxy.html"+ querryString(gitParams));
}
var gitParams = getParams();
var headerConfig = { };
getHeaderConfig();
});
})(window.angular);