@@ -44,21 +44,47 @@ define(function(require) {
4444 return ;
4545 }
4646
47- // Open the modal.
48- Modal . open ( _this . $cropModal ,
49- {
50- animated : false ,
51- }
52- ) ;
53-
54- // Add the preview image to the modal
55- _this . previewImage ( files , _this . $imagePreview ) ;
56-
57- // Make sure submit button is enabled when the modal opens.
58- var $submitButton = _this . $cropModal . find ( ".-done" ) ;
59-
60- if ( $submitButton . prop ( "disabled" ) ) {
61- $submitButton . prop ( "disabled" , false ) ;
47+ if ( / ^ i m a g e / . test ( files [ 0 ] . type ) ) {
48+ var reader = new FileReader ( ) ;
49+ reader . readAsDataURL ( files [ 0 ] ) ;
50+
51+ reader . onloadend = function ( ) {
52+ var result = this . result ;
53+
54+ var isValid = _this . validImage ( result ) ;
55+ var $errorMsg = _this . $uploadButton . parent ( ) . siblings ( ".error" ) ;
56+
57+ if ( isValid ) {
58+ // Remove any previously added error message.
59+ if ( $errorMsg . length ) {
60+ $errorMsg . remove ( ) ;
61+ }
62+
63+ // Open the modal.
64+ Modal . open ( _this . $cropModal ,
65+ {
66+ animated : false ,
67+ }
68+ ) ;
69+
70+ // Add the preview image to the modal
71+ _this . previewImage ( result , _this . $imagePreview ) ;
72+
73+ // Make sure submit button is enabled when the modal opens.
74+ var $submitButton = _this . $cropModal . find ( ".-done" ) ;
75+
76+ if ( $submitButton . prop ( "disabled" ) ) {
77+ $submitButton . prop ( "disabled" , false ) ;
78+ }
79+ }
80+ // Show user an error if they upload an image that is too small.
81+ else {
82+ if ( ! $errorMsg . length ) {
83+ var $error = $ ( "<div class='messages error'>Please upload a larger photo. Min. size is 480px by 480px.</div>" ) ;
84+ $error . insertAfter ( _this . $uploadButton . parent ( ) ) ;
85+ }
86+ }
87+ } ;
6288 }
6389 } ) ;
6490
@@ -73,6 +99,16 @@ define(function(require) {
7399 } ) ;
74100 } ;
75101
102+ /**
103+ * Validates image size.
104+ */
105+ ImageUploadBeta . prototype . validImage = function ( fileReaderResult ) {
106+ var image = new Image ( ) ;
107+ image . src = fileReaderResult ;
108+
109+ return ( image . width > 480 && image . height > 480 ) ;
110+ } ;
111+
76112 /**
77113 * Resizes an image so that the dimensions of the scaled image.
78114 * are never greater than it's container's maximum width (height of the container is variable).
@@ -103,42 +139,35 @@ define(function(require) {
103139 * Creates a preview image of an uploaded file and adds it to a container.
104140 * If cropping is enabled, it initiates the cropping script.
105141 *
106- * @param {jQuery } files An object representing a file .
107- * @param {jQuery } $imageContainer The DOM element to put the preview image in.
142+ * @param {jQuery } fileReaderResult The result property of a file reader object.
143+ * @param {jQuery } $imageContainer The DOM element to put the preview image in.
108144 */
109- ImageUploadBeta . prototype . previewImage = function ( files , $imageContainer ) {
145+ ImageUploadBeta . prototype . previewImage = function ( fileReaderResult , $imageContainer ) {
146+ var _this = this ;
147+
110148 var $preview = $ ( "<img class='preview' src=''>" ) ;
111149 $imageContainer . html ( $preview ) ;
112150
113- if ( / ^ i m a g e / . test ( files [ 0 ] . type ) ) {
114- var reader = new FileReader ( ) ;
115- reader . readAsDataURL ( files [ 0 ] ) ;
116- var _this = this ;
117-
118- reader . onloadend = function ( ) {
119- var result = this . result ;
120- $preview . attr ( "src" , result ) . load ( function ( ) {
121- var $this = $ ( this ) ;
151+ $preview . attr ( "src" , fileReaderResult ) . load ( function ( ) {
152+ var $this = $ ( this ) ;
122153
123- _this . resizeImage ( $this , $imageContainer ) ;
154+ _this . resizeImage ( $this , $imageContainer ) ;
124155
125- // Initiate cropping if it is enabled.
126- if ( _this . cropEnabled ) {
127- _this . cropInit ( result ) ;
128- }
129- } ) ;
130- } ;
131- }
156+ // Initiate cropping if it is enabled.
157+ if ( _this . cropEnabled ) {
158+ _this . cropInit ( fileReaderResult ) ;
159+ }
160+ } ) ;
132161 } ;
133162
134163 /**
135164 * Initiates cropping on a file.
136165 *
137- * @param {jQuery } result The result property of a file reader object.
166+ * @param {jQuery } fileReaderResult The result property of a file reader object.
138167 */
139- ImageUploadBeta . prototype . cropInit = function ( result ) {
168+ ImageUploadBeta . prototype . cropInit = function ( fileReaderResult ) {
140169 var originalImage = new Image ( ) ;
141- originalImage . src = result ;
170+ originalImage . src = fileReaderResult ;
142171
143172 ImageCrop . dsCropperInit ( originalImage ) ;
144173 } ;
0 commit comments