-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.shared.options.js
87 lines (78 loc) · 3.2 KB
/
jquery.shared.options.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
(function( $ ){
$.fn.sharedOptions = function() {
var elements = $(this);
var allOptions = [];
var $optGroups = elements.first().find('optgroup');
elements.first().find('option').each(function(idx, option) {
var $option = $(option);
var optionValue = $option.attr('value');
if ( optionValue ) {
allOptions[optionValue] = {'label': $option.text(), 'beforeValue': $option.prev().attr('value'), 'index': idx };
if ( $optGroups.length ) {
allOptions[optionValue].optgroup = $optGroups.index($option.parent('optgroup'));
}
}
});
//add change handler to all dropdowns
elements.change(function(){
var $this = $(this); //the dropdown that changed
var selectedOption = $this.find('option:selected');
var newValue = selectedOption.attr('value');
var oldValue = $this.data('old-value');
//nothing to change
if ( newValue === oldValue ) {
return true;
}
if ( oldValue && $.trim(oldValue).length !== 0 ) {
elements.not($this).each(function(junk, select) {
var myIndex = allOptions[oldValue].index;
var $select = $(select);
var $parent = $optGroups.length > 0 ?
$select.find('optgroup').eq(allOptions[oldValue].optgroup) : $select;
var $options = $parent.find('option');
$options.each(function(ignore, option) {
var $option = $(option);
var optionValue = $option.attr('value');
if ( !optionValue || $.trim(optionValue).length === 0 ) {
return true;
}
var $nextOption = $option.next('option');
var curIndex = allOptions[optionValue].index;
var nextOptionValue;
var nextIndex;
if ( $nextOption.length ) {
nextOptionValue = $nextOption.attr('value');
if ( nextOptionValue && $.trim(nextOptionValue).length !== 0 ) {
nextIndex = allOptions[nextOptionValue].index;
}
}
if ( myIndex < curIndex ) {
$option.before('<option value="' + oldValue + '">' + allOptions[oldValue].label + '</option>');
return false;
} else if ( !nextIndex || myIndex < nextIndex ) {
$option.after('<option value="' + oldValue + '">' + allOptions[oldValue].label + '</option>');
return false;
}
});
if ( !$options.length ) {
$parent.append('<option value="' + oldValue + '">' + allOptions[oldValue].label + '</option>');
}
});
}
if ( newValue && $.trim(newValue).length !== 0 ) {
elements.not($this).find('option[value=' + newValue + ']').remove();
}
$this.data('old-value', newValue);
});
//remove selected options
return elements.each(function(){
var $this = $(this);
var $selected = $this.find('option:selected');
var value = $selected.attr('value');
if ( value && $.trim(value).length !== 0 ) {
$this.data('old-value', value);
elements.not($this).find('option[value=' + value + ']').remove();
}
});
};
})( jQuery );