</div>\r
{literal}\r
<script type="text/javascript">\r
+(function (_) {\r
CRM.$(function($) {\r
- var $form = $('form.{/literal}{$form.formClass}{literal}');\r
+ var $form = $('form.{/literal}{$form.formClass}{literal}'),\r
+ defaultDate = null;\r
\r
// Prevent html5 errors\r
$form.attr('novalidate', 'novalidate');\r
}\r
})\r
.on('select2-opening', function(e) {\r
- var $el = $(this);\r
+ var $el = $(this),\r
+ data = $el.select2('data') || [],\r
+ existingSelections = _.pluck(data, 'id');\r
// Prevent select2 from opening and show a datepicker instead\r
e.preventDefault();\r
$('.select2-search-field input', $el.select2('container'))\r
- .datepicker()\r
+ .datepicker({changeMonth: true, changeYear: true})\r
+ .datepicker('option', 'defaultDate', defaultDate)\r
+ .datepicker('option', 'beforeShowDay', checkSelectedDates)\r
.datepicker('show')\r
.off('.crmDate')\r
.on('change.crmDate', function() {\r
if ($(this).val()) {\r
- var date = $(this).datepicker('getDate'),\r
- data = $el.select2('data') || [];\r
+ var date = defaultDate = $(this).datepicker('getDate');\r
data.push({\r
text: $.datepicker.formatDate(CRM.config.dateInputFormat, date),\r
id: $.datepicker.formatDate('yy-mm-dd', date)\r
});\r
- $el.select2('data', data);\r
+ $el.select2('data', data, true);\r
}\r
});\r
+ // Don't allow the same date to be selected twice\r
+ function checkSelectedDates(date) {\r
+ var dateStr = $.datepicker.formatDate('yy-mm-dd', date);\r
+ if (_.includes(existingSelections, dateStr)) {\r
+ return [false, '', '{/literal}{ts escape='js'}Already selected{/ts}{literal}'];\r
+ }\r
+ return [true, '', ''];\r
+ }\r
});\r
\r
+\r
// Dialog for preview repeat Configuration dates\r
function previewDialog() {\r
// Set default value for start date on activity forms before generating preview\r
$('[name=repetition_frequency_interval]', $form).each(pluralizeUnits).change(pluralizeUnits);\r
\r
});\r
-\r
+})(CRM._);\r
</script>\r
{/literal}\r