b657fbef1630dce5969e1490eb8407d45c64bd7d
[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;}).
64                 enter().append("li").append("div").attr("class","singlepic");
65             singlepicspace.append("img").attr("class","left").attr("src",webbase+"/icons/left.png");
66             singlepicspace.append("img")
67                 .attr("class","large")
68                 .attr("src",function(d) {
69                     s = d.base_uri+'/'+d.filename;
70                     s = s.replace('file:\/\/'+dbprefix,webbase+'/Photos-small/');
71                     return s;
72                 });
73             singlepicspace.append("img").attr("class","right").attr("src",webbase+"/icons/right.png");
74             /* show description and time of photo */
75             singlepicspace.append("p")
76                 .text(function(d) {
77                     var date = new Date(d.time*1000);
78                     if (d.description=="")
79                         return "Time: "+date.toUTCString();
80                     else
81                         return "Description: "+d.description + "Time: "+date.toUTCString();
82                 });
83             /* show tags */
84             d3.json(webbase+"/getjson.php?IDT="+ID, function(jsontag) {
85                 tags=0;
86                 d3.select("#pictags").selectAll("span").remove();
87                 d3.select("#pictags").selectAll("span").data(jsontag)
88                     .enter().append("span")
89                     .attr("class","btn btn-small")
90                     .text( function(t) {
91                         tags=1;
92                         return t.name;
93                     })
94                     .on("mouseover", function(d){ d3.select(this).classed("btn-success",true)})
95                     .on("mouseout",  function(d){ d3.select(this).classed("btn-success",false)})
96                     .on("click",     function(d){ document.location.href=webbase+'/tag/'+d.name});
97                 if(tags)
98                 d3.select("#pictags").insert("span",":first-child").text("tags in current picture:");
99             });
100
101             update_thumbnails();
102         }
103       else
104         {
105           d3.select(".nextprev").select("ul").selectAll("li").remove();
106           pics.selectAll("li").data(picdata)
107             .enter().append("li")
108             .append("a")
109             .on("click", function(d) { load_pic(d.id); })
110             .append("img")
111             .attr("src",function(d) {
112                 count++;
113                 s= d.base_uri+'/'+d.filename;
114                 s = s.replace('file:\/\/'+dbprefix,webbase+'/Photos-tiny/');
115                 return s;
116               });
117         };
118       checkbutton();
119       update_permalink()
120     });
121 }
122
123 function update_permalink() {
124   /* update permalink */
125
126   permalink = webbase;
127
128   if(T!="")
129     permalink += '/tag/' + T;
130   if(page!=1)
131     permalink += '/page/' + page;
132   if(ID>0)
133     permalink += '/pic/' + ID;
134
135   d3.select(".permalink").html("Permalink: <a href=\""+permalink+"\">"+permalink+"</a>");
136 }
137
138 function prev_page() {
139     if (page>1) page=page-1;
140     load_content();
141     update_page_index();
142 }
143
144 function next_page() {
145     if (page<maxpage) page=page+1;
146     load_content();
147     update_page_index();
148 }
149
150 function load_pic(myid) {
151   ID=myid;
152   update_page_index();
153   load_content();
154 }
155
156 function tagcloud() {
157
158   url = webbase+"/getjson.php?CLOUD=1";
159
160   pics.selectAll("li").remove();
161
162   var svgelement=pics.append("li")
163     .append("svg").attr("width",400).attr("height",400);
164
165   /* update pics */
166   d3.json(url, function(json) {
167       svgelement.selectAll("text").data(json).enter().append("text")
168         .style("font-size", function(d){return (Math.log(d.count+1)/3.0+0.5)+"em"})
169         .text(function(d) { return d.name+" "; })
170         .on("mouseover", function(d){ d3.select(this).style("color","red")  })
171         .on("mouseout",  function(d){ d3.select(this).style("color","black")})
172         .on("click", function(d) { document.location.href=webbase+'/tag/'+d.name});
173     });
174 }
175
176 function update_thumbnails(){
177   if(T!="")
178     url2 = webbase+"/getjson.php?NP=1&T="+T+"&ID="+ID;
179   else
180     url2 = webbase+"/getjson.php?NP=1&ID="+ID;
181
182   var IDprev=-1;
183   var IDnext=-1;
184   var IDcurr=-1;
185   d3.json(url2, function(json2) {
186       /* figure out where the arrows on the pic should link to */
187       all=""
188       for (var i in json2){
189         if( IDcurr != ID )
190           {
191             IDprev = IDcurr;
192             IDcurr = IDnext;
193             IDnext = json2[i].id;
194           };
195       }
196
197       var thumbs= d3.select(".nextprev").select("ul").selectAll("li").data(json2, function(d) {return d.id;});
198
199       thumbs.exit().select("img").transition().duration(1000).style("height","0px").transition().duration(1050).remove();
200       thumbs.exit().transition().duration(1050).remove();
201       thumbs.enter().append("li")
202         .append("a")
203         .on("click", function(d) {
204             load_pic(d.id); }
205           )
206         .append("img")
207         .attr("src",function(d) {
208             s= d.base_uri+'/'+d.filename;
209             s = s.replace('file:\/\/'+dbprefix,webbase+'/Photos-tiny/');
210             return s;
211           })
212         .style("height","0px")
213         .transition().duration(500)
214         .style("height","100px");
215
216       /* resort elements */
217       d3.select(".nextprev").select("ul").selectAll("li").sort(function(a,b){return a.id-b.id;});
218       d3.select(".nextprev").select("ul").selectAll("li").select("a").select("img")
219           .classed("current",function(d){return (d.id==IDcurr);});
220
221
222       /* add links for left/right arrows */
223       if (IDprev != -1 )
224           d3.select(".left" ).on("click", function() { load_pic(IDprev); });
225       if (IDnext != -1 )
226           d3.select(".right").on("click", function() { load_pic(IDnext); });
227
228       /* keyboard shortcuts */
229       $(document)
230           .keydown(  function (e) {
231               if(e.which == 37 && IDprev != -1) { $(document).unbind('keydown'); load_pic(IDprev);  }
232               if(e.which == 39 && IDnext != -1) { $(document).unbind('keydown'); load_pic(IDnext);  }
233           });
234   });
235 }
236
237 function checkbutton()
238 {
239   if (page==1)
240     { d3.select(".pagination ul li:first-child").classed("disabled", true);}
241   else
242     { d3.select(".pagination ul li:first-child").classed("disabled", false);};
243
244   if (page==maxpage)
245     { d3.select(".pagination ul li:last-child").classed("disabled", true);}
246   else
247     { d3.select(".pagination ul li:last-child").classed("disabled", false);};
248 }
249
250 function update_page_index()
251 {
252   /* load number of pictures */
253   myID = "";
254   if(ID > 0)
255     myID = "&ID="+ID;
256
257   if(T!="")
258     url = webbase+"/getjson.php?C=1&T="+T+myID;
259   else
260     url = webbase+"/getjson.php?C=1"+myID;
261
262   d3.json(url, function(json) {
263       /* update index, show only page +-5 pages max */
264       n  = Math.floor( json[0].total/N   ); /* how many pages */
265       nr = Math.floor( (json[0].row-1)/N ); /* which row are we in? rowid starts at 1 not 0 */
266
267       maxpage=n+1;
268
269       if(nr > 0) page = nr+1;
270
271       var mydata = new Array();  // add json data  {page: <nr>, name: <name>} ; at end reform array into real json and use d3 to parse it
272
273       if(n>0)
274       {
275           mydata.push('{ page:0.1, name:"Prev"}');
276           if(page>4)
277           {
278               mydata.push('{ page:1, name:"1"}');
279               mydata.push('{ page:1.5, name:"..."}');
280               start = page-3;
281           }
282           else
283               start=1;
284
285           for(i=start;i<=Math.min(n+1,page+3);i++)
286               mydata.push('{ page:'+i+', name:"'+i+'"}');
287
288           if(page+3<n)
289           {
290               mydata.push('{ page:'+(n+0.5)+', name:"..."}');
291               mydata.push('{ page:'+(n+1)+', name:"'+(n+1)+'"}');
292           }
293           else if(page+3==n)
294               mydata.push('{ page:'+(n+1)+', name:"'+(n+1)+'"}');
295
296           mydata.push('{ page:'+(n+2.1)+', name:"Next"}');
297       }
298       else
299       {
300           mydata.push('{ page:0.1, name:"Prev"}');
301           mydata.push('{ page:1.1, name:"Next"}');
302       }
303
304       mydata = "["+mydata.join(",")+"]";
305       mydata =  eval('(' + mydata + ')');
306
307       /* remove old elements */
308       d3.selectAll(".pagination").select("ul").selectAll("li").remove();
309
310       /* create new ones */
311       var pageindex = d3.selectAll(".pagination").select("ul").selectAll("li").data(mydata, function(d){return d.page});
312       pageindex.selectAll("li").data(mydata, function(d){ return d.page; });
313       pageindex.enter().append("li").append("a")
314           .on("click", function(d) { if(  (d.page - Math.floor(d.page)) ==0 )  {page=d.page; ID=-1;load_content(); update_page_index();} })
315           .text(function(d) {return " "+d.name+" "});
316
317       pageindex.sort( function(a,b) { return a.page- b.page;} );
318
319       /* add callbacks to prev and next buttons */
320       d3.selectAll(".pagination ul li:first-child a").on("click", function(){prev_page();});
321       d3.selectAll(".pagination ul li:last-child  a").on("click", function(){next_page();});
322
323       d3.select(".pagination").select("ul").selectAll("li").classed("active", false);
324       d3.select(".pagination").select("ul").selectAll("li").classed("active", function(d) {return ( d.page == page ); });
325       checkbutton();
326     } );
327
328 }