-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapi-designer-git-reader.html
244 lines (221 loc) · 11 KB
/
api-designer-git-reader.html
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="querry-parameters.js"></script>
<title>Stephanesan's app</title>
<link rel="stylesheet" href="https://cdn.rawgit.com/mulesoft/api-designer/read-only-mode/dist/styles/vendor.css">
<link rel="stylesheet" href="https://cdn.rawgit.com/mulesoft/api-designer/read-only-mode/dist/styles/main.css">
</head>
<body ng-app="ramlEditorApp">
<raml-editor></raml-editor>
<script src="https://cdn.rawgit.com/mulesoft/api-designer/read-only-mode/dist/scripts/vendor.js"></script>
<script src="https://cdn.rawgit.com/mulesoft/api-designer/read-only-mode/dist/scripts/main.js"></script>
<script>
// This part is needed only if you want to provide your own Persistance Implementation
// Angular Module must match "ramlEditorApp"
angular.module('ramlEditorApp')
.factory('MyFileSystem', function ($q, config, $rootScope, $http) {
var service = {};
var files = [];
var dirs = [];
var gitParams = getParams();
if(gitParams.file != undefined) {
var newPath = gitParams.file;
config.set('currentFile', JSON.stringify({path: newPath, name: newPath.slice(newPath.lastIndexOf('/') + 1)}));
}
service.directory = function (path) {
var deferred = $q.defer();
// trim the trailing slash, github api doesn't permit this when using query parameters.
if(path != "/") {
path = path.replace(/\/$/, "");
path = path.replace(/^\//, "");
}
console.log("directory path " + path);
// make a closure with the current object to insert the results into
function makeParseGit (current) {
console.log("makeParseGit " + current.path);
return function parseGit (response) {
current.children = [];
console.log("current? " + JSON.stringify(current));
// response is either an array of objects (download_url request)
if(Array.isArray(response.data)) {
var data = response.data;
for(var i=0; i<data.length; i++) {
var type = data[i].type;
var index = (current.path.slice(current.path.length-1) == "/")? current.path + data[i].name : (current.path +'/' + (data[i].name));
// use JSON format instead intead of plain text
// (i.e. use .url instead of .download_url)
// because github doesn't allow cross origin
// on plain text files but does allow it on JSON
if( ('file' == type && null == data[i].download_url) || 'file' != type ) {
// files that don't have a download_url seem to be submodules
// so these need to be treated as folders instead of files
console.log("has folder " + data[i].name);
current.children.push({path: index, type: 'folder', meta: {}});
dirs[index] = data[i].git_url;
console.log('push dirs['+index+']='+data[i].git_url);
} else if( 'file' == type && null != data[i].url) {
console.log("has file " + data[i].name);
if((/\.(md|raml|json|schema|xml|xsd|yaml)$/i).test(data[i].name)) {
current.children.push({path: index, type: 'file', meta: {}});
files[index] = {type: 'blob', url: data[i].url};
}
}
}
// or response has a tree with an array of objects (git_url request)
} else if(Array.isArray(response.data.tree)) {
data = response.data.tree;
for(var i=0; i<data.length; i++) {
var index = (current.path.slice(current.path.length-1) == "/")? current.path + data[i].path : (current.path +'/' + (data[i].path));
var type = data[i].type;
if( 'blob' == type) {
console.log("has blob " + data[i].path);
if((/\.(md|raml|json|schema|xml|xsd|yaml)$/i).test(data[i].path)) {
current.children.push({path: index, type: 'file', meta: {}});
files[index] = {type: type, url: data[i].url};
}
} else {
console.log("has tree " + data[i].path);
current.children.push({path: index, type: 'folder', meta: {}});
dirs[index] = data[i].url;
console.log('push dirs['+index+']='+data[i].git_url);
}
}
} else {
console.error("unhandled data!");
}
return current;
};
};
function dir(current) {
p1 = new Promise(
function(resolve, reject) {
var f = makeParseGit(current);
var git_url = dirs[current.path];
console.log('pop dirs['+current.path+']='+git_url);
$http.get(git_url).then(
function (data) {
var out = f(data);
var promisses = [];
for (var i=0; i<out.children.length; i++) {
if(out.children[i].type == 'folder') {
promisses.push(dir(out.children[i]));
}
}
if(promisses.length != 0) {
Promise.all(promisses).then(function(data) {
resolve(out);
});
} else {
resolve(out);
}
}, function(response) {
alert("Failed: " + response.config.method + " on \"" + response.config.url +"\" returned: "+response.statusText+".");
reject(response.status);
}
);
}
);
return p1;
};
console.log('start recurse') ;
var root = {path: '/'};
dirs[root.path]='https://'+ assemblePath([gitParams.api,'repos/',gitParams.repo,'/contents/',gitParams.path])+'?ref='+gitParams.ref;
dir(root).then(function(data) {
console.log('done recurse');
deferred.resolve(data);
});
// Your magic goes here:
// Do deferred.resolve(data); to fulfull the promise or
// deferred.reject(error); to reject it.
return deferred.promise;
};
service.load = function (path) {
var deferred = $q.defer();
if(files[(path)] == undefined || files[(path)].url == undefined) {
console.log("skip download " + path);
throw ("Unknow file location");
}
var download_url = files[(path)].url;
console.log("download (" +path+") " + download_url);
ga('send', 'pageview', location.pathname + location.search + '#'+ encodeURIComponent(path));
// make a closure with the path to lookup context of the callback
function makeParseBlob (path) {
console.log("makeParseBlob " + path);
return function (response) {
if(files[path] != undefined && files[path].type == 'blob')
// Windows return an error when '/n' is present in base64 encoded content
// Remove those from the content before calling atob
return atob(JSON.parse(response.data).content.replace(/\s/g, ""));
else
return response.data;
};
}
var f = makeParseBlob(path);
$http(
{ url: download_url,
method: 'GET',
transformResponse: [function (data) {
return data;
}]
}).then(function(response) {
//console.log(response.data);
var data = f(response);
deferred.resolve(data);
}, function(response) {
alert("Failed: " + response.config.method + " on \"" + response.config.url +"\" returned: "+response.statusText+".");
reject(response.status);
});
// Your magic goes here:
// Do deferred.resolve(data); to fulfull the promise or
// deferred.reject(error); to reject it.
return deferred.promise;
};
service.remove = function (path, name) {
var deferred = $q.defer();
// Your magic goes here:
// Do deferred.resolve(data); to fulfull the promise or
// deferred.reject(error); to reject it.
return deferred.promise;
};
service.save = function (path, name, contents) {
var deferred = $q.defer();
// Your magic goes here:
// Do deferred.resolve(data); to fulfull the promise or
// deferred.reject(error); to reject it.
return deferred.promise;
};
return service;
})
.run(function (MyFileSystem, config, options, $rootScope) {
options.readOnly = true;
// Set MyFileSystem as the filesystem to use
config.set('fsFactory', 'MyFileSystem');
// In case you want to send notifications to the user
// (for instance, that he must login to save).
// The expires flags means whether
// it should be hidden after a period of time or the
// user should dismiss it manually.
$rootScope.$broadcast('event:notification',
{message: 'File saved.', expires: true});
});
</script>
<style>
html,
body {
height: 100%;
}
</style>
<footer>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-73616509-1', 'auto');
ga('send', 'pageview');
</script>
</footer>
</body>
</html>