software-mariodiana
12/12/2014 - 2:19 PM

Fix IE bug for datepicker reopening. (I am not sure if this works on modern IE versions.)

Fix IE bug for datepicker reopening. (I am not sure if this works on modern IE versions.)

/**********************************************************
 * Hack to fix datepicker issue in IE. 
 * 
 * https://gist.github.com/sinelaw/5416130
 **********************************************************
 *
 * After jquery ui datepicker selection, blur and change 
 * events fire before focus is returned to the input field,
 * handling a quirk from IE browsers 
 */
var setupDatepickerIEHack = function() {
    // The 'isIE' var below requires an '.old-ie' class to be set on the html, 
    // for example:
    // <!--[if lt IE 7]>      <html class="old-ie lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
    // <!--[if IE 7]>         <html class="old-ie ie7 lt-ie9 lt-ie8"> <![endif]-->
    // <!--[if IE 8]>         <html class="old-ie ie8 lt-ie9"> <![endif]-->
    // <!--[if IE 9]>         <html class="old-ie ie9"> <![endif]-->
    // <!--[if (gt IE 9)|!(IE)]><!--> <html class="" lang="en"> <!--<![endif]-->

    var isIE = 0 < $('html.old-ie').length;

    $.datepicker.setDefaults({
        changeMonth: true,
        changeYear: true,
        showAnim: "fadeIn",
        yearRange: 'c-30:c+30',
        showButtonPanel: true,

        /* fix buggy IE focus functionality */
        fixFocusIE: false,

        /* blur needed to correctly handle placeholder text */
        onSelect: function(dateText, inst) {
            this.fixFocusIE = true;
        },

        onClose: function(dateText, inst) {
            this.fixFocusIE = true;
        },

        beforeShow: function(input, inst) {
            var result = isIE ? !this.fixFocusIE : true;
            this.fixFocusIE = false;
            return result;
        }
    });
};