1
1
/*!
2
- * Annotations Support for Patterns - v0.2
2
+ * Annotations Support for Patterns - v0.3
3
3
*
4
4
* Copyright (c) 2013 Dave Olsen, http://dmolsen.com
5
5
* Licensed under the MIT license
8
8
9
9
var annotationsPattern = {
10
10
11
- commentsActive : false ,
11
+ commentsOverlayActive : false ,
12
+ commentsOverlay : false ,
13
+ commentsOverlayElement : "" ,
14
+ commentsEmbeddedActive : false ,
15
+ commentsEmbedded : false ,
12
16
13
17
/**
14
18
* add an onclick handler to each element in the pattern that has an annotation
15
19
*/
16
20
showComments : function ( ) {
17
21
18
- for ( comment in comments . comments ) {
19
- var item = comments . comments [ comment ] ;
20
- var els = document . querySelectorAll ( item . el ) ;
21
- for ( var i = 0 ; i < els . length ; ++ i ) {
22
- els [ i ] . onclick = ( function ( item ) {
23
- return function ( e ) {
24
- if ( annotationsPattern . commentsActive ) {
22
+ // make sure this only added when we're on a pattern specific view
23
+ var body = document . getElementsByTagName ( "body" ) ;
24
+ if ( ! body [ 0 ] . classList . contains ( "pattern-list" ) ) {
25
+ for ( comment in comments . comments ) {
26
+ var item = comments . comments [ comment ] ;
27
+ var els = document . querySelectorAll ( item . el ) ;
28
+ for ( var i = 0 ; i < els . length ; ++ i ) {
29
+ els [ i ] . onclick = ( function ( item ) {
30
+ return function ( e ) {
25
31
e . preventDefault ( ) ;
26
32
e . stopPropagation ( ) ;
27
- var obj = { "el" : item . el , "title" : item . title , "comment" : item . comment } ;
33
+ var obj = { } ;
34
+
35
+ if ( annotationsPattern . commentsOverlayActive && ! annotationsPattern . commentsOverlay ) {
36
+
37
+ // if this is for an overlay and comments overlay is false set the payload to turn the overlay on
38
+ annotationsPattern . commentsOverlay = true ;
39
+ obj = { "commentOverlay" : "on" , "swapOverlay" : false , "el" : item . el , "title" : item . title , "comment" : item . comment } ;
40
+
41
+ } else if ( annotationsPattern . commentsOverlayActive && annotationsPattern . commentsOverlay ) {
42
+
43
+ if ( item . el == annotationsPattern . commentsOverlayElement ) {
44
+
45
+ // if the last element was clicked again turn off the overlay
46
+ annotationsPattern . commentsOverlay = false ;
47
+ obj = { "commentOverlay" : "off" } ;
48
+
49
+ } else {
50
+
51
+ // if an element was clicked on while the overlay was already on swap it
52
+ obj = { "commentOverlay" : "on" , "swapOverlay" : true , "el" : item . el , "title" : item . title , "comment" : item . comment } ;
53
+
54
+ }
55
+
56
+ }
57
+
58
+ annotationsPattern . commentsOverlayElement = item . el ;
28
59
var targetOrigin = ( window . location . protocol == "file:" ) ? "*" : window . location . protocol + "//" + window . location . host ;
29
60
parent . postMessage ( obj , targetOrigin ) ;
61
+
30
62
}
31
- }
32
- } ) ( item ) ;
63
+ } ) ( item ) ;
64
+ }
33
65
}
34
66
}
35
67
36
- } ,
68
+ } ,
69
+
70
+ /**
71
+ * embed a comment by building the sg-annotations div (if necessary) and building an sg-annotation div
72
+ * @param {Object } element to check the parent node of
73
+ * @param {String } the title of the comment
74
+ * @param {String } the comment HTML
75
+ */
76
+ embedComments : function ( el , title , comment ) {
77
+
78
+ // build the annotation div and add the content to it
79
+ var annotationDiv = document . createElement ( "div" ) ;
80
+ annotationDiv . classList . add ( "sg-annotation" ) ;
81
+
82
+ var h3 = document . createElement ( "h3" ) ;
83
+ var p = document . createElement ( "p" ) ;
84
+ h3 . innerHTML = title ;
85
+ p . innerHTML = comment ;
86
+
87
+ annotationDiv . appendChild ( h3 ) ;
88
+ annotationDiv . appendChild ( p ) ;
89
+
90
+ // find the parent element to attach things to
91
+ var parentEl = annotationsPattern . findParent ( el ) ;
92
+
93
+ // see if a child with the class annotations exists
94
+ var els = parentEl . getElementsByClassName ( "sg-annotations" ) ;
95
+ if ( els . length > 0 ) {
96
+ els [ 0 ] . appendChild ( annotationDiv ) ;
97
+ } else {
98
+ var annotationsDiv = document . createElement ( "div" ) ;
99
+ annotationsDiv . classList . add ( "sg-annotations" ) ;
100
+ annotationsDiv . appendChild ( annotationDiv ) ;
101
+ parentEl . appendChild ( annotationsDiv ) ;
102
+ }
103
+
104
+ } ,
105
+
106
+ /**
107
+ * recursively find the parent of an element to see if it contains the sg-pattern class
108
+ * @param {Object } element to check the parent node of
109
+ */
110
+ findParent : function ( el ) {
111
+
112
+ if ( el . parentNode . classList . contains ( "sg-pattern" ) ) {
113
+ return el . parentNode ;
114
+ } else {
115
+ var parentEl = annotationsPattern . findParent ( el . parentNode ) ;
116
+ }
117
+
118
+ return parentEl ;
119
+
120
+ } ,
37
121
38
122
/**
39
- * toggle the has-comment class on/off based on user clicking in the viewer UI
123
+ * toggle the annotation feature on/off
40
124
* based on the great MDN docs at https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage
41
125
* @param {Object } event info
42
126
*/
@@ -48,22 +132,81 @@ var annotationsPattern = {
48
132
}
49
133
50
134
if ( event . data . commentToggle != undefined ) {
51
- annotationsPattern . commentsActive = ( event . data . commentToggle == "on" ) ? true : false ;
52
- for ( comment in comments . comments ) {
53
- var item = comments . comments [ comment ] ;
54
- var els = document . querySelectorAll ( item . el ) ;
55
- for ( var i = 0 ; i < els . length ; ++ i ) {
56
- if ( event . data . commentToggle == "on" ) {
135
+
136
+ // if this is an overlay make sure it's active for the onclick event
137
+ annotationsPattern . commentsOverlayActive = false ;
138
+ annotationsPattern . commentsEmbeddedActive = false ;
139
+
140
+ // see which flag to toggle based on if this is a styleguide or view-all page
141
+ var body = document . getElementsByTagName ( "body" ) ;
142
+ if ( ( event . data . commentToggle == "on" ) && ( body [ 0 ] . classList . contains ( "pattern-list" ) ) ) {
143
+ annotationsPattern . commentsEmbeddedActive = true ;
144
+ } else if ( event . data . commentToggle == "on" ) {
145
+ annotationsPattern . commentsOverlayActive = true ;
146
+ }
147
+
148
+ // if comments overlay is turned off make sure to remove the has-comment class and pointer
149
+ if ( ! annotationsPattern . commentsOverlayActive ) {
150
+ var els = document . querySelectorAll ( ".has-comment" ) ;
151
+ for ( var i = 0 ; i < els . length ; i ++ ) {
152
+ els [ i ] . classList . remove ( "has-comment" ) ;
153
+ }
154
+ }
155
+
156
+ // if comments embedding is turned off make sure to hide the annotations div
157
+ if ( ! annotationsPattern . commentsEmbeddedActive ) {
158
+ var els = document . getElementsByClassName ( "sg-annotations" ) ;
159
+ for ( var i = 0 ; i < els . length ; i ++ ) {
160
+ els [ i ] . style . display = "none" ;
161
+ }
162
+ }
163
+
164
+ // if comments overlay is turned on add the has-comment class and pointer
165
+ if ( annotationsPattern . commentsOverlayActive ) {
166
+
167
+ for ( comment in comments . comments ) {
168
+ var item = comments . comments [ comment ] ;
169
+ var els = document . querySelectorAll ( item . el ) ;
170
+ for ( var i = 0 ; i < els . length ; i ++ ) {
57
171
els [ i ] . classList . add ( "has-comment" ) ;
58
- } else {
59
- els [ i ] . classList . remove ( "has-comment" ) ;
60
172
}
61
173
}
174
+
175
+ } else if ( annotationsPattern . commentsEmbeddedActive && ! annotationsPattern . commentsEmbedded ) {
176
+
177
+ // if comment embedding is turned on and comments haven't been embedded yet do it
178
+ for ( comment in comments . comments ) {
179
+ var item = comments . comments [ comment ] ;
180
+ var els = document . querySelectorAll ( item . el ) ;
181
+ for ( var i = 0 ; i < els . length ; ++ i ) {
182
+ annotationsPattern . embedComments ( els [ i ] , item . title , item . comment ) ;
183
+ }
184
+ annotationsPattern . commentsEmbedded = true ;
185
+ }
186
+
187
+ } else if ( annotationsPattern . commentsEmbeddedActive && annotationsPattern . commentsEmbedded ) {
188
+
189
+ // if comment embedding is turned on and comments have been embedded simply display them
190
+ var els = document . getElementsByClassName ( "sg-annotations" ) ;
191
+ for ( var i = 0 ; i < els . length ; ++ i ) {
192
+ els [ i ] . style . display = "block" ;
193
+ }
194
+
62
195
}
196
+
63
197
}
198
+
64
199
}
65
200
66
201
} ;
67
202
203
+ // add the onclick handlers to the elements that have an annotations
68
204
annotationsPattern . showComments ( ) ;
69
- window . addEventListener ( "message" , annotationsPattern . receiveIframeMessage , false ) ;
205
+ window . addEventListener ( "message" , annotationsPattern . receiveIframeMessage , false ) ;
206
+
207
+ // before unloading the iframe make sure any active overlay is turned off/closed
208
+ window . onbeforeunload = function ( ) {
209
+ var obj = { "commentOverlay" : "off" } ;
210
+ var targetOrigin = ( window . location . protocol == "file:" ) ? "*" : window . location . protocol + "//" + window . location . host ;
211
+ parent . postMessage ( obj , targetOrigin ) ;
212
+ } ;
0 commit comments