-
Notifications
You must be signed in to change notification settings - Fork 34
/
SearchbarNavImprovements.user.js
2127 lines (1976 loc) · 75.2 KB
/
SearchbarNavImprovements.user.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
// ==UserScript==
// @name Searchbar & Nav Improvements
// @description Searchbar & Nav Improvements. Advanced search helper when search box is focused. Bookmark any search for reuse (stored locally, per-site).
// @homepage https://github.com/samliew/SO-mod-userscripts
// @author Samuel Liew
// @version 7.0.13
//
// @match https://*.stackoverflow.com/*
// @match https://*.serverfault.com/*
// @match https://*.superuser.com/*
// @match https://*.askubuntu.com/*
// @match https://*.mathoverflow.net/*
// @match https://*.stackapps.com/*
// @match https://*.stackexchange.com/*
// @match https://stackoverflowteams.com/*
//
// @exclude https://api.stackexchange.com/*
// @exclude https://data.stackexchange.com/*
// @exclude https://contests.stackoverflow.com/*
// @exclude https://winterbash*.stackexchange.com/*
// @exclude *chat.*
// @exclude *blog.*
// @exclude */tour
//
// @require https://raw.githubusercontent.com/samliew/SO-mod-userscripts/master/lib/se-ajax-common.js
// @require https://raw.githubusercontent.com/samliew/SO-mod-userscripts/master/lib/common.js
// ==/UserScript==
/* globals StackExchange, store, isMSE, parentUrl, metaUrl, siteApiSlug */
/// <reference types="./globals" />
'use strict';
const svgicons = {
delete: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M336 64l-33.6-44.8C293.3 7.1 279.1 0 264 0h-80c-15.1 0-29.3 7.1-38.4 19.2L112 64H24C10.7 64 0 74.7 0 88v2c0 3.3 2.7 6 6 6h26v368c0 26.5 21.5 48 48 48h288c26.5 0 48-21.5 48-48V96h26c3.3 0 6-2.7 6-6v-2c0-13.3-10.7-24-24-24h-88zM184 32h80c5 0 9.8 2.4 12.8 6.4L296 64H152l19.2-25.6c3-4 7.8-6.4 12.8-6.4zm200 432c0 8.8-7.2 16-16 16H80c-8.8 0-16-7.2-16-16V96h320v368zm-176-44V156c0-6.6 5.4-12 12-12h8c6.6 0 12 5.4 12 12v264c0 6.6-5.4 12-12 12h-8c-6.6 0-12-5.4-12-12zm-80 0V156c0-6.6 5.4-12 12-12h8c6.6 0 12 5.4 12 12v264c0 6.6-5.4 12-12 12h-8c-6.6 0-12-5.4-12-12zm160 0V156c0-6.6 5.4-12 12-12h8c6.6 0 12 5.4 12 12v264c0 6.6-5.4 12-12 12h-8c-6.6 0-12-5.4-12-12z"/></svg>',
bookmark: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><path d="M0 512V48C0 21.49 21.49 0 48 0h288c26.51 0 48 21.49 48 48v464L192 400 0 512z"/></svg>',
refresh: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M481.162 164.326c19.478 25.678 30.997 57.709 30.836 92.388C511.61 340.638 442.361 408 358.436 408H176v64c-.001 10.683-12.949 16.021-20.485 8.485l-88-87.995c-4.686-4.686-4.687-12.284 0-16.971l88-88.005c7.58-7.58 20.485-2.14 20.485 8.485v64h182.668C415.933 360 464.06 313.154 464 255.889c-.023-22.372-7.149-43.111-19.237-60.082-3.431-4.817-2.962-11.387 1.223-15.564 8.269-8.255 13.592-13.545 17.137-17.104 5.131-5.152 13.645-4.605 18.039 1.187zM48 256.111C47.94 198.846 96.067 152 153.332 152H336v64c0 10.625 12.905 16.066 20.485 8.485l88-88.005c4.687-4.686 4.686-12.285 0-16.971l-88-87.995C348.949 23.979 336.001 29.317 336 40v64H153.564C69.639 104 .389 171.362.002 255.286c-.16 34.679 11.358 66.71 30.836 92.388 4.394 5.792 12.908 6.339 18.039 1.188 3.545-3.559 8.867-8.849 17.137-17.105 4.185-4.178 4.653-10.748 1.223-15.564-12.088-16.971-19.213-37.71-19.237-60.082z"/></svg>',
};
const isChildMeta = typeof StackExchange !== 'undefined' && StackExchange.options?.site?.isChildMeta;
const mainName = StackExchange.options.site.name.replace(/\bmeta\b/i, '').replace(/\bStack Exchange\b/, '').trim();
const hasSearchResults = (location.pathname === '/search' && location.search.length > 2) || location.pathname.indexOf('/questions/tagged/') == 0;
const searchSelector = $(`<div class="flex--item s-select wmn1"><select id="search-channel-selector" class="search-channel-switcher w100 pr24">
<option data-url="${parentUrl}/search" ${!isChildMeta ? 'selected="selected"' : ''}>${mainName}</option>
<option data-url="${metaUrl}/search" ${isChildMeta ? 'selected="selected"' : ''}>Meta</option>
<option data-url="https://meta.stackexchange.com/search">Meta Stack Exchange</option>
</select></div>`);
const lsidebar = $('#left-sidebar');
const searchform = $('#search');
const searchfield = $('#search input[name="q"]');
let searchbtn = $('#search .js-search-submit');
// If search button is missing due to layout change, attempt to re-insert a semi-hidden one
if (searchbtn.length === 0) {
searchbtn = $(`<button type="submit" class="js-search-submit alt-submit-button"></button>`).insertAfter(searchfield);
}
const autoRefreshDefaultSecs = 60;
let searchhelper, orderby, autoRefreshTimeout;
// Fetch and store last sort option
const sortKeyRoot = 'SavedSearch-SearchLastSort';
function setLastSort(val) {
store.setItem(sortKeyRoot, val);
}
function getLastSort() {
return store.getItem(sortKeyRoot);
}
// Fetch and store watched/ignored tags
const wtKeyRoot = 'SavedSearch-TagsWatched';
const itKeyRoot = 'SavedSearch-TagsIgnored';
function tryUpdateWatchedIgnoredTags() {
if ($('.js-tag-preferences-container').length === 0) return;
const wTags = $('.js-watched-tag-list .post-tag').map((i, el) => el.text).get() || [];
const iTags = $('.js-ignored-tag-list .post-tag').map((i, el) => el.text).get() || [];
store.setItem(wtKeyRoot, JSON.stringify(wTags));
store.setItem(itKeyRoot, JSON.stringify(iTags));
}
function getWatchedTags() {
return (JSON.parse(store.getItem(wtKeyRoot)) || []).join(' ');
}
function getIgnoredTags() {
return (JSON.parse(store.getItem(itKeyRoot)) || []).join(' ');
}
function loadSvgIcons() {
$('[data-svg]').each(function () {
if ($(this).children('svg').length > 0) return; // once
const ico = svgicons[this.dataset.svg];
if (ico) $(this).append(ico);
});
}
// Display name to ID lookup plugin
jQuery.fn.dnLookup = function (multiple = false, delay = 800) {
const field = $(this);
let debounceDuration = delay;
let acTimeout = null;
function doDnLookup(el) {
const query = encodeURIComponent(multiple ? el.value.trim().replace(/^.+\s/, '') : el.value.trim());
const resultElem = $(el).nextAll('.aclookup_results').html('<li class="disabled" data-val>loading...</li>');
const field = $(el).addClass('js-aclookup-complete');
$.get('${seApiUrl}/users?filter=!)RwcIFN1JaCrhVpgyYeR_oO*&order=desc&sort=reputation&inname=' + query + '&site=' + siteApiSlug, function (data) {
const resultlist = data.items.map(v => `<li data-val="${v.user_id}"><img src="${v.profile_image.replace('=128', '=16')}" /> ${v.display_name}</li>`).join('');
resultElem.html(resultlist);
});
}
const resultslist = $(`<ul class="aclookup_results"></ul>`)
.on('click', 'li', function (evt) {
const field = $(this).parent().prevAll('input').first();
field.removeClass('js-aclookup-complete');
field.val((i, v) => ((multiple ? (' ' + v).replace(/\s\S+$/, '') : '') + ' ' + evt.target.dataset.val).trim() + ' ');
field.triggerHandler('keyup');
});
field.after(resultslist)
.on('keydown blur', function (evt) {
if (acTimeout) clearTimeout(acTimeout);
$(this).removeClass('js-aclookup-complete').next('.aclookup_results').html();
})
.on('keyup', function (evt) {
if (acTimeout) clearTimeout(acTimeout);
if (evt.target.value.trim().length > 1) {
acTimeout = setTimeout(doDnLookup, debounceDuration, evt.target);
}
});
};
// Tags lookup plugin
jQuery.fn.tagLookup = function (multiple = false, delay = 800) {
const field = $(this);
let debounceDuration = delay;
let acTimeout = null;
function doTagLookup(el) {
const query = encodeURIComponent(multiple ? el.value.trim().replace(/^.+\s/, '') : el.value.trim());
const resultElem = $(el).siblings('.aclookup_results').html('<li class="disabled" data-val>loading...</li>');
const field = $(el).addClass('js-aclookup-complete');
$.get('${seApiUrl}/tags?filter=!*MPoAL(KAgsdNw0T&order=desc&sort=popular&inname=' + query + '&site=' + siteApiSlug, function (data) {
const resultlist = data.items.map(v => `<li data-val="${v.name}">${v.name}</li>`).join('');
resultElem.html(resultlist);
});
}
const resultslist = $(`<ul class="aclookup_results"></ul>`)
.on('click', 'li', function (evt) {
const field = $(this).parent().prevAll('input').first();
field.removeClass('js-aclookup-complete');
field.val((i, v) => ((multiple ? (' ' + v).replace(/\s\S+$/, '') : '') + ' ' + evt.target.dataset.val).trim() + ' ');
});
field.after(resultslist)
.on('keydown blur', function (evt) {
if (acTimeout) clearTimeout(acTimeout);
$(this).removeClass('js-aclookup-complete').next('.aclookup_results').html();
})
.on('keyup', function (evt) {
if (acTimeout) clearTimeout(acTimeout);
if (evt.target.value.trim().length > 1) {
acTimeout = setTimeout(doTagLookup, debounceDuration, evt.target);
}
});
// Prevent tag brackets [] from being typed
field
.on('keydown blur', function (evt) {
return /[^\[\]]/.test(evt.key);
})
.on('change blur', function (evt) {
this.value = this.value.trim().replace(/[\[\]]/g, '').replace(/\s+/g, ' ');
});
};
// Saved Search helper functions
const ssKeyRoot = 'SavedSearch';
// Sanitize: strip page, convert to lowercase
function sanitizeQuery(value) {
return value.toLowerCase()
.replace(/[?&]page=\d+/, '')
.replace(/[?&]pagesize=\d+/, '')
.replace(/[?&]refresh=\d+/, '')
.replace(/^[&]/, '?')
.replace(/%20/g, '+')
.replace('tab=&', 'tab=relevance&');
}
function addSavedSearch(value, append = false) {
if (value == null || value == '') return false;
value = sanitizeQuery(value);
let items = getSavedSearches();
if (append) {
items.push(value); // add to end
}
else {
items.unshift(value); // add to beginning
}
store.setItem(ssKeyRoot, JSON.stringify(items));
}
function addSavedSearches(arrayValues) {
if (typeof arrayValues !== 'object' || arrayValues.length == 0) return false;
arrayValues.forEach(o => addSavedSearch(o, true));
}
function hasSavedSearch(value) {
if (value == null || value == '') return false;
value = sanitizeQuery(value);
const items = getSavedSearches();
const result = jQuery.grep(items, function (v) {
return v == value;
});
return result.length > 0;
}
function removeSavedSearch(value) {
if (value == null || value == '') return false;
value = sanitizeQuery(value);
const items = getSavedSearches();
const result = jQuery.grep(items, function (v) {
return v != value;
});
store.setItem(ssKeyRoot, JSON.stringify(result));
}
function removeAllSavedSearches() {
store.setItem(ssKeyRoot, JSON.stringify([]));
}
function getSavedSearches() {
return JSON.parse(store.getItem(ssKeyRoot)) || [];
}
function humanizeSearchQuery(value) {
if (value == null || value == '') return false;
value = decodeURIComponent(value);
value = sanitizeQuery(value)
.replace(/[?&][a-z]+=/g, ' ')
.replace(/\+/g, ' ')
.trim();
return value;
}
// Search auto-refresh helper functions
const afKeyRoot = 'SavedSearch-AutoRefresh';
let animInterval;
function addAutoRefresh(value, duration = autoRefreshDefaultSecs) {
if (value == null || value == '') return false;
value = sanitizeQuery(value);
let items = getAutoRefreshes();
items.push([value, duration]);
store.setItem(afKeyRoot, JSON.stringify(items));
}
function getAutoRefreshDuration(value) {
if (value == null || value == '') return false;
value = sanitizeQuery(value);
const items = getAutoRefreshes();
const result = jQuery.grep(items, function (v) {
return v[0] == value;
});
if (result.length > 0 && result[0] && result[0][1]) {
return Number(result[0][1]);
}
return false;
}
function removeAutoRefresh(value) {
if (value == null || value == '') return false;
value = sanitizeQuery(value);
const items = getAutoRefreshes();
const result = jQuery.grep(items, function (v) {
return v[0] != value;
});
store.setItem(afKeyRoot, JSON.stringify(result));
}
function removeAllAutoRefreshes() {
store.setItem(afKeyRoot, JSON.stringify([]));
}
function getAutoRefreshes() {
return JSON.parse(store.getItem(afKeyRoot)) || [];
}
function stopAutoRefresh() {
if (autoRefreshTimeout) {
clearTimeout(autoRefreshTimeout);
//console.log(`Auto Refresh stopped`);
}
if (animInterval) {
clearInterval(animInterval);
}
}
function startAutoRefresh(duration = autoRefreshDefaultSecs) {
stopAutoRefresh();
autoRefreshTimeout = setTimeout("location.reload()", duration * 1000);
//console.log(`Auto Refresh started (${duration} seconds)`);
// Animation
const pb = document.querySelector('#btn-auto-refresh .radial-progress');
if (pb) {
pb.dataset.progress = 0;
animInterval = setInterval(function () {
let v = Number(pb.dataset.progress) + 1;
if (v > duration) v = duration;
pb.dataset.progress = v;
}, 1000);
}
}
function initAutoRefresh() {
if (!hasSearchResults) return;
// Has auto refresh?
const currRefreshDurationSecs = getAutoRefreshDuration(location.search);
let refreshDurationSecs = currRefreshDurationSecs || autoRefreshDefaultSecs;
const btnAutoRefresh = $(`
<a id="btn-auto-refresh" class="s-btn" href="#" data-svg="refresh" tabindex="0" title="Auto Refresh (${refreshDurationSecs} seconds)">
<div class="radial-progress" data-progress="0">
<div class="circle">
<div class="mask full">
<div class="fill"></div>
</div>
<div class="mask half">
<div class="fill"></div>
<div class="fill fix"></div>
</div>
</div>
<div class="inset"></div>
</div>
</a>`)
.on('click', function () {
$(this).toggleClass('active');
if ($(this).hasClass('active')) {
addAutoRefresh(location.search);
startAutoRefresh(refreshDurationSecs);
}
else {
removeAutoRefresh(location.search);
stopAutoRefresh();
}
});
// Insert refresh button
btnAutoRefresh.insertBefore('#btn-bookmark-search');
// If set, start auto refresh on page load
if (currRefreshDurationSecs !== false) {
btnAutoRefresh.addClass('active');
startAutoRefresh(currRefreshDurationSecs);
}
}
function initQuickfilters() {
if (!hasSearchResults) return;
let query = sanitizeQuery(location.search);
const lastSort = getLastSort();
const isOnTagPage = query == '';
if (isOnTagPage) {
const tag = location.pathname.split('/').pop();
query = `/search?tab=${lastSort}&q=%5B${tag}%5D`;
}
query = sanitizeQuery(query.toLowerCase());
let quickfilter = $(`
<div id="res-quickfilter">Quick filters:
<div class="grid tabs-filter tt-capitalize">
<a class="flex--item s-btn s-btn__muted s-btn__outlined py8 ws-nowrap ${isOnTagPage ? 'is-selected' : ''}"
href="${removeParam(query, 'is')}+is%3aq" data-onwhen="is%3aq"
data-toggleoff="${removeParam(query, 'is')}">questions</a>
<a class="flex--item s-btn s-btn__muted s-btn__outlined py8 ws-nowrap"
href="${removeParam(query, 'is')}+is%3aa" data-onwhen="is%3aa"
data-toggleoff="${removeParam(query, 'is')}">answers</a>
</div>
<div class="grid tabs-filter tt-capitalize">
<a class="flex--item s-btn s-btn__muted s-btn__outlined py8 ws-nowrap"
href="${removeParam(query, 'deleted')}+deleted%3a1" data-onwhen="deleted%3ayes"
data-toggleoff="${removeParam(query, 'deleted')}+deleted%3aany">deleted</a>
<a class="flex--item s-btn s-btn__muted s-btn__outlined py8 ws-nowrap"
href="${removeParam(query, 'deleted')}+deleted%3a0" data-onwhen="deleted%3ano" data-onwhenmissing="deleted%3a"
data-toggleoff="${removeParam(query, 'deleted')}+deleted%3aany">not deleted</a>
</div>
</div>`).insertAfter('.js-advanced-tips');
quickfilter.find('a[data-onwhen]').each(function () {
let se = this.dataset.onwhen;
if (se.match(/(yes|no)$/) != null) {
se = se.replace(/yes$/, '(yes|1)').replace(/no$/, '(no|0)');
}
const matches = query.toLowerCase().match(se);
if (matches != null || (this.dataset.onwhenmissing && query.toLowerCase().match(this.dataset.onwhenmissing) == null)) {
$(this).addClass('is-selected');
}
});
quickfilter.find('.is-selected').each(function () {
this.href = this.dataset.toggleoff;
});
function removeParam(query, param) {
const re = new RegExp('\\+?' + param + '%3a[a-z0-9]+');
return query.toLowerCase().replace(re, '');
}
}
function initSavedSearch() {
const ss = $('#saved-search');
const btnBookmark = $(`<a id="btn-bookmark-search" class="s-btn" href="#" data-svg="bookmark" tabindex="0" title="Bookmark Search"></a>`)
.on('click', function () {
$(this).toggleClass('active');
if ($(this).hasClass('active')) {
addSavedSearch(location.search);
}
else {
removeSavedSearch(location.search);
}
reloadSavedSearchList();
});
// Button toggle in Search helper
$('#btn-saved-search').on('click', function () {
$(this).toggleClass('active');
});
// Load Saved Searches
function reloadSavedSearchList() {
ss.empty();
const ssitems = getSavedSearches();
$.each(ssitems, function (i, v) {
const readable = humanizeSearchQuery(v);
const sstemplate = $(`
<div class="item" data-value="${v}">
<span class="handle"></span>
<a href="/search${v}">${readable}</a>
<div class="actions">
<a class="delete" data-svg="delete" title="Delete (no confirmation)"></a>
</div>
</div>`).appendTo(ss);
});
loadSvgIcons();
}
reloadSavedSearchList(); // Once on init
// Sortable bookmarks
$.getScript('https://cdn.rawgit.com/RubaXa/Sortable/master/Sortable.js', function () {
// Script validation
if (typeof Sortable === 'undefined') {
console.error('Searchbar & Nav Improvements - Sortable not loaded');
return;
}
Sortable.create(ss.get(0), {
ghostClass: 'sortable-ghost',
onUpdate: function (evt) {
const items = ss.children('.item').map((i, el) => el.dataset.value).get();
removeAllSavedSearches();
addSavedSearches(items);
},
});
});
// Handle delete button
ss.on('click', 'a.delete', function (evt) {
const item = $(this).parents('.item');
removeSavedSearch(item.get(0).dataset.value);
// Update current search page's bookmark
btnBookmark.toggleClass('active', hasSavedSearch(location.search));
item.remove();
return false;
});
// On Search Result page and has search query
if (location.pathname === '/search' && location.search.length > 2) {
// Check if current query is already bookmarked
btnBookmark.toggleClass('active', hasSavedSearch(location.search));
// Replace advanced search link with bookmark link
$('.advanced-tips-toggle, .js-advanced-tips-toggle').after(btnBookmark).remove();
}
}
function handleAdvancedSearch(evt) {
const filledFields = searchhelper.find('input[data-autofill]:text, input[data-autofill]:checked, select[data-autofill]').hasValue();
const rangedFields = searchhelper.find('input[data-range-to], select[data-range-to]');
let addQuery = '';
filledFields.each(function (i, el) {
let currValue = el.value.trim().replace(/\s+/g, ' ');
if (currValue === '') return;
const term = searchhelper.find(`[name="${el.dataset.termvalue}"]:checked`).val() || '';
const neg = el.dataset.neg || '';
const prefix = el.dataset.prefix || '';
const suffix = el.dataset.suffix || '';
const joiner = el.dataset.join || '';
if (prefix !== '"' && suffix !== '"') currValue = currValue.split(' ').join(neg + joiner + term);
addQuery += ' ' + neg + term + prefix + currValue + suffix;
});
rangedFields.each(function (i, el) {
const addSep = el.dataset.additionalSep || '';
const fromValue = el.value;
const fromAdditional = document.getElementById(el.dataset.additional);
const fromAdditionalValue = fromAdditional ? fromAdditional.value : null;
const linkedToField = document.getElementById(el.dataset.rangeTo);
const linkedToValue = linkedToField.value;
const linkedToAdditional = document.getElementById(linkedToField.dataset.additional);
const linkedToAdditionalValue = linkedToAdditional ? linkedToAdditional.value : null;
const linkedSuffixFrom = linkedToField.dataset.suffixFrom ? document.getElementById(linkedToField.dataset.suffixFrom).value : '';
const term = searchhelper.find(`[name="${el.dataset.termvalue}"]:checked`).val() || '';
const prefix = el.dataset.prefix || '';
const suffix = el.dataset.suffix || '';
const suffixFrom = el.dataset.suffixFrom ? document.getElementById(el.dataset.suffixFrom).value : '';
if (fromValue === '' && linkedToValue === '') return;
// Do not validate if NOT age range (because you can use different unit values)
if (el.id !== 'agerange-from') {
// First value must be more than or equal to second value
if (fromValue !== '' && linkedToValue !== '' && Number(fromValue) >= Number(linkedToValue)) return;
}
addQuery += ' ' + term + prefix +
(fromValue ? fromValue + suffixFrom : '') + (fromAdditionalValue ? addSep + fromAdditionalValue : '') + '..' +
(linkedToValue ? linkedToValue + linkedSuffixFrom : '') + (linkedToAdditionalValue ? addSep + linkedToAdditionalValue : '');
});
// Append search to existing field value
searchfield.val((i, v) => (v + addQuery).replace(/\s+/g, ' ').replace(/([:])\s+/g, '$1').trim());
// Move order-by fields before search field so that the resulting query will match SE's format
orderby.hide().insertBefore(searchfield);
// Save last search sort
setLastSort(orderby.find(':checked').val() || 'relevance');
// Remove search helper on submit so it doesn't pollute the query string
searchhelper.remove();
}
function initAdvancedSearch() {
const today = new Date();
// Remove new top-search
$('#top-search').remove();
orderby = $(`<div id="order-by">
<span class="label">Order by: </span>
<input type="radio" name="tab" id="tab-relevance" value="" checked /><label for="tab-relevance">relevance</label>
<input type="radio" name="tab" id="tab-newest" value="newest" /><label for="tab-newest">newest</label>
<input type="radio" name="tab" id="tab-votes" value="votes" /><label for="tab-votes">votes</label>
<input type="radio" name="tab" id="tab-active" value="active" /><label for="tab-active">active</label>
</div>`);
searchhelper = $(`<div id="search-helper" class="search-helper">
<button type="reset" class="btnreset s-btn s-btn__xs s-btn__filled s-btn__danger">Reset</button>
<a id="btn-saved-search" data-svg="bookmark" title="Saved Search"></a>
<div id="saved-search"></div>
<div id="search-helper-tabs" class="tabs">
<a class="youarehere">Text</a>
<a>Tags</a>
<a>Score</a>
<a>Type</a>
<a>Questions</a>
<a>Answers</a>
<a>Status</a>
<a>Author</a>
<a>Saves</a>
<a>Dates</a>
<a>Other</a>
</div>
<div id="search-helper-tabcontent">
<div class="active">
<label class="section-label">Text</label>
<label for="section">search these sections:</label>
<input type="radio" name="section" id="section-any" value="" checked /><label for="section-any">Any</label>
<input type="radio" name="section" id="section-title" value="title:" /><label for="section-title">Title</label>
<input type="radio" name="section" id="section-body" value="body:" /><label for="section-body">Body</label>
<label for="all-words">all these words:</label>
<input name="all-words" id="all-words" data-clearbtn data-autofill data-termvalue="section" data-join=" " data-prefix=' ' />
<label for="exact-phrase">this exact word or phrase:</label>
<input name="exact-phrase" id="exact-phrase" data-clearbtn data-autofill data-termvalue="section" data-prefix='"' data-suffix='"' />
<label for="not-words">excluding these words:</label>
<input name="not-words" id="not-words" data-clearbtn data-autofill data-termvalue="section" data-neg=" -" />
<label for="code-words">in a code block: <em>(exact phrase, case-sensitive)</em></label>
<input name="code-words" id="code-words" data-clearbtn data-autofill data-prefix='code:"' data-suffix='"' />
<label class="section-label">URL</label>
<label for="url">mentions url/domain (accepts * wildcard):</label>
<input name="url" id="url" placeholder="example.com" data-clearbtn data-autofill data-validate-url data-prefix='url:"' data-suffix='"' />
</div>
<div>
<label class="section-label">Tags</label>
<div>
<label for="tags-self">my own tags:</label>
<input type="checkbox" name="tags-self" id="tags-self" value="intags:mine" data-autofill /><label for="tags-self">self</label>
</div>
<label for="tags">all of these tags:</label>
<div><input name="tags" id="tags" class="js-taglookup" data-clearbtn data-autofill data-join="] [" data-prefix="[" data-suffix="]" /></div>
<label for="any-tags">any of these tags:</label>
<div><input name="any-tags" id="any-tags" class="js-taglookup" data-clearbtn data-autofill data-join="] OR [" data-prefix="[" data-suffix="]" />
<div class="checkbox-right">
<input type="checkbox" name="user-watched-tags" id="user-watched-tags" data-autofills-for="#any-tags" value="${getWatchedTags()}" />
<label for="user-watched-tags" title="any watched tag">watched tags</label>
</div>
</div>
<label for="not-tags">excluding these tags:</label>
<div><input name="not-tags" id="not-tags" class="js-taglookup" data-clearbtn data-autofill data-join="] -[" data-prefix="-[" data-suffix="]" />
<div class="checkbox-right">
<input type="checkbox" name="user-ignored-tags" id="user-ignored-tags" data-autofills-for="#not-tags" value="${getIgnoredTags()}" />
<label for="user-ignored-tags" title="exclude all ignored tags">ignored tags</label>
</div>
</div>
</div>
<div>
<label class="section-label">Post Score</label>
<div class="fromto">
<label for="score-from">from:</label>
<input type="number" name="score-from" id="score-from" placeholder="any" data-range-to="score-to" data-prefix="score:" />
<label for="score-to">to:</label>
<input type="number" name="score-to" id="score-to" placeholder="any" />
</div>
</div>
<div>
<label class="section-label">Post Type</label>
<div><input type="radio" name="posttype" id="type-any" value="" checked /><label for="type-any">any</label></div>
<div><input type="radio" name="posttype" id="type-q" value="is:q" data-autofill data-clears-tab="#tab-answers" /><label for="type-q">question</label></div>
<div><input type="radio" name="posttype" id="type-a" value="is:a" data-autofill data-clears-tab="#tab-questions" /><label for="type-a">answer</label></div>
</div>
<div id="tab-questions" class="fixed-width-radios">
<label class="section-label">Questions</label>
<div>
<label class="radio-group-label">closed:</label>
<input type="radio" name="status-closed" id="status-closed-any" value="" checked /><label for="status-closed-any">any</label>
<input type="radio" name="status-closed" id="status-closed-yes" value="closed:yes" data-autofill data-checks="#type-q" /><label for="status-closed-yes">yes</label>
<input type="radio" name="status-closed" id="status-closed-no" value="closed:no" data-autofill data-checks="#type-q" /><label for="status-closed-no">no</label>
</div>
<div>
<label class="radio-group-label">duplicate:</label>
<input type="radio" name="status-duplicate" id="status-duplicate-any" value="" checked /><label for="status-duplicate-any">any</label>
<input type="radio" name="status-duplicate" id="status-duplicate-yes" value="duplicate:yes" data-autofill data-checks="#type-q" /><label for="status-duplicate-yes">yes</label>
<input type="radio" name="status-duplicate" id="status-duplicate-no" value="duplicate:no" data-autofill data-checks="#type-q" /><label for="status-duplicate-no">no</label>
</div>
<div>
<label class="radio-group-label">accepted:</label>
<input type="radio" name="status-hasaccepted" id="status-hasaccepted-any" value="" checked /><label for="status-hasaccepted-any">any</label>
<input type="radio" name="status-hasaccepted" id="status-hasaccepted-yes" value="hasaccepted:yes" data-autofill data-checks="#type-q" /><label for="status-hasaccepted-yes">yes</label>
<input type="radio" name="status-hasaccepted" id="status-hasaccepted-no" value="hasaccepted:no" data-autofill data-checks="#type-q" /><label for="status-hasaccepted-no">no</label>
</div>
<div>
<label class="radio-group-label">answered:</label>
<input type="radio" name="status-isanswered" id="status-isanswered-any" value="" checked /><label for="status-isanswered-any">any</label>
<input type="radio" name="status-isanswered" id="status-isanswered-yes" value="isanswered:yes" data-autofill data-checks="#type-q" /><label for="status-isanswered-yes">yes</label>
<input type="radio" name="status-isanswered" id="status-isanswered-no" value="isanswered:no" data-autofill data-checks="#type-q" /><label for="status-isanswered-no">no</label>
</div>
<div>
<label class="radio-group-label">migrated:</label>
<input type="radio" name="status-migrated" id="status-migrated-any" value="" checked /><label for="status-migrated-any">any</label>
<input type="radio" name="status-migrated" id="status-migrated-yes" value="migrated:yes" data-autofill /><label for="status-migrated-yes">yes</label>
<input type="radio" name="status-migrated" id="status-migrated-no" value="migrated:no" /><label for="status-migrated-no">no</label>
</div>
<label class="section-label"># Views</label>
<div class="fromto">
<label for="views-from">from:</label>
<input type="number" name="views-from" id="views-from" placeholder="any" data-range-to="views-to" data-prefix="views:" data-checks="#type-q" />
<label for="views-to">to:</label>
<input type="number" name="views-to" id="views-to" placeholder="any" data-checks="#type-q" />
</div>
<label class="section-label"># Answers</label>
<div class="fromto">
<label for="answers-from">from:</label>
<input type="number" name="answers-from" id="answers-from" placeholder="any" data-range-to="answers-to" data-prefix="answers:" data-checks="#type-q" />
<label for="answers-to">to:</label>
<input type="number" name="answers-to" id="answers-to" placeholder="any" data-checks="#type-q" />
</div>
</div>
<div id="tab-answers" class="fixed-width-radios">
<label class="section-label">Answers</label>
<div>
<label class="radio-group-label">is accepted:</label>
<input type="radio" name="status-isaccepted" id="status-isaccepted-any" value="" checked /><label for="status-isaccepted-any">any</label>
<input type="radio" name="status-isaccepted" id="status-isaccepted-yes" value="isaccepted:yes" data-autofill data-checks="#type-a" /><label for="status-isaccepted-yes">yes</label>
<input type="radio" name="status-isaccepted" id="status-isaccepted-no" value="isaccepted:no" data-autofill data-checks="#type-a" /><label for="status-isaccepted-no">no</label>
</div>
<label class="section-label">In a Specific Question</label>
<input type="checkbox" name="question-current" id="question-current" data-currentfor="#question-id" data-checks="#type-a" /><label for="question-current">current question</label>
<label for="question-id">question id:</label>
<input name="question-id" id="question-id" class="input-small" maxlength="12" data-clearbtn data-validate-numeric data-checks="#type-a" data-clears="#question-current" data-autofill data-prefix="inquestion:" />
</div>
<div class="fixed-width-radios">
<label class="section-label">Post Status</label>
<p>See Questions/Answers tab for type-specific status (closed/duplicate/etc.)</p>
<div>
<label class="radio-group-label">deleted:</label>
<input type="radio" name="status-deleted" id="status-deleted-any" value="deleted:any" data-autofill /><label for="status-deleted-any">any</label>
<input type="radio" name="status-deleted" id="status-deleted-yes" value="deleted:yes" data-autofill /><label for="status-deleted-yes">yes</label>
<input type="radio" name="status-deleted" id="status-deleted-no" value="" checked /><label for="status-deleted-no">no</label>
</div>
<div>
<label class="radio-group-label">wiki:</label>
<input type="radio" name="status-wiki" id="status-wiki-any" value="" checked /><label for="status-wiki-any">any</label>
<input type="radio" name="status-wiki" id="status-wiki-yes" value="wiki:yes" data-autofill /><label for="status-wiki-yes">yes</label>
<input type="radio" name="status-wiki" id="status-wiki-no" value="wiki:no" data-autofill /><label for="status-wiki-no">no</label>
</div>
<div>
<label class="radio-group-label">locked:</label>
<input type="radio" name="status-locked" id="status-locked-any" value="" checked /><label for="status-locked-any">any</label>
<input type="radio" name="status-locked" id="status-locked-yes" value="locked:yes" data-autofill /><label for="status-locked-yes">yes</label>
<input type="radio" name="status-locked" id="status-locked-no" value="locked:no" data-autofill /><label for="status-locked-no">no</label>
</div>
<div>
<label class="radio-group-label">notice:</label>
<input type="radio" name="status-hasnotice" id="status-hasnotice-any" value="" checked /><label for="status-hasnotice-any">any</label>
<input type="radio" name="status-hasnotice" id="status-hasnotice-yes" value="hasnotice:yes" data-autofill /><label for="status-hasnotice-yes">yes</label>
<input type="radio" name="status-hasnotice" id="status-hasnotice-no" value="hasnotice:no" data-autofill /><label for="status-hasnotice-no">no</label>
</div>
<div>
<label class="radio-group-label">code block:</label>
<input type="radio" name="status-hascode" id="status-hascode-any" value="" checked /><label for="status-hascode-any">any</label>
<input type="radio" name="status-hascode" id="status-hascode-yes" value="hascode:yes" data-autofill /><label for="status-hascode-yes">yes</label>
<input type="radio" name="status-hascode" id="status-hascode-no" value="hascode:no" data-autofill /><label for="status-hascode-no">no</label>
</div>
</div>
<div>
<label class="section-label">Post Author</label>
<div>
<label for="user-self">my own posts:</label>
<input type="checkbox" name="user-self" id="user-self" value="user:me" data-clears="#user-id" data-autofill /><label for="user-self">self</label>
</div>
<label for="user-id">posts by user: (autocomplete)</label>
<input name="user-id" id="user-id" class="input-small js-dnlookup" placeholder="username or id" data-clearbtn data-clears="#user-self" data-autofill data-prefix="user:" />
</div>
<div>
<label class="section-label">Saves</label>
<em>(also known as bookmarks/starred questions)</em>
<div>
<input type="checkbox" name="fav-self" id="fav-self" value="in:saves" data-clears="#fav-id" data-autofill /><label for="fav-self">in saved posts</label>
</div>
</div>
<div>
<label class="section-label">Post Date</label>
<div>
<label for="datetype">date type:</label>
<input type="radio" name="datetype" id="datetype-created" value="created:" checked /><label for="datetype-created">created</label>
<input type="radio" name="datetype" id="datetype-lastactive" value="lastactive:" /><label type="radio" for="datetype-lastactive">last active</label>
</div>
<label class="section-label">Age</label>
<label for="age-quickselect">quick select:</label>
<select name="age-quickselect" id="age-quickselect" data-autofill data-termvalue="datetype"
data-clears="#yearrange-from, #monthrange-from, #yearrange-to, #monthrange-to, #agerange-from, #agerange-to">
<option></option>
<option value="${today.getUTCFullYear()}-${today.getUTCMonth() + 1}-${today.getUTCDate()}">today</option>
<option value="1d">yesterday</option>
<option value="7d..">past 7 days</option>
<option value="14d..">past 14 days</option>
<option value="1m..">this month</option>
<option value="1m">last month</option>
<option value="3m..">this quarter</option>
<option value="${today.getUTCFullYear()}">this year</option>
</select>
<label class="section-label">Age Range</label>
<label for="agerange-from">from:</label>
<input type="number" name="agerange-from" id="agerange-from" min="1" placeholder="any" data-termvalue="datetype" data-range-to="agerange-to" data-suffix-from="agerange-from-type"
data-clears="#yearrange-from, #monthrange-from, #yearrange-to, #monthrange-to, #age-quickselect" />
<select name="agerange-from-type" id="agerange-from-type">
<option value="d" selected>days</option>
<option value="m">months</option>
<option value="y">years</option>
</select> ago
<label for="agerange-to">to:</label>
<input type="number" name="agerange-to" id="agerange-to" min="1" placeholder="any" data-suffix-from="agerange-to-type"
data-clears="#yearrange-from, #monthrange-from, #yearrange-to, #monthrange-to, #age-quickselect" />
<select name="agerange-to-type" id="agerange-to-type">
<option value="d" selected>days</option>
<option value="m">months</option>
<option value="y">years</option>
</select> ago
<label class="section-label">Year Range</label>
<div class="fromto">
<label for="yearrange-from">from:</label>
<select name="yearrange-from" id="yearrange-from" class="js-yearpicker" data-termvalue="datetype" data-range-to="yearrange-to" data-additional="monthrange-from" data-additional-sep="-"
data-clears="#agerange-from, #agerange-to, #age-quickselect">
<option value="" selected>any</option>
</select>
<select name="monthrange-from" id="monthrange-from" data-clears="#agerange-from, #agerange-to, #age-quickselect">
<option value="" selected>any</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<label for="yearrange-to">to:</label>
<select name="yearrange-to" id="yearrange-to" class="js-yearpicker" data-additional="monthrange-to"
data-clears="#agerange-from, #agerange-to, #age-quickselect">
<option value="" selected>any</option>
</select>
<select name="monthrange-to" id="monthrange-to" data-clears="#agerange-from, #agerange-to, #age-quickselect">
<option value="" selected>any</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
</div>
</div>
<div id="tab-other">
<label class="section-label">Questions closed as duplicate of</label>
<div class="ext">
<input type="checkbox" name="dupe-current" id="dupe-current" data-currentfor="#dupe-id" /><label for="dupe-current">current question</label>
<label for="dupe-id">question id:</label>
<input name="dupe-id" id="dupe-id" class="input-small" maxlength="12" data-clearbtn data-validate-numeric data-clears="#dupe-current" />
<a class="s-btn s-btn__sm extbutton" data-exturl="https://data.stackexchange.com/${siteApiSlug}/query/874526/?QuestionId={dupe-id}">SEDE</a>
</div>
<label class="section-label">Search comments</label>
<div class="ext">
<label for="comment-query">Text:</label>
<input name="comment-query" id="comment-query" data-clearbtn />
<a class="s-btn s-btn__sm extbutton" data-exturl="https://data.stackexchange.com/${siteApiSlug}/query/898774/?Query={comment-query}">SEDE</a>
</div>
<div class="ext">
<label for="comment-query2">Comments replying to username:</label>
<input name="comment-query2" id="comment-query2" data-clearbtn placeholder="user display name without spaces (case-insensitive)" />
<a class="s-btn s-btn__sm extbutton" data-exturl="https://data.stackexchange.com/${siteApiSlug}/query/1160376/?UsernameWithoutSpaces={comment-query2}">SEDE</a>
</div>
<div class="ext">
<label for="comment-query3">Comments by user: (autocomplete)</label>
<input name="comment-query3" id="comment-query3" class="input-small js-dnlookup" data-clearbtn placeholder="username or id" />
<a class="s-btn s-btn__sm extbutton" data-exturl="https://data.stackexchange.com/${siteApiSlug}/query/1160377/?UserId={comment-query3}">SEDE</a>
</div>
<label class="section-label">Archive for</label>
<div class="ext">
<label for="archive-org">URL:</label>
<input name="archive-org" id="archive-org" data-clearbtn />
<a class="s-btn s-btn__sm extbutton" data-exturl="https://web.archive.org/web/*/{archive-org}">Search archive.org</a>
</div>
</div>
</div>
</div>`).appendTo(searchform);
orderby.insertBefore('#search-helper-tabs');
// Get last search sort
let lastSort = getLastSort();
if (lastSort) orderby.find('#tab-' + lastSort).trigger('click');
// Opened/closed state
let keepOpen = () => { searchhelper.addClass('open'); stopAutoRefresh(); }
let clearOpen = () => searchhelper.removeClass('open');
$(document).on('click', function (evt) {
// Any click on page except header
if ($(evt.target).closest('.js-top-bar').length === 0) {
clearOpen();
}
}).on('keydown', function (evt) {
// On 'esc' keypress
// https://stackoverflow.com/a/3369743/584192
evt = evt || window.event;
var isEscape = false;
if ("key" in evt) {
isEscape = (evt.key == "Escape" || evt.key == "Esc");
} else {
isEscape = (evt.keyCode == 27);
}
if (isEscape) {
clearOpen();
}
});
searchhelper
.on('mouseenter click mouseup', keepOpen)
.on('focus change click mouseup', 'input, select', keepOpen);
searchform
.on('focus change click mouseup', '.js-search-field', keepOpen);
// Save default checked state on radio buttons for reset purposes
$('input:radio:checked').attr('data-default', '');
// Tabs
$('#search-helper-tabs', searchhelper).on('click', 'a', function (e) {
$(this).addClass('youarehere').siblings().removeClass('youarehere');
$('#search-helper-tabcontent > div').removeClass('active').eq($(this).index()).addClass('active');
return false;
});
// Pre-populate year pickers with years up to current
const currYear = new Date().getFullYear();
$('.js-yearpicker', searchhelper).each(function () {
for (let i = currYear; i >= 2008; i--) {
$(this).append(`<option value="${i}">${i}</option>`);
}
});
// Current question
const currentQid = $('#question').attr('data-questionid') || null;
searchhelper
.on('click', '[data-currentfor]', function (evt) {
$(evt.target.dataset.currentfor).val(currentQid).triggerHandler('change');
})
.find('[data-currentfor]').each(function () {
if (!currentQid) {
$(this).next('label').addBack().remove();
}
});
// Restrict typed value to numerical value
searchhelper
.on('keydown', '[data-validate-numeric]', function (evt) {
const isSpecialKey = evt.altKey || evt.ctrlKey || evt.metaKey || evt.shiftKey || evt.key.length > 1;
return /\d/.test(evt.key) || isSpecialKey;
})
.on('change blur', '[data-validate-numeric]', function (evt) {
this.value = this.value.replace(/[^\d]+/g, '');
});
// Restrict typed value to URL
searchhelper
.on('change blur', '[data-validate-url]', function (evt) {
this.value = this.value.replace(/^https?:\/\//, '').replace(/\/$/, '');
});
// Handle display name lookup using API
searchhelper.find('.js-dnlookup').dnLookup();
// Handle tag name lookup using API
searchhelper.find('.js-taglookup').tagLookup(true);
// Handle radio/checkbox fields that populates another field
searchhelper.on('click', '[data-autofills-for]', function () {
if (this.checked) $(this.dataset.autofillsFor).val(this.value).trigger('change');
});
// Handle fields that checks another radio/checkbox
searchhelper.on('change keyup click', '[data-checks]', function () {
$(this.dataset.checks).prop('checked', true).trigger('change');
});
// Handle fields that resets another
searchhelper.on('change keyup click clear', '[data-clears]', function (evt) {
if (this.value.trim() != '' || this.type == 'radio' || this.type == 'checkbox' || evt.type == 'clear') {
$(this.dataset.clears).val('').prop('checked', false);
}
})
// Handle fields that resets fields in another tab
searchhelper.on('change keyup click', '[data-clears-tab]', function () {
$(this.dataset.clearsTab)
.find('input').val('').prop('checked', false)
.filter('[data-default]').prop('checked', true);
});
// Focus submit button when a radio/checkbox is clicked
searchhelper.on('click', 'input:radio, input:checkbox', function () {
searchbtn.focus();
});
// Insert clear buttons
searchhelper