Commit eec448c0 authored by Carsten  Rose's avatar Carsten Rose
Browse files

Merge remote-tracking branch 'origin/fix_3466' into crose_work

parents ab4cd20d 35abb4e0
......@@ -25,8 +25,10 @@ var QfqNS = QfqNS || {};
n.TypeAhead.install = function (typeahead_endpoint) {
$('.qfq-typeahead').each(function () {
var $shadowElement, bloodhoundConfiguration;
var $element = $(this);
var suggestions = new Bloodhound({
bloodhoundConfiguration = {
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('key', 'value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
identify: function (obj) {
......@@ -36,7 +38,14 @@ var QfqNS = QfqNS || {};
url: n.TypeAhead.makeUrl(typeahead_endpoint, $element),
wildcard: '%QUERY'
}
});
};
if ($element.val() !== '') {
// We prefetch the value provided
bloodhoundConfiguration.prefetch = n.TypeAhead.makePrefetchUrl(typeahead_endpoint, $element.val(), $element);
}
var suggestions = new Bloodhound(bloodhoundConfiguration);
n.TypeAhead.makeShadowElement($element);
......@@ -58,10 +67,27 @@ var QfqNS = QfqNS || {};
}
}
});
n.TypeAhead.fillTypeAheadFromShadowElement($element, suggestions);
$element.bind('typeahead:select typeahead:autocomplete', function (event, suggestion) {
var $shadowElement = $(event.delegateTarget).data('shadow-element');
var $shadowElement = n.TypeAhead.getShadowElement($(event.delegateTarget));
$shadowElement.val(suggestion.key);
});
if (!!$element.data('typeahead-pedantic')) {
$element.bind('typeahead:change', (function (bloodHound) {
return function (event) {
var $typeAhead = $(event.delegateTarget);
var $shadowElement = n.TypeAhead.getShadowElement($typeAhead);
if ($shadowElement.val() === '') {
$typeAhead.typeahead('val', '');
$typeAhead.closest('form').change();
}
};
})(suggestions)
);
}
});
};
......@@ -69,6 +95,9 @@ var QfqNS = QfqNS || {};
n.TypeAhead.makeUrl = function (endpoint, element) {
return endpoint + "?query=%QUERY" + "&sip=" + n.TypeAhead.getSip(element);
};
n.TypeAhead.makePrefetchUrl = function (endpoint, prefetchKey, element) {
return endpoint + "?prefetch=" + encodeURIComponent(prefetchKey) + "&sip=" + n.TypeAhead.getSip(element);
};
n.TypeAhead.getLimit = function ($element) {
return $element.data('typeahead-limit');
......@@ -103,19 +132,43 @@ var QfqNS = QfqNS || {};
};
n.TypeAhead.makeShadowElement = function ($element) {
var $parent, inputName, uniqueId, $shadowElement;
var $parent, inputName, inputValue, uniqueId, $shadowElement;
$parent = $element.parent();
inputName = $element.attr('name');
$element.removeAttr('name');
inputValue = $element.val();
$shadowElement = $('<input>')
.attr('type', 'hidden')
.attr('name', inputName);
.attr('name', inputName)
.val(inputValue);
$element.data('shadow-element', $shadowElement);
$parent.append($shadowElement);
return $shadowElement;
};
n.TypeAhead.getShadowElement = function ($element) {
return $element.data('shadow-element');
};
n.TypeAhead.fillTypeAheadFromShadowElement = function ($element, bloodhound) {
var results;
var $shadowElement = n.TypeAhead.getShadowElement($element);
var key = $shadowElement.val();
if (key === '') {
return;
}
results = bloodhound.get(key);
if (results.length === 0) {
return;
}
$element.typeahead('val', results[0].value);
};
})(QfqNS);
......@@ -11,7 +11,7 @@ if (isset($_GET['provoke_500'])) {
exit(0);
}
if (!isset($_GET['query'])) {
if (!isset($_GET['query']) && !isset($_GET['prefetch'])) {
http_response_code(400);
echo json_encode(['message' => 'wrong arguments']);
exit(0);
......@@ -76,12 +76,22 @@ $dict = [
['value' => "Wyoming", 'key' => "WY"],
];
$result = [];
foreach ($dict as $obj) {
if (stristr($obj['value'], $_GET['query']) ||
stristr($obj['key'], $_GET['query'])
) {
$result[] = $obj;
if (isset($_GET['prefetch'])) {
$result = [];
foreach ($dict as $obj) {
if (stristr($obj['key'], $_GET['prefetch'])) {
$result[] = $obj;
}
}
} else {
$result = [];
foreach ($dict as $obj) {
if (stristr($obj['value'], $_GET['query']) ||
stristr($obj['key'], $_GET['query'])
) {
$result[] = $obj;
}
}
}
......
......@@ -96,12 +96,30 @@
<div id="formgroup2" class="form-group">
<div class="col-md-2">
<label for="dropdown2" class="control-label">Text input 2</label>
<label for="dropdown2" class="control-label">Text input 2 (pedantic, required)</label>
</div>
<div class="col-md-6">
<input id="dropdown2" type="text" class="form-control qfq-typeahead" name="dropdown2"
data-typeahead-sip="abcdef" data-typeahead-limit="10" data-typeahead-minlength="1">
data-typeahead-sip="abcdef" data-typeahead-limit="10" data-typeahead-minlength="1"
data-typeahead-pedantic="true" required>
</div>
<div class="col-md-4">
<p class="help-block with-errors"></p>
</div>
</div>
<div id="formgroup3" class="form-group">
<div class="col-md-2">
<label for="dropdown3" class="control-label">Text input 3 (prefilled)</label>
</div>
<div class="col-md-6">
<input id="dropdown3" type="text" class="form-control qfq-typeahead" name="dropdown3"
data-typeahead-sip="abcdef" data-typeahead-limit="10" data-typeahead-minlength="1"
value="AL">
</div>
</div>
......
Supports Markdown
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