]> BookStack Code Mirror - bookstack/commitdiff
Limited tag value autosuggestions based on tag name
authorDan Brown <redacted>
Sat, 4 Jun 2016 13:54:31 +0000 (14:54 +0100)
committerDan Brown <redacted>
Sat, 4 Jun 2016 13:54:31 +0000 (14:54 +0100)
As requested on #121

app/Http/Controllers/TagController.php
app/Repos/TagRepo.php
resources/assets/js/directives.js
resources/views/pages/form-toolbox.blade.php

index 1823b0dc8b1e7ee47ddd8188fd0430942231fe5d..b6749aec10cfc2ab40243b49f425127446571ad9 100644 (file)
@@ -67,7 +67,8 @@ class TagController extends Controller
     public function getValueSuggestions(Request $request)
     {
         $searchTerm = $request->get('search');
-        $suggestions = $this->tagRepo->getValueSuggestions($searchTerm);
+        $tagName = $request->has('name') ? $request->get('name') : false;
+        $suggestions = $this->tagRepo->getValueSuggestions($searchTerm, $tagName);
         return response()->json($suggestions);
     }
 
index 7d51d87f743db6d7a5ba432639ac0d12a963a26f..e87732cf591fd5d9c6fb55767bd2aaa1da7fd488 100644 (file)
@@ -72,15 +72,20 @@ class TagRepo
     /**
      * 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
index d41fdd83bce030e162862bcb3164886c56e2763d..df5284a9772cef9cb59697519f89906f3d519ef8 100644 (file)
@@ -378,7 +378,7 @@ module.exports = function (ngApp, events) {
         }
     }]);
 
-    ngApp.directive('autosuggestions', ['$http', function($http) {
+    ngApp.directive('tagAutosuggestions', ['$http', function($http) {
         return {
             restrict: 'A',
             link: function(scope, elem, attrs) {
@@ -403,6 +403,8 @@ module.exports = function (ngApp, events) {
                     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) {
@@ -410,12 +412,21 @@ module.exports = function (ngApp, events) {
                             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);
@@ -448,15 +459,17 @@ module.exports = function (ngApp, events) {
                         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;
+                        }
                     }
                 });
 
@@ -523,7 +536,8 @@ module.exports = function (ngApp, events) {
 
                 // 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]) {
index ae17045d109849b7bacdab2d339ed853f8b8c9e7..b3fcd7c13c29f8c9971a9f91095841b9c5e7a71c 100644 (file)
         <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>