tb_photos 1.0.6 → 1.1.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/images/admin/photos/photo_albums_thumb.png +0 -0
  3. data/app/assets/javascripts/admin/photos/photos.js +389 -382
  4. data/app/assets/stylesheets/admin/photos/application.css.scss +49 -51
  5. data/app/controllers/admin/photo_albums_controller.rb +3 -3
  6. data/app/controllers/admin/photo_galleries_controller.rb +4 -3
  7. data/app/controllers/admin/photos_controller.rb +12 -1
  8. data/app/controllers/photo_albums_controller.rb +4 -4
  9. data/app/controllers/photo_galleries_controller.rb +1 -1
  10. data/app/models/spud_photo_album.rb +2 -0
  11. data/app/models/spud_photo_gallery.rb +2 -0
  12. data/app/views/admin/photo_albums/_album.html.erb +5 -5
  13. data/app/views/admin/photo_albums/_form.html.erb +32 -23
  14. data/app/views/admin/photo_albums/destroy.js.erb +1 -1
  15. data/app/views/admin/photo_albums/index.html.erb +1 -1
  16. data/app/views/admin/photo_galleries/_album.html.erb +9 -0
  17. data/app/views/admin/photo_galleries/_form.html.erb +21 -21
  18. data/app/views/admin/photo_galleries/index.html.erb +6 -6
  19. data/app/views/admin/photos/_form.html.erb +11 -28
  20. data/app/views/admin/photos/_photo.html.erb +6 -6
  21. data/app/views/admin/photos/index.html.erb +1 -1
  22. data/app/views/layouts/admin/spud_photos.html.erb +1 -1
  23. data/app/views/photo_albums/index.html.erb +2 -2
  24. data/app/views/photo_albums/show.html.erb +2 -2
  25. data/app/views/photo_galleries/index.html.erb +2 -2
  26. data/db/migrate/20140730201754_add_fingerprint_to_spud_photos.rb +6 -0
  27. data/lib/spud_photos/version.rb +1 -1
  28. metadata +6 -7
  29. data/app/assets/images/admin/photos/buttons/cog_16x16.png +0 -0
  30. data/app/assets/images/admin/photos/buttons/x_16x16.png +0 -0
  31. data/app/assets/images/admin/photos/photo_albums_thumb@2x.png +0 -0
@@ -1,427 +1,434 @@
1
1
  (function(){
2
2
 
3
- spud.admin.photos = {};
4
- var self = spud.admin.photos;
3
+ var html5upload = false;
5
4
 
6
- self.html5upload = false;
7
-
8
- self.init = function(){
5
+ var self = spud.admin.photos = {
6
+ init: function(){
9
7
  // event handlers
10
- $('.spud_admin_photo_ui_thumbs_sortable').sortable({
11
- connectWith:'.spud_admin_photo_ui_thumbs_sortable'
8
+ $('.admin-photo-ui-thumbs-sortable').sortable({
9
+ connectWith:'.admin-photo-ui-thumbs-sortable'
12
10
  });
13
- $('body').on('submit', '#spud_admin_photo_album_form', self.submittedPhotoAlbumForm);
14
- $('body').on('submit', '#spud_admin_photo_gallery_form', self.submittedPhotoGalleryForm);
15
- $('body').on('submit', '#spud_admin_photo_form', self.submittedPhotoForm);
16
- $('body').on('click', '.spud_admin_photos_btn_remove', self.clickedPhotoRemoveFromAlbum);
17
- $('body').on('click', '.spud_admin_photo_ui_thumbs_selectable .spud_admin_photo_ui_thumb', self.selectedPhotoUiThumb);
18
- $('body').on('click', '#spud_admin_photo_album_action_library', self.clickedPhotoLibrary);
19
- $('body').on('click', '#spud_admin_photo_album_action_upload, .spud_admin_photo .spud_admin_photos_btn_edit', self.clickedPhotoAddOrEdit);
20
- $('body').on('click', '.spud_admin_photo_library_add_selected', self.addSelectedPhotosFromLibrary);
21
- $('body').on('click', '.spud_admin_photo_library_delete_selected', self.deleteSelectedPhotosFromLibrary);
11
+ $('body').on('submit', '#admin-photo-album-form', submittedPhotoAlbumForm);
12
+ $('body').on('submit', '#admin-photo-gallery-form', submittedPhotoGalleryForm);
13
+ $('body').on('submit', '#admin-photo-form', submittedPhotoForm);
14
+ $('body').on('click', '.admin-photos-btn-remove', clickedPhotoRemoveFromAlbum);
15
+ $('body').on('click', '.admin-photo-ui-thumbs-selectable .admin-photo-ui-thumb', selectedPhotoUiThumb);
16
+ $('body').on('click', '#admin-photo-album-action-library', clickedPhotoLibrary);
17
+ $('body').on('click', '#admin-photo-album-action-upload, .admin-photo .admin-photos-btn-edit', clickedPhotoAddOrEdit);
18
+ $('body').on('click', '.admin-photo-library-add-selected', addSelectedPhotosFromLibrary);
19
+ $('body').on('click', '.admin-photo-library-delete-selected', deleteSelectedPhotosFromLibrary);
20
+ $('#admin-photo-gallery-form').on('click', '.admin-photos-btn-delete', clickedDeleteAlbumFromGallery);
22
21
 
23
22
  // html5 drag and drop file
24
- if(typeof(FormData) != 'undefined' && typeof(XMLHttpRequest) != 'undefined' && (droparea = document.getElementById('spud_admin_photo_upload_queue'))){
25
- self.html5upload = true;
26
- $('#spud_admin_photo_upload_queue').show();
27
- droparea.addEventListener('dragenter', self.stopDndPropagation, false);
28
- droparea.addEventListener('dragexit', self.stopDndPropagation, false);
29
- droparea.addEventListener('dragover', self.stopDndPropagation, false);
30
- droparea.addEventListener('drop', self.droppedFile, false);
23
+ if(typeof(FormData) != 'undefined' && typeof(XMLHttpRequest) != 'undefined' && (droparea = document.getElementById('admin-photo-upload-queue'))){
24
+ html5upload = true;
25
+ $('#admin-photo-upload-queue').show();
26
+ droparea.addEventListener('dragenter', stopDndPropagation, false);
27
+ droparea.addEventListener('dragexit', stopDndPropagation, false);
28
+ droparea.addEventListener('dragover', stopDndPropagation, false);
29
+ droparea.addEventListener('drop', droppedFile, false);
31
30
 
32
31
  // prevent accidental drops outside the queue
33
32
  var body = document.getElementsByTagName("body")[0];
34
- body.addEventListener('dragenter', self.stopDndPropagation, false);
35
- body.addEventListener('dragexit', self.stopDndPropagation, false);
36
- body.addEventListener('dragover', self.stopDndPropagation, false);
37
- body.addEventListener('drop', self.stopDndPropagation, false);
33
+ body.addEventListener('dragenter', stopDndPropagation, false);
34
+ body.addEventListener('dragexit', stopDndPropagation, false);
35
+ body.addEventListener('dragover', stopDndPropagation, false);
36
+ body.addEventListener('drop', stopDndPropagation, false);
38
37
  }
39
- };
40
-
41
- self.submittedPhotoAlbumForm = function(e){
42
-
43
- };
44
-
45
- self.submittedPhotoGalleryForm = function(e){
46
- $('#spud_admin_photo_albums_available .spud_admin_photo_ui_thumb').remove();
47
- };
48
-
49
- self.clickedPhotoRemoveFromAlbum = function(e){
50
- $(this).parents('.spud_admin_photo_ui_thumb').fadeOut(200, function(){
51
- $(this).remove();
52
- });
53
- return false;
54
- };
55
-
56
- self.validatePhoto = function(file) {
57
- var errors = [];
38
+ },
58
39
 
59
- if(file) {
60
- var allowedTypes = ["image/jpeg", "image/png", "image/gif", "image/bmp"];
61
- if(allowedTypes.indexOf(file.type) < 0) {
62
- errors.push("Unsupported file format");
63
- }
64
- var maxAllowedSize = spud.admin.photos.max_image_upload_size_bytes;
65
- if(maxAllowedSize && file.size > maxAllowedSize) {
66
- errors.push("Your file size of " + self.getFileSizeHumanized(file.size) + " exceeded the maximum limit of " + spud.admin.photos.max_image_upload_size_humanized);
67
- }
68
- }
69
- else {
70
- if($('.admin-photo-form-current-photo').length === 0){
71
- errors.push("No file found");
72
- }
73
- }
74
- return errors;
75
- };
76
-
77
- /* Handle file uploads passed via iframe (legacy support)
78
- * -------------------------------------------------------- */
79
-
80
- self.photoLegacyUploadErrors = function(html){
81
- $('#spud_admin_photo_form').replaceWith(html);
82
- };
83
-
84
- self.photoLegacyUploadComplete = function(id, html){
85
- var element = $('#spud_admin_photo_' + id);
86
- if(element.length > 0){
87
- element.replaceWith(html);
88
- }
89
- else{
90
- var target = $('#spud_admin_photos_selected, #spud_admin_photos');
91
- target.prepend(html);
92
- }
93
- hideModalDialog();
94
- };
95
-
96
- self.selectedPhotoUiThumb = function(e){
97
- var thumb = $(this);
98
- if(thumb.hasClass('spud_admin_photo_ui_thumb_selected')){
99
- $(this).removeClass('spud_admin_photo_ui_thumb_selected');
100
- }
101
- else{
102
- $(this).addClass('spud_admin_photo_ui_thumb_selected');
40
+ getFileSizeHumanized: function(bytes) {
41
+ var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
42
+ if(bytes === 0){
43
+ return 'n/a';
103
44
  }
104
- };
45
+ var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10);
46
+ return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
47
+ },
105
48
 
106
- self.markPhotoAsDeleted = function(photo_id){
107
- var photo = $('#spud_admin_photo_' + photo_id);
49
+ /* Ajax Callbacks
50
+ -------------------*/
51
+ markPhotoAsDeleted: function(photo_id){
52
+ var photo = $('#admin-photo-' + photo_id);
108
53
  photo.fadeOut(200, function(){
109
54
  photo.remove();
110
55
  });
111
- };
56
+ },
112
57
 
113
- self.markPhotoAlbumAsDeleted = function(photo_album_id){
114
- var photo_album = $('#spud_admin_photo_album_' + photo_album_id);
58
+ markPhotoAlbumAsDeleted: function(photo_album_id){
59
+ var photo_album = $('#admin-photo-album-' + photo_album_id);
115
60
  photo_album.fadeOut(200, function(){
116
61
  photo_album.remove();
117
62
  });
118
- };
63
+ },
119
64
 
120
- self.markPhotoGalleryAsDeleted = function(photo_gallery_id){
121
- var photo_gallery = $('#spud_admin_photo_gallery_' + photo_gallery_id);
65
+ markPhotoGalleryAsDeleted: function(photo_gallery_id){
66
+ var photo_gallery = $('#admin-photo-gallery-' + photo_gallery_id);
122
67
  photo_gallery.fadeOut(200, function(){
123
68
  photo_gallery.remove();
124
69
  });
125
- };
70
+ },
126
71
 
127
- /*
128
- * Single-Photo Form Upload
129
- -------------------------------- */
130
-
131
- self.generateFileUploadErrors = function(errors, opts) {
72
+ /* Handle file uploads passed via iframe (legacy support)
73
+ * -------------------------------------------------------- */
74
+ photoLegacyUploadErrors: function(html){
75
+ $('#admin-photo-form').replaceWith(html);
76
+ },
132
77
 
133
- if(typeof errors == "string") {
134
- errors = [errors];
78
+ photoLegacyUploadComplete: function(id, html){
79
+ var element = $('#admin-photo-' + id);
80
+ if(element.length > 0){
81
+ element.replaceWith(html);
135
82
  }
136
- var totalErrors = errors.length;
137
- var errorMsg = totalErrors + " errors prohibited you from saving:";
138
- if(totalErrors == 1) {
139
- errorMsg = "1 error prohibited you from saving:";
83
+ else{
84
+ var target = $('#admin-photos-selected, #admin-photos');
85
+ target.prepend(html);
140
86
  }
141
- if(opts && opts.showAsDialog) {
142
- alert(errorMsg + '\n\t' + errors.join('\n\t'));
143
- return false;
87
+ spud.admin.modal.hide();
88
+ }
89
+ };
90
+
91
+ var submittedPhotoAlbumForm = function(e){
92
+
93
+ };
94
+
95
+ var submittedPhotoGalleryForm = function(e){
96
+ $('.admin-photo-albums-available input').remove();
97
+ };
98
+
99
+ var clickedDeleteAlbumFromGallery = function(e){
100
+ e.preventDefault();
101
+ var thumb = $(this).parents('.admin-photo-ui-thumb');
102
+ thumb.remove();
103
+ $('.admin-photo-albums-available').append(thumb);
104
+ };
105
+
106
+ var clickedPhotoRemoveFromAlbum = function(e){
107
+ $(this).parents('.admin-photo-ui-thumb').fadeOut(200, function(){
108
+ $(this).remove();
109
+ });
110
+ return false;
111
+ };
112
+
113
+ var validatePhoto = function(file) {
114
+ var errors = [];
115
+ if(file) {
116
+ var allowedTypes = ["image/jpeg", "image/png", "image/gif", "image/bmp"];
117
+ if(allowedTypes.indexOf(file.type) < 0) {
118
+ errors.push("Unsupported file format");
144
119
  }
145
-
146
- $(".spud_admin_form_error_list").remove();
147
- $("#spud_admin_photo_form").find("[for=spud_photo_photo]").css("color","black");
148
- $("#spud_admin_photo_form").before('<div class="spud_admin_form_error_list"><ul><h4>' + errorMsg +'</h4></ul></div>');
149
- $("#spud_admin_photo_form").find("[for=spud_photo_photo]").css("color","red");
150
- for(var i = 0; i < errors.length; i++) {
151
- $(".spud_admin_form_error_list > ul").append("<li>"+ errors[i] + "</li>");
120
+ var maxAllowedSize = spud.admin.photos.max_image_upload_size_bytes;
121
+ if(maxAllowedSize && file.size > maxAllowedSize) {
122
+ errors.push("Your file size of " + self.getFileSizeHumanized(file.size) + " exceeded the maximum limit of " + spud.admin.photos.max_image_upload_size_humanized);
152
123
  }
124
+ }
125
+ else{
126
+ if($('.admin-photo-form-current-photo').length === 0){
127
+ errors.push("No file found");
128
+ }
129
+ }
130
+ return errors;
131
+ };
132
+
133
+ var selectedPhotoUiThumb = function(e){
134
+ var thumb = $(this);
135
+ if(thumb.hasClass('admin-photo-ui-thumb-selected')){
136
+ $(this).removeClass('admin-photo-ui-thumb-selected');
137
+ }
138
+ else{
139
+ $(this).addClass('admin-photo-ui-thumb-selected');
140
+ }
141
+ };
142
+
143
+ /*
144
+ * Single-Photo Form Upload
145
+ -------------------------------- */
146
+
147
+ var generateFileUploadErrors = function(errors, opts) {
148
+
149
+ if(typeof errors == "string") {
150
+ errors = [errors];
151
+ }
152
+ var totalErrors = errors.length;
153
+ var errorMsg = totalErrors + " errors prohibited you from saving:";
154
+ if(totalErrors == 1) {
155
+ errorMsg = "1 error prohibited you from saving:";
156
+ }
157
+ if(opts && opts.showAsDialog) {
158
+ alert(errorMsg + '\n\t' + errors.join('\n\t'));
153
159
  return false;
154
- };
155
-
156
- self.getFileSizeHumanized = function(bytes) {
157
- var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
158
- if(bytes === 0){
159
- return 'n/a';
160
+ }
161
+
162
+ $(".form-errors").remove();
163
+ $("#admin-photo-form").find("[for=spud_photo_photo]").css("color","black");
164
+ $("#admin-photo-form").before('<div class="form-errors"><h4>' + errorMsg +'</h4><ul></ul></div>');
165
+ $("#admin-photo-form").find("[for=spud_photo_photo]").css("color","red");
166
+ for(var i = 0; i < errors.length; i++) {
167
+ $(".form-errors > ul").append("<li>"+ errors[i] + "</li>");
168
+ }
169
+ return false;
170
+ };
171
+
172
+ var submittedPhotoForm = function(e){
173
+ if(html5upload){
174
+ // create a FormData object and attach form values
175
+ var fd = new FormData();
176
+ var form = $(this);
177
+ fd.append('_method', form.find('[name=_method]').val());
178
+ fd.append('authenticity_token', form.find('[name=authenticity_token]').val());
179
+ fd.append('spud_photo[title]', form.find('#spud_photo_title').val());
180
+ fd.append('spud_photo[caption]', form.find('#spud_photo_caption').val());
181
+
182
+ // progress bar to send events to
183
+ var progressBar;
184
+ var file = form.find('#spud_photo_photo')[0].files[0];
185
+ var photoValidationErrors = validatePhoto(file);
186
+ if(photoValidationErrors.length) {
187
+ return generateFileUploadErrors(photoValidationErrors);
160
188
  }
161
- var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10);
162
- return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
163
- };
164
-
165
- self.submittedPhotoForm = function(e){
166
-
167
- if(self.html5upload){
168
- // create a FormData object and attach form values
169
- var fd = new FormData();
170
- var form = $(this);
171
- fd.append('_method', form.find('[name=_method]').val());
172
- fd.append('authenticity_token', form.find('[name=authenticity_token]').val());
173
- fd.append('spud_photo[title]', form.find('#spud_photo_title').val());
174
- fd.append('spud_photo[caption]', form.find('#spud_photo_caption').val());
175
-
176
- // progress bar to send events to
177
- var progressBar;
178
- var file = form.find('#spud_photo_photo')[0].files[0];
179
- var photoValidationErrors = self.validatePhoto(file);
180
- if(photoValidationErrors.length) {
181
- return self.generateFileUploadErrors(photoValidationErrors);
182
- }
183
-
184
- if(file){
185
- progressBar = self.progressBarForUpload(file.name);
186
- fd.append('spud_photo[photo]', file);
187
- form.append(progressBar);
188
- }
189
- else{
190
- progressBar = self.progressBarForUpload('');
191
- }
192
-
193
- // send FormData object as ajax request
194
- var xhr = new XMLHttpRequest();
195
- xhr.upload.addEventListener('progress', function(e){ self.onPhotoUploadProgress(e, progressBar); }, false);
196
- xhr.addEventListener('load', function(e){ self.onPhotoUploadComplete(e, progressBar); }, false);
197
- xhr.addEventListener('error', function(e){ self.onPhotoUploadFailure(e, progressBar); }, false);
198
- xhr.addEventListener('abort', function(e){ self.onPhotoUploadCancel(e, progressBar); }, false);
199
- xhr.open('POST', form.attr('action'));
200
- xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
201
- xhr.send(fd);
202
- return false;
189
+
190
+ if(file){
191
+ progressBar = progressBarForUpload(file.name);
192
+ fd.append('spud_photo[photo]', file);
193
+ form.append(progressBar);
203
194
  }
204
- };
205
-
206
- /*
207
- * Upload Progress Monitoring
208
- -------------------------------- */
209
-
210
- self.progressBarForUpload = function(fileName){
211
- var html = [
212
- '<div class="spud_admin_photo_progress">',
213
- '<h6>',
214
- '<span class="spud_admin_photo_progress_filename">'+fileName+'</span>: ',
215
- '<span class="spud_admin_photo_progress_status">Uploading</span>',
216
- '</h6>',
217
- '<div class="progress progress-striped active">',
218
- '<div class="bar" style="width: 0;"></div>',
219
- '</div>',
220
- '</div>'
221
- ].join('');
222
- return $(html);
223
- };
224
-
225
- self.onPhotoUploadProgress = function(e, progressBar){
226
- var percent = Math.round(e.loaded * 100 / e.total);
227
- progressBar.find('.bar').css({width: percent + '%'});
228
- if(percent == 100){
229
- progressBar.find('.progress').addClass('progress-success');
230
- progressBar.find('.spud_admin_photo_progress_status').text('Processing');
195
+ else{
196
+ progressBar = progressBarForUpload('');
231
197
  }
232
- };
233
-
234
- self.onPhotoUploadComplete = function(e, progressBar){
235
- // success
236
- var photo = $.parseJSON(e.target.response);
237
- if(e.target.status == 200){
238
- progressBar.find('.spud_admin_photo_progress_status').text('Done!');
239
- progressBar.find('.progress').removeClass('progress-striped active');
240
- var element = $('#spud_admin_photo_' + photo.id);
241
- if(element.length > 0){
242
- element.replaceWith(photo.html);
243
- }
244
- else{
245
- var target = $('#spud_admin_photos_selected, #spud_admin_photos');
246
- target.prepend(photo.html).fadeIn(200);
247
- }
248
- hideModalDialog();
198
+
199
+ // send FormData object as ajax request
200
+ var xhr = new XMLHttpRequest();
201
+ xhr.upload.addEventListener('progress', function(e){ onPhotoUploadProgress(e, progressBar); }, false);
202
+ xhr.addEventListener('load', function(e){ onPhotoUploadComplete(e, progressBar); }, false);
203
+ xhr.addEventListener('error', function(e){ onPhotoUploadFailure(e, progressBar); }, false);
204
+ xhr.addEventListener('abort', function(e){ onPhotoUploadCancel(e, progressBar); }, false);
205
+ xhr.open('POST', form.attr('action'));
206
+ xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
207
+ xhr.send(fd);
208
+ return false;
209
+ }
210
+ };
211
+
212
+ /*
213
+ * Upload Progress Monitoring
214
+ -------------------------------- */
215
+
216
+ var progressBarForUpload = function(fileName){
217
+ var html = [
218
+ '<div class="admin-photo-progress">',
219
+ '<h6>',
220
+ '<span class="admin-photo-progress_filename">'+fileName+'</span>: ',
221
+ '<span class="admin-photo-progress_status">Uploading</span>',
222
+ '</h6>',
223
+ '<div class="progress">',
224
+ '<div class="progress-bar progress-bar-striped active" style="width: 0;"></div>',
225
+ '</div>',
226
+ '</div>'
227
+ ].join('');
228
+ return $(html);
229
+ };
230
+
231
+ var onPhotoUploadProgress = function(e, progressBar){
232
+ var percent = Math.round(e.loaded * 100 / e.total);
233
+ progressBar.find('.progress-bar').css({width: percent + '%'});
234
+ if(percent == 100){
235
+ progressBar.find('.admin-photo-progress_status').text('Processing');
236
+ }
237
+ };
238
+
239
+ var onPhotoUploadComplete = function(e, progressBar){
240
+ // success
241
+ var photo = $.parseJSON(e.target.response);
242
+ if(e.target.status == 200){
243
+ progressBar.find('.admin-photo-progress_status').text('Done!');
244
+ progressBar.find('.progress-bar').removeClass('progress-bar-striped active').addClass('progress-bar-success');
245
+ var element = $('#admin-photo-' + photo.id);
246
+ if(element.length > 0){
247
+ element.replaceWith(photo.html);
249
248
  }
250
- // validation error
251
249
  else{
252
- $("#modal_window .modal-body").html(photo.html);
250
+ var target = $('#admin-photos-selected, #admin-photos');
251
+ target.prepend(photo.html).fadeIn(200);
253
252
  }
254
- };
255
-
256
- self.onPhotoUploadCancel = function(e, progressBar){
257
- progressBar.find('.spud_admin_photo_progress_status').text('Done!');
258
- progressBar.find('.progress').addClass('progress-danger');
259
- };
260
-
261
- self.onPhotoUploadFailure = function(e, progressBar){
262
- if(typeof(console) == 'object'){
263
- console.error('An unexpected error occurred during upload', e);
253
+ spud.admin.modal.hide();
254
+ }
255
+ // validation error
256
+ else{
257
+ $("#modal_window .modal-body").html(photo.html);
258
+ }
259
+ };
260
+
261
+ var onPhotoUploadCancel = function(e, progressBar){
262
+ progressBar.find('.admin-photo-progress_status').text('Done!');
263
+ progressBar.find('.progress').addClass('progress-danger');
264
+ };
265
+
266
+ var onPhotoUploadFailure = function(e, progressBar){
267
+ if(typeof(console) == 'object'){
268
+ console.error('An unexpected error occurred during upload', e);
269
+ }
270
+ };
271
+
272
+ /*
273
+ * Photo Upload/Edit Form
274
+ ------------------------------- */
275
+ var clickedPhotoAddOrEdit = function(e){
276
+ var url = this.href;
277
+ $.ajax({
278
+ url:url,
279
+ success:photoUploadFormLoaded
280
+ });
281
+ return false;
282
+ };
283
+
284
+ var photoUploadFormLoaded = function(html){
285
+ spud.admin.modal.displayWithOptions({
286
+ title: 'Upload Photo',
287
+ html: html
288
+ });
289
+ $(".admin-photo-form-max-size").text('Maximum upload size: '+ spud.admin.photos.max_image_upload_size_humanized);
290
+ };
291
+
292
+ /*
293
+ * Add From Photo Library
294
+ ------------------------------- */
295
+
296
+ var clickedPhotoLibrary = function(e){
297
+ var url = this.href;
298
+ $.ajax({
299
+ url:url,
300
+ success:photoLibraryLoaded
301
+ });
302
+ return false;
303
+ };
304
+
305
+ var photoLibraryLoaded = function(html){
306
+ var dialog = $('#modal_window');
307
+ $('#admin-photos-selected .admin-photo-ui-thumb').each(function(){
308
+ var id = $(this).attr('id');
309
+ var dupe = dialog.find('#'+id);
310
+ if(dupe){
311
+ dupe.remove();
264
312
  }
265
- };
266
-
267
- /*
268
- * Photo Upload/Edit Form
269
- ------------------------------- */
270
- self.clickedPhotoAddOrEdit = function(e){
271
- var url = this.href;
272
- $.ajax({
273
- url:url,
274
- success:self.photoUploadFormLoaded
275
- });
276
- return false;
277
- };
278
-
279
- self.photoUploadFormLoaded = function(html){
280
- displayModalDialogWithOptions({
281
- title: 'Upload Photo',
282
- html: html
283
- });
284
- $("#fileUploadSizeLegend").html('File Upload <span class="split-pane-item-meta">max allowed size : '+ spud.admin.photos.max_image_upload_size_humanized +'</span>');
285
- };
286
-
287
- /*
288
- * Add From Photo Library
289
- ------------------------------- */
290
-
291
- self.clickedPhotoLibrary = function(e){
292
- var url = this.href;
293
- $.ajax({
294
- url:url,
295
- success:self.photoLibraryLoaded
296
- });
297
- return false;
298
- };
299
-
300
- self.photoLibraryLoaded = function(html){
301
- var dialog = $('#modal_window');
302
- $('#spud_admin_photos_selected .spud_admin_photo_ui_thumb').each(function(){
303
- var id = $(this).attr('id');
304
- var dupe = dialog.find('#'+id);
305
- if(dupe){
306
- dupe.remove();
307
- }
308
- });
309
- displayModalDialogWithOptions({
310
- title: 'My Photo Library',
311
- html: html,
312
- buttons:{
313
- 'spud_admin_photo_library_add_selected btn-primary': 'Add Selected',
314
- 'spud_admin_photo_library_delete_selected btn-danger': 'Delete Selected'
315
- }
316
- });
317
- };
318
-
319
- self.addSelectedPhotosFromLibrary = function(e){
320
- $('#spud_admin_photo_library .spud_admin_photo_ui_thumb_selected')
321
- .removeClass('spud_admin_photo_ui_thumb_selected')
322
- .prependTo('#spud_admin_photos_selected')
323
- .hide()
324
- .fadeIn(200);
325
- hideModalDialog();
326
- };
327
-
328
- self.deleteSelectedPhotosFromLibrary = function(e){
329
- var ids = $.map($('.spud_admin_photo_ui_thumb_selected'), function(val, i){
330
- return $(val).attr('rel');
331
- });
332
- $.ajax({
333
- type: 'POST',
334
- url: '/admin/photos/mass_destroy',
335
- data: {spud_photo_ids:ids},
336
- success: function(data, textStatus, jqXHR){
337
- $('.spud_admin_photo_ui_thumb_selected').fadeOut(200, function(){
338
- $(this).remove();
339
- });
340
- },
341
- error: function(jqXHR, textStatus, errorThrown){
342
- console.log('An error occurred:');
343
- console.log(arguments);
344
- }
345
- });
346
-
347
- };
348
-
349
- /*
350
- * Drag & Drop File Upload Queue
351
- -------------------------------- */
352
-
353
- self.fileQueue = [];
354
- self.fileQueueStarted = false;
355
-
356
- // prevent default browser behavior of opening the dropped file
357
- self.stopDndPropagation = function(e){
358
- e.stopPropagation();
359
- e.preventDefault();
360
- };
361
-
362
- // add files to queue. starts queue if not started already
363
- self.droppedFile = function(e){
364
- e.stopPropagation();
365
- e.preventDefault();
366
- $('#spud_admin_photo_upload_queue').show();
367
- var files = e.dataTransfer.files;
368
- var i = 0;
369
- while(i < files.length){
370
- var file = files[i];
371
- var photoValidationErrors = self.validatePhoto(file);
372
- if(photoValidationErrors.length) {
373
- return self.generateFileUploadErrors(photoValidationErrors, {showAsDialog: true});
374
- }
375
- self.fileQueue.push(file);
376
- i++;
313
+ });
314
+ spud.admin.modal.displayWithOptions({
315
+ title: 'My Photo Library',
316
+ html: html,
317
+ buttons:{
318
+ 'admin-photo-library-add-selected btn-primary': 'Add Selected',
319
+ 'admin-photo-library-delete-selected btn-danger': 'Delete Selected'
377
320
  }
378
- self.updateQueueCountLabel();
379
- if(!this.fileQueueStarted){
380
- self.uploadNextPhoto();
381
- if(self.fileQueue.length > 0){
382
- self.uploadNextPhoto();
321
+ });
322
+ };
323
+
324
+ var addSelectedPhotosFromLibrary = function(e){
325
+ $('#admin-photo-library .admin-photo-ui-thumb-selected')
326
+ .removeClass('admin-photo-ui-thumb-selected')
327
+ .prependTo('#admin-photos-selected')
328
+ .hide()
329
+ .fadeIn(200);
330
+ spud.admin.modal.hide();
331
+ };
332
+
333
+ var deleteSelectedPhotosFromLibrary = function(e){
334
+ var ids = $.map($('.admin-photo-ui-thumb-selected'), function(val, i){
335
+ return $(val).attr('rel');
336
+ });
337
+ $.ajax({
338
+ type: 'POST',
339
+ url: '/admin/photos/mass_destroy',
340
+ data: {spud_photo_ids:ids},
341
+ success: function(data, textStatus, jqXHR){
342
+ $('.admin-photo-ui-thumb-selected').fadeOut(200, function(){
343
+ $(this).remove();
344
+ });
345
+ for(var i=0; i<ids.length; i++){
346
+ spud.admin.photos.markPhotoAsDeleted(ids[i]);
383
347
  }
348
+ },
349
+ error: function(jqXHR, textStatus, errorThrown){
350
+ console.log('An error occurred:');
351
+ console.log(arguments);
384
352
  }
385
- };
386
-
387
- self.updateQueueCountLabel = function(){
388
- $('#spud_admin_photo_upload_queue_label span').text(self.fileQueue.length);
389
- };
390
-
391
- self.uploadNextPhoto = function(){
392
- if(self.fileQueue.length === 0){
393
- self.fileQueueStarted = false;
394
- return;
353
+ });
354
+ };
355
+
356
+ /*
357
+ * Drag & Drop File Upload Queue
358
+ -------------------------------- */
359
+
360
+ var fileQueue = [];
361
+ var fileQueueStarted = false;
362
+
363
+ // prevent default browser behavior of opening the dropped file
364
+ var stopDndPropagation = function(e){
365
+ e.stopPropagation();
366
+ e.preventDefault();
367
+ };
368
+
369
+ // add files to queue. starts queue if not started already
370
+ var droppedFile = function(e){
371
+ e.stopPropagation();
372
+ e.preventDefault();
373
+ $('#admin-photo-upload-queue').show();
374
+ var files = e.dataTransfer.files;
375
+ var i = 0;
376
+ while(i < files.length){
377
+ var file = files[i];
378
+ var photoValidationErrors = validatePhoto(file);
379
+ if(photoValidationErrors.length) {
380
+ return generateFileUploadErrors(photoValidationErrors, {showAsDialog: true});
395
381
  }
396
-
397
- // formdata object
398
- self.fileQueueStarted = true;
399
- var file = self.fileQueue.pop();
400
- var fd = new FormData();
401
- fd.append('spud_photo[photo]', file);
402
-
403
- var csrf = $('meta[name=csrf-token]').prop('content');
404
- fd.append('authenticity_token', csrf);
405
-
406
- // create a progress bar
407
- var progressBar = self.progressBarForUpload(file.name);
408
- $('#spud_admin_photo_upload_queue_bars').prepend(progressBar);
409
-
410
- // send formdata as xhr
411
- var xhr = new XMLHttpRequest();
412
- xhr.upload.addEventListener('progress', function(e){ self.onPhotoUploadProgress(e, progressBar); }, false);
413
- xhr.addEventListener('load', function(e){ self.onQueuedPhotoUploadComplete(e, progressBar); }, false);
414
- xhr.addEventListener('error', function(e){ self.onPhotoUploadFailure(e, progressBar); }, false);
415
- xhr.addEventListener('abort', function(e){ self.onPhotoUploadCancel(e, progressBar); }, false);
416
- xhr.open('POST', '/admin/photos');
417
- xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
418
- xhr.send(fd);
419
- };
420
-
421
- self.onQueuedPhotoUploadComplete = function(e, progressBar){
422
- self.onPhotoUploadComplete(e, progressBar);
423
- self.updateQueueCountLabel();
424
- self.uploadNextPhoto();
425
- };
382
+ fileQueue.push(file);
383
+ i++;
384
+ }
385
+ updateQueueCountLabel();
386
+ if(!this.fileQueueStarted){
387
+ uploadNextPhoto();
388
+ if(fileQueue.length > 0){
389
+ uploadNextPhoto();
390
+ }
391
+ }
392
+ };
393
+
394
+ var updateQueueCountLabel = function(){
395
+ $('#admin-photo-upload-queue_label span').text(fileQueue.length);
396
+ };
397
+
398
+ var uploadNextPhoto = function(){
399
+ if(fileQueue.length === 0){
400
+ fileQueueStarted = false;
401
+ return;
402
+ }
403
+
404
+ // formdata object
405
+ fileQueueStarted = true;
406
+ var file = fileQueue.pop();
407
+ var fd = new FormData();
408
+ fd.append('spud_photo[photo]', file);
409
+
410
+ var csrf = $('meta[name=csrf-token]').prop('content');
411
+ fd.append('authenticity_token', csrf);
412
+
413
+ // create a progress bar
414
+ var progressBar = progressBarForUpload(file.name);
415
+ $('#admin-photo-upload-queue_bars').prepend(progressBar);
416
+
417
+ // send formdata as xhr
418
+ var xhr = new XMLHttpRequest();
419
+ xhr.upload.addEventListener('progress', function(e){ onPhotoUploadProgress(e, progressBar); }, false);
420
+ xhr.addEventListener('load', function(e){ onQueuedPhotoUploadComplete(e, progressBar); }, false);
421
+ xhr.addEventListener('error', function(e){ onPhotoUploadFailure(e, progressBar); }, false);
422
+ xhr.addEventListener('abort', function(e){ onPhotoUploadCancel(e, progressBar); }, false);
423
+ xhr.open('POST', '/admin/photos');
424
+ xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
425
+ xhr.send(fd);
426
+ };
427
+
428
+ var onQueuedPhotoUploadComplete = function(e, progressBar){
429
+ onPhotoUploadComplete(e, progressBar);
430
+ updateQueueCountLabel();
431
+ uploadNextPhoto();
432
+ };
426
433
 
427
434
  })();