visual_condition_builder 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +155 -0
  4. data/Rakefile +34 -0
  5. data/app/assets/javascripts/visual_condition_builder/Sortable.js +1385 -0
  6. data/app/assets/javascripts/visual_condition_builder/autoNumeric-2.0-BETA.js +2156 -0
  7. data/app/assets/javascripts/visual_condition_builder/autonumeric_ujs.js +94 -0
  8. data/app/assets/javascripts/visual_condition_builder/condition_builder.js +633 -0
  9. data/app/assets/javascripts/visual_condition_builder/diacritics.js +116 -0
  10. data/app/assets/javascripts/visual_condition_builder/select2.full.js +6436 -0
  11. data/app/assets/javascripts/visual_condition_builder/select2_i18n/ar.js +3 -0
  12. data/app/assets/javascripts/visual_condition_builder/select2_i18n/az.js +3 -0
  13. data/app/assets/javascripts/visual_condition_builder/select2_i18n/bg.js +3 -0
  14. data/app/assets/javascripts/visual_condition_builder/select2_i18n/ca.js +3 -0
  15. data/app/assets/javascripts/visual_condition_builder/select2_i18n/cs.js +3 -0
  16. data/app/assets/javascripts/visual_condition_builder/select2_i18n/da.js +3 -0
  17. data/app/assets/javascripts/visual_condition_builder/select2_i18n/de.js +3 -0
  18. data/app/assets/javascripts/visual_condition_builder/select2_i18n/el.js +3 -0
  19. data/app/assets/javascripts/visual_condition_builder/select2_i18n/en.js +3 -0
  20. data/app/assets/javascripts/visual_condition_builder/select2_i18n/es.js +3 -0
  21. data/app/assets/javascripts/visual_condition_builder/select2_i18n/et.js +3 -0
  22. data/app/assets/javascripts/visual_condition_builder/select2_i18n/eu.js +3 -0
  23. data/app/assets/javascripts/visual_condition_builder/select2_i18n/fa.js +3 -0
  24. data/app/assets/javascripts/visual_condition_builder/select2_i18n/fi.js +3 -0
  25. data/app/assets/javascripts/visual_condition_builder/select2_i18n/fr.js +3 -0
  26. data/app/assets/javascripts/visual_condition_builder/select2_i18n/gl.js +3 -0
  27. data/app/assets/javascripts/visual_condition_builder/select2_i18n/he.js +3 -0
  28. data/app/assets/javascripts/visual_condition_builder/select2_i18n/hi.js +3 -0
  29. data/app/assets/javascripts/visual_condition_builder/select2_i18n/hr.js +3 -0
  30. data/app/assets/javascripts/visual_condition_builder/select2_i18n/hu.js +3 -0
  31. data/app/assets/javascripts/visual_condition_builder/select2_i18n/id.js +3 -0
  32. data/app/assets/javascripts/visual_condition_builder/select2_i18n/is.js +3 -0
  33. data/app/assets/javascripts/visual_condition_builder/select2_i18n/it.js +3 -0
  34. data/app/assets/javascripts/visual_condition_builder/select2_i18n/ja.js +3 -0
  35. data/app/assets/javascripts/visual_condition_builder/select2_i18n/km.js +3 -0
  36. data/app/assets/javascripts/visual_condition_builder/select2_i18n/ko.js +3 -0
  37. data/app/assets/javascripts/visual_condition_builder/select2_i18n/lt.js +3 -0
  38. data/app/assets/javascripts/visual_condition_builder/select2_i18n/lv.js +3 -0
  39. data/app/assets/javascripts/visual_condition_builder/select2_i18n/mk.js +3 -0
  40. data/app/assets/javascripts/visual_condition_builder/select2_i18n/ms.js +3 -0
  41. data/app/assets/javascripts/visual_condition_builder/select2_i18n/nb.js +3 -0
  42. data/app/assets/javascripts/visual_condition_builder/select2_i18n/nl.js +3 -0
  43. data/app/assets/javascripts/visual_condition_builder/select2_i18n/pl.js +3 -0
  44. data/app/assets/javascripts/visual_condition_builder/select2_i18n/pt-BR.js +3 -0
  45. data/app/assets/javascripts/visual_condition_builder/select2_i18n/pt.js +3 -0
  46. data/app/assets/javascripts/visual_condition_builder/select2_i18n/ro.js +3 -0
  47. data/app/assets/javascripts/visual_condition_builder/select2_i18n/ru.js +3 -0
  48. data/app/assets/javascripts/visual_condition_builder/select2_i18n/sk.js +3 -0
  49. data/app/assets/javascripts/visual_condition_builder/select2_i18n/sr-Cyrl.js +3 -0
  50. data/app/assets/javascripts/visual_condition_builder/select2_i18n/sr.js +3 -0
  51. data/app/assets/javascripts/visual_condition_builder/select2_i18n/sv.js +3 -0
  52. data/app/assets/javascripts/visual_condition_builder/select2_i18n/th.js +3 -0
  53. data/app/assets/javascripts/visual_condition_builder/select2_i18n/tr.js +3 -0
  54. data/app/assets/javascripts/visual_condition_builder/select2_i18n/uk.js +3 -0
  55. data/app/assets/javascripts/visual_condition_builder/select2_i18n/vi.js +3 -0
  56. data/app/assets/javascripts/visual_condition_builder/select2_i18n/zh-CN.js +3 -0
  57. data/app/assets/javascripts/visual_condition_builder/select2_i18n/zh-TW.js +3 -0
  58. data/app/assets/javascripts/visual_condition_builder.js +6 -0
  59. data/app/assets/stylesheets/visual_condition_builder/condition_builder.css +87 -0
  60. data/app/assets/stylesheets/visual_condition_builder/select2-bootstrap.css +722 -0
  61. data/app/assets/stylesheets/visual_condition_builder/select2.css +484 -0
  62. data/app/assets/stylesheets/visual_condition_builder.css +5 -0
  63. data/app/controllers/application_widget.rb +56 -0
  64. data/app/controllers/visual_condition_builder/application_controller.rb +5 -0
  65. data/app/controllers/visual_condition_builder/widgets_controller.rb +45 -0
  66. data/app/helpers/visual_condition_builder/application_helper.rb +74 -0
  67. data/app/models/visual_condition_builder/user.rb +10 -0
  68. data/app/views/visual_condition_builder/widgets/_widgets_list.html.erb +24 -0
  69. data/app/views/visual_condition_builder/widgets/index.html.erb +25 -0
  70. data/config/initializers/assets.rb +1 -0
  71. data/config/initializers/visual_condition_builder.rb +7 -0
  72. data/config/routes.rb +19 -0
  73. data/lib/generators/templates/create_taxweb_widgets_users.rb +18 -0
  74. data/lib/generators/templates/generic_widget.erb +12 -0
  75. data/lib/generators/templates/generic_widget.html.erb +2 -0
  76. data/lib/generators/visual_condition_builder/install_generator.rb +26 -0
  77. data/lib/generators/visual_condition_builder/view_generator.rb +11 -0
  78. data/lib/generators/visual_condition_builder/widget_generator.rb +25 -0
  79. data/lib/visual_condition_builder/converter.rb +22 -0
  80. data/lib/visual_condition_builder/dictionary.rb +166 -0
  81. data/lib/visual_condition_builder/engine.rb +6 -0
  82. data/lib/visual_condition_builder/helper.rb +19 -0
  83. data/lib/visual_condition_builder/version.rb +3 -0
  84. data/lib/visual_condition_builder.rb +9 -0
  85. metadata +127 -0
@@ -0,0 +1,94 @@
1
+ /**
2
+ * autonumeric_ujs.js
3
+ * @author: randoum
4
+ * @version: 1.9.43 - 2015-12-26
5
+ *
6
+ * Created by Randoum on 2013-08-15. Please report any bugs to https://github.com/randoum/autonumeric-rails
7
+ *
8
+ * Wrap-up autoNumeric.js library to be used with Rails in a UJS flavor
9
+ * All credits for autoNumeric library goes to its original creators
10
+ * Whom can be reached at https://github.com/BobKnothe/autoNumeric
11
+ *
12
+ * The MIT License (http://www.opensource.org/licenses/mit-license.php)
13
+ *
14
+ * Permission is hereby granted, free of charge, to any person
15
+ * obtaining a copy of this software and associated documentation
16
+ * files (the "Software"), to deal in the Software without
17
+ * restriction, including without limitation the rights to use,
18
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
19
+ * copies of the Software, and to permit persons to whom the
20
+ * Software is furnished to do so, subject to the following
21
+ * conditions:
22
+ *
23
+ * The above copyright notice and this permission notice shall be
24
+ * included in all copies or substantial portions of the Software.
25
+ *
26
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
28
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
30
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
31
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33
+ * OTHER DEALINGS IN THE SOFTWARE.
34
+ */
35
+
36
+ var AutonumericRails;
37
+
38
+ window.AutonumericRails = AutonumericRails = (function() {
39
+ AutonumericRails.create_autonumeric_object = function(obj) {
40
+ if (!obj.data('autonumeric-initialized')) {
41
+ return new this(obj);
42
+ }
43
+ };
44
+
45
+ AutonumericRails.delete_autonumeric_object = function(obj) {
46
+ if (obj.data('autonumeric-initialized')) {
47
+ obj.removeData('autonumeric-initialized').removeData('autonumeric').removeAttr('data-autonumeric').off('keyup blur').autoNumeric('destroy');
48
+ $('input#' + obj.attr('id') + '_val[type="hidden"][name="' + obj.attr('name') + '"]').remove();
49
+ }
50
+ };
51
+
52
+ function AutonumericRails(field) {
53
+ this.field = field;
54
+ this.field.data('autonumeric-initialized', true);
55
+ this.create_hidden_field();
56
+ this.init_autonumeric();
57
+ this.sanitize_value();
58
+ this.field.on('keyup blur', $.proxy(function() {
59
+ this.sanitize_value();
60
+ }, this));
61
+ }
62
+
63
+ AutonumericRails.prototype.create_hidden_field = function() {
64
+ this.hidden = $('<input>').attr('type', 'hidden').attr('id', this.field.attr('id') + '_val').attr('name', this.field.attr('name'));
65
+ this.hidden.insertAfter(this.field);
66
+ };
67
+
68
+ AutonumericRails.prototype.init_autonumeric = function() {
69
+ this.field.autoNumeric('init', $.parseJSON(this.field.attr('data-autonumeric')));
70
+ };
71
+
72
+ AutonumericRails.prototype.sanitize_value = function() {
73
+ this.hidden.val(this.field.autoNumeric('get'));
74
+ };
75
+
76
+ return AutonumericRails;
77
+
78
+ })();
79
+
80
+ window.refresh_autonumeric = function() {
81
+ $('input[data-autonumeric]').each(function() {
82
+ AutonumericRails.create_autonumeric_object($(this));
83
+ });
84
+ };
85
+
86
+ jQuery(function() {
87
+ window.refresh_autonumeric();
88
+ $(document).on('refresh_autonumeric', function() {
89
+ window.refresh_autonumeric();
90
+ });
91
+ $(document).on('ajaxComplete', function() {
92
+ window.refresh_autonumeric();
93
+ });
94
+ });
@@ -0,0 +1,633 @@
1
+ (function ($) {
2
+
3
+ $.conditionBuilder = function (element, options) {
4
+ var defaults = {
5
+ placeholder: {
6
+ operators: 'Select a operator...',
7
+ values: 'Enter a value...'
8
+ },
9
+ dictionary: [],
10
+ values: [],
11
+ input: '',
12
+ debug: false,
13
+ numericConfig: {
14
+ aSep: '',
15
+ aDec: '.',
16
+ aSign: ''
17
+ },
18
+ select2Config: {
19
+ theme: "bootstrap",
20
+ width: 'auto',
21
+ placeholder: '',
22
+ allowClear: false,
23
+ dropdownAutoWidth: 'true',
24
+ tags: false
25
+ }
26
+ };
27
+
28
+ var plugin = this;
29
+ var $element = $(element), element = element;
30
+ var jsonCache = {};
31
+ plugin.parameters = {};
32
+ plugin.result = function (data) {
33
+ };
34
+
35
+ $element.addClass('condition-builder');
36
+
37
+ //~~~ PUBLIC
38
+ plugin.add_condition = function (field_name) {
39
+ var fieldObj = getFieldByName(field_name);
40
+ if (typeof fieldObj !== 'object') {
41
+ return;
42
+ }
43
+
44
+ //BLOCK ELEMENTS HTML
45
+ var block = $('<div class="group-conditions clearfix"></div>');
46
+ block.append('<span class="conditions-move"></span>');
47
+ var field_label = fieldObj.label;
48
+ if (!is_blank(fieldObj.group)) {
49
+ field_label = Object.values(fieldObj.group)[0] + ' : ' + field_label;
50
+ }
51
+ block.append('<input type="hidden" class="field form-control" value="' + fieldObj.field + '" data-type="' + fieldObj.type + '" />');
52
+ block.append('<span class="field_name label label-info">' + field_label + ' <a href="" class="remove-condition">&#10006;</a></span>');
53
+ block.append('<select class="operators hide form-control"></select>');
54
+ block.append('<span class="fixed_operator hide form-control"></span>');
55
+ block.append('<select class="values hide form-control"></select>');
56
+ block.append('<input class="fixed_value hide form-control" />');
57
+ if (plugin.parameters.debug == true) {
58
+ block.append('<p class="expression help-block"></p>');
59
+ }
60
+ $elField = block.find('.field');
61
+ $elOperators = block.find('.operators');
62
+ $elValues = block.find('.values, .fixed_value');
63
+
64
+ //EVENTS
65
+ block.find('.remove-condition').on('click', event_remove_condition);
66
+ $elOperators.on('change', event_load_values);
67
+
68
+ $element.append(block);
69
+ $element.trigger('change');
70
+
71
+ //LOAD OPERATORS
72
+ plugin.load_operators($elField);
73
+
74
+ return block;
75
+
76
+ }; //END add_condition
77
+
78
+ plugin.clear_rows = function () {
79
+ if (confirm('Essa ação removerá todos os itens. Deseja continuar?') == true) {
80
+ $element.find('.group-conditions').remove();
81
+ plugin.getResult();
82
+ }
83
+ };
84
+
85
+ plugin.load_operators = function (fieldEl) {
86
+ var $fieldElement = $(fieldEl);
87
+ var field_name = getFieldValue($fieldElement);
88
+ var field = getFieldByName(field_name);
89
+ var $groupConditions = $fieldElement.closest('.group-conditions');
90
+ var $operators = $groupConditions.find('.operators');
91
+ var $fixedOperator = $groupConditions.find('.fixed_operator');
92
+ var $values = $groupConditions.find('.values');
93
+ var $fixedValue = $groupConditions.find('.fixed_value');
94
+ var operators = field.operators;
95
+
96
+ remove_plugins_elements($groupConditions);
97
+ $operators.html('').addClass('hide');
98
+ $fixedOperator.val('').addClass('hide');
99
+ $values.html('').addClass('hide');
100
+ $fixedValue.val('').addClass('hide');
101
+
102
+ operators = normalize_operators(operators);
103
+ $.each(operators, function (op_i, op_el) {
104
+ var op_option = $('<option data-index="' + op_i + '" data-no-value="' + op_el.no_value + '" data-multiple="' + op_el.multiple + '" value="' + op_el.operator + '">' + getLabel(op_el) + '</option>');
105
+ $operators.append(op_option);
106
+ });
107
+ if (operators != undefined && operators.length == 1) {
108
+ $fixedOperator.html(getLabel(operators[0])).removeClass('hide');
109
+ } else {
110
+ $operators.removeClass('hide');
111
+ }
112
+ $operators.trigger('change');
113
+ };
114
+
115
+ plugin.fill_condition = function (groupConditions, data) {
116
+ if (data != undefined && data.length > 0 && groupConditions != undefined) {
117
+ $elOperators = getOperatorElement(groupConditions);
118
+ $elOperators.val(data[1]).trigger('change');
119
+
120
+ $elOperator = $elOperators.find('option:selected');
121
+ var multiple = $elOperator.attr('data-multiple');
122
+
123
+ $elValues = getValueElement(groupConditions);
124
+ if ($elValues.length > 1) {
125
+ var values = (typeof data[2] == 'string') ? [data[2]] : data[2];
126
+ $.each($elValues, function (i, elValue) {
127
+ $(elValue).val(values[i]);
128
+ });
129
+ } else if (multiple == 'true' || $elValues.attr('data-ajax-values') !== undefined) {
130
+ var values = (typeof data[2] == 'string') ? [data[2]] : data[2];
131
+ if (values != undefined) {
132
+ $.each(values, function (i, value) {
133
+ if ($elValues.find('option[value="' + value + '"]').length <= 0) {
134
+ $elValues.append('<option value="' + value + '">' + value + '</option>');
135
+ }
136
+ });
137
+ $elValues.val(values).trigger('change');
138
+ }
139
+ } else {
140
+ $elValues.val(data[2]);
141
+ }
142
+ $elValues.trigger('change');
143
+ }
144
+ }; //END fill_condition
145
+
146
+ plugin.load_values = function (values) {
147
+ if (typeof values == 'string') {
148
+ plugin.parameters.values = getJson(values);
149
+ } else if (is_blank(values) && !is_blank(plugin.parameters.input)) {
150
+ $elInput = $(plugin.parameters.input);
151
+ if ($elInput.length > 0) {
152
+ plugin.parameters.values = JSON.parse($elInput.val());
153
+ }
154
+ console.log(plugin.parameters.values);
155
+ } else {
156
+ plugin.parameters.values = values;
157
+ }
158
+ build_rows();
159
+ }; //END load_values
160
+
161
+ plugin.load_values_from_input = function (element_input) {
162
+ var $elInput = $(element_input);
163
+ if ($elInput.length > 0) {
164
+ plugin.parameters.values = JSON.parse($elInput.val());
165
+ }
166
+ plugin.load_values(plugin.parameters.values);
167
+ }; //END load_values_from_input
168
+
169
+ plugin.getResult = function () {
170
+ var data = [];
171
+ $element.find('.group-conditions').each(function (i, groupConditions) {
172
+ var field_name = getFieldValue(groupConditions);
173
+ var operator = getOperator(groupConditions);
174
+ var value = getValue(groupConditions);
175
+ if (!is_blank(field_name) && !is_blank(operator) && !is_blank(value)) {
176
+ data.push([field_name, operator, value]);
177
+ }
178
+ });
179
+ if (typeof plugin.result === 'function') {
180
+ plugin.result(data); //CB
181
+ }
182
+ if (!is_blank(plugin.parameters.input)) {
183
+ $(plugin.parameters.input).val(JSON.stringify(data));
184
+ }
185
+ return data;
186
+ }; //END getResult;
187
+
188
+ //~~~ PRIVATE
189
+ var getFieldByName = function (field_name) {
190
+ var f = $.map(plugin.parameters.dictionary, function (h, i) {
191
+ if (h.field == field_name) {
192
+ return h;
193
+ }
194
+ });
195
+ if (jQuery.isArray(f)) {
196
+ f = f[0];
197
+ }
198
+ return f;
199
+ }; //END getFieldByName
200
+
201
+ var getLabel = function (obj) {
202
+ if (!is_blank(obj.label)) {
203
+ return String(obj.label);
204
+ } else {
205
+ return '';
206
+ }
207
+ };
208
+
209
+ var getFieldElement = function (groupConditions) {
210
+ return $(groupConditions).find('.field:input:first');
211
+ }; //END getFieldElement
212
+
213
+ var getFieldValue = function (element) {
214
+ var value;
215
+ var $el = $(element);
216
+ if ($el.hasClass('field')) {
217
+ value = $el.val();
218
+ } else {
219
+ var el = getFieldElement(element);
220
+ value = !is_blank(el) ? getFieldValue(el) : '';
221
+ }
222
+ return value;
223
+ }; //END getFieldValue
224
+
225
+ var getOperatorElement = function (groupConditions) {
226
+ return $(groupConditions).find('.operators:first');
227
+ }; //END getOperatorElement
228
+
229
+ var getOperator = function (groupConditions) {
230
+ var operator = getOperatorElement(groupConditions).val();
231
+ if (is_blank(operator)) {
232
+ operator = ''
233
+ }
234
+ return operator;
235
+ }; //END getOperator
236
+
237
+ var getValueElement = function (groupConditions) {
238
+ var element = $(groupConditions).find('.values');
239
+ if (element.hasClass('hide')) {
240
+ element = $(groupConditions).find('.fixed_value');
241
+ }
242
+ return element;
243
+ }; //END getValues
244
+
245
+ var getValue = function (groupConditions) {
246
+ var values = getValueElement(groupConditions);
247
+ var value = [];
248
+ var op_el = getOperatorElement(groupConditions);
249
+ if (op_el.hasClass('operators')) {
250
+ op_el = op_el.find('option:selected');
251
+ }
252
+ if (op_el != undefined && op_el.length > 0 && op_el.attr('data-no-value') == 'false') {
253
+ $.each(values, function (i, elValue) {
254
+ value.push($(elValue).val());
255
+ });
256
+ }
257
+ return normalize_values(value);
258
+ }; //END getValue
259
+
260
+ var normalize_values = function (values) {
261
+ // if (typeof values=='string') {values = [values]}
262
+ if (typeof values == 'object') {
263
+ if (typeof values[0] == 'object') {
264
+ values = flatten(values);
265
+ }
266
+ $.each(values, function (i, v) {
267
+ if (is_blank(v)) {
268
+ values[i] = ''
269
+ }
270
+ });
271
+ }
272
+ if (typeof values[0] == 'string' && values.length == 1) {
273
+ values = values[0];
274
+ }
275
+ if (is_blank(values)) {
276
+ values = ''
277
+ }
278
+ return values;
279
+ }; //END normalize_values
280
+
281
+ var event_remove_condition = function (ev) {
282
+ ev.preventDefault();
283
+ $(this).closest('.group-conditions').remove();
284
+ $element.find('.conditions').trigger('change');
285
+ }; //END event_remove_condition
286
+
287
+ var event_load_values = function (ev) {
288
+ var $groupConditions = $(this).closest('.group-conditions');
289
+ var field_name = getFieldValue($groupConditions);
290
+ var field = getFieldByName(field_name);
291
+ var op_el = $(this).hasClass('operators') ? $(this).find('option:selected') : $(this);
292
+ var values = field.values;
293
+ build_values(op_el, values);
294
+ }; //END event_load_values
295
+
296
+ var event_build_expression = function (ev) {
297
+ if (plugin.parameters.debug == true) {
298
+ var $groupConditions = $(this).closest('.group-conditions');
299
+ var index = $groupConditions.attr('data-index');
300
+ var $expression = $groupConditions.find('.expression');
301
+ var $field = getFieldValue($groupConditions);
302
+ var $operator = getOperator($groupConditions);
303
+ var $value = getValue($groupConditions);
304
+ $expression.html($field + ' ' + $operator + ' ' + $value);
305
+ }
306
+ plugin.getResult();
307
+ }; //END event_build_expression
308
+
309
+ var build_rows = function () {
310
+ $element.find('.conditions').html('');
311
+ if (!is_blank(plugin.parameters.values) && plugin.parameters.values.length > 0) {
312
+ $.each(plugin.parameters.values, function (i, data) {
313
+ var field = data[0];
314
+ var groupConditions = plugin.add_condition(field);
315
+ plugin.fill_condition(groupConditions, data);
316
+ });
317
+ }
318
+ };
319
+
320
+ var build_values = function (op_el, values) {
321
+ var $groupConditions = $(op_el).closest('.group-conditions');
322
+ var no_value = op_el.attr('data-no-value');
323
+ var $values = $groupConditions.find('.values');
324
+ var $fixedValue = $groupConditions.find('.fixed_value');
325
+ var multiple = op_el.attr('data-multiple');
326
+ multiple = (multiple == 'true' ? 'true' : 'false');
327
+ $values.html('').removeAttr('data-ajax-values').addClass('hide');
328
+ $fixedValue.removeAttr('data-ajax-values').addClass('hide');
329
+
330
+ if (typeof values === 'string') {
331
+ $values.attr('data-ajax-values', values);
332
+ $fixedValue.attr('data-ajax-values', values);
333
+ values = [''];
334
+ }
335
+
336
+ var list_with_item = (values != undefined && values.length > 0);
337
+
338
+ if (no_value == 'false') {
339
+ if (list_with_item) {
340
+ $.each(values, function (val_i, val_el) {
341
+ if (typeof val_el == 'object') {
342
+ var _id = val_el.id;
343
+ var _label = getLabel(val_el);
344
+ if (is_blank(_label)) {
345
+ _label = _id;
346
+ }
347
+ } else {
348
+ var _id = val_el;
349
+ var _label = _id;
350
+ }
351
+ $values.append('<option data-index="' + val_i + '" value="' + _id + '">' + _label + '</option>');
352
+ });
353
+ $values.removeClass('hide');
354
+ $values.trigger('change');
355
+ } else {
356
+ if (multiple == 'true') {
357
+ $values.removeClass('hide');
358
+ $values.trigger('change');
359
+ } else {
360
+ $fixedValue.removeClass('hide');
361
+ $fixedValue.trigger('change');
362
+ }
363
+ }
364
+ }
365
+
366
+ validate_multiple_values($groupConditions);
367
+ normalize_values_type($groupConditions, list_with_item, multiple);
368
+ }; //END build_values
369
+
370
+ var validate_multiple_values = function (groupConditions) {
371
+ var $groupConditions = $(groupConditions);
372
+ var $operatorEl = getOperatorElement($groupConditions);
373
+ var $valueEl = getValueElement($groupConditions);
374
+ if ($operatorEl.hasClass('operators')) {
375
+ $operatorEl = $operatorEl.find('option:selected');
376
+ }
377
+ var multiple = $operatorEl.attr('data-multiple');
378
+ remove_plugins_elements(groupConditions);
379
+ if (!isNaN(multiple)) {
380
+ for (var i = 1; i < parseInt(multiple); i++) {
381
+ var $valueElClone = $valueEl.clone();
382
+ $valueElClone.addClass('clone').removeClass('hide');
383
+ $valueElClone.insertAfter($valueEl);
384
+ }
385
+ } else {
386
+ $valueEl.attr('multiple', multiple == 'true');
387
+ }
388
+ $valueEl.attr('multiple', multiple == 'true');
389
+ }; //END validate_multiple_values
390
+
391
+ var remove_plugins_elements = function (groupConditions) {
392
+ $groupConditions = $(groupConditions);
393
+ $groupConditions.find('.fixed_value[class*="select2-"], .values[class*="select2-"]').select2('destroy');
394
+ $groupConditions.find('.values.clone, .fixed_value.clone').remove();
395
+ }; //END remove_plugins_elements
396
+
397
+ var normalize_values_type = function (groupConditions, list_with_item, multiple) {
398
+ var $groupConditions = $(groupConditions);
399
+ var $fieldEl = getFieldElement($groupConditions);
400
+ var $valueEl = getValueElement($groupConditions);
401
+ var uTypeField = $fieldEl.attr('data-type'); //.toUpperCase();
402
+ var field_name = getFieldValue(groupConditions);
403
+
404
+ $.each($valueEl, function (i, el) {
405
+ var $el = $(el);
406
+
407
+ //RESET ELEMENT
408
+ $el.autoNumeric('destroy');
409
+ // $element.tooltip('destroy');
410
+ if (plugin.parameters.debug == true) {
411
+ $el.parent().find('.type_field').remove();
412
+ }
413
+
414
+ //VALIDATE TYPE
415
+ switch (true) {
416
+ case /^DATE$/.test(uTypeField):
417
+ $el.attr('type', 'date');
418
+ break;
419
+ case /^TIME$/.test(uTypeField):
420
+ $el.attr('type', 'time');
421
+ break;
422
+ case /^DATETIME$/.test(uTypeField):
423
+ $el.attr('type', 'datetime');
424
+ break;
425
+ case /^DECIMAL/.test(uTypeField):
426
+ $el.attr('type', 'text');
427
+ var decimal_places = uTypeField.match(/\(([0-9]+)\)$/);
428
+ if (decimal_places == undefined || decimal_places == null) {
429
+ decimal_places = /Vl[A-Z]/.test(field_name) == true ? 2 : 4;
430
+ } else {
431
+ decimal_places = decimal_places[1];
432
+ }
433
+ $el.autoNumeric("init", plugin.parameters.numericConfig);
434
+ $el.autoNumeric("update", {mDec: decimal_places});
435
+ window.setTimeout(function () {
436
+ if ($el.autoNumeric('getSettings') != undefined) {
437
+ $el.trigger('blur'); //FORCE REFRESH
438
+ }
439
+ }, 500);
440
+ // $element.tooltip({
441
+ // placement: 'top',
442
+ // title: 'Use DOT as decimal separator',
443
+ // trigger: 'hover focus active'
444
+ // });
445
+ break;
446
+ case /^INTEGER$/.test(uTypeField):
447
+ $el.attr('type', 'number');
448
+ break;
449
+ case /^STRING$/.test(uTypeField):
450
+ default:
451
+ $el.attr('type', 'text');
452
+ break;
453
+ }
454
+ if (plugin.parameters.debug == true) {
455
+ console.log(field_name, uTypeField);
456
+ $el.parent().append('<i class="type_field" style="font-size: 10px; color: #ccc;"><br>' + uTypeField + '</i>');
457
+ }
458
+ if (($el.hasClass('values') && !$el.hasClass('hide'))) {
459
+ // build_select2_element($el, (!list_with_item && multiple == 'true'));
460
+ build_select2_element($el);
461
+ }
462
+ });
463
+ }; //END normalize_values_type
464
+
465
+ var normalize_operators = function (operators) {
466
+ if (operators != undefined && typeof operators == 'object' && operators.length > 0) {
467
+ $.each(operators, function (i, row) {
468
+ var label = getLabel(row);
469
+ if (is_blank(label)) {
470
+ operators[i].label = row.operator;
471
+ }
472
+ if (is_blank(row.multiple)) {
473
+ operators[i].multiple = 'false';
474
+ }
475
+ if (is_blank(row.no_value)) {
476
+ operators[i].no_value = 'false';
477
+ }
478
+ });
479
+ }
480
+ return operators;
481
+ }; //END normalize_operators
482
+
483
+ var is_blank = function (value) {
484
+ return (value === undefined || value === null || value === [] || value === '')
485
+ }; //END is_blank
486
+
487
+ var flatten = function (arrays) {
488
+ return [].concat.apply([], arrays);
489
+ }; //END flatten
490
+
491
+ var getJson = function (url, force, cb) {
492
+ if (force == undefined || force == null) {
493
+ force = false;
494
+ }
495
+ if (force == true) {
496
+ jsonCache[url] = undefined;
497
+ }
498
+ if (jsonCache[url] == undefined) {
499
+ $.ajax({
500
+ url: url,
501
+ async: false,
502
+ dataType: 'json',
503
+ success: function (dates) {
504
+ jsonCache[url] = dates;
505
+ if (cb != undefined && typeof cb == 'function') {
506
+ cb(dates)
507
+ }
508
+ ;
509
+ }, errorr: function (errorr) {
510
+ alert("Could not load url " + url);
511
+ }
512
+ });
513
+ }
514
+ return jsonCache[url];
515
+ }; //END getJson
516
+
517
+ var build_select2_element = function (el, tags) {
518
+ if (tags == undefined) tags = true;
519
+ var $el = $(el);
520
+ var select2Config = plugin.parameters.select2Config;
521
+ var ajax_url = $el.attr('data-ajax-values');
522
+ if (ajax_url != undefined) {
523
+ $.extend(select2Config, {
524
+ ajax: {
525
+ url: ajax_url,
526
+ dataType: 'json',
527
+ delay: 250,
528
+ data: function (params) {
529
+ return {
530
+ key: params.term, // search term
531
+ page: params.page
532
+ };
533
+ },
534
+ processResults: function (data, params) {
535
+ return {
536
+ results: $.map(data, function (item) {
537
+ return {
538
+ text: getLabel(item),
539
+ id: item.id
540
+ }
541
+ })
542
+ };
543
+ },
544
+ cache: true
545
+ },
546
+ initSelection: function (element, callback) {
547
+ var initVal = $(element).val();
548
+ if (initVal !== "") {
549
+ $.ajax(ajax_url, {
550
+ dataType: 'json',
551
+ data: {init: initVal}
552
+ }).done(function (data) {
553
+ var row = $.isArray(data) ? data[0] : data;
554
+ if (!is_blank(row.id) && !is_blank(row.label)) {
555
+ callback({id: row.id, text: row.label});
556
+ } else {
557
+ callback({id: initVal, text: initVal});
558
+ }
559
+ });
560
+ }
561
+ },
562
+ minimumInputLength: 1
563
+ });
564
+ }
565
+
566
+ var totalItems = $el.find('option').length;
567
+ if ($el.hasClass('operators')) {
568
+ $.extend(select2Config, {placeholder: plugin.parameters.placeholder.operators});
569
+ }
570
+ if ($el.hasClass('values')) {
571
+ $.extend(select2Config, {placeholder: plugin.parameters.placeholder.values});
572
+ }
573
+
574
+ if (tags == true) {
575
+ $.extend(select2Config, {tags: true, tokenSeparators: [',', ';']});
576
+ }
577
+
578
+ $.fn.select2.defaults.set("theme", (select2Config.theme || 'bootstrap'));
579
+ var select2El = $el.select2(select2Config);
580
+ var select2DefaultValue = (totalItems == 1 ? $el.find('option:first').val() : null);
581
+ select2El.val(select2DefaultValue).trigger("change"); //FORCE RESET DO SET PLACEHOLDER
582
+ };
583
+
584
+ plugin.init = function () {
585
+ plugin.parameters = $.extend(true, defaults, options);
586
+
587
+ //~~~ INIT
588
+ $element.append('<div class="conditions"></div>');
589
+ $element.on('change', '.conditions, .operators, .values, .fixed_value', event_build_expression);
590
+
591
+ if (typeof plugin.parameters.dictionary == 'string') {
592
+ plugin.parameters.dictionary = getJson(plugin.parameters.dictionary);
593
+ }
594
+
595
+ if (!is_blank(plugin.parameters.input)) {
596
+ plugin.load_values_from_input(plugin.parameters.input);
597
+ $(document).on('change', plugin.parameters.input, function (ev) {
598
+ plugin.load_values_from_input(plugin.parameters.input);
599
+ });
600
+ } else {
601
+ plugin.load_values(plugin.parameters.values);
602
+ }
603
+
604
+ Sortable.create(element, {
605
+ handle: '.conditions-move',
606
+ animation: 150,
607
+ onSort: function (evt) {
608
+ plugin.getResult();
609
+ }
610
+ });
611
+ };
612
+
613
+ plugin.init();
614
+ };
615
+
616
+ $.fn.conditionBuilder = function (options) {
617
+ return this.each(function () {
618
+ if (undefined == $(this).data('conditionBuilder')) {
619
+ var plugin = new $.conditionBuilder(this, options);
620
+ $(this).data('conditionBuilder', plugin);
621
+ }
622
+ });
623
+ };
624
+ })
625
+ (jQuery);
626
+
627
+ $(document).on('click', '.add-condition-field', function (ev) {
628
+ ev.preventDefault();
629
+ var container = $(this).closest('.add-condition').attr('data-target');
630
+ var field_name = $(this).attr('data-field');
631
+ var $conditionBuilder = $(container).data('conditionBuilder');
632
+ $conditionBuilder.add_condition(field_name);
633
+ });