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