forked from jeff-zucker/solid-content-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolid-ide.js
365 lines (357 loc) · 12.8 KB
/
solid-ide.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* VERSION 0.1.3
** 2019-03-30
*/
const sol = new SolidHandler() // from solid-ide-solidHandler.js
const fc = SolidFileClient; // from solid-file-client.bundle.js
var init = function(){
app.getStoredPrefs()
sol.get().then( results => {
app.processResults(results)
})
}
var app = new Vue({
el: '#app',
methods : {
//
// COMMUNICATE WITH THE SOLID SERVER
//
get : function(thing){
var oldThing = this.currentThing
this.currentThing = thing
view.hide();
sol.get(thing).then( results => {
if(sol.err){
alert(sol.err)
if(oldThing.url===thing.url)
oldThing.url = sol.homeUrl;
view.refresh(oldThing.url)
}
else this.processResults(results)
})
},
rm : function(f){
if(!this.perms.Control) return
if( confirm("DELETE RESOURCE "+f.url+"???") ){
view.hide('fileManager')
view.hide('folderManager')
var parentFolder =f.url.replace(f.name,'')
sol.rm( f.url ).then( success =>{
if(success){
alert("Resource deleted: " + f.url)
view.refresh(parentFolder)
}
else alert("Couldn't delete "+sol.err)
})
}
},
upload : async function(f){
view.hide('folderManager')
var inputFile = document.getElementById("upFile")
for (var i = 0; i < inputFile.files.length; i++) {
var content = inputFile.files[i]
var url = this.folder.url+content.name;
success = await sol.replace(url,content )
if(success){
alert("Resource created: " + content.name)
}
else alert("Couldn't create "+url+" "+sol.err)
}
view.refresh(this.folder.url)
},
addThing : function(type){
if(!this.newThing.name){
alert("You didn't supply a name!")
return;
}
view.hide('folderManager')
var name = this.newThing.name
var url = this.folder.url
sol.add(url,name,type ).then( success => {
if(success){
alert("Resource created: " + name)
view.refresh(this.folder.url)
}
else alert("Couldn't create "+url+" "+sol.err)
})
},
manageResource : function(thing){
if(!this.perms.Control) return
if(thing.type==="folder"){
this.folder = thing;
view.show('folderManager');
}
else {
this.file = thing;
view.show('fileManager');
}
},
getProfile : function(){
var url = this.webId.replace('#me','')
view.refresh( url )
},
download : function(f){
var a = document.createElement("a");
a.href = f.url
a.setAttribute("download", f.name);
var b = document.createEvent("MouseEvents");
b.initEvent("click", false, true);
a.dispatchEvent(b);
return false;
},
//
// EDITOR & FILE MANAGER SETTINGS
//
setEditKeys : function(){
fileDisplay.setEditKeys(this.editKeys)
},
setEditTheme : function(){
fileDisplay.setEditTheme(this.editTheme)
},
//
// LOGIN STATES
//
canControl : function(){
if( this.perms.Control ) return "canControl"
},
setLogState : function(){
if( this.loggedIn ){
this.logState = "login"
sol.webId=this.webId="";
fc.logout().then( res => {
view.refresh(sol.homeUrl)
})
}
else {
this.logState = "logout"
sol.homeUrl = this.homeUrl
fc.logout().then( ()=> {
fc.popupLogin().then(function(){
view.refresh()
})
})
}
},
getLogState : function(status){
var elm = document.getElementById('optionsButton')
if(status.loggedIn){
this.webId = status.webId
this.logState = "logout"; // logState is the button label
this.loggedIn = true; // loggedIn is true/false
}
else{
this.webId = ""
this.logState = "login";
this.loggedIn = false;
}
this.perms=status.permissions
},
//
// LOCAL STORAGE OF PREFERENCES
//
storePrefs : function(){
localStorage.setItem("solState", JSON.stringify({
home : this.homeUrl,
idp : sol.idp,
keys : this.editKeys,
theme : this.editTheme,
}))
},
getStoredPrefs : function(){
var state = localStorage.getItem("solState");
if(!state) {
sol.homeUrl = this.homeUrl =
"https://solside.solid.community/public/samples/"
sol.idp = this.idp = "https://solid.community"
return;
}
state = JSON.parse(state)
sol.homeUrl = this.homeUrl = state.home
sol.idp = this.idp = state.idp
this.editKeys = state.keys
this.editTheme = state.theme
fileDisplay.initEditor();
fileDisplay.setEditTheme(this.editTheme);
fileDisplay.setEditKeys(this.editKeys);
return state
},
//
// MAIN PROCESS LOOP, CALLED ON RETURN FROM ASYNC SOLID CALLS
//
processResults : function(results){
if(!results){
alert( sol.err )
return
}
var key = results.key
var val = results.value
if(key.match("folder")){
app.folder = val
app.currentThing = val
if(sol.qname) {
app.currentThing = sol.qname
sol.get(sol.qname).then( results => {
if(!results) alert(sol.err)
app.processResults(results)
})
}
else if(sol.hasIndexHtml) {
app.currentThing = {
url : val.url + "index.html",
type : "text/html"
}
sol.get(app.currentThing).then( results => {
if( !results ) alert(sol.err)
app.processResults(results)
})
}
}
if( val.type.match(/(image|audio|video)/) ){
val.content=""
}
fileDisplay.setContent(val.content)
fileDisplay.file.srcUrl = app.currentThing.url
sol.checkStatus(val.url).then( status => {
var url = location.href.replace(/^[^\?]*\?/,'')
var url2 = location.href.replace(url,'').replace(/\?$/,'')
if(url2) {
url2 = url2 + "?url="+encodeURI(val.url)
}
history.pushState({s:2,b:1},"solside",url2)
app.getLogState(status)
view.modUI(status,val.type)
}, err => { console.log(err)})
}, /* process results */
}, /* methods */
data: {
fontSize : "medium",
editKeys : "emacs",
editTheme : "dark theme",
perms : {},
currentThing : {},
newThing : {},
file : {},
folder : { name:'loading...' },
idp : "",
homeUrl : "",
webId : "",
logState : "login",
}, /* data */
}) /* app */
var fileDisplay = new Vue({
el : '#fileDisplay',
data : {file:{content:""},displayState:"both" },
methods : {
initEditor : function(){
this.zed = new SolidIdeEditor('editor');
var keys = app.editKeys || "emacs"
var theme = app.editTheme || "dark theme"
this.setEditKeys(keys);
this.setEditTheme(theme);
var size=14; //1260x720 iH = 581px; 1366x768 = 618px
if(window.innerHeight>600) size = 18
if(window.innerHeight>900) size = 22
if(this.displayState==="edOnly") size = size*2;
this.zed.setSize(size);
},
setEditKeys : function(keys){
var newKey ="zemacs";
if(keys==='vim') newKey ="vim"
this.zed.setKeys(newKey)
this.keys = newKey;
},
setEditTheme : function(theme){
var newTheme = "github"
if(theme.match("dark")){
newTheme = "monokai"
}
this.zed.setTheme(newTheme)
this.theme=newTheme
},
setContent : function(content){
// if(!this.zed) this.initEditor()
this.initEditor()
this.file = app.currentThing;
this.file.content = content;
if(!this.file.type && this.file.url)
this.file.type = sol.guessFileType(this.file.url)
this.zed.setModeFromType(this.file.type)
this.zed.setContents(content)
this.zed.ed.clearSelection() // remove blue overlay
},
saveEdits : function(){
sol.replace(
this.file.url,
this.zed.getContents()
).then( success => {
if(success){
alert("Resource saved: " + this.file.url)
view.refresh(this.file.url)
}
else alert("Couldn't save "+sol.err)
})
},
togglePanes : function(){
if(this.displayState==='edOnly'){
this.displayState="dataOnly";
this.initEditor()
return;
}
if(this.displayState==='dataOnly'){
this.displayState="both";
this.initEditor()
return;
}
if(this.displayState==='both'){
this.displayState="edOnly";
this.initEditor()
return;
}
},
}
})
var view = {
currentForm : "",
show : function(area){
this.currentForm = this.currentForm || area;
var x = document.getElementById(this.currentForm)
document.getElementById(this.currentForm).style.display = 'none';
this.currentForm = area;
document.getElementById(area).style.display = 'block';
document.getElementById('fileDisplay').style.display = 'none';
},
hide : function(area){
document.getElementById('fileDisplay').style.display = 'block';
area = area || this.currentForm;
if(area)
document.getElementById(area).style.display = 'none';
},
refresh : function(url) {
var url = url || app.currentThing.url
url = location.href.replace(/\/\??.*/,'').replace(/#$/,'')
+ "?url=" + url
url = url.replace(/\/\/$/,'/')
location.href = url
},
modUI : function(status,type){
var saveButton = document.getElementById('saveEdits')
var optionsButton = document.getElementById('optionsButton')
var profileButton = document.getElementById('profileButton')
var editDisabled = document.getElementById('editDisabled')
saveButton.style.display="none"
optionsButton.style.backgroundColor="#ddd"
profileButton.style.display="none"
editDisabled.style.display="table-cell"
if(status.loggedIn) {
optionsButton.style.backgroundColor = "rgba(145,200,220,2)";
profileButton.style.display="inline-block"
if( status.permissions.Write
&& !type.match(/(image|audio|video|folder)/)
){
saveButton.style.display = 'table-cell'
saveButton.style.backgroundColor = "rgba(145,200,220,2)"
editDisabled.style.display="none"
}
}
}
}
init()