From 6bd8e318f02fa3a78ade7f43e3cd57bef626f69b Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sun, 8 Dec 2013 19:11:18 +0400 Subject: [PATCH] started work on tagging... Signed-off-by: Alex A. Naanou --- ui/tags.js | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100755 ui/tags.js diff --git a/ui/tags.js b/ui/tags.js new file mode 100755 index 00000000..c9c2cca1 --- /dev/null +++ b/ui/tags.js @@ -0,0 +1,129 @@ +/********************************************************************** +* +* +* +**********************************************************************/ + +// format: +// { +// tag: [ gid, ... ], +// ... +// } +// +TAGS = { + +} + + + +/*********************************************************************/ + +function buildTagsFromImages(images){ +} + + +/*********************************************************************/ + +function addTag(tags, gid, tagset, images){ + tags = typeof(tag) == typeof('str') ? [ tags ] : tag + gid = gid == null ? getImageGID() : gid + tagset = tagset == null ? TAGS : tagset + images = images == null ? IMAGES : images + + // add tags to tagset... + tags.map(function(tag){ + var set = tagset[tag] + if(set == null){ + set = [] + tagset[tag] = set + } + set.push(tag) + set.sort() + }) + + // add tag to image... + var img = images[gid] + img.tags = img.tags == null ? tags : img.tags.concat(tags) + + // XXX hardcoded and not customizable... + IMAGES_UPDATED.push(gid) +} + + +function removeTag(tags, gid, tagset, images){ + tags = typeof(tag) == typeof('str') ? [ tags ] : tag + gid = gid == null ? getImageGID() : gid + tagset = tagset == null ? TAGS : tagset + images = images == null ? IMAGES : images + + var updated = false + var img = images[gid] + + // remove tags to tagset... + tags.map(function(tag){ + var set = tagset[tag] + if(set != null && set.indexOf(tag) >= 0){ + updated = true + + set.splice(set.indexOf(tag), 1) + img.tags.splice(img.tags.indexOf(tag), 1) + + // clear the tags... + if(img.tags.length == 0){ + delete img.tags + } + } + }) + + if(updated){ + // XXX hardcoded and not customizable... + IMAGES_UPDATED.push(gid) + } +} + + +// this implements the AND selector... +// NOTE: do not like this algorithm as it can get O(n^2)-ish +function selectByTags(tags, tagset){ + var subtagset = [] + var res = [] + + // populate the subtagset... + tags.map(function(tag){ + subtagset.push(tagset[tag]) + }) + subtagset.sort(function(a, b){ + return a.length - b.length + }) + + // set the res to the shortest subset... + var cur = subtagset.pop().splice() + + // filter out the result... + cur.map(function(gid){ + for(var i=0; i