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