-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.rails_flexible_select.js
57 lines (49 loc) · 2.19 KB
/
jquery.rails_flexible_select.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Flexible Select - jQuery plugin to add "Create New" link to select element.
//
// https://github.com/coryschires/rails-flexible-select
// Version 0.0.1
//
// Dual licensed under the MIT and GPL licenses:
// http://www.opensource.org/licenses/mit-license.php
// http://www.gnu.org/licenses/gpl.html
//
//
(function($) {
$.flexible_select = {
defaults: {
params_name: "name", // name of the parameters sent to server
button_text: "-- Create New --", // text for the link inside drop select
prompt_text: "Please Enter Name" // what appears in javascript popup box
}
}
$.fn.extend({
flexible_select: function(config) {
var config = $.extend({}, $.flexible_select.defaults, config);
return this.each(function() {
var select = $(this);
var ajax_url = select.attr('flexible_select');
var create_link = function() {
select.find('option:first').after('<option class="create_link">'+config.button_text+'</option>');
return select.find('option.create_link')
}();
var prompt_box = function() {
var input_value = prompt(config.prompt_text)
if (input_value) {
var data = {};
data[config.params_name] = input_value;
$.post( ajax_url, data, function(data) {
var new_option = '<option class="added_via_ajax" value="'+ data.value +'">'+ data.name +'</option>';
create_link.after(new_option).next().attr('selected', 'selected');
});
} else {
select.find('option:first').attr('selected', 'selected');
}
}
select.change(function() {
var clicked_create_link = select.find('option:selected').text() === config.button_text;
if (clicked_create_link) { prompt_box(); }
});
})
}
})
})(jQuery);