some polishing...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2013-12-08 22:14:51 +04:00
parent b933eae8a6
commit d4bae60f58
2 changed files with 57 additions and 23 deletions

View File

@ -21,6 +21,11 @@ TAGS = {
function buildTagsFromImages(images){
}
// XXX
function normalizeTag(tag){
return tag.trim()
}
/*********************************************************************/
@ -30,13 +35,21 @@ function addTag(tags, gid, tagset, images){
tagset = tagset == null ? TAGS : tagset
images = images == null ? IMAGES : images
var img = images[gid]
if(img.tags == null){
img.tags = []
if(tags.length == 0){
return
}
var updated = false
var img = images[gid]
img_tags = img.tags == null ? [] : img.tags
// add tags to tagset...
tags.map(function(tag){
// skip empty tags...
if(normalizeTag(tag) == ''){
return
}
updated = true
var set = tagset[tag]
if(set == null){
set = []
@ -47,13 +60,16 @@ function addTag(tags, gid, tagset, images){
set.sort()
}
if(img.tags.indexOf(tag) < 0){
img.tags.push(tag)
if(img_tags.indexOf(tag) < 0){
img_tags.push(tag)
}
})
// XXX hardcoded and not customizable...
IMAGES_UPDATED.push(gid)
if(updated){
img.tags = img_tags
// XXX hardcoded and not customizable...
IMAGES_UPDATED.push(gid)
}
}
@ -63,6 +79,10 @@ function removeTag(tags, gid, tagset, images){
tagset = tagset == null ? TAGS : tagset
images = images == null ? IMAGES : images
if(tags.length == 0){
return
}
var updated = false
var img = images[gid]
@ -75,14 +95,14 @@ function removeTag(tags, gid, tagset, images){
if(img.tags != null && img.tags.indexOf(tag) >= 0){
updated = true
img.tags.splice(img.tags.indexOf(tag), 1)
// clear the tags...
if(img.tags.length == 0){
delete img.tags
}
}
})
// clear the tags...
if(updated && img.tags.length == 0){
delete img.tags
}
if(updated){
// XXX hardcoded and not customizable...
IMAGES_UPDATED.push(gid)
@ -90,6 +110,30 @@ function removeTag(tags, gid, tagset, images){
}
function updateTags(tags, gid, tagset, images){
tags = typeof(tags) == typeof('str') ? [ tags ] : tags
gid = gid == null ? getImageGID() : gid
tagset = tagset == null ? TAGS : tagset
images = images == null ? IMAGES : images
var img = images[gid]
// remove...
if(img.tags != null){
var rtags = []
img.tags.map(function(tag){
if(tags.indexOf(tag) < 0){
rtags.push(tag)
}
})
removeTag(rtags, gid, tagset, images)
}
// add...
addTag(tags, gid, tagset, images)
}
// this implements the AND selector...
// NOTE: do not like this algorithm as it can get O(n^2)-ish
function selectByTags(tags, tagset){

View File

@ -1254,17 +1254,7 @@ function showImageInfo(){
if(ntags != tags){
ntags = ntags.split(/\s*,\s*/)
// remove...
var rtags = []
data.tags.map(function(tag){
if(ntags.indexOf(tag) < 0){
rtags.push(tag)
}
})
removeTag(rtags, gid)
// add...
addTag(ntags, gid)
updateTags(ntags, gid)
}
})
}