]> git.scottworley.com Git - picsort/blame - picsorter.js
Extra newline at the end for easier copy/paste
[picsort] / picsorter.js
CommitLineData
31092212
SW
1var picinfo = {};
2if ("picsorter_picinfo" in localStorage) {
3 picinfo = JSON.parse(localStorage.picsorter_picinfo);
2d325f41 4}
31092212
SW
5$.each(files, function(i, f) {
6 if (!(f in picinfo)) {
7 picinfo[f] = {};
8 }
9});
01dd0481
SW
10function save_picinfo() {
11 localStorage.picsorter_picinfo = JSON.stringify(picinfo);
12}
13save_picinfo();
2d325f41 14
ca898dc7
SW
15var exposure = 20;
16var zoom = "sm/";
17var input_index = -1;
ca898dc7 18
8bc0aad4
SW
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}
ca898dc7 29
ed9d7c54 30function setpic() {
cd368e97
SW
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";
07f84670 40 } else {
cd368e97 41 display_filename = zoom + files[input_index];
07f84670 42 }
59a0fba1 43 var $pic = $("#pic");
6219f116
SW
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 });
59a0fba1 50 $pic.attr("src", display_filename);
9952a1a7
SW
51}
52
9c7566e1
SW
53function say(message) {
54 $("#message").text(message).removeClass("fade");
55 setTimeout(function() { $("#message").addClass("fade"); }, 1);
56}
57
9952a1a7
SW
58function toggle_zoom() {
59 if (zoom) {
60 zoom = "";
61 } else {
62 zoom = "sm/";
63 }
ac4559ea 64 setpic();
97c26a9c
SW
65}
66
1433682c
SW
67function move_by_filter(direction, filter) {
68 // Keep moving in direction until filter is satisfied
755cf903
SW
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;
d92eb78e 86 say(input_index + " " + (picinfo[files[input_index]].name || ""));
c0230e38 87 setpic();
2d325f41 88}
1433682c
SW
89function move(direction) {
90 move_by_filter(direction, function() { return true; });
91}
92function move_to_nondeleted(direction) {
755cf903
SW
93 move_by_filter(direction, function(i) {
94 return !("deleted" in picinfo[files[i]]); });
1433682c
SW
95}
96function move_to_unnamed(direction) {
755cf903
SW
97 move_by_filter(direction, function(i) {
98 return !("deleted" in picinfo[files[i]]) &&
99 !("name" in picinfo[files[i]]); });
1433682c 100}
2d325f41
SW
101
102function mark_deleted() {
31092212 103 picinfo[files[input_index]].deleted = 1;
01dd0481 104 save_picinfo();
9c7566e1 105 say("Deleted");
2d325f41
SW
106}
107
108function mark_not_deleted() {
31092212 109 delete picinfo[files[input_index]].deleted;
01dd0481 110 save_picinfo();
9c7566e1 111 say("Undeleted");
2d325f41
SW
112}
113
138122d7 114function change_exposure(amount) {
b11d9c69
SW
115 if (!endsWith(files[input_index], ".NEF")) {
116 say("Exposure adjustment not available");
117 return;
118 }
138122d7 119 exposure += amount;
07f84670 120 picinfo[files[input_index]].exposure = exposure;
01dd0481 121 save_picinfo();
138122d7
SW
122 setpic();
123 var display_exposure = (exposure / 4) - 3.5;
124 say((display_exposure >= 0 ? "+" : "") + display_exposure);
125}
126
59a0fba1
SW
127function 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
d92eb78e
SW
139function set_name() {
140 var name_input = $("#name").hide().get(0);
141 var name = name_input.value;
142 name_input.value = "";
143 picinfo[files[input_index]].name = name;
144 save_picinfo();
145 say("Named " + name);
146}
147
eaa5e016
SW
148function shell_escape(x) {
149 return x.replace(/'/g, "'\\''");
150}
151
152function show_commands() {
153 var commands = [];
154 $.each(files, function(i, f) {
f1b39471
SW
155 if ("name" in picinfo[f] && picinfo[f].name.length > 0 &&
156 !("deleted" in picinfo[f])) {
eaa5e016
SW
157 var command = ["pic-mv"];
158 if ("exposure" in picinfo[f]) {
159 command.push("-e " + picinfo[f].exposure);
160 }
161 if ("rotate" in picinfo[f]) {
162 command.push("-r " + picinfo[f].rotate);
163 }
164 command.push("'" + shell_escape(f) + "'");
165 command.push("'" + shell_escape(picinfo[f].name) + "'");
166 commands.push(command.join(" "));
167 }
168 });
0f889466 169 commands.push("");
eaa5e016
SW
170 $("#shell_out").text(commands.join("\n")).show();
171}
172
5d9946cb 173$(function() {
d92eb78e 174 $("#name").hide().on("keyup", function(e) { e.which == 13 && set_name(); });
c0230e38 175 move_to_nondeleted(1);
e18c13a6 176});
ecb5b4ef 177
ac4559ea 178Mousetrap.bind('z', toggle_zoom);
9952a1a7 179Mousetrap.bind('Z', function() { $("#pic").toggleClass("fit_view"); });
a5ef2857
SW
180Mousetrap.bind(['n', 'l'], function() { move_to_nondeleted(1); });
181Mousetrap.bind(['p', 'h'], function() { move_to_nondeleted(-1); });
1433682c
SW
182Mousetrap.bind(['N', 'L'], function() { move_to_unnamed(1); });
183Mousetrap.bind(['P', 'H'], function() { move_to_unnamed(-1); });
184Mousetrap.bind(['m n', 'm l'], function() { move(1); });
185Mousetrap.bind(['m n', 'm h'], function() { move(-1); });
a5ef2857
SW
186Mousetrap.bind(['b', 'k'], function() { change_exposure(1); });
187Mousetrap.bind(['d', 'j'], function() { change_exposure(-1); });
ac4559ea
SW
188Mousetrap.bind('x', mark_deleted);
189Mousetrap.bind('X', mark_not_deleted);
59a0fba1 190Mousetrap.bind('r', rotate);
d92eb78e 191Mousetrap.bind('c', function() { $("#name").show().focus(); return false; });
76812d8f 192Mousetrap.bind('%', function() { say((100 * input_index / files.length).toFixed(2) + "%"); });
eaa5e016 193Mousetrap.bind('!', show_commands);
de26d1f8 194Mousetrap.bind('esc', function() { $("#name").hide(); $("#shell_out").hide(); });