| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
const UiElement = require( './mmv.ui.js' ); |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class Dialog extends UiElement { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
constructor( $container, $openButton ) { |
| 29 |
super( $container ); |
| 30 |
|
| 31 |
|
| 32 |
this.isOpen = false; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
this.eventPrefix = ''; |
| 38 |
|
| 39 |
|
| 40 |
this.$openButton = $openButton; |
| 41 |
|
| 42 |
|
| 43 |
this.$dialog = $( '<div>' ) |
| 44 |
.addClass( 'mw-mmv-dialog' ); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
this.$downArrow = $( '<div>' ) |
| 51 |
.addClass( 'mw-mmv-dialog-down-arrow' ) |
| 52 |
.appendTo( this.$dialog ); |
| 53 |
|
| 54 |
this.initWarning(); |
| 55 |
|
| 56 |
this.$dialog.appendTo( this.$container ); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
initWarning() { |
| 65 |
this.$warning = $( '<div>' ) |
| 66 |
.addClass( 'mw-mmv-dialog-warning' ) |
| 67 |
.hide() |
| 68 |
.on( 'click', ( e ) => { |
| 69 |
|
| 70 |
e.stopPropagation(); |
| 71 |
} ) |
| 72 |
.appendTo( this.$dialog ); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
handleOpenCloseClick( openEvent, e ) { |
| 83 |
this.toggleDialog( e ); |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
toggleDialog( e ) { |
| 94 |
if ( this.isOpen ) { |
| 95 |
this.closeDialog( e ); |
| 96 |
} else { |
| 97 |
this.openDialog(); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
openDialog() { |
| 105 |
this.startListeningToOutsideClick(); |
| 106 |
this.$dialog.show(); |
| 107 |
this.isOpen = true; |
| 108 |
this.$openButton.addClass( 'mw-mmv-dialog-open' ); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
closeDialog() { |
| 115 |
this.stopListeningToOutsideClick(); |
| 116 |
this.$dialog.hide(); |
| 117 |
this.isOpen = false; |
| 118 |
this.$openButton.removeClass( 'mw-mmv-dialog-open' ); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
startListeningToOutsideClick() { |
| 125 |
this.outsideClickHandler = this.outsideClickHandler || ( ( e ) => { |
| 126 |
const $clickTarget = $( e.target ); |
| 127 |
|
| 128 |
|
| 129 |
if ( |
| 130 |
$clickTarget.closest( this.$dialog ).length || |
| 131 |
$clickTarget.closest( '.mw-mmv-next-image' ).length || |
| 132 |
$clickTarget.closest( '.mw-mmv-prev-image' ).length || |
| 133 |
e.which === 3 |
| 134 |
) { |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
this.closeDialog(); |
| 139 |
return false; |
| 140 |
} ); |
| 141 |
$( document ).on( `click.mmv.${ this.eventPrefix }`, this.outsideClickHandler ); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
stopListeningToOutsideClick() { |
| 148 |
$( document ).off( `click.mmv.${ this.eventPrefix }`, this.outsideClickHandler ); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
unattach() { |
| 155 |
super.unattach(); |
| 156 |
|
| 157 |
this.stopListeningToOutsideClick(); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
empty() { |
| 164 |
this.closeDialog(); |
| 165 |
this.clearWarning(); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
setWarning( content ) { |
| 175 |
this.$warning |
| 176 |
.empty() |
| 177 |
.append( content ) |
| 178 |
.show(); |
| 179 |
this.$dialog.addClass( 'mw-mmv-warning-visible' ); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
clearWarning() { |
| 186 |
this.$warning.hide(); |
| 187 |
this.$dialog.removeClass( 'mw-mmv-warning-visible' ); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
getImageWarnings( image ) { |
| 195 |
const warnings = []; |
| 196 |
|
| 197 |
if ( image.deletionReason ) { |
| 198 |
warnings.push( mw.message( 'multimediaviewer-reuse-warning-deletion' ).plain() ); |
| 199 |
|
| 200 |
return warnings; |
| 201 |
} |
| 202 |
|
| 203 |
if ( !image.license || image.license.needsAttribution() && !image.author && !image.attribution ) { |
| 204 |
warnings.push( mw.message( 'multimediaviewer-reuse-warning-noattribution' ).plain() ); |
| 205 |
} |
| 206 |
|
| 207 |
if ( image.license && !image.license.isFree() ) { |
| 208 |
warnings.push( mw.message( 'multimediaviewer-reuse-warning-nonfree' ).plain() ); |
| 209 |
} |
| 210 |
|
| 211 |
return warnings; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
showImageWarnings( image ) { |
| 218 |
const warnings = this.getImageWarnings( image ); |
| 219 |
|
| 220 |
if ( warnings.length > 0 ) { |
| 221 |
warnings.push( mw.message( 'multimediaviewer-reuse-warning-generic', image.descriptionUrl ).parse() ); |
| 222 |
this.setWarning( warnings.join( '<br />' ) ); |
| 223 |
} else { |
| 224 |
this.clearWarning(); |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
module.exports = Dialog; |