1
+ const mod = import ( './comments' ) ;
2
+ let wasm ;
3
+ mod . then ( m => {
4
+ wasm = m ;
5
+ let button = document . getElementById ( 'add-comment-button' ) ;
6
+ if ( ! button ) return console . error ( 'Unable to find add button' ) ;
7
+ button . addEventListener ( 'click' , newComment ) ;
8
+ displayComments ( ) ;
9
+ } ) ;
10
+
11
+ /**
12
+ * Click event handler for add button
13
+ * @param {MouseEvent } ev
14
+ */
15
+ function newComment ( ev ) {
16
+ clearError ( ) ;
17
+ let name = document . getElementById ( 'name' ) ;
18
+ if ( ! name ) return console . error ( 'Failed to find name input' ) ;
19
+ if ( name . value == '' ) return displayError ( 'Name cannot be blank' ) ;
20
+ let comment = document . getElementById ( 'comment' ) ;
21
+ if ( ! comment ) return console . error ( 'Failed to find comment input' ) ;
22
+ if ( comment . value == '' ) return displayError ( 'comment cannot be blank' ) ;
23
+ addComment ( name . value , comment . value ) ;
24
+ name . form . reset ( ) ;
25
+ displayComments ( ) ;
26
+ }
27
+
28
+ /**
29
+ * Add a comment to the list
30
+ * @param {string } name The name of the person submitting
31
+ * @param {string } content The actual text of the comment
32
+ */
33
+ function addComment ( name , content ) {
34
+ let existing = comments ( ) ;
35
+ let count = existing . length ;
36
+ existing . push ( new wasm . Comment ( name , content , count ) ) ;
37
+ storeComments ( ) ;
38
+ }
39
+
40
+ /**
41
+ * Convert the rust comments to JSON comments and store
42
+ * in local storage
43
+ */
44
+ function storeComments ( ) {
45
+ let json = comments ( ) . map ( c => {
46
+ console . log ( 'mapping comments for storage' , c ) ;
47
+ return {
48
+ name : c . name ( ) ,
49
+ comment : c . comment ( ) ,
50
+ count : c . count ,
51
+ }
52
+ } ) ;
53
+ localStorage . setItem ( 'comments' , JSON . stringify ( json ) ) ;
54
+ }
55
+ /**
56
+ * Get the comments from local storage and convert them to
57
+ * rust comments
58
+ */
59
+ function getComments ( ) {
60
+ let json = localStorage . getItem ( 'comments' ) ;
61
+ if ( ! json ) return [ ] ;
62
+ let raw = JSON . parse ( json ) ;
63
+ return raw . map ( c => new wasm . Comment ( c . name , c . comment , c . count ) ) ;
64
+ }
65
+
66
+ /**A in memory cache of the localStorage comments for this site */
67
+ let cachedComments = null ;
68
+ /**
69
+ * Get a list of comments for this page
70
+ * @param {boolean } refresh force a refresh from localStorage
71
+ */
72
+ function comments ( refresh = false ) {
73
+ if ( refresh || ! cachedComments ) {
74
+ cachedComments = getComments ( ) ;
75
+ }
76
+ return cachedComments ;
77
+ }
78
+
79
+ /**
80
+ * Clear the comments section and re-render with the
81
+ * current comments list
82
+ */
83
+ function displayComments ( ) {
84
+ let node = document . getElementById ( 'comments' ) ;
85
+ if ( ! node ) return console . error ( 'Failed to get comments container' ) ;
86
+ while ( node . hasChildNodes ( ) ) {
87
+ node . removeChild ( node . lastChild ) ;
88
+ }
89
+ for ( let comment of comments ( ) ) {
90
+ node . appendChild ( renderComment ( comment ) ) ;
91
+ }
92
+ }
93
+
94
+ /**
95
+ * Generate the HTML needed to display a single comment
96
+ * @param {Comment } comment the comment to display
97
+ * @returns {HTMLDivElement } The div containing the comment html
98
+ */
99
+ function renderComment ( comment ) {
100
+ let cls = `comment ${ comment . color ( ) == wasm . Color . Blue ? 'blue' : 'pink' } ` ;
101
+ let div = document . createElement ( 'div' ) ;
102
+ div . setAttribute ( 'class' , cls ) ;
103
+ let top = document . createElement ( 'div' ) ;
104
+ top . setAttribute ( 'class' , 'comment-top' ) ;
105
+ let ct = document . createElement ( 'span' ) ;
106
+ ct . setAttribute ( 'class' , 'count' ) ;
107
+ ct . appendChild ( document . createTextNode ( `${ comment . count } :` ) ) ;
108
+ let name = document . createElement ( 'span' ) ;
109
+ name . setAttribute ( 'class' , 'user-name' ) ;
110
+ name . appendChild ( document . createTextNode ( `${ comment . name ( ) } ` ) ) ;
111
+ top . appendChild ( ct ) ;
112
+ top . appendChild ( name ) ;
113
+ let bottom = document . createElement ( 'div' ) ;
114
+ bottom . setAttribute ( 'class' , 'comment-bottom' ) ;
115
+ let title = document . createElement ( 'span' ) ;
116
+ title . setAttribute ( 'class' , 'comment-title' ) ;
117
+ title . appendChild ( document . createTextNode ( 'comment' ) ) ;
118
+ bottom . appendChild ( title ) ;
119
+ let text = document . createElement ( 'span' ) ;
120
+ text . setAttribute ( 'class' , 'comment-text' ) ;
121
+ text . appendChild ( document . createTextNode ( comment . comment ( ) ) )
122
+ bottom . appendChild ( text ) ;
123
+ div . appendChild ( top ) ;
124
+ div . appendChild ( bottom )
125
+ return div ;
126
+ }
127
+
128
+ function displayError ( message ) {
129
+ let e = document . getElementById ( 'error' ) ;
130
+ if ( ! e ) return console . error ( 'Failed to find error container' ) ;
131
+ if ( e . innerHTML != '' ) return setTimeout ( displayError , 1000 , message ) ;
132
+ e . innerHTML = message ;
133
+ setTimeout ( clearError , 3000 ) ;
134
+ }
135
+
136
+ function clearError ( ) {
137
+ let e = document . getElementById ( 'error' ) ;
138
+ if ( ! e ) return console . error ( 'Failed to find error container' ) ;
139
+ e . innerHTML = '' ;
140
+ }
0 commit comments