Skip to content
This repository was archived by the owner on Feb 7, 2023. It is now read-only.

Commit 0019d9b

Browse files
committed
Merge branch 'release-0.7.0'
1 parent 4548c90 commit 0019d9b

File tree

8 files changed

+532
-361
lines changed

8 files changed

+532
-361
lines changed
Lines changed: 137 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* Annotations Support for Patterns - v0.3
33
*
4-
* Copyright (c) 2013 Dave Olsen, http://dmolsen.com
4+
* Copyright (c) 2013-2014 Dave Olsen, http://dmolsen.com
55
* Licensed under the MIT license
66
*
77
*/
@@ -10,59 +10,57 @@ var annotationsPattern = {
1010

1111
commentsOverlayActive: false,
1212
commentsOverlay: false,
13-
commentsOverlayElement: "",
1413
commentsEmbeddedActive: false,
1514
commentsEmbedded: false,
15+
commentsGathered: { "commentOverlay": "on", "comments": { } },
16+
trackedElements: [ ],
1617

1718
/**
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
1920
*/
20-
showComments: function() {
21+
gatherComments: function() {
2122

2223
// 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+
2528
for (comment in comments.comments) {
2629
var item = comments.comments[comment];
2730
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) {
3639

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 = {};
4043

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 };
4246

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);
5549

5650
}
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+
}
6453
}
54+
55+
6556
}
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+
6664
}
6765

6866
},
@@ -109,10 +107,12 @@ var annotationsPattern = {
109107
*/
110108
findParent: function(el) {
111109

110+
var parentEl;
111+
112112
if (el.parentNode.classList.contains("sg-pattern")) {
113113
return el.parentNode;
114114
} else {
115-
var parentEl = annotationsPattern.findParent(el.parentNode);
115+
parentEl = annotationsPattern.findParent(el.parentNode);
116116
}
117117

118118
return parentEl;
@@ -131,64 +131,139 @@ var annotationsPattern = {
131131
return;
132132
}
133133

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;
135152

136153
// if this is an overlay make sure it's active for the onclick event
137154
annotationsPattern.commentsOverlayActive = false;
138155
annotationsPattern.commentsEmbeddedActive = false;
139156

140157
// 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)) {
143159
annotationsPattern.commentsEmbeddedActive = true;
144-
} else if (event.data.commentToggle == "on") {
160+
} else if (event.data.commentToggle === "on") {
145161
annotationsPattern.commentsOverlayActive = true;
146162
}
147163

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
149165
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";
153173
}
154174
}
155175

156176
// if comments embedding is turned off make sure to hide the annotations div
157177
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++) {
160180
els[i].style.display = "none";
161181
}
162182
}
163183

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
165185
if (annotationsPattern.commentsOverlayActive) {
166186

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];
169232
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 };
172243
}
244+
173245
}
174246

247+
// send the list of annotations for the page back to the parent
248+
parent.postMessage(annotationsPattern.commentsGathered,targetOrigin);
249+
175250
} else if (annotationsPattern.commentsEmbeddedActive && !annotationsPattern.commentsEmbedded) {
176251

177252
// 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);
181256
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
183258
}
184259
annotationsPattern.commentsEmbedded = true;
185260
}
186261

187262
} else if (annotationsPattern.commentsEmbeddedActive && annotationsPattern.commentsEmbedded) {
188263

189264
// 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) {
192267
els[i].style.display = "block";
193268
}
194269

@@ -201,12 +276,12 @@ var annotationsPattern = {
201276
};
202277

203278
// add the onclick handlers to the elements that have an annotations
204-
annotationsPattern.showComments();
279+
annotationsPattern.gatherComments();
205280
window.addEventListener("message", annotationsPattern.receiveIframeMessage, false);
206281

207282
// before unloading the iframe make sure any active overlay is turned off/closed
208283
window.onbeforeunload = function() {
209284
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;
211286
parent.postMessage(obj,targetOrigin);
212-
};
287+
};

0 commit comments

Comments
 (0)