@@ -17,16 +17,17 @@ define(function(require) {
1717 * @param {jQuery } $button The file upload button
1818 */
1919 var ImageUploadBeta = function ( $button ) {
20- this . $uploadButton = $button ;
21- this . $cropModal = $ ( "#modal--crop" ) ;
22- this . $reportbackForm = $ ( "#dosomething-reportback-form" ) ;
23- this . $imageForm = this . $cropModal . find ( "#dosomething-reportback-image-form" ) ;
24- this . $imageCaption = this . $imageForm . find ( "#modal-caption" ) ;
25- this . $cropButton = this . $imageForm . find ( ".button" ) ;
26- this . $imagePreview = this . $cropModal . find ( ".image-preview" ) ;
27- this . imageValues = { } ;
28- this . cropEnabled = ( typeof Drupal . settings . dsReportback . cropEnabled !== "undefined" ) ? Drupal . settings . dsReportback . cropEnabled : false ;
29- this . readyToSave = false ;
20+ this . $uploadButton = $button ;
21+ this . $cropModal = $ ( "#modal--crop" ) ;
22+ this . $reportbackForm = $ ( "#dosomething-reportback-form" ) ;
23+ this . $imageForm = this . $cropModal . find ( "#dosomething-reportback-image-form" ) ;
24+ this . $imageCaption = this . $imageForm . find ( "#modal-caption" ) ;
25+ this . $cropButton = this . $imageForm . find ( ".button" ) ;
26+ this . $imagePreview = this . $cropModal . find ( ".image-preview" ) ;
27+ this . $reportbackSubmissions = $ ( ".reportback__submissions" ) ;
28+ this . imageValues = { } ;
29+ this . cropEnabled = ( typeof Drupal . settings . dsReportback . cropEnabled !== "undefined" ) ? Drupal . settings . dsReportback . cropEnabled : false ;
30+ this . readyToSave = false ;
3031
3132 this . init ( ) ;
3233 } ;
@@ -56,91 +57,15 @@ define(function(require) {
5657 var blob = _this . getStrippedBlob ( this . result ) ;
5758 var urlCreator = window . URL || window . webkitURL ;
5859 var imageUrl = urlCreator . createObjectURL ( blob ) ;
59- var image = new Image ( ) ;
60-
61- image . src = imageUrl ;
62- image . onload = function ( ) {
63- var image = this ;
64- var isValid = _this . validImage ( this ) ;
65- var $errorMsg = _this . $uploadButton . parent ( ) . siblings ( ".error" ) ;
66-
67- if ( isValid ) {
68- // Remove any previously added error message.
69- if ( $errorMsg . length ) {
70- $errorMsg . remove ( ) ;
71- }
72-
73- // Open the modal.
74- _this . openModal ( ) ;
75-
76- // Add the preview image to the modal
77- _this . previewImage ( image , _this . $imagePreview ) ;
78-
79- //Unbind before rebind so it doesnt fire twice.
80- _this . $cropButton . off ( "click" ) ;
81-
82- // When the user submits the modal form, send crop values to the form.
83- _this . $cropButton . on ( 'click' , function ( event ) {
84- event . preventDefault ( ) ;
85-
86- // Validate the caption field and make sure we are good to proceed.
87- _this . validateCaption ( _this . engageImageCrop , image ) ;
88- } ) ;
89- }
90- // Show user an error if they upload an image that is too small.
91- else {
92- if ( ! $errorMsg . length ) {
93- var $error = $ ( "<div class='messages error'>Please upload a larger photo. Min. size is 480px by 480px.</div>" ) ;
94- $error . insertAfter ( _this . $uploadButton . parent ( ) ) ;
95- }
96- }
97- } ;
60+ _this . loadImage ( imageUrl ) ;
9861 } ;
9962 }
10063 else {
10164 fr . readAsDataURL ( files [ 0 ] ) ;
10265
10366 fr . onloadend = function ( ) {
10467 var result = this . result ;
105- var image = new Image ( ) ;
106- image . src = result ;
107-
108- image . onload = function ( ) {
109- var image = this ;
110- var isValid = _this . validImage ( this ) ;
111- var $errorMsg = _this . $uploadButton . parent ( ) . siblings ( ".error" ) ;
112-
113- if ( isValid ) {
114- // Remove any previously added error message.
115- if ( $errorMsg . length ) {
116- $errorMsg . remove ( ) ;
117- }
118-
119- // Open the modal.
120- _this . openModal ( ) ;
121-
122- // Add the preview image to the modal
123- _this . previewImage ( image , _this . $imagePreview ) ;
124-
125- //Unbind before rebind so it doesnt fire twice.
126- _this . $cropButton . off ( "click" ) ;
127-
128- // When the user submits the modal form, send crop values to the form.
129- _this . $cropButton . on ( 'click' , function ( event ) {
130- event . preventDefault ( ) ;
131-
132- // Validate the caption field and make sure we are good to proceed.
133- _this . validateCaption ( _this . engageImageCrop , image ) ;
134- } ) ;
135- }
136- // Show user an error if they upload an image that is too small.
137- else {
138- if ( ! $errorMsg . length ) {
139- var $error = $ ( "<div class='messages error'>Please upload a larger photo. Min. size is 480px by 480px.</div>" ) ;
140- $error . insertAfter ( _this . $uploadButton . parent ( ) ) ;
141- }
142- }
143- } ;
68+ _this . loadImage ( result ) ;
14469 } ;
14570 }
14671 } ) ;
@@ -156,7 +81,6 @@ define(function(require) {
15681 } ) ;
15782 } ;
15883
159-
16084 /**
16185 * Validates the caption field in the modal.
16286 * @param {Function } callback If validation passes, then lets rock on!
@@ -172,7 +96,7 @@ define(function(require) {
17296 callback ( _this , image ) ;
17397 }
17498 } ) ;
175- }
99+ } ;
176100
177101
178102 /**
@@ -200,7 +124,7 @@ define(function(require) {
200124
201125 // Close the modal.
202126 Modal . close ( ) ;
203- }
127+ } ;
204128
205129
206130 /**
@@ -236,7 +160,7 @@ define(function(require) {
236160 ImageUploadBeta . prototype . createEditButton = function ( ) {
237161 var _this = this ;
238162
239- if ( ! $ ( ".reportback__submissions" ) . find ( ".button--edit" ) . length ) {
163+ if ( ! this . $reportbackSubmissions . find ( ".button--edit" ) . length ) {
240164 var $editButton = $ ( "<span class='button button--edit -tertiary'>edit photo</span>" ) ;
241165 this . $uploadButton . parent ( ) . hide ( ) . before ( $editButton ) ;
242166
@@ -333,6 +257,48 @@ define(function(require) {
333257 } ) ;
334258 } ;
335259
260+ ImageUploadBeta . prototype . loadImage = function ( imageUrl ) {
261+ var _this = this ;
262+ var image = new Image ( ) ;
263+ image . src = imageUrl ;
264+
265+ image . onload = function ( ) {
266+ var isValid = _this . validImage ( this ) ;
267+ var $errorMsg = _this . $uploadButton . parent ( ) . siblings ( ".error" ) ;
268+
269+ if ( isValid ) {
270+ // Remove any previously added error message.
271+ if ( $errorMsg . length ) {
272+ $errorMsg . remove ( ) ;
273+ }
274+
275+ // Open the modal.
276+ _this . openModal ( ) ;
277+
278+ // Add the preview image to the modal
279+ _this . previewImage ( image , _this . $imagePreview ) ;
280+
281+ //Unbind before rebind so it doesnt fire twice.
282+ _this . $cropButton . off ( "click" ) ;
283+
284+ // When the user submits the modal form, send crop values to the form.
285+ _this . $cropButton . on ( "click" , function ( event ) {
286+ event . preventDefault ( ) ;
287+
288+ // Validate the caption field and make sure we are good to proceed.
289+ _this . validateCaption ( _this . engageImageCrop , image ) ;
290+ } ) ;
291+ }
292+ // Show user an error if they upload an image that is too small.
293+ else {
294+ if ( ! $errorMsg . length ) {
295+ var $error = $ ( "<div class='messages error'>Please upload a larger photo. Min. size is 480px by 480px.</div>" ) ;
296+ _this . $reportbackSubmissions . prepend ( $error ) ;
297+ }
298+ }
299+ }
300+ } ;
301+
336302 /**
337303 * This function strips out EXIF data from a file and recreates a
338304 * FileReader Blob that we use to create the image object that is used
@@ -407,7 +373,7 @@ define(function(require) {
407373 // robust fix for this that maybe only runs through the EXIF
408374 // stuff if it is a jpeg in the first place.
409375 else {
410- generatedBlob = new Blob ( [ dataView ] , { type : ' image/png' } ) ;
376+ generatedBlob = new Blob ( [ dataView ] , { type : " image/png" } ) ;
411377 }
412378 return generatedBlob ;
413379 } ;
0 commit comments