2 copyright 2012,2013 Arun Persaud <arun@nubati.net>
4 This file is part of photo-tags.
6 Photo-tags is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Photo-tags is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Photo-tags. If not, see <http://www.gnu.org/licenses/>.
21 var pics = d3.select(".pics").select("ul");
27 /* populate data list with tags*/
28 d3.json(webbase+"/getjson.php?S", function(json) {
29 d3.select("#MyTags").selectAll("option").data(json)
30 .enter().append("option").attr("value",function(d) {return d.name});
33 /* update form to point to new link */
34 d3.select("input").on("keyup", function(d) {
35 d3.select('form').attr("action",webbase+"/tag/"+document.getElementById('MyTagsInput').value.replace(" ","+"));
38 d3.select("#currenttags").select("a").remove();
41 var mycurrenttags = T.split(",");
43 d3.select("#currenttags").selectAll("a")
44 .data(mycurrenttags).enter()
45 .append("a").attr("class","btn btn-small").text( function(d) {return d;} )
46 .on("mouseover", function(d){ d3.select(this).classed("btn-danger",true)})
47 .on("mouseout", function(d){ d3.select(this).classed("btn-danger",false)})
48 .attr("href", function(d) { return removeTag(T.split(","),d) });
52 function removeTag(alltags,removetag)
54 /* return a link to a page with 'removetag' removed from the array of tags*/
55 var index = alltags.indexOf(removetag);
56 alltags.splice(index, 1);
58 return webbase+'/tag/'+alltags.join(",");
64 function load_content() {
65 // d3.select(".debug").text("T,P,N = *"+T+"* *"+page+"* *"+N+"*");
68 url = webbase+"/getjson.php?ID="+ID;
70 url = webbase+"/getjson.php?T="+T+"&P="+page;
72 url = webbase+"/getjson.php?P="+page;
75 d3.json(url, function(json) {
77 pics.selectAll("li").remove();
80 /* if ID is set, just show one pictures, else create an array of pictures */
83 var singlepicspace=pics.selectAll("li").data(picdata, function(d){return ID;}).
84 enter().append("li").append("div").attr("class","singlepic");
85 singlepicspace.append("img").attr("class","left").attr("src",webbase+"/icons/left.png");
86 singlepicspace.append("img")
87 .attr("class","large")
88 .attr("src",function(d) {
89 s = d.base_uri+'/'+d.filename;
90 s = s.replace('file:\/\/'+dbprefix,webbase+'/Photos-small/');
93 singlepicspace.append("img").attr("class","right").attr("src",webbase+"/icons/right.png");
94 /* show description and time of photo */
95 singlepicspace.append("p")
97 var date = new Date(d.time*1000);
98 if (d.description=="")
99 return "Time: "+date.toUTCString();
101 return "Description: "+d.description + "Time: "+date.toUTCString();
104 d3.json(webbase+"/getjson.php?IDT="+ID, function(jsontag) {
106 d3.select("#pictags").selectAll("span").remove();
107 d3.select("#pictags").selectAll("span").data(jsontag)
108 .enter().append("span")
109 .attr("class","btn btn-small")
114 .on("mouseover", function(d){ d3.select(this).classed("btn-success",true)})
115 .on("mouseout", function(d){ d3.select(this).classed("btn-success",false)})
116 .on("click", function(d){ document.location.href=webbase+'/tag/'+d.name});
118 d3.select("#pictags").insert("span",":first-child").text("tags in current picture:");
125 d3.select(".nextprev").select("ul").selectAll("li").remove();
126 pics.selectAll("li").data(picdata)
127 .enter().append("li")
129 .on("click", function(d) { load_pic(d.id); })
131 .attr("src",function(d) {
133 s= d.base_uri+'/'+d.filename;
134 s = s.replace('file:\/\/'+dbprefix,webbase+'/Photos-tiny/');
143 function update_permalink() {
144 /* update permalink */
149 permalink += '/tag/' + T;
151 permalink += '/page/' + page;
153 permalink += '/pic/' + ID;
155 d3.select(".permalink").html("Permalink: <a href=\""+permalink+"\">"+permalink+"</a>");
158 function prev_page() {
159 if (page>1) page=page-1;
164 function next_page() {
165 if (page<maxpage) page=page+1;
170 function load_pic(myid) {
176 function tagcloud() {
178 url = webbase+"/getjson.php?CLOUD=1";
180 pics.selectAll("li").remove();
182 var svgelement=pics.append("li")
183 .append("svg").attr("width",400).attr("height",400);
186 d3.json(url, function(json) {
187 svgelement.selectAll("text").data(json).enter().append("text")
188 .style("font-size", function(d){return (Math.log(d.count+1)/3.0+0.5)+"em"})
189 .text(function(d) { return d.name+" "; })
190 .on("mouseover", function(d){ d3.select(this).style("color","red") })
191 .on("mouseout", function(d){ d3.select(this).style("color","black")})
192 .on("click", function(d) { document.location.href=webbase+'/tag/'+d.name});
196 function update_thumbnails(){
198 url2 = webbase+"/getjson.php?NP=1&T="+T+"&ID="+ID;
200 url2 = webbase+"/getjson.php?NP=1&ID="+ID;
205 d3.json(url2, function(json2) {
206 /* figure out where the arrows on the pic should link to */
208 for (var i in json2){
213 IDnext = json2[i].id;
217 var thumbs= d3.select(".nextprev").select("ul").selectAll("li").data(json2, function(d) {return d.id;});
219 thumbs.exit().select("img").transition().duration(1000).style("height","0px").transition().duration(1050).remove();
220 thumbs.exit().transition().duration(1050).remove();
221 thumbs.enter().append("li")
223 .on("click", function(d) {
227 .attr("src",function(d) {
228 s= d.base_uri+'/'+d.filename;
229 s = s.replace('file:\/\/'+dbprefix,webbase+'/Photos-tiny/');
232 .style("height","0px")
233 .transition().duration(500)
234 .style("height","100px");
236 /* resort elements */
237 d3.select(".nextprev").select("ul").selectAll("li").sort(function(a,b){return a.id-b.id;});
238 d3.select(".nextprev").select("ul").selectAll("li").select("a").select("img")
239 .classed("current",function(d){return (d.id==IDcurr);});
242 /* add links for left/right arrows */
244 d3.select(".left" ).on("click", function() { load_pic(IDprev); });
246 d3.select(".right").on("click", function() { load_pic(IDnext); });
248 /* keyboard shortcuts */
250 .keydown( function (e) {
251 if(e.which == 37 && IDprev != -1) { $(document).unbind('keydown'); load_pic(IDprev); }
252 if(e.which == 39 && IDnext != -1) { $(document).unbind('keydown'); load_pic(IDnext); }
257 function checkbutton()
260 { d3.select(".pagination ul li:first-child").classed("disabled", true);}
262 { d3.select(".pagination ul li:first-child").classed("disabled", false);};
265 { d3.select(".pagination ul li:last-child").classed("disabled", true);}
267 { d3.select(".pagination ul li:last-child").classed("disabled", false);};
270 function update_page_index()
272 /* load number of pictures */
278 url = webbase+"/getjson.php?C=1&T="+T+myID;
280 url = webbase+"/getjson.php?C=1"+myID;
282 d3.json(url, function(json) {
283 /* update index, show only page +-5 pages max */
284 n = Math.floor( json[0].total/N ); /* how many pages */
285 nr = Math.floor( (json[0].row-1)/N ); /* which row are we in? rowid starts at 1 not 0 */
289 if(nr > 0) page = nr+1;
291 var mydata = new Array(); // add json data {page: <nr>, name: <name>} ; at end reform array into real json and use d3 to parse it
295 mydata.push('{ page:0.1, name:"Prev"}');
298 mydata.push('{ page:1, name:"1"}');
299 mydata.push('{ page:1.5, name:"..."}');
305 for(i=start;i<=Math.min(n+1,page+3);i++)
306 mydata.push('{ page:'+i+', name:"'+i+'"}');
310 mydata.push('{ page:'+(n+0.5)+', name:"..."}');
311 mydata.push('{ page:'+(n+1)+', name:"'+(n+1)+'"}');
314 mydata.push('{ page:'+(n+1)+', name:"'+(n+1)+'"}');
316 mydata.push('{ page:'+(n+2.1)+', name:"Next"}');
320 mydata.push('{ page:0.1, name:"Prev"}');
321 mydata.push('{ page:1.1, name:"Next"}');
324 mydata = "["+mydata.join(",")+"]";
325 mydata = eval('(' + mydata + ')');
327 /* remove old elements */
328 d3.selectAll(".pagination").select("ul").selectAll("li").remove();
330 /* create new ones */
331 var pageindex = d3.selectAll(".pagination").select("ul").selectAll("li").data(mydata, function(d){return d.page});
332 pageindex.selectAll("li").data(mydata, function(d){ return d.page; });
333 pageindex.enter().append("li").append("a")
334 .on("click", function(d) { if( (d.page - Math.floor(d.page)) ==0 ) {page=d.page; ID=-1;load_content(); update_page_index();} })
335 .text(function(d) {return " "+d.name+" "});
337 pageindex.sort( function(a,b) { return a.page- b.page;} );
339 /* add callbacks to prev and next buttons */
340 d3.selectAll(".pagination ul li:first-child a").on("click", function(){prev_page();});
341 d3.selectAll(".pagination ul li:last-child a").on("click", function(){next_page();});
343 d3.select(".pagination").select("ul").selectAll("li").classed("active", false);
344 d3.select(".pagination").select("ul").selectAll("li").classed("active", function(d) {return ( d.page == page ); });