Skip to content

Commit 3998952

Browse files
committed
Remove jquery dependency from site/index.html
1 parent f6f4a95 commit 3998952

File tree

10 files changed

+183
-72
lines changed

10 files changed

+183
-72
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
site/js/vendor
22
site/js/plugins.js
33
site/*.html
4+
*auto.html
45
.github

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
},
2222
"types": "./dist/index.d.ts",
2323
"scripts": {
24-
"html": "node scripts/generate-examples.cjs",
24+
"html": "node site/js/examples-to-html.cjs",
2525
"build": "npx rimraf dist && npm run html && npm run docs && npm run build:typescript && npm run build:webpack && npm run build:site",
2626
"build:webpack": "NODE_ENV=production webpack",
2727
"build:typescript": "tsc && mkdir -p dist/samplers && cp src/samplers/*.mjs dist/samplers/",

site/index.html

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -197,53 +197,55 @@ <h1><span class="logo">$f(x)$</span> - Function Plot<br /></h1>
197197
</div>
198198
</div>
199199

200-
<!-- includes -->
201-
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
202200
<script>
203-
window.jQuery || document.write('<script src="js/vendor/jquery-1.11.2.min.js"><\/script>')
201+
MathJax = {
202+
tex: {
203+
inlineMath: [['$', '$'], ["\\(", "\\)"]],
204+
processEscapes: true,
205+
},
206+
svg: {
207+
fontCache: 'global'
208+
}
209+
};
204210
</script>
205-
<script src="js/plugins.js"></script>
211+
<script id="MathJax-script" src="https://cdn.jsdelivr.net/npm/mathjax@3.0.1/es5/tex-mml-chtml.js"></script>
206212

207213
<!-- app -->
208214
<script src="function-plot.js"></script>
209-
<script src="js/site.js"></script>
215+
<script src="js/examples.js"></script>
210216
<script>
211-
$('#recipes').load('partials/recipes.html')
212-
$('#examples').load('partials/examples.html', function () {
213-
// declared in js/site.js
214-
onSiteDependenciesLoaded()
215-
$('pre code').each(function (i, block) {
216-
hljs.highlightBlock(block)
217-
})
218-
219-
$('#p-slider').on('change', function () {
220-
var value = +this.value
221-
$('#p-slider-value').html(value)
222-
})
223-
})
224-
</script>
225-
<script
226-
type="text/javascript"
227-
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
228-
></script>
229-
<script type="text/x-mathjax-config">
230-
MathJax.Hub.Config({
231-
showProcessingMessages: false,
232-
messageStyle: 'none',
233-
tex2jax: {
234-
inlineMath: [['$','$']],
235-
displayMath: [['$$','$$']],
236-
processEnvironments: false
237-
},
238-
TeX: {
239-
equationNumbers: {
240-
autoNumber: "AMS"
241-
}
242-
},
243-
'HTML-CSS': {
244-
imageFont: null
217+
async function siteRenderExamples () {
218+
async function loadHtmlToId(path, target) {
219+
const response = await fetch(path);
220+
const body = await response.text();
221+
document.querySelector(target).innerHTML = body;
222+
}
223+
224+
await Promise.all([
225+
loadHtmlToId("partials/recipes.auto.html", '#recipes'),
226+
loadHtmlToId("partials/examples.auto.html", '#examples')
227+
])
228+
229+
// Render math
230+
MathJax.typesetPromise()
231+
232+
// render examples, declared in js/examples.js
233+
renderExamples()
234+
235+
// highlight code snippets
236+
document.querySelectorAll('pre code').forEach(function (el, i) {
237+
hljs.highlightBlock(el)
238+
})
239+
}
240+
241+
function ready(fn) {
242+
if (document.readyState !== 'loading') {
243+
fn();
244+
} else {
245+
document.addEventListener('DOMContentLoaded', fn);
246+
}
245247
}
246-
});
248+
ready(siteRenderExamples)
247249
</script>
248250
</body>
249251
</html>

scripts/generate-examples.cjs renamed to site/js/examples-to-html.cjs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/**
2-
* Created by mauricio on 4/9/15.
2+
* Performs a build time transformation of the examples
3+
* written as JavaScript
34
*/
5+
46
const fs = require('fs')
57
const dox = require('dox')
68
const _ = require('lodash')
@@ -12,7 +14,7 @@ const md = require('markdown-it')({
1214
})
1315

1416
function renderExamples() {
15-
const file = fs.readFileSync('./site/js/site.js', { encoding: 'utf-8' })
17+
const file = fs.readFileSync('./site/js/examples.js', { encoding: 'utf-8' })
1618
const comments = dox.parseComments(file)
1719

1820
const parsed = comments
@@ -46,16 +48,16 @@ function renderExamples() {
4648
return entry.ids
4749
})
4850

49-
const output = fs.createWriteStream('./site/partials/examples.html')
50-
output.write(pug.compileFile('./site/tpl/examples.pug')({ comments: parsed }))
51+
const output = fs.createWriteStream('./site/partials/examples.auto.html')
52+
output.write(pug.compileFile('./site/partials/examples.pug')({ comments: parsed }))
5153
output.end()
5254
}
5355

5456
function renderRecipes() {
55-
const file = fs.readFileSync('./site/tpl/recipes.md', { encoding: 'utf-8' })
57+
const file = fs.readFileSync('./site/partials/recipes.md', { encoding: 'utf-8' })
5658
const result = md.render(file)
5759

58-
const output = fs.createWriteStream('./site/partials/recipes.html')
60+
const output = fs.createWriteStream('./site/partials/recipes.auto.html')
5961
output.write(result)
6062
output.end()
6163
}

site/js/site.js renamed to site/js/examples.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function onSiteDependenciesLoaded() {
1+
function renderExamples() {
22
const functionPlot = window.functionPlot
33
let a, b, c
44

site/js/plugins.js

Lines changed: 0 additions & 24 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

site/partials/recipes.auto.html

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<h3>Evaluate a function at some value <code>x</code></h3>
2+
<pre><code class="language-javascript">const y = functionPlot.$eval.builtIn(datum, fnProperty, scope)
3+
</code></pre>
4+
<p>Where <code>datum</code> is an object that has a function to be evaluated in the property <code>fnProperty</code> ,
5+
to eval this function we need an <code>x</code> value which is sent through the scope</p>
6+
<p>e.g.</p>
7+
<pre><code class="language-javascript">const datum = {
8+
fn: 'x^2'
9+
}
10+
const scope = {
11+
x: 2
12+
}
13+
const y = functionPlot.$eval.builtIn(datum, 'fn', scope)
14+
</code></pre>
15+
<p>Every element of the <code>data</code> property sent to <code>functionPlot</code> is saved on <code>instance.options.data</code>,
16+
if you want to get the evaluated values of all the elements here run</p>
17+
<pre><code class="language-javascript">const instance = functionPlot( ... )
18+
instance.options.data.forEach(function (datum) {
19+
const datum = {
20+
fn: 'x^2'
21+
}
22+
const scope = {
23+
// a value for x
24+
x: 2
25+
}
26+
const y = functionPlot.$eval.builtIn(datum, 'fn', scope)
27+
}
28+
</code></pre>
29+
<h3>Maintain aspect ratio</h3>
30+
<p>Given the <code>xDomain</code> values you can compute the corresponding <code>yDomain</code> values to main
31+
the aspect ratio between the axes</p>
32+
<pre><code class="language-javascript">function computeYScale(width, height, xScale) {
33+
const xDiff = xScale[1] - xScale[0]
34+
const yDiff = (height * xDiff) / width
35+
return [-yDiff / 2, yDiff / 2]
36+
}
37+
38+
const width = 800
39+
const height = 400
40+
41+
// desired xDomain values
42+
const xScale = [-10, 10]
43+
44+
functionPlot({
45+
width: width,
46+
height: height,
47+
xDomain: xScale,
48+
yDomain: computeYScale(width, height, xScale),
49+
50+
target: '#demo',
51+
data: [
52+
{
53+
fn: 'x^2',
54+
derivative: {
55+
fn: '2x',
56+
updateOnMouseMove: true
57+
}
58+
}
59+
]
60+
})
61+
</code></pre>
62+
<h3>Changing the format of the values shown on the axes</h3>
63+
<pre><code class="language-javascript">const instance = functionPlot({
64+
target: '#complex-plane',
65+
xLabel: 'real',
66+
yLabel: 'imaginary'
67+
})
68+
// old format
69+
const format = instance.meta.yAxis.tickFormat()
70+
const imaginaryFormat = function (d) {
71+
// new format = old format + ' i' for imaginary
72+
return format(d) + ' i'
73+
}
74+
// update format
75+
instance.meta.yAxis.tickFormat(imaginaryFormat)
76+
// redraw the graph
77+
instance.draw()
78+
</code></pre>
79+
<h3>React Component</h3>
80+
<pre><code class="language-typescript">import React, { useEffect, useRef } from 'react'
81+
import functionPlot, { FunctionPlotOptions } from 'function-plot'
82+
83+
export interface FunctionPlotProps {
84+
options?: FunctionPlotOptions
85+
}
86+
87+
export const FunctionPlot: React.FC&lt;FunctionPlotProps&gt; = React.memo(({ options }) =&gt; {
88+
const rootEl = useRef(null)
89+
90+
useEffect(() =&gt; {
91+
try {
92+
functionPlot(Object.assign({}, options, { target: rootEl.current }))
93+
} catch (e) {}
94+
})
95+
96+
return (&lt;div ref={rootEl} /&gt;)
97+
}, () =&gt; false))
98+
</code></pre>
99+
<h3>Styling</h3>
100+
<p>Selectors (sass)</p>
101+
<pre><code class="language-sass">.function-plot {
102+
.x.axis {
103+
.tick {
104+
line {
105+
// grid's vertical lines
106+
}
107+
text {
108+
// x axis labels
109+
}
110+
}
111+
path.domain {
112+
// d attribute defines the graph bounds
113+
}
114+
}
115+
116+
.y.axis {
117+
.tick {
118+
line {
119+
// grid's horizontal lines
120+
}
121+
text {
122+
// y axis labels
123+
}
124+
}
125+
path.domain {
126+
// d attribute defines the graph bounds
127+
}
128+
}
129+
}
130+
</code></pre>
File renamed without changes.

0 commit comments

Comments
 (0)