/**
* Get tag value suggestions from scanning existing tag values.
* @param $searchTerm
+ * @param $tagName
* @return array
*/
- public function getValueSuggestions($searchTerm)
+ public function getValueSuggestions($searchTerm, $tagName = false)
{
if ($searchTerm === '') return [];
$query = $this->tag->where('value', 'LIKE', $searchTerm . '%')->groupBy('value')->orderBy('value', 'desc');
+ if ($tagName !== false) {
+ $query = $query->where('name', '=', $tagName);
+ }
$query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
return $query->get(['value'])->pluck('value');
}
+
/**
* Save an array of tags to an entity
* @param Entity $entity
}
}]);
- ngApp.directive('autosuggestions', ['$http', function($http) {
+ ngApp.directive('tagAutosuggestions', ['$http', function($http) {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
let $input = $(this);
let val = $input.val();
let url = $input.attr('autosuggest');
+ let type = $input.attr('autosuggest-type');
+
// No suggestions until at least 3 chars
if (val.length < 3) {
if (isShowing) {
isShowing = false;
}
return;
- };
+ }
+
+ // Add name param to request if for a value
+ if (type.toLowerCase() === 'value') {
+ let $nameInput = $input.closest('tr').find('[autosuggest-type="name"]').first();
+ let nameVal = $nameInput.val();
+ if (nameVal === '') return;
+ url += '?name=' + encodeURIComponent(nameVal);
+ console.log(url);
+ }
let suggestionPromise = getSuggestions(val.slice(0, 3), url);
- suggestionPromise.then((suggestions) => {
+ suggestionPromise.then(suggestions => {
if (val.length > 2) {
- suggestions = suggestions.filter((item) => {
+ suggestions = suggestions.filter(item => {
return item.toLowerCase().indexOf(val.toLowerCase()) !== -1;
}).slice(0, 4);
displaySuggestions($input, suggestions);
let newActive = (active === 0) ? suggestCount-1 : active - 1;
changeActiveTo(newActive, suggestionElems);
}
- // Enter key
- else if (event.keyCode === 13) {
+ // Enter or tab key
+ else if (event.keyCode === 13 || event.keyCode === 9) {
let text = suggestionElems[active].textContent;
currentInput[0].value = text;
currentInput.focus();
$suggestionBox.hide();
isShowing = false;
- event.preventDefault();
- return false;
+ if (event.keyCode === 13) {
+ event.preventDefault();
+ return false;
+ }
}
});
// Get suggestions & cache
function getSuggestions(input, url) {
- let searchUrl = url + '?search=' + encodeURIComponent(input);
+ let hasQuery = url.indexOf('?') !== -1;
+ let searchUrl = url + (hasQuery?'&':'?') + 'search=' + encodeURIComponent(input);
// Get from local cache if exists
if (localCache[searchUrl]) {
<h4>Page Tags</h4>
<div class="padded tags">
<p class="muted small">Add some tags to better categorise your content. <br> You can assign a value to a tag for more in-depth organisation.</p>
- <table class="no-style" autosuggestions style="width: 100%;">
+ <table class="no-style" tag-autosuggestions style="width: 100%;">
<tbody ui-sortable="sortOptions" ng-model="tags" >
<tr ng-repeat="tag in tags track by $index">
<td width="20" ><i class="handle zmdi zmdi-menu"></i></td>
- <td><input autosuggest="/ajax/tags/suggest/names" class="outline" ng-attr-name="tags[@{{$index}}][name]" type="text" ng-model="tag.name" ng-change="tagChange(tag)" ng-blur="tagBlur(tag)" placeholder="Tag"></td>
- <td><input autosuggest="/ajax/tags/suggest/values" class="outline" ng-attr-name="tags[@{{$index}}][value]" type="text" ng-model="tag.value" ng-change="tagChange(tag)" ng-blur="tagBlur(tag)" placeholder="Tag Value (Optional)"></td>
+ <td><input autosuggest="/ajax/tags/suggest/names" autosuggest-type="name" class="outline" ng-attr-name="tags[@{{$index}}][name]" type="text" ng-model="tag.name" ng-change="tagChange(tag)" ng-blur="tagBlur(tag)" placeholder="Tag"></td>
+ <td><input autosuggest="/ajax/tags/suggest/values" autosuggest-type="value" class="outline" ng-attr-name="tags[@{{$index}}][value]" type="text" ng-model="tag.value" ng-change="tagChange(tag)" ng-blur="tagBlur(tag)" placeholder="Tag Value (Optional)"></td>
<td width="10" ng-show="tags.length != 1" class="text-center text-neg" style="padding: 0;" ng-click="removeTag(tag)"><i class="zmdi zmdi-close"></i></td>
</tr>
</tbody>