]> git.scottworley.com Git - picsort/blob - picsorter.js
Move startup stuff to start()
[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 }
55
56 function toggle_zoom() {
57 if (zoom) {
58 zoom = "";
59 } else {
60 zoom = "sm/";
61 }
62 setpic();
63 }
64
65 function move_by_filter(direction, filter) {
66 // Keep moving in direction until filter is satisfied
67 var new_index = input_index;
68 while (true) {
69 var next = new_index + direction;
70 if (next < 0) {
71 say("At beginning");
72 return;
73 }
74 if (next >= files.length) {
75 say("At end");
76 return;
77 }
78 new_index = next;
79 if (filter(new_index)) {
80 break;
81 }
82 }
83 input_index = new_index;
84 announce();
85 setpic();
86 }
87 function move(direction) {
88 move_by_filter(direction, function() { return true; });
89 }
90 function move_to_begenning() {
91 move_by_filter(-1, function(i) { return i == 0; });
92 }
93 function move_to_end() {
94 move_by_filter(1, function(i) { return i == files.length - 1; });
95 }
96 function move_to_nondeleted(direction) {
97 move_by_filter(direction, function(i) {
98 return !("deleted" in picinfo[files[i]]); });
99 }
100 function move_to_unnamed(direction) {
101 move_by_filter(direction, function(i) {
102 return !("deleted" in picinfo[files[i]]) &&
103 !("name" in picinfo[files[i]]); });
104 }
105
106 function mark_deleted(method) {
107 picinfo[files[input_index]].deleted = method;
108 save_picinfo();
109 say(method);
110 }
111
112 function mark_not_deleted() {
113 delete picinfo[files[input_index]].deleted;
114 save_picinfo();
115 say("Undeleted");
116 }
117
118 function say_exposure() {
119 if (!endsWith(files[input_index], ".NEF")) {
120 say("Exposure adjustment not available");
121 return;
122 }
123 var display_exposure = (exposure / 4) - 3.5;
124 say((display_exposure >= 0 ? "+" : "") + display_exposure);
125 }
126
127 function change_exposure(amount) {
128 if (!endsWith(files[input_index], ".NEF")) {
129 say("Exposure adjustment not available");
130 return;
131 }
132 exposure += amount;
133 picinfo[files[input_index]].exposure = exposure;
134 save_picinfo();
135 setpic();
136 say_exposure();
137 }
138
139 function rotate() {
140 var rotation = picinfo[files[input_index]].rotate || 0;
141 rotation = (rotation + 90) % 360;
142 if (rotation > 1e-5) {
143 picinfo[files[input_index]].rotate = rotation;
144 } else {
145 delete picinfo[files[input_index]].rotate;
146 }
147 save_picinfo();
148 setpic();
149 }
150
151
152 function set_name(name) {
153 if (name) {
154 picinfo[files[input_index]].name = name;
155 save_picinfo();
156 say("Named " + name);
157 last_name = name;
158 }
159 }
160
161 function set_name_from_form() {
162 var name_input = $("#name").hide().get(0);
163 set_name(name_input.value);
164 name_input.value = "";
165 }
166
167 function shell_escape(x) {
168 return x.replace(/'/g, "'\\''");
169 }
170
171 function show_commands() {
172 var commands = [];
173 $.each(files, function(i, f) {
174 var escaped_filename = "'" + shell_escape(f) + "'";
175 if (picinfo[f].deleted == "deleted") {
176 commands.push("shred -u " + escaped_filename);
177 } else if (picinfo[f].deleted == "extra") {
178 commands.push("mv -vi " + escaped_filename + " \"$EXTRADIR\"");
179 } else if (picinfo[f].deleted == "blurry") {
180 commands.push("mv -vi " + escaped_filename + " \"$BLURRYDIR\"");
181 } else if ("name" in picinfo[f] && picinfo[f].name.length > 0) {
182 var command = ["pic-mv"];
183 if ("exposure" in picinfo[f]) {
184 command.push("-e " + picinfo[f].exposure);
185 }
186 if ("rotate" in picinfo[f]) {
187 command.push("-r " + picinfo[f].rotate);
188 }
189 command.push(escaped_filename);
190 command.push("'" + shell_escape(picinfo[f].name) + "'");
191 commands.push(command.join(" "));
192 }
193 });
194 commands.push("");
195 $("#shell_out").text(commands.join("\n")).show();
196 }
197
198 function clean_picinfo() {
199 files_index = {};
200 $.each(files, function(i, f) {
201 files_index[f] = true;
202 });
203 for (f in picinfo) {
204 if (!files_index[f]) {
205 delete picinfo[f];
206 }
207 }
208 }
209
210 function undelete_all() {
211 for (f in picinfo) {
212 delete picinfo[f].deleted;
213 }
214 save_picinfo();
215 say("Undeleted everything");
216 }
217
218 function start() {
219 if ("picsorter_picinfo" in localStorage) {
220 picinfo = JSON.parse(localStorage.picsorter_picinfo);
221 }
222 $.each(files, function(i, f) {
223 if (!(f in picinfo)) {
224 picinfo[f] = {};
225 }
226 });
227 save_picinfo();
228
229 $(function() {
230 $("#name").hide().on("keyup", function(e) { e.which == 13 && set_name_from_form(); });
231 move_to_nondeleted(1);
232 });
233
234 Mousetrap.bind('z', toggle_zoom);
235 Mousetrap.bind('Z', function() { $("#pic").toggleClass("fit_view"); return false; });
236 Mousetrap.bind(['n', 'l'], function() { move_to_nondeleted(1); return false; });
237 Mousetrap.bind(['p', 'h'], function() { move_to_nondeleted(-1); return false; });
238 Mousetrap.bind(['N', 'L'], function() { move_to_unnamed(1); return false; });
239 Mousetrap.bind(['P', 'H'], function() { move_to_unnamed(-1); return false; });
240 Mousetrap.bind(['m n', 'm l'], function() { move(1); return false; });
241 Mousetrap.bind(['m n', 'm h'], function() { move(-1); return false; });
242 Mousetrap.bind(['b', 'k'], function() { change_exposure(1); return false; });
243 Mousetrap.bind(['d', 'j'], function() { change_exposure(-1); return false; });
244 Mousetrap.bind('0', move_to_begenning);
245 Mousetrap.bind('$', move_to_end);
246 Mousetrap.bind('x', function(){ mark_deleted("deleted"); return false; });
247 Mousetrap.bind('X', mark_not_deleted);
248 Mousetrap.bind('e', function(){ mark_deleted("extra"); return false; });
249 Mousetrap.bind('E', mark_not_deleted);
250 Mousetrap.bind('g', function(){ mark_deleted("blurry"); return false; });
251 Mousetrap.bind('G', mark_not_deleted);
252 Mousetrap.bind('r', rotate);
253 Mousetrap.bind('i', announce);
254 Mousetrap.bind('f', function() { say(files[input_index]); return false; });
255 Mousetrap.bind('B', say_exposure);
256 Mousetrap.bind('c', function() { $("#name").show().focus(); return false; });
257 Mousetrap.bind('C', function() { if (last_name) { set_name(last_name); } return false; });
258 Mousetrap.bind('%', function() { say((100 * input_index / files.length).toFixed(2) + "%"); return false; });
259 Mousetrap.bind('!', show_commands);
260 Mousetrap.bind('esc', function() { $("#name").hide(); $("#shell_out").hide(); return false; });
261 }