2013-02-11 14:18:14 +00:00
|
|
|
(function ($) {
|
|
|
|
$.fn.singleSelect = function () {
|
|
|
|
return this.each(function (i, select) {
|
2013-10-30 19:45:47 +00:00
|
|
|
var input = $('<input/>'),
|
2014-08-13 12:20:14 +00:00
|
|
|
gravity = $(select).attr('data-tipsy-gravity'),
|
2014-03-01 01:46:27 +00:00
|
|
|
inputTooltip = $(select).attr('data-inputtitle');
|
2013-10-30 19:45:47 +00:00
|
|
|
if (inputTooltip){
|
|
|
|
input.attr('title', inputTooltip);
|
|
|
|
}
|
2014-08-12 16:04:52 +00:00
|
|
|
if (typeof gravity === 'undefined') {
|
2017-05-01 03:42:53 +00:00
|
|
|
gravity = 'n';
|
2014-08-12 16:04:52 +00:00
|
|
|
}
|
2013-02-11 14:18:14 +00:00
|
|
|
select = $(select);
|
|
|
|
input.css('position', 'absolute');
|
|
|
|
input.css({
|
|
|
|
'box-sizing': 'border-box',
|
|
|
|
'-moz-box-sizing': 'border-box',
|
|
|
|
'margin': 0,
|
|
|
|
'width': (select.width() - 5) + 'px',
|
|
|
|
'height': (select.outerHeight() - 2) + 'px',
|
|
|
|
'border': 'none',
|
|
|
|
'box-shadow': 'none',
|
|
|
|
'margin-top': '1px',
|
|
|
|
'margin-left': '1px',
|
|
|
|
'z-index': 1000
|
|
|
|
});
|
|
|
|
input.hide();
|
|
|
|
$('body').append(input);
|
|
|
|
|
|
|
|
select.on('change', function (event) {
|
|
|
|
var value = $(this).val(),
|
|
|
|
newAttr = $('option:selected', $(this)).attr('data-new');
|
|
|
|
if (!(typeof newAttr !== 'undefined' && newAttr !== false)) {
|
|
|
|
input.hide();
|
|
|
|
select.data('previous', value);
|
|
|
|
} else {
|
|
|
|
event.stopImmediatePropagation();
|
2013-10-31 09:50:18 +00:00
|
|
|
// adjust offset, in case the user scrolled
|
|
|
|
input.css(select.offset());
|
2013-02-11 14:18:14 +00:00
|
|
|
input.show();
|
2013-10-30 19:45:47 +00:00
|
|
|
if ($.fn.tipsy){
|
2014-08-12 16:04:52 +00:00
|
|
|
input.tipsy({gravity: gravity, trigger: 'manual'});
|
2013-10-30 19:45:47 +00:00
|
|
|
input.tipsy('show');
|
|
|
|
}
|
2013-02-11 14:18:14 +00:00
|
|
|
input.focus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$(select).data('previous', $(select).val());
|
|
|
|
|
|
|
|
input.on('change', function () {
|
|
|
|
var value = $(this).val();
|
|
|
|
if (value) {
|
|
|
|
select.children().attr('selected', null);
|
|
|
|
var existingOption = select.children().filter(function (i, option) {
|
|
|
|
return ($(option).val() == value);
|
|
|
|
});
|
|
|
|
if (existingOption.length) {
|
|
|
|
existingOption.attr('selected', 'selected');
|
|
|
|
} else {
|
|
|
|
var option = $('<option/>');
|
|
|
|
option.attr('selected', 'selected').attr('value', value).text(value);
|
|
|
|
select.children().last().before(option);
|
|
|
|
}
|
|
|
|
select.val(value);
|
|
|
|
select.css('background-color', null);
|
|
|
|
input.val(null);
|
|
|
|
input.hide();
|
|
|
|
select.change();
|
|
|
|
} else {
|
|
|
|
var previous = select.data('previous');
|
|
|
|
select.children().attr('selected', null);
|
|
|
|
select.children().each(function (i, option) {
|
|
|
|
if ($(option).val() == previous) {
|
|
|
|
$(option).attr('selected', 'selected');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
select.removeClass('active');
|
|
|
|
input.hide();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
input.on('blur', function () {
|
|
|
|
$(this).change();
|
2013-10-30 19:45:47 +00:00
|
|
|
if ($.fn.tipsy){
|
|
|
|
$(this).tipsy('hide');
|
|
|
|
}
|
2013-02-11 14:18:14 +00:00
|
|
|
});
|
2014-08-15 10:43:24 +00:00
|
|
|
input.click(function(ev) {
|
|
|
|
// prevent clicks to close any container
|
|
|
|
ev.stopPropagation();
|
|
|
|
});
|
2013-02-11 14:18:14 +00:00
|
|
|
});
|
2014-03-01 01:46:27 +00:00
|
|
|
};
|
2013-02-11 14:18:14 +00:00
|
|
|
})(jQuery);
|