fixed tag display and clicking on a tag button now removes the tag
[photo-tags.git] / js / photo-tags.js
1 var pics = d3.select(".pics").select("ul");
2 var page;
3 var maxpage;
4
5 function init()
6 {
7   /* populate data list with tags*/
8   d3.json(webbase+"/getjson.php?S", function(json) {
9       d3.select("#MyTags").selectAll("option").data(json)
10         .enter().append("option").attr("value",function(d) {return d.name});
11     });
12
13   /* update form to point to new link */
14   d3.select("input").on("keyup", function(d) {
15       d3.select('form').attr("action",webbase+"/tag/"+document.getElementById('MyTagsInput').value.replace(" ","+"));
16   });
17
18   d3.select("#currenttags").select("a").remove();
19   if (T!="")
20     {
21       var mycurrenttags = T.split(",");
22
23       d3.select("#currenttags").selectAll("a")
24         .data(mycurrenttags).enter()
25         .append("a").attr("class","btn btn-small").text( function(d) {return d;} )
26         .on("mouseover", function(d){ d3.select(this).classed("btn-danger",true)})
27         .on("mouseout", function(d){ d3.select(this).classed("btn-danger",false)})
28         .attr("href", function(d) { return removeTag(T.split(","),d) });
29     };
30 }
31
32 function removeTag(alltags,removetag)
33 {
34     /* return a link to a page with 'removetag' removed from the array of tags*/
35     var index = alltags.indexOf(removetag);
36     alltags.splice(index, 1);
37     if (alltags.length)
38         return webbase+'/tag/'+alltags.join(",");
39     else
40         return webbase;
41 }
42
43
44 function load_content() {
45   // d3.select(".debug").text("T,P,N = *"+T+"* *"+page+"* *"+N+"*");
46
47   if (ID>=0)
48     url = webbase+"/getjson.php?ID="+ID;
49   else if(T!="")
50     url = webbase+"/getjson.php?T="+T+"&P="+page;
51   else
52     url = webbase+"/getjson.php?P="+page;
53
54   /* update pics */
55   d3.json(url, function(json) {
56       count=0;
57       pics.selectAll("li").remove();
58       picdata=json;
59
60       /* if ID is set, just show one pictures, else create an array of pictures */
61       if (ID>=0)
62         {
63           var singlepicspace=pics.selectAll("li").data(picdata, function(d){return ID;}).enter().append("li").append("div").attr("class","singlepic");
64           singlepicspace.append("img").attr("class","left").attr("src",webbase+"/icons/left.png");
65           singlepicspace.append("img")
66             .attr("class","large")
67             .attr("src",function(d) {
68                 s = d.base_uri+'/'+d.filename;
69                 s = s.replace('file:\/\/'+dbprefix,webbase+'/Photos-small/');
70                 return s;
71               });
72           singlepicspace.append("img").attr("class","right").attr("src",webbase+"/icons/right.png");
73
74           update_thumbnails();
75         }
76       else
77         {
78           d3.select(".nextprev").select("ul").selectAll("li").remove();
79           pics.selectAll("li").data(picdata)
80             .enter().append("li")
81             .append("a")
82             .on("click", function(d) { load_pic(d.id); })
83             .append("img")
84             .attr("src",function(d) {
85                 count++;
86                 s= d.base_uri+'/'+d.filename;
87                 s = s.replace('file:\/\/'+dbprefix,webbase+'/Photos-tiny/');
88                 return s;
89               });
90         };
91       checkbutton();
92       update_permalink()
93     });
94 }
95
96 function update_permalink() {
97   /* update permalink */
98
99   permalink = webbase;
100
101   if(T!="")
102     permalink += '/tag/' + T;
103   if(page!=1)
104     permalink += '/page/' + page;
105   if(ID>0)
106     permalink += '/pic/' + ID;
107
108   d3.select(".permalink").html("Permalink: <a href=\""+permalink+"\">"+permalink+"</a>");
109 }
110
111 function prev_page() {
112     if (page>1) page=page-1;
113     load_content();
114     update_page_index();
115 }
116
117 function next_page() {
118     if (page<maxpage) page=page+1;
119     load_content();
120     update_page_index();
121 }
122
123 function prev_pic() {
124 }
125
126 function next_pic() {
127 }
128
129 function load_pic(myid) {
130   ID=myid;
131   update_page_index();
132   update_thumbnails();
133   load_content();
134 }
135
136 function tagcloud() {
137
138   url = webbase+"/getjson.php?CLOUD=1";
139
140   pics.selectAll("li").remove();
141
142   var svgelement=pics.append("li")
143     .append("svg").attr("width",400).attr("height",400);
144
145   /* update pics */
146   d3.json(url, function(json) {
147       svgelement.selectAll("text").data(json).enter().append("text")
148         .style("font-size", function(d){return (Math.log(d.count+1)/3.0+0.5)+"em"})
149         .text(function(d) { return d.name+" "; })
150         .on("mouseover", function(d){ d3.select(this).style("color","red")} )
151         .on("mouseout", function(d){ d3.select(this).style("color","black")} )
152         .on("click", function(d) { document.location.href=webbase+'/tag/'+d.name })
153     });
154 }
155
156 function update_thumbnails(){
157   if(T!="")
158     url2 = webbase+"/getjson.php?NP=1&T="+T+"&ID="+ID;
159   else
160     url2 = webbase+"/getjson.php?NP=1&ID="+ID;
161
162   var IDprev=-1;
163   var IDnext=-1;
164   var IDcurr=-1;
165   d3.json(url2, function(json2) {
166       /* figure out where the arrows on the pic should link to */
167       all=""
168       for (var i in json2){
169         if( IDcurr != ID )
170           {
171             IDprev = IDcurr;
172             IDcurr = IDnext;
173             IDnext = json2[i].id;
174           };
175       }
176
177       var thumbs= d3.select(".nextprev").select("ul").selectAll("li").data(json2, function(d) {return d.id;});
178
179       thumbs.exit().select("img").transition().duration(1000).style("height","0px").transition().duration(1050).remove();
180       thumbs.exit().transition().duration(1050).remove();
181       thumbs.enter().append("li")
182         .append("a")
183         .on("click", function(d) {
184             load_pic(d.id); }
185           )
186         .append("img")
187         .attr("src",function(d) {
188             s= d.base_uri+'/'+d.filename;
189             s = s.replace('file:\/\/'+dbprefix,webbase+'/Photos-tiny/');
190             return s;
191           })
192         .style("height","0px")
193         .transition().duration(500)
194         .style("height","100px");
195
196       /* resort elements */
197       d3.select(".nextprev").select("ul").selectAll("li").sort(function(a,b){return a.id-b.id;});
198       d3.select(".nextprev").select("ul").selectAll("li").select("a").select("img").classed("current",function(d){return (d.id==IDcurr);});
199
200
201       /* add links for left/right arrows */
202       if (IDprev != -1 )
203         d3.select(".left").on("click", function() { load_pic(IDprev); });
204       if (IDnext != -1 )
205         d3.select(".right").on("click", function() { load_pic(IDnext); });
206
207     });
208 }
209
210 function checkbutton()
211 {
212   if (page==1)
213     { d3.select(".pagination ul li:first-child").classed("disabled", true);}
214   else
215     { d3.select(".pagination ul li:first-child").classed("disabled", false);};
216
217   if (page==maxpage)
218     { d3.select(".pagination ul li:last-child").classed("disabled", true);}
219   else
220     { d3.select(".pagination ul li:last-child").classed("disabled", false);};
221 }
222
223 function update_page_index()
224 {
225   /* load number of pictures */
226   myID = "";
227   if(ID > 0)
228     myID = "&ID="+ID;
229
230   if(T!="")
231     url = webbase+"/getjson.php?C=1&T="+T+myID;
232   else
233     url = webbase+"/getjson.php?C=1"+myID;
234
235   d3.json(url, function(json) {
236       /* update index, show only page +-5 pages max */
237       n  = Math.floor( json[0].total/N   ); /* how many pages */
238       nr = Math.floor( (json[0].row-1)/N ); /* which row are we in? rowid starts at 1 not 0 */
239
240       maxpage=n+1;
241
242       if(nr > 0) page = nr+1;
243
244       var mydata = new Array();  // add json data  {page: <nr>, name: <name>} ; at end reform array into real json and use d3 to parse it
245
246       if(n>0)
247       {
248           mydata.push('{ page:0.1, name:"Prev"}');
249           if(page>4)
250           {
251               mydata.push('{ page:1, name:"1"}');
252               mydata.push('{ page:1.5, name:"..."}');
253               start = page-3;
254           }
255           else
256               start=1;
257
258           for(i=start;i<=Math.min(n+1,page+3);i++)
259               mydata.push('{ page:'+i+', name:"'+i+'"}');
260
261           if(page+3<n)
262           {
263               mydata.push('{ page:'+(n+0.5)+', name:"..."}');
264               mydata.push('{ page:'+(n+1)+', name:"'+(n+1)+'"}');
265           }
266           else if(page+3==n)
267               mydata.push('{ page:'+(n+1)+', name:"'+(n+1)+'"}');
268
269           mydata.push('{ page:'+(n+2.1)+', name:"Next"}');
270       }
271       else
272       {
273           mydata.push('{ page:0.1, name:"Prev"}');
274           mydata.push('{ page:1.1, name:"Next"}');
275       }
276
277       mydata = "["+mydata.join(",")+"]";
278       mydata =  eval('(' + mydata + ')');
279
280       /* remove old elements */
281       d3.selectAll(".pagination").select("ul").selectAll("li").remove();
282
283       /* create new ones */
284       var pageindex = d3.selectAll(".pagination").select("ul").selectAll("li").data(mydata, function(d){return d.page});
285       pageindex.selectAll("li").data(mydata, function(d){ return d.page; });
286       pageindex.enter().append("li").append("a")
287           .on("click", function(d) { if(  (d.page - Math.floor(d.page)) ==0 )  {page=d.page; ID=-1;load_content(); update_page_index();} })
288           .text(function(d) {return " "+d.name+" "});
289
290       pageindex.sort( function(a,b) { return a.page- b.page;} );
291
292       /* add callbacks to prev and next buttons */
293       d3.selectAll(".pagination ul li:first-child a").on("click", function(){prev_page();});
294       d3.selectAll(".pagination ul li:last-child  a").on("click", function(){next_page();});
295
296       d3.select(".pagination").select("ul").selectAll("li").classed("active", false);
297       d3.select(".pagination").select("ul").selectAll("li").classed("active", function(d) {return ( d.page == page ); });
298       checkbutton();
299     } );
300
301 }