Skip to content

Commit 9766899

Browse files
committed
refactoring
1 parent 4c8fafc commit 9766899

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

src/fixed-footnotes.js

+26-20
Original file line numberDiff line numberDiff line change
@@ -92,36 +92,40 @@ FixedFootnotes.prototype.removeRefreshListener = function(listener) {
9292
* From here: "private" methods that user is not supposed to call directly.
9393
*/
9494

95+
/*
96+
* Create the fixed container that will host the footnotes.
97+
*/
98+
FixedFootnotes.prototype._createFixedContainer = function() {
99+
var fixedContainer = this._window.document.createElement("div");
100+
fixedContainer.id = this.options.fixedContainerId;
101+
fixedContainer.className = this.options.fixedContainerClass + " fixed-footnotes-empty";
102+
fixedContainer.appendChild(this._window.document.createElement("ul"));
103+
this._window.document.querySelector(this.options.fixedContainerLocation).appendChild(fixedContainer);
104+
return fixedContainer;
105+
}
106+
95107
/*
96108
* Refresh the view.
97109
*/
98110
FixedFootnotes.prototype._refreshView = function() {
99111
var self = this;
100112
util.emptyElement(this._fixedContainerList);
101-
this._fixedContainer.className = this.options.fixedContainerClass + " fixed-footnotes-empty";
113+
var containerEmpty = true;
102114
this._getReferences().forEach(function(reference) {
103115
var note = self._getNoteFromRef(reference);
104-
if (!note) return;
105-
if (util.isElementInViewport(reference, self._window) && !util.isElementInViewport(note, self._window)) {
116+
if (self._shouldDisplayNoteFor(reference, note)) {
106117
self._displayNote(note);
107-
self._fixedContainer.className = self.options.fixedContainerClass;
118+
containerEmpty = false;
108119
}
109120
});
121+
if (containerEmpty) {
122+
this._fixedContainer.classList.add("fixed-footnotes-empty");
123+
} else {
124+
this._fixedContainer.classList.remove("fixed-footnotes-empty");
125+
}
110126
this._dispatchRefresh();
111127
};
112128

113-
/*
114-
* Create the fixed container that will host the footnotes.
115-
*/
116-
FixedFootnotes.prototype._createFixedContainer = function() {
117-
var fixedContainer = this._window.document.createElement("div");
118-
fixedContainer.id = this.options.fixedContainerId;
119-
fixedContainer.className = this.options.fixedContainerClass + " fixed-footnotes-empty";
120-
fixedContainer.appendChild(this._window.document.createElement("ul"));
121-
this._window.document.querySelector(this.options.fixedContainerLocation).appendChild(fixedContainer);
122-
return fixedContainer;
123-
}
124-
125129
/*
126130
* Get all the references.
127131
*/
@@ -130,11 +134,13 @@ FixedFootnotes.prototype._getReferences = function() {
130134
};
131135

132136
/*
133-
* Given a reference, display the footnote in the fixed container if the reference is on screen.
134-
* It won't display the footnote in the fixed container if the footnote is already on screen.
137+
* Return true if we should display the note of the given reference.
138+
* Note must exist, reference must be visible, and original note must not be visible.
135139
*/
136-
FixedFootnotes.prototype._displayIfVisible = function(reference) {
137-
140+
FixedFootnotes.prototype._shouldDisplayNoteFor = function(reference, note) {
141+
return note != undefined &&
142+
util.isElementInViewport(reference, this._window) &&
143+
!util.isElementInViewport(note, this._window);
138144
};
139145

140146
/*

0 commit comments

Comments
 (0)