-
Notifications
You must be signed in to change notification settings - Fork 0
/
ion-month-picker.js
89 lines (78 loc) · 2.7 KB
/
ion-month-picker.js
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
'use strict';
var ionMonthPicker = angular.module('ionMonthPicker', ['ionic']);
//Relative Path
var scripts = document.getElementsByTagName("script");
var currentScriptPath = scripts[scripts.length - 1].src;
var popUpTemplatePath = currentScriptPath.replace('ion-month-picker.js', 'ion-month-picker.html')
var directiveTemplatePath = currentScriptPath.replace('ion-month-picker.js', 'ion-select-element.html');
ionMonthPicker.directive('ionMonthPicker', function($timeout, $ionicPopup, $filter) {
return {
restrict: 'EAC',
scope: {
label: '@',
format: '@',
ngModel: '=?',
},
require: '?ngModel',
transclude: false,
replace: false,
templateUrl: directiveTemplatePath || 'ion-select-element.html',
link: function(scope, element, attrs, ngModel) {
scope.monthList = ["Jan", "Fev", "Mar", "Abr", "Mai", "Jun", "Jul", "Ago", "Set", "Out", "Nov", "Dez"];
var dataAtual = new Date();
scope.currentDate = {
year: dataAtual.getFullYear(),
month: dataAtual.getMonth()
};
scope.prev = function(currentYear) {
scope.currentDate.year = currentYear - 1;
};
scope.next = function(currentYear) {
scope.currentDate.year = currentYear + 1;
};
scope.setMonth = function(month) {
scope.currentDate.month = month;
};
var dateFormat = attrs.format;
console.log('dateformat',dateFormat);
attrs.$observe('format', function(newValue) {
if (dateFormat == newValue || !ngModel.$modelValue) return;
dateFormat = newValue;
ngModel.$modelValue = new Date(ngModel.$setViewValue);
});
ngModel.$formatters.unshift(function(modelValue) {
scope = scope;
if (!dateFormat || !modelValue) return "";
var retVal = $filter('date')(modelValue,dateFormat);
return retVal;
});
ngModel.$parsers.unshift(function(viewValue) {
scope = scope;
var date = $filter('date')(viewValue,dateFormat);
return date || "";
});
scope.resetInput = function() {
ngModel.$setViewValue(null);
ngModel.$render();
};
scope.openPopup = function() {
$ionicPopup.show({
templateUrl: popUpTemplatePath || 'ion-month-picker.html',
title: 'Selecione um mês',
subTitle: '',
scope: scope,
buttons: [{
text: 'Cancelar'
}, {
text: '<b>Selecionar</b>',
type: 'button-positive',
onTap: function(e) {
ngModel.$setViewValue(new Date(scope.currentDate.year, scope.currentDate.month));
ngModel.$render();
}
}]
});
};
}
};
});