combo_auto_box 0.0.5 → 0.0.6

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.
@@ -1,3 +1,3 @@
1
1
  module ComboAutoBox
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
@@ -9,11 +9,24 @@ var ComboAutoBox = {
9
9
  now++;
10
10
  }
11
11
 
12
- return "combo-auto-box-" + now;
12
+ return prefix + "-" + now;
13
13
  };
14
14
 
15
15
  // binds autocomplete to text field
16
16
  var bindAutoComplete = function (inputId) {
17
+ var previuosValue = '';
18
+ $('#' + inputId).keydown(function(e) {
19
+ if ((e.keyCode == 8) && (options.type == 'multiple') && ($('#' + inputId).val() == '')) {
20
+ $('#' + container + ' > div.multiple > div.item:last').remove();
21
+ }
22
+
23
+ if ((e.keyCode == 8) && (options.type == 'searchable') && ($('#' + inputId).val() == '')) {
24
+ $('#' + container + ' > div.searchable > div.item:last').remove();
25
+ selectData('');
26
+ }
27
+
28
+ });
29
+
17
30
  $('#' + inputId).keypress(function(e) {
18
31
  if (e.which === 13) {
19
32
  if (options.type == 'full') {
@@ -24,8 +37,8 @@ var ComboAutoBox = {
24
37
  addItem(inputId, $('#' + inputId).val(), $('#' + inputId).val());
25
38
  selectData($('#' + inputId).val());
26
39
  $('#' + inputId).val('');
27
- }
28
- return false;
40
+ }
41
+ return false;
29
42
  }
30
43
  });
31
44
 
@@ -38,17 +51,47 @@ var ComboAutoBox = {
38
51
  return selectData($('#' + inputId).val());
39
52
  } else if (options.type == 'multiple') {
40
53
  $('#' + inputId).val('');
41
- addItem(inputId, ui.item.label, ui.item.id);
54
+ addItem(inputId, ui.item.id, ui.item.label);
55
+ selectData(ui.item.id);
56
+ return false;
57
+ } else if (options.type == 'searchable') {
58
+ $('#' + inputId).val('');
59
+ addSearchableItemForRansack(inputId, ui.item.id, ui.item.label);
42
60
  selectData(ui.item.id);
43
61
  return false;
44
62
  }
45
- }
63
+ },
64
+ search: function(event, ui) {
65
+ if (options.type == 'searchable') {
66
+ $('#' + inputId).autocomplete("option", { source: setAutoCompleteSource(inputId) });
67
+ }
68
+ },
69
+ change: function (event, ui) {
70
+ if (!ui.item) {
71
+ $(this).val('');
72
+ }
73
+ },
74
+ focus: function (event, ui) {
75
+ event.preventDefault();
76
+ },
46
77
  });
47
78
  };
48
79
 
49
80
  // set autocomplete source
50
81
  var setAutoCompleteSource = function (inputId) {
51
- if (typeof options.source == 'string') {
82
+ if (options.type == 'searchable') {
83
+ var new_source = new Array();
84
+ var operators = i18nMath(options.lang);
85
+ $.each(options.source, function(i){
86
+ validIndexes = validSource(options.source[i]);
87
+ $.each(operators, function(j){
88
+ if (validIndexes.indexOf(j) >= 0) {
89
+ new_source.push( { id: options.source[i]['id'] + '_' + operators[j]['id'], label: options.source[i]['label'] + ' ' + operators[j]['label'] + ' ' + $('#' + inputId).val()} );
90
+ }
91
+ });
92
+ });
93
+ return new_source;
94
+ } else if (typeof options.source == 'string') {
52
95
  return function(request, response) {
53
96
  var term = 'term=' + $('#' + inputId).val();
54
97
  var params = (options.data == null) ? term : options.data + '&' + term;
@@ -59,12 +102,37 @@ var ComboAutoBox = {
59
102
  }
60
103
  };
61
104
 
105
+ // get i18n math comparisons
106
+ var i18nMath = function (language) {
107
+ var operators = new Array();
108
+ switch(language) {
109
+ case 'en':
110
+ operators = [ { id: 'cont', label: 'contains' }, { id: 'eq', label: 'equal' }, { id: 'gteq', label: 'greater or equal' }, { id: 'lteq', label: 'less or equal' } ];
111
+ break;
112
+ case 'pt-br':
113
+ operators = [ { id: 'cont', label: 'contém' }, { id: 'eq', label: 'igual' }, { id: 'gteq', label: 'maior que' }, { id: 'lteq', label: 'menor que' } ];
114
+ break;
115
+ case 'pt':
116
+ operators = [ { id: 'cont', label: 'contém' }, { id: 'eq', label: 'igual' }, { id: 'gteq', label: 'maior que' }, { id: 'lteq', label: 'menor que' } ];
117
+ break;
118
+ case 'fr':
119
+ operators = [ { id: 'cont', label: 'contient' }, { id: 'eq', label: 'égal' }, { id: 'gteq', label: 'supérieur ou égal' }, { id: 'lteq', label: 'inférieur ou égal' } ];
120
+ break;
121
+ case 'es':
122
+ operators = [ { id: 'cont', label: 'contiene' }, { id: 'eq', label: 'igual' }, { id: 'gteq', label: 'mayor o igual' }, { id: 'lteq', label: 'menos o igual' } ];
123
+ break;
124
+ default:
125
+ operators = [ { id: 'cont', label: '~=' }, { id: 'eq', label: '=' }, { id: 'gteq', label: '>=' }, { id: 'lteq', label: '<=' } ];
126
+ }
127
+ return operators;
128
+ };
129
+
62
130
  // generates text field with html options
63
131
  var generateInputTag = function () {
64
132
  var html = 'input type="text"';
65
133
  if (options.html != null) {
66
134
  $.each(options.html, function(key, value) {
67
- if ((key == 'name') && (options.type == 'multiple')) {
135
+ if ((key == 'name') && ((options.type == 'multiple') || (options.type == 'searchable'))) {
68
136
  return true;
69
137
  }
70
138
  html = html + ' '+ key +'="' + value + '"';
@@ -79,10 +147,31 @@ var ComboAutoBox = {
79
147
  };
80
148
 
81
149
  // On click opens modal image tag inside "i" tag through css
82
- var generateImageTag = function () {
83
- return '<span class="expand"><i></i></span>';
150
+ var generateExpander = function () {
151
+ if (options.type == 'simple') {
152
+ return '<span class="simple"><i></i></span>';
153
+ } else if (options.type == 'multiple') {
154
+ return '<span class="multiple">' + options.label + ':</span>';
155
+ }
84
156
  };
85
157
 
158
+ var adjustExpanderImage = function() {
159
+ if (options.type != 'simple') {
160
+ return false;
161
+ }
162
+
163
+ spanTag = $('#' + container + ' > div.container-combo-auto-box > span.simple');
164
+ var paddingRight = 1;
165
+ try {
166
+ paddingRight = parseInt(textField.css('padding-right').replace(/px/, ''));
167
+ } catch (error) {
168
+ paddingRight = 1;
169
+ }
170
+ spanTag.css('margin', '2px 0px 0px ' + (paddingRight + textField.width() + 9).toString() + 'px');
171
+
172
+ return true;
173
+ }
174
+
86
175
  // Global div for combo auto box
87
176
  var generateDivTag = function () {
88
177
  var derivation = ''
@@ -115,7 +204,7 @@ var ComboAutoBox = {
115
204
 
116
205
  $("#" + modalDialogId).siblings('div.ui-dialog-titlebar').remove();
117
206
 
118
- $('#' + container + ' > div.container-combo-auto-box > span.expand').click(function() {
207
+ $('#' + container + ' > div.container-combo-auto-box > span.' + options.type).click(function() {
119
208
  openModalDialog(modalDialogId)
120
209
  });
121
210
  };
@@ -151,9 +240,19 @@ var ComboAutoBox = {
151
240
  $('#' + modalDialogId + ' > div.list').css('height', ($('#' + modalDialogId).dialog("option", "height") - 60) + 'px');
152
241
  $('#' + modalDialogId + ' > div.list > ul').html(items.join(''));
153
242
  $('#' + modalDialogId + ' > div.list > ul > li').click(function() {
243
+ var thisId = $(this).children('span.combo-auto-box-item-id').text();
244
+ var thisLabel = $(this).children('span.combo-auto-box-item-label').text();
245
+
154
246
  $('#' + modalDialogId).dialog('close');
155
- $('#' + container + ' > div.container-combo-auto-box > input').val($(this).children('span.combo-auto-box-item-label').text());
156
- selectData($(this).children('span.combo-auto-box-item-id').text());
247
+
248
+ if (options.type == 'simple') {
249
+ $('#' + container + ' > div.container-combo-auto-box > input').val(thisLabel);
250
+ selectData(thisId);
251
+ } else if (options.type == 'multiple') {
252
+ $('#' + container + ' > div.container-combo-auto-box > input').val('');
253
+ addItem($('#' + container + ' > div.container-combo-auto-box > input').attr('id'), thisLabel, thisLabel);
254
+ selectData(thisId);
255
+ }
157
256
  });
158
257
  };
159
258
 
@@ -164,22 +263,15 @@ var ComboAutoBox = {
164
263
 
165
264
  // starting generate modial dialog
166
265
  var generateModalDialog = function (textField) {
167
- $(generateImageTag()).prependTo('#' + container + ' > div.container-combo-auto-box');
266
+ $(generateExpander()).prependTo('#' + container + ' > div.container-combo-auto-box');
168
267
 
169
- spanTag = $('#' + container + ' > div.container-combo-auto-box > span.expand');
170
- var paddingRight = 1;
171
- try {
172
- paddingRight = parseInt(textField.css('padding-right').replace(/px/, ''));
173
- } catch (error) {
174
- paddingRight = 1;
175
- }
176
- spanTag.css('margin', '2px 0px 0px ' + (paddingRight + textField.width() + 9).toString() + 'px');
268
+ adjustExpanderImage();
177
269
 
178
270
  generateDivDialogModal(generateAnId('model-dialog'));
179
271
  };
180
272
 
181
273
  // add item
182
- var addItem = function (inputId, selectedData, selectedId) {
274
+ var addItem = function (inputId, selectedId, selectedData) {
183
275
  if (selectedData != '') {
184
276
  var id = generateAnId('item');
185
277
  $('#' + inputId).before('<div class="item" title="Remove Item" id="' + id + '">'+ selectedData +'<span>x</span><input type="hidden" name="'+ options.html.name +'[]" value="'+ selectedId +'"></div>');
@@ -190,12 +282,72 @@ var ComboAutoBox = {
190
282
 
191
283
  }
192
284
  };
285
+
286
+ // add searchable item for ransack
287
+ var addSearchableItemForRansack = function (inputId, selectedId, selectedData) {
288
+ if (selectedData != '') {
289
+ var ransackId = generateAnId('ransack');
290
+ attribute = getSearchableAttribute(selectedId);
291
+ fieldAttribute = '<input type="hidden" name="q[g]['+ attribute +'][c]['+ ransackId +'][a][0][name]" value="'+ attribute +'">';
292
+ fieldCondition = '<input type="hidden" name="q[g]['+ attribute +'][c]['+ ransackId +'][p]" value="'+ getSearchableCondition(selectedId) +'">';
293
+ fieldValue = '<input type="hidden" name="q[g]['+ attribute +'][c]['+ ransackId +'][v][0][value]" value="'+ getSearchableValue(selectedData) +'">';
294
+ var id = generateAnId('item');
295
+ $('#' + inputId).before('<div class="item" title="Remove Item" id="' + id + '">'+ selectedData +'<span>x</span>'+ fieldAttribute + fieldCondition + fieldValue +'</div>');
296
+
297
+ $('#' + id + ' > span').click(function() {
298
+ $(this).parent().remove();
299
+ selectData('');
300
+ });
301
+ }
302
+ };
193
303
 
194
- // Bind click on div for multiple
304
+ // add searchable item
305
+ var addSearchableItem = function (inputId, selectedId, selectedData) {
306
+ if (selectedData != '') {
307
+ var id = generateAnId('item');
308
+ $('#' + inputId).before('<div class="item" title="Remove Item" id="' + id + '">'+ selectedData +'<span>x</span><input type="hidden" name="'+ options.html.name +'['+ selectedId +'][]" value="'+ getSearchableValue(selectedData) +'"></div>');
309
+
310
+ $('#' + id + ' > span').click(function() {
311
+ $(this).parent().remove();
312
+ selectData('');
313
+ });
314
+
315
+ }
316
+ };
317
+
318
+ // get only the attribute from selected Data
319
+ var getSearchableAttribute = function (selectedId) {
320
+ var fields = $.map(options.source, function(val, i) { return val['id']}).join('|');
321
+ var maths = $.map(i18nMath(options.lang), function(val, i) { return val['id']}).join('|');
322
+ var pattern = new RegExp('(' + fields + ')_(' + maths + ')');
323
+ return selectedId.match(pattern)[1];
324
+ }
325
+
326
+ // get only the condition from selected Data
327
+ var getSearchableCondition = function (selectedId) {
328
+ var fields = $.map(options.source, function(val, i) { return val['id']}).join('|');
329
+ var maths = $.map(i18nMath(options.lang), function(val, i) { return val['id']}).join('|');
330
+ var pattern = new RegExp('(' + fields + ')_(' + maths + ')');
331
+ return selectedId.match(pattern)[2];
332
+ }
333
+
334
+ // get only the value from selected Data
335
+ var getSearchableValue = function (selectedData) {
336
+ var fields = $.map(options.source, function(val, i) { return val['label']}).join('|');
337
+ var maths = $.map(i18nMath(options.lang), function(val, i) { return val['label']}).join('|');
338
+ var pattern = new RegExp('(' + fields + ') (' + maths + ') (.*)');
339
+ return selectedData.match(pattern)[3];
340
+ }
341
+
342
+ // Bind click on div for multiple or searchble
195
343
  var bindContainerClick = function(inputId) {
196
344
  $('#' + container + ' > div.multiple').click(function() {
197
345
  $('#' + inputId).focus();
198
346
  });
347
+
348
+ $('#' + container + ' > div.searchable').click(function() {
349
+ $('#' + inputId).focus();
350
+ });
199
351
  };
200
352
 
201
353
  // on select data
@@ -205,23 +357,76 @@ var ComboAutoBox = {
205
357
  }
206
358
  };
207
359
 
360
+ // valid language or set 'en' as default
361
+ var validLanguage = function () {
362
+ var langs = ['math', 'en', 'pt-br', 'pt', 'es', 'fr'];
363
+ for(var i=0; i<langs.length;i++) {
364
+ if (options.lang == langs[i]) {
365
+ return true;
366
+ }
367
+ }
368
+
369
+ options.lang = 'en';
370
+ return true;
371
+ };
372
+
373
+ // valid language or set 'en' as default
374
+ var validType = function () {
375
+ var types = ['simple', 'full', 'multiple', 'searchable'];
376
+ for(var i=0; i<types.length;i++) {
377
+ if (options.type == types[i]) {
378
+ return true;
379
+ }
380
+ }
381
+
382
+ options.type = 'simple';
383
+ return true;
384
+ };
385
+
386
+ // valid sources for only and except
387
+ var validSource = function (source) {
388
+ operators = i18nMath('math');
389
+ validIndexes = new Array();
390
+
391
+ if (((source['only'] != null) && (source['except'] != null)) || ((source['only'] == null) && (source['except'] == null))) {
392
+ for(var i=0; i<operators.length;i++) {
393
+ validIndexes.push(i);
394
+ }
395
+ } else if (source['only'] != null) {
396
+ for(var i=0; i<operators.length;i++) {
397
+ if ((source['only'].indexOf(operators[i]['id']) + source['only'].indexOf(operators[i]['label'])) >= -1) {
398
+ validIndexes.push(i);
399
+ }
400
+ }
401
+ } else if (source['except'] != null) {
402
+ for(var i=0; i<operators.length;i++) {
403
+ if ((source['except'].indexOf(operators[i]['id']) + source['except'].indexOf(operators[i]['label'])) == -2) {
404
+ validIndexes.push(i);
405
+ }
406
+ }
407
+ }
408
+
409
+ return validIndexes;
410
+ }
411
+
208
412
  // main
209
413
  if (options == null) {
210
414
  options = {};
211
415
  }
212
-
213
- if ((options.type == null) || ((options.type != 'simple') && (options.type != 'full') && (options.type != 'multiple') && (options.type != 'searchable'))) {
214
- options.type = 'simple';
215
- }
416
+
417
+ validLanguage();
418
+ validType();
216
419
 
217
420
  $('#' + container).html(generateDivTag());
218
421
 
219
422
  textField = $('#' + container + ' > div.container-combo-auto-box > input');
220
423
  bindAutoComplete(textField.attr('id'));
221
424
 
222
- if (options.type == 'simple') {
425
+ if ((options.type == 'simple') || (options.type == 'multiple')) {
223
426
  generateModalDialog(textField);
224
- } else if (options.type == 'multiple') {
427
+ }
428
+
429
+ if ((options.type == 'multiple') || (options.type == 'searchable')) {
225
430
  bindContainerClick(textField.attr('id'));
226
431
  }
227
432
  }
@@ -1,16 +1,22 @@
1
1
  span.combo-auto-box-item-id { display:none; }
2
- div.container-combo-auto-box span.expand{display:block;position:absolute;cursor:pointer;display:block;margin:0px 0px 0px 0px; width:20px;}
3
-
4
- div.container-combo-auto-box span.expand i{background-image:url(/assets/combo_auto_box_expand.png);width:19px;height:19px;display:block;font-size:0;}
2
+ div.container-combo-auto-box span.simple { position:absolute;cursor:pointer;display:block;margin:0px 0px 0px 0px; width:20px; }
3
+ div.container-combo-auto-box span.simple i{ background-image:url(/assets/combo_auto_box_expand.png);width:19px;height:19px;display:block;font-size:0; }
5
4
 
6
5
  div.multiple { border: 1px solid #CDCDCD; display:table; }
6
+ div.multiple span.multiple { font-family: arial,sans-serif; float:left; font-size: 13px; color:#9A9A9A; margin: 2px 2px 2px 2px; padding:2px 10px 2px 10px; }
7
7
  div.multiple input { border: 0px; float:left; height:22px; }
8
-
9
- div.multiple div.item { font-family: arial,sans-serif; float:left; background-color:#EDEDED; margin: 2px 10px 2px 2px; padding:2px 10px 2px 10px; -moz-border-radius:4px;-webkit-border-radius:4px; border:1px solid #CECECE; font-size: 13px; }
8
+ div.multiple div.item { font-family: arial,sans-serif; float:left; font-size: 13px; color:#000000; background-color:#EDEDED; margin: 2px 10px 2px 2px; padding:2px 10px 2px 10px; -moz-border-radius:4px;-webkit-border-radius:4px; border:1px solid #CECECE; }
10
9
  div.multiple div.item span { margin-left:10px; color:#CCCCCC; }
11
10
  div.multiple div.item span:hover { color:#9A9A9A; cursor:pointer;}
12
11
  div.multiple input:focus {outline:none; }
13
12
 
13
+ div.searchable { border: 1px solid #CDCDCD; display:table; }
14
+ div.searchable input { border: 0px; float:left; height:22px; }
15
+ div.searchable div.item { font-family: arial,sans-serif; float:left; font-size: 13px; color:#FFFFFF; background-color:#008000; margin: 2px 10px 2px 2px; padding:2px 10px 2px 10px; -moz-border-radius:4px;-webkit-border-radius:4px; border:1px solid #ECECEC; }
16
+ div.searchable div.item span { margin-left:10px; color:#CCCCCC; }
17
+ div.searchable div.item span:hover { color:#FFFFFF; cursor:pointer;}
18
+ div.searchable input:focus {outline:none; }
19
+
14
20
  div.dialog-modal div.head { display:block; width:99%; position:absolute; z-index:2002; top:0px; left:0px; border: 1px solid #000; background-color:#CECECE; height:30px; -moz-border-radius:4px;-webkit-border-radius:4px;}
15
21
  div.dialog-modal div.head span.label { float:left; margin: 5px 0 0 10px; padding: 0 0 0 0; text-weight: bold; font-size:16px; font-family: "Trebuchet MS", "Helvetica", "Arial", "Verdana", "sans-serif";}
16
22
  div.dialog-modal div.head span.close { float:right; margin: 5px 10px 0 0; padding: 0 0 0 0; font-size:16px; font-family: "Trebuchet MS", "Helvetica", "Arial", "Verdana", "sans-serif"; text-indent: 0;}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: combo_auto_box
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-12 00:00:00.000000000 Z
12
+ date: 2013-07-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport