CRM-15932 - Fix multi-date widget edge-case behavior
authorColeman Watts <coleman@civicrm.org>
Sun, 8 Mar 2015 19:50:21 +0000 (15:50 -0400)
committerColeman Watts <coleman@civicrm.org>
Sun, 8 Mar 2015 19:50:21 +0000 (15:50 -0400)
templates/CRM/Core/Form/RecurringEntity.tpl

index 83f51e543f409dccc092cd95cf18747f2aba428d..cd5659cb318bb7ac470073c4dab47bbee0d4a438 100644 (file)
     })\r
       .on('select2-opening', function(e) {\r
         var $el = $(this),\r
-          data = $el.select2('data') || [],\r
-          existingSelections = _.pluck(data, 'id');\r
+          $input = $('.select2-search-field input', $el.select2('container'));\r
         // Prevent select2 from opening and show a datepicker instead\r
         e.preventDefault();\r
-        $('.select2-search-field input', $el.select2('container'))\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 = 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, 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
+        if (!$input.data('datepicker')) {\r
+          $input\r
+            .datepicker({\r
+              beforeShow: function() {\r
+                var existingSelections = _.pluck($el.select2('data') || [], 'id');\r
+                return {\r
+                  changeMonth: true,\r
+                  changeYear: true,\r
+                  defaultDate: defaultDate,\r
+                  beforeShowDay: function(date) {\r
+                    // Don't allow the same date to be selected twice\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
+            .datepicker('show')\r
+            .on('change.crmDate', function() {\r
+              if ($(this).val()) {\r
+                var date = defaultDate = $(this).datepicker('getDate'),\r
+                data = $el.select2('data') || [];\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, true);\r
+              }\r
+            })\r
+            .on('keyup', function() {\r
+              $(this).val('').datepicker('show');\r
+            });\r
         }\r
+      })\r
+      // Don't leave datepicker open when clearing selections\r
+      .on('select2-removed', function() {\r
+        $('input.hasDatepicker', $(this).select2('container'))\r
+          .datepicker('hide');\r
       });\r
 \r
 \r