]> git.scottworley.com Git - picsort/blob - picsorter.js
cf2c399f9cb39ae3bbf61e8bec99991fa1bcaa0a
[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 $("#pic").attr("src", display_filename);
44 }
45
46 function say(message) {
47 $("#message").text(message).removeClass("fade");
48 setTimeout(function() { $("#message").addClass("fade"); }, 1);
49 }
50
51 function toggle_zoom() {
52 if (zoom) {
53 zoom = "";
54 } else {
55 zoom = "sm/";
56 }
57 setpic();
58 }
59
60 function move_by_filter(direction, filter) {
61 // Keep moving in direction until filter is satisfied
62 do {
63 input_index += direction;
64 } while (!filter());
65 say(input_index + " " + (picinfo[files[input_index]].name || ""));
66 setpic();
67 }
68 function move(direction) {
69 move_by_filter(direction, function() { return true; });
70 }
71 function move_to_nondeleted(direction) {
72 move_by_filter(direction, function() {
73 return !("deleted" in picinfo[files[input_index]]); });
74 }
75 function move_to_unnamed(direction) {
76 move_by_filter(direction, function() {
77 return !("deleted" in picinfo[files[input_index]]) &&
78 !("name" in picinfo[files[input_index]]); });
79 }
80
81 function mark_deleted() {
82 picinfo[files[input_index]].deleted = 1;
83 save_picinfo();
84 say("Deleted");
85 }
86
87 function mark_not_deleted() {
88 delete picinfo[files[input_index]].deleted;
89 save_picinfo();
90 say("Undeleted");
91 }
92
93 function change_exposure(amount) {
94 if (!endsWith(files[input_index], ".NEF")) {
95 say("Exposure adjustment not available");
96 return;
97 }
98 exposure += amount;
99 picinfo[files[input_index]].exposure = exposure;
100 save_picinfo();
101 setpic();
102 var display_exposure = (exposure / 4) - 3.5;
103 say((display_exposure >= 0 ? "+" : "") + display_exposure);
104 }
105
106 function set_name() {
107 var name_input = $("#name").hide().get(0);
108 var name = name_input.value;
109 name_input.value = "";
110 picinfo[files[input_index]].name = name;
111 save_picinfo();
112 say("Named " + name);
113 }
114
115 $(function() {
116 $("#name").hide().on("keyup", function(e) { e.which == 13 && set_name(); });
117 move_to_nondeleted(1);
118 });
119
120 Mousetrap.bind('z', toggle_zoom);
121 Mousetrap.bind('Z', function() { $("#pic").toggleClass("fit_view"); });
122 Mousetrap.bind(['n', 'l'], function() { move_to_nondeleted(1); });
123 Mousetrap.bind(['p', 'h'], function() { move_to_nondeleted(-1); });
124 Mousetrap.bind(['N', 'L'], function() { move_to_unnamed(1); });
125 Mousetrap.bind(['P', 'H'], function() { move_to_unnamed(-1); });
126 Mousetrap.bind(['m n', 'm l'], function() { move(1); });
127 Mousetrap.bind(['m n', 'm h'], function() { move(-1); });
128 Mousetrap.bind(['b', 'k'], function() { change_exposure(1); });
129 Mousetrap.bind(['d', 'j'], function() { change_exposure(-1); });
130 Mousetrap.bind('x', mark_deleted);
131 Mousetrap.bind('X', mark_not_deleted);
132 Mousetrap.bind('c', function() { $("#name").show().focus(); return false; });