| 1 |
import StockpackTimer from './utils/stockpack-timer'; |
| 2 |
import StockpackRegister from './utils/stockpack-register'; |
| 3 |
import StockpackUpgrade from './utils/stockpack-upgrade'; |
| 4 |
|
| 5 |
const StockpackDialog = wp.media.View.extend( /** @lends wp.media.view.Toolbar.prototype */{ |
| 6 |
tagName: 'div', |
| 7 |
className: 'stockpack-dialog', |
| 8 |
template: wp.template( 'stockpack-dialog' ), |
| 9 |
|
| 10 |
events: { |
| 11 |
'click .ui-widget-overlay': 'close', |
| 12 |
}, |
| 13 |
|
| 14 |
defaults: { |
| 15 |
id: 'stockpack-dialog', |
| 16 |
title: '', |
| 17 |
message: '', |
| 18 |
link: '', |
| 19 |
error: '', |
| 20 |
iframe: '', |
| 21 |
state: '', |
| 22 |
external: '', |
| 23 |
externalUrl: '', |
| 24 |
directLicenseUrl: '', |
| 25 |
}, |
| 26 |
|
| 27 |
timer: null, |
| 28 |
register: null, |
| 29 |
upgrade: null, |
| 30 |
controller: null, |
| 31 |
|
| 32 |
initialize() { |
| 33 |
if ( this.options.controller ) { |
| 34 |
this.controller = this.options.controller; |
| 35 |
delete this.options.controller; |
| 36 |
} |
| 37 |
|
| 38 |
this.model = new Backbone.Model( this.defaults ); |
| 39 |
// If any of the `options` have a key from `defaults`, apply its |
| 40 |
// value to the `model` and remove it from the `options object. |
| 41 |
_.each( this.defaults, function( def, key ) { |
| 42 |
const value = this.options[ key ]; |
| 43 |
if ( _.isUndefined( value ) ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
this.model.set( key, value ); |
| 48 |
delete this.options[ key ]; |
| 49 |
}, this ); |
| 50 |
|
| 51 |
this.insertElement(); |
| 52 |
|
| 53 |
this.listenTo( this.model, 'change', this.render ); |
| 54 |
this.createDialog(); |
| 55 |
this.maybeCreateTimer(); |
| 56 |
this.maybeHookRegister(); |
| 57 |
this.maybeHookUpgrade(); |
| 58 |
}, |
| 59 |
|
| 60 |
maybeCreateTimer() { |
| 61 |
if ( this.$el.find( '.stockpack-timer' ).length ) { |
| 62 |
this.timer = new StockpackTimer( this.$el.find( '.stockpack-timer' ) ); |
| 63 |
} |
| 64 |
}, |
| 65 |
|
| 66 |
maybeHookRegister() { |
| 67 |
const $iframe = this.$el.find( 'iframe#stockpack-token-iframe' ); |
| 68 |
if ( $iframe.length > 0 ) { |
| 69 |
this.register = new StockpackRegister( $iframe.parent(), this.controller ); |
| 70 |
this.controller.on( 'token-updated', _.bind( this.startRetry, this ) ); |
| 71 |
} |
| 72 |
}, |
| 73 |
|
| 74 |
maybeHookUpgrade() { |
| 75 |
const $iframe = this.$el.find( 'iframe#stockpack-upgrade-iframe' ); |
| 76 |
if ( $iframe.length > 0 ) { |
| 77 |
this.upgrade = new StockpackUpgrade( $iframe.parent(), this.controller ); |
| 78 |
this.controller.on( 'account-upgraded', _.bind( this.startRetry, this ) ); |
| 79 |
} |
| 80 |
}, |
| 81 |
|
| 82 |
startRetry() { |
| 83 |
this.controller.trigger( 'start-retry' ); |
| 84 |
this.close(); |
| 85 |
}, |
| 86 |
|
| 87 |
createDialog() { |
| 88 |
this.$el.data( 'initialized', 'true' ).dialog( { |
| 89 |
title: this.model.get( 'title' ), |
| 90 |
dialogClass: 'wp-dialog stockpack-dialog', |
| 91 |
autoOpen: false, |
| 92 |
draggable: false, |
| 93 |
width: 'auto', |
| 94 |
modal: true, |
| 95 |
resizable: false, |
| 96 |
closeOnEscape: true, |
| 97 |
position: { |
| 98 |
my: 'center', |
| 99 |
at: 'center', |
| 100 |
of: window, |
| 101 |
}, |
| 102 |
} ); |
| 103 |
}, |
| 104 |
|
| 105 |
setStatus( status ) { |
| 106 |
this.model.set( 'state', status ); |
| 107 |
this.render(); |
| 108 |
}, |
| 109 |
|
| 110 |
setExternalUrl( externalUrl ) { |
| 111 |
this.model.set( 'externalUrl', externalUrl ); |
| 112 |
this.render(); |
| 113 |
}, |
| 114 |
|
| 115 |
buttons( options ) { |
| 116 |
this.$el.dialog( 'option', 'buttons', options ); |
| 117 |
}, |
| 118 |
|
| 119 |
insertElement() { |
| 120 |
const $dialog = jQuery( '<div id="' + this.model.get( 'id' ) + '" data-title="' + this.model.get( 'title' ) + '"></div> ' ); |
| 121 |
jQuery( 'body' ).append( $dialog ); |
| 122 |
this.setElement( $dialog ); |
| 123 |
this.render(); |
| 124 |
}, |
| 125 |
|
| 126 |
dispose() { |
| 127 |
if ( this.$el.data( 'initialized' ) ) { |
| 128 |
this.$el.dialog( 'destroy' ); |
| 129 |
} |
| 130 |
this.$el.remove(); |
| 131 |
if ( this.timer ) { |
| 132 |
this.timer.destroy(); |
| 133 |
} |
| 134 |
if ( this.register ) { |
| 135 |
this.register.destroy(); |
| 136 |
} |
| 137 |
if ( this.upgrade ) { |
| 138 |
this.upgrade.destroy(); |
| 139 |
} |
| 140 |
}, |
| 141 |
|
| 142 |
prepare() { |
| 143 |
return this.model.toJSON(); |
| 144 |
}, |
| 145 |
|
| 146 |
close() { |
| 147 |
if ( this.$el.data( 'initialized' ) ) { |
| 148 |
this.$el.dialog( 'close' ); |
| 149 |
} |
| 150 |
this.dispose(); |
| 151 |
}, |
| 152 |
open() { |
| 153 |
this.$el.dialog( 'open' ); |
| 154 |
}, |
| 155 |
|
| 156 |
} ); |
| 157 |
|
| 158 |
export default StockpackDialog; |
| 159 |
|