PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / trunk
Secure Custom Fields vtrunk
6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / assets / src / js / _acf-field-image.js
secure-custom-fields / assets / src / js Last commit date
bindings 6 months ago commands 2 weeks ago pro 2 weeks ago _acf-compatibility.js 1 year ago _acf-condition-types.js 8 months ago _acf-condition.js 1 year ago _acf-conditions.js 1 year ago _acf-field-accordion.js 6 months ago _acf-field-button-group.js 8 months ago _acf-field-checkbox.js 1 month ago _acf-field-color-picker.js 8 months ago _acf-field-date-picker.js 1 month ago _acf-field-date-time-picker.js 1 month ago _acf-field-file.js 1 month ago _acf-field-google-map.js 6 months ago _acf-field-icon-picker.js 1 month ago _acf-field-image.js 1 month ago _acf-field-link.js 1 year ago _acf-field-oembed.js 1 month ago _acf-field-page-link.js 1 year ago _acf-field-post-object.js 1 year ago _acf-field-radio.js 1 month ago _acf-field-range.js 1 year ago _acf-field-relationship.js 1 month ago _acf-field-select.js 1 month ago _acf-field-tab.js 1 month ago _acf-field-taxonomy.js 8 months ago _acf-field-time-picker.js 1 month ago _acf-field-true-false.js 1 month ago _acf-field-url.js 1 year ago _acf-field-user.js 1 year ago _acf-field-wysiwyg.js 1 month ago _acf-field.js 1 year ago _acf-fields.js 1 year ago _acf-helpers.js 1 year ago _acf-hooks.js 1 year ago _acf-internal-post-type.js 8 months ago _acf-media.js 2 weeks ago _acf-modal.js 1 year ago _acf-model.js 1 year ago _acf-notice.js 8 months ago _acf-panel.js 1 year ago _acf-popup.js 8 months ago _acf-postbox.js 1 year ago _acf-screen.js 10 months ago _acf-select2.js 1 year ago _acf-tinymce.js 6 months ago _acf-tooltip.js 10 months ago _acf-unload.js 1 year ago _acf-validation.js 1 month ago _acf.js 7 months ago _browse-fields-modal.js 8 months ago _field-group-compatibility.js 1 year ago _field-group-conditions.js 1 year ago _field-group-field.js 4 months ago _field-group-fields.js 10 months ago _field-group-locations.js 1 year ago _field-group-settings.js 1 year ago _field-group.js 10 months ago acf-escaped-html-notice.js 1 year ago acf-field-group.js 1 year ago acf-input.js 1 year ago acf-internal-post-type.js 1 year ago acf.js 1 year ago
_acf-field-image.js
267 lines
1 ( function ( $ ) {
2 const Field = acf.Field.extend( {
3 type: 'image',
4
5 $control: function () {
6 return this.$( '.acf-image-uploader' );
7 },
8
9 $input: function () {
10 return this.$( 'input[type="hidden"]:first' );
11 },
12
13 events: {
14 'click a[data-name="add"]': 'onClickAdd',
15 'click a[data-name="edit"]': 'onClickEdit',
16 'click a[data-name="remove"]': 'onClickRemove',
17 'change input[type="file"]': 'onChange',
18 'keydown .image-wrap': 'onImageWrapKeydown',
19 },
20
21 initialize: function () {
22 // add attribute to form
23 if ( this.get( 'uploader' ) === 'basic' ) {
24 this.$el
25 .closest( 'form' )
26 .attr( 'enctype', 'multipart/form-data' );
27 }
28 },
29
30 validateAttachment: function ( attachment ) {
31 // Use WP attachment attributes when available.
32 if ( attachment && attachment.attributes ) {
33 attachment = attachment.attributes;
34 }
35
36 // Apply defaults.
37 attachment = acf.parseArgs( attachment, {
38 id: 0,
39 url: '',
40 alt: '',
41 title: '',
42 caption: '',
43 description: '',
44 width: 0,
45 height: 0,
46 } );
47
48 // Override with "preview size".
49 const size = acf.isget(
50 attachment,
51 'sizes',
52 this.get( 'preview_size' )
53 );
54 if ( size ) {
55 attachment.url = size.url;
56 attachment.width = size.width;
57 attachment.height = size.height;
58 }
59
60 // Return.
61 return attachment;
62 },
63
64 render: function ( attachment ) {
65 attachment = this.validateAttachment( attachment );
66
67 // Update value.
68 acf.val( this.$input(), String( attachment.id || '' ), true );
69
70 // Update DOM.
71 this.$( 'img' ).attr( {
72 src: attachment.url,
73 alt: attachment.alt,
74 } );
75 if ( attachment.id ) {
76 this.$control().addClass( 'has-value' );
77 const imageWrap = this.$( '.image-wrap' );
78 if ( imageWrap.length ) {
79 imageWrap.trigger( 'focus' );
80 }
81 } else {
82 this.$control().removeClass( 'has-value' );
83 }
84 },
85
86 // create a new repeater row and render value
87 append: function ( attachment, parent ) {
88 // create function to find next available field within parent
89 const getNext = function ( field, parent ) {
90 // find existing file fields within parent
91 const fields = acf.getFields( {
92 key: field.get( 'key' ),
93 parent: parent.$el,
94 } );
95
96 // find the first field with no value
97 for ( let i = 0; i < fields.length; i++ ) {
98 if ( ! fields[ i ].val() ) {
99 return fields[ i ];
100 }
101 }
102
103 // return
104 return false;
105 };
106
107 // find existing file fields within parent
108 let field = getNext( this, parent );
109
110 // add new row if no available field
111 if ( ! field ) {
112 parent.$( '.acf-button:last' ).trigger( 'click' );
113 field = getNext( this, parent );
114 }
115
116 // render
117 if ( field ) {
118 acf.val(
119 field.$input(),
120 String( attachment.id || attachment.attributes?.id || '' )
121 );
122 field.render( attachment );
123 }
124 },
125
126 selectAttachment: function () {
127 // vars
128 const parent = this.parent();
129 const multiple = parent && parent.get( 'type' ) === 'repeater';
130
131 // new frame
132 const frame = acf.newMediaPopup( {
133 mode: 'select',
134 type: 'image',
135 title: acf.__( 'Select Image' ),
136 field: this.get( 'key' ),
137 multiple: multiple,
138 library: this.get( 'library' ),
139 allowedTypes: this.get( 'mime_types' ),
140 select: $.proxy( function ( attachment, i ) {
141 if ( i > 0 ) {
142 this.append( attachment, parent );
143 } else {
144 acf.val(
145 this.$input(),
146 String(
147 attachment.id || attachment.attributes?.id || ''
148 )
149 );
150 this.render( attachment );
151 }
152 }, this ),
153 } );
154 },
155
156 editAttachment: function ( attachment ) {
157 // vars
158 const val = this.val();
159
160 if ( val ) {
161 // popup
162 var frame = acf.newMediaPopup( {
163 mode: 'edit',
164 title: acf.__( 'Edit Image' ),
165 button: acf.__( 'Update Image' ),
166 attachment: val,
167 field: this.get( 'key' ),
168 select: $.proxy( function ( attachment ) {
169 acf.val(
170 this.$input(),
171 String(
172 attachment.id || attachment.attributes?.id || ''
173 )
174 );
175 this.render( attachment );
176 }, this ),
177 close: $.proxy( function () {
178 if ( 'edit-button' === attachment ) {
179 const edit = this.$el.find( 'a[data-name="edit"]' );
180 if ( edit.length ) {
181 edit.trigger( 'focus' );
182 }
183 } else {
184 const imageWrap = this.$( '.image-wrap' );
185 if ( imageWrap.length ) {
186 imageWrap.trigger( 'focus' );
187 }
188 }
189 }, this ),
190 } );
191 }
192 },
193
194 removeAttachment: function () {
195 acf.val( this.$input(), '' );
196 this.render( false );
197 },
198
199 onClickAdd: function ( e, $el ) {
200 this.selectAttachment();
201 },
202
203 onClickEdit: function ( e, $el ) {
204 this.editAttachment( 'edit-button' );
205 },
206
207 onClickRemove: function ( e, $el ) {
208 this.removeAttachment();
209 },
210
211 onChange: function ( e, $el ) {
212 const $hiddenInput = this.$input();
213
214 if ( ! $el.val() ) {
215 $hiddenInput.val( '' );
216 }
217
218 acf.getFileInputData( $el, function ( data ) {
219 $hiddenInput.val( $.param( data ) );
220 } );
221 },
222 setValue: function ( val ) {
223 val = val ? String( val ) : '';
224
225 if ( acf.val( this.$input(), val, true ) === false ) {
226 return;
227 }
228
229 if ( val ) {
230 if ( window.wp && wp.media && wp.media.attachment ) {
231 const attachment = wp.media.attachment( val );
232 attachment.fetch().then(
233 $.proxy( function () {
234 this.render( attachment );
235 }, this )
236 );
237 } else {
238 this.$control().addClass( 'has-value' );
239 }
240 } else {
241 this.render( false );
242 }
243 },
244 onImageWrapKeydown: function ( event, imageWrapElement ) {
245 // Check if Enter key was pressed (keycode 13)
246 if ( event.which === 13 ) {
247 // Check if the event target is the imageWrapElement itself
248 if ( event.target === imageWrapElement[ 0 ] ) {
249 // Prevent the default Enter key behavior
250 event.preventDefault();
251
252 // Check if the field has a value
253 if ( this.val() ) {
254 // Check if the uploader is NOT the basic uploader
255 if ( this.get( 'uploader' ) !== 'basic' ) {
256 // Open the edit attachment dialog
257 this.editAttachment();
258 }
259 }
260 }
261 }
262 },
263 } );
264
265 acf.registerFieldType( Field );
266 } )( jQuery );
267