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