1
+ import $ from 'jquery' ;
2
+
3
+ class Search {
4
+ // 1. describe and create/initiate our object
5
+ constructor ( ) {
6
+ this . addSearchHTML ( ) ;
7
+ this . resultsDiv = $ ( "#search-overlay__results" ) ;
8
+ this . openButton = $ ( ".js-search-trigger" ) ;
9
+ this . closeButton = $ ( ".search-overlay__close" ) ;
10
+ this . searchOverlay = $ ( ".search-overlay" ) ;
11
+ this . searchField = $ ( "#search-term" ) ;
12
+ this . events ( ) ;
13
+ this . isOverlayOpen = false ;
14
+ this . isSpinnerVisible = false ;
15
+ this . previousValue ;
16
+ this . typingTimer ;
17
+ }
18
+
19
+ // 2. events
20
+ events ( ) {
21
+ this . openButton . on ( "click" , this . openOverlay . bind ( this ) ) ;
22
+ this . closeButton . on ( "click" , this . closeOverlay . bind ( this ) ) ;
23
+ $ ( document ) . on ( "keydown" , this . keyPressDispatcher . bind ( this ) ) ;
24
+ this . searchField . on ( "keyup" , this . typingLogic . bind ( this ) ) ;
25
+ }
26
+
27
+
28
+ // 3. methods (function, action...)
29
+ typingLogic ( ) {
30
+ if ( this . searchField . val ( ) != this . previousValue ) {
31
+ clearTimeout ( this . typingTimer ) ;
32
+
33
+ if ( this . searchField . val ( ) ) {
34
+ if ( ! this . isSpinnerVisible ) {
35
+ this . resultsDiv . html ( '<div class="spinner-loader"></div>' ) ;
36
+ this . isSpinnerVisible = true ;
37
+ }
38
+ this . typingTimer = setTimeout ( this . getResults . bind ( this ) , 750 ) ;
39
+ } else {
40
+ this . resultsDiv . html ( '' ) ;
41
+ this . isSpinnerVisible = false ;
42
+ }
43
+
44
+ }
45
+
46
+ this . previousValue = this . searchField . val ( ) ;
47
+ }
48
+
49
+ getResults ( ) {
50
+ $ . when (
51
+ $ . getJSON ( universityData . root_url + '/wp-json/wp/v2/posts?search=' + this . searchField . val ( ) ) ,
52
+ $ . getJSON ( universityData . root_url + '/wp-json/wp/v2/pages?search=' + this . searchField . val ( ) )
53
+ ) . then ( ( posts , pages ) => {
54
+ var combinedResults = posts [ 0 ] . concat ( pages [ 0 ] ) ;
55
+ this . resultsDiv . html ( `
56
+ <h2 class="search-overlay__section-title">General Information</h2>
57
+ ${ combinedResults . length ? '<ul class="link-list min-list">' : '<p>No general information matches that search.</p>' }
58
+ ${ combinedResults . map ( item => `<li><a href="${ item . link } ">${ item . title . rendered } </a></li>` ) . join ( '' ) }
59
+ ${ combinedResults . length ? '</ul>' : '' }
60
+ ` ) ;
61
+ this . isSpinnerVisible = false ;
62
+ } , ( ) => {
63
+ this . resultsDiv . html ( '<p>Unexpected error; please try again.</p>' ) ;
64
+ } ) ;
65
+ }
66
+
67
+ keyPressDispatcher ( e ) {
68
+ if ( e . keyCode == 83 && ! this . isOverlayOpen && ! $ ( "input, textarea" ) . is ( ':focus' ) ) {
69
+ this . openOverlay ( ) ;
70
+ }
71
+
72
+ if ( e . keyCode == 27 && this . isOverlayOpen ) {
73
+ this . closeOverlay ( ) ;
74
+ }
75
+
76
+ }
77
+
78
+ openOverlay ( ) {
79
+ this . searchOverlay . addClass ( "search-overlay--active" ) ;
80
+ $ ( "body" ) . addClass ( "body-no-scroll" ) ;
81
+ this . searchField . val ( '' ) ;
82
+ setTimeout ( ( ) => this . searchField . focus ( ) , 301 ) ;
83
+ console . log ( "our open method just ran!" ) ;
84
+ this . isOverlayOpen = true ;
85
+ }
86
+
87
+ closeOverlay ( ) {
88
+ this . searchOverlay . removeClass ( "search-overlay--active" ) ;
89
+ $ ( "body" ) . removeClass ( "body-no-scroll" ) ;
90
+ console . log ( "our close method just ran!" ) ;
91
+ this . isOverlayOpen = false ;
92
+ }
93
+
94
+ addSearchHTML ( ) {
95
+ $ ( "body" ) . append ( `
96
+ <div class="search-overlay">
97
+ <div class="search-overlay__top">
98
+ <div class="container">
99
+ <i class="fa fa-search search-overlay__icon" aria-hidden="true"></i>
100
+ <input type="text" class="search-term" placeholder="What are you looking for?" id="search-term">
101
+ <i class="fa fa-window-close search-overlay__close" aria-hidden="true"></i>
102
+ </div>
103
+ </div>
104
+
105
+ <div class="container">
106
+ <div id="search-overlay__results"></div>
107
+ </div>
108
+
109
+ </div>
110
+ ` ) ;
111
+ }
112
+
113
+ }
114
+
115
+ export default Search ;
0 commit comments