Commit b63e82a8 authored by Marc Egger's avatar Marc Egger
Browse files

add static list to normal typeahead. not tested yet

parent 1c7542d4
Pipeline #3299 failed with stages
in 1 minute and 17 seconds
......@@ -182,7 +182,7 @@ var QfqNS = QfqNS || {};
suggestion: function (obj) {
return "<div>" + n.TypeAhead.htmlEncode(obj.value) + "</div>";
},
// No message if field is not set to pedantic.
// only show not found for static list if API is not given.
notFound: bloodhoundConfiguration === null ? notFound : undefined
}
}
......@@ -240,8 +240,8 @@ var QfqNS = QfqNS || {};
n.TypeAhead.installWithoutTags = function (typeahead_endpoint, $element, bloodhoundConfiguration) {
var $shadowElement;
// Prefetch the value that is already in the field
if ($element.val() !== '') {
// Prefetch
if ($element.val() !== '' && bloodhoundConfiguration !== null) {
bloodhoundConfiguration.prefetch = {};
bloodhoundConfiguration.prefetch.url = n.TypeAhead.makePrefetchUrl(typeahead_endpoint, $element.val(), $element);
// Disable cache, we expect only a few entries. Caching gives sometimes strange behavior.
......@@ -251,26 +251,65 @@ var QfqNS = QfqNS || {};
// create a shadow element with the same value. This seems to be important for the pedantic mode. (?)
$shadowElement = n.TypeAhead.makeShadowElement($element);
// prefetch data
var suggestions = new Bloodhound(bloodhoundConfiguration);
var promise = suggestions.initialize();
// get static list of tags
var staticList = $element.data('typeahead-tag-static-list');
staticList = staticList !== undefined && staticList !== '' ? staticList : [];
// use shadow element to back fill field value, if it is in the fetched suggestions (why?)
promise.done((function ($element, suggestions) {
return function () {
n.TypeAhead.fillTypeAheadFromShadowElement($element, suggestions);
};
})($element, suggestions));
// get key from field value and fetch value from staticList (and later from API if SIP given)
var filledFromStaticList = n.TypeAhead.fillTypeAheadFromShadowElementStaticList($element, staticList);
// list of current typeahead suggestions
var typeaheadList = [];
var typeaheadListApi = [];
var typeaheadListStatic = [];
var notFound = (function ($_) {
return function (obj) {
if (!!$element.data('typeahead-pedantic'))
return "<div>'" + n.TypeAhead.htmlEncode(obj.query) + "' not found";
};
})($element);
$element.typeahead({
var typeaheadConfig = [
{
// options
hint: n.TypeAhead.getHint($element),
highlight: n.TypeAhead.getHighlight($element),
minLength: n.TypeAhead.getMinLength($element)
},
{
// dataset
name: 'staticList',
display: 'value',
source: n.TypeAhead.substringMatcher(staticList),
limit: n.TypeAhead.getLimit($element),
templates: {
// header: '<h3 class="league-name">List</h3>',
suggestion: function (obj) {
return "<div>" + n.TypeAhead.htmlEncode(obj.value) + "</div>";
},
// only show not found for static list if API is not given.
notFound: bloodhoundConfiguration === null ? notFound : undefined
}
}
];
if (bloodhoundConfiguration !== null) {
// initialize bloodhound (typeahead suggestion engine)
var suggestions = new Bloodhound(bloodhoundConfiguration);
var promise = suggestions.initialize();
// get key from field value and fetch value from API, if not already filled from static list
if(!filledFromStaticList) {
promise.done((function ($element, suggestions) {
return function () {
n.TypeAhead.fillTypeAheadFromShadowElement($element, suggestions);
};
})($element, suggestions));
}
typeaheadConfig.push({
name: 'restApi',
display: 'value',
source: suggestions,
limit: n.TypeAhead.getLimit($element),
......@@ -281,13 +320,30 @@ var QfqNS = QfqNS || {};
// No message if field is not set to pedantic.
notFound: (function ($_) {
return function (obj) {
if (!!$_.data('typeahead-pedantic'))
if (!!$element.data('typeahead-pedantic') && typeaheadList.length === 0)
return "<div>'" + n.TypeAhead.htmlEncode(obj.query) + "' not found";
};
})($element)
}
});
}
// add typahead
$element.typeahead.apply($element, typeaheadConfig);
// update typahead list when typahead changes
$element.bind('typeahead:render', function (event, sugg, asynch, datasetName) {
if (datasetName === 'restApi') {
typeaheadListApi.length = 0;
typeaheadListApi.push.apply(typeaheadListApi, sugg);
} else if (datasetName === 'staticList') {
typeaheadListStatic.length = 0;
typeaheadListStatic.push.apply(typeaheadListStatic, sugg);
}
typeaheadList.length = 0;
typeaheadList.push.apply(typeaheadList, typeaheadListApi);
typeaheadList.push.apply(typeaheadList, typeaheadListStatic);
});
// bind select and autocomplete events
$element.bind('typeahead:select typeahead:autocomplete', function (event, suggestion) {
......@@ -446,4 +502,20 @@ var QfqNS = QfqNS || {};
}
$element.typeahead('val', results[0].value);
};
n.TypeAhead.fillTypeAheadFromShadowElementStaticList = function ($element, staticList) {
var $shadowElement = n.TypeAhead.getShadowElement($element);
var key = $shadowElement.val();
if (key === '') {
return false;
}
var tagLookup = staticList.filter(function (t) {return t.key === key;})[0];
if (undefined !== tagLookup) {
$element.typeahead('val', tagLookup.value);
return true;
} else {
return false;
}
};
})(QfqNS);
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment