forked from smeup/Sme.UP-Documentazione
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditOnGithub.js
71 lines (60 loc) · 1.91 KB
/
EditOnGithub.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
;(function(win) {
win.EditOnGithubPlugin = {}
function create(docBase, docEditBase, title) {
title = title || 'Edit on github'
docEditBase = docEditBase || docBase.replace(/\/blob\//, '/edit/')
function editDoc(event, vm) {
var found;
mdUrl = vm.route.file
var xhttp = new XMLHttpRequest(); // Chiamata per controllare se il file esiste
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 404) {
console.log("NOT FOUND");
found = false;
console.log(found);
}else if (this.readyState == 4 && this.status == 200){
console.log("FOUND")
found = true;
console.log(found);
}
};
xhttp.open("GET", mdUrl, false);
xhttp.send();
var docName = vm.route.file
//docName = docName.replace('smeup-wiki/','')
console.log(docName)
if (docName) {
if(found == false){
docEditBase = docEditBase.replace('/edit/', '/new/')
docName = "?filename=" + docName
}else{
docEditBase = docEditBase.replace('/new/', '/edit/')
}
var editLink = docEditBase + docName
window.open(editLink)
event.preventDefault()
return false
} else {
return true
}
}
win.EditOnGithubPlugin.editDoc = editDoc
return function(hook, vm) {
win.EditOnGithubPlugin.onClick = function(event) {
EditOnGithubPlugin.editDoc(event, vm)
}
var header = [
'<div>',
'<p style="float: right"><a href="',
docBase,
'" target="_blank" onclick="EditOnGithubPlugin.onClick(event)"><img src="edit.png" style="margin-left:20px; width:25px; height:25px">',
'</a></p>',
'</div>'
].join('')
hook.afterEach(function (html) {
return header + html
})
}
}
win.EditOnGithubPlugin.create = create
}) (window)