@@ -112,16 +112,20 @@ jQuery('.cropper input[type="file"]').on('change', function() {
112112 file = files [ 0 ] ;
113113
114114 if ( / ^ i m a g e \/ \w + / . test ( file . type ) ) {
115- var oldId = null ;
115+ var oldId = null ;
116+ var replacedId = null ;
116117 if ( options ) {
117118 oldId = options . originalImageId ;
119+ replacedId = oldId ;
118120 if ( options . uploadedImageURL ) {
119121 URL . revokeObjectURL ( options . uploadedImageURL ) ;
120122 }
123+ if ( options . replacedImageId ) {
124+ replacedId = options . replacedImageId ;
125+ }
121126 }
122127
123128 if ( cropper ) {
124- // TODO remember deleted image for later delete with save
125129 cropperUI . destroy ( this ) ;
126130 }
127131
@@ -138,6 +142,7 @@ jQuery('.cropper input[type="file"]').on('change', function() {
138142
139143 var newId = 'uploadImage-' + cropperUI . nextId ( ) ;
140144 options = {
145+ replacedImageId : replacedId ,
141146 originalImageId : newId ,
142147 originalImageURL : url ,
143148 uploadedImageType : file . type ,
@@ -415,13 +420,42 @@ class CropperUI {
415420
416421 save ( domElement ) {
417422 var cropper = this . getCropper ( domElement ) ;
418- if ( cropper ) {
419- var options = this . getOptions ( domElement ) ;
423+ var options = this . getOptions ( domElement ) ;
424+ if ( cropper && options . changed ) {
425+ var data = {
426+ action : 'save' ,
427+ imageId : options . originalImageId ,
428+ data : cropper . getData ( ) ,
429+ containerData : cropper . getContainerData ( ) ,
430+ imageData : cropper . getImageData ( ) ,
431+ canvasData : cropper . getCanvasData ( ) ,
432+ cropBoxData : cropper . getCropBoxData ( ) ,
433+ order : [ ] ,
434+ replacedId : null ,
435+ blob : null ,
436+ }
437+ if ( options . replacedImageId ) {
438+ data . replacedId = options . replacedImageId ;
439+ }
420440 if ( options . uploadedImageURL ) {
421- // TODO Upload with data options
422- } else {
423- // Send data options for modifications
441+ // Upload with data options
442+ data . blob = options . file ;
443+ }
444+ var nav = this . getNav ( domElement ) ;
445+ if ( nav ) {
446+ nav . children ( ) . each ( function ( index ) {
447+ var link = jQuery ( this ) ;
448+ if ( link . data ( 'imgid' ) != 'newImg' ) {
449+ if ( link . find ( 'img' ) . attr ( 'src' ) != options . uploadedImageURL ) {
450+ data . order . push ( link . data ( 'imgid' ) ) ;
451+ } else {
452+ data . order . push ( 'NEW' ) ;
453+ }
454+ }
455+ } ) ;
424456 }
457+ // Send data options for modifications
458+ webApp . POST ( document . location , data , new SaveImageAjaxController ( options ) ) ;
425459 }
426460 }
427461
@@ -542,5 +576,23 @@ class DeleteImageModal extends ChangeConfirmModal {
542576 }
543577}
544578
579+ class SaveImageAjaxController extends WebAppDefaultAjaxController {
580+
581+ constructor ( options ) {
582+ super ( ) ;
583+ this . options = options ;
584+ }
585+
586+ done ( ajaxParams , data , textStatus , jqXHR ) {
587+ // Change nav and editor URL (replace/reset?), set changed to FALSE
588+ super . done ( ajaxParams , data , textStatus , jqXHR ) ;
589+ }
590+
591+ fail ( ajaxParams , jqXHR , textStatus , errorThrown ) {
592+ super . fail ( ajaxParams , jqXHR , textStatus , errorThrown ) ;
593+ // Show error message
594+ }
595+ }
596+
545597var cropperUI = new CropperUI ( ) ;
546598cropperUI . init ( ) ;
0 commit comments