1 (function(angular
, $, _
) {
2 // Represent a datetime field as if it were a radio ('schedule.mode') and a datetime ('schedule.datetime').
3 // example: <div crm-mailing-radio-date="mySchedule" ng-model="mailing.scheduled_date">...</div>
4 angular
.module('crmMailing').directive('crmMailingRadioDate', function(crmUiAlert
) {
7 link: function($scope
, element
, attrs
, ngModel
) {
10 var schedule
= $scope
[attrs
.crmMailingRadioDate
] = {
15 ngModel
.$render
= function $render() {
16 var sched
= ngModel
.$viewValue
;
17 if (!_
.isEmpty(sched
)) {
19 schedule
.datetime
= sched
;
22 schedule
.mode
= 'now';
23 schedule
.datetime
= '';
27 var updateParent
= (function() {
28 switch (schedule
.mode
) {
30 ngModel
.$setViewValue(null);
31 schedule
.datetime
= '';
34 schedule
.datetime
= schedule
.datetime
|| '?';
35 ngModel
.$setViewValue(schedule
.datetime
);
38 throw 'Unrecognized schedule mode: ' + schedule
.mode
;
43 // Open datepicker when clicking "At" radio
44 .on('click', ':radio[value=at]', function() {
45 $('.crm-form-date', element
).focus();
47 // Reset mode if user entered an invalid date
48 .on('change', '.crm-hidden-date', function(e
, context
) {
49 if (context
=== 'userInput' && $(this).val() === '' && $(this).siblings('.crm-form-date').val().length
) {
51 schedule
.datetime
= '?';
54 month
= '' + (d
.getMonth() + 1),
55 day
= '' + d
.getDate(),
56 year
= d
.getFullYear(),
57 hours
= '' + d
.getHours(),
58 minutes
= '' + d
.getMinutes();
59 var submittedDate
= $(this).val();
60 if (month
.length
< 2) month
= '0' + month
;
61 if (day
.length
< 2) day
= '0' + day
;
62 if (hours
.length
< 2) hours
= '0' + hours
;
63 if (minutes
.length
< 2) minutes
= '0' + minutes
;
64 date
= [year
, month
, day
].join('-');
65 time
= [hours
, minutes
, "00"].join(':');
66 currentDate
= date
+ ' ' + time
;
67 var isInPast
= ($(this).val().length
&& submittedDate
< currentDate
);
68 ngModel
.$setValidity('dateTimeInThePast', !isInPast
);
69 if (lastAlert
&& lastAlert
.isOpen
) {
73 lastAlert
= crmUiAlert({
74 text
: ts('The scheduled date and time is in the past'),
81 $scope
.$watch(attrs
.crmMailingRadioDate
+ '.mode', updateParent
);
82 $scope
.$watch(attrs
.crmMailingRadioDate
+ '.datetime', function(newValue
, oldValue
) {
83 // automatically switch mode based on datetime entry
84 if (typeof oldValue
=== 'undefined') {
87 if (typeof newValue
=== 'undefined') {
90 if (oldValue
!== newValue
) {
91 if (_
.isEmpty(newValue
)) {
92 schedule
.mode
= 'now';
103 })(angular
, CRM
.$, CRM
._
);