Project

General

Profile

« Previous | Next » 

Revision 02fff58c

Added by Jérôme Schneider over 12 years ago

agenda: move js into a specific file

View differences:

calebasse/agenda/templates/agenda/index.html
1 1
{% extends "calebasse/base.html" %}
2 2
{% load url from future %}
3 3
{% block extrascripts %}
4
<script>
5
    function replace_anchor(url, anchor) {
6
        var splitted = url.split('#');
7
        return splitted[0] + '#' + encodeURI(anchor);
8
    }
9
    function extract_anchor() {
10
        if (window.location.href.indexOf('#') == -1) {
11
            return "";
12
        }
13
        var splitted = window.location.href.split('#');
14
        return decodeURI(splitted[1]).split(',');
15
    }
16
    function make_anchor() {
17
        return $.makeArray($('.person-item.active').map(function (v, i) { return '_' + $(i).attr('id'); })).join(',');
18
    }
19
    function toggle_worker(worker_selector) {
20
        $(worker_selector).toggleClass('active');
21
        // update the anchor
22
        var anchor = make_anchor();
23
        var new_uri = replace_anchor(window.location.href, anchor);
24
        console.log(new_uri);
25
        window.location.href = new_uri;
26
        $('a[href^="../"]').each(function (i, a) {
27
            $(a).attr('href', replace_anchor($(a).attr('href'), anchor));
28
        });
29

  
30
        var $target = $($(worker_selector).data('target'));
31
        $target.toggle();
32
        if ($target.is(':visible')) {
33
            $target.click();
34
        }
35
    }
36
    $(function() {
37
        $('#tabs').tabs();
38

  
39
        $('div.agenda > div').accordion({active: false, autoHeight: false});
40

  
41
        $('.person-item').on('click', function() {
42
            toggle_worker(this);
43
        });
44
        // select all anchors
45
        $.each(extract_anchor(), function (i, anchor) {
46
            $('#'+anchor.substr(1)).each(function (i, worker_selector) { toggle_worker(worker_selector); });
47
        });
48

  
49
        $('.textedit').on('keydown', function() {
50
            $('button', this).removeAttr("disabled");
51
        });
52
        $('.textedit button').on('click', function() {
53
            var textarea = $(this).prev();
54
            var span = textarea.prev()
55
            var btn = $(this)
56
            $.ajax({
57
                url: '/api/event/' + $(this).data("event-id") + '/?format=json',
58
                type: 'PATCH',
59
                contentType: 'application/json',
60
                data: '{"description": "' + textarea.val() + '"}',
61
                success: function(data) {
62
                    btn.attr('disabled', 'disabled');
63
                    span.html('Commentaire modifiée avec succès');
64
                }
65
            });
66
        });
67
        $('.appointment').on('click', function() {
68
            $('.textedit span', this).html('');
69
        });
70

  
71
        $('.remove-appointment').on('click', function() {
72
            var r = confirm("Etes-vous sûr de vouloir supprimer le rendez-vous " + $(this).data('rdv') + " ?");
73
            if (r == true)
74
            {
75
                $.ajax({
76
                    url: '/api/occurrence/' + $(this).data('occurrence-id') + '/',
77
                    type: 'DELETE',
78
                    success: function(data) {
79
                        window.location.reload(true);
80
                        return false;
81
                    }
82
                });
83
            }
84
            return false;
85
        });
86

  
87
        /* Gestion du filtre sur les utilisateurs */
88
        $('#filtre input').keyup(function() {
89
            var filtre = $(this).val();
90
            if (filtre) {
91
                $('#users li').each(function() {
92
                    if ($(this).text().match(filtre)) {
93
                        $(this).show();
94
                        } else {
95
                        $(this).hide();
96
                    }
97
                });
98
                } else {
99
                $('#users li').show();
100
            }
101

  
102
        });
103
        $('#agenda-date').datepicker({
104
            dateFormat: "DD d MM yy",
105
            onClose: function(dateText, inst) {
106
                console.log('close');
107
            }
108
        });
109
        $('#agenda-date').on('change', function () {
110
            window.location.href=replace_anchor('../' + $(this).datepicker('getDate').toISOString().substr(0,10), make_anchor());
111
        });
112
        $('.date').datepicker({showOn: 'button'});
113
        $('#add-intervenant-btn').click(function() {
114
            var text = $(this).prev().val();
115
            $('#intervenants ul').append('<li><input type="checkbox" value="' + text + '" checked="checked">' + text + '</input></li>');
116
            $(this).prev().val('').focus();
117
            return false;
118
        });
119
        $('#newrdv').click(function() {
120
            var participants = $('.person-item.active').map(function (i, v) { return $(v).data('worker-id'); });
121
            var qs = $.param({participants: $.makeArray(participants) }, true);
122
            var new_appointment_url = "{% url 'nouveau-rdv' service=service date=date %}?" + qs;
123
            $('#rdv').load(new_appointment_url,
124
            function () {
125
                function onsuccess(response, status, xhr, form) {
126
                    var parse = $(response);
127
                    if ($('.errorlist', parse).length != 0) {
128
                        $('#rdv').html(response);
129
                        $('#rdv form').ajaxForm({
130
                            success: onsuccess,
131
                        });
132
                        $('#rdv .datepicker-date').datepicker({dateFormat: 'yy-m-d', showOn: 'button'});
133
                        console.log('error');
134
                        } else {
135
                        console.log('success');
136
                        window.location.reload(true);
137
                    }
138
                }
139
                $('#rdv .datepicker-date').datepicker({dateFormat: 'yy-m-d', showOn: 'button'});
140
                $('form', this).ajaxForm({
141
                    success: onsuccess
142
                });
143
                $(this).dialog({title: 'Nouveau rendez-vous',
144
                    width: '800px',
145
                    buttons: [ { text: "Fermer",
146
                        click: function() { $(this).dialog("close"); } },
147
                    { text: "Ajouter",
148
                        click: function() { $("#rdv form").submit(); } }]});
149
            });
150
        });
151
    });
152
</script>
4
  <script src="{{ STATIC_URL }}js/calebasse.agenda.js"></script>
153 5
  <style>
154 6
    .person-item span { display: none; }
155 7
    .person-item.active span { display: inline; }
calebasse/static/js/calebasse.agenda.js
1

  
2
function replace_anchor(url, anchor) {
3
    var splitted = url.split('#');
4
    return splitted[0] + '#' + encodeURI(anchor);
5
}
6

  
7
function extract_anchor() {
8
    if (window.location.href.indexOf('#') == -1) {
9
        return "";
10
    }
11
    var splitted = window.location.href.split('#');
12
    return decodeURI(splitted[1]).split(',');
13
}
14

  
15
function make_anchor() {
16
    return $.makeArray($('.person-item.active').map(function (v, i) { return '_' + $(i).attr('id'); })).join(',');
17
}
18

  
19
function toggle_worker(worker_selector) {
20
    $(worker_selector).toggleClass('active');
21
    // update the anchor
22
    var anchor = make_anchor();
23
    var new_uri = replace_anchor(window.location.href, anchor);
24
    console.log(new_uri);
25
    window.location.href = new_uri;
26
    $('a[href^="../"]').each(function (i, a) {
27
        $(a).attr('href', replace_anchor($(a).attr('href'), anchor));
28
    });
29

  
30
    var $target = $($(worker_selector).data('target'));
31
    $target.toggle();
32
    if ($target.is(':visible')) {
33
        $target.click();
34
    }
35
}
36

  
37
$(function() {
38
    $('#tabs').tabs();
39

  
40
    $('div.agenda > div').accordion({active: false, autoHeight: false});
41

  
42
    $('.person-item').on('click', function() {
43
        toggle_worker(this);
44
    });
45
    // select all anchors
46
    $.each(extract_anchor(), function (i, anchor) {
47
        $('#'+anchor.substr(1)).each(function (i, worker_selector) { toggle_worker(worker_selector); });
48
    });
49

  
50
    $('.textedit').on('keydown', function() {
51
        $('button', this).removeAttr("disabled");
52
    });
53
    $('.textedit button').on('click', function() {
54
        var textarea = $(this).prev();
55
        var span = textarea.prev()
56
        var btn = $(this)
57
        $.ajax({
58
            url: '/api/event/' + $(this).data("event-id") + '/?format=json',
59
            type: 'PATCH',
60
            contentType: 'application/json',
61
            data: '{"description": "' + textarea.val() + '"}',
62
            success: function(data) {
63
                btn.attr('disabled', 'disabled');
64
                span.html('Commentaire modifiée avec succès');
65
            }
66
        });
67
    });
68
    $('.appointment').on('click', function() {
69
        $('.textedit span', this).html('');
70
    });
71

  
72
    $('.remove-appointment').on('click', function() {
73
        var r = confirm("Etes-vous sûr de vouloir supprimer le rendez-vous " + $(this).data('rdv') + " ?");
74
        if (r == true)
75
    {
76
        $.ajax({
77
            url: '/api/occurrence/' + $(this).data('occurrence-id') + '/',
78
            type: 'DELETE',
79
            success: function(data) {
80
                window.location.reload(true);
81
                return false;
82
            }
83
        });
84
    }
85
    return false;
86
    });
87

  
88
    /* Gestion du filtre sur les utilisateurs */
89
    $('#filtre input').keyup(function() {
90
        var filtre = $(this).val();
91
        if (filtre) {
92
            $('#users li').each(function() {
93
                if ($(this).text().match(filtre)) {
94
                    $(this).show();
95
                } else {
96
                    $(this).hide();
97
                }
98
            });
99
        } else {
100
            $('#users li').show();
101
        }
102

  
103
    });
104
    $('#agenda-date').datepicker({
105
        dateFormat: "DD d MM yy",
106
        onClose: function(dateText, inst) {
107
            console.log('close');
108
        }
109
    });
110
    $('#agenda-date').on('change', function () {
111
        window.location.href=replace_anchor('../' + $(this).datepicker('getDate').toISOString().substr(0,10), make_anchor());
112
    });
113
    $('.date').datepicker({showOn: 'button'});
114
    $('#add-intervenant-btn').click(function() {
115
        var text = $(this).prev().val();
116
        $('#intervenants ul').append('<li><input type="checkbox" value="' + text + '" checked="checked">' + text + '</input></li>');
117
        $(this).prev().val('').focus();
118
        return false;
119
    });
120
    $('#newrdv').click(function() {
121
        var participants = $('.person-item.active').map(function (i, v) { return $(v).data('worker-id'); });
122
        var qs = $.param({participants: $.makeArray(participants) }, true);
123
        var new_appointment_url = "{% url 'nouveau-rdv' service=service date=date %}?" + qs;
124
        $('#rdv').load(new_appointment_url,
125
            function () {
126
                function onsuccess(response, status, xhr, form) {
127
                    var parse = $(response);
128
                    if ($('.errorlist', parse).length != 0) {
129
                        $('#rdv').html(response);
130
                        $('#rdv form').ajaxForm({
131
                            success: onsuccess,
132
                        });
133
                        $('#rdv .datepicker-date').datepicker({dateFormat: 'yy-m-d', showOn: 'button'});
134
                        console.log('error');
135
                    } else {
136
                        console.log('success');
137
                        window.location.reload(true);
138
                    }
139
                }
140
                $('#rdv .datepicker-date').datepicker({dateFormat: 'yy-m-d', showOn: 'button'});
141
                $('form', this).ajaxForm({
142
                    success: onsuccess
143
                });
144
                $(this).dialog({title: 'Nouveau rendez-vous',
145
                    width: '800px',
146
                    buttons: [ { text: "Fermer",
147
                        click: function() { $(this).dialog("close"); } },
148
                    { text: "Ajouter",
149
                        click: function() { $("#rdv form").submit(); } }]});
150
            });
151
    });
152
});
153

  

Also available in: Unified diff