-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
132 lines (111 loc) · 3.18 KB
/
index.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
//https://www.youtube.com/watch?v=Kic2rnwU2_s
<h1>Tables</h1>
<form id="sign">
<input id="alias" placeholder="username">
<input id="pass" type="password" placeholder="passphrase">
<input id="in" type="submit" value="sign in">
<input id="up" type="button" value="sign up">
</form>
<button id="tadd">+table</button>
<ul></ul><br\>
<div id="table"></div>
<button id="radd">+ row</button>
<button id="cadd">+ col</button>
<style>
div {
position: relative;
overflow:hidden;
}
.cell {
width: 5em;
height: 5em;
border: 1px solid black;
float: left;
}
.item {
fload: left;
min-width: 5em;
}
</style>
<div class="model" style="display: none;">
<div class="item"></div>
<div class="cell"></div>
<div class="row"></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/gun/examples/jquery.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gun/gun.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gun/sea.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gun/lib/open.js"></script>
<script>
var gun =Gun(['http://llocalhost:8080/gun']);
//var gun =Gun();
var user = gun.user();
$('#up').on('click', function(e){
user.create($('#alias').val(),$('#pass').val());
});
$('#sign').on('submit', function(e){
e.preventDefault();
user.auth($('#alias').val(),$('#pass').val());
});
user.recall({sessionStroage: true});
gun.on('auth', function(){
$('#sign').hide();
user.get('tables').map().once(list);
});
$('#tadd').on('click', function(e){
if(!userr.is){return alert("login first")}
var cell =user.get('cells').set({what: "Edit me!", sort: 1});
var row = user.get('rows').set({sort: 1});
row.get('cells').set(cell);
var name = prompt("What do you want to call this table?")
var table = user.get('tables').set({name: name});
table.get('rows').set(row);
choose(table);
});
$(document).on('click', '.item', function(){
choose($(this).data('$'));
});
function choose(table){
choose.table = table;
table.open(render);
}
function render(data){
console.log(data);
$('#table').empty(); //BAD
Gun.obj.map(data.rows, function(row, id){
var $r = grab(id, '.model .row', '#tble');
Gun.obj.map(row.cells, function(cell, id){
var $c = grab(id, '.model .cell', $r);
$c.text(cell.what);
});
});
}
$(document).on('click', '.cell', function(e){
var id =$(this).data('id'), ref = gun.get(id);
var what = prompt("what should be in this vell");
ref.get('what').put(what);
});
$('#radd').on('click', function(){
if(!user.is){ return alert("loginfirst")}
choose.table.get('rows').set();
var cell = user.get('cells').set({what: "Edit me!", sort: 1});
var row = user.get('rows').set({sort: 1});
row.get('cells').set(cell);
choose.table.get('rows').set(row);
});
$('#cadd').on('click', function(){
if(!user.is){return alert("login first")}
choose.table.get('rows').map().once(function(){
var cell = user.get('cells').set({what: "Edit me!", sort: 1});
this.get('cells').set(cell);
});
});
function grab(id,$model, $to){
var did = btoa(id).replace(/[\+\-\=\.]/ig, '');
return $('#' +did).get(0) || $($model).clone(true,true).attr('id', did).appendTo($to).data('id', id);
};
function list(table, id, $model, $to){
var li= grab(id, '.model .item', 'ul');
$(li).text(table.name).data('$', this);
};
</script>