/**
* The functions necessary for editing images.
*
* @since 2.9.0
* @output wp-admin/js/image-edit.js
*/
/* global ajaxurl, confirm */
(function($) {
var __ = wp.i18n.__;
/**
* Contains all the methods to initialize and control the image editor.
*
* @namespace imageEdit
*/
var imageEdit = window.imageEdit = {
iasapi : {},
hold : {},
postid : '',
_view : false,
/**
* Enable crop tool.
*/
toggleCropTool: function( postid, nonce, cropButton ) {
var img = $( '#image-preview-' + postid ),
selection = this.iasapi.getSelection();
imageEdit.toggleControls( cropButton );
var $el = $( cropButton );
var state = ( $el.attr( 'aria-expanded' ) === 'true' ) ? 'true' : 'false';
// Crop tools have been closed.
if ( 'false' === state ) {
// Cancel selection, but do not unset inputs.
this.iasapi.cancelSelection();
imageEdit.setDisabled($('.imgedit-crop-clear'), 0);
} else {
imageEdit.setDisabled($('.imgedit-crop-clear'), 1);
// Get values from inputs to restore previous selection.
var startX = ( $( '#imgedit-start-x-' + postid ).val() ) ? $('#imgedit-start-x-' + postid).val() : 0;
var startY = ( $( '#imgedit-start-y-' + postid ).val() ) ? $('#imgedit-start-y-' + postid).val() : 0;
var width = ( $( '#imgedit-sel-width-' + postid ).val() ) ? $('#imgedit-sel-width-' + postid).val() : img.innerWidth();
var height = ( $( '#imgedit-sel-height-' + postid ).val() ) ? $('#imgedit-sel-height-' + postid).val() : img.innerHeight();
// Ensure selection is available, otherwise reset to full image.
if ( isNaN( selection.x1 ) ) {
this.setCropSelection( postid, { 'x1': startX, 'y1': startY, 'x2': width, 'y2': height, 'width': width, 'height': height } );
selection = this.iasapi.getSelection();
}
// If we don't already have a selection, select the entire image.
if ( 0 === selection.x1 && 0 === selection.y1 && 0 === selection.x2 && 0 === selection.y2 ) {
this.iasapi.setSelection( 0, 0, img.innerWidth(), img.innerHeight(), true );
this.iasapi.setOptions( { show: true } );
this.iasapi.update();
} else {
this.iasapi.setSelection( startX, startY, width, height, true );
this.iasapi.setOptions( { show: true } );
this.iasapi.update();
}
}
},
/**
* Handle crop tool clicks.
*/
handleCropToolClick: function( postid, nonce, cropButton ) {
if ( cropButton.classList.contains( 'imgedit-crop-clear' ) ) {
this.iasapi.cancelSelection();
imageEdit.setDisabled($('.imgedit-crop-apply'), 0);
$('#imgedit-sel-width-' + postid).val('');
$('#imgedit-sel-height-' + postid).val('');
$('#imgedit-start-x-' + postid).val('0');
$('#imgedit-start-y-' + postid).val('0');
$('#imgedit-selection-' + postid).val('');
} else {
// Otherwise, perform the crop.
imageEdit.crop( postid, nonce , cropButton );
}
},
/**
* Converts a value to an integer.
*
* @since 2.9.0
*
* @memberof imageEdit
*
* @param {number} f The float value that should be converted.
*
* @return {number} The integer representation from the float value.
*/
intval : function(f) {
/*
* Bitwise OR operator: one of the obscure ways to truncate floating point figures,
* worth reminding JavaScript doesn't have a distinct "integer" type.
*/
return f | 0;
},
/**
* Adds the disabled attribute and class to a single form element or a field set.
*
* @since 2.9.0
*
* @memberof imageEdit
*
* @param {jQuery} el The element that should be modified.
* @param {boolean|number} s The state for the element. If set to true
* the element is disabled,
* otherwise the element is enabled.
* The function is sometimes called with a 0 or 1
* instead of true or false.
*
* @return {void}
*/
setDisabled : function( el, s ) {
/*
* `el` can be a single form element or a fieldset. Before #28864, the disabled state on
* some text fields was handled targeting $('input', el). Now we need to handle the