]> git.scottworley.com Git - picsort/blob - picsorter.js
24cc8938e054363048ab1159ca805662c86a70eb
[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.attr("src", display_filename);
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
51 function say(message) {
52 $("#message").text(message).removeClass("fade");
53 setTimeout(function() { $("#message").addClass("fade"); }, 1);
54 }
55
56 function toggle_zoom() {
57 if (zoom) {
58 zoom = "";
59 } else {
60 zoom = "sm/";
61 }
62 setpic();
63 }
64
65 function move_by_filter(direction, filter) {
66 // Keep moving in direction until filter is satisfied
67 var new_index = input_index;
68 while (true) {
69 var next = new_index + direction;
70 if (next < 0) {
71 say("At beginning");
72 return;
73 }
74 if (next >= files.length) {
75 say("At end");
76 return;
77 }
78 new_index = next;
79 if (filter(new_index)) {
80 break;
81 }
82 }
83 input_index = new_index;
84 say(input_index + " " + (picinfo[files[input_index]].name || ""));
85 setpic();
86 }
87 function move(direction) {
88 move_by_filter(direction, function() { return true; });
89 }
90 function move_to_nondeleted(direction) {
91 move_by_filter(direction, function(i) {
92 return !("deleted" in picinfo[files[i]]); });
93 }
94 function move_to_unnamed(direction) {
95 move_by_filter(direction, function(i) {
96 return !("deleted" in picinfo[files[i]]) &&
97 !("name" in picinfo[files[i]]); });
98 }
99
100 function mark_deleted() {
101 picinfo[files[input_index]].deleted = 1;
102 save_picinfo();
103 say("Deleted");
104 }
105
106 function mark_not_deleted() {
107 delete picinfo[files[input_index]].deleted;
108 save_picinfo();
109 say("Undeleted");
110 }
111
112 function change_exposure(amount) {
113 if (!endsWith(files[input_index], ".NEF")) {
114 say("Exposure adjustment not available");
115 return;
116 }
117 exposure += amount;
118 picinfo[files[input_index]].exposure = exposure;
119 save_picinfo();
120 setpic();
121 var display_exposure = (exposure / 4) - 3.5;
122 say((display_exposure >= 0 ? "+" : "") + display_exposure);
123 }
124
125 function rotate() {
126 var rotation = picinfo[files[input_index]].rotate || 0;
127 rotation = (rotation + 90) % 360;
128 if (rotation > 1e-5) {
129 picinfo[files[input_index]].rotate = rotation;
130 } else {
131 delete picinfo[files[input_index]].rotate;
132 }
133 save_picinfo();
134 setpic();
135 }
136
137 function set_name() {
138 var name_input = $("#name").hide().get(0);
139 var name = name_input.value;
140 name_input.value = "";
141 picinfo[files[input_index]].name = name;
142 save_picinfo();
143 say("Named " + name);
144 }
145
146 $(function() {
147 $("#name").hide().on("keyup", function(e) { e.which == 13 && set_name(); });
148 move_to_nondeleted(1);
149 });
150
151 Mousetrap.bind('z', toggle_zoom);
152 Mousetrap.bind('Z', function() { $("#pic").toggleClass("fit_view"); });
153 Mousetrap.bind(['n', 'l'], function() { move_to_nondeleted(1); });
154 Mousetrap.bind(['p', 'h'], function() { move_to_nondeleted(-1); });
155 Mousetrap.bind(['N', 'L'], function() { move_to_unnamed(1); });
156 Mousetrap.bind(['P', 'H'], function() { move_to_unnamed(-1); });
157 Mousetrap.bind(['m n', 'm l'], function() { move(1); });
158 Mousetrap.bind(['m n', 'm h'], function() { move(-1); });
159 Mousetrap.bind(['b', 'k'], function() { change_exposure(1); });
160 Mousetrap.bind(['d', 'j'], function() { change_exposure(-1); });
161 Mousetrap.bind('x', mark_deleted);
162 Mousetrap.bind('X', mark_not_deleted);
163 Mousetrap.bind('r', rotate);
164 Mousetrap.bind('c', function() { $("#name").show().focus(); return false; });