CRM-15932 - Multi-date-select2 widget improvements
authorColeman Watts <coleman@civicrm.org>
Sun, 8 Mar 2015 16:12:10 +0000 (12:12 -0400)
committerColeman Watts <coleman@civicrm.org>
Sun, 8 Mar 2015 16:23:02 +0000 (12:23 -0400)
templates/CRM/Core/Form/RecurringEntity.tpl

index d85e499f9e5ac84f84a527374eb516c07d69be07..83f51e543f409dccc092cd95cf18747f2aba428d 100644 (file)
 </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