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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
var AttachmentDisplay = wp.media.view.Settings.AttachmentDisplay,
$ = jQuery,
ImageDetails;
/**
* wp.media.view.ImageDetails
*
* @memberOf wp.media.view
*
* @class
* @augments wp.media.view.Settings.AttachmentDisplay
* @augments wp.media.view.Settings
* @augments wp.media.View
* @augments wp.Backbone.View
* @augments Backbone.View
*/
ImageDetails = AttachmentDisplay.extend(/** @lends wp.media.view.ImageDetails.prototype */{
className: 'image-details',
template: wp.template('image-details'),
events: _.defaults( AttachmentDisplay.prototype.events, {
'click .edit-attachment': 'editAttachment',
'click .replace-attachment': 'replaceAttachment',
'click .advanced-toggle': 'onToggleAdvanced',
'change [data-setting="customWidth"]': 'onCustomSize',
'change [data-setting="customHeight"]': 'onCustomSize',
'keyup [data-setting="customWidth"]': 'onCustomSize',
'keyup [data-setting="customHeight"]': 'onCustomSize'
} ),
initialize: function() {
// Used in AttachmentDisplay.prototype.updateLinkTo.
this.options.attachment = this.model.attachment;
this.listenTo( this.model, 'change:url', this.updateUrl );
this.listenTo( this.model, 'change:link', this.toggleLinkSettings );
this.listenTo( this.model, 'change:size', this.toggleCustomSize );
AttachmentDisplay.prototype.initialize.apply( this, arguments );
},
prepare: function() {
var attachment = false;
if ( this.model.attachment ) {
attachment = this.model.attachment.toJSON();
}
return _.defaults({
model: this.model.toJSON(),
attachment: attachment
}, this.options );
},
render: function() {
var args = arguments;
if ( this.model.attachment && 'pending' === this.model.dfd.state() ) {
this.model.dfd
.done( _.bind( function() {
AttachmentDisplay.prototype.render.apply( this, args );
this.postRender();
}, this ) )
.fail( _.bind( function() {
this.model.attachment = false;
AttachmentDisplay.prototype.render.apply( this, args );
this.postRender();
}, this ) );
} else {
AttachmentDisplay.prototype.render.apply( this, arguments );
this.postRender();
}
return this;
},
postRender: function() {
setTimeout( _.bind( this.scrollToTop, this ), 10 );
this.toggleLinkSettings();
if ( window.getUserSetting( 'advImgDetails' ) === 'show' ) {
this.toggleAdvanced( true );
}
this.trigger( 'post-render' );
},
scrollToTop: function() {
this.$( '.embed-media-settings' ).scrollTop( 0 );
},
updateUrl: function() {
this.$( '.image img' ).attr( 'src', this.model.get( 'url' ) );
this.$( '.url' ).val( this.model.get( 'url' ) );
},
toggleLinkSettings: function() {
if ( this.model.get( 'link' ) === 'none' ) {
this.$( '.link-settings' ).addClass('hidden');
} else {
this.$( '.link-settings' ).removeClass('hidden');
}
},
toggleCustomSize: function() {
if ( this.model.get( 'size' ) !== 'custom' ) {
this.$( '.custom-size' ).addClass('hidden');
} else {
this.$( '.custom-size' ).removeClass('hidden');
}
},
onCustomSize: function( event ) {
var dimension = $( event.target ).data('setting'),
num = $( event.target ).val(),
value;
// Ignore bogus input.
if ( ! /^\d+/.test( num ) || parseInt( num, 10 ) < 1 ) {
event.preventDefault();
return;
}
if ( dimension === 'customWidth' ) {
value = Math.round( 1 / this.model.get( 'aspectRatio' ) * num );
this.model.set( 'customHeight', value, { silent: true } );
this.$( '[data-setting="customHeight"]' ).val( value );
} else {
value = Math.round( this.model.get( 'aspectRatio' ) * num );
this.model.set( 'customWidth', value, { silent: true } );
this.$( '[data-setting="customWidth"]' ).val( value );
}
},
onToggleAdvanced: function( event ) {
event.preventDefault();
this.toggleAdvanced();
},
toggleAdvanced: function( show ) {
var $advanced = this.$el.find( '.advanced-section' ),
mode;
if ( $advanced.hasClass('advanced-visible') || show === false ) {
$advanced.removeClass('advanced-visible');
$advanced.find('.advanced-settings').addClass('hidden');
mode = 'hide';
} else {
$advanced.addClass('advanced-visible');
$advanced.find('.advanced-settings').removeClass('hidden');
mode = 'show';
}
window.setUserSetting( 'advImgDetails', mode );
},
editAttachment: function( event ) {
var editState = this.controller.states.get( 'edit-image' );
if ( window.imageEdit && editState ) {
event.preventDefault();
editState.set( 'image', this.model.attachment );
this.controller.setState( 'edit-image' );
}
},
replaceAttachment: function( event ) {
event.preventDefault();
this.controller.setState( 'replace-image' );
}
});
module.exports = ImageDetails;
|