From f9656aa2ad257959dd4ed469714ca75791b70040 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Sat, 17 Feb 2007 18:35:14 +0000 Subject: [PATCH] Rather more lightweight (and functional) tag completion code. git-svn-id: http://svn.simon-cozens.org/memories/trunk@35 041978f6-d955-411f-a9d7-1d8545c9c3c7 --- templates/header | 9 +-- templates/photo/tagedit | 6 +- templates/photo/upload | 20 +----- templates/photo/view | 2 +- templates/static/memories.css | 3 + templates/static/upload.js | 115 ++++++++++++++-------------------- 6 files changed, 57 insertions(+), 98 deletions(-) diff --git a/templates/header b/templates/header index 50c09d7..797ad08 100644 --- a/templates/header +++ b/templates/header @@ -9,15 +9,10 @@ href="[%base%]/[%path%]?format=rss" /> [% END %] -[% IF request.params.active == "tagedit" %] - - - - -[% ELSE %] + + -[% END %] [% INCLUDE nav %] diff --git a/templates/photo/tagedit b/templates/photo/tagedit index a34d8bd..24f60ad 100644 --- a/templates/photo/tagedit +++ b/templates/photo/tagedit @@ -1,3 +1,4 @@ +
Tagging advice: Tags should be words, (portrait, henry) or phrases surrounded by double quotes. ("tall buildings") You can @@ -11,11 +12,10 @@ put any number of tags in the "add tags" box, like this:
Delete tags:
-

Add tags:

+ diff --git a/templates/photo/upload b/templates/photo/upload index ad88ff8..02ef5e8 100644 --- a/templates/photo/upload +++ b/templates/photo/upload @@ -1,21 +1,4 @@ - - - Memories - Photo Sharing - - - - - -[% INCLUDE nav %] -
- -
- [% IF messages %] -
-
    [% FOR m = messages %]
  • [%m%]
  • [% END %] -
- [% END %] -
+[% INCLUDE header %]

Upload a photo

This is where you can upload your photographs. At the moment, you must @@ -38,6 +21,7 @@ server, so don't press stop or reload after pressing the Upload button.

 
+ Tagging advice: Tags should be words, (portrait, henry) or phrases surrounded by double quotes. ("tall buildings") You can put any number of tags in the "add tags" box, like this: diff --git a/templates/photo/view b/templates/photo/view index 0fbccbb..0216bda 100644 --- a/templates/photo/view +++ b/templates/photo/view @@ -75,7 +75,7 @@ END %] function select_tab(name) { document.getElementsByClassName("active").each(function (x) { x.removeClassName("active")}); new Ajax.Updater("content","[%base%]/photo/"+name+"/[%photo.id%]", - { method: "get" } + { method: "get", evalScripts: true } ) $(name).addClassName("active"); } diff --git a/templates/static/memories.css b/templates/static/memories.css index 77476f2..e6df986 100644 --- a/templates/static/memories.css +++ b/templates/static/memories.css @@ -93,6 +93,9 @@ p { font-family: sans-serif; } .frontpagetable { padding: 10px; } .frontpagetable th { border: 1px solid black; } + +.tagedittagdelete { margin-right: 2em; } + span.tagcloud0 { font-size: 9px;} span.tagcloud1 { font-size: 10px;} span.tagcloud2 { font-size: 10px;} diff --git a/templates/static/upload.js b/templates/static/upload.js index 0b033c6..c62663d 100644 --- a/templates/static/upload.js +++ b/templates/static/upload.js @@ -1,82 +1,59 @@ String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g,'') } String.prototype.escRegExp = function(){ return this.replace(/[\\$*+?()=!|,{}\[\]\.^]/g,'\\$&') } String.prototype.unescHtml = function(){ var i,t=this; for(i in e) t=t.replace(new RegExp(i,'g'),e[i]); return t } -function Suggestions() { this.length=0; this.picked=0 } -var suggestions = new Suggestions(); -var tagSearch='', lastEdit=''; -var h={}, sections=[{},{},{},{},{},{}], selected={}, currentTag={}, -e={'<':'<','>':'>','&':'&','"':'"'}; - -function init () { - document.onkeydown = document.onkeypress = document.onkeyup = handler - h.suggest = document.getElementById("suggestionlist"); - h.tags = document.getElementById("tags"); -} - -function handler(event) { var e=(event||window.event) //w3||ie - if (e.type == 'keyup') { +var availhash = {}; +var tags = tagList.split(/\s+/); +for (t in tags) { availhash[tags[t]] = 1 } + +function addHandler( elt ) { + var params = { + elt: elt, + oldvalue: elt.value + }; + document.onkeyup = function (event) { + var e=(event||window.event) //w3||ie switch(e.keyCode) { - //case 8: //backspace - //case 46: //delete case 35: //end case 36: //home case 39: // right case 37: // left case 32: // space - hideSuggestions(); break - case 38: case 40: break; - case 9: break; - case 13: break; + params.elt.style.background = "#fff"; + $("suggestionlist").style.visibility='hidden'; + break + case 38: case 40: case 9: case 13: break; default: - updateSuggestions() -}} - else if (e.type == "keypress") { lastEdit = h.tags.value } -} - -function makeTag(parent, tag, js) { - parent.appendChild(document.createTextNode(" "+ tag)) -} - -function updateSuggestions() { - while (h.suggest.hasChildNodes()) h.suggest.removeChild(h.suggest.firstChild) - if(!getCurrentTag() || !currentTag.text) { - hideSuggestions(); return false - } - var tagArray = h.tags.value.toLowerCase().split(' '), - txt=currentTag.text.trim().escRegExp(), tagHash={}, t - for(t in tagArray) tagHash[tagArray[t]] = true; - - var search = tagList.match(new RegExp(("(?:^| )("+txt+"[^ ]+)"), "gi")) - if(search){ - var i; - for (i=0; i 0) { showSuggestions() } - else { hideSuggestions(); } -} - -function getCurrentTag() { - if(h.tags.value == lastEdit) return true // no edit - if(h.tags == '') return false - currentTag = {} - var tagArray=h.tags.value.toLowerCase().split(' '), oldArray=lastEdit.toLowerCase().split(' '), currentTags = [], matched=false, t,o - for (t in tagArray) { - for (o in oldArray) { - if(typeof oldArray[o] == 'undefined') { oldArray.splice(o,1); break } - if(tagArray[t] == oldArray[o]) { matched = true; oldArray.splice(o,1); break; } + var tagArray = params.elt.value.toLowerCase().split(' '), + txt=tagArray[tagArray.length-1].trim().escRegExp(), tagHash={}, t + for(t in tagArray) tagHash[tagArray[t]] = true; + var sl = $("suggestionlist"); + if (!txt) { sl.style.visibility = 'hidden'; return; } + + var search = tagList.match(new RegExp(("(?:^| )("+txt+"[^ ]+)"), "gi")) + if(search){ + params.elt.style.background = availhash[txt] ? "#dfd" : "#fff"; + while(sl.childNodes.length > 0) { sl.removeChild(sl.firstChild) } + sl.style.visibility='visible'; + var i; + for (i in search) { + if (!tagHash[search[i]]) { + var tn = document.createTextNode(search[i]); + var a = document.createElement("a"); + var elt = params.elt; + var closureTxt = txt; + a.onclick = function () { + elt.value = elt.value.replace(new RegExp(closureTxt+"$"),this.firstChild.nodeValue+" "); + elt.focus(); + sl.style.visibility='hidden'; + }; + a.appendChild(tn); + sl.appendChild(a); + } + } + } else { + params.elt.style.background = availhash[txt] ? "#dfd" : "#fdd"; + sl.style.visibility='hidden'; + } } - if(!matched) currentTags[currentTags.length] = t - matched=false } - // more than one word changed... abort - currentTag = { text:tagArray[currentTags[0]], index:currentTags[0] } - return true } - - -function hideSuggestions() { h.suggest.style.visibility='hidden' } -function showSuggestions() { h.suggest.style.visibility='visible' } -- 2.39.5