]> git.scottworley.com Git - picsort/blame - picsorter.js
Move startup stuff to start()
[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);
52c0fe1e 54}
9c7566e1 55
9952a1a7
SW
56function toggle_zoom() {
57 if (zoom) {
58 zoom = "";
59 } else {
60 zoom = "sm/";
61 }
ac4559ea 62 setpic();
97c26a9c
SW
63}
64
1433682c
SW
65function move_by_filter(direction, filter) {
66 // Keep moving in direction until filter is satisfied
755cf903
SW
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;
52c0fe1e 84 announce();
c0230e38 85 setpic();
2d325f41 86}
1433682c
SW
87function move(direction) {
88 move_by_filter(direction, function() { return true; });
89}
8a9d6b07
SW
90function move_to_begenning() {
91 move_by_filter(-1, function(i) { return i == 0; });
92}
93function move_to_end() {
94 move_by_filter(1, function(i) { return i == files.length - 1; });
95}
1433682c 96function move_to_nondeleted(direction) {
755cf903
SW
97 move_by_filter(direction, function(i) {
98 return !("deleted" in picinfo[files[i]]); });
1433682c
SW
99}
100function move_to_unnamed(direction) {
755cf903
SW
101 move_by_filter(direction, function(i) {
102 return !("deleted" in picinfo[files[i]]) &&
103 !("name" in picinfo[files[i]]); });
1433682c 104}
2d325f41 105
aa798f2c
SW
106function mark_deleted(method) {
107 picinfo[files[input_index]].deleted = method;
01dd0481 108 save_picinfo();
aa798f2c 109 say(method);
2d325f41
SW
110}
111
112function mark_not_deleted() {
31092212 113 delete picinfo[files[input_index]].deleted;
01dd0481 114 save_picinfo();
9c7566e1 115 say("Undeleted");
2d325f41
SW
116}
117
1899b67c
SW
118function 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
138122d7 127function change_exposure(amount) {
b11d9c69
SW
128 if (!endsWith(files[input_index], ".NEF")) {
129 say("Exposure adjustment not available");
130 return;
131 }
138122d7 132 exposure += amount;
07f84670 133 picinfo[files[input_index]].exposure = exposure;
01dd0481 134 save_picinfo();
138122d7 135 setpic();
1899b67c 136 say_exposure();
138122d7
SW
137}
138
59a0fba1
SW
139function 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
0799b4b3
SW
151
152function set_name(name) {
7b24c22c
SW
153 if (name) {
154 picinfo[files[input_index]].name = name;
155 save_picinfo();
156 say("Named " + name);
157 last_name = name;
158 }
0799b4b3
SW
159}
160
161function set_name_from_form() {
162 var name_input = $("#name").hide().get(0);
163 set_name(name_input.value);
164 name_input.value = "";
d92eb78e
SW
165}
166
eaa5e016
SW
167function shell_escape(x) {
168 return x.replace(/'/g, "'\\''");
169}
170
171function show_commands() {
172 var commands = [];
173 $.each(files, function(i, f) {
fcfd9f01
SW
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") {
53194dd7 178 commands.push("mv -vi " + escaped_filename + " \"$EXTRADIR\"");
5875d960
SW
179 } else if (picinfo[f].deleted == "blurry") {
180 commands.push("mv -vi " + escaped_filename + " \"$BLURRYDIR\"");
fcfd9f01 181 } else if ("name" in picinfo[f] && picinfo[f].name.length > 0) {
eaa5e016
SW
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 }
61332449 189 command.push(escaped_filename);
f20ac636 190 command.push("'" + shell_escape(picinfo[f].name) + "'");
eaa5e016
SW
191 commands.push(command.join(" "));
192 }
193 });
0f889466 194 commands.push("");
eaa5e016
SW
195 $("#shell_out").text(commands.join("\n")).show();
196}
197
8cec3490
SW
198function 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
59b4cd23
SW
210function undelete_all() {
211 for (f in picinfo) {
212 delete picinfo[f].deleted;
213 }
214 save_picinfo();
215 say("Undeleted everything");
216}
f2744775
SW
217
218function 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}