better display of description and time for a single photo
[photo-tags.git] / js / photo-tags.js
1   /**
2     copyright 2012,2013 Arun Persaud <arun@nubati.net>
3
4     This file is part of photo-tags.
5
6     Photo-tags is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     Photo-tags is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with Photo-tags.  If not, see <http://www.gnu.org/licenses/>.
18
19   **/
20
21 var pics = d3.select(".pics").select("ul");
22 var page;
23 var maxpage;
24
25 function init()
26 {
27   /* populate data list with tags*/
28   d3.json(webbase+"/getjson.php?S", function(json) {
29       d3.select("#MyTags").selectAll("option").data(json)
30         .enter().append("option").attr("value",function(d) {return d.name});
31     });
32
33   /* update form to point to new link */
34   d3.select("input").on("keyup", function(d) {
35       d3.select('form').attr("action",webbase+"/tag/"+document.getElementById('MyTagsInput').value.replace(" ","+"));
36   });
37
38   d3.select("#currenttags").select("a").remove();
39   if (T!="")
40     {
41       var mycurrenttags = T.split(",");
42
43       d3.select("#currenttags").selectAll("a")
44         .data(mycurrenttags).enter()
45         .append("a").attr("class","btn btn-small").text( function(d) {return d;} )
46         .on("mouseover", function(d){ d3.select(this).classed("btn-danger",true)})
47         .on("mouseout", function(d){ d3.select(this).classed("btn-danger",false)})
48         .attr("href", function(d) { return removeTag(T.split(","),d) });
49     };
50 }
51
52 function removeTag(alltags,removetag)
53 {
54     /* return a link to a page with 'removetag' removed from the array of tags*/
55     var index = alltags.indexOf(removetag);
56     alltags.splice(index, 1);
57     if (alltags.length)
58         return webbase+'/tag/'+alltags.join(",");
59     else
60         return webbase;
61 }
62
63
64 function load_content() {
65   // d3.select(".debug").text("T,P,N = *"+T+"* *"+page+"* *"+N+"*");
66
67   if (ID>=0)
68     url = webbase+"/getjson.php?ID="+ID;
69   else if(T!="")
70     url = webbase+"/getjson.php?T="+T+"&P="+page;
71   else
72     url = webbase+"/getjson.php?P="+page;
73
74   /* update pics */
75   d3.json(url, function(json) {
76       count=0;
77       pics.selectAll("li").remove();
78       picdata=json;
79
80       /* if ID is set, just show one pictures, else create an array of pictures */
81       if (ID>=0)
82         {
83             var singlepicspace=pics.selectAll("li").data(picdata, function(d){return ID;}).
84                 enter().append("li").append("div").attr("class","singlepic");
85             singlepicspace.append("img").attr("class","left").attr("src",webbase+"/icons/left.png");
86             singlepicspace.append("img")
87                 .attr("class","large")
88                 .attr("src",function(d) {
89                     s = d.base_uri+'/'+d.filename;
90                     s = s.replace('file:\/\/'+dbprefix,webbase+'/Photos-small/');
91                     return s;
92                 });
93             singlepicspace.append("img").attr("class","right").attr("src",webbase+"/icons/right.png");
94             /* show description and time of photo */
95             singlepicspace.append("p")
96                 .text(function(d) {
97                     var date = new Date(d.time*1000);
98                     if (d.description=="")
99                         return " "+date.toUTCString();
100                     else
101                         return " "+d.description + " "+date.toUTCString();
102                 });
103             /* show tags */
104             d3.json(webbase+"/getjson.php?IDT="+ID, function(jsontag) {
105                 tags=0;
106                 d3.select("#pictags").selectAll("span").remove();
107                 d3.select("#pictags").selectAll("span").data(jsontag)
108                     .enter().append("span")
109                     .attr("class","btn btn-small")
110                     .text( function(t) {
111                         tags=1;
112                         return t.name;
113                     })
114                     .on("mouseover", function(d){ d3.select(this).classed("btn-success",true)})
115                     .on("mouseout",  function(d){ d3.select(this).classed("btn-success",false)})
116                     .on("click",     function(d){ document.location.href=webbase+'/tag/'+d.name});
117                 if(tags)
118                 d3.select("#pictags").insert("span",":first-child").text("tags in current picture:");
119             });
120
121             update_thumbnails();
122         }
123       else
124         {
125           d3.select(".nextprev").select("ul").selectAll("li").remove();
126           pics.selectAll("li").data(picdata)
127             .enter().append("li")
128             .append("a")
129             .on("click", function(d) { load_pic(d.id); })
130             .append("img")
131             .attr("src",function(d) {
132                 count++;
133                 s= d.base_uri+'/'+d.filename;
134                 s = s.replace('file:\/\/'+dbprefix,webbase+'/Photos-tiny/');
135                 return s;
136               });
137         };
138       checkbutton();
139       update_permalink()
140     });
141 }
142
143 function update_permalink() {
144   /* update permalink */
145
146   permalink = webbase;
147
148   if(T!="")
149     permalink += '/tag/' + T;
150   if(page!=1)
151     permalink += '/page/' + page;
152   if(ID>0)
153     permalink += '/pic/' + ID;
154
155   d3.select(".permalink").html("Permalink: <a href=\""+permalink+"\">"+permalink+"</a>");
156 }
157
158 function prev_page() {
159     if (page>1) page=page-1;
160     load_content();
161     update_page_index();
162 }
163
164 function next_page() {
165     if (page<maxpage) page=page+1;
166     load_content();
167     update_page_index();
168 }
169
170 function load_pic(myid) {
171   ID=myid;
172   update_page_index();
173   load_content();
174 }
175
176 function tagcloud() {
177
178   url = webbase+"/getjson.php?CLOUD=1";
179
180   pics.selectAll("li").remove();
181
182   var svgelement=pics.append("li")
183     .append("svg").attr("width",400).attr("height",400);
184
185   /* update pics */
186   d3.json(url, function(json) {
187       svgelement.selectAll("text").data(json).enter().append("text")
188         .style("font-size", function(d){return (Math.log(d.count+1)/3.0+0.5)+"em"})
189         .text(function(d) { return d.name+" "; })
190         .on("mouseover", function(d){ d3.select(this).style("color","red")  })
191         .on("mouseout",  function(d){ d3.select(this).style("color","black")})
192         .on("click", function(d) { document.location.href=webbase+'/tag/'+d.name});
193     });
194 }
195
196 function update_thumbnails(){
197   if(T!="")
198     url2 = webbase+"/getjson.php?NP=1&T="+T+"&ID="+ID;
199   else
200     url2 = webbase+"/getjson.php?NP=1&ID="+ID;
201
202   var IDprev=-1;
203   var IDnext=-1;
204   var IDcurr=-1;
205   d3.json(url2, function(json2) {
206       /* figure out where the arrows on the pic should link to */
207       all=""
208       for (var i in json2){
209         if( IDcurr != ID )
210           {
211             IDprev = IDcurr;
212             IDcurr = IDnext;
213             IDnext = json2[i].id;
214           };
215       }
216
217       var thumbs= d3.select(".nextprev").select("ul").selectAll("li").data(json2, function(d) {return d.id;});
218
219       thumbs.exit().select("img").transition().duration(1000).style("height","0px").transition().duration(1050).remove();
220       thumbs.exit().transition().duration(1050).remove();
221       thumbs.enter().append("li")
222         .append("a")
223         .on("click", function(d) {
224             load_pic(d.id); }
225           )
226         .append("img")
227         .attr("src",function(d) {
228             s= d.base_uri+'/'+d.filename;
229             s = s.replace('file:\/\/'+dbprefix,webbase+'/Photos-tiny/');
230             return s;
231           })
232         .style("height","0px")
233         .transition().duration(500)
234         .style("height","100px");
235
236       /* resort elements */
237       d3.select(".nextprev").select("ul").selectAll("li").sort(function(a,b){return a.id-b.id;});
238       d3.select(".nextprev").select("ul").selectAll("li").select("a").select("img")
239           .classed("current",function(d){return (d.id==IDcurr);});
240
241
242       /* add links for left/right arrows */
243       if (IDprev != -1 )
244           d3.select(".left" ).on("click", function() { load_pic(IDprev); });
245       if (IDnext != -1 )
246           d3.select(".right").on("click", function() { load_pic(IDnext); });
247
248       /* keyboard shortcuts */
249       $(document)
250           .keydown(  function (e) {
251               if(e.which == 37 && IDprev != -1) { $(document).unbind('keydown'); load_pic(IDprev);  }
252               if(e.which == 39 && IDnext != -1) { $(document).unbind('keydown'); load_pic(IDnext);  }
253           });
254   });
255 }
256
257 function checkbutton()
258 {
259   if (page==1)
260     { d3.select(".pagination ul li:first-child").classed("disabled", true);}
261   else
262     { d3.select(".pagination ul li:first-child").classed("disabled", false);};
263
264   if (page==maxpage)
265     { d3.select(".pagination ul li:last-child").classed("disabled", true);}
266   else
267     { d3.select(".pagination ul li:last-child").classed("disabled", false);};
268 }
269
270 function update_page_index()
271 {
272   /* load number of pictures */
273   myID = "";
274   if(ID > 0)
275     myID = "&ID="+ID;
276
277   if(T!="")
278     url = webbase+"/getjson.php?C=1&T="+T+myID;
279   else
280     url = webbase+"/getjson.php?C=1"+myID;
281
282   d3.json(url, function(json) {
283       /* update index, show only page +-5 pages max */
284       n  = Math.floor( json[0].total/N   ); /* how many pages */
285       nr = Math.floor( (json[0].row-1)/N ); /* which row are we in? rowid starts at 1 not 0 */
286
287       maxpage=n+1;
288
289       if(nr > 0) page = nr+1;
290
291       var mydata = new Array();  // add json data  {page: <nr>, name: <name>} ; at end reform array into real json and use d3 to parse it
292
293       if(n>0)
294       {
295           mydata.push('{ page:0.1, name:"Prev"}');
296           if(page>4)
297           {
298               mydata.push('{ page:1, name:"1"}');
299               mydata.push('{ page:1.5, name:"..."}');
300               start = page-3;
301           }
302           else
303               start=1;
304
305           for(i=start;i<=Math.min(n+1,page+3);i++)
306               mydata.push('{ page:'+i+', name:"'+i+'"}');
307
308           if(page+3<n)
309           {
310               mydata.push('{ page:'+(n+0.5)+', name:"..."}');
311               mydata.push('{ page:'+(n+1)+', name:"'+(n+1)+'"}');
312           }
313           else if(page+3==n)
314               mydata.push('{ page:'+(n+1)+', name:"'+(n+1)+'"}');
315
316           mydata.push('{ page:'+(n+2.1)+', name:"Next"}');
317       }
318       else
319       {
320           mydata.push('{ page:0.1, name:"Prev"}');
321           mydata.push('{ page:1.1, name:"Next"}');
322       }
323
324       mydata = "["+mydata.join(",")+"]";
325       mydata =  eval('(' + mydata + ')');
326
327       /* remove old elements */
328       d3.selectAll(".pagination").select("ul").selectAll("li").remove();
329
330       /* create new ones */
331       var pageindex = d3.selectAll(".pagination").select("ul").selectAll("li").data(mydata, function(d){return d.page});
332       pageindex.selectAll("li").data(mydata, function(d){ return d.page; });
333       pageindex.enter().append("li").append("a")
334           .on("click", function(d) { if(  (d.page - Math.floor(d.page)) ==0 )  {page=d.page; ID=-1;load_content(); update_page_index();} })
335           .text(function(d) {return " "+d.name+" "});
336
337       pageindex.sort( function(a,b) { return a.page- b.page;} );
338
339       /* add callbacks to prev and next buttons */
340       d3.selectAll(".pagination ul li:first-child a").on("click", function(){prev_page();});
341       d3.selectAll(".pagination ul li:last-child  a").on("click", function(){next_page();});
342
343       d3.select(".pagination").select("ul").selectAll("li").classed("active", false);
344       d3.select(".pagination").select("ul").selectAll("li").classed("active", function(d) {return ( d.page == page ); });
345       checkbutton();
346     } );
347
348 }