Projekt

Obecné

Profil

Stáhnout (6.46 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
        thirdTdBody.selectedIndex = 0;
112

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

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

    
124
        secondTd.appendChild(secondTdBody);
125

    
126
        row.appendChild(secondTd);
127

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

    
132
    thirdTd.appendChild(thirdTdBody);
133

    
134
    row.appendChild(thirdTd);
135

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

    
138
    reindexOrder();
139

    
140
}
141

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

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

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

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

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

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

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

    
230
    var index = 0;
231

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

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

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

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

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