wp/wp-admin/js/image-edit.js
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
     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.
    35 		 */
    62 		 */
    36 		return f | 0;
    63 		return f | 0;
    37 	},
    64 	},
    38 
    65 
    39 	/**
    66 	/**
    40 	 * @summary Adds the disabled attribute and class to a single form element
    67 	 * Adds the disabled attribute and class to a single form element or a field set.
    41 	 *          or a field set.
       
    42 	 *
    68 	 *
    43 	 * @memberof imageEdit
    69 	 * @memberof imageEdit
    44 	 * @since    2.9.0
    70 	 * @since    2.9.0
    45 	 *
    71 	 *
    46 	 * @param {jQuery}         el The element that should be modified.
    72 	 * @param {jQuery}         el The element that should be modified.
    65 			el.addClass( 'disabled' ).prop( 'disabled', true );
    91 			el.addClass( 'disabled' ).prop( 'disabled', true );
    66 		}
    92 		}
    67 	},
    93 	},
    68 
    94 
    69 	/**
    95 	/**
    70 	 * @summary Initializes the image editor.
    96 	 * Initializes the image editor.
    71 	 *
    97 	 *
    72 	 * @memberof imageEdit
    98 	 * @memberof imageEdit
    73 	 * @since    2.9.0
    99 	 * @since    2.9.0
    74 	 *
   100 	 *
    75 	 * @param {number} postid The post id.
   101 	 * @param {number} postid The post id.
   108 			}
   134 			}
   109 		});
   135 		});
   110 	},
   136 	},
   111 
   137 
   112 	/**
   138 	/**
   113 	 * @summary Toggles the wait/load icon in the editor.
   139 	 * Toggles the wait/load icon in the editor.
   114 	 *
   140 	 *
   115 	 * @memberof imageEdit
   141 	 * @memberof imageEdit
   116 	 * @since    2.9.0
   142 	 * @since    2.9.0
   117 	 *
   143 	 *
   118 	 * @param {number} postid The post id.
   144 	 * @param {number} postid The post id.
   129 			wait.fadeOut('fast');
   155 			wait.fadeOut('fast');
   130 		}
   156 		}
   131 	},
   157 	},
   132 
   158 
   133 	/**
   159 	/**
   134 	 * @summary Shows or hides the image edit help box.
   160 	 * Shows or hides the image edit help box.
   135 	 *
   161 	 *
   136 	 * @memberof imageEdit
   162 	 * @memberof imageEdit
   137 	 * @since    2.9.0
   163 	 * @since    2.9.0
   138 	 *
   164 	 *
   139 	 * @param {HTMLElement} el The element to create the help window in.
   165 	 * @param {HTMLElement} el The element to create the help window in.
   148 
   174 
   149 		return false;
   175 		return false;
   150 	},
   176 	},
   151 
   177 
   152 	/**
   178 	/**
   153 	 * @summary Gets the value from the image edit target.
   179 	 * Gets the value from the image edit target.
   154 	 *
   180 	 *
   155 	 * The image edit target contains the image sizes where the (possible) changes
   181 	 * The image edit target contains the image sizes where the (possible) changes
   156 	 * have to be applied to.
   182 	 * have to be applied to.
   157 	 *
   183 	 *
   158 	 * @memberof imageEdit
   184 	 * @memberof imageEdit
   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
   204 			warn.css('visibility', 'hidden');
   230 			warn.css('visibility', 'hidden');
   205 		}
   231 		}
   206 	},
   232 	},
   207 
   233 
   208 	/**
   234 	/**
   209 	 * @summary Gets the selected aspect ratio.
   235 	 * Gets the selected aspect ratio.
   210 	 *
   236 	 *
   211 	 * @memberof imageEdit
   237 	 * @memberof imageEdit
   212 	 * @since    2.9.0
   238 	 * @since    2.9.0
   213 	 *
   239 	 *
   214 	 * @param {number} postid The post id.
   240 	 * @param {number} postid The post id.
   230 
   256 
   231 		return '1:1';
   257 		return '1:1';
   232 	},
   258 	},
   233 
   259 
   234 	/**
   260 	/**
   235 	 * @summary Removes the last action from the image edit history
   261 	 * Removes the last action from the image edit history.
   236 	 * The history consist of (edit) actions performed on the image.
   262 	 * The history consist of (edit) actions performed on the image.
   237 	 *
   263 	 *
   238 	 * @memberof imageEdit
   264 	 * @memberof imageEdit
   239 	 * @since    2.9.0
   265 	 * @since    2.9.0
   240 	 *
   266 	 *
   294 			return JSON.stringify(op);
   320 			return JSON.stringify(op);
   295 		}
   321 		}
   296 		return '';
   322 		return '';
   297 	},
   323 	},
   298 	/**
   324 	/**
   299 	 * @summary Binds the necessary events to the image.
   325 	 * Binds the necessary events to the image.
   300 	 *
   326 	 *
   301 	 * When the image source is reloaded the image will be reloaded.
   327 	 * When the image source is reloaded the image will be reloaded.
   302 	 *
   328 	 *
   303 	 * @memberof imageEdit
   329 	 * @memberof imageEdit
   304 	 * @since    2.9.0
   330 	 * @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.
   437 			}
   462 			}
   438 		});
   463 		});
   439 	},
   464 	},
   440 
   465 
   441 	/**
   466 	/**
   442 	 * @summary Stores the changes that are made to the image.
   467 	 * Stores the changes that are made to the image.
   443 	 *
   468 	 *
   444 	 * @memberof imageEdit
   469 	 * @memberof imageEdit
   445 	 * @since    2.9.0
   470 	 * @since    2.9.0
   446 	 *
   471 	 *
   447 	 * @param {number}  postid   The post id to get the image from the database.
   472 	 * @param {number}  postid   The post id to get the image from the database.
   502 			}
   527 			}
   503 		});
   528 		});
   504 	},
   529 	},
   505 
   530 
   506 	/**
   531 	/**
   507 	 * @summary Creates the image edit window.
   532 	 * Creates the image edit window.
   508 	 *
   533 	 *
   509 	 * @memberof imageEdit
   534 	 * @memberof imageEdit
   510 	 * @since    2.9.0
   535 	 * @since    2.9.0
   511 	 *
   536 	 *
   512 	 * @param {number} postid   The post id for the image.
   537 	 * @param {number} postid   The post id for the image.
   559 
   584 
   560 		return dfd;
   585 		return dfd;
   561 	},
   586 	},
   562 
   587 
   563 	/**
   588 	/**
   564 	 * @summary Initializes the cropping tool and sets a default cropping selection.
   589 	 * Initializes the cropping tool and sets a default cropping selection.
   565 	 *
   590 	 *
   566 	 * @memberof imageEdit
   591 	 * @memberof imageEdit
   567 	 * @since    2.9.0
   592 	 * @since    2.9.0
   568 	 *
   593 	 *
   569 	 * @param {number} postid The post id.
   594 	 * @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}
   677 			}
   711 			}
   678 		});
   712 		});
   679 	},
   713 	},
   680 
   714 
   681 	/**
   715 	/**
   682 	 * @summary Stores the current crop selection.
   716 	 * Stores the current crop selection.
   683 	 *
   717 	 *
   684 	 * @memberof imageEdit
   718 	 * @memberof imageEdit
   685 	 * @since    2.9.0
   719 	 * @since    2.9.0
   686 	 *
   720 	 *
   687 	 * @param {number} postid The post id.
   721 	 * @param {number} postid The post id.
   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 		}
   708 		$('#imgedit-selection-' + postid).val( JSON.stringify(sel) );
   742 		$('#imgedit-selection-' + postid).val( JSON.stringify(sel) );
   709 	},
   743 	},
   710 
   744 
   711 
   745 
   712 	/**
   746 	/**
   713 	 * @summary Closes the image editor.
   747 	 * Closes the image editor.
   714 	 *
   748 	 *
   715 	 * @memberof imageEdit
   749 	 * @memberof imageEdit
   716 	 * @since    2.9.0
   750 	 * @since    2.9.0
   717 	 *
   751 	 *
   718 	 * @param {number}  postid The post id.
   752 	 * @param {number}  postid The post id.
   749 
   783 
   750 
   784 
   751 	},
   785 	},
   752 
   786 
   753 	/**
   787 	/**
   754 	 * @summary Checks if the image edit history is saved.
   788 	 * Checks if the image edit history is saved.
   755 	 *
   789 	 *
   756 	 * @memberof imageEdit
   790 	 * @memberof imageEdit
   757 	 * @since    2.9.0
   791 	 * @since    2.9.0
   758 	 *
   792 	 *
   759 	 * @param {number} postid The post id.
   793 	 * @param {number} postid The post id.
   773 		}
   807 		}
   774 		return false;
   808 		return false;
   775 	},
   809 	},
   776 
   810 
   777 	/**
   811 	/**
   778 	 * @summary Adds an image edit action to the history.
   812 	 * Adds an image edit action to the history.
   779 	 *
   813 	 *
   780 	 * @memberof imageEdit
   814 	 * @memberof imageEdit
   781 	 * @since    2.9.0
   815 	 * @since    2.9.0
   782 	 *
   816 	 *
   783 	 * @param {object} op     The original position.
   817 	 * @param {object} op     The original position.
   806 			t.setDisabled($('#image-redo-' + postid), false);
   840 			t.setDisabled($('#image-redo-' + postid), false);
   807 		});
   841 		});
   808 	},
   842 	},
   809 
   843 
   810 	/**
   844 	/**
   811 	 * @summary Rotates the image.
   845 	 * Rotates the image.
   812 	 *
   846 	 *
   813 	 * @memberof imageEdit
   847 	 * @memberof imageEdit
   814 	 * @since    2.9.0
   848 	 * @since    2.9.0
   815 	 *
   849 	 *
   816 	 * @param {string} angle  The angle the image is rotated with.
   850 	 * @param {string} angle  The angle the image is rotated with.
   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.
   877 			this.addStep({ 'c': sel }, postid, nonce);
   911 			this.addStep({ 'c': sel }, postid, nonce);
   878 		}
   912 		}
   879 	},
   913 	},
   880 
   914 
   881 	/**
   915 	/**
   882 	 * @summary Undoes an image edit action.
   916 	 * Undoes an image edit action.
   883 	 *
   917 	 *
   884 	 * @memberof imageEdit
   918 	 * @memberof imageEdit
   885 	 * @since    2.9.0
   919 	 * @since    2.9.0
   886 	 *
   920 	 *
   887 	 * @param {number} postid   The post id.
   921 	 * @param {number} postid   The post id.
   940 			}
   974 			}
   941 		});
   975 		});
   942 	},
   976 	},
   943 
   977 
   944 	/**
   978 	/**
   945 	 * @summary Sets the selection for the height and width in pixels.
   979 	 * Sets the selection for the height and width in pixels.
   946 	 *
   980 	 *
   947 	 * @memberof imageEdit
   981 	 * @memberof imageEdit
   948 	 * @since    2.9.0
   982 	 * @since    2.9.0
   949 	 *
   983 	 *
   950 	 * @param {number} postid The post id.
   984 	 * @param {number} postid The post id.