Skip to content
This repository was archived by the owner on Oct 29, 2018. It is now read-only.

Commit 4595e75

Browse files
Merge pull request #3 from wassname/master
Added options
2 parents 8481a76 + ae90bc4 commit 4595e75

10 files changed

+177
-17
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,31 @@ Then use directive `timezone-selector`.
2828
<timezone-selector ng-model="timezone">
2929
```
3030

31+
## Options
32+
33+
Options can be included as attributes in the html element.
34+
35+
- **sort-by** ["offset"] - This lets the list be sorted by UTC offset instead of alphabetical order.
36+
- **display-utc** ["true"] - This show UTC offsets in the timezone names
37+
- **show-local** ["true"] - This detects local timezone's and includes at the top. If jsTimezoneDetect is installed if will include the detected timezone otherwise it fallback on moment js and list all timezones with the same browsers UTC offset.
38+
- **primary-choices** ["space seperated timezone names"] - This lets you put important timezone's at the top of the list or include extra aliases. Use names from momentjs-timezone, which you can list with the command: `moment.tz.names;`.
39+
40+
An example of using the options is below:
41+
42+
```html
43+
<timezone-selector
44+
ng-model="timezone"
45+
display-utc="true"
46+
sort-by="offset"
47+
show-local="true"
48+
primary-choices="UTC GB WET GMT Asia/Macau"
49+
></timezone-selector>
50+
```
51+
52+
## Screenshot
53+
The screenshot below show angular-selector in action with all options enabled:
54+
<img src="./images/primary_local_selection.png" alt-text="Angular-selector in action with all options enabled"></img>
55+
3156
# Attributions
3257
Inspired by [angular-timezone-select](https://github.com/alexcheng1982/angular-timezone-select) from [alexcheng1982](https://github.com/alexcheng1982).
3358

angular-timezone-selector.js

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ angular.module('angular-timezone-selector', [])
1616
.factory('timezones', ['_', 'moment', function (_, moment) {
1717
var timezoneMap = {}
1818
_.forEach(moment.tz.names(), function (zoneName) {
19+
var tz=moment.tz(zoneName);
1920
timezoneMap[zoneName] = {
2021
id: zoneName,
2122
name: zoneName.replace(/_/g, ' '),
22-
offset: 'UTC' + moment().tz(zoneName).format('Z')
23+
offset: 'UTC' + tz.format('Z'),
24+
nOffset: tz.utcOffset()
2325
}
2426
})
2527
return timezoneMap
@@ -73,19 +75,61 @@ angular.module('angular-timezone-selector', [])
7375
_.forEach(timezonesGroupedByCC, function (zonesByCountry, CC) {
7476
var zonesForCountry = {
7577
text: CCToCountryName[CC] + ': ',
76-
children: zonesByCountry
78+
children: zonesByCountry,
79+
firstNOffset: zonesByCountry[0].nOffset
7780
}
7881

7982
data.push(zonesForCountry)
8083
})
8184

82-
// Sort by country name
83-
data = _.sortBy(data, 'text')
85+
// Sort by UTC or country name
86+
if (attrs.sortBy=="offset"){
87+
data = _.sortBy(data, 'firstNOffset');
88+
_.forEach(data,function(zonesForCountry,key){
89+
zonesForCountry.children=_.sortBy(zonesForCountry.children, 'nOffset');
90+
});
91+
} else {
92+
data = _.sortBy(data, 'text')
93+
}
94+
95+
// add initial options forlocal
96+
if (attrs.showLocal!=undefined){
97+
if (jstz!=undefined){
98+
var extraTZs = _.where(timezones,{'name':jstz.determine().name() });
99+
} else {
100+
var localUTC = 'UTC'+moment().format('Z');
101+
var extraTZs = _.where(timezones,{'offset':localUTC});
102+
}
103+
data.splice(0,0,{
104+
text: 'Local' + ': ',
105+
children: extraTZs,
106+
firstNOffset: extraTZs[0].nOffset,
107+
firstOffset: extraTZs[0].offset
108+
})
109+
}
110+
111+
// add initial options
112+
if (attrs.primaryChoices!=undefined){
113+
// var primaryChoices=['UTC','GB','WET','GMT','Asia/Macau']
114+
var primaryChoices = attrs.primaryChoices.split(' ');
115+
var extraTZs = _.filter(timezones,function(tz){return _.contains(primaryChoices,tz.name)});
116+
data.splice(0,0,{
117+
text: 'Primary' + ': ',
118+
children: extraTZs,
119+
firstNOffset: extraTZs[0].nOffset,
120+
firstOffset: extraTZs[0].offset
121+
})
122+
}
84123

85124
// Construct a select box with the timezones grouped by country
86125
_.forEach(data, function (group) {
87126
var $optgroup = $('<optgroup label="' + group.text + '">')
88127
group.children.forEach(function (option) {
128+
129+
if (attrs.displayUtc=="true" && !option.name.includes('(UTC')){
130+
option.name = option.name + ' (' + option.offset+')';
131+
}
132+
89133
$optgroup.append('<option value="' + option.id + '">' +
90134
option.name + '</option>')
91135
})

bower.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-timezone-selector",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"homepage": "https://github.com/mishguruorg/angular-timezone-selector",
55
"authors": [
66
"Ashok Fernandez <ashok@mish.guru>"
@@ -29,5 +29,8 @@
2929
"lodash": "~3.9.3",
3030
"chosen": "~1.4.2",
3131
"bootstrap": "~3.3.4"
32+
},
33+
"devDependencies": {
34+
"jsTimezoneDetect": "latest"
3235
}
3336
}

build/angular-timezone-selector.js

Lines changed: 48 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-timezone-selector.js

Lines changed: 48 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-timezone-selector.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-timezone-selector.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

images/primary_local_selection.png

19.2 KB
Loading

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-timezone-selector",
3-
"version": "1.0.1",
3+
"version": "1.2.0",
44
"description": "AngularJS timezone selector",
55
"main": "dist/angular-timezone-selector.min.js",
66
"scripts": {

styling/angular-timezone-selector.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)