forked from Sefaria/Sefaria-Mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchFilterPage.js
202 lines (189 loc) · 7.5 KB
/
SearchFilterPage.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
'use strict';
import PropTypes from 'prop-types';
import React, { useContext } from 'react';
import {
View,
Text,
TouchableOpacity,
ScrollView,
Image,
} from 'react-native';
import { FilterNode, SearchPropTypes } from '@sefaria/search';
import {
DirectedButton,
ButtonToggleSet,
LibraryNavButton,
} from './Misc.js';
import { GlobalStateContext, getTheme } from './StateManager';
import styles from './Styles';
import strings from './LocalizedStrings';
const SearchFilterPage = ({
subMenuOpen,
toggleFilter,
clearAllFilters,
query,
openSubMenu,
search,
setSearchOptions,
searchState,
}) => {
const { interfaceLanguage, themeStr } = useContext(GlobalStateContext);
const theme = getTheme(themeStr);
const { type } = searchState;
const sortOptions = [
{name: "chronological", text: strings.chronological, onPress: () => { setSearchOptions(type, "chronological", searchState.field); }},
{name: "relevance", text: strings.relevance, onPress: () => { setSearchOptions(type, "relevance", searchState.field); }}
];
const exactOptions = [
{name: false, text: strings.off, onPress: () => {
setSearchOptions(type, searchState.sortType, searchState.fieldBroad, ()=>search(type, query, true, false, true));
}},
{name: true, text: strings.on, onPress: () => {
setSearchOptions(type, searchState.sortType, searchState.fieldExact, ()=>search(type, query, true, false, true));
}}
];
const backFromFilter = () => {
const backPage = subMenuOpen == "filter" ? null : "filter"; // if you're at a category filter page, go back to main filter page
openSubMenu(backPage, true);
};
const applyFilters = () => {
openSubMenu(null);
search(type, query, true, false);
};
const toggleFilterBound = filter => { toggleFilter(type, filter); };
var isheb = interfaceLanguage === "hebrew"; //TODO enable when we properly handle interface hebrew throughout app
var langStyle = !isheb ? styles.enInt : styles.heInt;
var backImageStyle = isheb && false ? styles.directedButtonWithTextHe : styles.directedButtonWithTextEn;
var loadingMessage = (<Text style={[langStyle, theme.searchResultSummaryText]}>{strings.loadingFilters}</Text>);
var content = null;
var closeSrc = themeStr == "white" ? require("./img/circle-close.png") : require("./img/circle-close-light.png");
var flexDir = { flexDirection: interfaceLanguage === "hebrew" ? "row-reverse" : "row" };
switch (subMenuOpen) {
case "filter":
content =
(<View>
<TouchableOpacity style={[styles.readerDisplayOptionsMenuItem, styles.button, theme.readerDisplayOptionsMenuItem]} onPress={() => { clearAllFilters(type); }}>
<Image source={closeSrc}
resizeMode={'contain'}
style={styles.searchFilterClearAll} />
<Text style={[isheb ? styles.heInt : styles.enInt, styles.heInt, theme.tertiaryText]}>{strings.clearAll}</Text>
</TouchableOpacity>
<View style={styles.settingsSection}>
<View>
<Text style={[isheb ? styles.heInt : styles.enInt, styles.settingsSectionHeader, theme.tertiaryText]}>{strings.sortBy}</Text>
</View>
<ButtonToggleSet
options={sortOptions}
lang={interfaceLanguage}
active={searchState.sortType} />
</View>
<View style={styles.settingsSection}>
<View>
<Text style={[isheb ? styles.heInt : styles.enInt, styles.settingsSectionHeader, theme.tertiaryText]}>{strings.exactSearch}</Text>
</View>
<ButtonToggleSet
options={exactOptions}
lang={interfaceLanguage}
active={searchState.field === searchState.fieldExact} />
</View>
<View style={styles.settingsSection}>
<View>
<Text style={[isheb ? styles.heInt : styles.enInt, styles.settingsSectionHeader, theme.tertiaryText]}>{strings.filterByText}</Text>
</View>
<View>
{ searchState.filtersValid ?
searchState.availableFilters.map((filter, ifilter)=>{
return (
<SearchFilter
key={ifilter}
filterNode={filter}
openSubMenu={openSubMenu}
toggleFilter={toggleFilterBound}
/>);
}) : loadingMessage
}
</View>
</View>
</View>);
break;
default:
var currFilter = FilterNode.findFilterInList(searchState.availableFilters, subMenuOpen);
var filterList =
[(<SearchFilter
key={0}
filterNode={currFilter}
toggleFilter={toggleFilterBound}
/>)];
content =
(<View>
{ searchState.filtersValid ?
filterList.concat(currFilter.getLeafNodes().map((filter, ifilter)=>{
return (
<SearchFilter
key={ifilter+1}
filterNode={filter}
toggleFilter={toggleFilterBound}
/>);
})) : loadingMessage
}
</View>);
}
return (<View style={{flex:1}}>
<View style={[styles.header, theme.header, {justifyContent: "space-between", paddingHorizontal: 12}]}>
<DirectedButton
onPress={backFromFilter}
text={strings.back}
direction="back"
language="english"
textStyle={[theme.searchResultSummaryText, langStyle]}
imageStyle={[styles.menuButton, backImageStyle]}/>
<TouchableOpacity onPress={applyFilters} style={{marginLeft: 7, marginRight: 7}}>
<Text style={[theme.searchResultSummaryText, langStyle, {marginTop: -1}]}>{strings.apply}</Text>
</TouchableOpacity>
</View>
<ScrollView key={subMenuOpen} contentContainerStyle={styles.menuContent} style={styles.scrollViewPaddingInOrderToScroll}>
{content}
</ScrollView>
</View>);
}
SearchFilterPage.propTypes = {
subMenuOpen: PropTypes.string.isRequired,
toggleFilter: PropTypes.func.isRequired,
clearAllFilters: PropTypes.func.isRequired,
query: PropTypes.string,
openSubMenu: PropTypes.func,
search: PropTypes.func,
setSearchOptions: PropTypes.func,
searchState: PropTypes.object,
};
const SearchFilter = ({ filterNode, openSubMenu, toggleFilter }) => {
const { textLanguage, interfaceLanguage, themeStr } = useContext(GlobalStateContext);
const theme = getTheme(themeStr);
const clickCheckBox = () => { toggleFilter(filterNode); }
const onPress = () => { openSubMenu ? openSubMenu(title) : clickCheckBox() }
const { title, heTitle, selected, children, docCount } = filterNode;
let isCat = children.length > 0;
let catColor = Sefaria.palette.categoryColor(title.replace(" Commentaries", ""));
let colorStyle = isCat ? [{"borderColor": catColor}] : [theme.searchResultSummary, {"borderTopWidth": 1}];
let textStyle = [isCat ? styles.spacedText : null];
let enTitle = isCat ? title.toUpperCase() : title;
let flexDir = Sefaria.util.get_menu_language(interfaceLanguage, textLanguage) == "hebrew" ? "row-reverse" : "row";
return (
<LibraryNavButton
onPress={onPress}
onPressCheckBox={clickCheckBox}
checkBoxSelected={selected}
enText={enTitle}
count={docCount}
heText={heTitle}
catColor={isCat ? catColor : null}
withArrow={!!openSubMenu}
buttonStyle={{ margin: 2, paddingVertical: 0, paddingHorizontal: 5,}} />
);
}
SearchFilter.propTypes = {
filterNode: SearchPropTypes.filterNode,
openSubMenu: PropTypes.func,
toggleFilter: PropTypes.func.isRequired,
};
export default SearchFilterPage;