| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
i |
| 6 |
|
| 7 |
|
| 8 |
( function () { |
| 9 |
const identity = ( data ) => data; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
he |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
$.fn.confirmable = function ( options ) { |
| 53 |
options = $.extend( true, {}, $.fn.confirmable.defaultOptions, options || {} ); |
| 54 |
|
| 55 |
if ( options.delegate === null ) { |
| 56 |
return this.on( options.events, ( e ) => { |
| 57 |
$.fn.confirmable.handler( e, options ); |
| 58 |
} ); |
| 59 |
} |
| 60 |
|
| 61 |
return this.on( options.events, options.delegate, ( e ) => { |
| 62 |
$.fn.confirmable.handler( e, options ); |
| 63 |
} ); |
| 64 |
}; |
| 65 |
|
| 66 |
$.fn.confirmable.handler = function ( event, options ) { |
| 67 |
const $element = $( event.target ); |
| 68 |
|
| 69 |
if ( $element.data( 'jquery-confirmable-button' ) ) { |
| 70 |
|
| 71 |
|
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
event.preventDefault(); |
| 78 |
|
| 79 |
const rtl = $element.css( 'direction' ) === 'rtl'; |
| 80 |
let positionOffscreen, positionRestore, sideMargin, elementSideMargin; |
| 81 |
if ( rtl ) { |
| 82 |
positionOffscreen = { position: 'absolute', right: '-9999px' }; |
| 83 |
positionRestore = { position: '', right: '' }; |
| 84 |
sideMargin = 'marginRight'; |
| 85 |
elementSideMargin = parseInt( $element.css( 'margin-right' ) ); |
| 86 |
} else { |
| 87 |
positionOffscreen = { position: 'absolute', left: '-9999px' }; |
| 88 |
positionRestore = { position: '', left: '' }; |
| 89 |
sideMargin = 'marginLeft'; |
| 90 |
elementSideMargin = parseInt( $element.css( 'margin-left' ) ); |
| 91 |
} |
| 92 |
|
| 93 |
$element.addClass( 'hidden' ); |
| 94 |
let $wrapper, $interface, interfaceWidth, elementWidth, elementPadding; |
| 95 |
|
| 96 |
if ( $element.hasClass( 'jquery-confirmable-element' ) ) { |
| 97 |
$wrapper = $element.closest( '.jquery-confirmable-wrapper' ); |
| 98 |
$interface = $wrapper.find( '.jquery-confirmable-interface' ); |
| 99 |
|
| 100 |
interfaceWidth = $interface.data( 'jquery-confirmable-width' ); |
| 101 |
elementWidth = $element.data( 'jquery-confirmable-width' ); |
| 102 |
elementPadding = $element.data( 'jquery-confirmable-padding' ); |
| 103 |
|
| 104 |
const $existingText = $interface.find( '.jquery-confirmable-text' ); |
| 105 |
$existingText.removeClass( 'hidden' ); |
| 106 |
} else { |
| 107 |
const $elementClone = $element.clone( true ); |
| 108 |
$element.addClass( 'jquery-confirmable-element' ); |
| 109 |
|
| 110 |
elementWidth = $element.width(); |
| 111 |
elementPadding = parseInt( $element.css( 'padding-left' ) ) + parseInt( $element.css( 'padding-right' ) ); |
| 112 |
$element.data( 'jquery-confirmable-width', elementWidth ); |
| 113 |
$element.data( 'jquery-confirmable-padding', elementPadding ); |
| 114 |
|
| 115 |
$wrapper = $( '<span>' ) |
| 116 |
.addClass( 'jquery-confirmable-wrapper' ); |
| 117 |
$element.wrap( $wrapper ); |
| 118 |
|
| 119 |
|
| 120 |
const $text = $( '<span>' ) |
| 121 |
.addClass( 'jquery-confirmable-text' ) |
| 122 |
.text( options.i18n.confirm ); |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
let $buttonYes = $elementClone.clone( true ) |
| 128 |
.addClass( 'jquery-confirmable-button jquery-confirmable-button-yes' ) |
| 129 |
.removeClass( 'hidden' ) |
| 130 |
.data( 'jquery-confirmable-button', true ) |
| 131 |
.text( options.i18n.yes ); |
| 132 |
if ( options.handler ) { |
| 133 |
$buttonYes.on( options.events, options.handler ); |
| 134 |
} |
| 135 |
if ( options.i18n.yesTitle ) { |
| 136 |
$buttonYes.attr( 'title', options.i18n.yesTitle ); |
| 137 |
} |
| 138 |
$buttonYes = options.buttonCallback( $buttonYes, 'yes' ); |
| 139 |
|
| 140 |
|
| 141 |
let $buttonNo = $elementClone.clone( false ) |
| 142 |
.addClass( 'jquery-confirmable-button jquery-confirmable-button-no' ) |
| 143 |
.removeClass( 'hidden' ) |
| 144 |
.data( 'jquery-confirmable-button', true ) |
| 145 |
.text( options.i18n.no ) |
| 146 |
.on( options.events, ( e ) => { |
| 147 |
$element |
| 148 |
.css( sideMargin, elementSideMargin ) |
| 149 |
.removeClass( 'hidden' ); |
| 150 |
$interface.css( 'width', 0 ); |
| 151 |
e.preventDefault(); |
| 152 |
} ); |
| 153 |
if ( options.i18n.noTitle ) { |
| 154 |
$buttonNo.attr( 'title', options.i18n.noTitle ); |
| 155 |
} else { |
| 156 |
$buttonNo.removeAttr( 'title' ); |
| 157 |
} |
| 158 |
$buttonNo = options.buttonCallback( $buttonNo, 'no' ); |
| 159 |
|
| 160 |
|
| 161 |
$elementClone.remove(); |
| 162 |
|
| 163 |
$interface = $( '<span>' ) |
| 164 |
.addClass( 'jquery-confirmable-interface' ) |
| 165 |
.append( $text, options.i18n.space, $buttonYes, options.i18n.space, $buttonNo ); |
| 166 |
$interface = options.wrapperCallback( $interface ); |
| 167 |
|
| 168 |
|
| 169 |
$interface.css( positionOffscreen ); |
| 170 |
|
| 171 |
$element.after( $interface ); |
| 172 |
interfaceWidth = $interface.width(); |
| 173 |
$interface |
| 174 |
.data( 'jquery-confirmable-width', interfaceWidth ) |
| 175 |
.css( positionRestore ) |
| 176 |
|
| 177 |
.css( 'width', 0 ); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
setTimeout( () => { |
| 183 |
$element.css( sideMargin, -elementWidth - elementPadding ); |
| 184 |
$interface |
| 185 |
.css( 'width', interfaceWidth ) |
| 186 |
.css( sideMargin, elementSideMargin ); |
| 187 |
}, 1 ); |
| 188 |
}; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
$.fn.confirmable.defaultOptions = { |
| 196 |
events: 'click', |
| 197 |
wrapperCallback: identity, |
| 198 |
buttonCallback: identity, |
| 199 |
handler: null, |
| 200 |
delegate: null, |
| 201 |
i18n: { |
| 202 |
space: ' ', |
| 203 |
confirm: 'Are you sure?', |
| 204 |
yes: 'Yes', |
| 205 |
no: 'No', |
| 206 |
yesTitle: undefined, |
| 207 |
noTitle: undefined |
| 208 |
} |
| 209 |
}; |
| 210 |
}() ); |