| 1 |
const StockpackNoSearch = wp.media.View.extend( /** @lends wp.media.view.Toolbar.prototype */{ |
| 2 |
tagName: 'div', |
| 3 |
className: 'stockpack-no-search', |
| 4 |
template: wp.template( 'stockpack-no-search' ), |
| 5 |
|
| 6 |
defaults: { |
| 7 |
message: '', |
| 8 |
inspiration: [], |
| 9 |
}, |
| 10 |
|
| 11 |
initialize() { |
| 12 |
this.model = new Backbone.Model( this.defaults ); |
| 13 |
// If any of the `options` have a key from `defaults`, apply its |
| 14 |
// value to the `model` and remove it from the `options object. |
| 15 |
_.each( this.defaults, function( def, key ) { |
| 16 |
const value = this.options[ key ]; |
| 17 |
if ( _.isUndefined( value ) ) { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
this.model.set( key, value ); |
| 22 |
delete this.options[ key ]; |
| 23 |
}, this ); |
| 24 |
this.setInspiration(); |
| 25 |
this.listenTo( this.model, 'change', this.render ); |
| 26 |
}, |
| 27 |
|
| 28 |
setInspiration() { |
| 29 |
const random = this.getRandomInt( 1, 8 ); |
| 30 |
|
| 31 |
this.model.set( 'unsure', this.model.get( 'inspiration' ).unsure ); |
| 32 |
const inspiration = this.model.get( 'inspiration' ).random[ 'item_' + random ]; |
| 33 |
this.model.set( 'illustration', inspiration.image ); |
| 34 |
this.model.set( 'response', inspiration.message ); |
| 35 |
}, |
| 36 |
|
| 37 |
render() { |
| 38 |
wp.Backbone.View.prototype.render.apply( this, arguments ); |
| 39 |
return this; |
| 40 |
}, |
| 41 |
|
| 42 |
getRandomInt( min, max ) { |
| 43 |
min = Math.ceil( min ); |
| 44 |
max = Math.floor( max ); |
| 45 |
return Math.floor( Math.random() * ( max - min + 1 ) ) + min; |
| 46 |
}, |
| 47 |
|
| 48 |
dispose() { |
| 49 |
this.$el.remove(); |
| 50 |
if ( this.timer ) { |
| 51 |
this.timer.destroy(); |
| 52 |
} |
| 53 |
}, |
| 54 |
|
| 55 |
prepare() { |
| 56 |
return this.model.toJSON(); |
| 57 |
}, |
| 58 |
|
| 59 |
show() { |
| 60 |
this.$el.removeClass( 'hidden' ); |
| 61 |
}, |
| 62 |
hide() { |
| 63 |
this.$el.addClass( 'hidden' ); |
| 64 |
}, |
| 65 |
|
| 66 |
} ); |
| 67 |
|
| 68 |
export default StockpackNoSearch; |
| 69 |
|