|
1 | | -/* tslint:disable */ |
| 1 | +/* tslint:disable:no-unused-variable */ |
2 | 2 |
|
3 | 3 | declare const BASEURL, hljs; |
4 | 4 |
|
5 | | -function example(name, dir) { |
6 | | - const fullUrl = BASEURL + '/examples/' + (dir ? (dir + '/') : '') + name + '.json'; |
| 5 | +function renderExample(name: string, text: string) { |
| 6 | + const target = '#ex-' + name.replace('/', '-'); |
| 7 | + const $target = d3.select(target); |
| 8 | + $target.classed('example', true); |
7 | 9 |
|
8 | | - d3.text(fullUrl, function(error, text) { |
9 | | - const target = '#ex-' + name.replace('/', '-'); |
10 | | - const $target = d3.select(target); |
11 | | - $target.classed('example', true); |
| 10 | + const code = $target.insert('pre', 'div.example-vis').attr('class', 'example-code') |
| 11 | + .append('code').attr('class', 'json').text(text); |
| 12 | + hljs.highlightBlock(code.node()); |
12 | 13 |
|
13 | | - const code = $target.insert('pre', "div.example-vis").attr('class', 'example-code') |
14 | | - .append('code').attr('class', 'json').text(text); |
15 | | - hljs.highlightBlock(code.node()); |
| 14 | + let vis = $target.select('div.example-vis'); |
| 15 | + if (vis.empty()) { |
| 16 | + vis = $target.append('div').attr('class', 'example-vis'); |
| 17 | + } |
16 | 18 |
|
17 | | - let vis = $target.select('div.example-vis'); |
18 | | - if (vis.empty()) { |
19 | | - vis = $target.append('div').attr('class', 'example-vis'); |
20 | | - } |
| 19 | + const spec = JSON.parse(text); |
| 20 | + if (spec.data.url) { |
| 21 | + spec.data.url = BASEURL + '/' + spec.data.url; |
| 22 | + } |
21 | 23 |
|
22 | | - const spec = JSON.parse(text); |
23 | | - if (spec.data.url) { |
24 | | - spec.data.url = BASEURL + '/' + spec.data.url; |
| 24 | + vg.embed(vis.node(), { |
| 25 | + mode: 'vega-lite', |
| 26 | + spec: spec, |
| 27 | + renderer: 'svg', |
| 28 | + actions: { |
| 29 | + source: false, |
| 30 | + export: false |
25 | 31 | } |
| 32 | + }); |
| 33 | +} |
26 | 34 |
|
27 | | - vg.embed(vis.node(), { |
28 | | - mode: 'vega-lite', |
29 | | - spec: spec, |
30 | | - renderer: 'svg', |
31 | | - actions: { |
32 | | - source: false, |
33 | | - export: false |
| 35 | +/** |
| 36 | + * Show example in the decs. Provide the name of the example or a spec. |
| 37 | + */ |
| 38 | +function example(name: string, dirOrSpec) { |
| 39 | + if (dirOrSpec !== null && typeof dirOrSpec === 'object') { |
| 40 | + const spec = JSON3.stringify(dirOrSpec, null, 2, 80); |
| 41 | + renderExample(name, spec); |
| 42 | + } else { |
| 43 | + const dir = dirOrSpec; |
| 44 | + const fullUrl = BASEURL + '/examples/' + (dir ? (dir + '/') : '') + name + '.json'; |
| 45 | + |
| 46 | + d3.text(fullUrl, function(error, text) { |
| 47 | + if (error) { |
| 48 | + console.error(error); |
| 49 | + } else { |
| 50 | + renderExample(name, text); |
34 | 51 | } |
35 | 52 | }); |
36 | | - }); |
| 53 | + } |
37 | 54 | } |
0 commit comments