combo_auto_box 0.0.1 → 0.0.2

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.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -1,7 +1,7 @@
1
1
  var ComboAutoBox = {
2
2
  // constructor
3
3
  addTo: function (container, options) {
4
-
4
+
5
5
  // generatea an ID based on current time
6
6
  var generateAnId = function(prefix) {
7
7
  var now = new Date().getTime();;
@@ -25,11 +25,7 @@ var ComboAutoBox = {
25
25
  });
26
26
 
27
27
  $('#' + inputId).autocomplete({
28
- source: function(request, response) {
29
- var term = 'term=' + $('#' + inputId).val();
30
- var params = (options.data == null) ? term : options.data + '&' + term;
31
- return $.getJSON(options.source + '?' + params, response);
32
- },
28
+ source: setAutoCompleteSource(inputId),
33
29
  select: function(event, ui) {
34
30
  if (options.type == 'simple') {
35
31
  return selectData(ui.item.id);
@@ -40,6 +36,19 @@ var ComboAutoBox = {
40
36
  });
41
37
  };
42
38
 
39
+ // set autocomplete source
40
+ var setAutoCompleteSource = function (inputId) {
41
+ if (typeof options.source == 'string') {
42
+ return function(request, response) {
43
+ var term = 'term=' + $('#' + inputId).val();
44
+ var params = (options.data == null) ? term : options.data + '&' + term;
45
+ return $.getJSON(options.source + '?' + params, response);
46
+ };
47
+ } else {
48
+ return options.source;
49
+ }
50
+ };
51
+
43
52
  // generates text field with html options
44
53
  var generateInputTag = function () {
45
54
  var html = 'input type="text"';
@@ -74,8 +83,6 @@ var ComboAutoBox = {
74
83
  $('#' + modalDialogId).dialog('close');
75
84
  });
76
85
 
77
- getListForModalDialog(modalDialogId);
78
-
79
86
  $('#' + modalDialogId).dialog({
80
87
  width: 400,
81
88
  height: 400,
@@ -83,6 +90,8 @@ var ComboAutoBox = {
83
90
  closeOnEscape: true,
84
91
  autoOpen: false,
85
92
  });
93
+
94
+ getListForModalDialog(modalDialogId);
86
95
 
87
96
  $("#" + modalDialogId).siblings('div.ui-dialog-titlebar').remove();
88
97
 
@@ -99,22 +108,33 @@ var ComboAutoBox = {
99
108
 
100
109
  // generates list for modal dialog
101
110
  var getListForModalDialog = function (modalDialogId) {
102
- var term = 'term=';
103
- var params = (options.data == null) ? term : options.data + '&' + term;
104
- var items = [];
111
+ if (typeof options.source == 'string') {
112
+ var term = 'term=';
113
+ var params = (options.data == null) ? term : options.data + '&' + term;
105
114
 
106
- $.getJSON(options.source + '?' + params, function(data) {
107
- $.each(data, function(index){
108
- 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>');
109
- });
110
- $('#' + modalDialogId + ' > div.list').css('height', ($('#' + modalDialogId).dialog("option", "height") - 65) + 'px');
111
- $('#' + modalDialogId + ' > div.list > ul').html(items.join(''));
112
- $('#' + modalDialogId + ' > div.list > ul > li').click(function() {
113
- $('#' + modalDialogId).dialog('close');
114
- $('#' + container + ' > div.container-combo-auto-box > input').val($(this).children('span.combo-auto-box-item-label').text());
115
- selectData($(this).children('span.combo-auto-box-item-id').text());
115
+ $.getJSON(options.source + '?' + params, function(data) {
116
+ setListForModalDialog(modalDialogId, data);
116
117
  });
118
+ } else {
119
+ setListForModalDialog(modalDialogId, options.source);
120
+ }
121
+ };
122
+
123
+ // set list for modal dialog
124
+ var setListForModalDialog = function (modalDialogId, data) {
125
+ var items = [];
126
+
127
+ $.each(data, function(index){
128
+ 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>');
117
129
  });
130
+
131
+ $('#' + modalDialogId + ' > div.list').css('height', ($('#' + modalDialogId).dialog("option", "height") - 65) + 'px');
132
+ $('#' + modalDialogId + ' > div.list > ul').html(items.join(''));
133
+ $('#' + modalDialogId + ' > div.list > ul > li').click(function() {
134
+ $('#' + modalDialogId).dialog('close');
135
+ $('#' + container + ' > div.container-combo-auto-box > input').val($(this).children('span.combo-auto-box-item-label').text());
136
+ selectData($(this).children('span.combo-auto-box-item-id').text());
137
+ });
118
138
  };
119
139
 
120
140
  // opens modal dialog
@@ -130,14 +150,14 @@ var ComboAutoBox = {
130
150
  spanTag.css('margin', '2px 0px 0px ' + (textField.width() + 9).toString() + 'px');
131
151
 
132
152
  generateDivDialogModal(generateAnId('model-dialog'));
133
- }
153
+ };
134
154
 
135
155
  // on select data
136
156
  var selectData = function (selectedData) {
137
157
  if (options.complete != null) {
138
158
  options.complete(selectedData);
139
159
  }
140
- }
160
+ };
141
161
 
142
162
  // main
143
163
  if (options == null) {
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.1
4
+ version: 0.0.2
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-05-24 00:00:00.000000000 Z
12
+ date: 2013-05-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport