summaryrefslogtreecommitdiff
path: root/assets/js/src/calendar.js
blob: fd6570f38f58763318fa570bd27abaf6835f61d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
Kanboard.Calendar = (function() {

    var filter_storage_key = "";

    // Show the empty calendar
    function show_calendar()
    {
        var calendar = $("#calendar");
        var translations = calendar.data("translations");

        calendar.fullCalendar({
            editable: true,
            eventLimit: true,
            header: {
                left: 'prev,next today',
                center: 'title',
                right: ''
            },
            viewRender: load_filters,
            eventDrop: move_calendar_event,
            monthNames: [translations.January, translations.February, translations.March, translations.April, translations.May, translations.June, translations.July, translations.August, translations.September, translations.October, translations.November, translations.December],
            monthNamesShort: [translations.Jan, translations.Feb, translations.Mar, translations.Apr, translations.May, translations.Jun, translations.Jul, translations.Aug, translations.Sep, translations.Oct, translations.Nov, translations.Dec],
            buttonText: {today: translations.Today},
            dayNames: [translations.Sunday, translations.Monday, translations.Tuesday, translations.Wednesday, translations.Thursday, translations.Friday, translations.Saturday],
            dayNamesShort: [translations.Sun, translations.Mon, translations.Tue, translations.Wed, translations.Thu, translations.Fri, translations.Sat]
        });
    }

    // Save the new due date for a moved task
    function move_calendar_event(calendar_event)
    {    
        $.ajax({
            cache: false,
            url: $("#calendar").data("save-url"),
            contentType: "application/json",
            type: "POST",
            processData: false,
            data: JSON.stringify({
                "task_id": calendar_event.id,
                "date_due": calendar_event.start.format()
            })
        });
    }

    // Refresh the calendar events
    function refresh_calendar(filters)
    {
        var calendar = $("#calendar");
        var url = calendar.data("check-url");
        var params = {
            "start": calendar.fullCalendar('getView').start.format(),
            "end": calendar.fullCalendar('getView').end.format()
        }

        jQuery.extend(params, filters);

        for (var key in params) {
            url += "&" + key + "=" + params[key];
        }

        $.getJSON(url, function(events) {
            calendar.fullCalendar('removeEvents');
            calendar.fullCalendar('addEventSource', events);         
            calendar.fullCalendar('rerenderEvents');
        });
    }

    // Restore saved filters
    function load_filters()
    {
        var filters = Kanboard.GetStorageItem(filter_storage_key);
        
        if (filters !== "undefined" && filters !== "") {
            filters = JSON.parse(filters);

            for (var filter in filters) {
                $("select[name=" + filter + "]").val(filters[filter]);
            }
        }

        refresh_calendar(filters || {});

        $('.calendar-filter').change(apply_filters);
    }

    // Apply filters on change
    function apply_filters()
    {
        var filters = {};

        $('.calendar-filter').each(function(index, element) {
            filters[$(this).attr("name")] = $(this).val();
        });
        
        Kanboard.SetStorageItem(filter_storage_key, JSON.stringify(filters));
        refresh_calendar(filters);
    }

    return {
        Init: function() {
            filter_storage_key = "calendar_filters_" + $("#calendar").data("project-id");
            show_calendar();
            load_filters();
        }
    };

})();