1 /* global imageEditL10n, ajaxurl, confirm */ |
|
2 /** |
1 /** |
3 * @summary The functions necessary for editing images. |
2 * The functions necessary for editing images. |
4 * |
3 * |
5 * @since 2.9.0 |
4 * @since 2.9.0 |
|
5 * @output wp-admin/js/image-edit.js |
6 */ |
6 */ |
|
7 |
|
8 /* global imageEditL10n, ajaxurl, confirm */ |
7 |
9 |
8 (function($) { |
10 (function($) { |
9 |
11 |
10 /** |
12 /** |
11 * Contains all the methods to initialise and control the image editor. |
13 * Contains all the methods to initialise and control the image editor. |
17 hold : {}, |
19 hold : {}, |
18 postid : '', |
20 postid : '', |
19 _view : false, |
21 _view : false, |
20 |
22 |
21 /** |
23 /** |
22 * @summary Converts a value to an integer. |
24 * Handle crop tool clicks. |
|
25 */ |
|
26 handleCropToolClick: function( postid, nonce, cropButton ) { |
|
27 var img = $( '#image-preview-' + postid ), |
|
28 selection = this.iasapi.getSelection(); |
|
29 |
|
30 // Ensure selection is available, otherwise reset to full image. |
|
31 if ( isNaN( selection.x1 ) ) { |
|
32 this.setCropSelection( postid, { 'x1': 0, 'y1': 0, 'x2': img.innerWidth(), 'y2': img.innerHeight(), 'width': img.innerWidth(), 'height': img.innerHeight() } ); |
|
33 selection = this.iasapi.getSelection(); |
|
34 } |
|
35 |
|
36 // If we don't already have a selection, select the entire image. |
|
37 if ( 0 === selection.x1 && 0 === selection.y1 && 0 === selection.x2 && 0 === selection.y2 ) { |
|
38 this.iasapi.setSelection( 0, 0, img.innerWidth(), img.innerHeight(), true ); |
|
39 this.iasapi.setOptions( { show: true } ); |
|
40 this.iasapi.update(); |
|
41 } else { |
|
42 |
|
43 // Otherwise, perform the crop. |
|
44 imageEdit.crop( postid, nonce , cropButton ); |
|
45 } |
|
46 }, |
|
47 |
|
48 /** |
|
49 * Converts a value to an integer. |
23 * |
50 * |
24 * @memberof imageEdit |
51 * @memberof imageEdit |
25 * @since 2.9.0 |
52 * @since 2.9.0 |
26 * |
53 * |
27 * @param {number} f The float value that should be converted. |
54 * @param {number} f The float value that should be converted. |
166 getTarget : function(postid) { |
192 getTarget : function(postid) { |
167 return $('input[name="imgedit-target-' + postid + '"]:checked', '#imgedit-save-target-' + postid).val() || 'full'; |
193 return $('input[name="imgedit-target-' + postid + '"]:checked', '#imgedit-save-target-' + postid).val() || 'full'; |
168 }, |
194 }, |
169 |
195 |
170 /** |
196 /** |
171 * @summary Recalculates the height or width and keeps the original aspect ratio. |
197 * Recalculates the height or width and keeps the original aspect ratio. |
172 * |
198 * |
173 * If the original image size is exceeded a red exclamation mark is shown. |
199 * If the original image size is exceeded a red exclamation mark is shown. |
174 * |
200 * |
175 * @memberof imageEdit |
201 * @memberof imageEdit |
176 * @since 2.9.0 |
202 * @since 2.9.0 |
349 max1 = Math.max( t.hold.w, t.hold.h ); |
375 max1 = Math.max( t.hold.w, t.hold.h ); |
350 max2 = Math.max( $(img).width(), $(img).height() ); |
376 max2 = Math.max( $(img).width(), $(img).height() ); |
351 t.hold.sizer = max1 > max2 ? max2 / max1 : 1; |
377 t.hold.sizer = max1 > max2 ? max2 / max1 : 1; |
352 |
378 |
353 t.initCrop(postid, img, parent); |
379 t.initCrop(postid, img, parent); |
354 t.setCropSelection(postid, 0); |
|
355 |
380 |
356 if ( (typeof callback !== 'undefined') && callback !== null ) { |
381 if ( (typeof callback !== 'undefined') && callback !== null ) { |
357 callback(); |
382 callback(); |
358 } |
383 } |
359 |
384 |
370 t.toggleEditor(postid, 0); |
395 t.toggleEditor(postid, 0); |
371 }) |
396 }) |
372 .attr('src', ajaxurl + '?' + $.param(data)); |
397 .attr('src', ajaxurl + '?' + $.param(data)); |
373 }, |
398 }, |
374 /** |
399 /** |
375 * @summary Performs an image edit action. |
400 * Performs an image edit action. |
376 * |
401 * |
377 * @memberof imageEdit |
402 * @memberof imageEdit |
378 * @since 2.9.0 |
403 * @since 2.9.0 |
379 * |
404 * |
380 * @param {number} postid The post id. |
405 * @param {number} postid The post id. |
577 if ( 'undefined' === typeof this.hold.sizer ) { |
602 if ( 'undefined' === typeof this.hold.sizer ) { |
578 this.init( postid ); |
603 this.init( postid ); |
579 } |
604 } |
580 |
605 |
581 this.initCrop(postid, img, parent); |
606 this.initCrop(postid, img, parent); |
582 this.setCropSelection(postid, 0); |
607 this.setCropSelection( postid, { 'x1': 0, 'y1': 0, 'x2': 0, 'y2': 0, 'width': img.innerWidth(), 'height': img.innerHeight() } ); |
|
608 |
583 this.toggleEditor(postid, 0); |
609 this.toggleEditor(postid, 0); |
584 // Editor is ready, move focus to the first focusable element. |
610 // Editor is ready, move focus to the first focusable element. |
585 $( '.imgedit-wrap .imgedit-help-toggle' ).eq( 0 ).focus(); |
611 $( '.imgedit-wrap .imgedit-help-toggle' ).eq( 0 ).focus(); |
586 }, |
612 }, |
587 |
613 |
588 /** |
614 /** |
589 * @summary Initializes the cropping tool. |
615 * Initializes the cropping tool. |
590 * |
616 * |
591 * @memberof imageEdit |
617 * @memberof imageEdit |
592 * @since 2.9.0 |
618 * @since 2.9.0 |
593 * |
619 * |
594 * @param {number} postid The post id. |
620 * @param {number} postid The post id. |
610 keys: true, |
636 keys: true, |
611 minWidth: 3, |
637 minWidth: 3, |
612 minHeight: 3, |
638 minHeight: 3, |
613 |
639 |
614 /** |
640 /** |
615 * @summary Sets the CSS styles and binds events for locking the aspect ratio. |
641 * Sets the CSS styles and binds events for locking the aspect ratio. |
|
642 * |
|
643 * @ignore |
616 * |
644 * |
617 * @param {jQuery} img The preview image. |
645 * @param {jQuery} img The preview image. |
618 */ |
646 */ |
619 onInit: function( img ) { |
647 onInit: function( img ) { |
620 // Ensure that the imgAreaSelect wrapper elements are position:absolute. |
648 // Ensure that the imgAreaSelect wrapper elements are position:absolute. |
621 // (even if we're in a position:fixed modal) |
649 // (even if we're in a position:fixed modal) |
622 $img = $( img ); |
650 $img = $( img ); |
623 $img.next().css( 'position', 'absolute' ) |
651 $img.next().css( 'position', 'absolute' ) |
624 .nextAll( '.imgareaselect-outer' ).css( 'position', 'absolute' ); |
652 .nextAll( '.imgareaselect-outer' ).css( 'position', 'absolute' ); |
625 /** |
653 /** |
626 * @summary Binds mouse down event to the cropping container. |
654 * Binds mouse down event to the cropping container. |
627 * |
655 * |
628 * @returns {void} |
656 * @returns {void} |
629 */ |
657 */ |
630 parent.children().on( 'mousedown, touchstart', function(e){ |
658 parent.children().on( 'mousedown, touchstart', function(e){ |
631 var ratio = false, sel, defRatio; |
659 var ratio = false, sel, defRatio; |
641 }); |
669 }); |
642 }); |
670 }); |
643 }, |
671 }, |
644 |
672 |
645 /** |
673 /** |
646 * @summary Event triggered when starting a selection. |
674 * Event triggered when starting a selection. |
|
675 * |
|
676 * @ignore |
647 * |
677 * |
648 * @returns {void} |
678 * @returns {void} |
649 */ |
679 */ |
650 onSelectStart: function() { |
680 onSelectStart: function() { |
651 imageEdit.setDisabled($('#imgedit-crop-sel-' + postid), 1); |
681 imageEdit.setDisabled($('#imgedit-crop-sel-' + postid), 1); |
652 }, |
682 }, |
653 /** |
683 /** |
654 * @summary Event triggered when the selection is ended. |
684 * Event triggered when the selection is ended. |
|
685 * |
|
686 * @ignore |
655 * |
687 * |
656 * @param {object} img jQuery object representing the image. |
688 * @param {object} img jQuery object representing the image. |
657 * @param {object} c The selection. |
689 * @param {object} c The selection. |
658 * |
690 * |
659 * @returns {object} |
691 * @returns {object} |
661 onSelectEnd: function(img, c) { |
693 onSelectEnd: function(img, c) { |
662 imageEdit.setCropSelection(postid, c); |
694 imageEdit.setCropSelection(postid, c); |
663 }, |
695 }, |
664 |
696 |
665 /** |
697 /** |
666 * @summary Event triggered when the selection changes. |
698 * Event triggered when the selection changes. |
|
699 * |
|
700 * @ignore |
667 * |
701 * |
668 * @param {object} img jQuery object representing the image. |
702 * @param {object} img jQuery object representing the image. |
669 * @param {object} c The selection. |
703 * @param {object} c The selection. |
670 * |
704 * |
671 * @returns {void} |
705 * @returns {void} |
693 var sel; |
727 var sel; |
694 |
728 |
695 c = c || 0; |
729 c = c || 0; |
696 |
730 |
697 if ( !c || ( c.width < 3 && c.height < 3 ) ) { |
731 if ( !c || ( c.width < 3 && c.height < 3 ) ) { |
698 this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 0); |
732 this.setDisabled( $( '.imgedit-crop', '#imgedit-panel-' + postid ), 1 ); |
699 this.setDisabled($('#imgedit-crop-sel-' + postid), 0); |
733 this.setDisabled( $( '#imgedit-crop-sel-' + postid ), 1 ); |
700 $('#imgedit-sel-width-' + postid).val(''); |
734 $('#imgedit-sel-width-' + postid).val(''); |
701 $('#imgedit-sel-height-' + postid).val(''); |
735 $('#imgedit-sel-height-' + postid).val(''); |
702 $('#imgedit-selection-' + postid).val(''); |
736 $('#imgedit-selection-' + postid).val(''); |
703 return false; |
737 return false; |
704 } |
738 } |
827 |
861 |
828 this.addStep({ 'r': { 'r': angle, 'fw': this.hold.h, 'fh': this.hold.w }}, postid, nonce); |
862 this.addStep({ 'r': { 'r': angle, 'fw': this.hold.h, 'fh': this.hold.w }}, postid, nonce); |
829 }, |
863 }, |
830 |
864 |
831 /** |
865 /** |
832 * @summary Flips the image. |
866 * Flips the image. |
833 * |
867 * |
834 * @memberof imageEdit |
868 * @memberof imageEdit |
835 * @since 2.9.0 |
869 * @since 2.9.0 |
836 * |
870 * |
837 * @param {number} axis The axle the image is flipped on. |
871 * @param {number} axis The axle the image is flipped on. |
848 |
882 |
849 this.addStep({ 'f': { 'f': axis, 'fw': this.hold.w, 'fh': this.hold.h }}, postid, nonce); |
883 this.addStep({ 'f': { 'f': axis, 'fw': this.hold.w, 'fh': this.hold.h }}, postid, nonce); |
850 }, |
884 }, |
851 |
885 |
852 /** |
886 /** |
853 * @summary Crops the image. |
887 * Crops the image. |
854 * |
888 * |
855 * @memberof imageEdit |
889 * @memberof imageEdit |
856 * @since 2.9.0 |
890 * @since 2.9.0 |
857 * |
891 * |
858 * @param {number} postid The post id. |
892 * @param {number} postid The post id. |