Projekt

Obecné

Profil

Stáhnout (9.51 KB) Statistiky
| Větev: | Revize:
1
let ROW = 'row';
2
let COLUMN = 'column';
3
let VALUE = 'value';
4

    
5
$(document).ready(function() {
6

    
7
    // reaction for add/delete parameters from selected fields (row, column, value)
8
    $('.select-action-button').on('click', function () {
9
        // if input has headlight class (parameter is already added -> remove)
10
        if ($(this).hasClass('select-action-headlight'))
11
        {
12
            // remove headlight class from type
13
            $(this).removeClass('select-action-headlight');
14

    
15
            // remove parameter from selected inputs
16
            removeParameter(getParameterTitle($(this)), getParameterType($(this)));
17
        }
18
        else
19
        {
20
            // remove headlight from other types for parameter if exists
21
            $(this).closest('.select-action-buttons').find('.select-action-button').removeClass('select-action-headlight');
22

    
23
            // remove element from selected inputs if exists
24
            removeFromAll(getParameterTitle($(this)));
25

    
26
            // headlight selected input
27
            $(this).addClass('select-action-headlight');
28

    
29
            // add selected parameter to selected parameters by type
30
            addParameter(getParameterIndex($(this)),
31
                getParameterTitle($(this)),
32
                getParameterType($(this)),
33
                getParameterHiddenValue($(this)));
34

    
35
            // reinitialize actions for new parameter
36
            initParameters();
37
        }
38

    
39
        validateCombination();
40
    });
41

    
42
    // init actions
43
    initParameters();
44

    
45
    // spinner init
46
    initSpinnerToButtons();
47

    
48
    // generation init
49
    initTableGenerationButtons();
50

    
51
    // sort selected parameters
52
    initSelectedParameters();
53

    
54
});
55

    
56
/**
57
 * Remove value from all selected fields
58
 * @param title
59
 */
60
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
/**
74
 * Returns parameter index
75
 * @param button
76
 * @returns {*|jQuery|string|undefined}
77
 */
78
function getParameterIndex(button) {
79
    return $(button).closest('.parameter-row').find('.parameterIndex').val();
80
}
81

    
82
/**
83
 * Adds new parameter to selected fields
84
 * @param index
85
 * @param parameterName
86
 * @param type
87
 * @param hiddenValue
88
 */
89
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
    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

    
105
    row.appendChild(hiddenInput);
106
    row.appendChild(hiddenInputOrder);
107

    
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
    if (type == VALUE)
124
    {
125
        thirdTdBody = $('#function-select-' + index).clone()[0];
126
        thirdTdBody.selectedIndex = 0;
127

    
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
    reindexOrder();
154

    
155
}
156

    
157
/**
158
 * Remove specific parameter from fields by name and container
159
 * @param title
160
 * @param parameterClass
161
 */
162
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
/**
176
 * Base on class returns type
177
 * @param context
178
 * @returns {string}
179
 */
180
function getParameterType(context) {
181
    if ($(context).hasClass('column-button'))
182
    {
183
        return COLUMN;
184
    }
185
    else if ($(context).hasClass('row-button'))
186
    {
187
        return ROW;
188
    }
189
    else if ($(context).hasClass('value-button'))
190
    {
191
        return VALUE;
192
    }
193
}
194

    
195
/**
196
 * Base on type return integer value
197
 * @param context
198
 * @returns {number}
199
 */
200
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
/**
216
 * Returns parameter name
217
 * @param context
218
 * @returns {*|jQuery}
219
 */
220
function getParameterTitle(context) {
221
    return $(context).closest('.parameter-row').find('.parameter-name').text();
222
}
223

    
224
/**
225
 * Init base actions
226
 */
227
function initParameters()
228
{
229
    $('.sortable').sortable({
230
       stop: function () {
231
           reindexOrder();
232
       }
233
    });
234
    $("select").selectpicker('refresh');
235
}
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
}
262

    
263
function checkIfConfigurationNameIsNull() {
264
    var configurationName = document.getElementById('configurationName');
265

    
266
    if (configurationName.value == null || configurationName.value == "") {
267
        alert("Není vyplněn název šablony !");
268
    }
269
}
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
/**
283
 * Shows spinner with overlay
284
 */
285
function showSpinner() {
286
    let spinner = document.getElementById('spinner-overlay');
287

    
288
    spinner.style.display = 'block';
289
}
290

    
291
/**
292
 * Hides spinner with overlay
293
 */
294
function hideSpinner() {
295
    let spinner = document.getElementById('spinner-overlay');
296

    
297
    spinner.style.display = 'none';
298
}
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
}
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
}
(2-2/16)