combo_auto_box 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module ComboAutoBox
2
- VERSION = '0.0.6'
3
- end
2
+ VERSION = '0.0.7'
3
+ end
@@ -1,32 +1,41 @@
1
1
  var ComboAutoBox = {
2
2
  // constructor
3
- addTo: function (container, options) {
4
-
3
+ addTo: function (container, options) {
5
4
  // generatea an ID based on current time
6
- var generateAnId = function(prefix) {
7
- var now = new Date().getTime();;
5
+ var generateShortId = function(prefix) {
6
+ var now = 0;
8
7
  while ($("#" + prefix +"-" + now).length != 0) {
9
8
  now++;
10
9
  }
10
+
11
+ return prefix + "-" + now;
12
+ };
11
13
 
14
+ // generatea an ID based on current time
15
+ var generateAnId = function(prefix) {
16
+ var now = new Date().getTime();
17
+ while ($("#" + prefix +"-" + now).length != 0) {
18
+ now++;
19
+ }
20
+
12
21
  return prefix + "-" + now;
13
22
  };
14
-
23
+
15
24
  // binds autocomplete to text field
16
25
  var bindAutoComplete = function (inputId) {
17
- var previuosValue = '';
26
+ var previuosValue = '';
18
27
  $('#' + inputId).keydown(function(e) {
19
28
  if ((e.keyCode == 8) && (options.type == 'multiple') && ($('#' + inputId).val() == '')) {
20
29
  $('#' + container + ' > div.multiple > div.item:last').remove();
21
30
  }
22
-
31
+
23
32
  if ((e.keyCode == 8) && (options.type == 'searchable') && ($('#' + inputId).val() == '')) {
24
33
  $('#' + container + ' > div.searchable > div.item:last').remove();
25
34
  selectData('');
26
35
  }
27
-
36
+
28
37
  });
29
-
38
+
30
39
  $('#' + inputId).keypress(function(e) {
31
40
  if (e.which === 13) {
32
41
  if (options.type == 'full') {
@@ -41,7 +50,7 @@ var ComboAutoBox = {
41
50
  return false;
42
51
  }
43
52
  });
44
-
53
+
45
54
  $('#' + inputId).autocomplete({
46
55
  source: setAutoCompleteSource(inputId),
47
56
  select: function(event, ui) {
@@ -76,7 +85,7 @@ var ComboAutoBox = {
76
85
  },
77
86
  });
78
87
  };
79
-
88
+
80
89
  // set autocomplete source
81
90
  var setAutoCompleteSource = function (inputId) {
82
91
  if (options.type == 'searchable') {
@@ -101,7 +110,7 @@ var ComboAutoBox = {
101
110
  return options.source;
102
111
  }
103
112
  };
104
-
113
+
105
114
  // get i18n math comparisons
106
115
  var i18nMath = function (language) {
107
116
  var operators = new Array();
@@ -126,7 +135,7 @@ var ComboAutoBox = {
126
135
  }
127
136
  return operators;
128
137
  };
129
-
138
+
130
139
  // generates text field with html options
131
140
  var generateInputTag = function () {
132
141
  var html = 'input type="text"';
@@ -138,28 +147,28 @@ var ComboAutoBox = {
138
147
  html = html + ' '+ key +'="' + value + '"';
139
148
  });
140
149
  }
141
-
150
+
142
151
  if ((options.html == null) || (options.html.id == null)) {
143
152
  html = html + ' id="' + generateAnId('combo-auto-box') + '"';
144
153
  }
145
-
146
- return '<' + html + '>';
154
+
155
+ return '<' + html + '>';
147
156
  };
148
-
157
+
149
158
  // On click opens modal image tag inside "i" tag through css
150
159
  var generateExpander = function () {
151
160
  if (options.type == 'simple') {
152
- return '<span class="simple"><i></i></span>';
161
+ return '<span class="simple"><i></i></span>';
153
162
  } else if (options.type == 'multiple') {
154
- return '<span class="multiple">' + options.label + ':</span>';
163
+ return '<span class="multiple">' + options.label + ':</span>';
155
164
  }
156
165
  };
157
-
166
+
158
167
  var adjustExpanderImage = function() {
159
168
  if (options.type != 'simple') {
160
169
  return false;
161
170
  }
162
-
171
+
163
172
  spanTag = $('#' + container + ' > div.container-combo-auto-box > span.simple');
164
173
  var paddingRight = 1;
165
174
  try {
@@ -168,10 +177,10 @@ var ComboAutoBox = {
168
177
  paddingRight = 1;
169
178
  }
170
179
  spanTag.css('margin', '2px 0px 0px ' + (paddingRight + textField.width() + 9).toString() + 'px');
171
-
180
+
172
181
  return true;
173
182
  }
174
-
183
+
175
184
  // Global div for combo auto box
176
185
  var generateDivTag = function () {
177
186
  var derivation = ''
@@ -180,18 +189,18 @@ var ComboAutoBox = {
180
189
  } else if (options.type == 'searchable') {
181
190
  derivation = ' searchable'
182
191
  }
183
-
184
- return '<div class="container-combo-auto-box' + derivation + '">' + generateInputTag() + '</div>';
192
+
193
+ return '<div class="container-combo-auto-box' + derivation + '">' + generateInputTag() + '</div>';
185
194
  };
186
-
195
+
187
196
  // dialog modal
188
197
  var generateDivDialogModal = function (modalDialogId) {
189
- $('<div id="'+ modalDialogId +'" class="dialog-modal"><div class="head"><span class="label">' + options.label + '</span><span class="close" title="Close">X</span></div><div class="list"><ul></ul></div></div>').appendTo('#' + container);
198
+ $('<div id="'+ modalDialogId +'" class="dialog-modal"><div class="head"><span class="label">' + options.label + '</span><span class="close" title="Close">X</span></div><div class="list"><ul></ul></div></div>').appendTo('#' + container);
190
199
 
191
200
  $('#' + modalDialogId + ' > div.head > span.close').click(function() {
192
201
  $('#' + modalDialogId).dialog('close');
193
202
  });
194
-
203
+
195
204
  $('#' + modalDialogId).dialog({
196
205
  width: 500,
197
206
  height: 400,
@@ -199,28 +208,28 @@ var ComboAutoBox = {
199
208
  closeOnEscape: true,
200
209
  autoOpen: false,
201
210
  });
202
-
211
+
203
212
  getListForModalDialog(modalDialogId);
204
-
213
+
205
214
  $("#" + modalDialogId).siblings('div.ui-dialog-titlebar').remove();
206
-
215
+
207
216
  $('#' + container + ' > div.container-combo-auto-box > span.' + options.type).click(function() {
208
217
  openModalDialog(modalDialogId)
209
218
  });
210
219
  };
211
-
220
+
212
221
  // Selects an item form modal dialog when clicked on
213
222
  var selectValueFromModalDialog = function (value) {
214
223
  $('#' + container + ' > div.container-combo-auto-box > input').val(value);
215
224
  selectData(value);
216
225
  };
217
-
226
+
218
227
  // generates list for modal dialog
219
228
  var getListForModalDialog = function (modalDialogId) {
220
229
  if (typeof options.source == 'string') {
221
230
  var term = 'term=';
222
231
  var params = (options.data == null) ? term : options.data + '&' + term;
223
-
232
+
224
233
  $.getJSON(options.source + '?' + params, function(data) {
225
234
  setListForModalDialog(modalDialogId, data);
226
235
  });
@@ -228,23 +237,23 @@ var ComboAutoBox = {
228
237
  setListForModalDialog(modalDialogId, options.source);
229
238
  }
230
239
  };
231
-
240
+
232
241
  // set list for modal dialog
233
242
  var setListForModalDialog = function (modalDialogId, data) {
234
243
  var items = [];
235
-
244
+
236
245
  $.each(data, function(index){
237
246
  items.push('<li><span class="combo-auto-box-item-id">' + data[index].id +'</span><span class="combo-auto-box-item-label">'+ data[index].label + '</span></li>');
238
247
  });
239
-
248
+
240
249
  $('#' + modalDialogId + ' > div.list').css('height', ($('#' + modalDialogId).dialog("option", "height") - 60) + 'px');
241
250
  $('#' + modalDialogId + ' > div.list > ul').html(items.join(''));
242
251
  $('#' + modalDialogId + ' > div.list > ul > li').click(function() {
243
252
  var thisId = $(this).children('span.combo-auto-box-item-id').text();
244
253
  var thisLabel = $(this).children('span.combo-auto-box-item-label').text();
245
-
254
+
246
255
  $('#' + modalDialogId).dialog('close');
247
-
256
+
248
257
  if (options.type == 'simple') {
249
258
  $('#' + container + ' > div.container-combo-auto-box > input').val(thisLabel);
250
259
  selectData(thisId);
@@ -255,108 +264,126 @@ var ComboAutoBox = {
255
264
  }
256
265
  });
257
266
  };
258
-
267
+
259
268
  // opens modal dialog
260
269
  var openModalDialog = function (modalDialogId) {
261
- $('#' + modalDialogId).dialog("open");
270
+ $('#' + modalDialogId).dialog("open");
262
271
  };
263
-
272
+
264
273
  // starting generate modial dialog
265
274
  var generateModalDialog = function (textField) {
266
275
  $(generateExpander()).prependTo('#' + container + ' > div.container-combo-auto-box');
267
-
276
+
268
277
  adjustExpanderImage();
269
-
278
+
270
279
  generateDivDialogModal(generateAnId('model-dialog'));
271
280
  };
272
-
281
+
273
282
  // add item
274
283
  var addItem = function (inputId, selectedId, selectedData) {
275
284
  if (selectedData != '') {
276
285
  var id = generateAnId('item');
277
286
  $('#' + inputId).before('<div class="item" title="Remove Item" id="' + id + '">'+ selectedData +'<span>x</span><input type="hidden" name="'+ options.html.name +'[]" value="'+ selectedId +'"></div>');
278
-
287
+
279
288
  $('#' + id + ' > span').click(function() {
280
289
  $(this).parent().remove();
281
290
  });
282
-
291
+
283
292
  }
284
293
  };
285
-
294
+
286
295
  // add searchable item for ransack
287
296
  var addSearchableItemForRansack = function (inputId, selectedId, selectedData) {
288
297
  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) +'">';
298
+ var ransackId = generateShortId('r');
299
+ predicate = getSearchablePredicate(selectedId);
300
+ if (getSearchableValue(selectedData) == null) {
301
+ selectedData = predicate['full'] + ' ' + selectedData;
302
+ }
303
+ fieldAttribute = '<input type="hidden" name="q[g]['+ predicate['attribute'] +'][c]['+ ransackId +'][a][0][name]" value="'+ predicate['attribute'] +'">';
304
+ fieldCondition = '<input type="hidden" name="q[g]['+ predicate['attribute'] +'][c]['+ ransackId +'][p]" value="'+ predicate['condition'] +'">';
305
+ fieldValue = '<input type="hidden" name="q[g]['+ predicate['attribute'] +'][c]['+ ransackId +'][v][0][value]" value="'+ getSearchableValue(selectedData) +'">';
294
306
  var id = generateAnId('item');
295
307
  $('#' + inputId).before('<div class="item" title="Remove Item" id="' + id + '">'+ selectedData +'<span>x</span>'+ fieldAttribute + fieldCondition + fieldValue +'</div>');
296
-
308
+
297
309
  $('#' + id + ' > span').click(function() {
298
310
  $(this).parent().remove();
299
311
  selectData('');
300
312
  });
301
313
  }
302
314
  };
303
-
315
+
304
316
  // add searchable item
305
317
  var addSearchableItem = function (inputId, selectedId, selectedData) {
306
318
  if (selectedData != '') {
307
319
  var id = generateAnId('item');
308
320
  $('#' + 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
-
321
+
310
322
  $('#' + id + ' > span').click(function() {
311
323
  $(this).parent().remove();
312
324
  selectData('');
313
325
  });
314
-
326
+
315
327
  }
316
328
  };
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
329
 
326
- // get only the condition from selected Data
327
- var getSearchableCondition = function (selectedId) {
330
+ // return json with attribute and condition
331
+ var getSearchablePredicate = function (selectedId) {
328
332
  var fields = $.map(options.source, function(val, i) { return val['id']}).join('|');
329
333
  var maths = $.map(i18nMath(options.lang), function(val, i) { return val['id']}).join('|');
330
334
  var pattern = new RegExp('(' + fields + ')_(' + maths + ')');
331
- return selectedId.match(pattern)[2];
335
+ var matched = selectedId.match(pattern);
336
+ var full = getSearchableLabelForAttributeId(matched[1]) + ' ' + getSearchableLabelForConditionId(matched[2]);
337
+ return { attribute: matched[1], condition: matched[2], full: full };
332
338
  }
333
-
339
+
334
340
  // get only the value from selected Data
335
341
  var getSearchableValue = function (selectedData) {
336
342
  var fields = $.map(options.source, function(val, i) { return val['label']}).join('|');
337
343
  var maths = $.map(i18nMath(options.lang), function(val, i) { return val['label']}).join('|');
338
344
  var pattern = new RegExp('(' + fields + ') (' + maths + ') (.*)');
339
- return selectedData.match(pattern)[3];
345
+ try {
346
+ return selectedData.match(pattern)[3];
347
+ } catch (error) {
348
+ return null;
349
+ }
340
350
  }
341
-
351
+
352
+ var getSearchableLabelForAttributeId = function (attibuteId) {
353
+ for(var i=0; i<options.source.length;i++) {
354
+ if (options.source[i]['id'] == attibuteId) {
355
+ return options.source[i]['label'];
356
+ }
357
+ }
358
+ }
359
+
360
+ var getSearchableLabelForConditionId = function (conditionId) {
361
+ var conditions = i18nMath(options.lang);
362
+ for(var i=0; i<conditions.length;i++) {
363
+ if (conditions[i]['id'] == conditionId) {
364
+ return conditions[i]['label'];
365
+ }
366
+ }
367
+ }
368
+
342
369
  // Bind click on div for multiple or searchble
343
370
  var bindContainerClick = function(inputId) {
344
371
  $('#' + container + ' > div.multiple').click(function() {
345
372
  $('#' + inputId).focus();
346
373
  });
347
-
374
+
348
375
  $('#' + container + ' > div.searchable').click(function() {
349
376
  $('#' + inputId).focus();
350
377
  });
351
378
  };
352
-
379
+
353
380
  // on select data
354
381
  var selectData = function (selectedData) {
355
382
  if (options.complete != null) {
356
383
  options.complete(selectedData);
357
384
  }
358
385
  };
359
-
386
+
360
387
  // valid language or set 'en' as default
361
388
  var validLanguage = function () {
362
389
  var langs = ['math', 'en', 'pt-br', 'pt', 'es', 'fr'];
@@ -365,11 +392,11 @@ var ComboAutoBox = {
365
392
  return true;
366
393
  }
367
394
  }
368
-
395
+
369
396
  options.lang = 'en';
370
397
  return true;
371
398
  };
372
-
399
+
373
400
  // valid language or set 'en' as default
374
401
  var validType = function () {
375
402
  var types = ['simple', 'full', 'multiple', 'searchable'];
@@ -378,16 +405,16 @@ var ComboAutoBox = {
378
405
  return true;
379
406
  }
380
407
  }
381
-
408
+
382
409
  options.type = 'simple';
383
410
  return true;
384
411
  };
385
-
412
+
386
413
  // valid sources for only and except
387
414
  var validSource = function (source) {
388
415
  operators = i18nMath('math');
389
416
  validIndexes = new Array();
390
-
417
+
391
418
  if (((source['only'] != null) && (source['except'] != null)) || ((source['only'] == null) && (source['except'] == null))) {
392
419
  for(var i=0; i<operators.length;i++) {
393
420
  validIndexes.push(i);
@@ -405,30 +432,43 @@ var ComboAutoBox = {
405
432
  }
406
433
  }
407
434
  }
408
-
435
+
409
436
  return validIndexes;
410
437
  }
411
-
438
+
412
439
  // main
413
440
  if (options == null) {
414
441
  options = {};
415
442
  }
416
-
443
+
417
444
  validLanguage();
418
445
  validType();
419
-
446
+
420
447
  $('#' + container).html(generateDivTag());
421
-
448
+
422
449
  textField = $('#' + container + ' > div.container-combo-auto-box > input');
423
450
  bindAutoComplete(textField.attr('id'));
424
-
451
+
425
452
  if ((options.type == 'simple') || (options.type == 'multiple')) {
426
453
  generateModalDialog(textField);
427
454
  }
428
-
455
+
429
456
  if ((options.type == 'multiple') || (options.type == 'searchable')) {
430
457
  bindContainerClick(textField.attr('id'));
431
458
  }
432
- }
459
+
460
+ if ((options.type == 'multiple') && (options.initials != null)) {
461
+ $.each(options.initials, function(index){
462
+ addItem(textField.attr('id'), options.initials[index]['id'], options.initials[index]['label']);
463
+ });
464
+ }
465
+
466
+ if ((options.type == 'searchable') && (options.initials != null)) {
467
+ $.each(options.initials, function(index){
468
+ addSearchableItemForRansack(textField.attr('id'), options.initials[index]['id'], options.initials[index]['label']);
469
+ });
470
+ }
471
+
472
+ }
473
+ }
433
474
 
434
- }
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.6
4
+ version: 0.0.7
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-16 00:00:00.000000000 Z
12
+ date: 2013-07-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport