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