1
1
/*!
2
2
* Annotations Support for Patterns - v0.3
3
3
*
4
- * Copyright (c) 2013 Dave Olsen, http://dmolsen.com
4
+ * Copyright (c) 2013-2014 Dave Olsen, http://dmolsen.com
5
5
* Licensed under the MIT license
6
6
*
7
7
*/
@@ -10,59 +10,57 @@ var annotationsPattern = {
10
10
11
11
commentsOverlayActive : false ,
12
12
commentsOverlay : false ,
13
- commentsOverlayElement : "" ,
14
13
commentsEmbeddedActive : false ,
15
14
commentsEmbedded : false ,
15
+ commentsGathered : { "commentOverlay" : "on" , "comments" : { } } ,
16
+ trackedElements : [ ] ,
16
17
17
18
/**
18
- * add an onclick handler to each element in the pattern that has an annotation
19
+ * record which annotations are related to this pattern so they can be sent to the viewer when called
19
20
*/
20
- showComments : function ( ) {
21
+ gatherComments : function ( ) {
21
22
22
23
// 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 ( "sg-pattern-list" ) ) {
24
+ if ( document . getElementById ( "sg-patterns" ) === null ) {
25
+
26
+ var count = 0 ;
27
+
25
28
for ( comment in comments . comments ) {
26
29
var item = comments . comments [ comment ] ;
27
30
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 ) {
31
- e . preventDefault ( ) ;
32
- e . stopPropagation ( ) ;
33
- var obj = { } ;
34
-
35
- if ( annotationsPattern . commentsOverlayActive && ! annotationsPattern . commentsOverlay ) {
31
+ if ( els . length > 0 ) {
32
+
33
+ count ++ ;
34
+ item . displaynumber = count ;
35
+
36
+ for ( var i = 0 ; i < els . length ; ++ i ) {
37
+ els [ i ] . onclick = ( function ( item ) {
38
+ return function ( e ) {
36
39
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
+ e . preventDefault ( ) ;
41
+ e . stopPropagation ( ) ;
42
+ var obj = { } ;
40
43
41
- } else if ( annotationsPattern . commentsOverlayActive && annotationsPattern . commentsOverlay ) {
44
+ // if an element was clicked on while the overlay was already on swap it
45
+ obj = { "displaynumber" : item . displaynumber , "el" : item . el , "title" : item . title , "comment" : item . comment } ;
42
46
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
- }
47
+ var targetOrigin = ( window . location . protocol == "file:" ) ? "*" : window . location . protocol + "//" + window . location . host ;
48
+ parent . postMessage ( obj , targetOrigin ) ;
55
49
56
50
}
57
-
58
- annotationsPattern . commentsOverlayElement = item . el ;
59
- var targetOrigin = ( window . location . protocol == "file:" ) ? "*" : window . location . protocol + "//" + window . location . host ;
60
- parent . postMessage ( obj , targetOrigin ) ;
61
-
62
- }
63
- } ) ( item ) ;
51
+ } ) ( item ) ;
52
+ }
64
53
}
54
+
55
+
65
56
}
57
+
58
+ } else {
59
+
60
+ var obj = { "commentOverlay" : "off" } ;
61
+ var targetOrigin = ( window . location . protocol === "file:" ) ? "*" : window . location . protocol + "//" + window . location . host ;
62
+ parent . postMessage ( obj , targetOrigin ) ;
63
+
66
64
}
67
65
68
66
} ,
@@ -109,10 +107,12 @@ var annotationsPattern = {
109
107
*/
110
108
findParent : function ( el ) {
111
109
110
+ var parentEl ;
111
+
112
112
if ( el . parentNode . classList . contains ( "sg-pattern" ) ) {
113
113
return el . parentNode ;
114
114
} else {
115
- var parentEl = annotationsPattern . findParent ( el . parentNode ) ;
115
+ parentEl = annotationsPattern . findParent ( el . parentNode ) ;
116
116
}
117
117
118
118
return parentEl ;
@@ -131,64 +131,139 @@ var annotationsPattern = {
131
131
return ;
132
132
}
133
133
134
- if ( event . data . commentToggle != undefined ) {
134
+ var targetOrigin = ( window . location . protocol == "file:" ) ? "*" : window . location . protocol + "//" + window . location . host ;
135
+
136
+ if ( ( event . data . resize !== undefined ) && ( annotationsPattern . commentsOverlayActive ) ) {
137
+
138
+ for ( var i = 0 ; i < annotationsPattern . trackedElements . length ; ++ i ) {
139
+ var el = annotationsPattern . trackedElements [ i ] ;
140
+ if ( window . getComputedStyle ( el . element , null ) . getPropertyValue ( "max-height" ) == "0px" ) {
141
+ el . element . firstChild . style . display = "none" ;
142
+ parent . postMessage ( { "annotationState" : false , "displayNumber" : el . displayNumber } , targetOrigin ) ;
143
+ } else {
144
+ el . element . firstChild . style . display = "block" ;
145
+ parent . postMessage ( { "annotationState" : true , "displayNumber" : el . displayNumber } , targetOrigin ) ;
146
+ }
147
+ }
148
+
149
+ } else if ( event . data . commentToggle !== undefined ) {
150
+
151
+ var i , els , item , displayNum ;
135
152
136
153
// if this is an overlay make sure it's active for the onclick event
137
154
annotationsPattern . commentsOverlayActive = false ;
138
155
annotationsPattern . commentsEmbeddedActive = false ;
139
156
140
157
// 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 ( "sg-pattern-list" ) ) ) {
158
+ if ( ( event . data . commentToggle === "on" ) && ( document . getElementById ( "sg-patterns" ) !== null ) ) {
143
159
annotationsPattern . commentsEmbeddedActive = true ;
144
- } else if ( event . data . commentToggle == "on" ) {
160
+ } else if ( event . data . commentToggle === "on" ) {
145
161
annotationsPattern . commentsOverlayActive = true ;
146
162
}
147
163
148
- // if comments overlay is turned off make sure to remove the has-comment class and pointer
164
+ // if comments overlay is turned off make sure to remove the has-annotation class and pointer
149
165
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" ) ;
166
+ els = document . querySelectorAll ( ".has-annotation" ) ;
167
+ for ( i = 0 ; i < els . length ; i ++ ) {
168
+ els [ i ] . classList . remove ( "has-annotation" ) ;
169
+ }
170
+ els = document . querySelectorAll ( ".annotation-tip" ) ;
171
+ for ( i = 0 ; i < els . length ; i ++ ) {
172
+ els [ i ] . style . display = "none" ;
153
173
}
154
174
}
155
175
156
176
// if comments embedding is turned off make sure to hide the annotations div
157
177
if ( ! annotationsPattern . commentsEmbeddedActive ) {
158
- var els = document . getElementsByClassName ( "sg-annotations" ) ;
159
- for ( var i = 0 ; i < els . length ; i ++ ) {
178
+ els = document . getElementsByClassName ( "sg-annotations" ) ;
179
+ for ( i = 0 ; i < els . length ; i ++ ) {
160
180
els [ i ] . style . display = "none" ;
161
181
}
162
182
}
163
183
164
- // if comments overlay is turned on add the has-comment class and pointer
184
+ // if comments overlay is turned on add the has-annotation class and pointer
165
185
if ( annotationsPattern . commentsOverlayActive ) {
166
186
167
- for ( comment in comments . comments ) {
168
- var item = comments . comments [ comment ] ;
187
+ var count = 0 ;
188
+
189
+ for ( i = 0 ; i < comments . comments . length ; i ++ ) {
190
+ item = comments . comments [ i ] ;
191
+ els = document . querySelectorAll ( item . el ) ;
192
+
193
+ var state = true ;
194
+
195
+ if ( els . length ) {
196
+
197
+ count ++ ;
198
+
199
+ //Loop through all items with annotations
200
+ for ( k = 0 ; k < els . length ; k ++ ) {
201
+
202
+ els [ k ] . classList . add ( "has-annotation" ) ;
203
+
204
+ var span = document . createElement ( "span" ) ;
205
+ span . innerHTML = count ;
206
+ span . classList . add ( "annotation-tip" ) ;
207
+
208
+ if ( window . getComputedStyle ( els [ k ] , null ) . getPropertyValue ( "max-height" ) == "0px" ) {
209
+ span . style . display = "none" ;
210
+ state = false ;
211
+ }
212
+
213
+ annotationsPattern . trackedElements . push ( { "itemel" : item . el , "element" : els [ k ] , "displayNumber" : count , "state" : state } ) ;
214
+
215
+ els [ k ] . insertBefore ( span , els [ k ] . firstChild ) ;
216
+
217
+ }
218
+
219
+ }
220
+
221
+ }
222
+
223
+ // count elements so it can be used when displaying the results in the viewer
224
+ var count = 0 ;
225
+
226
+ // iterate over the comments in annotations.js
227
+ for ( i = 0 ; i < comments . comments . length ; i ++ ) {
228
+
229
+ var state = true ;
230
+
231
+ var item = comments . comments [ i ] ;
169
232
var els = document . querySelectorAll ( item . el ) ;
170
- for ( var i = 0 ; i < els . length ; i ++ ) {
171
- els [ i ] . classList . add ( "has-comment" ) ;
233
+
234
+ // if an element is found in the given pattern add it to the overall object so it can be passed when the overlay is turned on
235
+ if ( els . length > 0 ) {
236
+ count ++ ;
237
+ for ( k = 0 ; k < els . length ; k ++ ) {
238
+ if ( window . getComputedStyle ( els [ k ] , null ) . getPropertyValue ( "max-height" ) == "0px" ) {
239
+ state = false ;
240
+ }
241
+ }
242
+ annotationsPattern . commentsGathered . comments [ count ] = { "el" : item . el , "title" : item . title , "comment" : item . comment , "number" : count , "state" : state } ;
172
243
}
244
+
173
245
}
174
246
247
+ // send the list of annotations for the page back to the parent
248
+ parent . postMessage ( annotationsPattern . commentsGathered , targetOrigin ) ;
249
+
175
250
} else if ( annotationsPattern . commentsEmbeddedActive && ! annotationsPattern . commentsEmbedded ) {
176
251
177
252
// 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 ) ;
253
+ for ( i = 0 ; i < comments . comments . length ; i ++ ) {
254
+ item = comments . comments [ i ] ;
255
+ els = document . querySelectorAll ( item . el ) ;
181
256
if ( els . length > 0 ) {
182
- annotationsPattern . embedComments ( els [ 0 ] , item . title , item . comment ) ;
257
+ annotationsPattern . embedComments ( els [ 0 ] , item . title , item . comment ) ; //Embed the comment
183
258
}
184
259
annotationsPattern . commentsEmbedded = true ;
185
260
}
186
261
187
262
} else if ( annotationsPattern . commentsEmbeddedActive && annotationsPattern . commentsEmbedded ) {
188
263
189
264
// 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 ) {
265
+ els = document . getElementsByClassName ( "sg-annotations" ) ;
266
+ for ( i = 0 ; i < els . length ; ++ i ) {
192
267
els [ i ] . style . display = "block" ;
193
268
}
194
269
@@ -201,12 +276,12 @@ var annotationsPattern = {
201
276
} ;
202
277
203
278
// add the onclick handlers to the elements that have an annotations
204
- annotationsPattern . showComments ( ) ;
279
+ annotationsPattern . gatherComments ( ) ;
205
280
window . addEventListener ( "message" , annotationsPattern . receiveIframeMessage , false ) ;
206
281
207
282
// before unloading the iframe make sure any active overlay is turned off/closed
208
283
window . onbeforeunload = function ( ) {
209
284
var obj = { "commentOverlay" : "off" } ;
210
- var targetOrigin = ( window . location . protocol == "file:" ) ? "*" : window . location . protocol + "//" + window . location . host ;
285
+ var targetOrigin = ( window . location . protocol === "file:" ) ? "*" : window . location . protocol + "//" + window . location . host ;
211
286
parent . postMessage ( obj , targetOrigin ) ;
212
- } ;
287
+ } ;
0 commit comments