Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added show_values options and onchange event #124

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/gm.rpc/nbproject/private/
/sf-page-ref/nbproject/private/
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,29 @@ Options
* input_min_size - minimum size of the input element (default: 1)
* input_name - value of the input element's 'name'-attribute (no 'name'-attribute set if empty)
* select_all_text - text for select all link
* show_values - show values instead of titles (default: false)
* onchange - fire event on value change
* search_mode - where to search on text input (default: titles, possible values: titles, values, all)

Changelog
---------
- 2.8.9.3.a4 fixed highlighting

- 2.8.9.3.a3 added search_mode option

- 2.8.9.3.a2 added onchange event to fire after changes to SELECT

- 2.8.9.3.a1 added show_values option

- 2.8.9.3 input auto expand fixed, font-size dependency added

- 2.8.9.2 Merge pull request #94 from mysmallidea, Merge pull request #98 from musketyr, firefox undefined name fixed (by xavierp)

- 2.8.9.1 new options added input_min_size, select_all_text. The input_min_size added by @meteozond and @Александр, and select_all_text option added by @musketyr. feed clear fix added

- 2.8.9 cache mechanizm changed (ported from @CatoTH fork with minor changes), new event added "oncreate" by @jrencz
- 2.8.8 added "width" paramater, added 109fd92, 5f4f529, ee59f2a fixes from @CatoTH fork

- 2.8.8 added "width" paramater, added 109fd92, 5f4f529, ee59f2a fixes from @CatoTH fork

- 2.8.7 addItem fix when value is not a string by @meltix
new option added (filter_begin) enable filtration from begining
Expand All @@ -89,18 +100,18 @@ Changelog
- 2.8.5 cache object fix (case sensitive) by @ketwaroo

- 2.8.4 cache object fix by @tedberg

- 2.8.3 no more eval use
public function addItem and removeItem fix (thanks to Yaron)

- 2.8.2 json_cache bug fix
new option added "bricket"
newel bug fix thanks to Matt

- 2.8.1 some minor bug fixes
added selected attribute to preselected option thanks to @musketyr
fixed cache entry with space thanks to Matt

- 2.8.0 bug fixes
added jquery 1.6 support please note that old versions of jquery not supported
cache mechanizm updated
Expand All @@ -113,7 +124,7 @@ Changelog
addItem minor fix

- 2.7.3 event call fixed thanks to William Parry <williamparry!at!gmail.com>

- 2.7.2 some minor bug fixed
minified version recompacted due some problems

Expand Down
101 changes: 61 additions & 40 deletions jquery.fcbkcomplete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
FCBKcomplete v2.8.9.3 is released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
FCBKcomplete v2.8.9.3.a4 is released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
- Jquery version required: 1.6.x
*/

Expand Down Expand Up @@ -30,6 +30,9 @@
* input_min_size - minimum size of the input element (default: 1)
* input_name - value of the input element's 'name'-attribute (no 'name'-attribute set if empty)
* select_all_text - text for select all link
* show_values - show values instead of titles (default: false)
* onchange - fire event on value change
* search_mode - where to search on text input (default: titles, possible values: titles, values, all)
*/

(function( $, undefined ) {
Expand Down Expand Up @@ -74,7 +77,7 @@
}

var temp_elem = $('<'+element.get(0).tagName+' name="'+name+'" id="'+elemid+'" multiple="multiple" class="' + element.get(0).className + ' hidden">').data('cache', {});

$.each(element.children('option'), function(i, option) {
option = $(option);
temp_elem.data('cache')[option.val()] = option.text();
Expand All @@ -83,31 +86,35 @@
temp_elem.append('<option value="'+option.val()+'" selected="selected" id="opt_'+id+'"class="selected">'+option.text()+'</option>');
}
});

element.after(temp_elem);
element.remove();
element = temp_elem;

//public method to add new item
$(element).bind("addItem", function(event, data) {
addItem(data.title, data.value, 0, 0, 0);
});


$(element).bind("change", function(event, data) {
if(options.onchange) options.onchange.call(options.onchange);
});

//public method to remove item
$(element).bind("removeItem", function(event, data) {
var item = holder.children('li[rel=' + data.value + ']');
if (item.length) {
removeItem(item);
}
});

//public method to remove item
$(element).bind("destroy", function(event, data) {
holder.remove();
complete.remove();
element.show();
});

//public method to select all items
$(element).bind("selectAll", function(event, data) {
var currVals = $(element).val() || [];
Expand All @@ -118,16 +125,16 @@
});
feed.parent().hide()
});

}

function addItem(title, value, preadded, locked, focusme) {
if (!maxItems()) {
return false;
}
var liclass = "bit-box" + (locked ? " locked": "");
var id = randomId();
var txt = document.createTextNode(xssDisplay(title));
var txt = document.createTextNode(options.show_values ? xssDisplay(value) : xssDisplay(title));
var aclose = $('<a class="closebutton" href="#"></a>');
var li = $('<li class="'+liclass+'" rel="'+value+'" id="pt_'+id+'"></li>').prepend(txt).append(aclose);

Expand Down Expand Up @@ -185,7 +192,7 @@
complete.fadeIn("fast");
}
});

input.blur( function() {
isactive = false;
if (complete_hover) {
Expand All @@ -194,7 +201,7 @@
input.focus();
}
});

holder.click( function() {
if (options.input_min_size < 0 && feed.length) {
load_feed(xssPrevent(input.val(), 1));
Expand All @@ -207,7 +214,7 @@
complete.children(".default").show();
}
});

input.keypress( function(event) {
if (event.keyCode == _key.enter) {
return false;
Expand All @@ -220,7 +227,7 @@
input.keyup( function(event) {

var etext = xssPrevent(input.val(), 1);

if (event.keyCode == _key.backspace && etext.length == 0) {
clear_feed(1);
if (!holder.children("li.bit-box:last").hasClass('locked')) {
Expand All @@ -240,7 +247,7 @@
}
}

if (event.keyCode != _key.downarrow && event.keyCode != _key.uparrow && event.keyCode!= _key.leftarrow && event.keyCode!= _key.rightarrow && etext.length > options.input_min_size) {
if (event.keyCode != _key.downarrow && event.keyCode != _key.uparrow && event.keyCode!= _key.leftarrow && event.keyCode!= _key.rightarrow && etext.length >= options.input_min_size) {
load_feed(etext);
complete.children(".default").hide();
feed.show();
Expand Down Expand Up @@ -286,7 +293,7 @@
focuson = feed.children("li:visible:first");
focuson.addClass("auto-focus");
}

if (counter > options.height) {
feed.css({
"height": (options.height * 24) + "px",
Expand All @@ -295,16 +302,15 @@
} else {
feed.css("height", "auto");
}

if (maxItems() && complete.is(':hidden')) {
complete.show();
}
}

function itemIllumination(text, etext) {
var string_regex_adder = options.filter_begin ? '': '(.*)';
var regex_result = options.filter_begin ? '<em>$1</em>$2' : '$1<em>$2</em>$3';
var string_regex = string_regex_adder + (options.filter_case ? "(" + etext + ")(.*)" : "(" + etext.toLowerCase() + ")(.*)");
var regex_result = '<em>$1</em>$2';
var string_regex = (options.filter_case ? "(" + etext + ")(.*)" : "(" + etext.toLowerCase() + ")(.*)");
try {
var regex = new RegExp(string_regex, ((options.filter_case) ? "g":"gi"));
var text = text.replace(regex, regex_result);
Expand Down Expand Up @@ -334,14 +340,14 @@
function bindEvents() {
var maininput = $("#" + elemid + "_annoninput").children(".maininput");
bindFeedEvent();

feed.children("li").unbind("mousedown").mousedown( function() {
var option = $(this);
addItem(option.text(), option.attr("rel"), 0, 0, 1);
clear_feed(1);
complete.hide();
});

maininput.unbind("keydown");
maininput.keydown( function(event) {
if (event.keyCode != _key.backspace) {
Expand Down Expand Up @@ -376,7 +382,7 @@
}
});
}

function nextItem(position) {
removeFeedEvent();
if (focuson == null || focuson.length == 0) {
Expand All @@ -394,7 +400,7 @@
feed.children("li").removeClass("auto-focus");
focuson.addClass("auto-focus");
}

function _preventDefault(event) {
complete.hide();
event.preventDefault();
Expand Down Expand Up @@ -435,7 +441,7 @@
}
return true;
}

function xssPrevent(string, flag) {
if (typeof flag != "undefined") {
for(i = 0; i < string.length; i++) {
Expand All @@ -450,7 +456,7 @@
}
return string.replace(/script(.*)/g, "");
}

function xssDisplay(string, flag) {
string = string.toString();
string = string.replace('\\', "");
Expand All @@ -459,7 +465,7 @@
}
return unescape(string);
}

function clear_feed(flag) {
feed.children().remove();
if (flag) {
Expand Down Expand Up @@ -516,7 +522,10 @@
input_tabindex: 0,
input_min_size: 1,
input_name: "",
bricket: true
bricket: true,
show_values: false,
onchange: null,
search_mode: 'titles'
},
opt);

Expand All @@ -525,7 +534,7 @@
var feed = null;
var complete = null;
var counter = 0;

var isactive = false;
var focuson = null;
var deleting = 0;
Expand All @@ -534,7 +543,7 @@
var element = $(this);
var elemid = element.attr("id");
var getBoxTimeout = 0;

var json_cache_object = {
'set': function (id, val) {
var data = element.data("jsoncache");
Expand All @@ -548,7 +557,7 @@
element.data("jsoncache", {});
}
};

var _key = { 'enter': 13,
'tab': 9,
'comma': 188,
Expand All @@ -564,7 +573,7 @@
'squarebricket_left': 91,
'apostrof': 96
};

var randomId = function() {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var randomstring = '';
Expand All @@ -574,17 +583,29 @@
}
return randomstring;
};

var cache = {
'search': function (text, callback) {
var temp = new Array();
var regex = new RegExp((options.filter_begin ? '^' : '') + text, (options.filter_case ? "g": "gi"));
var ok
$.each(element.data("cache"), function (i, _elem) {
if (typeof _elem.search === 'function') {
if (_elem.search(regex) != -1) {
temp.push({'key': i, 'value': _elem});
ok = false
switch(options.search_mode) {
case 'titles':
if(typeof _elem.search === 'function' && _elem.search(regex) != -1) ok = true
break
case 'values':
if(typeof i.search === 'function' && i.search(regex) != -1) ok = true
break
case 'all':
if(typeof _elem.search === 'function' && _elem.search(regex) != -1)
ok = true
else if(typeof i.search === 'function' && i.search(regex) != -1)
ok = true
break
}
}
if(ok) temp.push({'key': i, 'value': _elem})
});
return temp;
},
Expand Down Expand Up @@ -616,14 +637,14 @@
}
}
};

//initialization
init();

//cache initialization
json_cache_object.init();
cache.init();

return this;
});
};
Expand Down
Loading