]> git.scottworley.com Git - picsort/blame_incremental - picsorter.js
This became racy
[picsort] / picsorter.js
... / ...
CommitLineData
1var picinfo = {};
2if ("picsorter_picinfo" in localStorage) {
3 picinfo = JSON.parse(localStorage.picsorter_picinfo);
4}
5$.each(files, function(i, f) {
6 if (!(f in picinfo)) {
7 picinfo[f] = {};
8 }
9});
10function save_picinfo() {
11 localStorage.picsorter_picinfo = JSON.stringify(picinfo);
12}
13save_picinfo();
14
15var exposure = 20;
16var zoom = "sm/";
17var input_index = -1;
18
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}
29
30function setpic() {
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";
40 } else {
41 display_filename = zoom + files[input_index];
42 }
43 var $pic = $("#pic");
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 });
50 $pic.attr("src", display_filename);
51}
52
53function say(message) {
54 $("#message").text(message).removeClass("fade");
55 setTimeout(function() { $("#message").addClass("fade"); }, 100);
56}
57function announce() {
58 say(input_index + " " + (picinfo[files[input_index]].name || ""));
59}
60
61function toggle_zoom() {
62 if (zoom) {
63 zoom = "";
64 } else {
65 zoom = "sm/";
66 }
67 setpic();
68}
69
70function move_by_filter(direction, filter) {
71 // Keep moving in direction until filter is satisfied
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;
89 announce();
90 setpic();
91}
92function move(direction) {
93 move_by_filter(direction, function() { return true; });
94}
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}
101function move_to_nondeleted(direction) {
102 move_by_filter(direction, function(i) {
103 return !("deleted" in picinfo[files[i]]); });
104}
105function move_to_unnamed(direction) {
106 move_by_filter(direction, function(i) {
107 return !("deleted" in picinfo[files[i]]) &&
108 !("name" in picinfo[files[i]]); });
109}
110
111function mark_deleted() {
112 picinfo[files[input_index]].deleted = 1;
113 save_picinfo();
114 say("Deleted");
115}
116
117function mark_not_deleted() {
118 delete picinfo[files[input_index]].deleted;
119 save_picinfo();
120 say("Undeleted");
121}
122
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
132function change_exposure(amount) {
133 if (!endsWith(files[input_index], ".NEF")) {
134 say("Exposure adjustment not available");
135 return;
136 }
137 exposure += amount;
138 picinfo[files[input_index]].exposure = exposure;
139 save_picinfo();
140 setpic();
141 say_exposure();
142}
143
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
156
157function set_name(name) {
158 picinfo[files[input_index]].name = name;
159 save_picinfo();
160 say("Named " + name);
161 last_name = name;
162}
163
164function set_name_from_form() {
165 var name_input = $("#name").hide().get(0);
166 set_name(name_input.value);
167 name_input.value = "";
168}
169
170function shell_escape(x) {
171 return x.replace(/'/g, "'\\''");
172}
173
174function show_commands() {
175 var commands = [];
176 $.each(files, function(i, f) {
177 if ("name" in picinfo[f] && picinfo[f].name.length > 0 &&
178 !("deleted" in picinfo[f])) {
179 var command = ["pic-mv"];
180 if ("exposure" in picinfo[f]) {
181 command.push("-e " + picinfo[f].exposure);
182 }
183 if ("rotate" in picinfo[f]) {
184 command.push("-r " + picinfo[f].rotate);
185 }
186 command.push("'" + shell_escape(f) + "'");
187 command.push("'" + shell_escape(picinfo[f].name) + "'");
188 commands.push(command.join(" "));
189 }
190 });
191 commands.push("");
192 $("#shell_out").text(commands.join("\n")).show();
193}
194
195$(function() {
196 $("#name").hide().on("keyup", function(e) { e.which == 13 && set_name_from_form(); });
197 move_to_nondeleted(1);
198});
199
200Mousetrap.bind('z', toggle_zoom);
201Mousetrap.bind('Z', function() { $("#pic").toggleClass("fit_view"); });
202Mousetrap.bind(['n', 'l'], function() { move_to_nondeleted(1); });
203Mousetrap.bind(['p', 'h'], function() { move_to_nondeleted(-1); });
204Mousetrap.bind(['N', 'L'], function() { move_to_unnamed(1); });
205Mousetrap.bind(['P', 'H'], function() { move_to_unnamed(-1); });
206Mousetrap.bind(['m n', 'm l'], function() { move(1); });
207Mousetrap.bind(['m n', 'm h'], function() { move(-1); });
208Mousetrap.bind(['b', 'k'], function() { change_exposure(1); });
209Mousetrap.bind(['d', 'j'], function() { change_exposure(-1); });
210Mousetrap.bind('0', move_to_begenning);
211Mousetrap.bind('$', move_to_end);
212Mousetrap.bind('x', mark_deleted);
213Mousetrap.bind('X', mark_not_deleted);
214Mousetrap.bind('r', rotate);
215Mousetrap.bind('i', announce);
216Mousetrap.bind('f', function() { say(files[input_index]); });
217Mousetrap.bind('e', function() { say_exposure(); });
218Mousetrap.bind('c', function() { $("#name").show().focus(); return false; });
219Mousetrap.bind('C', function() { if (last_name) { set_name(last_name); } });
220Mousetrap.bind('%', function() { say((100 * input_index / files.length).toFixed(2) + "%"); });
221Mousetrap.bind('!', show_commands);
222Mousetrap.bind('esc', function() { $("#name").hide(); $("#shell_out").hide(); });
223
224function undelete_all() {
225 for (f in picinfo) {
226 delete picinfo[f].deleted;
227 }
228 save_picinfo();
229 say("Undeleted everything");
230}