@@ -12,10 +12,11 @@ class RecipesApp {
12
12
this . recipesPage = new RecipesPage ( ) ;
13
13
this . tags = new Array ( ) ;
14
14
15
+ // DOM
16
+ this . $userInput = document . querySelector ( '#recipes_search' ) ;
15
17
this . $ingredientsList = document . querySelector ( '#ingredients_list' ) ;
16
18
this . $appliancesList = document . querySelector ( '#appliances_list' ) ;
17
19
this . $ustensilsList = document . querySelector ( '#ustensils_list' ) ;
18
-
19
20
this . $ingredientsSelect = document . querySelector ( 'select#ingredients' ) ;
20
21
this . $appliancesSelect = document . querySelector ( '#appliances' ) ;
21
22
this . $ustensilsSelect = document . querySelector ( '#ustensils' ) ;
@@ -27,10 +28,6 @@ class RecipesApp {
27
28
this . recipesData = recipesData
28
29
. map ( recipe => new RecipeFactories ( recipe , 'recipe' ) ) ;
29
30
30
- // Search form data
31
- new MainSearchBar ;
32
- this . recipesDataForMainSearchBar ( ) ;
33
-
34
31
// Select boxes
35
32
this . displayIngredientsDropdownWithData ( ) ;
36
33
this . displayAppliancesDropdownWithData ( ) ;
@@ -39,12 +36,85 @@ class RecipesApp {
39
36
// Cards
40
37
this . displayRecipeCardsWithData ( ) ;
41
38
39
+ // Main search bar
40
+ this . isUserInputValueMatches ( ) ;
41
+
42
42
// Tags
43
43
this . tags . push ( 'item 1' , 'item 2' , 'item 3' ) ;
44
44
const tags = this . tags ;
45
45
this . displayActiveTags ( tags ) ;
46
46
}
47
47
48
+ isUserInputValueMatches ( ) {
49
+
50
+ this . $userInput . addEventListener ( 'input' , ( e ) => {
51
+
52
+ this . displayMatchingRecipe ( e ) ;
53
+
54
+ this . sortRecipeByDescendingMatching ( ) ;
55
+ } ) ;
56
+ }
57
+
58
+ sortRecipeByDescendingMatching ( ) {
59
+ const cardsContainer = document . querySelector ( '.recipe-cards' ) ;
60
+ const containerArray = Array . from ( cardsContainer . children ) ;
61
+
62
+ const sorted = containerArray
63
+ . filter ( child => {
64
+ if ( child . hasAttribute ( 'data-matches' ) ) {
65
+ const matches = child . dataset . matches > parseInt ( 0 ) ;
66
+ if ( matches ) {
67
+ return child ;
68
+ }
69
+ }
70
+ } )
71
+ . sort ( ( a , b ) => {
72
+ return b . dataset . matches - a . dataset . matches ;
73
+ } ) ;
74
+
75
+ sorted
76
+ . forEach ( el => cardsContainer . append ( el ) ) ;
77
+ }
78
+
79
+ /**
80
+ * Displays matching recipe
81
+ * By main search bar
82
+ * @param {Event & {target: HTMLInputElement} } e
83
+ */
84
+ displayMatchingRecipe ( e ) {
85
+ const cards = Array . from ( document . querySelectorAll ( '.recipe-cards article' ) ) ;
86
+
87
+ cards
88
+ . forEach ( card => {
89
+ const recipesDataForMainSearchBar = this . recipesDataForMainSearchBar ( ) . sort ( ) ;
90
+ const binarySearch = new BinarySearch ( ) ;
91
+ const userInputValue = e . target . value ;
92
+ const cardTextContent = card . textContent . toLowerCase ( ) ;
93
+
94
+ const userInputMatchingList = binarySearch . isUserValueMatches ( userInputValue , recipesDataForMainSearchBar ) ;
95
+
96
+ const isUserInputMatching = userInputMatchingList
97
+ . some ( element => card . textContent . includes ( element ) ) ;
98
+
99
+ if ( isUserInputMatching ) {
100
+ card . style . display = 'block' ;
101
+
102
+ let position = cardTextContent . indexOf ( userInputValue ) ;
103
+ let count = 0 ;
104
+
105
+ while ( position !== - 1 ) {
106
+ count ++ ;
107
+ position = cardTextContent . indexOf ( userInputValue , position + 1 ) ;
108
+ }
109
+ // Number of occurrences of user input value matching
110
+ card . dataset . matches = count ;
111
+ } else {
112
+ card . dataset . matches = 0 ;
113
+ card . style . display = 'none' ;
114
+ }
115
+ } ) ;
116
+ }
117
+
48
118
/**
49
119
* Returns an array of not duplicated data for search form
50
120
* Data about name, description and ingredients of recipes
0 commit comments