-
Notifications
You must be signed in to change notification settings - Fork 31
/
bootstrap3.facetview.theme.js
1533 lines (1284 loc) · 66.1 KB
/
bootstrap3.facetview.theme.js
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
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function escapeHtml(unsafe, def) {
if (def === undefined) {
def = "";
}
if (unsafe === undefined || unsafe == null) {
return def;
}
try {
if (typeof unsafe.replace !== "function") {
return unsafe
}
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
} catch(err) {
return def;
}
}
/******************************************************************
* DEFAULT RENDER FUNCTIONS
*****************************************************************/
function theFacetview(options) {
/*****************************************
* overrides must provide the following classes and ids
*
* id: facetview - main div in which the facetview functionality goes
* id: facetview_filters - div where the facet filters will be displayed
* id: facetview_rightcol - the main window for result display (doesn't have to be on the right)
* class: facetview_search_options_container - where the search bar and main controls will go
* id : facetview_selectedfilters - where we summarise the filters which have been selected
* class: facetview_metadata - where we want paging to go
* id: facetview_results - the table id for where the results actually go
* id: facetview_searching - where the loading notification can go
*
* Should respect the following configs
*
* options.debug - is this a debug enabled facetview. If so, put a debug textarea somewhere
*/
// the facet view object to be appended to the page
var thefacetview = '<div id="facetview"><div class="row">';
// if there are facets, give them span3 to exist, otherwise, take up all the space
var showfacets = false;
for (var i = 0; i < options.facets.length; i++) {
var f = options.facets[i];
if (!f.hidden) {
showfacets = true;
break;
}
}
if (showfacets) {
thefacetview += '<div class="col-md-3"><div id="facetview_filters" style="padding-top:45px;"></div></div>';
thefacetview += '<div class="col-md-9" id="facetview_rightcol">';
} else {
thefacetview += '<div class="col-md-12" id="facetview_rightcol">';
}
// make space for the search options container at the top
thefacetview += '<div class="facetview_search_options_container"></div>';
// make space for the selected filters
thefacetview += '<div style="margin-top: 20px"><div class="row"><div class="col-md-12"><div class="btn-toolbar" id="facetview_selectedfilters"></div></div></div></div>';
// make space at the top for the pager
thefacetview += '<div class="facetview_metadata" style="margin-top:20px;"></div>';
// insert loading notification
thefacetview += '<div class="facetview_searching" style="display:none"></div>'
// insert the table within which the results actually will go
thefacetview += '<table class="table table-striped table-bordered" id="facetview_results" dir="auto"></table>'
// make space at the bottom for the pager
thefacetview += '<div class="facetview_metadata"></div>';
// debug window near the bottom
if (options.debug) {
thefacetview += '<div class="facetview_debug" style="display:none"><textarea style="width: 95%; height: 300px"></textarea></div>'
}
// close off all the big containers and return
thefacetview += '</div></div></div>';
return thefacetview
}
function searchOptions(options) {
/*****************************************
* overrides must provide the following classes and ids
*
* class: facetview_startagain - reset the search parameters
* class: facetview_pagesize - size of each result page
* class: facetview_order - ordering direction of results
* class: facetview_orderby - list of fields which can be ordered by
* class: facetview_searchfield - list of fields which can be searched on
* class: facetview_freetext - input field for freetext search
* class: facetview_force_search - button which triggers a search on the current page status
*
* should (not must) respect the following configs
*
* options.search_sortby - list of sort fields and directions
* options.searchbox_fieldselect - list of fields search can be focussed on
* options.sharesave_link - whether to provide a copy of a link which can be saved
* options.search_button - whether to provide a button to force a search
*/
// share and save link button
var sharesave = "";
if (options.sharesave_link) {
sharesave = '<div class="form-group"> \
<button class="btn btn-default facetview_sharesave" title="Share a link to this search" href="" style="margin-left: 10px"> \
<span class="glyphicon glyphicon-share-alt"></span> \
</button> \
</div>';
}
var sortbutton = "";
if (options.search_sortby.length > 0) {
sortbutton = '<button type="submit" class="btn btn-default facetview_order" title="Current order descending. Click to change to ascending" href="desc"> \
<span class="glyphicon glyphicon-arrow-down"></span> \
</button>';
}
var buttons = '<span class="input-group-btn"> \
<button type="submit" class="btn btn-default facetview_startagain" title="Clear all search settings and start again" href=""> \
<span class="glyphicon glyphicon-remove"></span> \
</button> \
<button type="submit" class="btn btn-default facetview_pagesize" title="Change result set size" href="#">10</button>' + sortbutton + ' \
</span>';
var sortby = "";
if (options.search_sortby.length > 0) {
sortby = '<select class="facetview_orderby form-control"> \
<option value="">order by ... relevance</option>';
for (var each = 0; each < options.search_sortby.length; each++) {
var obj = options.search_sortby[each];
var sortoption = '';
if ($.type(obj['field']) == 'array') {
sortoption = sortoption + '[';
sortoption = sortoption + "'" + obj['field'].join("','") + "'";
sortoption = sortoption + ']';
} else {
sortoption = obj['field'];
}
sortby += '<option value="' + sortoption + '">' + obj['display'] + '</option>';
}
sortby += "</select>";
}
var controls_left = '<div class="form-group"> \
<div class="input-group">' + buttons + sortby + '</div></div>';
var searchfields = "";
if (options.searchbox_fieldselect.length > 0) {
searchfields = '<div class="form-group"> \
<select class="facetview_searchfield form-control"> \
<option value="">Search all</option>';
for (var each = 0; each < options.searchbox_fieldselect.length; each++) {
var obj = options.searchbox_fieldselect[each];
searchfields += '<option value="' + obj['field'] + '">' + obj['display'] + '</option>';
}
searchfields += '</select></div>';
}
var searchbutton = "";
if (options.search_button) {
searchbutton = "<span class='input-group-btn'> \
<button class='btn btn-info facetview_force_search'> \
<span class='glyphicon glyphicon-white glyphicon-search'></span> \
</button> \
</span>";
}
var searchbox = '<div class="form-group"> \
<div class="input-group"> \
<input type="text" class="facetview_freetext form-control" name="q" value="" placeholder="Enter search" />';
searchbox += searchbutton + "</div></div>";
var searchOptions = '<form class="form-inline">' + sharesave + controls_left + searchfields + searchbox + "</form>";
// share and save link
var sharebox = "";
if (options.sharesave_link) {
sharebox = '<div class="facetview_sharesavebox alert alert-info" style="display:none;"> \
<button type="button" class="facetview_sharesave close">×</button> \
<p>Share a link to this search:';
// if there is a url_shortener available, render a link
if (options.url_shortener) {
sharebox += " <a href='#' class='facetview_shorten_url'>(shorten url)</a>";
sharebox += " <a href='#' class='facetview_lengthen_url' style='display: none'>(original url)</a>";
}
sharebox += '</p> \
<textarea class="facetview_sharesaveurl" style="width:100%">' + shareableUrl(options) + '</textarea> \
</div>';
}
return searchOptions + sharebox;
}
function facetList(options) {
/*****************************************
* overrides must provide the following classes and ids
*
* none - no requirements for specific classes and ids
*
* should (not must) respect the following config
*
* options.facet[x].hidden - whether the facet should be displayed in the UI or not
* options.render_terms_facet - renders a term facet into the list
* options.render_range_facet - renders a range facet into the list
* options.render_geo_facet - renders a geo distance facet into the list
*/
if (options.facets.length > 0) {
var filters = options.facets;
var thefilters = '';
for (var idx = 0; idx < filters.length; idx++) {
var facet = filters[idx]
// if the facet is hidden do not include it in this list
if (facet.hidden) {
continue;
}
// note that we do render disabled facets, so that they are available for enabling/disabling
// by callbacks
var type = facet.type ? facet.type : "terms"
if (type === "terms") {
thefilters += options.render_terms_facet(facet, options)
} else if (type === "range") {
thefilters += options.render_range_facet(facet, options)
} else if (type === "geo_distance") {
thefilters += options.render_geo_facet(facet, options)
} else if (type == "date_histogram") {
thefilters += options.render_date_histogram_facet(facet, options)
}
// FIXME: statistical facet and terms_stats facet?
}
return thefilters
}
return ""
}
function renderTermsFacet(facet, options) {
/*****************************************
* overrides must provide the following classes and ids
*
* id: facetview_filter_<safe filtername> - table for the specific filter
* class: facetview_morefacetvals - for increasing the size of the facet
* id: facetview_facetvals_<safe filtername> - id of anchor for changing facet vals
* class: facetview_sort - for changing the facet ordering
* id: facetview_sort_<safe filtername> - id of anchor for changing sorting
* class: facetview_or - for changing the default operator
* id: facetview_or_<safe filtername> - id of anchor for changing AND/OR operator
*
* each anchor must also have href="<filtername>"
*
* should (not must) respect the following config
*
* facet.controls - whether the size/sort/bool controls should be shown
*/
// full template for the facet - we'll then go on and do some find and replace
var filterTmpl = '<table id="facetview_filter_{{FILTER_NAME}}" class="facetview_filters table table-bordered table-condensed table-striped" data-href="{{FILTER_EXACT}}"> \
<tr><td><a class="facetview_filtershow" title="filter by {{FILTER_DISPLAY}}" \
style="color:#333; font-weight:bold;" href="{{FILTER_EXACT}}"><i class="glyphicon glyphicon-plus"></i> {{FILTER_DISPLAY}} \
</a>';
if (facet.controls) {
filterTmpl += '<div class="facetview_filteroptions">\
<form class="form-inline">\
<div class="input-group">\
<span class="input-group-btn">\
<button class="btn btn-default btn-sm facetview_morefacetvals" id="facetview_facetvals_{{FILTER_NAME}}" title="Filter list size" href="{{FILTER_EXACT}}">0</button> \
<button class="btn btn-default btn-sm facetview_sort" id="facetview_sort_{{FILTER_NAME}}" title="Filter value order" href="{{FILTER_EXACT}}"></button> \
<button class="btn btn-default btn-sm facetview_or" id="facetview_or_{{FILTER_NAME}}" href="{{FILTER_EXACT}}">OR</button> \
</span>\
</div>\
</form>\
</div>';
}
filterTmpl += '</td></tr> \
</table>';
// put the name of the field into FILTER_NAME and FILTER_EXACT
filterTmpl = filterTmpl.replace(/{{FILTER_NAME}}/g, safeId(facet['field'])).replace(/{{FILTER_EXACT}}/g, facet['field']);
// set the display name of the facet in FILTER_DISPLAY
if ('display' in facet) {
filterTmpl = filterTmpl.replace(/{{FILTER_DISPLAY}}/g, facet['display']);
} else {
filterTmpl = filterTmpl.replace(/{{FILTER_DISPLAY}}/g, facet['field']);
};
return filterTmpl
}
function renderRangeFacet(facet, options) {
/*****************************************
* overrides must provide the following classes and ids
*
* id: facetview_filter_<safe filtername> - table for the specific filter
*
* each anchor must also have href="<filtername>"
*/
// full template for the facet - we'll then go on and do some find and replace
var filterTmpl = '<table id="facetview_filter_{{FILTER_NAME}}" class="facetview_filters table table-bordered table-condensed table-striped" data-href="{{FILTER_EXACT}}"> \
<tr><td><a class="facetview_filtershow" title="filter by {{FILTER_DISPLAY}}" \
style="color:#333; font-weight:bold;" href="{{FILTER_EXACT}}"><i class="glyphicon glyphicon-plus"></i> {{FILTER_DISPLAY}} \
</a> \
</td></tr> \
</table>';
// put the name of the field into FILTER_NAME and FILTER_EXACT
filterTmpl = filterTmpl.replace(/{{FILTER_NAME}}/g, safeId(facet['field'])).replace(/{{FILTER_EXACT}}/g, facet['field']);
// set the display name of the facet in FILTER_DISPLAY
if ('display' in facet) {
filterTmpl = filterTmpl.replace(/{{FILTER_DISPLAY}}/g, facet['display']);
} else {
filterTmpl = filterTmpl.replace(/{{FILTER_DISPLAY}}/g, facet['field']);
}
return filterTmpl
}
function renderGeoFacet(facet, options) {
/*****************************************
* overrides must provide the following classes and ids
*
* id: facetview_filter_<safe filtername> - table for the specific filter
*
* each anchor must also have href="<filtername>"
*/
// full template for the facet - we'll then go on and do some find and replace
var filterTmpl = '<table id="facetview_filter_{{FILTER_NAME}}" class="facetview_filters table table-bordered table-condensed table-striped" data-href="{{FILTER_EXACT}}"> \
<tr><td><a class="facetview_filtershow" title="filter by {{FILTER_DISPLAY}}" \
style="color:#333; font-weight:bold;" href="{{FILTER_EXACT}}"><i class="glyphicon glyphicon-plus"></i> {{FILTER_DISPLAY}} \
</a> \
</td></tr> \
</table>';
// put the name of the field into FILTER_NAME and FILTER_EXACT
filterTmpl = filterTmpl.replace(/{{FILTER_NAME}}/g, safeId(facet['field'])).replace(/{{FILTER_EXACT}}/g, facet['field']);
// set the display name of the facet in FILTER_DISPLAY
if ('display' in facet) {
filterTmpl = filterTmpl.replace(/{{FILTER_DISPLAY}}/g, facet['display']);
} else {
filterTmpl = filterTmpl.replace(/{{FILTER_DISPLAY}}/g, facet['field']);
};
return filterTmpl
}
function renderDateHistogramFacet(facet, options) {
/*****************************************
* overrides must provide the following classes and ids
*
* id: facetview_filter_<safe filtername> - table for the specific filter
*
* each anchor must also have href="<filtername>"
*/
// full template for the facet - we'll then go on and do some find and replace
var filterTmpl = '<table id="facetview_filter_{{FILTER_NAME}}" class="facetview_filters table table-bordered table-condensed table-striped" data-href="{{FILTER_EXACT}}"> \
<tbody><tr><td><a class="facetview_filtershow" title="filter by {{FILTER_DISPLAY}}" \
style="color:#333; font-weight:bold;" href="{{FILTER_EXACT}}"><i class="glyphicon glyphicon-plus"></i> {{FILTER_DISPLAY}} \
</a> \
</td></tr></tbody> \
</table>';
// put the name of the field into FILTER_NAME and FILTER_EXACT
filterTmpl = filterTmpl.replace(/{{FILTER_NAME}}/g, safeId(facet['field'])).replace(/{{FILTER_EXACT}}/g, facet['field']);
// set the display name of the facet in FILTER_DISPLAY
if ('display' in facet) {
filterTmpl = filterTmpl.replace(/{{FILTER_DISPLAY}}/g, facet['display']);
} else {
filterTmpl = filterTmpl.replace(/{{FILTER_DISPLAY}}/g, facet['field']);
};
return filterTmpl
}
function renderTermsFacetValues(options, facet) {
/*****************************************
* overrides must provide the following classes and ids
*
* class: facetview_filtervalue - wrapper element for any value included in the list
* class: facetview_filterselected - for any anchors around selected filters
* class: facetview_clear - for any link which should remove a filter (must also provide data-field and data-value)
* class: facetview_filterchoice - tags the anchor wrapped around the name of the (unselected) field
*
* should (not must) respect the following config
*
* options.selected_filters_in_facet - whether to show selected filters in the facet pull-down (if that's your idiom)
* options.render_facet_result - function which renders the individual facets
* facet.value_function - the value function to be applied to all displayed values
*/
var selected_filters = options.active_filters[facet.field];
var frag = "";
// first render the active filters
if (options.selected_filters_in_facet) {
if (selected_filters) {
for (var i=0; i < selected_filters.length; i=i+1) {
var value = selected_filters[i];
if (facet.value_function) {
value = facet.value_function(value)
}
var sf = '<tr class="facetview_filtervalue" style="display:none;"><td>';
sf += "<strong>" + value + "</strong> ";
sf += '<button class="facetview_filterselected facetview_clear" data-field="' + facet.field + '" data-value="' + escapeHtml(value) + '" href="' + escapeHtml(value) + '" style="border: 0px; background: #ffffff"><i class="glyphicon glyphicon-black glyphicon-remove"></i></button>'
sf += "</td></tr>";
frag += sf
}
}
}
// is there a pre-defined filter on this facet?
var predefined = facet.field in options.predefined_filters ? options.predefined_filters[facet.field] : [];
// then render the remaining selectable facets
for (var i=0; i < facet["values"].length; i=i+1) {
var f = facet["values"][i];
if (options.exclude_predefined_filters_from_facets && $.inArray(f.term, predefined) > -1) { // note that the datatypes have to match
continue
}
if ($.inArray(f.term.toString(), selected_filters) === -1) { // the toString helps us with non-string filters (e.g integers)
var append = options.render_terms_facet_result(options, facet, f, selected_filters);
frag += append
}
}
return frag
}
function renderRangeFacetValues(options, facet) {
/*****************************************
* overrides must provide the following classes and ids
*
* class: facetview_filtervalue - wrapper element for any value included in the list
* class: facetview_filterselected - for any anchors around selected filters
* class: facetview_clear - for any link which should remove a filter (must also provide data-field and data-value)
* class: facetview_filterchoice - tags the anchor wrapped around the name of the (unselected) field
*
* should (not must) respect the following config
*
* options.selected_filters_in_facet - whether to show selected filters in the facet pull-down (if that's your idiom)
* options.render_facet_result - function which renders the individual facets
*/
function getValueForRange(range, values) {
for (var i=0; i < values.length; i=i+1) {
var value = values[i];
// the "to"s match if they both value and range have a "to" and they are the same, or if neither have a "to"
var match_to = (value.to && range.to && value.to === range.to) || (!value.to && !range.to);
// the "from"s match if they both value and range have a "from" and they are the same, or if neither have a "from"
var match_from = (value.from && range.from && value.from === range.from) || (!value.from && !range.from);
if (match_to && match_from) {
return value
}
}
}
function getRangeForValue(value, facet) {
for (var i=0; i < facet.range.length; i=i+1) {
var range = facet.range[i];
// the "to"s match if they both value and range have a "to" and they are the same, or if neither have a "to"
var match_to = (value.to && range.to && value.to === range.to.toString()) || (!value.to && !range.to);
// the "from"s match if they both value and range have a "from" and they are the same, or if neither have a "from"
var match_from = (value.from && range.from && value.from === range.from.toString()) || (!value.from && !range.from);
if (match_to && match_from) {
return range
}
}
}
var selected_range = options.active_filters[facet.field];
var frag = "";
// render the active filter if there is one
if (options.selected_filters_in_facet && selected_range) {
var range = getRangeForValue(selected_range, facet);
already_selected = true;
var data_to = range.to ? " data-to='" + range.to + "' " : "";
var data_from = range.from ? " data-from='" + range.from + "' " : "";
var sf = '<tr class="facetview_filtervalue" style="display:none;"><td>';
sf += "<strong>" + range.display + "</strong> ";
sf += '<a class="facetview_filterselected facetview_clear" data-field="' + facet.field + '" ' + data_to + data_from + ' href="#"><i class="glyphicon glyphicon-black glyphicon-remove" style="margin-top:1px;"></i></a>';
sf += "</td></tr>";
frag += sf;
// if a range is already selected, we don't render any more
return frag
}
// then render the remaining selectable facets if necessary
for (var i=0; i < facet["range"].length; i=i+1) {
var r = facet["range"][i];
var f = getValueForRange(r, facet["values"]);
if (f) {
if (f.count === 0 && facet.hide_empty_range) {
continue
}
var append = options.render_range_facet_result(options, facet, f, r);
frag += append
}
}
return frag
}
function renderGeoFacetValues(options, facet) {
/*****************************************
* overrides must provide the following classes and ids
*
* class: facetview_filtervalue - wrapper element for any value included in the list
* class: facetview_filterselected - for any anchors around selected filters
* class: facetview_clear - for any link which should remove a filter (must also provide data-field and data-value)
* class: facetview_filterchoice - tags the anchor wrapped around the name of the (unselected) field
*
* should (not must) respect the following config
*
* options.selected_filters_in_facet - whether to show selected filters in the facet pull-down (if that's your idiom)
* options.render_facet_result - function which renders the individual facets
*/
function getValueForRange(range, values) {
for (var i=0; i < values.length; i=i+1) {
var value = values[i];
// the "to"s match if they both value and range have a "to" and they are the same, or if neither have a "to"
var match_to = (value.to && range.to && value.to === range.to) || (!value.to && !range.to);
// the "from"s match if they both value and range have a "from" and they are the same, or if neither have a "from"
var match_from = (value.from && range.from && value.from === range.from) || (!value.from && !range.from);
if (match_to && match_from) {
return value
}
}
}
function getRangeForValue(value, facet) {
for (var i=0; i < facet.distance.length; i=i+1) {
var range = facet.distance[i];
// the "to"s match if they both value and range have a "to" and they are the same, or if neither have a "to"
var match_to = (value.to && range.to && value.to === range.to.toString()) || (!value.to && !range.to);
// the "from"s match if they both value and range have a "from" and they are the same, or if neither have a "from"
var match_from = (value.from && range.from && value.from === range.from.toString()) || (!value.from && !range.from);
if (match_to && match_from) {
return range
}
}
}
var selected_geo = options.active_filters[facet.field];
var frag = "";
// render the active filter if there is one
if (options.selected_filters_in_facet && selected_geo) {
var range = getRangeForValue(selected_geo, facet);
already_selected = true;
var data_to = range.to ? " data-to='" + range.to + "' " : "";
var data_from = range.from ? " data-from='" + range.from + "' " : "";
var sf = '<tr class="facetview_filtervalue" style="display:none;"><td>'
sf += "<strong>" + range.display + "</strong> "
sf += '<a class="facetview_filterselected facetview_clear" data-field="' + facet.field + '" ' + data_to + data_from + ' href="#"><i class="glyphicon glyphicon-black glyphicon-remove" style="margin-top:1px;"></i></a>'
sf += "</td></tr>"
frag += sf
// if a range is already selected, we don't render any more
return frag
}
// then render the remaining selectable facets if necessary
for (var i=0; i < facet["distance"].length; i=i+1) {
var r = facet["distance"][i]
var f = getValueForRange(r, facet["values"])
if (f) {
if (f.count === 0 && facet.hide_empty_distance) {
continue
}
var append = options.render_geo_facet_result(options, facet, f, r)
frag += append
}
}
return frag
}
function renderDateHistogramValues(options, facet) {
/*****************************************
* overrides must provide the following classes and ids
*
* class: facetview_filtervalue - wrapper element for any value included in the list
* class: facetview_filterselected - for any anchors around selected filters
* class: facetview_clear - for any link which should remove a filter (must also provide data-field and data-value)
* class: facetview_filterchoice - tags the anchor wrapped around the name of the (unselected) field
*
* should (not must) respect the following config
*
* options.selected_filters_in_facet - whether to show selected filters in the facet pull-down (if that's your idiom)
* options.render_facet_result - function which renders the individual facets
*/
var selected_range = options.active_filters[facet.field];
var frag = "";
// render the active filter if there is one
if (options.selected_filters_in_facet && selected_range) {
var from = selected_range.from;
var data_from = " data-from='" + from + "' ";
var display = from;
if (facet.value_function) {
display = facet.value_function(display);
}
var sf = '<tr class="facetview_filtervalue" style="display:none;"><td>';
sf += "<strong>" + display + "</strong> ";
sf += '<a class="facetview_filterselected facetview_clear" data-field="' + facet.field + '" '+ data_from + ' href="#"><i class="glyphicon glyphicon-black glyphicon-remove" style="margin-top:1px;"></i></a>';
sf += "</td></tr>";
frag += sf;
// if a range is already selected, we don't render any more
return frag
}
// then render the remaining selectable facets if necessary
// get the facet values in the right order for display
var values = facet["values"];
if (facet.sort === "desc") {
values.reverse()
}
var full_frag = "<tbody class='facetview_date_histogram_full table-striped-inverted' style='display: none'>";
var short_frag = "<tbody class='facetview_date_histogram_short table-striped-inverted'>";
for (var i = 0; i < values.length; i++) {
var f = values[i];
if (f) {
if (f.count === 0 && facet.hide_empty_date_bin) {
continue
}
var next = false;
if (facet.sort === "asc") {
if (i + 1 < values.length) {
next = values[i + 1]
}
} else if (facet.sort === "desc") {
if (i - 1 >= 0) {
next = values[i - 1];
}
}
var append = options.render_date_histogram_result(options, facet, f, next);
full_frag += append;
if (!facet["short_display"]) {
short_frag += append;
}
else if (facet["short_display"] && i < facet["short_display"]) {
short_frag += append;
}
}
}
full_frag += '<tr class="facetview_filtervalue" style="display:none;"><td><a href="#" class="facetview_date_histogram_showless" data-facet="' + facet['field'] + '">show less</a></td></tr>';
full_frag += "</tbody>";
if (facet["short_display"] && values.length > facet["short_display"]) {
short_frag += '<tr class="facetview_filtervalue" style="display:none;"><td><a href="#" class="facetview_date_histogram_showall" data-facet="' + facet['field'] + '">show all</a></td></tr>';
}
short_frag += "</tbody>";
return short_frag + full_frag;
}
function renderTermsFacetResult(options, facet, result, selected_filters) {
/*****************************************
* overrides must provide the following classes and ids
*
* class: facetview_filtervalue - tags the top level element as being a facet result
* class: facetview_filterchoice - tags the anchor wrapped around the name of the field
*
* should (not must) respect the following configuration:
*
* facet.value_function - the value function to be applied to all displayed values
*/
var display = result.term;
if (facet.value_function) {
display = facet.value_function(display)
}
var append = '<tr class="facetview_filtervalue" style="display:none;"><td><a class="facetview_filterchoice' +
'" data-field="' + facet['field'] + '" data-value="' + escapeHtml(result.term) + '" href="' + escapeHtml(result.term) +
'"><span class="facetview_filterchoice_text" dir="auto">' + display + '</span>' +
'<span class="facetview_filterchoice_count" dir="ltr"> (' + result.count + ')</span></a></td></tr>';
return append
}
function renderRangeFacetResult(options, facet, result, range) {
/*****************************************
* overrides must provide the following classes and ids
*
* class: facetview_filtervalue - tags the top level element as being a facet result
* class: facetview_filterchoice - tags the anchor wrapped around the name of the field
*/
var data_to = range.to ? " data-to='" + range.to + "' " : "";
var data_from = range.from ? " data-from='" + range.from + "' " : "";
var append = '<tr class="facetview_filtervalue" style="display:none;"><td><a class="facetview_filterchoice' +
'" data-field="' + facet['field'] + '" ' + data_to + data_from + ' href="#"><span class="facetview_filterchoice_text" dir="auto">' + range.display + '</span>' +
'<span class="facetview_filterchoice_count" dir="ltr"> (' + result.count + ')</span></a></td></tr>';
return append
}
function renderGeoFacetResult(options, facet, result, range) {
/*****************************************
* overrides must provide the following classes and ids
*
* class: facetview_filtervalue - tags the top level element as being a facet result
* class: facetview_filterchoice - tags the anchor wrapped around the name of the field
*/
var data_to = range.to ? " data-to='" + range.to + "' " : "";
var data_from = range.from ? " data-from='" + range.from + "' " : "";
var append = '<tr class="facetview_filtervalue" style="display:none;"><td><a class="facetview_filterchoice' +
'" data-field="' + facet['field'] + '" ' + data_to + data_from + ' href="#"><span class="facetview_filterchoice_text" dir="auto">' + range.display + '</span>' +
'<span class="facetview_filterchoice_count" dir="ltr"> (' + result.count + ')</span></a></td></tr>';
return append
}
function renderDateHistogramResult(options, facet, result, next) {
/*****************************************
* overrides must provide the following classes and ids
*
* class: facetview_filtervalue - tags the top level element as being a facet result
* class: facetview_filterchoice - tags the anchor wrapped around the name of the field
*/
var data_from = result.time ? " data-from='" + result.time + "' " : "";
var data_to = next ? " data-to='" + next.time + "' " : "";
var display = result.time;
if (facet.value_function) {
display = facet.value_function(display)
}
var append = '<tr class="facetview_filtervalue" style="display:none;"><td><a class="facetview_filterchoice' +
'" data-field="' + facet['field'] + '" ' + data_to + data_from + ' href="#"><span class="facetview_filterchoice_text" dir="auto">' + escapeHtml(display) + '</span>' +
'<span class="facetview_filterchoice_count" dir="ltr"> (' + result.count + ')</span></a></td></tr>';
return append
}
function searchingNotification(options) {
return "SEARCHING..."
}
function basicPager(options) {
/*****************************************
* overrides must provide the following classes and ids
*
* class: facetview_decrement - anchor to move the page back
* class: facetview_increment - anchor to move the page forward
* class: facetview_inactive_link - for links which should not have any effect (helpful for styling bootstrap lists without adding click features)
*
* should (not must) respect the config
*
* options.from - record number results start from (may be a string)
* options.page_size - number of results per page
* options.data.found - the total number of records in the search result set
*/
// ensure our starting points are integers, then we can do maths on them
var from = parseInt(options.from);
var size = parseInt(options.page_size);
// calculate the human readable values we want
var to = from + size;
from = from + 1; // zero indexed
if (options.data.found < to) { to = options.data.found }
var total = options.data.found;
// forward and back-links, taking into account start and end boundaries
var backlink = '<a class="facetview_decrement">« back</a>';
if (from < size) { backlink = "<a class='facetview_decrement facetview_inactive_link'>..</a>" }
var nextlink = '<a class="facetview_increment">next »</a>';
if (options.data.found <= to) { nextlink = "<a class='facetview_increment facetview_inactive_link'>..</a>" }
var meta = '<div><ul class="pagination">';
meta += '<li class="prev">' + backlink + '</li>';
meta += '<li class="active"><a>' + from + ' – ' + to + ' of ' + total + '</a></li>';
meta += '<li class="next">' + nextlink + '</li>';
meta += "</ul></div>";
return meta
}
function pageSlider(options) {
/*****************************************
* overrides must provide the following classes and ids
*
* class: facetview_decrement - anchor to move the page back
* class: facetview_increment - anchor to move the page forward
* class: facetview_inactive_link - for links which should not have any effect (helpful for styling bootstrap lists without adding click features)
*
* should (not must) respect the config
*
* options.from - record number results start from (may be a string)
* options.page_size - number of results per page
* options.data.found - the total number of records in the search result set
*/
// ensure our starting points are integers, then we can do maths on them
var from = parseInt(options.from);
var size = parseInt(options.page_size);
// calculate the human readable values we want
var to = from + size;
from = from + 1; // zero indexed
if (options.data.found < to) { to = options.data.found }
var total = options.data.found;
// forward and back-links, taking into account start and end boundaries
var backlink = '<a alt="previous" title="previous" class="facetview_decrement" style="color:#333;float:left;padding:0 40px 20px 20px;"><</a>';
if (from < size) {
backlink = '<a class="facetview_decrement facetview_inactive_link" style="color:#333;float:left;padding:0 40px 20px 20px;">..</a>'
}
var nextlink = '<a alt="next" title="next" class="facetview_increment" style="color:#333;float:right;padding:0 20px 20px 40px;">></a>';
if (options.data.found <= to) {
nextlink = '<a class="facetview_increment facetview_inactive_link" style="color:#333;float:right;padding:0 20px 20px 40px;">..</a>'
}
var meta = '<div style="font-size:20px;font-weight:bold;margin:5px 0 10px 0;padding:5px 0 5px 0;border:1px solid #eee;border-radius:5px;-moz-border-radius:5px;-webkit-border-radius:5px;">';
meta += backlink;
meta += '<span style="margin:30%;">' + from + ' – ' + to + ' of ' + total + '</span>';
meta += nextlink;
meta += '</div>';
return meta
}
function renderNotFound() {
/*****************************************
* overrides must provide the following classes and ids
*
* class: facetview_not_found - the id of the top level element containing the not found message
*/
return "<tr class='facetview_not_found'><td>No results found</td></tr>"
}
function renderResultRecord(options, record) {
/*****************************************
* overrides must provide the following classes and ids
*
* none - no specific requirements
*
* should (not must) use the config
*
* options.resultwrap_start - starting elements for any result object
* options.resultwrap_end - closing elements for any result object
* options.result_display - line-by-line display commands for the result object
*/
// get our custom configuration out of the options
var result = options.resultwrap_start;
var display = options.result_display;
// build up a full string representing the object
var lines = '';
for (var lineitem = 0; lineitem < display.length; lineitem++) {
var line = "";
for (var object = 0; object < display[lineitem].length; object++) {
var thekey = display[lineitem][object]['field'];
var thevalue = "";
if (typeof options.results_render_callbacks[thekey] == 'function') {
// a callback is defined for this field so just call it
thevalue = options.results_render_callbacks[thekey].call(this, record);
} else {
// split the key up into its parts, and work our way through the
// tree until we get to the node to display. Note that this will only
// work with a string hierarchy of dicts - it can't have lists in it
var parts = thekey.split('.');
var res = record;
for (var i = 0; i < parts.length; i++) {
if (res) {
res = res[parts[i]]
} else {
continue
}
}
// just get a string representation of the object
if (res) {
if ($.isArray(res)) {
thevalue = res.join(", ")
} else {
thevalue = res.toString()
}
}
thevalue = escapeHtml(thevalue);
}
// if we have a value to display, sort out the pre-and post- stuff and build the new line
if (thevalue && thevalue.toString().length) {
if (display[lineitem][object]['pre']) {
line += display[lineitem][object]['pre']
}
line += thevalue;
if (display[lineitem][object]['post']) {
line += display[lineitem][object]['post'];
} else if(!display[lineitem][object]['notrailingspace']) {
line += ' ';
}
}
}
// if we have a line, append it to the full lines and add a line break
if (line) {
lines += line.replace(/^\s/,'').replace(/\s$/,'').replace(/\,$/,'') + "<br />";
}
}
// if we have the lines, append them to the result wrap start
if (lines) {
result += lines
}
// close off the result with the ending strings, and then return
result += options.resultwrap_end;
return result;
}
function renderActiveTermsFilter(options, facet, field, filter_list) {
/*****************************************
* overrides must provide the following classes and ids
*
* class: facetview_filterselected - anchor tag for any clickable filter selection
* class: facetview_clear - anchor tag for any link which will remove the filter (should also provide data-value and data-field)
* class: facetview_inactive_link - any link combined with facetview_filterselected which should not execute when clicked
*
* should (not must) respect the config
*
* options.show_filter_field - whether to include the name of the field the filter is active on
* options.show_filter_logic - whether to include AND/OR along with filters
* facet.value_function - the value function to be applied to all displayed values
*/
var clean = safeId(field);
var display = facet.display ? facet.display : facet.field;
var logic = facet.logic ? facet.logic : options.default_facet_operator;
var frag = "<div id='facetview_filter_group_" + clean + "' class='btn-group' style='margin-left: 20px'>";
if (options.show_filter_field) {
frag += '<span class="facetview_filterselected_text"><strong>' + display + ':</strong> </span>';
}
for (var i = 0; i < filter_list.length; i++) {
var value = filter_list[i];
if (facet.value_function) {
value = facet.value_function(value)
}
frag += '<span class="facetview_filterselected_text">' + value + '</span> ';
frag += '<a class="facetview_filterselected facetview_clear" data-field="' + field + '" data-value="' + value + '" alt="remove" title="Remove" href="' + value + '">';
frag += '<i class="glyphicon glyphicon-black glyphicon-remove" style="margin-top:1px;"></i>';
frag += "</a>";