acha-framework
Version:
is a modular framework on both client (angular.js) and server (node.js) side, it provides security, orm, ioc, obfuscation and ...
184 lines • 6.72 kB
JavaScript
(function ($, angular, underscore, window, document, undefined) {
'use strict';
angular.module('frontend.directives').directive('calendar', [
'cultureService',
function (cultureService) {
return {
restrict: 'E',
replace: true,
scope: {
tag: '<?',
disabled: '=?',
visible: '=?',
cssClass: '=?',
culture: '=?',
onChange: '=?',
mode: '=?',
model: '=?'
},
templateUrl: '/templates/framework/directives/calendar/template.html',
link: function (scope, element, attr) {
scope.vm = {
cultureService: cultureService,
calendar: null,
current: null,
tempMonth: 0,
tempYear: 0,
tempYears: []
};
scope.vm.setToday = function () {
scope.vm.update(scope.vm.today.year, scope.vm.today.month, scope.vm.today.day);
if (scope.mode !== 'days')
scope.vm.switch('days');
else
scope.vm.paintDays();
};
scope.vm.paintDays = function () {
var result = [];
var currentMonthIndex = scope.vm.tempMonth - 1;
var prevMonthIndex = currentMonthIndex === 0 ? 11 : currentMonthIndex - 1;
var prevMonthDays = scope.vm.calendar.daysInMonth[prevMonthIndex];
var currentMonthDays = scope.vm.calendar.daysInMonth[currentMonthIndex];
var nextMonthIndex = currentMonthIndex === 11 ? 0 : currentMonthIndex + 1;
var nextYear = nextMonthIndex === 0 ? scope.vm.tempYear + 1 : scope.vm.tempYear;
var monthStart = scope.vm.calendar.getMonthStartWeekDate(scope.vm.tempYear, scope.vm.tempMonth);
var prevYear = prevMonthIndex === 11 ? scope.vm.tempYear - 1 : scope.vm.tempYear;
for (var i = prevMonthDays - monthStart + scope.vm.calendar.weekGap; i <= prevMonthDays; i++) {
result.push({
year: prevYear,
month: prevMonthIndex + 1,
number: i,
old: true
});
}
for (i = 1; i <= currentMonthDays; i++) {
result.push({
year: scope.vm.tempYear,
month: scope.vm.tempMonth,
number: i
});
}
var remaining = 7 - result.length % 7;
if (remaining) {
for (i = 1; i <= remaining; i++) {
result.push({
year: nextYear,
month: nextMonthIndex + 1,
number: i,
old: true
});
}
}
scope.vm.days = result.partition(7);
};
scope.vm.paintYears = function () {
var result = [];
for (var i = scope.vm.tempYear - 6; i <= scope.vm.tempYear + 5; i++) {
result.push(i);
}
scope.vm.tempYears = result;
};
scope.vm.forwardMonth = function () {
if (scope.vm.tempMonth === 12) {
scope.vm.tempMonth = 1;
scope.vm.tempYear++;
} else {
scope.vm.tempMonth++;
}
scope.vm.paintDays();
};
scope.vm.backwardMonth = function () {
if (scope.vm.tempMonth === 1) {
scope.vm.tempMonth = 12;
scope.vm.tempYear--;
} else {
scope.vm.tempMonth--;
}
scope.vm.paintDays();
};
scope.vm.updateYear = function (year) {
scope.vm.update(year, scope.vm.current.month, scope.vm.current.day);
};
scope.vm.updateMonth = function (month) {
scope.vm.update(scope.vm.current.year, month, scope.vm.current.day);
};
scope.vm.updateDay = function (day) {
scope.vm.update(scope.vm.current.year, scope.vm.current.month, day);
};
scope.vm.forwardYears = function (years) {
scope.vm.tempYear += years;
scope.vm.paintYears();
};
scope.vm.backwardYears = function (years) {
scope.vm.tempYear -= years;
scope.vm.paintYears();
};
scope.vm.switch = function (mode) {
scope.mode = mode;
};
scope.vm.getMonthName = function () {
return scope.vm.calendar.monthNames[scope.vm.tempMonth - 1];
};
scope.vm.init = function () {
if (angular.isUndefined(scope.disabled)) {
scope.disabled = false;
}
if (angular.isUndefined(scope.visible)) {
scope.visible = true;
}
if (angular.isUndefined(scope.cssClass)) {
scope.cssClass = '';
}
if (angular.isUndefined(scope.mode)) {
scope.mode = 'days';
}
if (angular.isUndefined(scope.culture)) {
scope.culture = cultureService.lang;
}
if (angular.isUndefined(scope.model)) {
scope.model = new Date();
}
scope.vm.bind();
};
scope.vm.update = function (year, month, day) {
scope.vm.current.year = year;
scope.vm.current.month = month;
scope.vm.current.day = day;
scope.vm.tempYear = year;
scope.vm.tempMonth = month;
scope.model = scope.vm.current.toDate();
if (scope.onChanged) {
scope.onChanged(scope.model, scope.tag);
}
};
scope.vm.bind = function () {
var culture = scope.culture.toLowerCase();
switch (culture) {
case 'fa':
this.calendar = PersianCalendar;
break;
case 'ar':
this.calendar = HijriCalendar;
break;
default:
this.calendar = GregorianCalendar;
}
scope.vm.current = new this.calendar(scope.model);
scope.vm.today = new this.calendar(new Date());
scope.vm.update(scope.vm.current.year, scope.vm.current.month, scope.vm.current.day);
scope.$watch('mode', function (val) {
switch ((val || '').toLowerCase()) {
case 'days':
scope.vm.paintDays();
break;
case 'years':
scope.vm.paintYears();
}
});
};
scope.vm.init();
}
};
}
]);
}(jQuery, angular, _, window, document));