forked from SiamAbdullah/Q2ATagSuggesion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomtagsuggestion.js
144 lines (122 loc) · 4.57 KB
/
customtagsuggestion.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var qa_tags_max = 5;
var qa_tag_template = '<li onmouseover="mouseMoveOnSuggestion(this)"><a href="#" onclick="return qa_suggession_tag_click(this);">^</a></li>';
var previousText='';
var qa_tag_hints = qa_custom_tag_hints;
var tagSuggestionId = '#tag_hints';
$.fn.textWidth = function(text, font) {
if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>').hide().appendTo(document.body);
$.fn.textWidth.fakeEl.text(text || this.val() || this.text()).css('font', font || this.css('font'));
return $.fn.textWidth.fakeEl.width();
};
function qa_custom_tag_hints(skipcomplete)
{
var elem=document.getElementById('tags');
if(elem.value === previousText) return;
previousText = elem.value;
var html='';
var completed=false;
// qa_tag_template='^';
// first try to auto-complete
if (!skipcomplete) {
var parts=qa_tag_typed_parts(elem);
if (parts.typed) {
html=qa_tags_to_html((qa_html_unescape(qa_tags_complete)).split(','), parts.typed.toLowerCase());
completed=html ? true : false;
}
}
if(!completed){
$(tagSuggestionId).css("display", "none");
$(tagSuggestionId).html('');
}else{
if($(tagSuggestionId).css("display") === "none"){
var widthPercentUnit = 100/ $(".qa-part-form").width();
var left = 3 + Math.ceil($.fn.textWidth($('#tags').val(), $('#tags').css('font-size') + ' Arial') * widthPercentUnit);
$(tagSuggestionId).css("left", left + "%");
}
$(tagSuggestionId).css("display", "block");
html = '<ul class="tag-suggestion" >' + html + '</ul>';
$(tagSuggestionId).html(html);
$('.tag-suggestion li').eq(0).addClass('selected');
}
}
function qa_suggession_tag_click(link)
{
var elem=document.getElementById('tags');
var parts=qa_tag_typed_parts(elem);
// removes any HTML tags and ampersand
var tag=qa_html_unescape(link.innerHTML.replace(/<[^>]*>/g, ''));
var separator=qa_tag_onlycomma ? ', ' : ' ';
// replace if matches typed, otherwise append
var newvalue=(parts.typed && (tag.toLowerCase().indexOf(parts.typed.toLowerCase())>=0))
? (parts.before+separator+tag+separator+parts.after+separator) : (elem.value+separator+tag+separator);
// sanitize and set value
if (qa_tag_onlycomma)
elem.value=newvalue.replace(/[\s,]*,[\s,]*/g, ', ').replace(/^[\s,]+/g, '');
else
elem.value=newvalue.replace(/[\s,]+/g, ' ').replace(/^[\s,]+/g, '');
//elem.focus();
hideSuggestionBox();
return false;
}
function overwriteUpDownKeyForTag(){
$('#tags').bind('keyup', function(e){
var liSelected = $('.tag-suggestion li.selected');
var li = $('.tag-suggestion li');
if(e.which === 40){
$('.tag-suggestion').focus();
if(liSelected && liSelected.length > 0){
liSelected.removeClass('selected');
var next = li.index(liSelected[0]) + 1;
if(next < li.length){
liSelected = li.eq(next).addClass('selected');
}else{
liSelected = li.eq(0).addClass('selected');
}
}else{
liSelected = li.eq(0).addClass('selected');
}
}else if(e.which === 38){
if(liSelected){
liSelected.removeClass('selected');
next = liSelected.prev();
if(next.length > 0){
liSelected = next.addClass('selected');
}else{
liSelected = li.last().addClass('selected');
}
}else{
liSelected = li.last().addClass('selected');
}
}else if(e.which === 27 ){
hideSuggestionBox();
}
else if(e.which === 13){
var tagLink = $('.tag-suggestion li.selected a');
if(tagLink){
qa_suggession_tag_click(tagLink[0]);
hideSuggestionBox();
}
}
});
$('#tags').bind('keydown', function(e){
if(e.which === 13){
e.preventDefault();
}
});
}
$('#tags').ready( function(){
overwriteUpDownKeyForTag();
});
function hideSuggestionBox(){
$(tagSuggestionId).css("display", "none");
$(tagSuggestionId).html('');
}
function mouseMoveOnSuggestion(e){
var liSelected = $('.tag-suggestion li.selected');
var li = $('.tag-suggestion li');
var next = li.index(e) ;
if(next > -1){
liSelected.removeClass('selected');
liSelected = li.eq(next).addClass('selected');
}
}