overview
1 year ago
campaign_builder_controller.js
1 year ago
chart_controller.js
6 months ago
chart_interval_controller.js
2 years ago
clipboard_controller.js
1 year ago
copy_report_controller.js
2 years ago
create_report_controller.js
1 year ago
delete_data_controller.js
1 year ago
delete_report_controller.js
2 years ago
easepick_controller.js
2 years ago
examiner_controller.js
9 months ago
examiner_header_controller.js
11 months ago
export_overview_controller.js
9 months ago
export_reports_controller.js
2 years ago
filters_controller.js
6 months ago
group_controller.js
2 years ago
import_reports_controller.js
2 years ago
journey_controller.js
6 months ago
map_controller.js
9 months ago
migration_redirect_controller.js
2 years ago
modal_controller.js
2 years ago
pause_emails_controller.js
6 months ago
pie_chart_controller.js
6 months ago
plugin_group_options_controller.js
1 year ago
pruner_controller.js
5 months ago
quick_stats_controller.js
1 year ago
real_time_controller.js
6 months ago
refresh_overview_controller.js
1 year ago
rename_report_controller.js
2 years ago
report_controller.js
6 months ago
reset_analytics_controller.js
1 year ago
reset_overview_controller.js
1 year ago
save_report_controller.js
2 years ago
select_input_controller.js
2 years ago
set_favorite_report_controller.js
2 years ago
sort_controller.js
2 years ago
sortable_reports_controller.js
2 years ago
table_columns_controller.js
2 years ago
tooltip_controller.js
6 months ago
woocommerce_settings_controller.js
1 year ago
map_controller.js
391 lines
| 1 | import {Controller} from "@hotwired/stimulus" |
| 2 | import {isDarkMode} from "../utils/appearance"; |
| 3 | import svgMap from 'svgmap'; |
| 4 | |
| 5 | |
| 6 | export default class extends Controller { |
| 7 | static targets = ["chart"] |
| 8 | static values = { |
| 9 | data: Array, |
| 10 | flagsUrl: String, |
| 11 | locale: String |
| 12 | } |
| 13 | |
| 14 | map = null |
| 15 | resizeObserver = null |
| 16 | |
| 17 | connect() { |
| 18 | if(this.map) { |
| 19 | this.resizeObserver.observe(this.chartTarget); |
| 20 | return |
| 21 | } |
| 22 | |
| 23 | this.initializeMap() |
| 24 | |
| 25 | window.iawpMaps = window.iawpMaps || [] |
| 26 | window.iawpMaps.push(this.map) |
| 27 | |
| 28 | this.resizeObserver.observe(this.chartTarget); |
| 29 | } |
| 30 | |
| 31 | disconnect() { |
| 32 | this.resizeObserver.disconnect(); |
| 33 | window.iawpMaps = window.iawpMaps || [] |
| 34 | window.iawpMaps = window.iawpMaps.filter(map => { |
| 35 | return map !== this.map |
| 36 | }) |
| 37 | } |
| 38 | |
| 39 | initializeMap() { |
| 40 | const id = 'iawp-map-' + Math.random().toString() |
| 41 | this.chartTarget.setAttribute('id', id) |
| 42 | |
| 43 | const chartValues = {} |
| 44 | |
| 45 | this.dataValue.forEach((country) => { |
| 46 | chartValues[country.country_code] = { |
| 47 | visitors: country.visitors, |
| 48 | views: country.views, |
| 49 | sessions: country.sessions, |
| 50 | } |
| 51 | }) |
| 52 | |
| 53 | this.map = new svgMap({ |
| 54 | targetElementID: id, |
| 55 | allowInteraction: false, |
| 56 | initialZoom: 1.1, |
| 57 | colorMax: '#5123A0', |
| 58 | colorMin: '#bea4eb', |
| 59 | colorNoData: '#dedede', |
| 60 | ratioType: 'log', |
| 61 | data: { |
| 62 | data: { |
| 63 | visitors: { |
| 64 | name: 'Visitors', |
| 65 | }, |
| 66 | views: { |
| 67 | name: 'Views', |
| 68 | }, |
| 69 | sessions: { |
| 70 | name: 'Sessions', |
| 71 | } |
| 72 | |
| 73 | }, |
| 74 | applyData: 'visitors', |
| 75 | values: chartValues, |
| 76 | }, |
| 77 | onGetTooltip: (tooltipDiv, countryCode, countryValues) => { |
| 78 | const country = this.dataValue.find((country) => { |
| 79 | return country['country_code'] === countryCode |
| 80 | }) |
| 81 | |
| 82 | const flagUrl = this.flagsUrlValue + '/' + countryCode.toLowerCase() + '.svg' |
| 83 | |
| 84 | // Render an empty tooltip when there's no country data |
| 85 | if(!country) { |
| 86 | const templateString = ` |
| 87 | <div class="iawp-geo-chart-tooltip"> |
| 88 | <img src="${flagUrl}" alt="Country flag"/> |
| 89 | <h1>${this.countryName(countryCode)}</h1> |
| 90 | <p>No data available</p> |
| 91 | </div> |
| 92 | `; |
| 93 | |
| 94 | const templateDocument= new DOMParser().parseFromString(templateString, "text/html"); |
| 95 | |
| 96 | return templateDocument.body.firstElementChild |
| 97 | } |
| 98 | |
| 99 | const formatted_views = this.formatNumber(country['views']) |
| 100 | const formatted_visitors = this.formatNumber(country['visitors']) |
| 101 | const formatted_sessions = this.formatNumber(country['sessions']) |
| 102 | |
| 103 | const templateString = ` |
| 104 | <div class="iawp-geo-chart-tooltip"> |
| 105 | <img src="${flagUrl}" alt="Country flag"/> |
| 106 | <h1>${this.countryName(countryCode)}</h1> |
| 107 | <div class="iawp-geo-chart-tooltip-table"> |
| 108 | <span>${iawpText.views}</span><span>${formatted_views}</span> |
| 109 | <span>${iawpText.visitors}</span><span>${formatted_visitors}</span> |
| 110 | <span>${iawpText.sessions}</span><span>${formatted_sessions}</span> |
| 111 | </div> |
| 112 | </div> |
| 113 | `; |
| 114 | |
| 115 | const templateDocument= new DOMParser().parseFromString(templateString, "text/html"); |
| 116 | |
| 117 | return templateDocument.body.firstElementChild |
| 118 | } |
| 119 | }) |
| 120 | |
| 121 | this.resizeObserver = new ResizeObserver(() => { |
| 122 | if(!this.chartTarget.checkVisibility()) { |
| 123 | return |
| 124 | } |
| 125 | |
| 126 | this.map.mapPanZoom.resize(); |
| 127 | this.map.mapPanZoom.fit(); |
| 128 | this.map.mapPanZoom.center(); |
| 129 | this.map.mapPanZoom.zoom(1.1); |
| 130 | }); |
| 131 | } |
| 132 | |
| 133 | formatNumber(number) { |
| 134 | return new Intl.NumberFormat(this.localeValue, { |
| 135 | maximumFractionDigits: 0 |
| 136 | }).format(number); |
| 137 | } |
| 138 | |
| 139 | countryName(countryCode) { |
| 140 | const countries = { |
| 141 | AF: 'Afghanistan', |
| 142 | AX: '� |
| 143 | land Islands', |
| 144 | AL: 'Albania', |
| 145 | DZ: 'Algeria', |
| 146 | AS: 'American Samoa', |
| 147 | AD: 'Andorra', |
| 148 | AO: 'Angola', |
| 149 | AI: 'Anguilla', |
| 150 | AQ: 'Antarctica', |
| 151 | AG: 'Antigua and Barbuda', |
| 152 | AR: 'Argentina', |
| 153 | AM: 'Armenia', |
| 154 | AW: 'Aruba', |
| 155 | AU: 'Australia', |
| 156 | AT: 'Austria', |
| 157 | AZ: 'Azerbaijan', |
| 158 | BS: 'Bahamas', |
| 159 | BH: 'Bahrain', |
| 160 | BD: 'Bangladesh', |
| 161 | BB: 'Barbados', |
| 162 | BY: 'Belarus', |
| 163 | BE: 'Belgium', |
| 164 | BZ: 'Belize', |
| 165 | BJ: 'Benin', |
| 166 | BM: 'Bermuda', |
| 167 | BT: 'Bhutan', |
| 168 | BO: 'Bolivia', |
| 169 | BA: 'Bosnia and Herzegovina', |
| 170 | BW: 'Botswana', |
| 171 | BR: 'Brazil', |
| 172 | IO: 'British Indian Ocean Territory', |
| 173 | VG: 'British Virgin Islands', |
| 174 | BN: 'Brunei Darussalam', |
| 175 | BG: 'Bulgaria', |
| 176 | BF: 'Burkina Faso', |
| 177 | BI: 'Burundi', |
| 178 | KH: 'Cambodia', |
| 179 | CM: 'Cameroon', |
| 180 | CA: 'Canada', |
| 181 | CV: 'Cape Verde', |
| 182 | BQ: 'Caribbean Netherlands', |
| 183 | KY: 'Cayman Islands', |
| 184 | CF: 'Central African Republic', |
| 185 | TD: 'Chad', |
| 186 | CL: 'Chile', |
| 187 | CN: 'China', |
| 188 | CX: 'Christmas Island', |
| 189 | CC: 'Cocos Islands', |
| 190 | CO: 'Colombia', |
| 191 | KM: 'Comoros', |
| 192 | CG: 'Congo', |
| 193 | CK: 'Cook Islands', |
| 194 | CR: 'Costa Rica', |
| 195 | HR: 'Croatia', |
| 196 | CU: 'Cuba', |
| 197 | CW: 'Curaçao', |
| 198 | CY: 'Cyprus', |
| 199 | CZ: 'Czech Republic', |
| 200 | CD: 'Democratic Republic of the Congo', |
| 201 | DK: 'Denmark', |
| 202 | DJ: 'Djibouti', |
| 203 | DM: 'Dominica', |
| 204 | DO: 'Dominican Republic', |
| 205 | EC: 'Ecuador', |
| 206 | EG: 'Egypt', |
| 207 | SV: 'El Salvador', |
| 208 | GQ: 'Equatorial Guinea', |
| 209 | ER: 'Eritrea', |
| 210 | EE: 'Estonia', |
| 211 | ET: 'Ethiopia', |
| 212 | FK: 'Falkland Islands', |
| 213 | FO: 'Faroe Islands', |
| 214 | FM: 'Federated States of Micronesia', |
| 215 | FJ: 'Fiji', |
| 216 | FI: 'Finland', |
| 217 | FR: 'France', |
| 218 | GF: 'French Guiana', |
| 219 | PF: 'French Polynesia', |
| 220 | TF: 'French Southern Territories', |
| 221 | GA: 'Gabon', |
| 222 | GM: 'Gambia', |
| 223 | GE: 'Georgia', |
| 224 | DE: 'Germany', |
| 225 | GH: 'Ghana', |
| 226 | GI: 'Gibraltar', |
| 227 | GR: 'Greece', |
| 228 | GL: 'Greenland', |
| 229 | GD: 'Grenada', |
| 230 | GP: 'Guadeloupe', |
| 231 | GU: 'Guam', |
| 232 | GT: 'Guatemala', |
| 233 | GN: 'Guinea', |
| 234 | GW: 'Guinea-Bissau', |
| 235 | GY: 'Guyana', |
| 236 | HT: 'Haiti', |
| 237 | HN: 'Honduras', |
| 238 | HK: 'Hong Kong', |
| 239 | HU: 'Hungary', |
| 240 | IS: 'Iceland', |
| 241 | IN: 'India', |
| 242 | ID: 'Indonesia', |
| 243 | IR: 'Iran', |
| 244 | IQ: 'Iraq', |
| 245 | IE: 'Ireland', |
| 246 | IM: 'Isle of Man', |
| 247 | IL: 'Israel', |
| 248 | IT: 'Italy', |
| 249 | CI: 'Ivory Coast', |
| 250 | JM: 'Jamaica', |
| 251 | JP: 'Japan', |
| 252 | JE: 'Jersey', |
| 253 | JO: 'Jordan', |
| 254 | KZ: 'Kazakhstan', |
| 255 | KE: 'Kenya', |
| 256 | KI: 'Kiribati', |
| 257 | XK: 'Kosovo', |
| 258 | KW: 'Kuwait', |
| 259 | KG: 'Kyrgyzstan', |
| 260 | LA: 'Laos', |
| 261 | LV: 'Latvia', |
| 262 | LB: 'Lebanon', |
| 263 | LS: 'Lesotho', |
| 264 | LR: 'Liberia', |
| 265 | LY: 'Libya', |
| 266 | LI: 'Liechtenstein', |
| 267 | LT: 'Lithuania', |
| 268 | LU: 'Luxembourg', |
| 269 | MO: 'Macau', |
| 270 | MK: 'Macedonia', |
| 271 | MG: 'Madagascar', |
| 272 | MW: 'Malawi', |
| 273 | MY: 'Malaysia', |
| 274 | MV: 'Maldives', |
| 275 | ML: 'Mali', |
| 276 | MT: 'Malta', |
| 277 | MH: 'Marshall Islands', |
| 278 | MQ: 'Martinique', |
| 279 | MR: 'Mauritania', |
| 280 | MU: 'Mauritius', |
| 281 | YT: 'Mayotte', |
| 282 | MX: 'Mexico', |
| 283 | MD: 'Moldova', |
| 284 | MC: 'Monaco', |
| 285 | MN: 'Mongolia', |
| 286 | ME: 'Montenegro', |
| 287 | MS: 'Montserrat', |
| 288 | MA: 'Morocco', |
| 289 | MZ: 'Mozambique', |
| 290 | MM: 'Myanmar', |
| 291 | NA: 'Namibia', |
| 292 | NR: 'Nauru', |
| 293 | NP: 'Nepal', |
| 294 | NL: 'Netherlands', |
| 295 | NC: 'New Caledonia', |
| 296 | NZ: 'New Zealand', |
| 297 | NI: 'Nicaragua', |
| 298 | NE: 'Niger', |
| 299 | NG: 'Nigeria', |
| 300 | NU: 'Niue', |
| 301 | NF: 'Norfolk Island', |
| 302 | KP: 'North Korea', |
| 303 | MP: 'Northern Mariana Islands', |
| 304 | NO: 'Norway', |
| 305 | OM: 'Oman', |
| 306 | PK: 'Pakistan', |
| 307 | PW: 'Palau', |
| 308 | PS: 'Palestine', |
| 309 | PA: 'Panama', |
| 310 | PG: 'Papua New Guinea', |
| 311 | PY: 'Paraguay', |
| 312 | PE: 'Peru', |
| 313 | PH: 'Philippines', |
| 314 | PN: 'Pitcairn Islands', |
| 315 | PL: 'Poland', |
| 316 | PT: 'Portugal', |
| 317 | PR: 'Puerto Rico', |
| 318 | QA: 'Qatar', |
| 319 | RE: 'Reunion', |
| 320 | RO: 'Romania', |
| 321 | RU: 'Russia', |
| 322 | RW: 'Rwanda', |
| 323 | SH: 'Saint Helena', |
| 324 | KN: 'Saint Kitts and Nevis', |
| 325 | LC: 'Saint Lucia', |
| 326 | PM: 'Saint Pierre and Miquelon', |
| 327 | VC: 'Saint Vincent and the Grenadines', |
| 328 | WS: 'Samoa', |
| 329 | SM: 'San Marino', |
| 330 | ST: 'São Tomé and Príncipe', |
| 331 | SA: 'Saudi Arabia', |
| 332 | SN: 'Senegal', |
| 333 | RS: 'Serbia', |
| 334 | SC: 'Seychelles', |
| 335 | SL: 'Sierra Leone', |
| 336 | SG: 'Singapore', |
| 337 | SX: 'Sint Maarten', |
| 338 | SK: 'Slovakia', |
| 339 | SI: 'Slovenia', |
| 340 | SB: 'Solomon Islands', |
| 341 | SO: 'Somalia', |
| 342 | ZA: 'South Africa', |
| 343 | GS: 'South Georgia and the South Sandwich Islands', |
| 344 | KR: 'South Korea', |
| 345 | SS: 'South Sudan', |
| 346 | ES: 'Spain', |
| 347 | LK: 'Sri Lanka', |
| 348 | SD: 'Sudan', |
| 349 | SR: 'Suriname', |
| 350 | SJ: 'Svalbard and Jan Mayen', |
| 351 | SZ: 'Eswatini', |
| 352 | SE: 'Sweden', |
| 353 | CH: 'Switzerland', |
| 354 | SY: 'Syria', |
| 355 | TW: 'Taiwan', |
| 356 | TJ: 'Tajikistan', |
| 357 | TZ: 'Tanzania', |
| 358 | TH: 'Thailand', |
| 359 | TL: 'Timor-Leste', |
| 360 | TG: 'Togo', |
| 361 | TK: 'Tokelau', |
| 362 | TO: 'Tonga', |
| 363 | TT: 'Trinidad and Tobago', |
| 364 | TN: 'Tunisia', |
| 365 | TR: 'Turkey', |
| 366 | TM: 'Turkmenistan', |
| 367 | TC: 'Turks and Caicos Islands', |
| 368 | TV: 'Tuvalu', |
| 369 | UG: 'Uganda', |
| 370 | UA: 'Ukraine', |
| 371 | AE: 'United Arab Emirates', |
| 372 | GB: 'United Kingdom', |
| 373 | US: 'United States', |
| 374 | UM: 'United States Minor Outlying Islands', |
| 375 | VI: 'United States Virgin Islands', |
| 376 | UY: 'Uruguay', |
| 377 | UZ: 'Uzbekistan', |
| 378 | VU: 'Vanuatu', |
| 379 | VA: 'Vatican City', |
| 380 | VE: 'Venezuela', |
| 381 | VN: 'Vietnam', |
| 382 | WF: 'Wallis and Futuna', |
| 383 | EH: 'Western Sahara', |
| 384 | YE: 'Yemen', |
| 385 | ZM: 'Zambia', |
| 386 | ZW: 'Zimbabwe' |
| 387 | } |
| 388 | |
| 389 | return countries[countryCode]; |
| 390 | } |
| 391 | } |