-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.html
More file actions
961 lines (792 loc) · 33 KB
/
index.html
File metadata and controls
961 lines (792 loc) · 33 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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/>
<meta name="HandheldFriendly" content="True"/>
<title>Atlas Database Framework for PHP</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.5/cerulean/bootstrap.min.css" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css" integrity="sha384-lKuwvrZot6UHsBSfcMvOkWwlCMgc0TaWr+30HWe3a4ltaBwTZhyTEggF5tJv8tbt" crossorigin="anonymous">
<link rel="stylesheet" href="https://tobiju.github.io/share/prismjs/prism-base16-ateliersulphurpool.light.css" />
<link rel="stylesheet" href="https://tobiju.github.io/share/prismjs/prism-linenumbers.css" />
<link rel="stylesheet" href="/style.css" />
</head>
<body data-spy="scroll" data-target="#sideNav" data-offset="50" class="bbt-theme-cerulean">
<div class="page-wrapper">
<header>
<nav class="navbar navbar-default navbar-fixed-top">
<div class="box-header container">
<form class="form-search navbar-form navbar-right" role="search">
<div class="form-group">
<input type="text"
placeholder="Search"
class="js-search-input form-control"
data-roothref="/">
<div class="js-search-results"></div>
</div>
</form>
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
data-target="#js-navbar-collapse" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">
<img alt="logo" src="https://raw.githubusercontent.com/atlasphp/atlasphp.github.io/master/images/atlas.png" title="Home">
</a>
</div>
<div class="collapse navbar-collapse" id="js-navbar-collapse">
<ul class="nav navbar-nav">
<li>
<a href="/dymaxion/">Dymaxion (v4)</a>
<ul class="dropdown-menu">
<li>
<a href="/dymaxion/pdo/">PDO Connection</a>
<ul class="dropdown-menu">
<li>
<a href="/dymaxion/pdo/connection.html">PDO Connection</a>
</li>
<li>
<a href="/dymaxion/pdo/connection-locator.html">Connection Locator</a>
</li>
<li>
<a href="/dymaxion/pdo/upgrade.html">Upgrade Notes</a>
</li>
</ul>
</li>
<li>
<a href="/dymaxion/statement/">Query Statement Builders</a>
<ul class="dropdown-menu">
<li>
<a href="/dymaxion/statement/getting-started.html">Getting Started</a>
</li>
<li>
<a href="/dymaxion/statement/binding.html">Value Binding</a>
</li>
<li>
<a href="/dymaxion/statement/ctes.html">Common Table Expressions</a>
</li>
<li>
<a href="/dymaxion/statement/select.html">SELECT</a>
</li>
<li>
<a href="/dymaxion/statement/insert.html">INSERT</a>
</li>
<li>
<a href="/dymaxion/statement/update.html">UPDATE</a>
</li>
<li>
<a href="/dymaxion/statement/delete.html">DELETE</a>
</li>
<li>
<a href="/dymaxion/statement/other.html">Other Topics</a>
</li>
</ul>
</li>
<li>
<a href="/dymaxion/query/">Query Statement Performers</a>
<ul class="dropdown-menu">
<li>
<a href="/dymaxion/query/getting-started.html">Getting Started</a>
</li>
<li>
<a href="/dymaxion/query/execution.html">Query Execution</a>
</li>
<li>
<a href="/dymaxion/query/upgrade.html">Upgrade Notes</a>
</li>
</ul>
</li>
</ul>
</li>
<li>
<a href="/cassini/">Cassini (v3)</a>
<ul class="dropdown-menu">
<li>
<a href="/cassini/orm/">ORM</a>
<ul class="dropdown-menu">
<li>
<a href="/cassini/orm/getting-started.html">Getting Started</a>
</li>
<li>
<a href="/cassini/orm/relationships.html">Mapper Relationships</a>
</li>
<li>
<a href="/cassini/orm/reading.html">Fetching Records and RecordSets</a>
</li>
<li>
<a href="/cassini/orm/records.html">Working with Records</a>
</li>
<li>
<a href="/cassini/orm/record-sets.html">Working with RecordSets</a>
</li>
<li>
<a href="/cassini/orm/transactions.html">Transactions</a>
</li>
<li>
<a href="/cassini/orm/behavior.html">Record and RecordSet Behaviors</a>
</li>
<li>
<a href="/cassini/orm/events.html">Events</a>
</li>
<li>
<a href="/cassini/orm/direct.html">Direct Queries</a>
</li>
<li>
<a href="/cassini/orm/other.html">Other Topics</a>
</li>
<li>
<a href="/cassini/orm/domain.html">Domain Models</a>
</li>
</ul>
</li>
<li>
<a href="/cassini/skeleton/">Skeleton Generator</a>
<ul class="dropdown-menu">
<li>
<a href="/cassini/skeleton/usage.html">Usage</a>
</li>
</ul>
</li>
<li>
<a href="/cassini/table/">Table Data Gateway</a>
<ul class="dropdown-menu">
<li>
<a href="/cassini/table/getting-started.html">Getting Started</a>
</li>
<li>
<a href="/cassini/table/reading.html">Reading From The Table</a>
</li>
<li>
<a href="/cassini/table/writing.html">Writing To The Table</a>
</li>
<li>
<a href="/cassini/table/events.html">Table Events</a>
</li>
</ul>
</li>
<li>
<a href="/cassini/query/">Query System</a>
<ul class="dropdown-menu">
<li>
<a href="/cassini/query/getting-started.html">Getting Started</a>
</li>
<li>
<a href="/cassini/query/binding.html">Value Binding</a>
</li>
<li>
<a href="/cassini/query/select.html">SELECT</a>
</li>
<li>
<a href="/cassini/query/insert.html">INSERT</a>
</li>
<li>
<a href="/cassini/query/update.html">UPDATE</a>
</li>
<li>
<a href="/cassini/query/delete.html">DELETE</a>
</li>
<li>
<a href="/cassini/query/other.html">Other Topics</a>
</li>
</ul>
</li>
<li>
<a href="/cassini/info/">Schema Information</a>
<ul class="dropdown-menu">
<li>
<a href="/cassini/info/usage.html">Usage</a>
</li>
</ul>
</li>
<li>
<a href="/cassini/pdo/">PDO Connection</a>
<ul class="dropdown-menu">
<li>
<a href="/cassini/pdo/connection.html">PDO Connection</a>
</li>
<li>
<a href="/cassini/pdo/connection-locator.html">Connection Locator</a>
</li>
</ul>
</li>
</ul>
</li>
<li>
<a href="/boggs/">Boggs (v2)</a>
<ul class="dropdown-menu">
<li>
<a href="/boggs/mapper/">ORM</a>
<ul class="dropdown-menu">
<li>
<a href="/boggs/mapper/getting-started.html">Getting Started</a>
</li>
<li>
<a href="/boggs/mapper/relationships.html">Mapper Relationships</a>
</li>
<li>
<a href="/boggs/mapper/reading.html">Fetching Records and RecordSets</a>
</li>
<li>
<a href="/boggs/mapper/records.html">Working with Records</a>
</li>
<li>
<a href="/boggs/mapper/record-sets.html">Working with RecordSets</a>
</li>
<li>
<a href="/boggs/mapper/transactions.html">Transactions (Unit of Work)</a>
</li>
<li>
<a href="/boggs/mapper/behavior.html">Record and RecordSet Behaviors</a>
</li>
<li>
<a href="/boggs/mapper/events.html">Events</a>
</li>
<li>
<a href="/boggs/mapper/direct.html">Direct Queries</a>
</li>
<li>
<a href="/boggs/mapper/domain.html">Domain Models</a>
</li>
</ul>
</li>
<li>
<a href="/boggs/skeleton/">Skeleton Generator</a>
<ul class="dropdown-menu">
<li>
<a href="/boggs/skeleton/getting-started.html">Atlas.Cli 1.x</a>
</li>
</ul>
</li>
</ul>
</li>
<li>
<a href="/albers/">Albers (v1)</a>
<ul class="dropdown-menu">
<li>
<a href="/albers/mapper/">ORM</a>
<ul class="dropdown-menu">
<li>
<a href="/albers/mapper/getting-started.html">Getting Started</a>
</li>
<li>
<a href="/albers/mapper/relationships.html">Mapper Relationships</a>
</li>
<li>
<a href="/albers/mapper/reading.html">Fetching Records and RecordSets</a>
</li>
<li>
<a href="/albers/mapper/records.html">Working with Records</a>
</li>
<li>
<a href="/albers/mapper/record-sets.html">Working with RecordSets</a>
</li>
<li>
<a href="/albers/mapper/transactions.html">Transactions (Unit of Work)</a>
</li>
<li>
<a href="/albers/mapper/behavior.html">Record and RecordSet Behaviors</a>
</li>
<li>
<a href="/albers/mapper/events.html">Events</a>
</li>
<li>
<a href="/albers/mapper/direct.html">Direct Queries</a>
</li>
<li>
<a href="/albers/mapper/domain.html">Domain Models</a>
</li>
</ul>
</li>
<li>
<a href="/albers/skeleton/">Skeleton Generator</a>
<ul class="dropdown-menu">
<li>
<a href="/albers/skeleton/getting-started.html">Atlas.Cli 1.x</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
</header>
<div class="container">
<div class="jumbotron jumbotron-fluid">
<div class="container">
<h1 class="display-1">Atlas</h1>
<h2 class="display-2">Simple. Sensible. SQL.</h2>
<p class="lead">
Atlas is a database framework for PHP to help you work with your
<strong>persistence</strong> model, while providing a path to
refactor towards a richer <strong>domain</strong> model as needed.
</p>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">
<h1 class="panel-title">Object-Relational Manager</h1>
</div>
<div class="panel-body">
<p>
A data mapper ORM for your persistence layer, not your
domain layer. The flagship package in the Atlas family.
Supports MySQL, PostgreSQL, SQLite, and SQL Server.
</p>
<p>
<i class="fas fa-book"></i> <a href="/cassini/orm/">Docs</a>
<i class="fab fa-github"></i> <a href="https://github.com/atlasphp/Atlas.Orm/">Code</a>
</p>
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">
<h1 class="panel-title">Skeleton Generator</h1>
</div>
<div class="panel-body">
<p>
A command-line interface to generate all of the necessary
Atlas classes to model your existing database tables in PHP.
Supports MySQL, PostgreSQL, SQLite, and SQL Server.
</p>
<p>
<i class="fas fa-book"></i> <a href="/cassini/skeleton/">Docs</a>
<i class="fab fa-github"></i> <a href="https://github.com/atlasphp/Atlas.Cli/">Code</a>
</p>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">
<h1 class="panel-title">Data Mapper</h1>
</div>
<div class="panel-body">
<p>
The core of the Atlas.Orm package, the data mapper helps
you define relationships between table data gateways so
you can retrieve Record objects with all their related rows.
Supports MySQL, PostgreSQL, SQLite, and SQL Server.
</p>
<p>
<i class="fas fa-book"></i> <a href="/cassini/orm/">Docs</a>
<i class="fab fa-github"></i> <a href="https://github.com/atlasphp/Atlas.Mapper/">Code</a>
</p>
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">
<h1 class="panel-title">Table Data Gateway</h1>
</div>
<div class="panel-body">
<p>
A table data gateway implementation, for when a full-fledged
persistence ORM seems too much. Supports MySQL, PostgreSQL,
SQLite, and SQL Server.
</p>
<p>
<i class="fas fa-book"></i> <a href="/cassini/table/">Docs</a>
<i class="fab fa-github"></i> <a href="https://github.com/atlasphp/Atlas.Table/">Code</a>
</p>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">
<h1 class="panel-title">Query System</h1>
</div>
<div class="panel-body">
<p>
Build and perform SELECT, INSERT, UPDATE, and DELETE queries
through a fluent interface using standard SQL vocabulary.
Supports all database drivers for PDO.
</p>
<p>
<i class="fas fa-book"></i> <a href="/cassini/query/">Docs</a>
<i class="fab fa-github"></i> <a href="https://github.com/atlasphp/Atlas.Query/">Code</a>
</p>
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">
<h1 class="panel-title">PDO Wrapper</h1>
</div>
<div class="panel-body">
<p>
A straightforward PDO connection wrapper to provide
convenience methods for fetching and yielding database
results. Supports all database drivers for PDO.
</p>
<p>
<i class="fas fa-book"></i> <a href="/cassini/pdo/">Docs</a>
<i class="fab fa-github"></i> <a href="https://github.com/atlasphp/Atlas.Pdo/">Code</a>
</p>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-1"></div>
<div class="col-md-10">
<h3>Why Atlas?</h3>
<p>
Atlas helps developers to get started about as easily as they would with Active
Record for their <strong>persistence</strong> model, and provides a path to refactor more
easily towards a richer <strong>domain</strong> model as needed.
</p>
<h3>What Is A Persistence Model?</h3>
<p>
A persistence model is a model of your database, not your
business domain. More specifically, it is an object-oriented
representation of the tables and rows in the database.
</p>
<p>
Very often, a developer does not have a good, clean domain model
to work with, especially early in the project. But there is
almost always a database of some sort. Atlas models that
database structure so you can use it more easily in PHP code.
</p>
<h3>Why Use A Persistence Model?</h3>
<p>
A persistence model alone should get your application a long
way, especially at the beginning of a project. The Row, Record,
and RecordSet objects in Atlas are disconnected from the database, which
should make the refactoring and integration process a lot
cleaner than with Active Record.
</p>
<p>
Because Atlas works with your database schema as-it-is,
and not as-the-ORM-thinks it-should-be, Atlas is a very good
fit for projects with pre-existing databases. (Indeed, Atlas
supports both <strong>composite primary keys</strong> and
<strong>composite foreign keys</strong> -- for some legacy
systems, composite keys are absolutely necessary.)
</p>
<h3>How Does Atlas Work?</h3>
<p>
At a high level, Atlas is mini-framework built from a stack of
packages, where any "lower" package can be used indepdendently
of the the ones "above" it. This means you can pick the exact
level of functionality you need right now, and expand to the
next greater set of functionality only as is becomes necessary.
</p>
<p>
The package hierarchy, from bottom to top, looks like this:
</p>
<ul>
<li>
<p><a href="https://github.com/atlasphp/Atlas.Pdo">Atlas.Pdo</a> provides a database Connection object and a ConnectionLocator. If all you need is convenience wrapper around PDO with fetch and yield methods, this is the package for you.</p>
</li>
<li>
<p><a href="https://github.com/atlasphp/Atlas.Query">Atlas.Query</a> is a query builder that wraps an Atlas.Pdo Connection. If you just want to build and perform SQL queries using an object-oriented approach, the Atlas query objects can handle that.</p>
</li>
<li>
<p><a href="https://github.com/atlasphp/Atlas.Table">Atlas.Table</a> is a table data gateway implementation that uses Atlas.Query under the hood. If you don't need a full data mapper system and only want to interact with individual tables and their row objects, this will do the trick.</p>
</li>
<li>
<p><a href="https://github.com/atlasphp/Atlas.Mapper">Atlas.Mapper</a> is a data mapper implementation that models the relationships between tables. It allows you to build Record and RecordSet objects whose Row objects map naturally back to Table objects; you can write them back to the database one by one, or persist an entire Record graph back to the database in one go.</p>
</li>
<li>
<p><a href="https://github.com/atlasphp/Atlas.Orm">Atlas.Orm</a> is the overarching ORM package; it provides a convenience wrapper around the Mapper system, as well as several strategies for transaction management.</p>
</li>
</ul>
<p>
Thus, Atlas uses a table data gateway for the underlying table Rows, then composes
those Rows into Records and RecordSets via a data mapper; the PDO connection and query
system move the data back and forth between PHP and the database.
<h3>From Persistence To Domain</h3>
<p>
At the beginning, your domain logic layer (e.g. service layer,
application service, interactor, use case, etc.) can manipulate
the Atlas persistence objects (Records and RecordSets) directly.
You can add simple behavior methods to your Records and
RecordSets as well.
</p>
<p>
Then, as a domain model grows within your application,
<a href="http://www.mehdi-khalili.com/orm-anti-patterns-part-4-persistence-domain-model">Mehdi Khalili</a>
shows that the refactoring process can move along one of two paths:
</p>
<ul>
<li>
"Domain Model composed of Persistence Model". That is, the domain objects
can use Atlas persistence model Record objects internally, but do not expose
them. The domain objects can manipulate the persistence model objects
internally as much as they wish. For example, a domain object might have a
<tt>getAddress()</tt> method that reads from the internal Record.
</li>
<li>
"DDD on top of ORM". Here, Repositories map the persistence model objects to
and from domain objects. This provides a full decoupling of the domain model
from the persistence model, but is more time-consuming to develop.
</li>
</ul>
<h3>Other Considerations</h3>
<p>Here some further considerations regarding Atlas:</p>
<ul>
<li>
<strong>PHPStorm IDE auto-completion.</strong> When you use
the <tt>phpstorm.meta.php</tt> resource included in relevant
packages, PHPStorm will autocomplete methods and properties
from your Mapper, Record, RecordSet, et al. classes.
</li>
<li>
<strong>No annotations.</strong> Code should be in code, not
in comments.
</li>
<li>
<strong>No lazy-loading.</strong> Lazy-loading is seductive,
but eventually is more trouble than it's worth. Atlas
doesn't make it available at all, so it cannot be invoked
accidentally.
</li>
<li>
<strong>No data-type abstractions.</strong> Data-type abstraction seems great at first, but
it too ends up not being worth the trouble. Therefore, the actual underlying
database types are exposed and available as much as possible.
</li>
</ul>
<h3>Contra-Indications</h3>
<p>You may not find Atlas suitable for your needs because of the following:</p>
<ul>
<li>
Atlas uses code generation, though only in a very limited way. It turns out
that code generation is useful for some boilerplate code. For example, each table
is described as a PHP class, one that returns things like table name, column
names, etc. Since the table class should change only when the database
changes, code generation makes sense here; however, some developers may
be against code generation just on principle alone.
</li>
<li>
Atlas uses base Row, Record, and RecordSet classes, instead of plain-old PHP
objects. If this were a domain modeling system, a base class would be
unacceptable. Because Atlas is a <strong>persistence</strong> modeling system, base classes
are less objectionable, but for some this may be undesired.
</li>
</ul>
</div>
<div class="col-md-1"></div>
</div>
</div>
<footer>
<div id="copyright">
<div class="container">
<div class="row">
<div class="col-md-12">
<p>© 2015-2018, Atlas for PHP</p>
</div>
</div>
</div>
</div>
</footer>
</div>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"
integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lunr.js/0.6.0/lunr.min.js"></script>
<script src="http://bartaz.github.io/sandbox.js/jquery.highlight.js"></script>
<script src="https://tobiju.github.io/share/prismjs/main.js"></script>
<script src="https://tobiju.github.io/share/prismjs/prism.js"></script>
<script type="text/javascript">
function Search() {
this.store = {};
this.index = lunr(function () {
this.ref('id');
this.field('title', {boost: 10});
this.field('content');
});
this.searchResults = $('.js-search-results').addClass('list-search-results');
}
Search.prototype = {
constructor: Search,
init: function () {
this.createIndex();
this.bindEvents();
},
createIndex: function () {
var $this = this,
indexFilePath = $('.js-search-input').data('roothref') + 'index.json';
$.getJSON(indexFilePath, function (data) {
$.each(data, function (key, item) {
$this.index.add({
id: item.id,
title: item.title,
content: item.content
});
$this.store[item.id] = {
href: item.id,
title: item.title,
content: item.content
}
});
});
},
bindEvents: function () {
var $this = this;
$('html')
.on('click', function (event) {
$this.close($(this), event);
});
$('.js-search-input, .js-search-results')
.on('click', function (event) {
$this.click($(this), event);
});
$('.js-search-input')
.on('focus', function (event) {
$this.focus($(this), event);
})
.on('keyup', function (event) {
$this.search($(this), event)
})
.on('keydown', function (event) {
if ($('.js-search-results ul').is(':visible')){
$this.navigation($(this), event)
}
})
},
click: function (element, event) {
event.stopPropagation();
},
focus: function (element, event) {
this.searchInputWidth = element.css('width');
element.animate({
'width': 600
}, 500);
$('#js-navbar-collapse > ul').fadeOut();
},
close: function (element, event) {
var $this = this;
$('.js-search-results').hide();
$('.js-search-input').animate({
'width': $this.searchInputWidth
}, 500);
$('#js-navbar-collapse > ul').fadeIn();
},
search: function (element, event) {
var $this = this;
if (event.keyCode == 13 || event.keyCode == 38 || event.keyCode == 40) {
return;
}
var query = element.val(),
results = $this.index.search(query);
if (!results.length) {
$this.searchResults.empty();
return;
}
var resultsList = results.reduce(function (ul, result) {
var item = $this.store[result.ref];
var title = $('<b>').text(item.title);
var cropText = $this.cropText(item.content, query);
if (cropText.length === 0) {
cropText = $('<p>').html(item.content.substring(0, 120) + "...");
}
var content = content = $('<p>').html(cropText);
var div = $('<div>')
.append(title)
.append(content);
var a = $('<a>').attr('href', item.href)
.append(div);
var li = $('<li>')
.append(a);
ul.append(li);
return ul;
}, $('<ul>'));
this.searchResults.html(resultsList);
$('.js-search-results').show();
$(".js-search-results li:first-child").addClass('selected');
},
cropText: function (content, query) {
var cropedText = '',
re = new RegExp("\\s?(.{0,30})?" + query + ".*?\\b(.{0,30}.)?\\s?", "gi");
$.each(content.match(re), function (key, value) {
cropedText += '...' + value + '...';
});
return cropedText;
},
navigation: function (element, event) {
var selected = null,
listSelector = ".js-search-results ul",
listItemSelector = listSelector + " li",
selectedListItemSelector = listItemSelector + ".selected",
selectedListItemSelectorAnchor = listItemSelector + ".selected a";
// enter
if (event.keyCode == 13) {
event.preventDefault();
this.close();
window.location.replace($(selectedListItemSelectorAnchor).attr('href'));
}
// up
if (event.keyCode == 38) {
selected = $(selectedListItemSelector);
$(listItemSelector).removeClass("selected");
if (selected.prev().length == 0) {
selected.siblings().last().addClass("selected");
} else {
selected.prev().addClass("selected");
}
selected = $(selectedListItemSelector);
this.scrollListUp(selected);
}
// down
if (event.keyCode == 40) {
selected = $(selectedListItemSelector);
$(listItemSelector).removeClass("selected");
if (selected.next().length == 0) {
selected.siblings().first().addClass("selected");
} else {
selected.next().addClass("selected");
}
selected = $(selectedListItemSelector);
this.scrollListDown(selected);
}
},
scrollListDown: function (element) {
var ul = element.parent(),
ulHeight = ul.height(),
ulBottomPosition = ulHeight + ul.scrollTop(),
liBottomPosition = element.position().top + element.height();
if (liBottomPosition > ulBottomPosition) {
ul.scrollTop(liBottomPosition - ulHeight);
}
if (element.is(':first-child')) {
ul.scrollTop(0);
}
},
scrollListUp: function (element) {
var ul = element.parent(),
ulTopPosition = ul.scrollTop(),
liTopPosition = element.position().top;
if (liTopPosition < ulTopPosition) {
ul.scrollTop(element.position().top);
}
if (element.is(':last-child')) {
ul.scrollTop(element.position().top - element.height());
}
}
};
$(function () {
var search = new Search();
search.init();
});
</script>
</body>
</html>