diff --git a/assets/js/dashboard/constants.js b/assets/js/dashboard/constants.js new file mode 100644 index 000000000..07e3d10d5 --- /dev/null +++ b/assets/js/dashboard/constants.js @@ -0,0 +1,253 @@ +export const country_codes = { + "AFG": "Afghanistan", + "ALA": "Aland Islands", + "ALB": "Albania", + "DZA": "Algeria", + "ASM": "American Samoa", + "AND": "Andorra", + "AGO": "Angola", + "AIA": "Anguilla", + "ATA": "Antarctica", + "ATG": "Antigua And Barbuda", + "ARG": "Argentina", + "ARM": "Armenia", + "ABW": "Aruba", + "AUS": "Australia", + "AUT": "Austria", + "AZE": "Azerbaijan", + "BHS": "Bahamas", + "BHR": "Bahrain", + "BGD": "Bangladesh", + "BRB": "Barbados", + "BLR": "Belarus", + "BEL": "Belgium", + "BLZ": "Belize", + "BEN": "Benin", + "BMU": "Bermuda", + "BTN": "Bhutan", + "BOL": "Bolivia", + "BIH": "Bosnia And Herzegovina", + "BWA": "Botswana", + "BVT": "Bouvet Island", + "BRA": "Brazil", + "IOT": "British Indian Ocean Territory", + "BRN": "Brunei Darussalam", + "BGR": "Bulgaria", + "BFA": "Burkina Faso", + "BDI": "Burundi", + "KHM": "Cambodia", + "CMR": "Cameroon", + "CAN": "Canada", + "CPV": "Cape Verde", + "CYM": "Cayman Islands", + "CAF": "Central African Republic", + "TCD": "Chad", + "CHL": "Chile", + "CHN": "China", + "CXR": "Christmas Island", + "CCK": "Cocos (Keeling) Islands", + "COL": "Colombia", + "COM": "Comoros", + "COG": "Congo", + "COD": "Congo, Democratic Republic", + "COK": "Cook Islands", + "CRI": "Costa Rica", + "CIV": "Cote D'Ivoire", + "HRV": "Croatia", + "CUB": "Cuba", + "CYP": "Cyprus", + "CZE": "Czech Republic", + "DNK": "Denmark", + "DJI": "Djibouti", + "DMA": "Dominica", + "DOM": "Dominican Republic", + "ECU": "Ecuador", + "EGY": "Egypt", + "SLV": "El Salvador", + "GNQ": "Equatorial Guinea", + "ERI": "Eritrea", + "EST": "Estonia", + "ETH": "Ethiopia", + "FLK": "Falkland Islands (Malvinas)", + "FRO": "Faroe Islands", + "FJI": "Fiji", + "FIN": "Finland", + "FRA": "France", + "GUF": "French Guiana", + "PYF": "French Polynesia", + "ATF": "French Southern Territories", + "GAB": "Gabon", + "GMB": "Gambia", + "GEO": "Georgia", + "DEU": "Germany", + "GHA": "Ghana", + "GIB": "Gibraltar", + "GRC": "Greece", + "GRL": "Greenland", + "GRD": "Grenada", + "GLP": "Guadeloupe", + "GUM": "Guam", + "GTM": "Guatemala", + "GGY": "Guernsey", + "GIN": "Guinea", + "GNB": "Guinea-Bissau", + "GUY": "Guyana", + "HTI": "Haiti", + "HMD": "Heard Island & Mcdonald Islands", + "VAT": "Holy See (Vatican City State)", + "HND": "Honduras", + "HKG": "Hong Kong", + "HUN": "Hungary", + "ISL": "Iceland", + "IND": "India", + "IDN": "Indonesia", + "IRN": "Iran, Islamic Republic Of", + "IRQ": "Iraq", + "IRL": "Ireland", + "IMN": "Isle Of Man", + "ISR": "Israel", + "ITA": "Italy", + "JAM": "Jamaica", + "JPN": "Japan", + "JEY": "Jersey", + "JOR": "Jordan", + "KAZ": "Kazakhstan", + "KEN": "Kenya", + "KIR": "Kiribati", + "KOR": "Korea", + "KWT": "Kuwait", + "KGZ": "Kyrgyzstan", + "LAO": "Lao People's Democratic Republic", + "LVA": "Latvia", + "LBN": "Lebanon", + "LSO": "Lesotho", + "LBR": "Liberia", + "LBY": "Libyan Arab Jamahiriya", + "LIE": "Liechtenstein", + "LTU": "Lithuania", + "LUX": "Luxembourg", + "MAC": "Macao", + "MKD": "Macedonia", + "MDG": "Madagascar", + "MWI": "Malawi", + "MYS": "Malaysia", + "MDV": "Maldives", + "MLI": "Mali", + "MLT": "Malta", + "MHL": "Marshall Islands", + "MTQ": "Martinique", + "MRT": "Mauritania", + "MUS": "Mauritius", + "MYT": "Mayotte", + "MEX": "Mexico", + "FSM": "Micronesia, Federated States Of", + "MDA": "Moldova", + "MCO": "Monaco", + "MNG": "Mongolia", + "MNE": "Montenegro", + "MSR": "Montserrat", + "MAR": "Morocco", + "MOZ": "Mozambique", + "MMR": "Myanmar", + "NAM": "Namibia", + "NRU": "Nauru", + "NPL": "Nepal", + "NLD": "Netherlands", + "NCL": "New Caledonia", + "NZL": "New Zealand", + "NIC": "Nicaragua", + "NER": "Niger", + "NGA": "Nigeria", + "NIU": "Niue", + "NFK": "Norfolk Island", + "MNP": "Northern Mariana Islands", + "NOR": "Norway", + "OMN": "Oman", + "PAK": "Pakistan", + "PLW": "Palau", + "PSE": "Palestinian Territory, Occupied", + "PAN": "Panama", + "PNG": "Papua New Guinea", + "PRY": "Paraguay", + "PER": "Peru", + "PHL": "Philippines", + "PCN": "Pitcairn", + "POL": "Poland", + "PRT": "Portugal", + "PRI": "Puerto Rico", + "QAT": "Qatar", + "REU": "Reunion", + "ROU": "Romania", + "RUS": "Russian Federation", + "RWA": "Rwanda", + "BLM": "Saint Barthelemy", + "SHN": "Saint Helena", + "KNA": "Saint Kitts And Nevis", + "LCA": "Saint Lucia", + "MAF": "Saint Martin", + "SPM": "Saint Pierre And Miquelon", + "VCT": "Saint Vincent And Grenadines", + "WSM": "Samoa", + "SMR": "San Marino", + "STP": "Sao Tome And Principe", + "SAU": "Saudi Arabia", + "SEN": "Senegal", + "SRB": "Serbia", + "SYC": "Seychelles", + "SLE": "Sierra Leone", + "SGP": "Singapore", + "SVK": "Slovakia", + "SVN": "Slovenia", + "SLB": "Solomon Islands", + "SOM": "Somalia", + "ZAF": "South Africa", + "SGS": "South Georgia And Sandwich Isl.", + "ESP": "Spain", + "LKA": "Sri Lanka", + "SDN": "Sudan", + "SUR": "Suriname", + "SJM": "Svalbard And Jan Mayen", + "SWZ": "Swaziland", + "SWE": "Sweden", + "CHE": "Switzerland", + "SYR": "Syrian Arab Republic", + "TWN": "Taiwan", + "TJK": "Tajikistan", + "TZA": "Tanzania", + "THA": "Thailand", + "TLS": "Timor-Leste", + "TGO": "Togo", + "TKL": "Tokelau", + "TON": "Tonga", + "TTO": "Trinidad And Tobago", + "TUN": "Tunisia", + "TUR": "Turkey", + "TKM": "Turkmenistan", + "TCA": "Turks And Caicos Islands", + "TUV": "Tuvalu", + "UGA": "Uganda", + "UKR": "Ukraine", + "ARE": "United Arab Emirates", + "GBR": "United Kingdom", + "USA": "United States", + "UMI": "United States Outlying Islands", + "URY": "Uruguay", + "UZB": "Uzbekistan", + "VUT": "Vanuatu", + "VEN": "Venezuela", + "VNM": "Viet Nam", + "VGB": "Virgin Islands, British", + "VIR": "Virgin Islands, U.S.", + "WLF": "Wallis And Futuna", + "ESH": "Western Sahara", + "YEM": "Yemen", + "ZMB": "Zambia", + "ZWE": "Zimbabwe" +} + +export const screen_sizes = [ + 'Desktop', + 'Mobile', + 'Laptop', + 'Tablet' +] diff --git a/assets/js/dashboard/filters.js b/assets/js/dashboard/filters.js index 1fd179918..bf66a5a26 100644 --- a/assets/js/dashboard/filters.js +++ b/assets/js/dashboard/filters.js @@ -216,7 +216,7 @@ class Filters extends React.Component { > )} this.removeFilter(key, history, query)}> - + ) diff --git a/assets/js/dashboard/stats/modals/filter.js b/assets/js/dashboard/stats/modals/filter.js index 84e513e24..dd434113f 100644 --- a/assets/js/dashboard/stats/modals/filter.js +++ b/assets/js/dashboard/stats/modals/filter.js @@ -3,6 +3,9 @@ import { withRouter, Redirect } from 'react-router-dom' import Modal from './modal' import { parseQuery, formattedFilters, navigateToQuery } from '../../query' +import { country_codes, screen_sizes } from "../../constants"; +import Transition from "../../../transition"; +import * as api from '../../api' class FilterModal extends React.Component { constructor(props) { @@ -12,31 +15,184 @@ class FilterModal extends React.Component { selectedFilter: "", negated: false, updatedValue: "", - filterSaved: false + filterSaved: false, + suggestions: [], + fetchSuggestionsTimeout: undefined, + initialSuggest: true, + focusedSuggestionIndex: -1 } this.editableGoals = Object.keys(this.state.query.filters).filter(filter => !['goal', 'props'].includes(filter)) + this.renderSelector = this.renderSelector.bind(this) + this.fetchSuggestions = this.fetchSuggestions.bind(this) + this.handleKeydown = this.handleKeydown.bind(this) } componentDidMount() { this.setState({ selectedFilter: this.props.match.params.field }) + + document.addEventListener("keydown", this.handleKeydown); } componentDidUpdate(prevProps, prevState) { - const { query, selectedFilter } = this.state + const { query, selectedFilter, updatedValue, preventSuggestions } = this.state if (prevState.selectedFilter !== selectedFilter) { const negated = query.filters[selectedFilter] && query.filters[selectedFilter][0] == '!' && this.negationSupported(selectedFilter) const updatedValue = negated ? query.filters[selectedFilter].slice(1) : (query.filters[selectedFilter] || "") - this.setState({ updatedValue, negated }) + clearTimeout(this.state.fetchSuggestionsTimeout) + + this.setState({ updatedValue, negated, suggestions: [], fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1 }) + } + + if (prevState.updatedValue !== updatedValue) { + if (!preventSuggestions || prevState.preventSuggestions) { + let fetchSuggestionsTimeout; + clearTimeout(this.state.fetchSuggestionsTimeout) + + if (!['country', 'screen'].includes(selectedFilter)) { + fetchSuggestionsTimeout = setTimeout(this.fetchSuggestions, 1000) + } + + this.setState({ suggestions: [], fetchSuggestionsTimeout, preventSuggestions: false, focusedSuggestionIndex: -1 }) + } else { + this.setState({ suggestions: [], fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1 }) + } + } + } + + componentWillUnmount() { + document.removeEventListener("keydown", this.handleKeydown); + } + + handleKeydown(e) { + const { suggestions } = this.state + let { focusedSuggestionIndex, updatedValue, preventSuggestions } = this.state + const navigationKeys = ['ArrowUp', undefined, 'ArrowDown'] + + if (e.target.id !== 'suggestions_input') return true + if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey || e.isComposing || e.keyCode === 229) return + + if ((e.key === 'Enter' || e.key === 'Tab') && suggestions[focusedSuggestionIndex]) { + updatedValue = suggestions[focusedSuggestionIndex] + focusedSuggestionIndex = -1 + preventSuggestions = true + e.preventDefault() + } + + if (navigationKeys.includes(e.key)) { + focusedSuggestionIndex += navigationKeys.indexOf(e.key)-1 + if (focusedSuggestionIndex < 0) { + focusedSuggestionIndex = suggestions.length - 1 + } else if (focusedSuggestionIndex >= suggestions.length) { + focusedSuggestionIndex = 0 + } + e.preventDefault() } + + this.setState({ focusedSuggestionIndex, updatedValue, preventSuggestions }) } negationSupported(filter) { return ['page', 'entry_page', 'exit_page'].includes(filter) } + fetchSuggestions() { + const { query, updatedValue, selectedFilter } = this.state + + query.filters[selectedFilter] = null + + api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/suggestions/${selectedFilter}`, query, { q: updatedValue }) + .then((res) => this.setState({ suggestions: res, fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1 })) + } + + renderSelector(filter) { + const { updatedValue, suggestions, initialSuggest, inputFocused, focusedSuggestionIndex } = this.state; + + if (!filter) { return null; } + + if (filter === 'country') { + return () + } + + if (filter === 'screen') { + return ( + <> + + > + ) + } + + return