]> git.scottworley.com Git - picsort/blob - picsorter.js
2f9df0ce94ee80aa2fa9d0781709df0eb7ddcde0
[picsort] / picsorter.js
1 var picinfo = {};
2 if ("picsorter_picinfo" in localStorage) {
3 picinfo = JSON.parse(localStorage.picsorter_picinfo);
4 }
5 $.each(files, function(i, f) {
6 if (!(f in picinfo)) {
7 picinfo[f] = {};
8 }
9 });
10 function save_picinfo() {
11 localStorage.picsorter_picinfo = JSON.stringify(picinfo);
12 }
13 save_picinfo();
14
15 var exposure = 20;
16 var zoom = "sm/";
17 var input_index = -1;
18
19 function endsWith(str, suffix) {
20 return str.indexOf(suffix, str.length - suffix.length) !== -1;
21 }
22
23 function stripFromEnd(str, suffix) {
24 if (endsWith(str, suffix)) {
25 return str.substr(0, str.length - suffix.length);
26 }
27 return str;
28 }
29
30 function setpic() {
31 if ("exposure" in picinfo[files[input_index]]) {
32 exposure = picinfo[files[input_index]].exposure;
33 } else {
34 picinfo[files[input_index]].exposure = exposure;
35 save_picinfo();
36 }
37 $("#pic").attr("src", zoom + exposure + "/" + files[input_index]);
38 }
39
40 function say(message) {
41 $("#message").text(message).removeClass("fade");
42 setTimeout(function() { $("#message").addClass("fade"); }, 1);
43 }
44
45 function toggle_zoom() {
46 if (zoom) {
47 zoom = "";
48 } else {
49 zoom = "sm/";
50 }
51 setpic();
52 }
53
54 function move_to_nondeleted(direction) {
55 do {
56 input_index += direction;
57 } while (picinfo[files[input_index]].deleted);
58 say(input_index + " " + (picinfo[files[input_index]].name || ""));
59 setpic();
60 }
61
62 function mark_deleted() {
63 picinfo[files[input_index]].deleted = 1;
64 save_picinfo();
65 say("Deleted");
66 }
67
68 function mark_not_deleted() {
69 delete picinfo[files[input_index]].deleted;
70 save_picinfo();
71 say("Undeleted");
72 }
73
74 function change_exposure(amount) {
75 exposure += amount;
76 picinfo[files[input_index]].exposure = exposure;
77 save_picinfo();
78 setpic();
79 var display_exposure = (exposure / 4) - 3.5;
80 say((display_exposure >= 0 ? "+" : "") + display_exposure);
81 }
82
83 function set_name() {
84 var name_input = $("#name").hide().get(0);
85 var name = name_input.value;
86 name_input.value = "";
87 picinfo[files[input_index]].name = name;
88 save_picinfo();
89 say("Named " + name);
90 }
91
92 $(function() {
93 $("#name").hide().on("keyup", function(e) { e.which == 13 && set_name(); });
94 move_to_nondeleted(1);
95 });
96
97 Mousetrap.bind('z', toggle_zoom);
98 Mousetrap.bind('Z', function() { $("#pic").toggleClass("fit_view"); });
99 Mousetrap.bind('n', function() { move_to_nondeleted(1); });
100 Mousetrap.bind('p', function() { move_to_nondeleted(-1); });
101 Mousetrap.bind('N', function() { input_index ++; say(input_index); setpic(); });
102 Mousetrap.bind('P', function() { input_index --; say(input_index); setpic(); });
103 Mousetrap.bind('b', function() { change_exposure(1); });
104 Mousetrap.bind('d', function() { change_exposure(-1); });
105 Mousetrap.bind('x', mark_deleted);
106 Mousetrap.bind('X', mark_not_deleted);
107 Mousetrap.bind('c', function() { $("#name").show().focus(); return false; });