]> git.scottworley.com Git - picsort/blame - picsorter.js
Use the correct filename for deleting
[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
SW
57function announce() {
58 say(input_index + " " + (picinfo[files[input_index]].name || ""));
59}
9c7566e1 60
9952a1a7
SW
61function toggle_zoom() {
62 if (zoom) {
63 zoom = "";
64 } else {
65 zoom = "sm/";
66 }
ac4559ea 67 setpic();
97c26a9c
SW
68}
69
1433682c
SW
70function move_by_filter(direction, filter) {
71 // Keep moving in direction until filter is satisfied
755cf903
SW
72 var new_index = input_index;
73 while (true) {
74 var next = new_index + direction;
75 if (next < 0) {
76 say("At beginning");
77 return;
78 }
79 if (next >= files.length) {
80 say("At end");
81 return;
82 }
83 new_index = next;
84 if (filter(new_index)) {
85 break;
86 }
87 }
88 input_index = new_index;
52c0fe1e 89 announce();
c0230e38 90 setpic();
2d325f41 91}
1433682c
SW
92function move(direction) {
93 move_by_filter(direction, function() { return true; });
94}
8a9d6b07
SW
95function move_to_begenning() {
96 move_by_filter(-1, function(i) { return i == 0; });
97}
98function move_to_end() {
99 move_by_filter(1, function(i) { return i == files.length - 1; });
100}
1433682c 101function move_to_nondeleted(direction) {
755cf903
SW
102 move_by_filter(direction, function(i) {
103 return !("deleted" in picinfo[files[i]]); });
1433682c
SW
104}
105function move_to_unnamed(direction) {
755cf903
SW
106 move_by_filter(direction, function(i) {
107 return !("deleted" in picinfo[files[i]]) &&
108 !("name" in picinfo[files[i]]); });
1433682c 109}
2d325f41 110
aa798f2c
SW
111function mark_deleted(method) {
112 picinfo[files[input_index]].deleted = method;
01dd0481 113 save_picinfo();
aa798f2c 114 say(method);
2d325f41
SW
115}
116
117function mark_not_deleted() {
31092212 118 delete picinfo[files[input_index]].deleted;
01dd0481 119 save_picinfo();
9c7566e1 120 say("Undeleted");
2d325f41
SW
121}
122
1899b67c
SW
123function say_exposure() {
124 if (!endsWith(files[input_index], ".NEF")) {
125 say("Exposure adjustment not available");
126 return;
127 }
128 var display_exposure = (exposure / 4) - 3.5;
129 say((display_exposure >= 0 ? "+" : "") + display_exposure);
130}
131
138122d7 132function change_exposure(amount) {
b11d9c69
SW
133 if (!endsWith(files[input_index], ".NEF")) {
134 say("Exposure adjustment not available");
135 return;
136 }
138122d7 137 exposure += amount;
07f84670 138 picinfo[files[input_index]].exposure = exposure;
01dd0481 139 save_picinfo();
138122d7 140 setpic();
1899b67c 141 say_exposure();
138122d7
SW
142}
143
59a0fba1
SW
144function rotate() {
145 var rotation = picinfo[files[input_index]].rotate || 0;
146 rotation = (rotation + 90) % 360;
147 if (rotation > 1e-5) {
148 picinfo[files[input_index]].rotate = rotation;
149 } else {
150 delete picinfo[files[input_index]].rotate;
151 }
152 save_picinfo();
153 setpic();
154}
155
0799b4b3
SW
156
157function set_name(name) {
7b24c22c
SW
158 if (name) {
159 picinfo[files[input_index]].name = name;
160 save_picinfo();
161 say("Named " + name);
162 last_name = name;
163 }
0799b4b3
SW
164}
165
166function set_name_from_form() {
167 var name_input = $("#name").hide().get(0);
168 set_name(name_input.value);
169 name_input.value = "";
d92eb78e
SW
170}
171
eaa5e016
SW
172function shell_escape(x) {
173 return x.replace(/'/g, "'\\''");
174}
175
176function show_commands() {
177 var commands = [];
178 $.each(files, function(i, f) {
61332449 179 if ("name" in picinfo[f] && picinfo[f].name.length > 0) {
f20ac636 180 var escaped_filename = "'" + shell_escape(f) + "'";
61332449 181 if (picinfo[f].deleted == "deleted") {
690fbbea 182 commands.push("shred -u " + escaped_filename);
61332449
SW
183 return;
184 }
22c8d6e6
SW
185 if (picinfo[f].deleted == "extra") {
186 commands.push("mv " + escaped_filename + " \"$EXTRADIR\"");
187 return;
188 }
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
SW
202 $("#shell_out").text(commands.join("\n")).show();
203}
204
5d9946cb 205$(function() {
0799b4b3 206 $("#name").hide().on("keyup", function(e) { e.which == 13 && set_name_from_form(); });
c0230e38 207 move_to_nondeleted(1);
e18c13a6 208});
ecb5b4ef 209
ac4559ea 210Mousetrap.bind('z', toggle_zoom);
9952a1a7 211Mousetrap.bind('Z', function() { $("#pic").toggleClass("fit_view"); });
a5ef2857
SW
212Mousetrap.bind(['n', 'l'], function() { move_to_nondeleted(1); });
213Mousetrap.bind(['p', 'h'], function() { move_to_nondeleted(-1); });
1433682c
SW
214Mousetrap.bind(['N', 'L'], function() { move_to_unnamed(1); });
215Mousetrap.bind(['P', 'H'], function() { move_to_unnamed(-1); });
216Mousetrap.bind(['m n', 'm l'], function() { move(1); });
217Mousetrap.bind(['m n', 'm h'], function() { move(-1); });
a5ef2857
SW
218Mousetrap.bind(['b', 'k'], function() { change_exposure(1); });
219Mousetrap.bind(['d', 'j'], function() { change_exposure(-1); });
8a9d6b07
SW
220Mousetrap.bind('0', move_to_begenning);
221Mousetrap.bind('$', move_to_end);
aa798f2c 222Mousetrap.bind('x', function(){ mark_deleted("deleted"); });
ac4559ea 223Mousetrap.bind('X', mark_not_deleted);
22c8d6e6
SW
224Mousetrap.bind('e', function(){ mark_deleted("extra"); });
225Mousetrap.bind('E', mark_not_deleted);
59a0fba1 226Mousetrap.bind('r', rotate);
52c0fe1e 227Mousetrap.bind('i', announce);
88880347 228Mousetrap.bind('f', function() { say(files[input_index]); });
d3aedd16 229Mousetrap.bind('B', function() { say_exposure(); });
d92eb78e 230Mousetrap.bind('c', function() { $("#name").show().focus(); return false; });
0799b4b3 231Mousetrap.bind('C', function() { if (last_name) { set_name(last_name); } });
76812d8f 232Mousetrap.bind('%', function() { say((100 * input_index / files.length).toFixed(2) + "%"); });
eaa5e016 233Mousetrap.bind('!', show_commands);
de26d1f8 234Mousetrap.bind('esc', function() { $("#name").hide(); $("#shell_out").hide(); });
59b4cd23 235
8cec3490
SW
236function clean_picinfo() {
237 files_index = {};
238 $.each(files, function(i, f) {
239 files_index[f] = true;
240 });
241 for (f in picinfo) {
242 if (!files_index[f]) {
243 delete picinfo[f];
244 }
245 }
246}
247
59b4cd23
SW
248function undelete_all() {
249 for (f in picinfo) {
250 delete picinfo[f].deleted;
251 }
252 save_picinfo();
253 say("Undeleted everything");
254}