root/trunk/uploadr/MacUploadr.app/Contents/Resources/chrome/content/uploadr/photos.js

Revision 9, 5.0 kB (checked in by rcrowley, 2 years ago)

Full network failure tolerance. It dares you to unplug your ethernet cable.

Line 
1 var photos = {
2
3         // Storage
4         list: [],
5         count: 0,
6         selected: [],
7         last: null,
8         unsaved: false,
9
10         // Batch size limiting
11         batch_size: 0,
12         reached_limit: false,
13
14         // Upload tracking
15         uploading: [],
16         current: 0,
17         uploaded: [],
18         add_to_set: [],
19         failed: [],
20         total: 0,
21         ok: 0,
22         fail: 0,
23
24         // Let the user select some files, thumbnail them and track them
25         add: function() {
26                 document.getElementById('button_upload').disabled = true;
27                 var fp = Cc['@mozilla.org/filepicker;1'].createInstance(Ci.nsIFilePicker);
28                 fp.init(window, locale.getString('dialog.add'),
29                         Ci.nsIFilePicker.modeOpenMultiple);
30                 fp.appendFilters(Ci.nsIFilePicker.filterImages);
31                 var res = fp.show();
32                 if (Ci.nsIFilePicker.returnOK == res) {
33                         var files = fp.files;
34                         while (files.hasMoreElements()) {
35                                 photos._add(files.getNext().QueryInterface(Ci.nsILocalFile).path);
36                         }
37
38                         // After the last file is added, sort the images by date taken
39                         threads.worker.dispatch(new Sort(), threads.worker.DISPATCH_NORMAL);
40
41                 } else if (photos.count) {
42                         document.getElementById('button_upload').disabled = false;
43                 }
44         },
45         _add: function(path) {
46
47                 // Add the original image to the list and set our status
48                 var id = photos.list.length;
49                 photos.list.push(new Photo(id, path));
50                 ++photos.count;
51                 photos.unsaved = true;
52
53                 // Create a spot for the image, leaving a spinning placeholder
54                 //   Add images to the start of the list because this is our best guess for ordering
55                 //   newest to oldest
56                 var img = document.createElementNS(NS_HTML, 'img');
57                 img.className = 'loading';
58                 img.setAttribute('width', 32);
59                 img.setAttribute('height', 32);
60                 img.src = 'chrome://uploadr/skin/loading.gif';
61                 var li = document.createElementNS(NS_HTML, 'li');
62                 li.id = 'photo' + id;
63                 li.appendChild(img);
64                 var list = document.getElementById('list');
65                 list.insertBefore(li, list.firstChild);
66
67                 // Create and show the thumbnail
68                 threads.worker.dispatch(new Thumb(id, uploadr.conf.thumbSize, path),
69                         threads.worker.DISPATCH_NORMAL);
70
71         },
72
73         // Rotate selected files
74         rotate: function(degrees) {
75
76                 // Prevent silliness
77                 var s = photos.selected;
78                 var ii = s.length;
79                 if (0 == ii) {
80                         return;
81                 }
82                 if (1 < ii && !confirm(locale.getString('rotate.confirm'),
83                         locale.getString('rotate.confirm.title'))) {
84                         return;
85                 }
86
87                 // For each selected image, show the loading spinner and dispatch the rotate job
88                 for (var i = 0; i < ii; ++i) {
89                         var p = photos.list[s[i]];
90                         var img = document.getElementById('photo' + p.id).getElementsByTagName('img')[0];
91                         img.className += ' loading';
92                         img.setAttribute('width', 32);
93                         img.setAttribute('height', 32);
94                         img.src = 'chrome://uploadr/skin/loading.gif';
95                         threads.worker.dispatch(new Rotate(p.id, degrees, uploadr.conf.thumbSize,
96                                 p.path), threads.worker.DISPATCH_NORMAL);
97                 }
98
99         },
100
101         // Upload photos
102         upload: function() {
103
104                 // If any photos need resizing to fit in the per-photo size limits, dispatch the
105                 // jobs and wait
106                 var resizing = false;
107                 for each (var p in photos.list) {
108                         if (null != p) {
109                                 if (null != p.resize && -1 != p.resize &&
110                                         p.square > p.resize) {
111                                         resizing = true;
112                                         threads.worker.dispatch(new Resize(p.id, p.resize, p.path),
113                                                 threads.worker.DISPATCH_NORMAL);
114                                 } else if (uploadr.fsize(p.path) > users.filesize && p.square > p.resize) {
115                                         resizing = true;
116                                         threads.worker.dispatch(new Resize(p.id, -1, p.path),
117                                                 threads.worker.DISPATCH_NORMAL);
118                                 }
119                         }
120                 }
121                 if (resizing) {
122                         threads.worker.dispatch(new RetryUpload(), threads.worker.DISPATCH_NORMAL);
123                         return;
124                 }
125
126                 // Decide if we're already in the midst of an upload
127                 var not_started = 0 == photos.uploading.length;
128
129                 // Take the list of photos into upload mode and reset the UI
130                 for each (var p in photos.list) {
131                         photos.uploading.push(p);
132                 }
133                 photos.list = [];
134                 photos.count = 0;
135                 document.getElementById('button_upload').disabled = true;
136                 photos.selected = [];
137                 photos.last = null;
138                 photos.unsaved = false;
139                 var list = document.getElementById('list');
140                 while (list.hasChildNodes()) {
141                         list.removeChild(list.firstChild);
142                 }
143
144                 // Find out how many photos we actually have
145                 photos.total = 0;
146                 var ii = photos.uploading.length;
147                 for (var i = 0; i < ii; ++i) {
148                         if (null != photos.uploading[i]) {
149                                 ++photos.total;
150                         }
151                 }
152
153                 // Update the UI
154                 status.set(locale.getString('status.uploading'));
155
156                 // Kick off the first batch job if we haven't started
157                 if (not_started) {
158                         for (var i = 0; i < ii; ++i) {
159                                 if (null != photos.uploading[i]) {
160                                         upload(i);
161                                         break;
162                                 }
163                         }
164                 }
165
166         }
167
168 };
169
170 // Photo properties
171 var Photo = function(id, path) {
172         this.id = id;
173         this.date_taken = '';
174         this.path = path;
175         var filename = path.match(/([^\/\\]*)$/);
176         if (null == filename) {
177                 this.filename = '';
178         } else {
179                 this.filename = filename[1];
180         }
181         this.square = 0;
182         this.title = '';
183         this.description = '';
184         this.tags = '';
185         this.is_public = settings.is_public;
186         this.is_friend = settings.is_friend;
187         this.is_family = settings.is_family;
188         this.content_type = settings.content_type;
189         this.safety_level = settings.safety_level;
190         this.hidden = settings.hidden;
191         this.resize = settings.resize;
192 };
Note: See TracBrowser for help on using the browser.