]> git.scottworley.com Git - picsort/blame - picsorter.js
Add a third delete mode: blurry
[picsort] / picsorter.js
CommitLineData
31092212
SW
1var picinfo = {};
2if ("picsorter_picinfo" in localStorage) {
3 picinfo = JSON.parse(localStorage.picsorter_picinfo);
2d325f41 4}
31092212
SW
5$.each(files, function(i, f) {
6 if (!(f in picinfo)) {
7 picinfo[f] = {};
8 }
9});
01dd0481
SW
10function save_picinfo() {
11 localStorage.picsorter_picinfo = JSON.stringify(picinfo);
12}
13save_picinfo();
2d325f41 14
ca898dc7
SW
15var exposure = 20;
16var zoom = "sm/";
17var input_index = -1;
ca898dc7 18
8bc0aad4
SW
19function endsWith(str, suffix) {
20 return str.indexOf(suffix, str.length - suffix.length) !== -1;
21}
22
23function stripFromEnd(str, suffix) {
24 if (endsWith(str, suffix)) {
25 return str.substr(0, str.length - suffix.length);
26 }
27 return str;
28}
ca898dc7 29
ed9d7c54 30function setpic() {
cd368e97
SW
31 var display_filename;
32 if (endsWith(files[input_index], ".NEF")) {
33 if ("exposure" in picinfo[files[input_index]]) {
34 exposure = picinfo[files[input_index]].exposure;
35 } else {
36 picinfo[files[input_index]].exposure = exposure;
37 save_picinfo();
38 }
39 display_filename = zoom + exposure + "/" + stripFromEnd(files[input_index], ".NEF") + ".jpeg";
07f84670 40 } else {
cd368e97 41 display_filename = zoom + files[input_index];
07f84670 42 }
59a0fba1 43 var $pic = $("#pic");
6219f116
SW
44 $pic.on("load", function() {
45 $pic.removeClass("rot90 rot180 rot270");
46 if ("rotate" in picinfo[files[input_index]]) {
47 $pic.addClass("rot" + picinfo[files[input_index]].rotate);
48 }
49 });
59a0fba1 50 $pic.attr("src", display_filename);
9952a1a7
SW
51}
52
9c7566e1
SW
53function say(message) {
54 $("#message").text(message).removeClass("fade");
370e0b55 55 setTimeout(function() { $("#message").addClass("fade"); }, 100);
9c7566e1 56}
52c0fe1e 57function announce() {
8ade79c8
SW
58 var msg = input_index + " " + (picinfo[files[input_index]].name || "");
59 if ("deleted" in picinfo[files[input_index]]) {
60 msg += "(" + picinfo[files[input_index]].deleted+ ")";
61 }
62 say(msg);
52c0fe1e 63}
9c7566e1 64
9952a1a7
SW
65function toggle_zoom() {
66 if (zoom) {
67 zoom = "";
68 } else {
69 zoom = "sm/";
70 }
ac4559ea 71 setpic();
97c26a9c
SW
72}
73
1433682c
SW
74function move_by_filter(direction, filter) {
75 // Keep moving in direction until filter is satisfied
755cf903
SW
76 var new_index = input_index;
77 while (true) {
78 var next = new_index + direction;
79 if (next < 0) {
80 say("At beginning");
81 return;
82 }
83 if (next >= files.length) {
84 say("At end");
85 return;
86 }
87 new_index = next;
88 if (filter(new_index)) {
89 break;
90 }
91 }
92 input_index = new_index;
52c0fe1e 93 announce();
c0230e38 94 setpic();
2d325f41 95}
1433682c
SW
96function move(direction) {
97 move_by_filter(direction, function() { return true; });
98}
8a9d6b07
SW
99function move_to_begenning() {
100 move_by_filter(-1, function(i) { return i == 0; });
101}
102function move_to_end() {
103 move_by_filter(1, function(i) { return i == files.length - 1; });
104}
1433682c 105function move_to_nondeleted(direction) {
755cf903
SW
106 move_by_filter(direction, function(i) {
107 return !("deleted" in picinfo[files[i]]); });
1433682c
SW
108}
109function move_to_unnamed(direction) {
755cf903
SW
110 move_by_filter(direction, function(i) {
111 return !("deleted" in picinfo[files[i]]) &&
112 !("name" in picinfo[files[i]]); });
1433682c 113}
2d325f41 114
aa798f2c
SW
115function mark_deleted(method) {
116 picinfo[files[input_index]].deleted = method;
01dd0481 117 save_picinfo();
aa798f2c 118 say(method);
2d325f41
SW
119}
120
121function mark_not_deleted() {
31092212 122 delete picinfo[files[input_index]].deleted;
01dd0481 123 save_picinfo();
9c7566e1 124 say("Undeleted");
2d325f41
SW
125}
126
1899b67c
SW
127function say_exposure() {
128 if (!endsWith(files[input_index], ".NEF")) {
129 say("Exposure adjustment not available");
130 return;
131 }
132 var display_exposure = (exposure / 4) - 3.5;
133 say((display_exposure >= 0 ? "+" : "") + display_exposure);
134}
135
138122d7 136function change_exposure(amount) {
b11d9c69
SW
137 if (!endsWith(files[input_index], ".NEF")) {
138 say("Exposure adjustment not available");
139 return;
140 }
138122d7 141 exposure += amount;
07f84670 142 picinfo[files[input_index]].exposure = exposure;
01dd0481 143 save_picinfo();
138122d7 144 setpic();
1899b67c 145 say_exposure();
138122d7
SW
146}
147
59a0fba1
SW
148function rotate() {
149 var rotation = picinfo[files[input_index]].rotate || 0;
150 rotation = (rotation + 90) % 360;
151 if (rotation > 1e-5) {
152 picinfo[files[input_index]].rotate = rotation;
153 } else {
154 delete picinfo[files[input_index]].rotate;
155 }
156 save_picinfo();
157 setpic();
158}
159
0799b4b3
SW
160
161function set_name(name) {
7b24c22c
SW
162 if (name) {
163 picinfo[files[input_index]].name = name;
164 save_picinfo();
165 say("Named " + name);
166 last_name = name;
167 }
0799b4b3
SW
168}
169
170function set_name_from_form() {
171 var name_input = $("#name").hide().get(0);
172 set_name(name_input.value);
173 name_input.value = "";
d92eb78e
SW
174}
175
eaa5e016
SW
176function shell_escape(x) {
177 return x.replace(/'/g, "'\\''");
178}
179
180function show_commands() {
181 var commands = [];
182 $.each(files, function(i, f) {
fcfd9f01
SW
183 var escaped_filename = "'" + shell_escape(f) + "'";
184 if (picinfo[f].deleted == "deleted") {
185 commands.push("shred -u " + escaped_filename);
186 } else if (picinfo[f].deleted == "extra") {
53194dd7 187 commands.push("mv -vi " + escaped_filename + " \"$EXTRADIR\"");
5875d960
SW
188 } else if (picinfo[f].deleted == "blurry") {
189 commands.push("mv -vi " + escaped_filename + " \"$BLURRYDIR\"");
fcfd9f01 190 } else if ("name" in picinfo[f] && picinfo[f].name.length > 0) {
eaa5e016
SW
191 var command = ["pic-mv"];
192 if ("exposure" in picinfo[f]) {
193 command.push("-e " + picinfo[f].exposure);
194 }
195 if ("rotate" in picinfo[f]) {
196 command.push("-r " + picinfo[f].rotate);
197 }
61332449 198 command.push(escaped_filename);
f20ac636 199 command.push("'" + shell_escape(picinfo[f].name) + "'");
eaa5e016
SW
200 commands.push(command.join(" "));
201 }
202 });
0f889466 203 commands.push("");
eaa5e016
SW
204 $("#shell_out").text(commands.join("\n")).show();
205}
206
5d9946cb 207$(function() {
0799b4b3 208 $("#name").hide().on("keyup", function(e) { e.which == 13 && set_name_from_form(); });
c0230e38 209 move_to_nondeleted(1);
e18c13a6 210});
ecb5b4ef 211
ac4559ea 212Mousetrap.bind('z', toggle_zoom);
9952a1a7 213Mousetrap.bind('Z', function() { $("#pic").toggleClass("fit_view"); });
a5ef2857
SW
214Mousetrap.bind(['n', 'l'], function() { move_to_nondeleted(1); });
215Mousetrap.bind(['p', 'h'], function() { move_to_nondeleted(-1); });
1433682c
SW
216Mousetrap.bind(['N', 'L'], function() { move_to_unnamed(1); });
217Mousetrap.bind(['P', 'H'], function() { move_to_unnamed(-1); });
218Mousetrap.bind(['m n', 'm l'], function() { move(1); });
219Mousetrap.bind(['m n', 'm h'], function() { move(-1); });
a5ef2857
SW
220Mousetrap.bind(['b', 'k'], function() { change_exposure(1); });
221Mousetrap.bind(['d', 'j'], function() { change_exposure(-1); });
8a9d6b07
SW
222Mousetrap.bind('0', move_to_begenning);
223Mousetrap.bind('$', move_to_end);
aa798f2c 224Mousetrap.bind('x', function(){ mark_deleted("deleted"); });
ac4559ea 225Mousetrap.bind('X', mark_not_deleted);
22c8d6e6
SW
226Mousetrap.bind('e', function(){ mark_deleted("extra"); });
227Mousetrap.bind('E', mark_not_deleted);
5875d960
SW
228Mousetrap.bind('g', function(){ mark_deleted("blurry"); });
229Mousetrap.bind('G', mark_not_deleted);
59a0fba1 230Mousetrap.bind('r', rotate);
52c0fe1e 231Mousetrap.bind('i', announce);
88880347 232Mousetrap.bind('f', function() { say(files[input_index]); });
d3aedd16 233Mousetrap.bind('B', function() { say_exposure(); });
d92eb78e 234Mousetrap.bind('c', function() { $("#name").show().focus(); return false; });
0799b4b3 235Mousetrap.bind('C', function() { if (last_name) { set_name(last_name); } });
76812d8f 236Mousetrap.bind('%', function() { say((100 * input_index / files.length).toFixed(2) + "%"); });
eaa5e016 237Mousetrap.bind('!', show_commands);
de26d1f8 238Mousetrap.bind('esc', function() { $("#name").hide(); $("#shell_out").hide(); });
59b4cd23 239
8cec3490
SW
240function clean_picinfo() {
241 files_index = {};
242 $.each(files, function(i, f) {
243 files_index[f] = true;
244 });
245 for (f in picinfo) {
246 if (!files_index[f]) {
247 delete picinfo[f];
248 }
249 }
250}
251
59b4cd23
SW
252function undelete_all() {
253 for (f in picinfo) {
254 delete picinfo[f].deleted;
255 }
256 save_picinfo();
257 say("Undeleted everything");
258}