summaryrefslogtreecommitdiff
path: root/assets/js/src/calendar.js
blob: 2f7316934dd9cd461284fe6fcad89178b10ac2bb (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
Kanboard.Calendar = (function() {

    var filter_storage_key = "";

    // 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()
            })
        });
    }

    // Show the user calendar
    function show_user_calendar()
    {
        var calendar = $("#user-calendar");
        var translations = calendar.data("translations");

        calendar.fullCalendar({
            lang: $("body").data("js-lang"),
            editable: false,
            eventLimit: true,
            defaultView: "agendaWeek",
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
            },
            viewRender: refresh_user_calendar
        });
    }

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

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

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

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

        calendar.fullCalendar({
            lang: $("body").data("js-lang"),
            editable: true,
            eventLimit: true,
            defaultView: "agendaWeek",
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
            },
            viewRender: load_project_filters,
            eventDrop: move_calendar_event
        });
    }

    // Refresh the calendar events
    function refresh_project_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_project_filters()
    {
        var filters = Kanboard.GetStorageItem(filter_storage_key);
        
        if (filters !== "") {
            filters = JSON.parse(filters);

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

        refresh_project_calendar(filters || {});

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

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

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

    jQuery(document).ready(function() {

        if (Kanboard.Exists("calendar")) {
            filter_storage_key = "calendar_filters_" + $("#calendar").data("project-id");
            show_project_calendar();
            load_project_filters();
        }
        else if (Kanboard.Exists("user-calendar")) {
            show_user_calendar();
        }
    });

})();