1"use strict"
2
3// The disable removes the 'datepicker' attribute and
4// settings, so you have to re-initialize it each time
5// the date range is selected and enabled
6// DOM is used instead of jQuery to find the elements
7// in all contexts
8function date_enable (key, action) {
9
10    var elemFrom=document.getElementById("date_from_"+key);
11    var elemTo=document.getElementById("date_to_"+key);
12
13    if ('enable' == action) {
14        elemFrom.removeAttribute("disabled");
15        elemTo.removeAttribute("disabled");
16
17        $(elemFrom).datepicker();
18        $(elemTo).datepicker();
19
20        $(elemFrom).datepicker( "option", "dateFormat", "dd/mm/yy" );
21        $(elemTo).datepicker( "option", "dateFormat", "dd/mm/yy" );
22
23        $(elemFrom).datepicker( "setDate", elemFrom.getAttribute( "data-setDate") );
24        $(elemTo).datepicker( "setDate", elemTo.getAttribute( "data-setDate") );
25        $(elemFrom).datepicker( "option", "minDate", elemFrom.getAttribute( "data-minDate"));
26        $(elemTo).datepicker( "option", "minDate",  elemTo.getAttribute( "data-minDate"));
27        $(elemFrom).datepicker( "option", "maxDate", elemFrom.getAttribute( "data-maxDate"));
28        $(elemTo).datepicker( "option", "maxDate", elemTo.getAttribute( "data-maxDate"));
29    } else {
30        elemFrom.setAttribute("disabled","disabled");
31        elemTo.setAttribute("disabled","disabled");
32    }
33}
34
35// Initialize the date picker elements with their default state variables, and
36// register the radio button and form actions
37function date_init (key, from_date, to_date, min_date, max_date, initial_enable) {
38
39    var elemFrom=document.getElementById("date_from_"+key);
40    var elemTo=document.getElementById("date_to_"+key);
41
42    // Were there any daterange filters instantiated? (e.g. no builds found)
43    if (null == elemFrom) {
44        return;
45    }
46
47    // init the datepicker context data
48    elemFrom.setAttribute( "data-setDate", from_date );
49    elemTo.setAttribute( "data-setDate", to_date );
50    elemFrom.setAttribute( "data-minDate", min_date);
51    elemTo.setAttribute( "data-minDate",  min_date);
52    elemFrom.setAttribute( "data-maxDate", max_date);
53    elemTo.setAttribute( "data-maxDate", max_date);
54
55    // does the date set start enabled?
56    if (key == initial_enable) {
57        date_enable (key, "enable");
58    } else {
59        date_enable (key, "disable");
60    }
61
62    // catch the radio button selects for enable/disable
63    $('input:radio[name="filter"]').change(function(){
64        if ($(this).val() == 'daterange') {
65            key=$(this).attr("data-key");
66            date_enable (key, 'enable');
67        } else {
68            key=$(this).attr("data-key");
69            date_enable (key, 'disable');
70        }
71    });
72
73    // catch any new 'from' date as minDate for 'to' date
74    $("#date_from_"+key).change(function(){
75        from_date = $("#date_from_"+key).val();
76        $("#date_to_"+key).datepicker( "option", "minDate", from_date );
77    });
78
79    // catch the submit (just once)
80    $("form").unbind('submit');
81    $("form").submit(function(e) {
82        // format a composite daterange filter value so that it can be parsed and post-processed in the view
83        if (key !== undefined) {
84            if ($("#date_from_"+key).length) {
85                var filter=key+"__gte!"+key+"__lt:"+$("#date_from_"+key).val()+"!"+$("#date_to_"+key).val()+"_daterange";
86                $("#last_date_from_"+key).val($("#date_from_"+key).val());
87                $("#last_date_to_"+key).val($("#date_to_"+key).val());
88                $("#filter_value_"+key).val(filter);
89            }
90        }
91        return true;
92    });
93
94};
95