summaryrefslogtreecommitdiff
path: root/public/app/views/welcome.php
blob: e97f748336832f440dcdc951d2122f99ff8825f0 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Welcome!</title>

    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">

    <!-- jQuery library -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.4/dist/jquery.slim.min.js"></script>

    <!-- select2 -->
    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>

    <style>
label { font-size:12px; }
.col, .col-1, .col-10, .col-11, .col-12, .col-2, .col-3, .col-4, .col-5, .col-6, .col-7, .col-8, .col-9 { padding-left: 0; }
.form-group { margin-bottom: .667rem; }
select { height: 31px; padding-left: 8px; padding-right: 24px; }
select, input,
select option { font-size: .87rem; }
    </style>
    
</head>
<body class='classroom'>
    <div class="container" style="max-width: 640px;">
    <div class="row">

    <?php
      $form = JsonToForm::make_a_form(REGISTRATION_FORM);
    ?>

        <?=$form['html']?>

    </div>
    </div>


        
<script>
// NOW RUN EVERYTHING AFTER JQUERY (AND DOM ready)
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////

// 00. FLAGS and NEEDED FUNCTIONS
// ---------------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////////

var waitingResponse = false;    // flag for an ajax request that was sent and still waiting response

// pure JS ajax GET request
// return data as JSON
// no fancy things like UTF8; if needed use base64 ---------------------------
function ajax_get(url, callback) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

            try {
                var data = JSON.parse(xmlhttp.responseText);
            } catch(err) {
                console.log(err.message + ' in ' + xmlhttp.responseText);
                return;
            }
            callback(data);
        }
    };

    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}


// 01. MENU MANAGEMENT
// ---------------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////////

// menu management
// ---------------------------------------------------------------------------
$('.dropdown-item').on('click', function(e){
    e.preventDefault();

    if (!waitingResponse) {
    waitingResponse = true;

    var r = confirm('Δεν έχετε αποθηκεύσει το τρέχον αντικείμενο!\nΕίστε σίγουροι ότι θέλετε να εγκαταλείψετε τη σελίδα;');
    if (r == true) {

        // all data-goto have the format: 'method,uri_address'
        // uri_address is the url to call
        // method options:
        // * url : means goto url (redirect)
        // * ajax : means call url via ajax; if 'success = true' then goto '/' after that
        // -----------------------------------------------------------------------
        var opt = $(this).data('goto').split(',');  // split data-goto

        if (opt[0] == 'url') {    // if method = url
        window.location.href = opt[1];
        }
        else {                    // else, method = ajax
        ajax_get(opt[1], function(response) {
            if (response.success) {
            window.location.href = '/';
            }
        });
        }

    }
    else {
        // user did not confirmed the selection; do nothing
    }

    waitingResponse = false;
    }

});

// 02. USER INTERFACE
// ---------------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////////

// tabs to accorderon --------------------------------------------------------
//////////////////////////////////////////////////////////////////////////////

// FUNCTION: Set tabsContainer height
// acording to which tab is visible
// and tab mode (tabs|acordeon)
// ---------------------------------------------------------------------------
function set_tabsContainer_height() {
    var ch_;      // content height
    var tc_base;    // tabs-container base height
    tc_base = ($(window).width() > 650) ? 100 : 400;
    if ($('#content1').is(':visible'))  ch_ = $('#content1').height();
    if ($('#content2').is(':visible'))  ch_ = $('#content2').height();
    if ($('#content3').is(':visible'))  ch_ = $('#content3').height();
    if ($('#content4').is(':visible'))  ch_ = $('#content4').height();
    if ($('#content5').is(':visible'))  ch_ = $('#content5').height();
    if ($('#content6').is(':visible'))  ch_ = $('#content6').height();

    $('#tabsContainer').height(ch_ + tc_base);

}
set_tabsContainer_height();  // init (run for 1st time)

// on tab selection
// calculate tabsContainer height
$('input:radio[name="tabs"]').change( function(){
    set_tabsContainer_height()
});

// on window change
// set tabsContainer height
$( window ).resize(function() {
    set_tabsContainer_height();
});



// 03. FORM-CONTROLS
// ---------------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////////

// Select2 controls
//////////////////////////////////////////////////////////////////////////////

// FUNCTION: Get selected value(s) of Select2 control
// pass arg: source-element
// return: value(s) comma-separated
// ---------------------------------------------------------------------------
function getValues(srcElm) {
    var res;
    var obj = srcElm.select2('data');    // get delected data array

    if (obj.length === 0)    return '';  // if empty list then nothig is selected
    else {
        var i = 0;
        obj.forEach(function(elm){   // loop through object elements array

            if (i)  res += ',' + elm.id; // if not first item, prepend ',' befor value(id)
            else    res = elm.id;        // else set (first) checked value

            i++;
        });
    }
    return res;
}

// patch select2-multiple widths
// ---------------------------------------------------------------------------
$('.select-field').select2({ width: '100%' });

// init select2 fields
// (embed from php)
// ---------------------------------------------------------------------------
<?=$form['init_js']?>


// 04. FORM-LOGIC
// ---------------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////////

// FUNCTION: Report empty fields
// ---------------------------------------------------------------------------
function reportEmpty() {
    var empty = '';
    var needit = [];
    var values = {};

    // embed if-conditions from php
    <?=$form['values_js']?>

    $("#empty-fields").html(empty);   // render empty fields

    // console.log(JSON.stringify(values));

    return { need : needit, data: values };    // return all fields
}
reportEmpty();

$('form').on('keyup change paste', 'input, select, textarea', function(){
    console.log('Form changed!');
    reportEmpty();
});

// validation?
// check: https://www.sitepoint.com/instant-validation/


$('#post-form').on('submit', function(e){
    e.preventDefault();
    if (!waitingResponse) {
        waitingResponse = true;
        var resp = reportEmpty();

        if (resp.need.length > 0) alert("Tα πεδία:\n\n" + resp.need.join(',\n') + "\n\nείναι υποχρεωτικά!");
        else {
        // alert('sending: '+JSON.stringify(resp.data));
        var postUrl = "/ajax.php?do=additem";   // url to post form
        var data2send = resp.data;              // parse data to send

        $.ajax({
            type: 'POST',
            url: postUrl,    // make sure you respect the same origin policy with this url
            data: data2send,
            dataType: "text",

            success: function(data) {  // server replies
                var json = $.parseJSON(data);
                if (json.success == true) {   // good! post accepted
                    // alert(json.id)
                    window.location.href = '/ui-show-item.php?id='+ json.id;
                }
                else {
                    alert("Post not accepted.\nPlease check the data you submit.");
                    // $('#oc-company-form')[0].reset();
                }
            },

            error: function() {
            alert('Some ajax error! :(');
            }
        });
        }

        waitingResponse = false;
    }

});

</script>



      </body>
</html>