fixed tag display and clicking on a tag button now removes the tag
[photo-tags.git] / js / photo-tags.js
index 7fce2f2290cb785f073f90b87446bf9b29d35199..e41272fd57e9bb4a4415a151321a2d8446411aa8 100644 (file)
@@ -1,33 +1,48 @@
 var pics = d3.select(".pics").select("ul");
+var page;
+var maxpage;
 
-/* populate data list with tags*/
-d3.json(webbase+"/getjson.php?S", function(json) {
-    d3.select("#MyTags").selectAll("option").data(json)
-      .enter().append("option").attr("value",function(d) {return d.name});
+function init()
+{
+  /* populate data list with tags*/
+  d3.json(webbase+"/getjson.php?S", function(json) {
+      d3.select("#MyTags").selectAll("option").data(json)
+        .enter().append("option").attr("value",function(d) {return d.name});
+    });
+
+  /* update form to point to new link */
+  d3.select("input").on("keyup", function(d) {
+      d3.select('form').attr("action",webbase+"/tag/"+document.getElementById('MyTagsInput').value.replace(" ","+"));
   });
 
-/* update form to point to new link */
-d3.select("input").on("keyup", function(d) {
-    d3.select('form').attr("action",webbase+"/tag/"+document.getElementById('MyTagsInput').value.replace(" ","+"));
-});
-
-if (T!="")
-  {
-    var mycurrenttags = T.split(",");
-
-    d3.select("#currenttags").select("button").remove();
-    d3.select("#currenttags").selectAll("button")
-      .data(mycurrenttags).enter()
-      .append("button").attr("type","button").text( function(d) {return d;} );
-  }
- else
-  {
-    d3.select("#currenttags").select("button").remove();
-    d3.select("#currenttags").append("span").text( ' none');
-  };
+  d3.select("#currenttags").select("a").remove();
+  if (T!="")
+    {
+      var mycurrenttags = T.split(",");
+
+      d3.select("#currenttags").selectAll("a")
+        .data(mycurrenttags).enter()
+        .append("a").attr("class","btn btn-small").text( function(d) {return d;} )
+       .on("mouseover", function(d){ d3.select(this).classed("btn-danger",true)})
+       .on("mouseout", function(d){ d3.select(this).classed("btn-danger",false)})
+       .attr("href", function(d) { return removeTag(T.split(","),d) });
+    };
+}
+
+function removeTag(alltags,removetag)
+{
+    /* return a link to a page with 'removetag' removed from the array of tags*/
+    var index = alltags.indexOf(removetag);
+    alltags.splice(index, 1);
+    if (alltags.length)
+       return webbase+'/tag/'+alltags.join(",");
+    else
+       return webbase;
+}
+
 
 function load_content() {
-  //  d3.select(".debug").text("T,P,N = *"+T+"* *"+page+"* *"+N+"*");
+  // d3.select(".debug").text("T,P,N = *"+T+"* *"+page+"* *"+N+"*");
 
   if (ID>=0)
     url = webbase+"/getjson.php?ID="+ID;
@@ -46,7 +61,7 @@ function load_content() {
       if (ID>=0)
        {
          var singlepicspace=pics.selectAll("li").data(picdata, function(d){return ID;}).enter().append("li").append("div").attr("class","singlepic");
-         singlepicspace.append("div").attr("class","left").append("img").attr("src",webbase+"/icons/left.png");
+         singlepicspace.append("img").attr("class","left").attr("src",webbase+"/icons/left.png");
          singlepicspace.append("img")
            .attr("class","large")
            .attr("src",function(d) {
@@ -54,7 +69,7 @@ function load_content() {
                s = s.replace('file:\/\/'+dbprefix,webbase+'/Photos-small/');
                return s;
              });
-         singlepicspace.append("div").attr("class","right").append("img").attr("src",webbase+"/icons/right.png");
+         singlepicspace.append("img").attr("class","right").attr("src",webbase+"/icons/right.png");
 
          update_thumbnails();
        }
@@ -73,11 +88,9 @@ function load_content() {
                return s;
              });
        };
-
-     checkbutton();
+      checkbutton();
+      update_permalink()
     });
-
-   update_permalink()
 }
 
 function update_permalink() {
@@ -96,13 +109,15 @@ function update_permalink() {
 }
 
 function prev_page() {
-  if (page>=2) page=page-1;
-  load_content();
+    if (page>1) page=page-1;
+    load_content();
+    update_page_index();
 }
 
 function next_page() {
-  page=page+1;
-  load_content();
+    if (page<maxpage) page=page+1;
+    load_content();
+    update_page_index();
 }
 
 function prev_pic() {
@@ -130,10 +145,10 @@ function tagcloud() {
   /* update pics */
   d3.json(url, function(json) {
       svgelement.selectAll("text").data(json).enter().append("text")
-       .style("font-size", function(d){return (Math.log(d.count+1)/2.0)+"em"})
+       .style("font-size", function(d){return (Math.log(d.count+1)/3.0+0.5)+"em"})
        .text(function(d) { return d.name+" "; })
        .on("mouseover", function(d){ d3.select(this).style("color","red")} )
-       .on("mouseout", function(d){ d3.select(this).style("color","white")} )
+       .on("mouseout", function(d){ d3.select(this).style("color","black")} )
        .on("click", function(d) { document.location.href=webbase+'/tag/'+d.name })
     });
 }
@@ -161,6 +176,8 @@ function update_thumbnails(){
 
       var thumbs= d3.select(".nextprev").select("ul").selectAll("li").data(json2, function(d) {return d.id;});
 
+      thumbs.exit().select("img").transition().duration(1000).style("height","0px").transition().duration(1050).remove();
+      thumbs.exit().transition().duration(1050).remove();
       thumbs.enter().append("li")
        .append("a")
        .on("click", function(d) {
@@ -172,16 +189,12 @@ function update_thumbnails(){
            s = s.replace('file:\/\/'+dbprefix,webbase+'/Photos-tiny/');
            return s;
          })
-       .style("height","0")
-       .transition().duration(1000)
+       .style("height","0px")
+       .transition().duration(500)
        .style("height","100px");
 
-      thumbs.exit().select("img").transition().duration(1000).style("height","0");
-      thumbs.exit().transition().duration(1050).remove();
-
       /* resort elements */
       d3.select(".nextprev").select("ul").selectAll("li").sort(function(a,b){return a.id-b.id;});
-      d3.select(".nextprev").select("ul").selectAll("li").select("a").select("img").classed("current",false);
       d3.select(".nextprev").select("ul").selectAll("li").select("a").select("img").classed("current",function(d){return (d.id==IDcurr);});
 
 
@@ -197,14 +210,14 @@ function update_thumbnails(){
 function checkbutton()
 {
   if (page==1)
-    { d3.select("button.prev").attr("disabled","disabled");}
+    { d3.select(".pagination ul li:first-child").classed("disabled", true);}
   else
-    { d3.select("button.prev").attr("disabled", null);};
+    { d3.select(".pagination ul li:first-child").classed("disabled", false);};
 
-  if (count<N)
-    { d3.select("button.next").attr("disabled","disabled");}
+  if (page==maxpage)
+    { d3.select(".pagination ul li:last-child").classed("disabled", true);}
   else
-    { d3.select("button.next").attr("disabled",null);}
+    { d3.select(".pagination ul li:last-child").classed("disabled", false);};
 }
 
 function update_page_index()
@@ -220,48 +233,69 @@ function update_page_index()
     url = webbase+"/getjson.php?C=1"+myID;
 
   d3.json(url, function(json) {
-    /* update index, show only page +-5 pages max */
-    n = Math.floor(json[0].total/N);
-    nr = Math.floor( (json[0].row-1)/N); /* rowid starts at 1 not 0 */
+      /* update index, show only page +-5 pages max */
+      n  = Math.floor( json[0].total/N   ); /* how many pages */
+      nr = Math.floor( (json[0].row-1)/N ); /* which row are we in? rowid starts at 1 not 0 */
+
+      maxpage=n+1;
 
-    if(nr > 0)
-      page = nr+1;
+      if(nr > 0) page = nr+1;
 
-    var mydata = new Array();  // add json data  {page: <nr>, name: <name>} ; at end reform array into real json and use d3 to parse it
+      var mydata = new Array();  // add json data  {page: <nr>, name: <name>} ; at end reform array into real json and use d3 to parse it
 
-    if(n>0)
+      if(n>0)
       {
-        if(page>7)
+         mydata.push('{ page:0.1, name:"Prev"}');
+          if(page>4)
          {
-           mydata.push('{ page:1, name:"1"}');
-           mydata.push('{ page:1.5, name:"..."}');
-           start = page-5;
+             mydata.push('{ page:1, name:"1"}');
+             mydata.push('{ page:1.5, name:"..."}');
+             start = page-3;
          }
-        else
-         start=1;
+          else
+             start=1;
 
-        for(i=start;i<=Math.min(n+1,page+5);i++)
-         mydata.push('{ page:'+i+', name:"'+i+'"}');
+          for(i=start;i<=Math.min(n+1,page+3);i++)
+             mydata.push('{ page:'+i+', name:"'+i+'"}');
 
-        if(page+5<n)
+          if(page+3<n)
          {
-           mydata.push('{ page:'+(n+0.5)+', name:"..."}');
-           mydata.push('{ page:'+(n+1)+', name:"'+(n+1)+'"}');
+             mydata.push('{ page:'+(n+0.5)+', name:"..."}');
+             mydata.push('{ page:'+(n+1)+', name:"'+(n+1)+'"}');
          }
-        else if(page+5==n)
-         mydata.push('{ page:'+(n+1)+', name:"'+(n+1)+'"}');
-      };
-    mydata = "["+mydata.join(",")+"]";
-    mydata =  eval('(' + mydata + ')');
-
-    var pageindex = d3.select(".index").selectAll("button").data(mydata, function(d){ return d.page; });
-    pageindex.exit().remove();
-    pageindex.enter().append("button")
-      .on("click", function(d) { if(  (d.page - Math.floor(d.page)) ==0 )  {page=d.page; ID=-1;load_content(); update_page_index();} })
-      .text(function(d) {return " "+d.name+" "});
-    pageindex.sort( function(a,b) { return a.page- b.page;} );
+          else if(page+3==n)
+             mydata.push('{ page:'+(n+1)+', name:"'+(n+1)+'"}');
+
+         mydata.push('{ page:'+(n+2.1)+', name:"Next"}');
+      }
+      else
+      {
+         mydata.push('{ page:0.1, name:"Prev"}');
+         mydata.push('{ page:1.1, name:"Next"}');
+      }
+
+      mydata = "["+mydata.join(",")+"]";
+      mydata =  eval('(' + mydata + ')');
+
+      /* remove old elements */
+      d3.selectAll(".pagination").select("ul").selectAll("li").remove();
+
+      /* create new ones */
+      var pageindex = d3.selectAll(".pagination").select("ul").selectAll("li").data(mydata, function(d){return d.page});
+      pageindex.selectAll("li").data(mydata, function(d){ return d.page; });
+      pageindex.enter().append("li").append("a")
+         .on("click", function(d) { if(  (d.page - Math.floor(d.page)) ==0 )  {page=d.page; ID=-1;load_content(); update_page_index();} })
+         .text(function(d) {return " "+d.name+" "});
+
+      pageindex.sort( function(a,b) { return a.page- b.page;} );
+
+      /* add callbacks to prev and next buttons */
+      d3.selectAll(".pagination ul li:first-child a").on("click", function(){prev_page();});
+      d3.selectAll(".pagination ul li:last-child  a").on("click", function(){next_page();});
+
+      d3.select(".pagination").select("ul").selectAll("li").classed("active", false);
+      d3.select(".pagination").select("ul").selectAll("li").classed("active", function(d) {return ( d.page == page ); });
+      checkbutton();
     } );
 
-  d3.select(".index").selectAll("button").classed("currentpage",false);
-  d3.select(".index").selectAll("button").classed("currentpage",function(d){return (d.page==page);});
 }