This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathpikaday-angular.js
146 lines (109 loc) · 3.66 KB
/
pikaday-angular.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['angular', 'pikaday'], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports, like Node.
module.exports = factory(require('angular'), require('pikaday'));
} else {
// Browser globals (root is window)
root.returnExports = factory(root.angular, root.Pikaday);
}
}(this, function (angular, Pikaday) {
angular.module('pikaday', [])
.provider('pikadayConfig', function pikadayProviderFn() {
// Create provider with getter and setter methods, allows setting of global configs
var config = {};
this.$get = function() {
return config;
};
this.setConfig = function setConfig(configs) {
config = configs;
};
})
.directive('pikaday', ['pikadayConfig', pikadayDirectiveFn]);
function pikadayDirectiveFn(pikadayConfig) {
return {
restrict: 'A',
scope: {
pikaday: '=', onSelect: '&', onOpen: '&', onClose: '&', onDraw: '&', disableDayFn: '&'
},
link: function (scope, elem, attrs) {
// Init config Object
var config = { field: elem[0], onSelect: function () {
setTimeout(function(){
scope.$apply();
});
}};
// Decorate config with globals
angular.forEach(pikadayConfig, function (value, key) {
config[key] = value;
});
// Decorate/Overide config with inline attributes
angular.forEach(attrs.$attr, function (dashAttr) {
var attr = attrs.$normalize(dashAttr); // normalize = ToCamelCase()
applyConfig(attr, attrs[attr]);
});
function applyConfig (attr, value) {
switch (attr) {
// Booleans, Integers & Arrays
case "setDefaultDate":
case "bound":
case "reposition":
case "disableWeekends":
case "showWeekNumber":
case "isRTL":
case "showMonthAfterYear":
case "firstDay":
case "yearRange":
case "numberOfMonths":
case "mainCalendar":
config[attr] = scope.$eval(value);
break;
// Functions
case "onSelect":
case "onOpen":
case "onClose":
case "onDraw":
case "disableDayFn":
config[attr] = function (date) {
setTimeout(function(){
scope.$apply();
});
return scope[attr]({ pikaday: this, date: date });
};
break;
// Strings
case "format":
case "position":
case "theme":
case "yearSuffix":
config[attr] = value;
break;
// Dates
case "minDate":
case "maxDate":
case "defaultDate":
config[attr] = new Date(value);
break;
// Elements
case "trigger":
case "container":
config[attr] = document.getElementById(value);
break;
// Translations
case "i18n":
config[attr] = pikadayConfig.locales[value];
}
}
// instantiate pikaday with config, bind to scope, add destroy event callback
var picker = new Pikaday(config);
scope.pikaday = picker;
scope.$on('$destroy', function () {
picker.destroy();
});
}
};
}
}));