This repository has been archived by the owner on Jan 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery-popup-selecting.js
108 lines (99 loc) · 3.14 KB
/
jquery-popup-selecting.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
[jquery popup selecting]
ver : 1.0.0
author : Andriy Sokolovskiy ([email protected])
*/
(function($) {
"use strict";
$.fn.popselect = function(userOptions) {
var that = this;
var settings = $.extend({
showKey : 32,
leftKey : 37,
rightKey : 39,
activateKey : 13,
speed : 200,
doActivate : undefined,
useShowKey : false
}, userOptions);
var selectedNum = 0;
var childCount = that.children().length;
// move function
var elementMove = function(direct) {
switch (direct) {
case 'right':
selectedNum += 1;
break;
case 'left':
selectedNum -= 1;
break;
}
if (childCount === selectedNum) {
selectedNum = childCount - 1;
}
if (selectedNum < 0) {
selectedNum = 0;
}
that.children().removeClass('selected').each(function(i,e) {
if(i === selectedNum) {
$(e).addClass('selected');
}
});
};
// close menu if click was outside
$('html').click(function() {
that.hide();
});
that.click(function(event) {
event.stopPropagation();
});
// first selected as default
that.children(":first").addClass('selected');
// selecting by mouse
that.children().each(function(i,e) {
$(e).on('mouseover', function() {
that.children().removeClass('selected');
$(e).addClass('selected');
selectedNum = i;
});
$(e).click(function() {
if(typeof settings.doActivate !== "undefined") {
settings.doActivate();
}
});
});
// selecting by keyboard
$(document).on('keyup', function(e) {
var isVisible = that.is(":visible");
switch (e.which) {
case settings.showKey:
if (settings.useShowKey) {
if (!isVisible) {
that.show(settings.speed);
}
else {
that.hide(settings.speed);
}
}
break;
case settings.rightKey:
if (isVisible) {
elementMove('right');
}
break;
case settings.leftKey:
if (isVisible) {
elementMove('left');
}
break;
case settings.activateKey:
if (isVisible) {
if (typeof settings.doActivate !== "undefined") {
settings.doActivate();
}
}
break;
}
});
};
})(jQuery);