/
refactored-responsive-annotations.js
183 lines (158 loc) · 6.35 KB
/
refactored-responsive-annotations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
$(function() {
// Storage of note model/properties.
var notes = {};
var resizeTimer; //set resizeTimer to empty so it resets on page load
//entry point
//caches note & image width, sets up resize handler
var init = function() {
findNotes();
//on resize, run the function and reset the timeout
//250 is the delay in milliseconds. Can be adjust as you see fit.
$(window).resize(function() {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(checkSize, 250);
});
}
var findNotes = function() {
$(".DC-note-container").each(function() { getCoordinates($(this)); /*swapURL(this);*/ });
}
//link notes to wapo embedder pages instead of document cloud
//var swapURL = function(note) {
// var note = $(note);
// if (note.data('wapo')) {
// var newLink = note.data('wapo');
// note.find('a').attr('href', newLink);
// note.find('a').attr('target', '_blank');
// }
//}
// gets default/canonical positioning data from the DOM for note & stores it.
var getCoordinates = function(noteEl) {
var noteID = $(noteEl).attr('id');
var wrapWidth = noteEl.find(".DC-note-excerpt").width();
var docWidth = noteEl.find(".DC-note-image").width();
var docTop = noteEl.find(".DC-note-image").position().top;
var lCoverWidth = noteEl.find(".DC-left-cover").width();
var rCoverWidth = noteEl.find(".DC-right-cover").width();
var excerptHeight = noteEl.find(".DC-note-excerpt").height();
var excerptLeft = noteEl.find(".DC-note-excerpt").position().left;
var excerptWidth = noteEl.find(".DC-note-excerpt").width();
if (docWidth != 0 && !(notes[noteID])) { //don't store notes that are set to display none
var note = {
x1: lCoverWidth,
x2: 750 - rCoverWidth,
y1: -1* docTop,
y2: -1* docTop + excerptHeight,
noteID: noteID,
wrapWidth: wrapWidth,
docWidth: docWidth,
docTop: docTop,
lCoverWidth: lCoverWidth,
rCoverWidth: rCoverWidth,
excerptHeight: excerptHeight,
excerptLeft: excerptLeft,
excerptWidth: excerptWidth,
noteWidth: 750 - (lCoverWidth + rCoverWidth)
}
notes[noteID] = note;
}
checkSize();
}
//diffs the note's natural width with its current width and decides what to do
var checkSize = function() {
$.each(notes, function() {
var noteElem = $("#" + this.noteID); // the notes lookup should just cache the element.
var newWrapWidth = noteElem.find('.DC-note-excerpt-wrap').width(); // Get viewable width
//if viewable width < left cover width + note width, kill left cover
//if viewable width < note width, scale!
//else, restoreDoc
( newWrapWidth < this.lCoverWidth+this.noteWidth ) ? resizeDoc(this.noteID) : restoreDoc(this.noteID);
});
}
//calculates scaling factor based on the new note width & updates the note element's attributes
var resizeDoc = function(noteID) {
//original values
var natNoteWidth = notes[noteID].noteWidth;
var natWrapWidth = notes[noteID].wrapWidth;
var natDocWidth = notes[noteID].docWidth;
//new values after resize of browser
var newWrapWidth = $("#" + noteID + " .DC-note-excerpt-wrap").width();
//calculate new width of document image
var newDocWidth = natDocWidth - natNoteWidth + newWrapWidth;
var scaling_factor = newDocWidth / natDocWidth;
var newExcerptWidth = (notes[noteID].excerptWidth) * scaling_factor;
var newExcerptHeight = (notes[noteID].excerptHeight) * scaling_factor;
//new x1,x2,y1,y2
var newX1 = (notes[noteID].x1) * scaling_factor;
var newX2 = (notes[noteID].x2) * scaling_factor;
//apply calculations to elements
scaleNoteImage(noteID, {
imageHeight: newExcerptHeight,
imageWidth: newDocWidth,
leftCoverWidth: newX1,
excerptWidth: newExcerptWidth,
rightCoverWidth: newExcerptWidth - newX2,
imageTop: (notes[noteID].docTop) * scaling_factor,
excerptLeft: -1 * newX1
});
}
//sets note element's attributes back to default
var restoreDoc = function(noteID) {
scaleNoteImage(noteID, {
imageHeight: notes[noteID].excerptHeight,
imageWidth: notes[noteID].docWidth,
leftCoverWidth: notes[noteID].lCoverWidth,
excerptWidth: notes[noteID].excerptWidth,
rightCoverWidth: notes[noteID].rCoverWidth,
imageTop: notes[noteID].docTop,
excerptLeft: notes[noteID].excerptLeft
});
}
var scaleNoteImage = function(noteID,dimensions){
// set heights for note images.
var noteEl = $("#"+noteID);
noteEl.find(".DC-note-excerpt").height(dimensions.imageHeight);
noteEl.find(".DC-left-cover" ).height(dimensions.imageHeight);
noteEl.find(".DC-right-cover" ).height(dimensions.imageHeight);
// Set widths of viewable area, left cover, note excerpt, and right cover.
noteEl.find(".DC-note-image" ).width(dimensions.imageWidth);
noteEl.find(".DC-left-cover" ).width(dimensions.leftCoverWidth);
noteEl.find(".DC-note-excerpt").width(dimensions.excerptWidth);
noteEl.find(".DC-right-cover" ).width(dimensions.rightCoverWidth);
// position left cover & note excerpt
noteEl.find(".DC-note-image" ).css('top', dimensions.imageTop);
noteEl.find(".DC-note-excerpt").css('left', dimensions.excerptLeft);
}
//footnote click
$(document).on('click', '.link-fn', function() {
var fnLink = $(this);
var fnLinkIcon = $(this).find('.fa');
var fnID = $(this).data('fn');
var footnote = $('#footnote' + fnID);
if (footnote.hasClass('show')) {
footnote.removeClass('show');
footnote.slideUp({
duration: 800,
easing: 'swing'
});
fnLink.removeClass('reverse');
} else {
footnote.addClass('show');
footnote.slideDown({
duration: 800,
easing: 'swing'
});
fnLink.addClass('reverse');
//add note to notes and run subsequent functions
var note = $(footnote).find('.DC-note-container');
getCoordinates(note);
}
if (fnLinkIcon.hasClass('fa-caret-down')) {
fnLinkIcon.removeClass('fa-caret-down').addClass('fa-caret-up');
} else {
fnLinkIcon.removeClass('fa-caret-up').addClass('fa-caret-down');
}
});
$(window).load(function() {
init();
});
});