summaryrefslogtreecommitdiff
path: root/public/assets/js/panel/own_petitions.js
blob: 84121e6a8122a8c8505bdb76340d5e207eed9ee1 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Globals
// -----------------------------------------------------------------------------
var petitions = [];
var table;



// Supplamentary functions
// -----------------------------------------------------------------------------

// array.indexOf polyfill
// --- -- -- - - -
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length >>> 0;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}


// return petitions record by petition-id
// --- -- -- - - -
function petition_record(id) {
    var record = false;
    petitions.forEach(el => { if (el.id == id) record = el; });
   
    return record;
}


// create actions html (edit and delete buttons)
// --- -- -- - - -
function create_actions_html(signature) {

    var del, edit, show;

    del = "";
    edit = "";

    show = [
        '<a type="button" target="_blank" class="btn btn-xs btn-warning"',
            'href="/petition/signature/'+ signature +'">',
            '<i class="fa-regular fa-circle-down"></i>',
        '</a>'
    ].join('\n');

    return show +' '+ edit +' '+ del;
}



$(document).ready(function() {

    // When document is ready
    // -------------------------------------------------------------------------

       
    /** init_table
     * --- -- -- - - -
     * get categories
     * constuct categories array
     * render dataTable 
     */
    function init_table() {

        // get all categories from server (ajax)
        $.getJSON( "/user/get/petitions")
        .done(function( json ) {
            // then ...
            console.log(json);

            // reset categories
            petitions.length = 0;

            // construct petitions data
            json.data.forEach( el => {
                petitions.push({
                    id: parseInt(el.id),
                    subject: el.subject,
                    protocol: el.protocol,
                    signature: el.signature,
                    datepost: el.creation_date,
                    actions: create_actions_html(el.signature)
                });
            });


            // destroy previous datatable table instances
            $('#dt-petitions').dataTable().fnClearTable();
            $('#dt-petitions').dataTable().fnDestroy();

            // (re-)create categories datatable
            table = $('#dt-petitions').DataTable({
                language: { url: '/assets/js/DataTables/localization/Greek.json' },           
                data: petitions,
                // ordering: false,
                columns: [
                    { data: 'subject', width: '55%' },
                    { data: 'protocol' },
                    // { data: 'signature' },
                    { data: 'datepost' },
                    { data: 'actions' }
                ]
            });

        })
        .fail(function( jqxhr, textStatus, error ) {
            console.log( "Request Failed (" + error +")" );
        });

    }
    init_table();   // init table on first run



    /** When modal show-up
     * -------------------------------------------------------------------------
     * ... prepare the manage-category form 
     * ... update select2 with possible parents
     */
    $('#manageCategory').on('show.bs.modal', event => {
        var button = $(event.relatedTarget);        // Button that triggered the modal
        var id = parseInt( button.data('id') );     // category_id from data-id
        // var modal = $(this);    // modal object     
        
        // update modal literature
        // ---
        $('#manageCategory .modal-title').html(             // modal title
            (id) ? 'Ενημέρωση Κατηγορίας' : 'Δημιουργία Κατηγορίας'
        );
        $('#manageCategory form input[name=id]').val( id );     // category id (hidden)
        $('#manageCategory form input[name=title]').val(    // category title
            (id) ? category_record(id).title : ""
        );
        prepare_parents_element(id);        // prepare <select> for parents
    })



    // When form is submited
    // -------------------------------------------------------------------------

    $('#manageCategory form').submit( event => {
        event.preventDefault();

        // prepare data to POST
        var data = {
            id: parseInt($('#manageCategory form input[name=id]').val()),
            title: $('#manageCategory form input[name=title]').val(),
            parent_id: parseInt($('#parent_id').select2('data')[0].id)
        };

        // select action url (add or update)
        var request = '/wiki/ajax?action=' + ((data.id == 0)  ? 'add_category' : 'update_category');

        // send POST request
        $.post(request, data)
        .done(function( data ) {

            $('#manageCategory').modal('hide');     // when done, close modal

            init_table();       // reload table of categories
        });       

    });

});