]> git.scottworley.com Git - picsort/blob - picsorter.js
e0a05ff0022c54e8db57814c418fa9c9403b97ad
[picsort] / picsorter.js
1 var picinfo = {};
2 var exposure = 20;
3 var zoom = "sm/";
4 var input_index = -1;
5
6 function save_picinfo() {
7 localStorage.picsorter_picinfo = JSON.stringify(picinfo);
8 }
9
10 function endsWith(str, suffix) {
11 return str.indexOf(suffix, str.length - suffix.length) !== -1;
12 }
13
14 function stripFromEnd(str, suffix) {
15 if (endsWith(str, suffix)) {
16 return str.substr(0, str.length - suffix.length);
17 }
18 return str;
19 }
20
21 function setpic() {
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";
31 } else {
32 display_filename = zoom + files[input_index];
33 }
34 var $pic = $("#pic");
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 });
41 $pic.attr("src", display_filename);
42 }
43
44 function say(message) {
45 $("#message").text(message).removeClass("fade");
46 setTimeout(function() { $("#message").addClass("fade"); }, 100);
47 }
48 function announce() {
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);
54 return false;
55 }
56
57 function toggle_zoom() {
58 if (zoom) {
59 zoom = "";
60 } else {
61 zoom = "sm/";
62 }
63 setpic();
64 return false;
65 }
66
67 function move_by_filter(direction, filter) {
68 // Keep moving in direction until filter is satisfied
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;
86 announce();
87 setpic();
88 }
89 function move(direction) {
90 move_by_filter(direction, function() { return true; });
91 }
92 function move_to_begenning() {
93 move_by_filter(-1, function(i) { return i == 0; });
94 return false;
95 }
96 function move_to_end() {
97 move_by_filter(1, function(i) { return i == files.length - 1; });
98 return false;
99 }
100 function move_to_nondeleted(direction) {
101 move_by_filter(direction, function(i) {
102 return !("deleted" in picinfo[files[i]]); });
103 }
104 function move_to_unnamed(direction) {
105 move_by_filter(direction, function(i) {
106 return !("deleted" in picinfo[files[i]]) &&
107 !("name" in picinfo[files[i]]); });
108 }
109
110 function mark_deleted(method) {
111 picinfo[files[input_index]].deleted = method;
112 save_picinfo();
113 say(method);
114 }
115
116 function mark_not_deleted() {
117 delete picinfo[files[input_index]].deleted;
118 save_picinfo();
119 say("Undeleted");
120 return false;
121 }
122
123 function say_exposure() {
124 if (!endsWith(files[input_index], ".NEF")) {
125 say("Exposure adjustment not available");
126 return false;
127 }
128 var display_exposure = (exposure / 4) - 3.5;
129 say((display_exposure >= 0 ? "+" : "") + display_exposure);
130 return false;
131 }
132
133 function change_exposure(amount) {
134 if (!endsWith(files[input_index], ".NEF")) {
135 say("Exposure adjustment not available");
136 return;
137 }
138 exposure += amount;
139 picinfo[files[input_index]].exposure = exposure;
140 save_picinfo();
141 setpic();
142 say_exposure();
143 }
144
145 function 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();
155 return false;
156 }
157
158
159 function set_name(name) {
160 if (name) {
161 picinfo[files[input_index]].name = name;
162 save_picinfo();
163 say("Named " + name);
164 last_name = name;
165 }
166 }
167
168 function set_name_from_form() {
169 var name_input = $("#name").hide().get(0);
170 set_name(name_input.value);
171 name_input.value = "";
172 }
173
174 function shell_escape(x) {
175 return x.replace(/'/g, "'\\''");
176 }
177
178 function show_commands() {
179 var commands = [];
180 $.each(files, function(i, f) {
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") {
185 commands.push("mv -vi " + escaped_filename + " \"$EXTRADIR\"");
186 } else if (picinfo[f].deleted == "blurry") {
187 commands.push("mv -vi " + escaped_filename + " \"$BLURRYDIR\"");
188 } else if ("name" in picinfo[f] && picinfo[f].name.length > 0) {
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 }
196 command.push(escaped_filename);
197 command.push("'" + shell_escape(picinfo[f].name) + "'");
198 commands.push(command.join(" "));
199 }
200 });
201 commands.push("");
202 $("#shell_out").text(commands.join("\n")).show();
203 return false;
204 }
205
206 function 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
218 function undelete_all() {
219 for (f in picinfo) {
220 delete picinfo[f].deleted;
221 }
222 save_picinfo();
223 say("Undeleted everything");
224 }
225
226 function 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 }