combo_auto_box 0.0.24 → 0.0.25

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.24'
2
+ VERSION = '0.0.25'
3
3
  end
@@ -31,7 +31,12 @@ var ComboAutoBox = {
31
31
  } else if (options.type == 'searchable') {
32
32
  removeLastSearchableItemForRansack();
33
33
  }
34
+ } else if ((e.keyCode == 9) && ($('#' + inputId).val() != '') && (options.type == 'simple') && (options.source_not_found)) {
35
+ $('#' + inputId).autocomplete( "close");
36
+ selectData($('#' + inputId).val(), $('#' + inputId).val());
37
+ return true;
34
38
  }
39
+
35
40
  });
36
41
 
37
42
  $('#' + inputId).keypress(function(e) {
@@ -39,6 +44,9 @@ var ComboAutoBox = {
39
44
  if (options.type == 'full') {
40
45
  $('#' + inputId).autocomplete( "close" );
41
46
  selectData($('#' + inputId).val(), $('#' + inputId).val());
47
+ } else if ((options.type == 'simple') && (options.source_not_found)) {
48
+ $('#' + inputId).autocomplete( "close");
49
+ selectData($('#' + inputId).val(), $('#' + inputId).val());
42
50
  } else if (options.type == 'multiple') {
43
51
  $('#' + inputId).autocomplete( "close" );
44
52
  addMultipleItem(inputId, $('#' + inputId).val(), $('#' + inputId).val());
@@ -65,7 +73,10 @@ var ComboAutoBox = {
65
73
  $('#' + inputId).autocomplete({
66
74
  source: setAutoCompleteSource(inputId),
67
75
  select: function(event, ui) {
68
- if (options.type == 'simple') {
76
+ if ((options.type == 'simple') && (options.source_not_found)) {
77
+ selectData($('#' + inputId).val(), $('#' + inputId).val());
78
+ return false;
79
+ } else if (options.type == 'simple') {
69
80
  return selectData(ui.item.id, ui.item.label);
70
81
  } else if (options.type == 'full') {
71
82
  return selectData($('#' + inputId).val(), $('#' + inputId).val());
@@ -87,8 +98,9 @@ var ComboAutoBox = {
87
98
  }
88
99
  },
89
100
  change: function (event, ui) {
90
- if (!ui.item) {
101
+ if ((!ui.item) && (!options.source_not_found)) {
91
102
  $(this).val('');
103
+ selectData('', '');
92
104
  }
93
105
  },
94
106
  focus: function (event, ui) {
@@ -101,16 +113,48 @@ var ComboAutoBox = {
101
113
  var setAutoCompleteSource = function (inputId) {
102
114
  if (options.type == 'searchable') {
103
115
  return sourceForSearchable(inputId);
104
- } else if (typeof options.source == 'string') {
105
- return function(request, response) {
106
- var term = 'term=' + $('#' + inputId).val();
107
- var params = (options.data == null) ? term : options.data + '&' + term;
108
- return $.getJSON(options.source + '?' + params, response);
109
- };
110
116
  } else {
111
- return options.source;
117
+ return function(request, response) {
118
+ if (typeof options.source == 'string') {
119
+ var term = 'term=' + $('#' + inputId).val();
120
+ var params = (options.data == null) ? term : options.data + '&' + term;
121
+ $.getJSON(options.source + '?' + params, function(data) {
122
+ if ((options.type == 'simple') && (data.length == 0)) {
123
+ return response(sourceForNotFound(inputId));
124
+ } else {
125
+ options.source_not_found = false;
126
+ response($.map(data, function (item) {
127
+ return item;
128
+ }));
129
+ }
130
+ });
131
+ } else {
132
+ var selectedSource = new Array();
133
+ $.each(options.source, function( index, value ){
134
+ var pattern = new RegExp($('#' + inputId).val(),'i');
135
+ if (value.label.match(pattern)) {
136
+ selectedSource.push(value);
137
+ }
138
+ });
139
+
140
+ if ((options.type == 'simple') && (selectedSource.length == 0)) {
141
+ return response(sourceForNotFound(inputId));
142
+ } else {
143
+ options.source_not_found = false;
144
+ return response(selectedSource);
145
+ }
146
+
147
+
148
+ }
149
+ }
112
150
  }
113
151
  };
152
+
153
+ // source items for not found item
154
+ var sourceForNotFound = function (inputId) {
155
+ options.source_not_found = true;
156
+ return [ { id: $('#' + inputId).val(), label: '"' + $('#' + inputId).val() + '" ' + options.not_found_message } ];
157
+ }
114
158
 
115
159
  // source items for searchable
116
160
  var sourceForSearchable = function (inputId) {
@@ -617,11 +661,23 @@ var ComboAutoBox = {
617
661
  });
618
662
  }
619
663
  }
664
+
665
+ var getSourceNotFound = function(value) {
666
+ var pattern = new RegExp('^"(' + value + ')"', 'i');
667
+ var matched = value.match(pattern);
668
+ return matched[1];
669
+ }
620
670
 
621
671
  // main
622
672
  if (options == null) {
623
673
  options = {};
624
674
  }
675
+
676
+ options.source_not_found = false;
677
+
678
+ if (options.not_found_message == null) {
679
+ options.not_found_message = "was not found";
680
+ }
625
681
 
626
682
  validLanguage();
627
683
  validType();
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.24
4
+ version: 0.0.25
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-12-13 00:00:00.000000000 Z
12
+ date: 2014-05-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec