2017-01-12 00:45:54 -05:00
|
|
|
/* eslint-disable func-names, space-before-function-paren, one-var, no-var, one-var-declaration-per-line, object-shorthand, comma-dangle, prefer-arrow-callback, no-else-return, newline-per-chained-call, wrap-iife, max-len */
|
2016-12-13 22:01:05 -05:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
window.CompareAutocomplete = (function() {
|
|
|
|
function CompareAutocomplete() {
|
|
|
|
this.initDropdown();
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
CompareAutocomplete.prototype.initDropdown = function() {
|
|
|
|
return $('.js-compare-dropdown').each(function() {
|
|
|
|
var $dropdown, selected;
|
|
|
|
$dropdown = $(this);
|
|
|
|
selected = $dropdown.data('selected');
|
|
|
|
const $dropdownContainer = $dropdown.closest('.dropdown');
|
|
|
|
const $fieldInput = $(`input[name="${$dropdown.data('field-name')}"]`, $dropdownContainer);
|
|
|
|
const $filterInput = $('input[type="search"]', $dropdownContainer);
|
|
|
|
$dropdown.glDropdown({
|
|
|
|
data: function(term, callback) {
|
|
|
|
return $.ajax({
|
|
|
|
url: $dropdown.data('refs-url'),
|
|
|
|
data: {
|
2017-03-16 08:04:51 -04:00
|
|
|
ref: $dropdown.data('ref'),
|
|
|
|
search: term,
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}).done(function(refs) {
|
|
|
|
return callback(refs);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
selectable: true,
|
|
|
|
filterable: true,
|
2017-03-16 08:04:51 -04:00
|
|
|
filterRemote: true,
|
2017-03-11 02:30:44 -05:00
|
|
|
fieldName: $dropdown.data('field-name'),
|
|
|
|
filterInput: 'input[type="search"]',
|
|
|
|
renderRow: function(ref) {
|
|
|
|
var link;
|
|
|
|
if (ref.header != null) {
|
|
|
|
return $('<li />').addClass('dropdown-header').text(ref.header);
|
|
|
|
} else {
|
|
|
|
link = $('<a />').attr('href', '#').addClass(ref === selected ? 'is-active' : '').text(ref).attr('data-ref', escape(ref));
|
|
|
|
return $('<li />').append(link);
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
},
|
|
|
|
id: function(obj, $el) {
|
|
|
|
return $el.attr('data-ref');
|
|
|
|
},
|
|
|
|
toggleLabel: function(obj, $el) {
|
|
|
|
return $el.text().trim();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$filterInput.on('keyup', (e) => {
|
|
|
|
const keyCode = e.keyCode || e.which;
|
|
|
|
if (keyCode !== 13) return;
|
|
|
|
const text = $filterInput.val();
|
|
|
|
$fieldInput.val(text);
|
|
|
|
$('.dropdown-toggle-text', $dropdown).text(text);
|
|
|
|
$dropdownContainer.removeClass('open');
|
|
|
|
});
|
2016-12-22 07:41:54 -05:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
$dropdownContainer.on('click', '.dropdown-content a', (e) => {
|
|
|
|
$dropdown.prop('title', e.target.text.replace(/_+?/g, '-'));
|
|
|
|
if ($dropdown.hasClass('has-tooltip')) {
|
|
|
|
$dropdown.tooltip('fixTitle');
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2017-03-11 02:30:44 -05:00
|
|
|
});
|
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
return CompareAutocomplete;
|
|
|
|
})();
|