imaginable 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,10 @@
1
1
  (function($){
2
-
2
+
3
3
  var Imaginable = function(element, options) {
4
4
  // CONSTRANTS
5
5
  var ASPECT_RATIOS = {square: '1:1', tv: '3:2', wide: '5:2', portrait: '13:20'}
6
6
  var ASPECT_RATIO_IDS = {original: 0, square: 1, tv: 2, wide: 3, portrait: 4}
7
-
7
+
8
8
  // ELEMENTS - Main
9
9
  var main_elem = $(element);
10
10
  var uploader_elem;
@@ -16,15 +16,15 @@
16
16
  var crop_image_elem;
17
17
  var save_crop_button_elem;
18
18
  var cancel_crop_button_elem;
19
-
19
+
20
20
  // FORM ELEMENTS
21
21
  var uuid_field;
22
22
  var token_field;
23
23
  var version_field;
24
-
24
+
25
25
  // SETTINGS
26
26
  var settings = $.extend({
27
- prefix: main_elem.data('imaginable-prefix'),
27
+ prefix: main_elem.data('imaginable-prefix'),
28
28
  scaleServer: main_elem.data('imaginable-scale-server') || 'http://127.0.0.1:3333',
29
29
  uploadServer: main_elem.data('imaginable-upload-server') || 'http://127.0.0.1:3001',
30
30
  preview_width: main_elem.data('imaginable-preview-width') || 250,
@@ -32,66 +32,99 @@
32
32
  force_crop: main_elem.data('imaginable-force-crop') || false,
33
33
  format: main_elem.data('imaginable-format') || 'original',
34
34
  new_version: main_elem.data('imaginable-new-version') + '' || '0',
35
-
35
+
36
36
  force_crop: main_elem.data('imaginable-force-crop') || false,
37
-
37
+
38
38
  runtimes: 'flash',
39
39
  max_file_size: '10mb',
40
40
  file_data_name: 'image_file',
41
41
  multipart: true,
42
42
  auto_start: true,
43
43
  flash_swf_url: '/javascripts/plupload.flash.swf',
44
- silverlight_xap_url: '/javascripts/plupload.silverlight.xap',
45
- filters: [
46
- {title: "Image files", extensions: "jpg,gif,png"},
47
- ]
44
+ silverlight_xap_url: '/javascripts/plupload.silverlight.xap',
45
+ filters: [
46
+ {title: "Image files", extensions: "jpg,gif,png,jpeg"},
47
+ ]
48
48
  }, options || {});
49
-
49
+
50
50
  // INSTANCE
51
51
  var obj = this;
52
-
52
+
53
53
  // INSTANCE VARIABLES - Shared
54
54
  var existing_image = false;
55
55
  var image_version;
56
-
56
+
57
57
  // INSTANCE VARIABLES - Uploader
58
58
  var uploader;
59
59
  var current_image_uuid;
60
60
  var current_image_token;
61
61
  var current_image_width;
62
62
  var current_image_height;
63
-
63
+
64
64
  // INSTANCE VARIABLES - Cropper
65
65
  var cropper;
66
66
  var current_crop_points = null;
67
-
67
+
68
68
  // Public methods
69
69
  this.refreshPreviewImage = function() {
70
70
  var preview_url = settings.scaleServer + '/image/' + current_image_uuid + '-' + image_version + '-' + settings.format + '-' + settings.preview_width + '.jpg';
71
-
71
+
72
72
  preview_image_elem.attr('src', preview_url).load(function() {
73
- preview_image_elem.fadeIn();
74
- uploader.refresh();
73
+ preview_image_elem.fadeIn();
74
+ uploader.refresh();
75
75
  });
76
76
  }
77
-
77
+
78
78
  this.refreshCroppingPreviewImage = function() {
79
79
  var crop_preview_url = settings.scaleServer + '/image/' + current_image_uuid + '-' + image_version + '-original-600-600.jpg';
80
-
80
+
81
81
  crop_image_elem.attr('src', crop_preview_url).load(function() {
82
82
  if (settings.force_crop == true)
83
83
  obj.showCropDialog();
84
84
  });
85
85
  }
86
-
86
+
87
87
  this.showCropDialog = function() {
88
- cropper_elem.trigger('click');
89
- $.fancybox.center();
88
+ var crop_container = main_elem.find('.imaginable_crop_container').first();
89
+ var crop_content = main_elem.find('.imaginable_crop_content').first();
90
+
91
+ $.fancybox({
92
+ content: crop_content.html(),
93
+ modal: false,
94
+ width: 600,
95
+ height: 670,
96
+ autoSize: false,
97
+ afterShow: function() {
98
+ var crop_points = getCropPoints();
99
+ var img_elm = $('.fancybox-wrap .imaginable_crop_image');
100
+
101
+ cropper = img_elm.imgAreaSelect({
102
+ handles: true,
103
+ instance: true,
104
+ imageHeight: current_image_height,
105
+ imageWidth: current_image_width,
106
+ onSelectEnd: onAreaSelectorSelectEnd
107
+ });
108
+
109
+ if (settings.format != 'original')
110
+ cropper.setOptions({aspectRatio: ASPECT_RATIOS[settings.format]});
111
+
112
+ cropper.setSelection(crop_points['x1'],crop_points['y1'],crop_points['x2'],crop_points['y2'],false);
113
+ cropper.setOptions({ show: true });
114
+ cropper.update();
115
+ },
116
+ afterClose: function() {
117
+ var img_elm = $('.fancybox-wrap .imaginable_crop_image');
118
+ img_elm.imgAreaSelect({
119
+ remove: true
120
+ });
121
+ }
122
+ });
90
123
  };
91
-
124
+
92
125
  this.saveCropping = function() {
93
126
  var crop_points = getCropPoints();
94
-
127
+
95
128
  var prefix = 'crop' + ASPECT_RATIO_IDS[settings.format] + '_';
96
129
  var x = crop_points['x1'];
97
130
  var y = crop_points['y1'];
@@ -101,17 +134,15 @@
101
134
  image_data[prefix + 'y'] = y
102
135
  image_data[prefix + 'width'] = width;
103
136
  var data = {_method: 'PUT', image: image_data, callback: '?', auth_token: current_image_token};
104
-
137
+
105
138
  var url = settings.uploadServer + '/images/' + current_image_uuid;
106
-
107
- console.log(data);
108
-
139
+
109
140
  $.ajaxSetup({
110
141
  headers: {
111
142
  "X-CSRF-Token": $("meta[name='csrf-token']").attr('content')
112
143
  }
113
144
  });
114
-
145
+
115
146
  $.ajax({
116
147
  type: 'POST',
117
148
  url: url,
@@ -119,10 +150,10 @@
119
150
  dataType: 'json',
120
151
  success: onCroppingSaved
121
152
  });
122
-
153
+
123
154
  writeToFormFields();
124
155
  };
125
-
156
+
126
157
  // Private methods
127
158
  var initialize = function() {
128
159
  initializeElements();
@@ -131,40 +162,40 @@
131
162
  initializeUploader();
132
163
  initializeCropper();
133
164
  };
134
-
165
+
135
166
  var initializeElements = function() {
136
167
  // Form Fields
137
168
  uuid_field = $('#' + settings.prefix + '_uuid');
138
169
  token_field = $('#' + settings.prefix + '_token');
139
170
  version_field = $('#' + settings.prefix + '_version');
140
-
171
+
141
172
  if (uuid_field.val() != null && uuid_field.val() != "") {
142
173
  existing_image = true;
143
174
  current_image_uuid = uuid_field.val();
144
175
  current_image_token = token_field.val();
145
176
  downloadImageMetadata();
146
177
  }
147
-
178
+
148
179
  // Uploader
149
180
  uploader_elem = main_elem.find('.imaginable_browse_files_button').first();
150
181
  file_list_elem = main_elem.find('.imaginable_file_list').first();
151
182
  preview_image_elem = main_elem.find('.imaginable_preview_image').first();
152
-
183
+
153
184
  // Cropper
154
185
  cropper_elem = main_elem.find('.imaginable_crop_button').first();
155
-
186
+
156
187
  crop_image_elem = main_elem.find('.imaginable_crop_image').first();
157
188
  save_crop_button_elem = main_elem.find('.imaginable_save_crop_button').first();
158
189
  cancel_crop_button_elem = main_elem.find('.imaginable_cancel_crop_button').first();
159
190
  }
160
-
191
+
161
192
  var initializeImageVersion = function() {
162
193
  image_version = settings.new_version.replace(/'/, "").replace(/'/, "");
163
-
194
+
164
195
  form_value = version_field.val();
165
196
  if (form_value != null && form_value.length > 0)
166
197
  obj.refreshPreviewImage();
167
-
198
+
168
199
  //form_value = version_field.val();
169
200
  //
170
201
  //if (form_value != null && form_value.length > 0) {
@@ -173,13 +204,13 @@
173
204
  // image_version = settings.new_version;
174
205
  //}
175
206
  };
176
-
207
+
177
208
  var initializeButtons = function() {
178
209
  // Cropper
179
- cancel_crop_button_elem.bind('click', onCancelCropButtonClick);
180
- save_crop_button_elem.bind('click', onSaveCropButtonClick);
210
+ $('.imaginable_save_crop_button').live('click', onSaveCropButtonClick);
211
+ $('.imaginable_cancel_crop_button').live('click', onCancelCropButtonClick);
181
212
  };
182
-
213
+
183
214
  var initializeUploader = function() {
184
215
  uploader = new plupload.Uploader({
185
216
  runtimes: settings['runtimes'],
@@ -193,9 +224,9 @@
193
224
  silverlight_xap_url: settings['silverlight_xap_url'],
194
225
  filters: settings['filters']
195
226
  });
196
-
227
+
197
228
  uploader.init();
198
-
229
+
199
230
  uploader.bind('FilesAdded', onUploaderFilesAdded);
200
231
  uploader.bind('FilesRemoved', onUploaderFilesRemoved);
201
232
  uploader.bind('UploadProgress', onUploaderUploadProgress);
@@ -203,47 +234,27 @@
203
234
  uploader.bind('FileUploaded', onUploaderFileUploaded);
204
235
  uploader.bind('UploadComplete', onUploaderUploadComplete);
205
236
  };
206
-
237
+
207
238
  var initializeCropper = function() {
208
- cropper_elem.fancybox({
209
- 'modal' : true,
210
- onComplete: function() {
211
- var crop_points = getCropPoints();
212
-
213
- cropper = crop_image_elem.imgAreaSelect({
214
- handles: true,
215
- instance: true,
216
- imageHeight: current_image_height,
217
- imageWidth: current_image_width,
218
- onSelectEnd: onAreaSelectorSelectEnd
219
- });
220
-
221
- if (settings.format != 'original')
222
- cropper.setOptions({aspectRatio: ASPECT_RATIOS[settings.format]});
223
-
224
- cropper.setSelection(crop_points['x1'],crop_points['y1'],crop_points['x2'],crop_points['y2'],false);
225
- cropper.setOptions({ show: true });
226
- cropper.update();
227
- },
228
- onCleanup: function() {
229
- crop_image_elem.imgAreaSelect({
230
- remove: true
231
- });
232
- }
233
- });
239
+ main_elem.find('.imaginable_crop_button').first().bind('click', onCropperElemClick);
234
240
  };
235
-
241
+
242
+ var onCropperElemClick = function(event) {
243
+ event.preventDefault();
244
+ obj.showCropDialog();
245
+ };
246
+
236
247
  var changeImageVersion = function(version) {
237
248
  image_version = version;
238
249
  writeToFormFields();
239
250
  };
240
-
251
+
241
252
  var getCropPoints = function() {
242
253
  if (current_crop_points != null)
243
254
  return current_crop_points;
244
-
255
+
245
256
  current_crop_points = {x1: 0, x2: current_image_width, y1: 0, y2: current_image_height, width: current_image_width, height: current_image_height};
246
-
257
+
247
258
  if (settings.format != 'original') {
248
259
  var numeric_aspect_ratio = numericAspectRatio();
249
260
  if (numeric_aspect_ratio > 1) {
@@ -258,22 +269,22 @@
258
269
  current_crop_points['width'] = current_crop_points['x2'];
259
270
  }
260
271
  }
261
-
272
+
262
273
  return current_crop_points;
263
274
  };
264
-
275
+
265
276
  var resetCropPoints = function() {
266
277
  current_crop_points = null;
267
278
  current_scaled_crop_points = null;
268
279
  };
269
-
280
+
270
281
  var showCropButton = function() {
271
282
  cropper_elem.fadeIn();
272
283
  };
273
-
284
+
274
285
  var numericAspectRatio = function() {
275
286
  var format = settings.format;
276
-
287
+
277
288
  if (format != 'original') {
278
289
  var aspect_ratio = ASPECT_RATIOS[format];
279
290
  return (d = (aspect_ratio || '').split(/:/))[0] / d[1];
@@ -281,78 +292,76 @@
281
292
  return 0;
282
293
  }
283
294
  };
284
-
295
+
285
296
  var writeToFormFields = function() {
286
297
  uuid_field.val(current_image_uuid);
298
+ uuid_field.trigger('change');
287
299
  token_field.val(current_image_token);
288
300
  version_field.val(image_version);
289
301
  };
290
-
302
+
291
303
  var downloadImageMetadata = function() {
292
304
  var url = settings.uploadServer + '/images/' + current_image_uuid + '?callback=?';
293
305
  $.getJSON(url, onDownloadImageMetadataComplete);
294
306
  };
295
-
307
+
296
308
  var onDownloadImageMetadataComplete = function(data) {
297
- console.log(data);
298
-
299
309
  current_image_width = data.image.width;
300
310
  current_image_height = data.image.height;
301
-
311
+
302
312
  // Make sure that crop points are initialized
303
313
  getCropPoints();
304
-
314
+
305
315
  var prefix = 'crop' + ASPECT_RATIO_IDS[settings.format] + '_';
306
-
316
+
307
317
  current_crop_points.x1 = parseInt(data.image[prefix + 'x']);
308
318
  current_crop_points.y1 = parseInt(data.image[prefix + 'y']);
309
319
  current_crop_points.width = parseInt(data.image[prefix + 'width']);
310
320
  current_crop_points.x2 = current_crop_points.x1 + current_crop_points.width;
311
-
321
+
312
322
  var numeric_aspect_ratio = settings.format != 'original' ? numericAspectRatio() : current_image_width / current_image_height
313
323
 
314
324
  current_crop_points.height = Math.round(parseFloat(current_crop_points.width) / numeric_aspect_ratio);
315
325
  current_crop_points.y2 = current_crop_points.y1 + current_crop_points.height;
316
-
326
+
317
327
  showCropButton();
318
- console.log(current_crop_points);
319
328
  };
320
-
329
+
321
330
  var onUploaderFilesAdded = function(up, files) {
322
331
  $.each(files, function(i, file) {
323
- file_list_elem.append(
324
- '<div id="' + file.id + '">' +
325
- file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
326
- '</div>');
327
- });
328
-
329
- up.refresh(); // Reposition Flash/Silverlight
330
-
331
- if (settings.auto_start == true) up.start();
332
+ file_list_elem.append(
333
+ '<div id="' + file.id + '">' +
334
+ file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
335
+ '</div>');
336
+ });
337
+
338
+ up.refresh(); // Reposition Flash/Silverlight
339
+
340
+ if (settings.auto_start == true) up.start();
332
341
  };
333
-
342
+
334
343
  var onUploaderFilesRemoved = function(up, files) {
335
344
  $.each(files, function(i, file) {
336
- $('#' + file.id).remove();
337
- });
338
-
339
- up.refresh(); // Reposition Flash/Silverlight
345
+ $('#' + file.id).remove();
346
+ });
347
+
348
+ up.refresh(); // Reposition Flash/Silverlight
340
349
  };
341
-
350
+
342
351
  var onUploaderUploadProgress = function(up, file) {
343
352
  $('#' + file.id + " b").html(file.percent + "%");
344
353
  };
345
-
354
+
346
355
  var onUploaderError = function(up, err) {
347
356
  file_list_elem.append("<div>Error: " + err.code +
348
- ", Message: " + err.message +
349
- (err.file ? ", File: " + err.file.name : "") +
350
- "</div>"
351
- );
352
-
353
- up.refresh(); // Reposition Flash/Silverlight
357
+ ", Message: " + err.message +
358
+ (err.file ? ", File: " + err.file.name : "") +
359
+ "</div>"
360
+ );
361
+
362
+ up.refresh(); // Reposition Flash/Silverlight
354
363
  };
355
-
364
+
356
365
  var onUploaderFileUploaded = function(up, file, response) {
357
366
  $('#' + file.id + " b").html("Done");
358
367
  responseData = jQuery.parseJSON(response.response);
@@ -363,7 +372,7 @@
363
372
  resetCropPoints();
364
373
  existing_image = false;
365
374
  };
366
-
375
+
367
376
  var onUploaderUploadComplete = function(up, files) {
368
377
  writeToFormFields();
369
378
  obj.refreshPreviewImage();
@@ -371,20 +380,20 @@
371
380
  showCropButton();
372
381
  up.splice();
373
382
  };
374
-
383
+
375
384
  var onCancelCropButtonClick = function(event) {
376
385
  event.preventDefault();
377
386
  $.fancybox.close();
378
387
  };
379
-
388
+
380
389
  var onSaveCropButtonClick = function(event) {
381
390
  event.preventDefault();
382
391
  obj.saveCropping();
383
392
  }
384
-
393
+
385
394
  var onAreaSelectorSelectEnd = function(img, selection) {
386
395
  selection = cropper.getSelection(false);
387
-
396
+
388
397
  current_crop_points['x1'] = selection['x1'];
389
398
  current_crop_points['y1'] = selection['y1'];
390
399
  current_crop_points['x2'] = selection['x2'];
@@ -392,38 +401,38 @@
392
401
  current_crop_points['width'] = selection['width'];
393
402
  current_crop_points['height'] = selection['height'];
394
403
  };
395
-
404
+
396
405
  var onCroppingSaved = function(response) {
397
406
  changeImageVersion(response.new_version);
398
407
  obj.refreshPreviewImage();
399
408
  $.fancybox.close();
400
409
  };
401
-
410
+
402
411
  // Get it all rolling
403
412
  initialize();
404
413
  };
405
-
414
+
406
415
  $.fn.imaginable = function(options) {
407
416
  return this.each(function() {
408
417
  var element = $(this);
409
-
418
+
410
419
  // Return early if this element already has a plugin instance
411
420
  if (element.data('imaginable')) return;
412
-
421
+
413
422
  var imaginable = new Imaginable(this, options);
414
-
423
+
415
424
  // Store plugin object in this element's data
416
425
  element.data('imaginable', imaginable);
417
426
  });
418
427
  };
419
-
428
+
420
429
  })(jQuery);
421
430
 
422
431
  $(function() {
423
-
432
+
424
433
  $('.imaginable').each(function(){
425
434
  var el = $(this);
426
435
  el.imaginable();
427
436
  });
428
-
429
- });
437
+
438
+ });
@@ -1,3 +1,3 @@
1
1
  module Imaginable
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 5
9
- version: 0.1.5
8
+ - 6
9
+ version: 0.1.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Thomas Dippel