forked from stephanesan/api-designer
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathraml-designer-4-git.js
More file actions
164 lines (147 loc) · 6.4 KB
/
raml-designer-4-git.js
File metadata and controls
164 lines (147 loc) · 6.4 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
(function(angular) {
'use strict';
angular
.module('headerApp', [])
.controller('headerController',
function ($scope, $http, $q, $sce) {
function updateHeader() {
var p1 = $http({method: "get", url: "https://api.github.com/repos/" + gitParams.repo, cache: true});
var p2 = $http({method: "get", url: "https://api.github.com/repos/" + gitParams.repo + "/branches", cache: true});
var p3 = $http({method: "get", url: "https://api.github.com/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});
for(var branch of branches) {
$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});
for(var tag of tags) {
$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()));
}
// 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()));
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);
for(var el of array) {
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 getParams() {
var queries = window.location.search.replace(/^\?/, '').split('&');
var searchObject = {};
for(var i = 0; i < queries.length; i++ ) {
var split = queries[i].split('=');
searchObject[split[0]] = decodeURIComponent(split[1]);
}
var repo = searchObject['gitRepo'];
var path = searchObject['gitPath'];
var ref = searchObject['gitRef'];
if (repo == null) {
repo = 'mulesoft/api-console';
path = 'dist/examples';
ref = 'master';
}
var out = {repo: repo, path: path, ref: ref};
console.log("gitParams=" + JSON.stringify(out));
return out;
}
function querryString() {
var querry = '?gitRepo='+ gitParams.repo;
if(gitParams.path!=null)
querry+='&gitPath=' + gitParams.path;
if(gitParams.ref!=null)
querry+='&gitRef=' + gitParams.ref;
return querry;
}
function updateIframe() {
$scope.ramlLocation = $sce.trustAsResourceUrl("./raml-designer-git-fs.html"+ querryString());
}
var gitParams = getParams();
var headerConfig = { };
getHeaderConfig();
});
})(window.angular);