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