Skip to content

Instantly share code, notes, and snippets.

@thers
Created April 26, 2014 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thers/11314963 to your computer and use it in GitHub Desktop.
Save thers/11314963 to your computer and use it in GitHub Desktop.
"use strict";
(function (ng, w) {
try {
var module = ng.module('user.directives');
} catch(e) {
var module = ng.module('user.directives', []);
}
module
.directive('uaBirthdaySelect', [
function () {
var
currentYear = (new Date()).getFullYear(),
// Filling years
calcYears = function () {
var years = [];
for (var i = currentYear - 13; i > 1900; i--) {
years.push(i.toString());
}
return years;
},
// Filling months
calcMonths = function () {
var monthString = '', months = {};
for (var i = 1; i <= 12; i++) {
monthString = i < 10 ? '0' + i.toString() : i.toString();
months[monthString] = w.i18n[monthString];
}
return months;
},
// Filling days
calcDays = function (year, month) {
var days = [];
for (var i = (new Date(year, parseInt(month), 0)).getDate(); i > 0; i--) {
days.push(i);
}
return days;
};
return {
restrict: 'AE',
scope: {
saveTo: '='
},
templateUrl: 'template/uaBirthdaySelect/template',
replace: true,
link: function ($scope) {
$scope.calc = {
years: calcYears(),
months: calcMonths(),
days: calcDays
};
$scope.current = {
year: $scope.calc.years[0],
month: '01',
day: 1
};
try {
var saveToString = $scope.saveTo.toString();
if (saveToString.length == 8) {
$scope.current = {
year: saveToString.substr(0, 4),
month: saveToString.substr(4, 2),
day: parseInt(saveToString.substr(6, 2))
};
}
} catch (e) {
}
$scope.$watch('current', function (current) {
$scope.saveTo = current.year.toString() + current.month + (current.day < 10 ? '0' + current.day.toString() : current.day.toString());
}, true);
}
};
}])
.run([
'$templateCache',
function ($templateCache) {
$templateCache.put('template/uaBirthdaySelect/template', [
'<div class="input-group ua-s-birthday-select">',
'<select class="form-control input-sm" ng-model="current.day" ng-options="day for day in calc.days(current.year, current.month)"></select>',
'<select class="form-control input-sm" ng-model="current.month" ng-options="num as title for (num, title) in calc.months"></select>',
'<select class="form-control input-sm" ng-model="current.year" ng-options="year as year+\'г.\' for year in calc.years"></select>',
'</div>'
].join(''));
}
]);
})(angular, window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment