]> git.scottworley.com Git - picsort/blob - picsorter.js
31a6a560da9b59e9208a153c494f59dd6a86360c
[picsort] / picsorter.js
1 var picinfo = {};
2 if ("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 });
10 function save_picinfo() {
11 localStorage.picsorter_picinfo = JSON.stringify(picinfo);
12 }
13 save_picinfo();
14
15 var exposure = 20;
16 var zoom = "sm/";
17 var input_index = -1;
18
19 function endsWith(str, suffix) {
20 return str.indexOf(suffix, str.length - suffix.length) !== -1;
21 }
22
23 function stripFromEnd(str, suffix) {
24 if (endsWith(str, suffix)) {
25 return str.substr(0, str.length - suffix.length);
26 }
27 return str;
28 }
29
30 function 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
53 function say(message) {
54 $("#message").text(message).removeClass("fade");
55 setTimeout(function() { $("#message").addClass("fade"); }, 1);
56 }
57 function announce() {
58 say(input_index + " " + (picinfo[files[input_index]].name || ""));
59 }
60
61 function toggle_zoom() {
62 if (zoom) {
63 zoom = "";
64 } else {
65 zoom = "sm/";
66 }
67 setpic();
68 }
69
70 function 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 }
92 function move(direction) {
93 move_by_filter(direction, function() { return true; });
94 }
95 function move_to_nondeleted(direction) {
96 move_by_filter(direction, function(i) {
97 return !("deleted" in picinfo[files[i]]); });
98 }
99 function move_to_unnamed(direction) {
100 move_by_filter(direction, function(i) {
101 return !("deleted" in picinfo[files[i]]) &&
102 !("name" in picinfo[files[i]]); });
103 }
104
105 function mark_deleted() {
106 picinfo[files[input_index]].deleted = 1;
107 save_picinfo();
108 say("Deleted");
109 }
110
111 function mark_not_deleted() {
112 delete picinfo[files[input_index]].deleted;
113 save_picinfo();
114 say("Undeleted");
115 }
116
117 function say_exposure() {
118 if (!endsWith(files[input_index], ".NEF")) {
119 say("Exposure adjustment not available");
120 return;
121 }
122 var display_exposure = (exposure / 4) - 3.5;
123 say((display_exposure >= 0 ? "+" : "") + display_exposure);
124 }
125
126 function change_exposure(amount) {
127 if (!endsWith(files[input_index], ".NEF")) {
128 say("Exposure adjustment not available");
129 return;
130 }
131 exposure += amount;
132 picinfo[files[input_index]].exposure = exposure;
133 save_picinfo();
134 setpic();
135 say_exposure();
136 }
137
138 function rotate() {
139 var rotation = picinfo[files[input_index]].rotate || 0;
140 rotation = (rotation + 90) % 360;
141 if (rotation > 1e-5) {
142 picinfo[files[input_index]].rotate = rotation;
143 } else {
144 delete picinfo[files[input_index]].rotate;
145 }
146 save_picinfo();
147 setpic();
148 }
149
150
151 function set_name(name) {
152 picinfo[files[input_index]].name = name;
153 save_picinfo();
154 say("Named " + name);
155 last_name = name;
156 }
157
158 function set_name_from_form() {
159 var name_input = $("#name").hide().get(0);
160 set_name(name_input.value);
161 name_input.value = "";
162 }
163
164 function shell_escape(x) {
165 return x.replace(/'/g, "'\\''");
166 }
167
168 function show_commands() {
169 var commands = [];
170 $.each(files, function(i, f) {
171 if ("name" in picinfo[f] && picinfo[f].name.length > 0 &&
172 !("deleted" in picinfo[f])) {
173 var command = ["pic-mv"];
174 if ("exposure" in picinfo[f]) {
175 command.push("-e " + picinfo[f].exposure);
176 }
177 if ("rotate" in picinfo[f]) {
178 command.push("-r " + picinfo[f].rotate);
179 }
180 command.push("'" + shell_escape(f) + "'");
181 command.push("'" + shell_escape(picinfo[f].name) + "'");
182 commands.push(command.join(" "));
183 }
184 });
185 commands.push("");
186 $("#shell_out").text(commands.join("\n")).show();
187 }
188
189 $(function() {
190 $("#name").hide().on("keyup", function(e) { e.which == 13 && set_name_from_form(); });
191 move_to_nondeleted(1);
192 });
193
194 Mousetrap.bind('z', toggle_zoom);
195 Mousetrap.bind('Z', function() { $("#pic").toggleClass("fit_view"); });
196 Mousetrap.bind(['n', 'l'], function() { move_to_nondeleted(1); });
197 Mousetrap.bind(['p', 'h'], function() { move_to_nondeleted(-1); });
198 Mousetrap.bind(['N', 'L'], function() { move_to_unnamed(1); });
199 Mousetrap.bind(['P', 'H'], function() { move_to_unnamed(-1); });
200 Mousetrap.bind(['m n', 'm l'], function() { move(1); });
201 Mousetrap.bind(['m n', 'm h'], function() { move(-1); });
202 Mousetrap.bind(['b', 'k'], function() { change_exposure(1); });
203 Mousetrap.bind(['d', 'j'], function() { change_exposure(-1); });
204 Mousetrap.bind('x', mark_deleted);
205 Mousetrap.bind('X', mark_not_deleted);
206 Mousetrap.bind('r', rotate);
207 Mousetrap.bind('i', announce);
208 Mousetrap.bind('f', function() { say(files[input_index]); });
209 Mousetrap.bind('e', function() { say_exposure(); });
210 Mousetrap.bind('c', function() { $("#name").show().focus(); return false; });
211 Mousetrap.bind('C', function() { if (last_name) { set_name(last_name); } });
212 Mousetrap.bind('%', function() { say((100 * input_index / files.length).toFixed(2) + "%"); });
213 Mousetrap.bind('!', show_commands);
214 Mousetrap.bind('esc', function() { $("#name").hide(); $("#shell_out").hide(); });