bindings
9 months ago
commands
1 year ago
pro
9 months ago
_acf-compatibility.js
1 year ago
_acf-condition-types.js
9 months ago
_acf-condition.js
1 year ago
_acf-conditions.js
1 year ago
_acf-field-accordion.js
1 year ago
_acf-field-button-group.js
9 months ago
_acf-field-checkbox.js
9 months ago
_acf-field-color-picker.js
9 months ago
_acf-field-date-picker.js
1 year ago
_acf-field-date-time-picker.js
1 year ago
_acf-field-file.js
9 months ago
_acf-field-google-map.js
1 year ago
_acf-field-icon-picker.js
1 year ago
_acf-field-image.js
9 months ago
_acf-field-link.js
1 year ago
_acf-field-oembed.js
1 year ago
_acf-field-page-link.js
1 year ago
_acf-field-post-object.js
1 year ago
_acf-field-radio.js
9 months ago
_acf-field-range.js
1 year ago
_acf-field-relationship.js
1 year ago
_acf-field-select.js
1 year ago
_acf-field-tab.js
1 year ago
_acf-field-taxonomy.js
9 months ago
_acf-field-time-picker.js
1 year ago
_acf-field-true-false.js
1 year ago
_acf-field-url.js
1 year ago
_acf-field-user.js
1 year ago
_acf-field-wysiwyg.js
1 year 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
9 months ago
_acf-media.js
1 year ago
_acf-modal.js
1 year ago
_acf-model.js
1 year ago
_acf-notice.js
9 months ago
_acf-panel.js
1 year ago
_acf-popup.js
9 months ago
_acf-postbox.js
1 year ago
_acf-screen.js
1 year ago
_acf-select2.js
1 year ago
_acf-tinymce.js
9 months ago
_acf-tooltip.js
1 year ago
_acf-unload.js
1 year ago
_acf-validation.js
9 months ago
_acf.js
9 months ago
_browse-fields-modal.js
9 months ago
_field-group-compatibility.js
1 year ago
_field-group-conditions.js
1 year ago
_field-group-field.js
9 months ago
_field-group-fields.js
1 year ago
_field-group-locations.js
1 year ago
_field-group-settings.js
1 year ago
_field-group.js
1 year 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
227 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 DOM. |
| 68 | this.$( 'img' ).attr( { |
| 69 | src: attachment.url, |
| 70 | alt: attachment.alt, |
| 71 | } ); |
| 72 | if ( attachment.id ) { |
| 73 | this.val( attachment.id ); |
| 74 | this.$control().addClass( 'has-value' ); |
| 75 | const imageWrap = this.$( '.image-wrap' ); |
| 76 | if ( imageWrap.length ) { |
| 77 | imageWrap.trigger( 'focus' ); |
| 78 | } |
| 79 | } else { |
| 80 | this.val( '' ); |
| 81 | this.$control().removeClass( 'has-value' ); |
| 82 | } |
| 83 | }, |
| 84 | |
| 85 | // create a new repeater row and render value |
| 86 | append: function ( attachment, parent ) { |
| 87 | // create function to find next available field within parent |
| 88 | const getNext = function ( field, parent ) { |
| 89 | // find existing file fields within parent |
| 90 | const fields = acf.getFields( { |
| 91 | key: field.get( 'key' ), |
| 92 | parent: parent.$el, |
| 93 | } ); |
| 94 | |
| 95 | // find the first field with no value |
| 96 | for ( let i = 0; i < fields.length; i++ ) { |
| 97 | if ( ! fields[ i ].val() ) { |
| 98 | return fields[ i ]; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // return |
| 103 | return false; |
| 104 | }; |
| 105 | |
| 106 | // find existing file fields within parent |
| 107 | let field = getNext( this, parent ); |
| 108 | |
| 109 | // add new row if no available field |
| 110 | if ( ! field ) { |
| 111 | parent.$( '.acf-button:last' ).trigger( 'click' ); |
| 112 | field = getNext( this, parent ); |
| 113 | } |
| 114 | |
| 115 | // render |
| 116 | if ( field ) { |
| 117 | field.render( attachment ); |
| 118 | } |
| 119 | }, |
| 120 | |
| 121 | selectAttachment: function () { |
| 122 | // vars |
| 123 | const parent = this.parent(); |
| 124 | const multiple = parent && parent.get( 'type' ) === 'repeater'; |
| 125 | |
| 126 | // new frame |
| 127 | const frame = acf.newMediaPopup( { |
| 128 | mode: 'select', |
| 129 | type: 'image', |
| 130 | title: acf.__( 'Select Image' ), |
| 131 | field: this.get( 'key' ), |
| 132 | multiple: multiple, |
| 133 | library: this.get( 'library' ), |
| 134 | allowedTypes: this.get( 'mime_types' ), |
| 135 | select: $.proxy( function ( attachment, i ) { |
| 136 | if ( i > 0 ) { |
| 137 | this.append( attachment, parent ); |
| 138 | } else { |
| 139 | this.render( attachment ); |
| 140 | } |
| 141 | }, this ), |
| 142 | } ); |
| 143 | }, |
| 144 | |
| 145 | editAttachment: function ( attachment ) { |
| 146 | // vars |
| 147 | const val = this.val(); |
| 148 | |
| 149 | if ( val ) { |
| 150 | // popup |
| 151 | var frame = acf.newMediaPopup( { |
| 152 | mode: 'edit', |
| 153 | title: acf.__( 'Edit Image' ), |
| 154 | button: acf.__( 'Update Image' ), |
| 155 | attachment: val, |
| 156 | field: this.get( 'key' ), |
| 157 | select: $.proxy( function ( attachment ) { |
| 158 | this.render( attachment ); |
| 159 | }, this ), |
| 160 | close: $.proxy( function () { |
| 161 | if ( 'edit-button' === attachment ) { |
| 162 | const edit = this.$el.find( 'a[data-name="edit"]' ); |
| 163 | if ( edit.length ) { |
| 164 | edit.trigger( 'focus' ); |
| 165 | } |
| 166 | } else { |
| 167 | const imageWrap = this.$( '.image-wrap' ); |
| 168 | if ( imageWrap.length ) { |
| 169 | imageWrap.trigger( 'focus' ); |
| 170 | } |
| 171 | } |
| 172 | }, this ), |
| 173 | } ); |
| 174 | } |
| 175 | }, |
| 176 | |
| 177 | removeAttachment: function () { |
| 178 | this.render( false ); |
| 179 | }, |
| 180 | |
| 181 | onClickAdd: function ( e, $el ) { |
| 182 | this.selectAttachment(); |
| 183 | }, |
| 184 | |
| 185 | onClickEdit: function ( e, $el ) { |
| 186 | this.editAttachment( 'edit-button' ); |
| 187 | }, |
| 188 | |
| 189 | onClickRemove: function ( e, $el ) { |
| 190 | this.removeAttachment(); |
| 191 | }, |
| 192 | |
| 193 | onChange: function ( e, $el ) { |
| 194 | const $hiddenInput = this.$input(); |
| 195 | |
| 196 | if ( ! $el.val() ) { |
| 197 | $hiddenInput.val( '' ); |
| 198 | } |
| 199 | |
| 200 | acf.getFileInputData( $el, function ( data ) { |
| 201 | $hiddenInput.val( $.param( data ) ); |
| 202 | } ); |
| 203 | }, |
| 204 | onImageWrapKeydown: function ( event, imageWrapElement ) { |
| 205 | // Check if Enter key was pressed (keycode 13) |
| 206 | if ( event.which === 13 ) { |
| 207 | // Check if the event target is the imageWrapElement itself |
| 208 | if ( event.target === imageWrapElement[ 0 ] ) { |
| 209 | // Prevent the default Enter key behavior |
| 210 | event.preventDefault(); |
| 211 | |
| 212 | // Check if the field has a value |
| 213 | if ( this.val() ) { |
| 214 | // Check if the uploader is NOT the basic uploader |
| 215 | if ( this.get( 'uploader' ) !== 'basic' ) { |
| 216 | // Open the edit attachment dialog |
| 217 | this.editAttachment(); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | }, |
| 223 | } ); |
| 224 | |
| 225 | acf.registerFieldType( Field ); |
| 226 | } )( jQuery ); |
| 227 |