This repository was archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathd3-view.html
More file actions
755 lines (566 loc) · 31.1 KB
/
Copy pathd3-view.html
File metadata and controls
755 lines (566 loc) · 31.1 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
<!DOCTYPE html>
<html data-debug='false' lang='en' prefix='og: http://ogp.me/ns#'>
<head>
<title>GiottoJs</title>
<meta charset='utf-8'>
<meta content='IE=edge' http-equiv='X-UA-Compatible'>
<meta content='width=device-width, initial-scale=1' name='viewport'>
<meta content='#ffffff' name='msapplication-TileColor'>
<meta content='/icons/ms-icon-144x144.png' name='msapplication-TileImage'>
<meta content='#ffffff' name='theme-color'>
<meta content='GiottoJS is a javascript visualization library built on top of d3js. It is designed to visualize both SVG and Canvas elements with a simple API. AngularJS integration' name='description'>
<meta content='eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJMT0NBTEUiOiJlbl9HQiIsInBhZ2UiOnsiZ3JvdXAiOiJzaXRlIiwic2x1ZyI6ImQzLXZpZXciLCJib2R5X3RlbXBsYXRlIjoiaG9tZS5odG1sIiwiYm9keV9sZW5ndGgiOjI2NjQ0LCJwYXRoIjoiL2QzLXZpZXciLCJhcGlfdXJsIjoiaHR0cHM6Ly9naW90dG9qcy5vcmcvYXBpL2NvbnRlbnRzL3NpdGUvZDMtdmlldyIsImlubmVyX3RlbXBsYXRlIjoicGFydGlhbHMvYmFzZS5odG1sIn0sImRlYnVnIjpmYWxzZSwiTUlOSUZJRURfTUVESUEiOnRydWUsIkNPUFlSSUdIVCI6Ikx1eCIsIkFQUF9OQU1FIjoiR2lvdHRvSnMiLCJEQVRFVElNRV9GT1JNQVQiOiJkIE1NTSB5LCBoOm1tOnNzIGEiLCJBUElfVVJMIjoiL2FwaSIsIk1FRElBX1VSTCI6Ii9tZWRpYS8iLCJEQVRFX0ZPUk1BVCI6ImQgTU1NIHkiLCJsdXhfdmVyc2lvbiI6IjAuOC4yLmRldjIwMTYxMjAyMTIwMjAwIn0.1pKqPv3AkQ8hBCz63zQQDfkmj_KR6Zqxv6FdfP4MLeI' name='html-context'>
<meta content='website' property='og:type'>
<meta content='https://giottojs.org/d3-view' property='og:url'>
<meta content='GiottoJs' property='og:title'>
<meta content='GiottoJS is a javascript visualization library built on top of d3js. It is designed to visualize both SVG and Canvas elements with a simple API. AngularJS integration' property='og:description'>
<meta content='en_GB' property='og:locale'>
<meta content='GiottoJs' property='og:site_name'>
<meta content='https://giottojs.org/giotto-banner.png' property='og:image'>
<meta content='summary' name='twitter:card'>
<meta content='@giottojs_org' name='twitter:site'>
<meta content='GiottoJs' name='twitter:title'>
<meta content='GiottoJS is a javascript visualization library built on top of d3js. It is designed to visualize both SVG and Canvas elements with a simple API. AngularJS integration' name='twitter:description'>
<meta content='https://giottojs.org/giotto-banner.png' name='twitter:image'>
<link href='https://giottojs.org/d3-view' rel='canonical'>
<link href='/giottojs-default.css' rel='stylesheet' type='text/css'>
<link href='/icons/apple-icon-57x57.png' rel='apple-touch-icon' sizes='57x57'>
<link href='/icons/apple-icon-60x60.png' rel='apple-touch-icon' sizes='60x60'>
<link href='/icons/apple-icon-72x72.png' rel='apple-touch-icon' sizes='72x72'>
<link href='/icons/apple-icon-76x76.png' rel='apple-touch-icon' sizes='76x76'>
<link href='/icons/apple-icon-114x114.png' rel='apple-touch-icon' sizes='114x114'>
<link href='/icons/apple-icon-120x120.png' rel='apple-touch-icon' sizes='120x120'>
<link href='/icons/apple-icon-144x144.png' rel='apple-touch-icon' sizes='144x144'>
<link href='/icons/apple-icon-152x152.png' rel='apple-touch-icon' sizes='152x152'>
<link href='/icons/apple-icon-180x180.png' rel='apple-touch-icon' sizes='180x180'>
<link href='/icons/favicon-16x16.png' rel='icon' sizes='16x16' type='image/png'>
<link href='/icons/favicon-32x32.png' rel='icon' sizes='32x32' type='image/png'>
<link href='/icons/favicon-96x96.png' rel='icon' sizes='96x96' type='image/png'>
<link href='/icons/android-icon-192x192.png' rel='icon' sizes='192x192' type='image/png'>
<link href='/icons/manifest.json' rel='manifest'>
<script src='/require.js' type='application/javascript'></script>
<script src='/giotto-dev.js' type='application/javascript'></script>
<script src='/giottojs.min.js' type='application/javascript'></script>
</head>
<body>
<div id="page">
<navbar d3-model="mainNavbar" brand-height="60"></navbar>
<div id="page-main" class="pt100">
<div class="container">
<div class="row">
<div class="col-sm-12">
<div class="page-header">
<h2></h2>
</div>
<div d3-highlight>
<h1 id="d3-view">d3-view</h1>
<p><a href="https://circleci.com/gh/quantmind/d3-view"><img alt="CircleCI" src="https://circleci.com/gh/quantmind/d3-view.svg?style=svg&circle-token=f84972c3cf4e8f17d74066ead28544da990115c3" /></a>
<a href="https://david-dm.org/quantmind/d3-view"><img alt="Dependency Status" src="https://david-dm.org/quantmind/d3-view.svg" /></a>
<a href="https://david-dm.org/quantmind/d3-view#info=devDependencies"><img alt="devDependency Status" src="https://david-dm.org/quantmind/d3-view/dev-status.svg" /></a></p>
<p><a href="https://circleci.com/api/v1/project/quantmind/d3-view/latest/artifacts/0/$CIRCLE_ARTIFACTS/coverage/index.html?branch=master&filter=successful">Coverage</a></p>
<p>This is a <a href="https://bost.ocks.org/mike/d3-plugin/">d3 plugin</a> for building
interactive web interfaces.
It provides data-reactive components with a simple and flexible API.</p>
<ul>
<li>Modern javascript</li>
<li>Minimal footprint - use only what you need</li>
<li>Built on top of <a href="https://github.com/d3">d3</a></li>
</ul>
<h2 id="contents">Contents</h2>
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
<ul>
<li><a href="#installing">Installing</a></li>
<li><a href="#getting-started">Getting Started</a></li>
<li><a href="#create-a-view-model-object">Create a view-model object</a></li>
<li><a href="#expressions">Expressions</a></li>
<li><a href="#view">View</a></li>
<li><a href="#view-api">View API</a><ul>
<li><a href="#viewmodel">view.model</a></li>
<li><a href="#viewparent">view.parent</a></li>
<li><a href="#viewel">view.el</a></li>
<li><a href="#viewcreateelementitagi">view.createElement(<i>tag</i>)</a></li>
<li><a href="#viewmountielementi">view.mount(<i>element</i>)</a></li>
<li><a href="#viewuseiplugini">view.use(<i>plugin</i>)</a></li>
</ul>
</li>
<li><a href="#model">Model</a></li>
<li><a href="#model-api">Model API</a></li>
<li><a href="#directives">Directives</a></li>
<li><a href="#core-directives">Core Directives</a><ul>
<li><a href="#d3-attr">d3-attr</a></li>
<li><a href="#d3-for">d3-for</a></li>
<li><a href="#d3-html">d3-html</a></li>
<li><a href="#d3-if">d3-if</a></li>
<li><a href="#d3-model">d3-model</a></li>
<li><a href="#d3-on">d3-on</a></li>
<li><a href="#d3-show">d3-show</a></li>
<li><a href="#d3-value">d3-value</a></li>
</ul>
</li>
<li><a href="#custom-directive">Custom Directive</a></li>
<li><a href="#directive-api">Directive API</a><ul>
<li><a href="#directiveexpression">directive.expression</a></li>
<li><a href="#directivecreateiexpressioni">directive.create(<i>expression</i>)</a></li>
<li><a href="#directivemountimodeli">directive.mount(<i>model</i>)</a></li>
<li><a href="#directiverefreshimodel-newvaluei">directive.refresh(<i>model, newValue</i>)</a></li>
<li><a href="#smalldirectivesmalldestroyimodeli"><small>directive.</small>destroy(<i>model</i>)</a></li>
</ul>
</li>
<li><a href="#components">Components</a></li>
<li><a href="#registration">Registration</a></li>
<li><a href="#components-api">Components API</a></li>
<li><a href="#creating-a-component">Creating a component</a><ul>
<li><a href="#componentmodel">component.model</a></li>
<li><a href="#componentinitioptionsi">component.init(<i>options</i>)</a></li>
<li><a href="#componentrenderidata-attrsi">component.render(<i>data, attrs</i>)</a></li>
<li><a href="#componentmounted">component.mounted()</a></li>
<li><a href="#componentdestroy">component.destroy()</a></li>
</ul>
</li>
<li><a href="#component-api">Component API</a><ul>
<li><a href="#componentevents">component.events</a></li>
<li><a href="#componentparent">component.parent</a></li>
<li><a href="#componentroot">component.root</a></li>
<li><a href="#componentuid">component.uid</a></li>
</ul>
</li>
<li><a href="#plugins">Plugins</a></li>
<li><a href="#form-plugin">Form Plugin</a><ul>
<li><a href="#importing">Importing</a></li>
<li><a href="#form-api">Form API</a></li>
</ul>
</li>
<li><a href="#bootstrap-plugin">Bootstrap Plugin</a></li>
<li><a href="#providers">Providers</a></li>
<li><a href="#other-frameworks">Other Frameworks</a></li>
<li><a href="#d3-plugins">D3 plugins</a></li>
</ul>
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
<h2 id="installing">Installing</h2>
<p>If you use <a href="https://www.npmjs.com/package/d3-view">NPM</a>, <code>npm install d3-view</code>.
Otherwise, download the <a href="https://github.com/quantmind/d3-view/releases">latest release</a>.
You can also load directly from <a href="https://giottojs.org">giottojs.org</a>,
as a <a href="https://giottojs.org/latest/d3-view.js">standalone library</a> or
<a href="https://unpkg.com/d3-view/">unpkg</a>.
AMD, CommonJS, and vanilla environments are supported. In vanilla, a d3 global is exported.
Try <a href="https://runkit.com/npm/d3-view">d3-view</a> in your browser.</p>
<pre><code class="javascript"><script src="https://d3js.org/d3-array.v1.min.js"></script>
<script src="https://d3js.org/d3-collection.v1.min.js"></script>
<script src="https://d3js.org/d3-color.v1.min.js"></script>
<script src="https://d3js.org/d3-dispatch.v1.min.js"></script>
<script src="https://d3js.org/d3-ease.v1.min.js"></script>
<script src="https://d3js.org/d3-selection.v1.min.js"></script>
<script src="https://d3js.org/d3-timer.v1.min.js"></script>
<script src="https://d3js.org/d3-array.v1.min.js"></script>
<script src="https://d3js.org/d3-interpolate.v1.min.js"></script>
<script src="https://d3js.org/d3-transition.v1.min.js"></script>
<script src="https://giottojs.org/latest/d3-let.min.js"></script>
<script src="https://giottojs.org/latest/d3-view.min.js"></script>
<script>
var vm = d3.view();
...
vm.mount("#my-element");
</script>
</code></pre>
<h2 id="getting-started">Getting Started</h2>
<p><code>d3.view</code> is a <a href="https://bost.ocks.org/mike/d3-plugin/">d3 plugin</a> for building
data driven web interfaces. It is not a framework as such, but you can easily
build one on top of it.</p>
<p>Importantly, this library does not make any choice for you, it is build on top
of the modular d3 library following very similar design patterns.</p>
<h3 id="create-a-view-model-object">Create a view-model object</h3>
<p>To create a view object for you application, invoke the <code>d3.view</code> function</p>
<pre><code class="javascript">var vm = d3.view({
model: {...},
components: {...},
directives: {...}
});
</code></pre>
<p>You can create more than one view:</p>
<pre><code>var vm2 = d3.view({
model: {...},
components: {...},
directives: {...}
});
</code></pre>
<p>All properties in the input object are optionals and are used to initialised the view with
custom data (<a href="#model">model</a>), <a href="#components">components</a> and <a href="#directives">directives</a>.</p>
<p>Lets consider one view for the sake of this introduction.
The <code>vm</code> obtained by the <code>`view</code> function is a view-model object which
can be <em>mounted</em> into the DOM via the view <code>mount</code> function.</p>
<h3 id="expressions">Expressions</h3>
<p>The text we put inside directive's values are called <code>binding expressions</code>.
In d3-view, a binding expression consists of a single JavaScript expression
but not operations. The difference between expressions and operations is akin
to the difference between a cell in an Excel spreadsheet vs. a proper JavaScript program.</p>
<p>Valid expression are:</p>
<pre><code class="javascript">"The sun" // literal
theme // An identifier (a property of a model)
dosomething() // A function
[theme, number] // Arrays of identifiers
x ? "Hi" : "goodbye" // Conditionals
</code></pre>
<p>and complex combinations of the above</p>
<pre><code>user.groups().join(", ")
[theme, user.groups(), "Hi"]
</code></pre>
<h2 id="view">View</h2>
<h3 id="view-api">View API</h3>
<p>With the exception of the <a href="#view-mount">mount</a> and
<a href="#view-use">use</a> methods, the view API is available once the view
has been mounted to an HTML element, i.e. once the <a href="#view-mount">mount</a>
method has been called.</p>
<h4 id="viewmodel">view.model</h4>
<p>The <a href="#model">model</a> bound to the view, the combo gives the name to the object, the <strong>view-model object</strong>.</p>
<h4 id="viewparent">view.parent</h4>
<p>The parent of a view, always <strong>undefined</strong>, a view is always the root element of
a view mounted DOM.</p>
<h4 id="viewel">view.el</h4>
<p>Root HTMLElement of the view.</p>
<h4 id="viewcreateelementtag">view.createElement(<i>tag</i>)</h4>
<p>Create a new HTML Element with the given tag. Return a [d3.selection][]
of the new element.</p>
<h4 id="viewmountelement">view.mount(<i>element</i>)</h4>
<p>Mount a view model into the HTML <code>element</code>.
The view only affect <code>element</code> and its children.
This method can be called <strong>once only</strong> for a given view model.</p>
<h4 id="viewuseplugin">view.use(<i>plugin</i>)</h4>
<p>Install a <a href="#plugins">plugin</a> into the view model. This method can be called several times with as many plugins as one needs,
however it can be called only before the view is mounted into an element.</p>
<h2 id="model">Model</h2>
<p>At the core of the library we have the Hierarchical Reactive Model.
The Model comparable to angular scope but its implementation is different.</p>
<ul>
<li>
<p><strong>Hierarchical</strong>: a <strong>root</strong> model is associated with a d3-view object.</p>
</li>
<li>
<p><strong>Reactive</strong>:</p>
</li>
</ul>
<p>A model can be associated with more than one element, new children model are created for elements that needs a new model.
For example, a <a href="#components">component</a> that specify the <code>model</code> object during initialisation, creates its own model,
a child model of the model associated with its parent element.</p>
<h3 id="model-api">Model API</h3>
<p><a name="user-content-model-parent" href="#model-parent">#</a> model.<b>parent</b></p>
<p>Get the ancestor of the model if it exists. It it does not exist, this is the root model.</p>
<p><a name="user-content-model-get" href="#model-get">#</a> model.<b>$get</b>(<i>attribute</i>)</p>
<p>Get an attribute value from the model, traversing the tree. If the <code>attribute</code> is not available in the model,
it will recursively retrieve it from its <a href="#model-parent">parent</a>.</p>
<p><a name="user-content-model-set" href="#model-set">#</a> model.<b>$set</b>(<i>attribute, value</i>)</p>
<p>Set an attribute value from in the model, traversing the tree.</p>
<p><a name="user-content-model-on" href="#model-on">#</a> model.<b>$on</b>(<i>attribute, callback</i>)</p>
<p>Add <code>callback</code> to a model reactive <code>attribute</code>. The callback is invoked when
the attribute change value only. It is possible to pass the <code>callback</code> only, in which
case it is triggered when any of the model <strong>own attributes</strong> change.</p>
<p><a name="user-content-model-child" href="#model-child">#</a> model.<b>$child</b>(<i>intials</i>)</p>
<p>Crate a child model with prototypical inheritance from the model.</p>
<p><a name="user-content-model-new" href="#model-new">#</a> model.<b>$new</b>(<i>intials</i>)</p>
<p>Create a child model, with no inheritance from the parent (an isolated model).</p>
<h2 id="directives">Directives</h2>
<p>Directives are special attributes with the <code>d3-</code> prefix.
Directive attribute values are expected to be binding <a href="#expressions">expressions</a>.
The library provides several directives for every day task.</p>
<p>For example the <code>d3-html</code> directive binds an expression to the inner
Html of the element containing the directive:</p>
<pre><code class="html"><div id="entry">
...
<p d3-html='paragrah'></p>
...
</div>
</code></pre>
<p>Here the <code>paragraph</code> is a reactive attribute of the View model.</p>
<pre><code class="javascript">d3.view({
model: {
paragraph: 'TODO'
}
}).mount('#entry');
</code></pre>
<h3 id="core-directives">Core Directives</h3>
<h4 id="d3-attr">d3-attr</h4>
<p>The <a href="https://github.com/quantmind/d3-view/blob/master/src/directives/attr.js">d3-attr</a> directive creates a <strong>one-way binding</strong> between a model
property and an HTML element attribute</p>
<pre><code class="html"><input d3-attr-name="code" d3-attr-placeholder="description || code">
</code></pre>
<p>The <code>attr</code> can be omitted for <code>class</code>, <code>name</code> , <code>disabled</code>,
<code>readonly</code> and <code>required</code>.</p>
<pre><code class="html"><input d3-name="code" d3-class="bright ? 'bright' : 'dull'">
</code></pre>
<p><code>code</code> and <code>bright</code> are reactive properties of the view-model.</p>
<h4 id="d3-for">d3-for</h4>
<p>As the name suggest, the <a href="https://github.com/quantmind/d3-view/blob/master/src/directives/for.js">d3-for</a> directive can be used to repeat the
element once per item from a collection. Each element gets its own model,
where the given loop variable is set to the current collection item,
and <code>index</code> is set to the item index or key.</p>
<h4 id="d3-html">d3-html</h4>
<p>The <a href="https://github.com/quantmind/d3-view/blob/master/src/directives/html.js">d3-html</a> directive creates a <strong>one-way-binding</strong> between a model property
and the innerHtml property of the hosting HTML element.
You can use it to attach html or text to element dynamically.</p>
<h4 id="d3-if">d3-if</h4>
<p>With <a href="https://github.com/quantmind/d3-view/blob/master/src/directives/if.js">d3-if</a> you can create conditionals.</p>
<h4 id="d3-model">d3-model</h4>
<p>The <a href="https://github.com/quantmind/d3-view/blob/master/src/directives/model.js">d3-model</a> directive is a special directive and the first to be mounted, if available,
into the hosting element. As the name suggests, the directive
creates a new model on the element based on data from parent models.</p>
<h4 id="d3-on">d3-on</h4>
<p>The <a href="https://github.com/quantmind/d3-view/blob/master/src/directives/on.js">d3-on</a> directive attaches an event listener to the element. The event type is denoted by the argument.
The expression should be model method call, the event <code>callback</code>. if the attribute is omitted
it is assumed to be a <code>click</code> event.
The event <code>callback</code> listens to <strong>native DOM events only</strong>.</p>
<pre><code class="html"><button d3-on-click="submit()">Submit</button>
</code></pre>
<h4 id="d3-show">d3-show</h4>
<p>The <a href="https://github.com/quantmind/d3-view/blob/master/src/directives/show.js">d3-show</a> directive display or hide an element. The display style is preserved.</p>
<h4 id="d3-value">d3-value</h4>
<p>The <a href="https://github.com/quantmind/d3-view/blob/master/src/directives/value.js">d3-value</a> directive establish a <strong>two-way data binding</strong> for HTML
elements supporting the value property.
The binding is two ways because</p>
<ul>
<li>an update in the model attribute causes an update in the HTML value property</li>
<li>an update in the HTML value property causes an update in the model attribute</li>
</ul>
<h3 id="custom-directive">Custom Directive</h3>
<p>Creating a custom directive involve the following steps:</p>
<ul>
<li>Create a (reusable) directive object:</li>
</ul>
<pre><code class="javascript">var mydir = {
create: function (expression) {
return expression;
},
mount: function (model) {
return model;
},
refresh: function (model, value) {
},
destroy: function () {
}
};
</code></pre>
<ul>
<li>Add the directive to the view constructor:</li>
</ul>
<pre><code class="javascript">var vm = d3.view({
el: '#entry',
...
directives: {
mydir: mydir
}
};
</code></pre>
<ul>
<li>Use the directive via the <code>d3-mydir</code> attribute.</li>
</ul>
<p>A directive is customized via the four methods highlighted above.
None of the method needs implementing, and indeed for some directive
the <code>refresh</code> method is the only one which needs attention.</p>
<p>Directives can also be added via <a href="#plugins">plugins</a></p>
<h3 id="directive-api">Directive API</h3>
<h4 id="directiveel">directive.el</h4>
<p>The HTML Element hosting the directive, available after initialisation and therefore accessible by all
API methods.</p>
<h4 id="directiveexpression">directive.expression</h4>
<p>The parsed expression, available after the <a href="#directivecreateexpression">create</a>
method has been called.</p>
<h4 id="directivecreateexpression">directive.create(<i>expression</i>)</h4>
<p>The <code>create</code> method is called once only, at the end of directive initialisation, no binding with the HTML element or model has yet occurred.
The <code>expression</code> is the attribute value, a string, and it is not yet parsed.
This method must return the expression for parsing (it doesn't need to be the same as the input expression).
However, if it returns nothing, the directive is not executed.</p>
<h4 id="directivemountmodel">directive.mount(<i>model</i>)</h4>
<p>The <code>mount</code> method is called once only, at the beginning of the binding process with the HTML element.
The expression returned by the <code>create</code> method
has been parsed and available in the <code>this.expression</code> attribute.
This method must return the model for binding (it doesn't need to be the same as the input model, but usually it is).
However, if it returns nothing, the binding execution is aborted.</p>
<h4 id="directiverefreshmodel-newvalue">directive.refresh(<i>model, newValue</i>)</h4>
<p>This method is called every time the model associated with the element hosting the directive, has changed value. It is also called at the end of a successful <a href="#directivemountmodel">mount</a>.</p>
<h4 id="directivedestroymodel">directive.destroy(<i>model</i>)</h4>
<p>Called when the element hosting the directive is removed from the DOM.</p>
<h2 id="components">Components</h2>
<p>Components help you extend basic HTML elements to encapsulate reusable code.
They are custom elements that <code>d3.view</code> attach specified behavior to.</p>
<h3 id="registration">Registration</h3>
<p>In order to use components you need to register them with the <code>view</code> object:</p>
<pre><code class="javascript">d3.view({
components: {
tag1: component1,
...
tagN: componentN
}
});
</code></pre>
<p>A component is either an object:</p>
<pre><code class="javascript">var component1 = {
render: function () {
return this.htmlElement('<p>Very simple component</p>');
}
};
</code></pre>
<p>or a function, the component render method:</p>
<pre><code class="javascript">function component1 () {
return this.htmlElement('<p>Another very simple component</p>');
}
</code></pre>
<h3 id="components-api">Components API</h3>
<p><a name="user-content-component-el" href="#component-el">#</a> component.<b>el</b></p>
<p>The HTML element created by the component <a href="#render">render</a> method. Available after the component is mounted.</p>
<p><a name="user-content-component-model" href="#component-model">#</a> component.<b>model</b></p>
<p>The <a href="#model">model</a> bound to the component</p>
<h3 id="creating-a-component">Creating a component</h3>
<p>A component is defined by the <a href="#render">render</a> method. However, there optional properties and
methods that can be used to customize construction and lifecycle of a component.</p>
<pre><code class="javascript">var component = {
model: {...},
init (options) {
},
render (data, attr) {
},
mounted () {
},
destroy () {
}
};
</code></pre>
<h4 id="componentmodel">component.model</h4>
<p>A function or an object which specifies the default values of the component model.</p>
<p>Once the component has been mounted, this is becomes the
model associated with the component and therefore an API property
of the component.</p>
<p>Some component have their own model, other they use the model of the parent component.</p>
<h4 id="componentinitoptions">component.init(<i>options</i>)</h4>
<p>Hook called once only at the beginning of the component initialisation process, before it is mounted into the DOM.</p>
<h4 id="componentrenderdata-attrs">component.render(<i>data, attrs</i>)</h4>
<p>This is <strong>the only required hook</strong>. It is called once only while the component is being mounted into the DOM
and must return a single HTMLElement or a selector with one node only.
The returned element replaces the component element in the DOM.
Importantly, this function can also return a <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a> which resolve in an HTMLElement or selector.</p>
<ul>
<li><strong>data</strong> is the data object in the component element</li>
<li><strong>attrs</strong> is an object containing the key-value of attributes in the component element</li>
</ul>
<h4 id="componentmounted">component.mounted()</h4>
<p>Hook called after the component has been mounted in to the DOM.
In this state the component has the full API available
and all its children elements are mounted too.</p>
<h4 id="componentdestroy">component.destroy()</h4>
<p>Called when the component HTML element is removed from the DOM.</p>
<h3 id="component-api">Component API</h3>
<p>The most important part of a component is the <code>render</code> method. This sections
deals with the API available to the component once it is created.
The API is very similar to the <a href="#view-api">view-api</a> since components and views share
the same constructor.</p>
<h4 id="componentevents">component.events</h4>
<p>Events object which can be used for registering event listeners or firing events.</p>
<h4 id="componentparent">component.parent</h4>
<p>The parent component. If not defined this is the root view, not a component.</p>
<h4 id="componentroot">component.root</h4>
<p>The view object the component belongs to.</p>
<h4 id="componentuid">component.uid</h4>
<p>Component unique identifier</p>
<h2 id="plugins">Plugins</h2>
<p>Plugins, usually, add functionality to a view-model.
There is no strictly defined scope for a plugin but there are typically several
types of plugins you can write:</p>
<ul>
<li>Add a group of <a href="#components">components</a></li>
<li>Add a group of <a href="#directives">directives</a></li>
<li>Add some components methods by attaching them to components prototype.</li>
<li>Add providers to the <code>view.providers</code> object</li>
</ul>
<p>A plugin can be an object with the <code>install</code> method or a simple
function (same signature as the install method).</p>
<pre><code class="javascript">var myPlugin = {
install: function (vm) {
// add a component
vm.addComponent('alert', {
model: {
style: "alert-info",
text: "Hi! this is a test!"
},
render: function () {
return view.htmlElement('<div class="alert" :class="style" d3-html="text"></div>');
}
});
// add another component
vm.addComponent('foo', ...);
// add a custom directive
vm.addDirective('mydir', {
refresh: function (model, value) {
...
}
});
}
}
</code></pre>
<p>A plugin is installed in a view via the chainable <code>use</code> method:</p>
<pre><code class="javascript">var vm = d3.view();
vm.use(myPlugin).use(anotherPlugin);
</code></pre>
<h3 id="form-plugin">Form Plugin</h3>
<p>This library include a form plugin for creating dynamic forms from JSON layouts.
The plugin adds the <code>d3form</code> <a href="#components">component</a> to the view-model:</p>
<pre><code class="javascript">import {view, viewForms} from 'd3-view';
var vm = view().use(viewForms);
</code></pre>
<h4 id="importing">Importing</h4>
<p>If you are using <a href="http://rollupjs.org/">rollup</a> to compile your javascript application, the form plugin
will be included in your compiled file only if</p>
<pre><code class="javascript">import {viewForms} from 'd3-view';
</code></pre>
<p>is present somewhere in your code. Otherwise, it will be eliminated thanks to
tree-shaking.</p>
<h4 id="form-api">Form API</h4>
<p><a name="user-content-form-setsubmit" href="#form-setsubmit">#</a> form.<b>setSubmit</b>()</p>
<p>Sets the form model <code>formSubmitted</code> and <code>formSubmitted</code> reactive attribute to <code>true</code> and
returns a <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a> which resolves in <code>true</code> or <code>false</code>
depending if the form inputs pass validation.</p>
<p><a name="user-content-form-isvalid" href="#form-isvalid">#</a> form.<b>isValid</b>()</p>
<p>Check if the form inputs pass validation, return <code>true</code> or <code>false</code>.</p>
<h3 id="bootstrap-plugin">Bootstrap Plugin</h3>
<p>It is possible to use bootstrap layouts for d3 forms by importing and using the <code>viewBootstrapForms</code> plugin:</p>
<pre><code class="javascript">import {view, viewForms, viewBootstrapForms} from 'd3-view';
var vm = view().use(viewForms).use(viewBootstrapForms);
</code></pre>
<h2 id="providers">Providers</h2>
<p><strong>TODO</strong></p>
<h2 id="other-frameworks">Other Frameworks</h2>
<p>In order of complexity</p>
<ul>
<li><a href="https://angularjs.org/">Angular</a></li>
<li><a href="https://facebook.github.io/react/">React</a></li>
<li><a href="http://vuejs.org/">Vue</a></li>
</ul>
<h2 id="d3-plugins">D3 plugins</h2>
<ul>
<li><a href="https://github.com/pbeshai/d3-interpolate-path">d3-interpolate-path</a></li>
<li><a href="https://github.com/pbeshai/d3-line-chunked">d3-line-chunked</a></li>
<li><a href="https://github.com/schnerd/d3-scale-cluster">d3-scale-cluster</a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="hidden-print">
<div id="footer">
<div class="container">
<div class="row">
<div class="col-sm-12 small">
<p>Library released under the <a href="http://opensource.org/licenses/BSD-3-Clause">BSD License</a>.</p>
<p>Copyright <year></year> <a href="http://lucasbardella.com">Luca Sbardella</a> and <a href="http://quantmind.com">Quantmind</a></p>
</div>
</div>
</div>
</div>
</div>
</div><script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-54439804-4', 'auto');
ga('send', 'pageview');
</script></body>
</html>