Projekt

Obecné

Profil

Stáhnout (9.51 KB) Statistiky
| Větev: | Revize:
1 2111fb8f cagy
let ROW = 'row';
2
let COLUMN = 'column';
3
let VALUE = 'value';
4
5 b3c57a7c cagy
$(document).ready(function() {
6
7 d149244c cagy
    // reaction for add/delete parameters from selected fields (row, column, value)
8 b3c57a7c cagy
    $('.select-action-button').on('click', function () {
9 d149244c cagy
        // if input has headlight class (parameter is already added -> remove)
10 b3c57a7c cagy
        if ($(this).hasClass('select-action-headlight'))
11
        {
12 d149244c cagy
            // remove headlight class from type
13 b3c57a7c cagy
            $(this).removeClass('select-action-headlight');
14 d149244c cagy
15
            // remove parameter from selected inputs
16 b3c57a7c cagy
            removeParameter(getParameterTitle($(this)), getParameterType($(this)));
17
        }
18
        else
19
        {
20 d149244c cagy
            // remove headlight from other types for parameter if exists
21 b3c57a7c cagy
            $(this).closest('.select-action-buttons').find('.select-action-button').removeClass('select-action-headlight');
22
23 d149244c cagy
            // remove element from selected inputs if exists
24 b3c57a7c cagy
            removeFromAll(getParameterTitle($(this)));
25
26 d149244c cagy
            // headlight selected input
27 b3c57a7c cagy
            $(this).addClass('select-action-headlight');
28
29 d149244c cagy
            // add selected parameter to selected parameters by type
30 b3c57a7c cagy
            addParameter(getParameterIndex($(this)),
31
                getParameterTitle($(this)),
32
                getParameterType($(this)),
33
                getParameterHiddenValue($(this)));
34
35 d149244c cagy
            // reinitialize actions for new parameter
36 b3c57a7c cagy
            initParameters();
37
        }
38 2111fb8f cagy
39
        validateCombination();
40 b3c57a7c cagy
    });
41
42 d149244c cagy
    // init actions
43 b3c57a7c cagy
    initParameters();
44
45 2111fb8f cagy
    // spinner init
46 0486d642 cagy
    initSpinnerToButtons();
47
48 2111fb8f cagy
    // generation init
49
    initTableGenerationButtons();
50
51 d9b0bac7 cagy
    // sort selected parameters
52
    initSelectedParameters();
53
54 b3c57a7c cagy
});
55
56 d149244c cagy
/**
57
 * Remove value from all selected fields
58
 * @param title
59
 */
60 b3c57a7c cagy
function removeFromAll(title) {
61
    $rows = $('.selected-input-container');
62
63
    $rows.each(function () {
64
        $(this).find('.parameter-name').each(function () {
65
            if ($(this).text() == title)
66
            {
67
                $(this).closest('.parameter').remove();
68
            }
69
        })
70
    });
71
}
72
73 d149244c cagy
/**
74
 * Returns parameter index
75
 * @param button
76
 * @returns {*|jQuery|string|undefined}
77
 */
78 b3c57a7c cagy
function getParameterIndex(button) {
79
    return $(button).closest('.parameter-row').find('.parameterIndex').val();
80
}
81
82 d149244c cagy
/**
83
 * Adds new parameter to selected fields
84
 * @param index
85
 * @param parameterName
86
 * @param type
87
 * @param hiddenValue
88
 */
89 b3c57a7c cagy
function addParameter(index, parameterName, type, hiddenValue) {
90
    var row = document.createElement('tr');
91
    row.classList.add(type + '-parameter', 'parameter');
92
93
    let hiddenInput = document.createElement('input');
94
    hiddenInput.type = 'hidden';
95
    hiddenInput.id = `parametersInConfiguration${index}.location.id`;
96
    hiddenInput.name = `parametersInConfiguration[${index}].location.id`;
97 36a626d9 cagy
    hiddenInput.setAttribute('value', hiddenValue);
98
99
    let hiddenInputOrder = document.createElement('input');
100
    hiddenInputOrder.type = 'hidden';
101
    hiddenInputOrder.id = `parametersInConfiguration${index}.parameterOrder`;
102
    hiddenInputOrder.name = `parametersInConfiguration[${index}].parameterOrder`;
103
    hiddenInputOrder.classList.add('parametr-order');
104 b3c57a7c cagy
105
    row.appendChild(hiddenInput);
106 36a626d9 cagy
    row.appendChild(hiddenInputOrder);
107 b3c57a7c cagy
108
    var firstTd = document.createElement('td');
109
110
    var firstTdBody = document.createElement('span');
111
    firstTdBody.classList.add('parameter-name');
112
    firstTdBody.innerHTML = parameterName;
113
114
    firstTd.appendChild(firstTdBody);
115
116
    row.appendChild(firstTd);
117
118
    var thirdTd = document.createElement('td');
119
120
    var thirdTdBody;
121
122
123 2111fb8f cagy
    if (type == VALUE)
124 b3c57a7c cagy
    {
125
        thirdTdBody = $('#function-select-' + index).clone()[0];
126 2f725e7f cagy
        thirdTdBody.selectedIndex = 0;
127 b3c57a7c cagy
128
        $(thirdTdBody).removeClass('hidden');
129
    }
130
    else
131
    {
132
        var secondTd = document.createElement('td');
133
134
        var secondTdBody = document.createElement('input');
135
        secondTdBody.type = 'text';
136
        secondTdBody.classList.add('form-control', 'select-filter-input-name');
137
        secondTdBody.name = `parametersInConfiguration[${index}].columnName`;
138
139
        secondTd.appendChild(secondTdBody);
140
141
        row.appendChild(secondTd);
142
143
        thirdTdBody = document.createElement('span');
144
        thirdTdBody.innerHTML = "<i class=\"fas fa-sort\"></i>";
145
    }
146
147
    thirdTd.appendChild(thirdTdBody);
148
149
    row.appendChild(thirdTd);
150
151
    $('#' + type + '-wrapper').append($(row));
152
153 36a626d9 cagy
    reindexOrder();
154
155 b3c57a7c cagy
}
156
157 d149244c cagy
/**
158
 * Remove specific parameter from fields by name and container
159
 * @param title
160
 * @param parameterClass
161
 */
162 b3c57a7c cagy
function removeParameter(title, parameterClass) {
163
    $rows = $('.' + parameterClass + '-parameter');
164
165
    $rows.each(function () {
166
        $(this).find('.parameter-name').each(function () {
167
            if ($(this).text() == title)
168
            {
169
                $(this).closest('.' + parameterClass + '-parameter').remove();
170
            }
171
        })
172
    });
173
}
174
175 d149244c cagy
/**
176
 * Base on class returns type
177
 * @param context
178
 * @returns {string}
179
 */
180 b3c57a7c cagy
function getParameterType(context) {
181
    if ($(context).hasClass('column-button'))
182
    {
183 2111fb8f cagy
        return COLUMN;
184 b3c57a7c cagy
    }
185
    else if ($(context).hasClass('row-button'))
186
    {
187 2111fb8f cagy
        return ROW;
188 b3c57a7c cagy
    }
189
    else if ($(context).hasClass('value-button'))
190
    {
191 2111fb8f cagy
        return VALUE;
192 b3c57a7c cagy
    }
193
}
194
195 d149244c cagy
/**
196
 * Base on type return integer value
197
 * @param context
198
 * @returns {number}
199
 */
200 b3c57a7c cagy
function getParameterHiddenValue(context) {
201
    if ($(context).hasClass('column-button'))
202
    {
203
        return 1;
204
    }
205
    else if ($(context).hasClass('row-button'))
206
    {
207
        return 2;
208
    }
209
    else if ($(context).hasClass('value-button'))
210
    {
211
        return 3;
212
    }
213
}
214
215 d149244c cagy
/**
216
 * Returns parameter name
217
 * @param context
218
 * @returns {*|jQuery}
219
 */
220 b3c57a7c cagy
function getParameterTitle(context) {
221
    return $(context).closest('.parameter-row').find('.parameter-name').text();
222
}
223
224 d149244c cagy
/**
225
 * Init base actions
226
 */
227 b3c57a7c cagy
function initParameters()
228
{
229 36a626d9 cagy
    $('.sortable').sortable({
230
       stop: function () {
231
           reindexOrder();
232
       }
233
    });
234 b3c57a7c cagy
    $("select").selectpicker('refresh');
235 36a626d9 cagy
}
236
237
/**
238
 * Update parametr order
239
 */
240
function reindexOrder() {
241
    var rowValues = $('.row-parameter');
242
    var columnValues = $('.column-parameter');
243
    var valueValues = $('.value-parameter');
244
245
    var index = 0;
246
247
    rowValues.each(function () {
248
        $(this).find('.parametr-order').attr('value', index);
249
        index++;
250
    });
251
252
    columnValues.each(function () {
253
        $(this).find('.parametr-order').attr('value', index);
254
        index++;
255
    });
256
257
    valueValues.each(function () {
258
        $(this).find('.parametr-order').attr('value', index);
259
        index++;
260
    });
261 10920ea3 Vojtěch Danišík
}
262 36a626d9 cagy
263 10920ea3 Vojtěch Danišík
function checkIfConfigurationNameIsNull() {
264
    var configurationName = document.getElementById('configurationName');
265 36a626d9 cagy
266 10920ea3 Vojtěch Danišík
    if (configurationName.value == null || configurationName.value == "") {
267
        alert("Není vyplněn název šablony !");
268
    }
269 0486d642 cagy
}
270
271
/**
272
 * Set up listener to export buttons to show spinner
273
 */
274
function initSpinnerToButtons() {
275
    hideSpinner();
276
277
    document.querySelectorAll('.show-spinner').forEach(button => {
278
        button.addEventListener('click', showSpinner);
279
    })
280
}
281
282 2111fb8f cagy
/**
283
 * Shows spinner with overlay
284
 */
285 0486d642 cagy
function showSpinner() {
286
    let spinner = document.getElementById('spinner-overlay');
287
288
    spinner.style.display = 'block';
289
}
290
291 2111fb8f cagy
/**
292
 * Hides spinner with overlay
293
 */
294 0486d642 cagy
function hideSpinner() {
295
    let spinner = document.getElementById('spinner-overlay');
296
297
    spinner.style.display = 'none';
298 2111fb8f cagy
}
299
300
/**
301
 * Table generation button validator
302
 * If row is selected allow to generate
303
 * if column is selected, value and row must be also selected
304
 */
305
function initTableGenerationButtons() {
306
    validateCombination();
307
}
308
309
/**
310
 * Check action buttons combination
311
 */
312
function validateCombination() {
313
    let selectedActionButtons = $('.select-action-headlight');
314
315
    var rowSelected = false;
316
    var columnSelected = false;
317
    var valueSelected = false;
318
319
    // check selected buttons
320
    selectedActionButtons.each(function () {
321
        let type = getParameterType($(this));
322
323
        switch (type) {
324
            case VALUE:
325
                valueSelected = true;
326
                break;
327
            case ROW:
328
                rowSelected = true;
329
                break;
330
            case COLUMN:
331
                columnSelected = true;
332
                break;
333
        }
334
    });
335
336
    // possible combinations with allowed export
337
    if (rowSelected && columnSelected && valueSelected)
338
    {
339
        allowExport();
340
    }
341
    else if (columnSelected && !rowSelected && !valueSelected)
342
    {
343
        allowExport();
344
    }
345
    else
346
    {
347
        disableExportButtons();
348
    }
349
350
351
}
352
353
/**
354
 * Disables buttons for export
355
 */
356
function disableExportButtons() {
357
    $('.show-spinner').attr('disabled','disabled');
358
}
359
360
/**
361
 * Allow buttons for export
362
 */
363
function allowExport() {
364
    $('.show-spinner').removeAttr('disabled');
365 d9b0bac7 cagy
}
366
367
/**
368
 * Sort selected parameters by order
369
 */
370
function initSelectedParameters() {
371
372
    sortSelectedParameters('.column-parameter', '#column-wrapper');
373
    sortSelectedParameters('.row-parameter', '#row-wrapper');
374
375
}
376
377
/**
378
 * Checks and sort specific parameters
379
 * @param parameterName
380
 * @param parameterWrapper
381
 */
382
function sortSelectedParameters(parameterName, parameterWrapper) {
383
384
    var tmpArray = [];
385
386
    var parameters = $(parameterName);
387
388
    // load specific parameters to array orderIndex => parameter
389
    parameters.each(function () {
390
        var orderIndex = $(this).find('.parametr-order').val();
391
392
        tmpArray[orderIndex] = $(this);
393
394
    });
395
396
    // sort parameters by key (order index)
397
    tmpArray.sort((a, b) => a - b);
398
399
    let wrapper = $(parameterWrapper);
400
401
    // clear wrapper
402
    wrapper.empty();
403
404
    // append sorted elements
405
    wrapper.append(tmpArray);
406 b3c57a7c cagy
}