-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapp.js
More file actions
140 lines (124 loc) · 4.71 KB
/
app.js
File metadata and controls
140 lines (124 loc) · 4.71 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
'use strict';
var solid = SolidClient;
var vocab = solid.vocab;
var Pastebin = Pastebin || {};
Pastebin = (function () {
// Default publish location
// ATTENTION: this variable must be set for the app to create new bins
var defaultContainer = '';
// Bin structure
var bin = {
url: '',
title: '',
body: ''
}
function init() {
document.getElementById('edit').classList.add('hidden');
document.getElementById('view').classList.add('hidden');
if (defaultContainer.lastIndexOf('/') != defaultContainer.length - 1) {
defaultContainer += '/';
}
console.log(defaultContainer)
if (queryVals['view'] && queryVals['view'].length > 0) {
load(queryVals['view']);
} else if (queryVals['edit'] && queryVals['edit'].length > 0) {
load(queryVals['edit'], true);
} else {
console.log('new pastebin form');
document.getElementById('submit')
.setAttribute('onclick', 'Pastebin.publish()');
document.getElementById('edit').classList.remove('hidden');
}
}
function load (url, showEditor) {
solid.web.get(url).then(function(response) {
var graph = response.parsedGraph();
// set url
bin.url = response.url;
var subject = $rdf.sym(response.url);
// add title
var title = graph.any(subject, vocab.dct('title'));
if (title) {
bin.title = title.value;
}
// add body
var body = graph.any(subject, vocab.sioc('content'));
if (body) {
bin.body = body.value;
}
if (showEditor) {
document.getElementById('edit-title').value = bin.title;
document.getElementById('edit-body').innerHTML = bin.body;
document.getElementById('submit').setAttribute('onclick',
'Pastebin.update()');
document.getElementById('edit').classList.remove('hidden');
} else {
document.getElementById('view-title').innerHTML = bin.title;
document.getElementById('view-body').innerHTML = bin.body;
document.getElementById('view').classList.remove('hidden');
}
}).catch(function(err) {
// do something with the error
console.log(err);
});
}
function publish () {
bin.title = document.getElementById('edit-title').value;
bin.body = document.getElementById('edit-body').value;
var graph = $rdf.graph();
var thisResource = $rdf.sym('');
graph.add(thisResource, vocab.dct('title'), $rdf.lit(bin.title));
graph.add(thisResource, vocab.sioc('content'), $rdf.lit(bin.body));
var data = new $rdf.Serializer(graph).toN3(graph);
solid.web.post(defaultContainer, data).then(function(meta) {
// view
var url = meta.url;
if (url && url.slice(0,4) != 'http') {
if (url.indexOf('/') === 0) {
url = url.slice(1, url.length);
}
url = defaultContainer + url.slice(url.lastIndexOf('/') + 1, url.length);
}
window.location.search = "?view="+encodeURIComponent(url);
}).catch(function(err) {
// do something with the error
console.log(err);
});
}
function update () {
bin.title = document.getElementById('edit-title').value;
bin.body = document.getElementById('edit-body').value;
var graph = $rdf.graph();
var thisResource = $rdf.sym('');
graph.add(thisResource, vocab.dct('title'), bin.title);
graph.add(thisResource, vocab.sioc('content'), bin.body);
var data = new $rdf.Serializer(graph).toN3(graph);
solid.web.put(bin.url, data).then(function(meta) {
// view
window.location.search = "?view="+encodeURIComponent(meta.url);
}).catch(function(err) {
// do something with the error
console.log(err);
});
}
// Utility function to parse URL query string values
var queryVals = (function(a) {
if (a === "") return {};
var b = {};
for (var i = 0; i < a.length; ++i)
{
var p=a[i].split('=', 2);
if (p.length === 1)
b[p[0]] = "";
else
b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}
return b;
})(window.location.search.substr(1).split('&'));
init();
// return public functions
return {
publish: publish,
update: update
};
}(this));