Projekt

Obecné

Profil

Stáhnout (6.42 KB) Statistiky
| Větev: | Revize:
1
$(document).ready(function() {
2

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

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

    
19
            // remove element from selected inputs if exists
20
            removeFromAll(getParameterTitle($(this)));
21

    
22
            // headlight selected input
23
            $(this).addClass('select-action-headlight');
24

    
25
            // add selected parameter to selected parameters by type
26
            addParameter(getParameterIndex($(this)),
27
                getParameterTitle($(this)),
28
                getParameterType($(this)),
29
                getParameterHiddenValue($(this)));
30

    
31
            // reinitialize actions for new parameter
32
            initParameters();
33
        }
34
    });
35

    
36
    // init actions
37
    initParameters();
38

    
39
});
40

    
41
/**
42
 * Remove value from all selected fields
43
 * @param title
44
 */
45
function removeFromAll(title) {
46
    $rows = $('.selected-input-container');
47

    
48
    $rows.each(function () {
49
        $(this).find('.parameter-name').each(function () {
50
            if ($(this).text() == title)
51
            {
52
                $(this).closest('.parameter').remove();
53
            }
54
        })
55
    });
56
}
57

    
58
/**
59
 * Returns parameter index
60
 * @param button
61
 * @returns {*|jQuery|string|undefined}
62
 */
63
function getParameterIndex(button) {
64
    return $(button).closest('.parameter-row').find('.parameterIndex').val();
65
}
66

    
67
/**
68
 * Adds new parameter to selected fields
69
 * @param index
70
 * @param parameterName
71
 * @param type
72
 * @param hiddenValue
73
 */
74
function addParameter(index, parameterName, type, hiddenValue) {
75
    var row = document.createElement('tr');
76
    row.classList.add(type + '-parameter', 'parameter');
77

    
78
    let hiddenInput = document.createElement('input');
79
    hiddenInput.type = 'hidden';
80
    hiddenInput.id = `parametersInConfiguration${index}.location.id`;
81
    hiddenInput.name = `parametersInConfiguration[${index}].location.id`;
82
    hiddenInput.setAttribute('value', hiddenValue);
83

    
84
    let hiddenInputOrder = document.createElement('input');
85
    hiddenInputOrder.type = 'hidden';
86
    hiddenInputOrder.id = `parametersInConfiguration${index}.parameterOrder`;
87
    hiddenInputOrder.name = `parametersInConfiguration[${index}].parameterOrder`;
88
    hiddenInputOrder.classList.add('parametr-order');
89

    
90
    row.appendChild(hiddenInput);
91
    row.appendChild(hiddenInputOrder);
92

    
93
    var firstTd = document.createElement('td');
94

    
95
    var firstTdBody = document.createElement('span');
96
    firstTdBody.classList.add('parameter-name');
97
    firstTdBody.innerHTML = parameterName;
98

    
99
    firstTd.appendChild(firstTdBody);
100

    
101
    row.appendChild(firstTd);
102

    
103
    var thirdTd = document.createElement('td');
104

    
105
    var thirdTdBody;
106

    
107

    
108
    if (type == 'value')
109
    {
110
        thirdTdBody = $('#function-select-' + index).clone()[0];
111

    
112
        $(thirdTdBody).removeClass('hidden');
113
    }
114
    else
115
    {
116
        var secondTd = document.createElement('td');
117

    
118
        var secondTdBody = document.createElement('input');
119
        secondTdBody.type = 'text';
120
        secondTdBody.classList.add('form-control', 'select-filter-input-name');
121
        secondTdBody.name = `parametersInConfiguration[${index}].columnName`;
122

    
123
        secondTd.appendChild(secondTdBody);
124

    
125
        row.appendChild(secondTd);
126

    
127
        thirdTdBody = document.createElement('span');
128
        thirdTdBody.innerHTML = "<i class=\"fas fa-sort\"></i>";
129
    }
130

    
131
    thirdTd.appendChild(thirdTdBody);
132

    
133
    row.appendChild(thirdTd);
134

    
135
    $('#' + type + '-wrapper').append($(row));
136

    
137
    reindexOrder();
138

    
139
}
140

    
141
/**
142
 * Remove specific parameter from fields by name and container
143
 * @param title
144
 * @param parameterClass
145
 */
146
function removeParameter(title, parameterClass) {
147
    $rows = $('.' + parameterClass + '-parameter');
148

    
149
    $rows.each(function () {
150
        $(this).find('.parameter-name').each(function () {
151
            if ($(this).text() == title)
152
            {
153
                $(this).closest('.' + parameterClass + '-parameter').remove();
154
            }
155
        })
156
    });
157
}
158

    
159
/**
160
 * Base on class returns type
161
 * @param context
162
 * @returns {string}
163
 */
164
function getParameterType(context) {
165
    if ($(context).hasClass('column-button'))
166
    {
167
        return 'column';
168
    }
169
    else if ($(context).hasClass('row-button'))
170
    {
171
        return 'row';
172
    }
173
    else if ($(context).hasClass('value-button'))
174
    {
175
        return 'value';
176
    }
177
}
178

    
179
/**
180
 * Base on type return integer value
181
 * @param context
182
 * @returns {number}
183
 */
184
function getParameterHiddenValue(context) {
185
    if ($(context).hasClass('column-button'))
186
    {
187
        return 1;
188
    }
189
    else if ($(context).hasClass('row-button'))
190
    {
191
        return 2;
192
    }
193
    else if ($(context).hasClass('value-button'))
194
    {
195
        return 3;
196
    }
197
}
198

    
199
/**
200
 * Returns parameter name
201
 * @param context
202
 * @returns {*|jQuery}
203
 */
204
function getParameterTitle(context) {
205
    return $(context).closest('.parameter-row').find('.parameter-name').text();
206
}
207

    
208
/**
209
 * Init base actions
210
 */
211
function initParameters()
212
{
213
    $('.sortable').sortable({
214
       stop: function () {
215
           reindexOrder();
216
       }
217
    });
218
    $("select").selectpicker('refresh');
219
}
220

    
221
/**
222
 * Update parametr order
223
 */
224
function reindexOrder() {
225
    var rowValues = $('.row-parameter');
226
    var columnValues = $('.column-parameter');
227
    var valueValues = $('.value-parameter');
228

    
229
    var index = 0;
230

    
231
    rowValues.each(function () {
232
        $(this).find('.parametr-order').attr('value', index);
233
        index++;
234
    });
235

    
236
    columnValues.each(function () {
237
        $(this).find('.parametr-order').attr('value', index);
238
        index++;
239
    });
240

    
241
    valueValues.each(function () {
242
        $(this).find('.parametr-order').attr('value', index);
243
        index++;
244
    });
245
}
246

    
247
function checkIfConfigurationNameIsNull() {
248
    var configurationName = document.getElementById('configurationName');
249

    
250
    if (configurationName.value == null || configurationName.value == "") {
251
        alert("Není vyplněn název šablony !");
252
    }
253
}
(2-2/16)