1
+ <!doctype html>
2
+ < html >
3
+
4
+ < head >
5
+ < title > Todo App: Gun + Vue</ title >
6
+
7
+ < script src ="https://unpkg.com/vue " type ="text/javascript " charset ="utf-8 "> </ script >
8
+ < script src ="http://rawgit.com/amark/gun/master/gun.js " type ="text/javascript " charset ="utf-8 "> </ script >
9
+ < link href ='https://fonts.googleapis.com/css?family=Alegreya+Sans:300italic ' rel ='stylesheet ' type ='text/css '>
10
+ < style >
11
+
12
+ body ,
13
+ html {
14
+ width : 100% ;
15
+ font-family : 'Alegreya Sans' , sans-serif;
16
+ }
17
+
18
+ body {
19
+ font-size : 20px ;
20
+ background-color : rgba (0 , 0 , 0 , .3 );
21
+ }
22
+
23
+ h1 ,
24
+ h3 ,
25
+ h5 {
26
+ text-transform : uppercase;
27
+ font-weight : 600 ;
28
+ }
29
+
30
+ # app {
31
+ margin : 0 auto;
32
+ background-color : # fff ;
33
+ max-width : 500px ;
34
+ padding : 2em ;
35
+ }
36
+
37
+ .add-todo {
38
+ font-size : 1em ;
39
+ border : 1px solid black;
40
+ padding : .5em .25em ;
41
+ }
42
+
43
+ .completed-header ,
44
+ .completed ,
45
+ .completed-time {
46
+ color : rgba (0 , 0 , 0 , .3 );
47
+ }
48
+
49
+ .completed {
50
+ text-decoration : line-through;
51
+ }
52
+
53
+ .completed-time {
54
+ font-size : .8em ;
55
+ }
56
+ </ style >
57
+ </ head >
58
+
59
+ < body >
60
+
61
+ < div id ="app ">
62
+
63
+ < div class ="todo-wrapper ">
64
+ < h1 > Todos App</ h1 >
65
+
66
+ < input class ="add-todo " type ="text " placeholder ="Add new todo " v-model ="newTodo " @keyup.enter ="addTodo ">
67
+
68
+ < h3 > Active</ h3 >
69
+
70
+ < div v-for ="active in activeTodos ">
71
+ < label > < input type ="checkbox " @click ="completeTodo(active.key) "> {{ active.todo.description }}</ label >
72
+ </ div >
73
+
74
+ < h5 v-show ="completedTodos.length > 0 " class ="completed-header "> Completed</ h5 >
75
+
76
+ < div v-for ="completed in completedTodos " @click ="reopenTodo(completed.key) ">
77
+ < span class ="completed "> {{ completed.todo.description }}</ span >
78
+ < span class ="completed-time "> (completed at {{ (new Date(completed.todo.completed)).toUTCString() }})</ span >
79
+ </ div >
80
+ </ div >
81
+
82
+ </ div >
83
+
84
+ </ body >
85
+
86
+ < script >
87
+
88
+ // Clear out local storage so we can start fresh each time.
89
+ localStorage . clear ( ) ;
90
+
91
+
92
+ // No peers for the sake of example,
93
+ // but this could easily be a collaborative
94
+ // todolist by adding a few peers
95
+ var gun = new Gun ( ) ;
96
+ var todos = gun . get ( 'todos' ) ;
97
+
98
+ new Vue ( {
99
+ data : {
100
+ todos : [ ] ,
101
+ newTodo : ""
102
+ } ,
103
+ computed : {
104
+ activeTodos : function ( ) {
105
+ return this . todos . filter ( function ( todo ) {
106
+ return todo . todo . completed === '' ;
107
+ } ) ;
108
+ } ,
109
+ completedTodos : function ( ) {
110
+ return this . todos . filter ( function ( todo ) {
111
+ return todo . todo . completed !== '' ;
112
+ } ) ;
113
+ }
114
+ } ,
115
+ methods : {
116
+ todoUpdated : function ( todo , nodeKey ) {
117
+ if ( / \D / . test ( nodeKey ) ) {
118
+ return ;
119
+ }
120
+
121
+ var existingTodoIndex = this . todos . findIndex ( function ( todo ) {
122
+ return todo . key === nodeKey
123
+ } ) ;
124
+ var todoToStore = {
125
+ todo : todo ,
126
+ key : nodeKey
127
+ } ;
128
+ if ( existingTodoIndex === - 1 ) {
129
+ this . todos . push ( todoToStore ) ;
130
+ } else {
131
+ this . todos . splice ( existingTodoIndex , 1 , todoToStore ) ;
132
+ }
133
+ } ,
134
+ addTodo : function ( ) {
135
+ var newTodo = todos . get ( Date . now ( ) . toString ( ) ) ;
136
+ newTodo . put ( {
137
+ description : this . newTodo ,
138
+ completed : ''
139
+ } ) ;
140
+ todos . set ( newTodo ) ;
141
+ this . newTodo = "" ;
142
+ } ,
143
+ completeTodo : function ( key ) {
144
+ todos . get ( key ) . put ( {
145
+ completed : Date . now ( )
146
+ } ) ;
147
+ } ,
148
+ reopenTodo : function ( key ) {
149
+ todos . get ( key ) . put ( {
150
+ completed : ''
151
+ } ) ;
152
+ }
153
+ } ,
154
+ mounted : function ( ) {
155
+
156
+ // Subscribe to updates on the todos set
157
+ todos . map ( ) . on ( this . todoUpdated . bind ( this ) ) ;
158
+ }
159
+ } ) . $mount ( '#app' ) ;
160
+
161
+ </ script >
162
+
163
+ </ html >
1 commit comments
kristianmandrup commentedon May 3, 2017
Sweet :) Thankz!