From 5f3e55036ee935cbc15c22edae094b1a6ffc66c0 Mon Sep 17 00:00:00 2001 From: Is Isilon Date: Mon, 28 Sep 2015 13:19:32 +0800 Subject: [PATCH] Added options for primary & local timezones, and show and sort by offset --- angular-timezone-selector.js | 46 +++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/angular-timezone-selector.js b/angular-timezone-selector.js index 10bed6c..c9466fd 100644 --- a/angular-timezone-selector.js +++ b/angular-timezone-selector.js @@ -77,19 +77,59 @@ angular.module('angular-timezone-selector', []) text: CCToCountryName[CC] + ': ', children: zonesByCountry firstNOffset: zonesByCountry[0].nOffset, - firstOffset: zonesByCountry[0].offset } data.push(zonesForCountry) }) - // Sort by country name - data = _.sortBy(data, 'text') + // Sort by UTC or country name + if (attrs.sortBy=="offset"){ + data = _.sortBy(data, 'firstNOffset'); + _.forEach(data,function(zonesForCountry,key){ + zonesForCountry.children=_.sortBy(zonesForCountry.children, 'nOffset'); + }); + } else { + data = _.sortBy(data, 'text') + } + + // add initial options forlocal + if (attrs.showLocal!=undefined){ + if (jstz!=undefined){ + var extraTZs = _.where(timezones,{'name':jstz.determine().name() }); + } else { + var localUTC = 'UTC'+moment().format('Z'); + var extraTZs = _.where(timezones,{'offset':localUTC}); + } + data.splice(0,0,{ + text: 'Local' + ': ', + children: extraTZs, + firstNOffset: extraTZs[0].nOffset, + firstOffset: extraTZs[0].offset + }) + } + + // add initial options + if (attrs.primaryChoices!=undefined){ + // var primaryChoices=['UTC','GB','WET','GMT','Asia/Macau'] + var primaryChoices = attrs.primaryChoices.split(' '); + var extraTZs = _.filter(timezones,function(tz){return _.contains(primaryChoices,tz.name)}); + data.splice(0,0,{ + text: 'Primary' + ': ', + children: extraTZs, + firstNOffset: extraTZs[0].nOffset, + firstOffset: extraTZs[0].offset + }) + } // Construct a select box with the timezones grouped by country _.forEach(data, function (group) { var $optgroup = $('') group.children.forEach(function (option) { + + if (attrs.displayUtc=="true" && !option.name.includes('(UTC')){ + option.name = option.name + ' (' + option.offset+')'; + } + $optgroup.append('') })