-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathgenerated-images-container.js
92 lines (80 loc) · 2.14 KB
/
generated-images-container.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* eslint object-shorthand: 0 */
import Images from '../collections/images';
import GeneratedImage from './generated-image';
/**
* View to render out the generated images container.
*
* This uses the Images collection to make our API
* request, showing a loading state and then rendering
* the images.
*/
const GeneratedImagesContainer = wp.media.View.extend( {
el: '.generated-images',
/**
* Initialize the view.
*
* @param {Object} options Options passed to the view.
*/
initialize: function ( options ) {
this.collection = new Images();
this.prompt = options.prompt;
this.quality = options.quality;
this.size = options.size;
this.style = options.style;
this.listenTo( this.collection, 'reset', this.renderAll );
this.listenTo( this.collection, 'error', this.error );
this.collection.makeRequest(
this.prompt,
this.quality,
this.size,
this.style
);
this.render();
},
/**
* Render the view.
*/
render: function () {
this.$el.prev().find( 'button' ).prop( 'disabled', true );
this.$el.prev().find( '.error' ).text( '' );
this.$( 'ul' ).empty();
this.$( '.spinner' ).addClass( 'active' );
this.$( '.prompt-text' ).addClass( 'hidden' );
return this;
},
/**
* Render an individual image.
*
* @param {wp.media.View.GeneratedImage} image Individual image model.
*/
renderImage: function ( image ) {
const view = new GeneratedImage( {
model: image,
prompt: this.prompt,
} );
this.$( 'ul' ).append( view.render().el );
},
/**
* Render all images.
*/
renderAll: function () {
if ( this.collection.length < 1 ) {
this.error();
this.$el
.prev()
.find( '.error' )
.text( classifaiDalleData.errorText );
} else {
this.$( '.prompt-text' ).removeClass( 'hidden' );
this.$( '.prompt-text span' ).text( this.prompt );
this.$( '.spinner' ).removeClass( 'active' );
this.collection.each( this.renderImage, this );
this.$el.prev().find( 'button' ).prop( 'disabled', false );
}
},
error: function () {
this.$( '.spinner' ).removeClass( 'active' );
this.$el.prev().find( 'button' ).prop( 'disabled', false );
},
} );
export default GeneratedImagesContainer;