diff --git a/public_html/js/Controls/AutoCompleteInput.js b/public_html/js/Controls/AutoCompleteInput.js index b74d2ddc..9bf4f644 100644 --- a/public_html/js/Controls/AutoCompleteInput.js +++ b/public_html/js/Controls/AutoCompleteInput.js @@ -46,11 +46,15 @@ App.Controls.AutoCompleteInput = function($input) { if (cachedSource) { return cachedSource; } else { - var source = source || tagList.getTags(); - source = _.pairs(source); - source = _.sortBy(source, function(a) { return -a[1]; }); - source = _.filter(source, function(a) { return a[1] > 0; }); - source = _.map(source, function(a) { return [a[0], a[0] + ' (' + a[1] + ')']; }); + var source = tagList.getTags(); + source = _.sortBy(source, function(a) { return -a.usages; }); + source = _.filter(source, function(a) { return a.usages > 0; }); + source = _.map(source, function(a) { + return { + tag: a.name, + caption: a.name + ' (' + a.usages + ')', + }; + }); cachedSource = source; return source; } @@ -152,12 +156,12 @@ App.Controls.AutoCompleteInput = function($input) { function getResultsFilter(textToFind) { if (textToFind.length < options.minLengthToArbitrarySearch) { return options.caseSensitive ? - function(resultItem) { return resultItem[0].indexOf(textToFind) === 0; } : - function(resultItem) { return resultItem[0].toLowerCase().indexOf(textToFind.toLowerCase()) === 0; }; + function(resultItem) { return resultItem.tag.indexOf(textToFind) === 0; } : + function(resultItem) { return resultItem.tag.toLowerCase().indexOf(textToFind.toLowerCase()) === 0; }; } else { return options.caseSensitive ? - function(resultItem) { return resultItem[0].indexOf(textToFind) >= 0; } : - function(resultItem) { return resultItem[0].toLowerCase().indexOf(textToFind.toLowerCase()) >= 0; }; + function(resultItem) { return resultItem.tag.indexOf(textToFind) >= 0; } : + function(resultItem) { return resultItem.tag.toLowerCase().indexOf(textToFind.toLowerCase()) >= 0; }; } } @@ -174,7 +178,7 @@ App.Controls.AutoCompleteInput = function($input) { function applyAutocomplete() { if (options.onApply) { - options.onApply(results[activeResult][0]); + options.onApply(results[activeResult].tag); } else { var val = $input.val(); var start = getSelectionStart(); @@ -186,7 +190,7 @@ App.Controls.AutoCompleteInput = function($input) { prefix = val.substring(0, index + 1); middle = val.substring(index + 1); } - $input.val(prefix + results[activeResult][0] + ' ' + suffix.trimLeft()); + $input.val(prefix + results[activeResult].tag + ' ' + suffix.trimLeft()); $input.focus(); } } @@ -200,8 +204,8 @@ App.Controls.AutoCompleteInput = function($input) { $list.empty(); _.each(results, function(resultItem, resultIndex) { var $listItem = jQuery('
  • '); - $listItem.text(resultItem[1]); - $listItem.attr('data-key', resultItem[0]); + $listItem.text(resultItem.caption); + $listItem.attr('data-key', resultItem.tag); $listItem.hover(function(e) { e.preventDefault(); activeResult = resultIndex; diff --git a/src/Services/TagService.php b/src/Services/TagService.php index 31c6b177..a031727b 100644 --- a/src/Services/TagService.php +++ b/src/Services/TagService.php @@ -73,7 +73,9 @@ class TagService $tags = []; foreach ($this->tagDao->findAll() as $tag) { - $tags[$tag->getName()] = $tag->getUsages(); + $tags[$tag->getId()] = [ + 'name' => $tag->getName(), + 'usages' => $tag->getUsages()]; } $json = json_encode($tags); $this->fileDao->save('tags.json', $json); diff --git a/tests/Services/TagServiceTest.php b/tests/Services/TagServiceTest.php index 52bbf6f9..ca55796d 100644 --- a/tests/Services/TagServiceTest.php +++ b/tests/Services/TagServiceTest.php @@ -78,7 +78,7 @@ final class TagServiceTest extends AbstractDatabaseTestCase $tagService = $this->getTagService(); $tagService->createTags([$tag1]); $tagService->exportJson(); - $this->assertEquals('{"test":0}', $fileDao->load('tags.json')); + $this->assertEquals('{"1":{"name":"test","usages":0}}', $fileDao->load('tags.json')); } public function testExportMultiple() @@ -93,7 +93,7 @@ final class TagServiceTest extends AbstractDatabaseTestCase $tagService = $this->getTagService(); $tagService->createTags([$tag1, $tag2]); $tagService->exportJson(); - $this->assertEquals('{"test1":0,"test2":0}', $fileDao->load('tags.json')); + $this->assertEquals('{"1":{"name":"test1","usages":0},"2":{"name":"test2","usages":0}}', $fileDao->load('tags.json')); } private function getPublicFileDao()