c178fe1eb3ed81a261976a1fcdbfd068a07f9ef8
[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     });
82
83    update_permalink()
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>=2) page=page-1;
103   load_content();
104 }
105
106 function next_page() {
107   page=page+1;
108   load_content();
109 }
110
111 function prev_pic() {
112 }
113
114 function next_pic() {
115 }
116
117 function load_pic(myid) {
118   ID=myid;
119   update_page_index();
120   update_thumbnails();
121   load_content();
122 }
123
124 function tagcloud() {
125
126   url = webbase+"/getjson.php?CLOUD=1";
127
128   pics.selectAll("li").remove();
129
130   var svgelement=pics.append("li")
131     .append("svg").attr("width",400).attr("height",400);
132
133   /* update pics */
134   d3.json(url, function(json) {
135       svgelement.selectAll("text").data(json).enter().append("text")
136         .style("font-size", function(d){return (Math.log(d.count+1)/2.0)+"em"})
137         .text(function(d) { return d.name+" "; })
138         .on("mouseover", function(d){ d3.select(this).style("color","red")} )
139         .on("mouseout", function(d){ d3.select(this).style("color","white")} )
140         .on("click", function(d) { document.location.href=webbase+'/tag/'+d.name })
141     });
142 }
143
144 function update_thumbnails(){
145   if(T!="")
146     url2 = webbase+"/getjson.php?NP=1&T="+T+"&ID="+ID;
147   else
148     url2 = webbase+"/getjson.php?NP=1&ID="+ID;
149
150   var IDprev=-1;
151   var IDnext=-1;
152   var IDcurr=-1;
153   d3.json(url2, function(json2) {
154       /* figure out where the arrows on the pic should link to */
155       all=""
156       for (var i in json2){
157         if( IDcurr != ID )
158           {
159             IDprev = IDcurr;
160             IDcurr = IDnext;
161             IDnext = json2[i].id;
162           };
163       }
164
165       var thumbs= d3.select(".nextprev").select("ul").selectAll("li").data(json2, function(d) {return d.id;});
166
167       thumbs.enter().append("li")
168         .append("a")
169         .on("click", function(d) {
170             load_pic(d.id); }
171           )
172         .append("img")
173         .attr("src",function(d) {
174             s= d.base_uri+'/'+d.filename;
175             s = s.replace('file:\/\/'+dbprefix,webbase+'/Photos-tiny/');
176             return s;
177           })
178         .style("height","0")
179         .transition().duration(1000)
180         .style("height","100px");
181
182       thumbs.exit().select("img").transition().duration(1000).style("height","0");
183       thumbs.exit().transition().duration(1050).remove();
184
185       /* resort elements */
186       d3.select(".nextprev").select("ul").selectAll("li").sort(function(a,b){return a.id-b.id;});
187       d3.select(".nextprev").select("ul").selectAll("li").select("a").select("img").classed("current",false);
188       d3.select(".nextprev").select("ul").selectAll("li").select("a").select("img").classed("current",function(d){return (d.id==IDcurr);});
189
190
191       /* add links for left/right arrows */
192       if (IDprev != -1 )
193         d3.select(".left").on("click", function() { load_pic(IDprev); });
194       if (IDnext != -1 )
195         d3.select(".right").on("click", function() { load_pic(IDnext); });
196
197     });
198 }
199
200 function checkbutton()
201 {
202   if (page==1)
203     { d3.select("button.prev").attr("disabled","disabled");}
204   else
205     { d3.select("button.prev").attr("disabled", null);};
206
207   if (count<N)
208     { d3.select("button.next").attr("disabled","disabled");}
209   else
210     { d3.select("button.next").attr("disabled",null);}
211 }
212
213 function update_page_index()
214 {
215   /* load number of pictures */
216   myID = "";
217   if(ID > 0)
218     myID = "&ID="+ID;
219
220   if(T!="")
221     url = webbase+"/getjson.php?C=1&T="+T+myID;
222   else
223     url = webbase+"/getjson.php?C=1"+myID;
224
225   d3.json(url, function(json) {
226     /* update index, show only page +-5 pages max */
227     n = Math.floor(json[0].total/N);
228     nr = Math.floor( (json[0].row-1)/N); /* rowid starts at 1 not 0 */
229
230     if(nr > 0)
231       page = nr+1;
232
233     var mydata = new Array();  // add json data  {page: <nr>, name: <name>} ; at end reform array into real json and use d3 to parse it
234
235     if(n>0)
236       {
237         if(page>7)
238           {
239             mydata.push('{ page:1, name:"1"}');
240             mydata.push('{ page:1.5, name:"..."}');
241             start = page-5;
242           }
243         else
244           start=1;
245
246         for(i=start;i<=Math.min(n+1,page+5);i++)
247           mydata.push('{ page:'+i+', name:"'+i+'"}');
248
249         if(page+5<n)
250           {
251             mydata.push('{ page:'+(n+0.5)+', name:"..."}');
252             mydata.push('{ page:'+(n+1)+', name:"'+(n+1)+'"}');
253           }
254         else if(page+5==n)
255           mydata.push('{ page:'+(n+1)+', name:"'+(n+1)+'"}');
256       };
257     mydata = "["+mydata.join(",")+"]";
258     mydata =  eval('(' + mydata + ')');
259
260     var pageindex = d3.select(".index").selectAll("button").data(mydata, function(d){ return d.page; });
261     pageindex.exit().remove();
262     pageindex.enter().append("button")
263       .on("click", function(d) { if(  (d.page - Math.floor(d.page)) ==0 )  {page=d.page; ID=-1;load_content(); update_page_index();} })
264       .text(function(d) {return " "+d.name+" "});
265     pageindex.sort( function(a,b) { return a.page- b.page;} );
266     } );
267
268   d3.select(".index").selectAll("button").classed("currentpage",false);
269   d3.select(".index").selectAll("button").classed("currentpage",function(d){return (d.page==page);});
270 }