-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.sticky-select.js
More file actions
110 lines (106 loc) · 2.13 KB
/
jquery.sticky-select.js
File metadata and controls
110 lines (106 loc) · 2.13 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*!
* jQuery DG Magnet Combo 1.3
* http://www.ablemindworks.com/
*
* Uses and works with jQuery.js
* http://jquery.com/
* Copyright 2011, DiGiTSS
* Released under the MIT, BSD, and GPL Licenses.
*
* Initial Release Date: Mon Jan 26 2011
* Last Updated Date: Tue Jan 13 2026
*/
// TODO: Make it work with Shift key naturally
jQuery.fn.extend({
stickySelect: function()
{
$.each($(this), function(){
$(this).makeSelectSticky();
});
}
,
makeSelectSticky: function()
{
var bControlPress; // Maintain Control state
var dataKey = "data_" + $(this).attr("id");
$(this).data("amwVal", $(this).val() || []); // Set selected values on load if any (reported by fflavio)
$(this).click( function()
{
var sTop = this.scrollTop;
var oVal = $(this).data("amwVal") || [];
var nVal = $(this).val();
if(bControlPress != true)
{
if(nVal.length > 0)
{
var cValIndex = $.inArray(nVal[0],oVal);
if(cValIndex > -1)
{
oVal.splice(cValIndex,1);
}
else
{
oVal.push(nVal[0]);
}
}
$(this).data("amwVal",oVal).val(oVal);
}
else
{ // This makes it work with CTL key
if(oVal.length > 0)
{
$.each(oVal, function(index, value){
if($.inArray(value, nVal) < 0)
{
oVal.splice(index, 1);
}
});
$(this).data("amwVal",oVal);
}
}
this.scrollTop = sTop;
return false;
});
$(this).keydown(function(e)
{
if(e.which == 17 || e.which == 16 || e.which == 18) // CTL, Shift Key check
{
bControlPress = true;
}
return true;
});
$(this).keyup( function(e)
{
var sTop = this.scrollTop;
if(e.which == 17 || e.which == 16 || e.which == 18) // CTL, Shift Key check
{
var oVal = $(this).data(dataKey) || [];
if(oVal.length > 0)
{
$.each($(this).val(), function(index, value)
{
if($.inArray(value, oVal) < 0)
{
oVal.push(value);
}
});
$(this).val(oVal);
}
bControlPress = false;
}
else
{
if(!(e.which >= 37 && e.which <= 40)) // TODO: Make Up and Down arrow work
{
//$(this).click();
}
else
{
$(this).val($(this).data(dataKey));
}
}
this.scrollTop = sTop;
return true;
});
}
});