]> git.scottworley.com Git - picsort/blob - picsorter.js
084831c17aac947a3551b091c27ab1fc59d45656
[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
58 function toggle_zoom() {
59 if (zoom) {
60 zoom = "";
61 } else {
62 zoom = "sm/";
63 }
64 setpic();
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 say(input_index + " " + (picinfo[files[input_index]].name || ""));
87 setpic();
88 }
89 function move(direction) {
90 move_by_filter(direction, function() { return true; });
91 }
92 function move_to_nondeleted(direction) {
93 move_by_filter(direction, function(i) {
94 return !("deleted" in picinfo[files[i]]); });
95 }
96 function move_to_unnamed(direction) {
97 move_by_filter(direction, function(i) {
98 return !("deleted" in picinfo[files[i]]) &&
99 !("name" in picinfo[files[i]]); });
100 }
101
102 function mark_deleted() {
103 picinfo[files[input_index]].deleted = 1;
104 save_picinfo();
105 say("Deleted");
106 }
107
108 function mark_not_deleted() {
109 delete picinfo[files[input_index]].deleted;
110 save_picinfo();
111 say("Undeleted");
112 }
113
114 function change_exposure(amount) {
115 if (!endsWith(files[input_index], ".NEF")) {
116 say("Exposure adjustment not available");
117 return;
118 }
119 exposure += amount;
120 picinfo[files[input_index]].exposure = exposure;
121 save_picinfo();
122 setpic();
123 var display_exposure = (exposure / 4) - 3.5;
124 say((display_exposure >= 0 ? "+" : "") + display_exposure);
125 }
126
127 function rotate() {
128 var rotation = picinfo[files[input_index]].rotate || 0;
129 rotation = (rotation + 90) % 360;
130 if (rotation > 1e-5) {
131 picinfo[files[input_index]].rotate = rotation;
132 } else {
133 delete picinfo[files[input_index]].rotate;
134 }
135 save_picinfo();
136 setpic();
137 }
138
139
140 function set_name(name) {
141 picinfo[files[input_index]].name = name;
142 save_picinfo();
143 say("Named " + name);
144 last_name = name;
145 }
146
147 function set_name_from_form() {
148 var name_input = $("#name").hide().get(0);
149 set_name(name_input.value);
150 name_input.value = "";
151 }
152
153 function shell_escape(x) {
154 return x.replace(/'/g, "'\\''");
155 }
156
157 function show_commands() {
158 var commands = [];
159 $.each(files, function(i, f) {
160 if ("name" in picinfo[f] && picinfo[f].name.length > 0 &&
161 !("deleted" in picinfo[f])) {
162 var command = ["pic-mv"];
163 if ("exposure" in picinfo[f]) {
164 command.push("-e " + picinfo[f].exposure);
165 }
166 if ("rotate" in picinfo[f]) {
167 command.push("-r " + picinfo[f].rotate);
168 }
169 command.push("'" + shell_escape(f) + "'");
170 command.push("'" + shell_escape(picinfo[f].name) + "'");
171 commands.push(command.join(" "));
172 }
173 });
174 commands.push("");
175 $("#shell_out").text(commands.join("\n")).show();
176 }
177
178 $(function() {
179 $("#name").hide().on("keyup", function(e) { e.which == 13 && set_name_from_form(); });
180 move_to_nondeleted(1);
181 });
182
183 Mousetrap.bind('z', toggle_zoom);
184 Mousetrap.bind('Z', function() { $("#pic").toggleClass("fit_view"); });
185 Mousetrap.bind(['n', 'l'], function() { move_to_nondeleted(1); });
186 Mousetrap.bind(['p', 'h'], function() { move_to_nondeleted(-1); });
187 Mousetrap.bind(['N', 'L'], function() { move_to_unnamed(1); });
188 Mousetrap.bind(['P', 'H'], function() { move_to_unnamed(-1); });
189 Mousetrap.bind(['m n', 'm l'], function() { move(1); });
190 Mousetrap.bind(['m n', 'm h'], function() { move(-1); });
191 Mousetrap.bind(['b', 'k'], function() { change_exposure(1); });
192 Mousetrap.bind(['d', 'j'], function() { change_exposure(-1); });
193 Mousetrap.bind('x', mark_deleted);
194 Mousetrap.bind('X', mark_not_deleted);
195 Mousetrap.bind('r', rotate);
196 Mousetrap.bind('f', function() { say(files[input_index]); });
197 Mousetrap.bind('c', function() { $("#name").show().focus(); return false; });
198 Mousetrap.bind('C', function() { if (last_name) { set_name(last_name); } });
199 Mousetrap.bind('%', function() { say((100 * input_index / files.length).toFixed(2) + "%"); });
200 Mousetrap.bind('!', show_commands);
201 Mousetrap.bind('esc', function() { $("#name").hide(); $("#shell_out").hide(); });