auto-collapse.js
11.5 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
!function(c) {
var s = c.config.menuSettings, r = {}, t = MathJax.Ajax.config.path;
t.a11y || (t.a11y = c.config.root + "/extensions/a11y");
var l = MathJax.Extension["auto-collapse"] = {
version: "1.6.0",
config: c.CombineConfig("auto-collapse", {
disabled: !1
}),
dependents: [],
Enable: function(t, e) {
s.autocollapse = !0, e && (r.autocollapse = !0), this.config.disabled = !1, MathJax.Extension.collapsible.Enable(!1, e),
t && c.Queue([ "Reprocess", c ], [ "CollapseWideMath", this ]);
},
Disable: function(t, e) {
s.autocollapse = !1, e && (r.autocollapse = !1), this.config.disabled = !0;
for (var n = this.dependents.length - 1; 0 <= n; n--) {
var o = this.dependents[n];
o.Disable && o.Disable(!1, e);
}
t && c.Queue([ "Rerender", c ]);
},
Dependent: function(t) {
this.dependents.push(t);
},
Startup: function() {
var t = MathJax.Extension.collapsible;
t && t.Dependent(this), c.postInputHooks.Add([ "Filter", l ], 150), c.Queue(function() {
return l.CollapseWideMath();
}), window.addEventListener ? window.addEventListener("resize", l.resizeHandler, !1) : window.attachEvent ? window.attachEvent("onresize", l.resizeHandler) : window.onresize = l.resizeHandler;
},
Filter: function(t, e, n) {
t.enriched && !this.config.disabled && ("block" === t.root.Get("display") || n.parentNode.childNodes.length <= 3) && (t.root.SRE = {
action: this.Actions(t.root)
});
},
Actions: function(t) {
var e = [];
return this.getActions(t, 0, e), this.sortActions(e);
},
getActions: function(t, e, n) {
if (!t.isToken && t.data) {
e++;
for (var o, i = 0, a = t.data.length; i < a; i++) {
t.data[i] && ((o = t.data[i]).collapsible ? (n[e] || (n[e] = []), n[e].push(o),
this.getActions(o.data[1], e, n)) : this.getActions(o, e, n));
}
}
},
sortActions: function(t) {
for (var e = [], n = 0, o = t.length; n < o; n++) t[n] && (e = e.concat(t[n].sort(this.sortActionsBy)));
return e;
},
sortActionsBy: function(t, e) {
return (t = t.data[1].complexity) < (e = e.data[1].complexity) ? -1 : e < t ? 1 : 0;
},
CollapseWideMath: function(t) {
if (!this.config.disabled) {
this.GetContainerWidths(t);
var e = c.getAllJax(t), n = {
collapse: [],
jax: e,
m: e.length,
i: 0,
changed: !1
};
return this.collapseState(n);
}
},
collapseState: function(t) {
for (var e = t.collapse; t.i < t.m; ) {
var n = t.jax[t.i], o = n.root.SRE;
if (t.changed = !1, o && o.action.length && (o.cwidth < o.m || o.cwidth > o.M)) {
var i = this.getActionWidths(n, t);
if (i) return i;
this.collapseActions(o, t), t.changed && e.push(n.SourceElement());
}
t.i++;
}
if (0 !== e.length) return 1 === e.length && (e = e[0]), c.Rerender(e);
},
collapseActions: function(t, e) {
for (var n = t.width, o = n, i = 1e6, a = t.action.length - 1; 0 <= a; a--) {
var s = t.action[a], r = s.selection;
n > t.cwidth ? (s.selection = 1, o = s.SREwidth, i = n) : s.selection = 2, n = s.SREwidth,
t.DOMupdate ? document.getElementById(s.id).setAttribute("selection", s.selection) : s.selection !== r && (e.changed = !0);
}
t.m = o, t.M = i;
},
getActionWidths: function(t, e) {
if (!t.root.SRE.actionWidths) {
MathJax.OutputJax[t.outputJax].getMetrics(t);
try {
this.computeActionWidths(t);
} catch (t) {
if (!t.restart) throw t;
return MathJax.Callback.After([ "collapseState", this, e ], t.restart);
}
e.changed = !0;
}
return null;
},
computeActionWidths: function(t) {
var e, n = t.root.SRE, o = n.action, i = {};
for (n.width = t.sreGetRootWidth(i), e = o.length - 1; 0 <= e; e--) o[e].selection = 2;
for (e = o.length - 1; 0 <= e; e--) {
var a = o[e];
null == a.SREwidth && (a.selection = 1, a.SREwidth = t.sreGetActionWidth(i, a));
}
n.actionWidths = !0;
},
GetContainerWidths: function(t) {
for (var e, n, o, i = c.getAllJax(t), a = MathJax.HTML.Element("span", {
style: {
display: "block"
}
}), s = [], r = 0, l = i.length; r < l; r++) o = (n = i[r]).root, SRE = o.SRE, SRE && SRE.action.length && (null == SRE.width && (n.sreGetMetrics(),
SRE.m = SRE.width, SRE.M = 1e6), (e = n.SourceElement()).previousSibling.style.display = "none",
e.parentNode.insertBefore(a.cloneNode(!1), e), s.push([ n, e ]));
for (r = 0, l = s.length; r < l; r++) n = s[r][0], (e = s[r][1]).previousSibling.offsetWidth && (n.root.SRE.cwidth = e.previousSibling.offsetWidth * n.root.SRE.em);
for (r = 0, l = s.length; r < l; r++) n = s[r][0], (e = s[r][1]).parentNode.removeChild(e.previousSibling),
e.previousSibling.style.display = "";
},
timer: null,
running: !1,
retry: !1,
saved_delay: 0,
resizeHandler: function(t) {
l.config.disabled || (l.running ? l.retry = !0 : (l.timer && clearTimeout(l.timer),
l.timer = setTimeout(l.resizeAction, 100)));
},
resizeAction: function() {
l.timer = null, l.running = !0, c.Queue(function() {
l.saved_delay = c.processSectionDelay, c.processSectionDelay = 0;
}, [ "CollapseWideMath", l ], [ "resizeCheck", l ]);
},
resizeCheck: function() {
l.running = !1, c.processSectionDelay = l.saved_delay, l.retry && (l.retry = !1,
setTimeout(l.resizeHandler, 0));
}
};
c.Register.StartupHook("End Extensions", function() {
null == s.autocollapse ? s.autocollapse = !l.config.disabled : l.config.disabled = !s.autocollapse,
c.Register.StartupHook("MathMenu Ready", function() {
r = MathJax.Menu.cookie;
var t, e = MathJax.Menu.ITEM, n = MathJax.Menu.menu, o = e.CHECKBOX([ "AutoCollapse", "Auto Collapse" ], "autocollapse", {
action: function(t) {
l[s.autocollapse ? "Enable" : "Disable"](!0, !0), MathJax.Menu.saveCookie();
}
}), i = (n.FindId("Accessibility") || {}).submenu;
i ? null !== (t = i.IndexOfId("AutoCollapse")) ? i.items[t] = o : (t = i.IndexOfId("CollapsibleMath"),
i.items.splice(t + 1, 0, o)) : (t = n.IndexOfId("CollapsibleMath"), n.items.splice(t + 1, 0, o));
function a() {
l[s.autocollapse ? "Enable" : "Disable"]();
}
MathJax.Extension.collapse ? a() : MathJax.Hub.Register.StartupHook("Auto Collapse Ready", a);
}, 25);
}, 25);
}(MathJax.Hub), MathJax.ElementJax.Augment({
sreGetMetrics: function() {
MathJax.OutputJax[this.outputJax].sreGetMetrics(this, this.root.SRE);
},
sreGetRootWidth: function(t) {
return MathJax.OutputJax[this.outputJax].sreGetRootWidth(this, t);
},
sreGetActionWidth: function(t, e) {
return MathJax.OutputJax[this.outputJax].sreGetActionWidth(this, t, e);
}
}), MathJax.OutputJax.Augment({
getMetrics: function() {},
sreGetMetrics: function(t, e) {
e.cwidth = 1e6, e.width = 0, e.em = 12;
},
sreGetRootWidth: function(t, e) {
return 0;
},
sreGetActionWidth: function(t, e, n) {
return 0;
}
}), MathJax.Hub.Register.StartupHook("HTML-CSS Jax Ready", function() {
MathJax.OutputJax["HTML-CSS"].Augment({
sreGetMetrics: function(t, e) {
e.width = t.root.data[0].HTMLspanElement().parentNode.bbox.w, e.em = 1 / t.HTMLCSS.em / t.HTMLCSS.scale;
},
sreGetRootWidth: function(t, e) {
var n = t.root.data[0].HTMLspanElement();
return e.box = n.parentNode, e.box.bbox.w;
},
sreGetActionWidth: function(t, e, n) {
return t.root.data[0].toHTML(e.box).bbox.w;
}
});
}), MathJax.Hub.Register.StartupHook("SVG Jax Ready", function() {
MathJax.OutputJax.SVG.Augment({
getMetrics: function(t) {
this.em = MathJax.ElementJax.mml.mbase.prototype.em = t.SVG.em, this.ex = t.SVG.ex,
this.linebreakWidth = t.SVG.lineWidth, this.cwidth = t.SVG.cwidth;
},
sreGetMetrics: function(t, e) {
e.width = t.root.SVGdata.w / 1e3, e.em = 1 / t.SVG.em;
},
sreGetRootWidth: function(t, e) {
return e.span = document.getElementById(t.inputID + "-Frame"), t.root.SVGdata.w / 1e3;
},
sreGetActionWidth: function(t, e, n) {
this.mathDiv = e.span, e.span.appendChild(this.textSVG);
try {
t.root.data[0].toSVG();
} catch (t) {
var o = t;
}
if (e.span.removeChild(this.textSVG), o) throw o;
return t.root.data[0].SVGdata.w / 1e3;
}
});
}), MathJax.Hub.Register.StartupHook("CommonHTML Jax Ready", function() {
MathJax.OutputJax.CommonHTML.Augment({
sreGetMetrics: function(t, e) {
e.width = t.root.CHTML.w, e.em = 1 / t.CHTML.em / t.CHTML.scale;
},
sreGetRootWidth: function(t, e) {
return e.span = document.getElementById(t.inputID + "-Frame").firstChild, e.tmp = document.createElement("span"),
e.tmp.className = e.span.className, t.root.CHTML.w / t.CHTML.scale;
},
sreGetActionWidth: function(t, e, n) {
e.span.parentNode.replaceChild(e.tmp, e.span), MathJax.OutputJax.CommonHTML.CHTMLnode = e.tmp;
try {
t.root.data[0].toCommonHTML(e.tmp);
} catch (t) {
var o = t;
}
if (e.tmp.parentNode.replaceChild(e.span, e.tmp), o) throw o;
return t.root.data[0].CHTML.w / t.CHTML.scale;
}
});
}), MathJax.Hub.Register.StartupHook("NativeMML Jax Ready", function() {
MathJax.OutputJax.NativeMML.Augment({
sreGetMetrics: function(t, e) {
var n = document.getElementById(t.inputID + "-Frame");
e.width = n.offsetWidth, e.em = 1, e.DOMupdate = !0;
},
sreGetRootWidth: function(t, e) {
return e.span = document.getElementById(t.inputID + "-Frame").firstChild, e.span.offsetWidth;
},
sreGetActionWidth: function(t, e, n) {
return document.getElementById(n.id).setAttribute("selection", 1), e.span.offsetWidth;
}
});
}), MathJax.Ajax.Require("[a11y]/collapsible.js"), MathJax.Hub.Register.StartupHook("Collapsible Ready", function() {
MathJax.Extension["auto-collapse"].Startup(), MathJax.Hub.Startup.signal.Post("Auto Collapse Ready"),
MathJax.Ajax.loadComplete("[a11y]/auto-collapse.js");
});