forked from fabiooshiro/jquery-do
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.do.js
114 lines (112 loc) · 3.57 KB
/
jquery.do.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
109
110
111
112
113
114
$(function(){
$("body").append(
'<div id="jqueryDo" title="jQuery Do">' +
'<div>jQuery-do: <input id="jqDoIn" type="text" /></div>' +
'</div>'
);
$( "#jqueryDo" ).dialog({
autoOpen:false,
height: 200,
width: 350
});
var loaded = false;
function doAction(el){
if(el.nodeName=='A' || el.nodeName=='a'){
var r = true;
try{
var r = $(el).triggerHandler('click');
if(r == undefined){
r = true;
}
}catch(e){}
if(r !== false){// se o click nao retornou false, acessamos o href
document.location.href = el.href;
}
}else if(el.nodeName == 'LABEL'){
console.log('label for='+$(el).attr('for'));
$('#' + $(el).attr('for')).focus();
$("#jqueryDo").dialog('close');
}else{
$(el).click();
}
}
function ctrlSpaceHandler(){
// start jQuery do inspirado no gnome-do
if(loaded==false){
loaded = true;
var ls = $.cookie('jqDoClickedLs');
if(ls == null){
ls = new Array();
}else{
ls = ls.split(',');
}
console.log(ls);
var txt = new Array();
$("a").each(function(){
var lbl = $.trim($(this).text());
var item = {label: lbl, value: this};
txt.push(item);
});
$(":button").each(function(){
var lbl = $.trim($(this).val());
var item = {label: lbl, value: this};
txt.push(item);
});
$("input[type='submit']").each(function(){
var lbl = $.trim($(this).val());
var item = {label: lbl, value: this};
txt.push(item);
});
$("label").each(function(){
var lbl = $.trim($(this).text());
var item = {label: lbl, value: this};
txt.push(item);
});
txt = txt.sort(function(a, b){
var pa = ls.indexOf(a.label);
var pb = ls.indexOf(b.label);
return pb - pa;
});
$("#jqDoIn").keyup(function(event){
if(event.keyCode=='13'){
var t = $('.ui-autocomplete > li:first').text();
for(var i=0;i < txt.length;i++){
if(txt[i].label==t){
doAction(txt[i].value);
}
}
}
});
$("#jqDoIn").autocomplete({
source: txt,
focus: function( event, ui ) {
return false; // nao troca o texto do input
},
select: function(event, ui) {
var tmp = new Array();
var lbl = ui.item.label;
for(var i = 0; i<ls.length; i++){
if(ls[i]!=lbl){
tmp.push(ls[i]);
}
}
ls = tmp;
ls.push(lbl);
$.cookie('jqDoClickedLs', ls, {expires: 15});
var el = ui.item.value;
doAction(el);
return false;
}
})
.data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.label + "</a>" )
.appendTo( ul );
};
}
$("#jqueryDo").dialog('open');
}
$(window).bind('keydown', 'ctrl+space', ctrlSpaceHandler);
$("input[type=text]").bind('keydown', 'ctrl+space', ctrlSpaceHandler);
});