Skip to content

Commit 279de48

Browse files
Notepad examples (SolidOS#281)
* Add rest of Notepad examples * Completed notepad examples :) * fixed up errors Co-authored-by: Michiel de Jong <michiel@unhosted.org>
1 parent 20de0a6 commit 279de48

1 file changed

Lines changed: 273 additions & 17 deletions

File tree

examples/notepad/index.html

Lines changed: 273 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,289 @@
11
<!DOCTYPE HTML>
22
<html lang="en">
3-
<head>
4-
<meta charset='UTF-8'>
5-
<title>solid-ui UI.pad examples page</title>
6-
<script type="text/javascript" src="../../lib/webpack-bundle.js"></script>
7-
<script>
8-
const $ = document.querySelector.bind(document)
3+
4+
<head>
5+
<meta charset='UTF-8'>
6+
<title>solid-ui UI.pad examples page</title>
7+
<script type="text/javascript" src="../../lib/webpack-bundle.js"></script>
8+
9+
<script>
10+
function showData(functionName) {
11+
const viewDataElement = document.querySelector(`#viewData-${functionName}`)
12+
if (!viewDataElement) return
13+
viewDataElement.innerHTML = document.querySelector(`#data-${functionName}`).innerText
14+
.replace(/ /g, '')
15+
.replace(/</g, '&lt;')
16+
.replace(/>/g, '&gt;')
17+
}
18+
19+
function showSource(functionName) {
20+
document.querySelector(`#viewSource-${functionName}`).innerHTML = document.querySelector(`#script-${functionName}`).innerText
21+
.replace(/&/g, '&amp;')
22+
.replace(/</g, '&lt;')
23+
.replace(/>/g, '&gt;')
24+
.replace(/"/g, '&quot;')
25+
.replace(/'/g, '&#039;')
26+
}
27+
28+
function loadData(dataTag) {
29+
UI.rdf.parse(dataTag.innerText, UI.store, location.origin, 'text/turtle', error => {
30+
if (error) console.error(error)
31+
})
32+
}
33+
34+
Array.from(document.querySelectorAll('script[type="text/turtle"]')).forEach(dataTag => loadData(dataTag))
35+
36+
window.addEventListener('DOMContentLoaded', () => {
37+
Array.from(document.querySelectorAll('article')).forEach(title => {
38+
const section = title.id
39+
showData(section)
40+
showSource(section)
41+
})
42+
})
43+
</script>
44+
</head>
45+
46+
<body>
47+
48+
<h1>The Pad module</h1>
49+
50+
<p>
51+
On this page you'll find some examples from the Pad module. See <a
52+
href="https://solid.github.io/solid-ui/Documentation/api/modules/_pad_.html#pad">API
53+
documentation</a> for more info about how to use the pad module.
54+
</p>
55+
56+
<article id="notepad">
57+
<h2>
58+
<a href='#notepad'>
59+
<code>notepad</code>: Working with the Notepad
60+
</a>
61+
</h2>
62+
<p>
63+
The Notepad provides functionality to both create a new Notepad or view/edit an existing one.
64+
The code below shows an example of how to view an existing notepad.
65+
<ul>
66+
<li>Use the store and fetcher to fetch the notepad document.</li>
67+
<li>Provide the full path of the pad document you are fetching.</li>
68+
<li>Provide the URI of the author/editor of the document.</li>
69+
<li>Provide the full path of the subject for this pad. As you can see in the example below it is the same
70+
as the pad, except you also need to include '#this'.
71+
</li>
72+
<li>Optional: Provide a StatusArea property (HTMLDIVElement) to display error messages</li>
73+
</ul>
74+
</p>
75+
76+
<script id='script-notepad'>
77+
window.addEventListener('DOMContentLoaded', async (event) => {
78+
const kb = UI.store
79+
const fetcher = UI.rdf.fetcher(kb)
80+
const pad = UI.rdf.namedNode('https://sharonstrats.inrupt.net/public/edu.mit.solid.pane.pad/id1585026584938/index.ttl')
81+
const me = new UI.rdf.NamedNode('https://sharonstrats.inrupt.net/profile/card#me')
82+
const subject = UI.store.sym('https://sharonstrats.inrupt.net/public/edu.mit.solid.pane.pad/id1585026584938/index.ttl#this')
83+
const options = { exists: true }
84+
85+
await fetcher.load(pad.doc()) // if you don't have this you will get an inconsistency error
86+
const result = UI.pad.notepad(document, pad.doc(), subject, me, options)
87+
document.querySelector('#div-notepad').appendChild(result)
88+
})
989
</script>
10-
</head>
11-
<body>
12-
<h2>notepadToHTML</h2>
90+
<pre id='viewSource-notepad'></pre>
91+
<div id='div-notepad'></div>
92+
93+
In order to create a new notepad you need to change three things:
94+
<ul>
95+
<li>Provide user authentication. You can do this by by including solid-auth-client as explained in
96+
https://solidproject.org/for-developers/apps/first-app/1-authentication </li>
97+
<li>Change the exists flag on the options object to <code>false</code>.</li>
98+
<li>Use the updater to create the document in the store, instead of using the <code>fetcher.load()</code> function
99+
above. The code below is a simple example (without error handling) to give you an idea of how this works.
100+
<code> kb.updater.put(pad, [], 'text/turtle', function (pad, subject, me, options) {
101+
const result = UI.pad.notepad(document, pad, subject, me, options)
102+
document.querySelector('#div-notepad').appendChild(result)
103+
}</code></li>
104+
</ul>
105+
</article>
106+
<article id="notepadToHTML">
107+
<h2>
108+
<a href="#notepadToHTML">
109+
<code>notepadToHTML</code>: Converts a Turtle file to an HTML
110+
</a>
111+
</h2>
112+
<p>
113+
This method converts a pad document to HTML so that it can be exported.
114+
</p>
115+
13116
<script id="script-notepadToHTML">
117+
14118
window.addEventListener('DOMContentLoaded', async (event) => {
15119
const kb = UI.store
16120
const fetcher = UI.rdf.fetcher(kb)
17-
const pad = kb.sym(
18-
document.location.href.replace(/\/[^\/]*$/,"/pad.ttl#this")
19-
)
121+
const pad = UI.rdf.namedNode('https://sharonstrats.inrupt.net/public/edu.mit.solid.pane.pad/id1584238219755/index.ttl#this')
20122
await fetcher.load(pad.doc())
21123
const htmlStr = UI.pad.notepadToHTML(pad, kb)
22-
const doc = $('#iframe-notepadToHTML').contentWindow.document;
124+
const doc = document.querySelector('#iframe-notepadToHTML').contentWindow.document
23125
doc.open()
24126
doc.write(htmlStr)
25127
doc.close()
26-
$('#div-notepadToHTML').appendChild(document.createTextNode(htmlStr))
27128
})
28129
</script>
29-
<pre id="viewSource-notepadToHTML"></pre><script>$('#viewSource-notepadToHTML').innerHTML = $('#script-notepadToHTML').innerText</script>
130+
<pre id="viewSource-notepadToHTML"></pre>
30131
<iframe id="iframe-notepadToHTML" src="about:blank"></iframe>
31132
<div id="div-notepadToHTML"></div>
32-
</body>
33-
</html>
133+
</article>
134+
135+
<article id="lightColorHash">
136+
<h2>
137+
<a href="#lightColorHash">
138+
<code>lightColorHash</code> : Returns a color in hex format calculated by hashing a WebID
139+
</a>
140+
</h2>
141+
<p>This method returns a hex value for setting a unique color on the input box of the notepad for a given
142+
editor/user.
143+
It does this by hashing the WebID passed in as an argument. Ideally this would be the WebID of the person
144+
logged in. However, this function could be used in any situation where you need to have a unique color to style
145+
a component based on WebIDs.
146+
</p>
147+
<p>The example below shows how you can use the lightColorHash function.</p>
148+
149+
<script id="script-lightColorHash">
150+
window.addEventListener('DOMContentLoaded', (event) => {
151+
const colorStr = UI.pad.lightColorHash(new UI.rdf.NamedNode('https://sharonstrats.inrupt.net/profile/card#me'))
152+
const inputBox = document.createElement('INPUT')
153+
inputBox.setAttribute("type", "text")
154+
inputBox.style.backgroundColor = colorStr
155+
document.querySelector('#div-lightColorHash').appendChild(inputBox)
156+
157+
})
158+
</script>
159+
<pre id="viewSource-lightColorHash"></pre>
160+
<div id="div-lightColorHash"></div>
161+
</article>
162+
163+
164+
<article id='manageParticipation'>
165+
<h2>
166+
<a href='#manageParticipation'>
167+
<code>manageParticipation</code>: Creates the table to display the participants
168+
</a>
169+
</h2>
170+
<p>ManageParticpation manages the pad document editors. </p>
171+
<p>The example below shows how to use this function. </p>
172+
<ul>
173+
<li>Provide the HTMLTableElement or HTMLDivElement on which to render the participants.</li>
174+
<li>Provide the full path to the pad document.</li>
175+
<li>Provide the full path to the subject, which is the pad document + '#this'.</li>
176+
<li>Optional: There are three properties that can be set on the options object.</li>
177+
<ul>
178+
<li>statusArea - a place to show error messages from pad.</li>
179+
<li>draggable - set this to true in order to make the participant draggable.</li>
180+
<li>deleteFunction - provide a delete function to enable the delete functionality.</li>
181+
<li>link - this should create a link to the WebID.</li>
182+
</ul>
183+
</ul>
184+
<script id='script-manageParticipation'>
185+
window.addEventListener('DOMContentLoaded', (event) => {
186+
const me = new UI.rdf.NamedNode('https://example-user.inrupt.net/profile/card#me')
187+
const structure = document.createElement('div')
188+
const padMessages = document.createElement('div')
189+
const subject = UI.store.sym('https://example-user.inrupt.net/public/example-notepad/index.ttl#this')
190+
const pad = UI.rdf.namedNode('https://example-user.inrupt.net/public/example-notepad/index.ttl')
191+
options = { statusArea: padMessages }
192+
// const result = UI.pad.manageParticipation(document, structure, pad.doc(), subject, me, options)
193+
// document.querySelector('#div-manageParticipation').appendChild(result)
194+
})
195+
</script>
196+
<pre id='viewSource-manageParticipation'></pre>
197+
<div id='div-manageParticipation'></div>
198+
</article>
199+
200+
<article id="renderPartipants">
201+
<h2>
202+
<a href="#renderPartipants">
203+
<code>renderPartipants</code>: Creates a table of participants on a given notepad document
204+
205+
</a>
206+
</h2>
207+
<p>Creates the table with the list of participants that have contributed to
208+
the given pad. This is used by manageParticipation, but can be used on it's own as well. </p>
209+
210+
<p>The example below shows how to use this function, however the function call
211+
is commented out because it will add a triple each time this page is rendered just as the renderParticpants
212+
function below. </p>
213+
<ul>
214+
<li>Use the fetcher to load the pad document.</li>
215+
<li>Provide the HTMLTableElement on which to render the participants.</li>
216+
<li>Provide the full path to the pad document.</li>
217+
<li>Provide the full path to the subject, which is the pad document + '#this'.</li>
218+
<li>Optional: There are three properties that can be set on the options object.</li>
219+
<ul>
220+
<li>draggable - set this to true in order to make the participant draggable.</li>
221+
<li>deleteFunction - provide a delete function to enable the delete functionality.</li>
222+
<li>link - this should create a link to the WebID.</li>
223+
</ul>
224+
</ul>
225+
<script id="script-renderPartipants">
226+
window.addEventListener('DOMContentLoaded', async (event) => {
227+
const kb = UI.store
228+
const fetcher = UI.rdf.fetcher(kb)
229+
const table = document.createElement('table');
230+
const pad = UI.rdf.namedNode('https://sharonstrats.inrupt.net/public/edu.mit.solid.pane.pad/id1585026584938/index.ttl')
231+
const subject = kb.sym('https://sharonstrats.inrupt.net/public/edu.mit.solid.pane.pad/id1585026584938/index.ttl#this')
232+
const options = { draggable: true, link: true, deleteFunction: () => alert("deleted") }
233+
await fetcher.load(pad.doc())
234+
const tableOfParticipants = UI.pad.renderPartipants(document, table, pad, subject, new UI.rdf.NamedNode('https://sharonstrats.inrupt.net/profile/card#me'), options)
235+
document.querySelector('#div-renderPartipants').appendChild(tableOfParticipants)
236+
})
237+
</script>
238+
<pre id='viewSource-renderPartipants'></pre>
239+
<div id='div-renderPartipants'></div>
240+
</article>
241+
242+
<article id="participationObject">
243+
<h2> <a href="#participationObject">
244+
<code>participationObject</code>: Contains the subject, pad and WebID for the participants
245+
</a>
246+
</h2>
247+
<p></p>
248+
<script id='script-participationObject'>
249+
window.addEventListener('DOMContentLoaded', async (event) => {
250+
const kb = UI.store
251+
const subject = kb.sym('https://sharonstrats.inrupt.net/public/edu.mit.solid.pane.pad/id1584238219755/index.ttl#this');
252+
const fetcher = UI.rdf.fetcher(kb)
253+
const pad = UI.rdf.namedNode('https://sharonstrats.inrupt.net/public/edu.mit.solid.pane.pad/id1584238219755/index.ttl')
254+
255+
await fetcher.load(pad.doc())
256+
const me = new UI.rdf.NamedNode('https://sharonstrats.inrupt.net/profile/card#me')
257+
const htmlStr = await UI.pad.participationObject(subject, pad, me);
258+
document.querySelector('#div-participationObject').appendChild(document.createTextNode(htmlStr))
259+
})
260+
</script>
261+
<pre id="viewSource-participationObject"></pre>
262+
<div id="div-participationObject"></div>
263+
</article>
264+
265+
<article id="recordParticipation">
266+
<h2> <a href="#recordParticipation">
267+
<code>recordParticipation</code>: Adds the WebID of the user to the participant store
268+
</a></h2>
269+
<p>Records the participants in the store for the given document and returns the participation object.
270+
This is also used in manageParticipation, but could also be used on it's own to create a particpation triple. </p>
271+
<p>The code below provides an example of the code you need to write to use this function, however the function call
272+
is commented out because it will add a triple each time this page is rendered. </p>
273+
<script id="script-recordParticipation">
274+
window.addEventListener('DOMContentLoaded', async (event) => {
275+
const kb = UI.store
276+
const subject = kb.sym('https://sharonstrats.inrupt.net/public/edu.mit.solid.pane.pad/id1585026584938/index.ttl#this');
277+
const fetcher = UI.rdf.fetcher(kb)
278+
const pad = UI.rdf.namedNode('https://sharonstrats.inrupt.net/public/example-notepad/index.ttl#this')
279+
await fetcher.load(pad.doc())
280+
// const htmlStr = UI.pad.recordParticipation(subject, pad, false);
281+
})
282+
</script>
283+
<pre id="viewSource-recordParticipation"></pre>
284+
<div id="div-recordParticipation"></div>
285+
</article>
286+
287+
</body>
288+
289+
</html>

0 commit comments

Comments
 (0)