Changed tag export structure

This commit is contained in:
Marcin Kurczewski 2014-10-14 22:40:34 +02:00
parent 35b893db9d
commit 8f03c68ec8
3 changed files with 22 additions and 16 deletions

View file

@ -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('<li/>');
$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;

View file

@ -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);

View file

@ -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()