-
Notifications
You must be signed in to change notification settings - Fork 2
/
country-select.html
119 lines (115 loc) · 3.63 KB
/
country-select.html
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
<link rel="import" href="../polymer/polymer.html">
<!-- Ensure Web Animations polyfill is loaded since neon-animation 2.0 doesn't import it -->
<link rel="import" href="../neon-animation/web-animations.html">
<link rel="import" href="../paper-dropdown-menu/paper-dropdown-menu.html">
<link rel="import" href="../paper-item/paper-item.html">
<link rel="import" href="../paper-listbox/paper-listbox.html">
<link rel="import" href="../app-localize-behavior/app-localize-behavior.html">
<!--
`country-select`
A simple country selector web component using the paper-dropdown-menu that implements the app-localize-behavior and can translate the country names. Currently it supports localized country names for 253 languages. The country list is based on [umpirsky/country-list](https://github.com/umpirsky/country-list).
@demo demo/index.html
-->
<dom-module id="country-select">
<template>
<style>
:host {
display: block;
}
</style>
<paper-dropdown-menu label="{{label}}">
<paper-listbox slot="dropdown-content" class="dropdown-content" attr-for-selected="code" selected="{{selectedCountry}}">
<template is="dom-repeat" items="{{_countries}}">
<paper-item code="{{item.code}}">[[item.name]]</paper-item>
</template>
</paper-listbox>
</paper-dropdown-menu>
</template>
<script>
Polymer({
is: 'country-select',
behaviors: [
Polymer.AppLocalizeBehavior
],
properties: {
/**
* The label to be displayed in the dropdown menu.
*
* @attribute label
* @type String
* @default 'Country'
*/
label: {
type: String,
value: 'Country'
},
/**
* The currently selected country. The country is represented by its [ISO 3166-1 alpha-2 code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2).
*
* @attribute selectedCountry
* @type String
*/
selectedCountry: {
type: String,
notify: true
},
/**
* The language code that is used to display the country names. It is represented using a "locale code" defined in [BCP 47](https://tools.ietf.org/html/bcp47).
*
* @attribute language
* @type String
* @default 'en'
*/
language: {
type: String,
value: 'en'
},
_codeUrl: {
type: String,
computed: '_calculateUrl(language)',
observer: '_reloadResources'
},
_countries: {
type: Array
},
_selectedIndex: {
type: Number
}
},
ready: function() {
this.addEventListener('app-localize-resources-loaded', this._onTranslationLoaded);
},
attached: function() {
this._reloadResources(this._codeUrl);
},
_reloadResources: function(url) {
this.loadResources(this.resolveUrl(url));
},
_onTranslationLoaded: function() {
var countries = [];
var lang = this.language.replace('_', '-');
var values = this.resources[lang];
var codes = Object.keys(values);
for (var i = 0; i < codes.length; i++) {
var country = {};
country.code = codes[i];
country.name = values[country.code];
countries.push(country);
}
countries.sort(this._compareCountry);
this.set('_countries', countries);
},
_calculateUrl: function(language) {
var lang = language.replace('-', '_');
return '../country-list/data/' + lang + '/formatJsCountries.json';
},
_compareCountry: function(a, b) {
if (a.name < b.name)
return -1;
if (a.name > b.name)
return 1;
return 0;
}
});
</script>
</dom-module>