]> git.scottworley.com Git - picsort/blame - picsorter.js
Disallow scrolling off the beginning and end
[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 }
cd368e97 43 $("#pic").attr("src", display_filename);
9952a1a7
SW
44}
45
9c7566e1
SW
46function say(message) {
47 $("#message").text(message).removeClass("fade");
48 setTimeout(function() { $("#message").addClass("fade"); }, 1);
49}
50
9952a1a7
SW
51function toggle_zoom() {
52 if (zoom) {
53 zoom = "";
54 } else {
55 zoom = "sm/";
56 }
ac4559ea 57 setpic();
97c26a9c
SW
58}
59
1433682c
SW
60function move_by_filter(direction, filter) {
61 // Keep moving in direction until filter is satisfied
755cf903
SW
62 var new_index = input_index;
63 while (true) {
64 var next = new_index + direction;
65 if (next < 0) {
66 say("At beginning");
67 return;
68 }
69 if (next >= files.length) {
70 say("At end");
71 return;
72 }
73 new_index = next;
74 if (filter(new_index)) {
75 break;
76 }
77 }
78 input_index = new_index;
d92eb78e 79 say(input_index + " " + (picinfo[files[input_index]].name || ""));
c0230e38 80 setpic();
2d325f41 81}
1433682c
SW
82function move(direction) {
83 move_by_filter(direction, function() { return true; });
84}
85function move_to_nondeleted(direction) {
755cf903
SW
86 move_by_filter(direction, function(i) {
87 return !("deleted" in picinfo[files[i]]); });
1433682c
SW
88}
89function move_to_unnamed(direction) {
755cf903
SW
90 move_by_filter(direction, function(i) {
91 return !("deleted" in picinfo[files[i]]) &&
92 !("name" in picinfo[files[i]]); });
1433682c 93}
2d325f41
SW
94
95function mark_deleted() {
31092212 96 picinfo[files[input_index]].deleted = 1;
01dd0481 97 save_picinfo();
9c7566e1 98 say("Deleted");
2d325f41
SW
99}
100
101function mark_not_deleted() {
31092212 102 delete picinfo[files[input_index]].deleted;
01dd0481 103 save_picinfo();
9c7566e1 104 say("Undeleted");
2d325f41
SW
105}
106
138122d7 107function change_exposure(amount) {
b11d9c69
SW
108 if (!endsWith(files[input_index], ".NEF")) {
109 say("Exposure adjustment not available");
110 return;
111 }
138122d7 112 exposure += amount;
07f84670 113 picinfo[files[input_index]].exposure = exposure;
01dd0481 114 save_picinfo();
138122d7
SW
115 setpic();
116 var display_exposure = (exposure / 4) - 3.5;
117 say((display_exposure >= 0 ? "+" : "") + display_exposure);
118}
119
d92eb78e
SW
120function set_name() {
121 var name_input = $("#name").hide().get(0);
122 var name = name_input.value;
123 name_input.value = "";
124 picinfo[files[input_index]].name = name;
125 save_picinfo();
126 say("Named " + name);
127}
128
5d9946cb 129$(function() {
d92eb78e 130 $("#name").hide().on("keyup", function(e) { e.which == 13 && set_name(); });
c0230e38 131 move_to_nondeleted(1);
e18c13a6 132});
ecb5b4ef 133
ac4559ea 134Mousetrap.bind('z', toggle_zoom);
9952a1a7 135Mousetrap.bind('Z', function() { $("#pic").toggleClass("fit_view"); });
a5ef2857
SW
136Mousetrap.bind(['n', 'l'], function() { move_to_nondeleted(1); });
137Mousetrap.bind(['p', 'h'], function() { move_to_nondeleted(-1); });
1433682c
SW
138Mousetrap.bind(['N', 'L'], function() { move_to_unnamed(1); });
139Mousetrap.bind(['P', 'H'], function() { move_to_unnamed(-1); });
140Mousetrap.bind(['m n', 'm l'], function() { move(1); });
141Mousetrap.bind(['m n', 'm h'], function() { move(-1); });
a5ef2857
SW
142Mousetrap.bind(['b', 'k'], function() { change_exposure(1); });
143Mousetrap.bind(['d', 'j'], function() { change_exposure(-1); });
ac4559ea
SW
144Mousetrap.bind('x', mark_deleted);
145Mousetrap.bind('X', mark_not_deleted);
d92eb78e 146Mousetrap.bind('c', function() { $("#name").show().focus(); return false; });