pro
1 year ago
_acf-compatibility.js
1 year ago
_acf-condition-types.js
1 year 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
1 year ago
_acf-field-checkbox.js
1 year ago
_acf-field-color-picker.js
1 year ago
_acf-field-date-picker.js
1 year ago
_acf-field-date-time-picker.js
1 year ago
_acf-field-file.js
1 year ago
_acf-field-google-map.js
1 year ago
_acf-field-icon-picker.js
1 year ago
_acf-field-image.js
1 year 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
1 year 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
1 year 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
1 year ago
_acf-media.js
1 year ago
_acf-modal.js
1 year ago
_acf-model.js
1 year ago
_acf-notice.js
1 year ago
_acf-panel.js
1 year ago
_acf-popup.js
1 year ago
_acf-postbox.js
1 year ago
_acf-screen.js
1 year ago
_acf-select2.js
1 year ago
_acf-tinymce.js
1 year ago
_acf-tooltip.js
1 year ago
_acf-unload.js
1 year ago
_acf-validation.js
1 year ago
_acf.js
1 year ago
_browse-fields-modal.js
1 year ago
_field-group-compatibility.js
1 year ago
_field-group-conditions.js
1 year ago
_field-group-field.js
1 year 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
191 lines
| 1 | ( function ( $, undefined ) { |
| 2 | var 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 | }, |
| 19 | |
| 20 | initialize: function () { |
| 21 | // add attribute to form |
| 22 | if ( this.get( 'uploader' ) === 'basic' ) { |
| 23 | this.$el |
| 24 | .closest( 'form' ) |
| 25 | .attr( 'enctype', 'multipart/form-data' ); |
| 26 | } |
| 27 | }, |
| 28 | |
| 29 | validateAttachment: function ( attachment ) { |
| 30 | // Use WP attachment attributes when available. |
| 31 | if ( attachment && attachment.attributes ) { |
| 32 | attachment = attachment.attributes; |
| 33 | } |
| 34 | |
| 35 | // Apply defaults. |
| 36 | attachment = acf.parseArgs( attachment, { |
| 37 | id: 0, |
| 38 | url: '', |
| 39 | alt: '', |
| 40 | title: '', |
| 41 | caption: '', |
| 42 | description: '', |
| 43 | width: 0, |
| 44 | height: 0, |
| 45 | } ); |
| 46 | |
| 47 | // Override with "preview size". |
| 48 | var size = acf.isget( |
| 49 | attachment, |
| 50 | 'sizes', |
| 51 | this.get( 'preview_size' ) |
| 52 | ); |
| 53 | if ( size ) { |
| 54 | attachment.url = size.url; |
| 55 | attachment.width = size.width; |
| 56 | attachment.height = size.height; |
| 57 | } |
| 58 | |
| 59 | // Return. |
| 60 | return attachment; |
| 61 | }, |
| 62 | |
| 63 | render: function ( attachment ) { |
| 64 | attachment = this.validateAttachment( attachment ); |
| 65 | |
| 66 | // Update DOM. |
| 67 | this.$( 'img' ).attr( { |
| 68 | src: attachment.url, |
| 69 | alt: attachment.alt, |
| 70 | } ); |
| 71 | if ( attachment.id ) { |
| 72 | this.val( attachment.id ); |
| 73 | this.$control().addClass( 'has-value' ); |
| 74 | } else { |
| 75 | this.val( '' ); |
| 76 | this.$control().removeClass( 'has-value' ); |
| 77 | } |
| 78 | }, |
| 79 | |
| 80 | // create a new repeater row and render value |
| 81 | append: function ( attachment, parent ) { |
| 82 | // create function to find next available field within parent |
| 83 | var getNext = function ( field, parent ) { |
| 84 | // find existing file fields within parent |
| 85 | var fields = acf.getFields( { |
| 86 | key: field.get( 'key' ), |
| 87 | parent: parent.$el, |
| 88 | } ); |
| 89 | |
| 90 | // find the first field with no value |
| 91 | for ( var i = 0; i < fields.length; i++ ) { |
| 92 | if ( ! fields[ i ].val() ) { |
| 93 | return fields[ i ]; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // return |
| 98 | return false; |
| 99 | }; |
| 100 | |
| 101 | // find existing file fields within parent |
| 102 | var field = getNext( this, parent ); |
| 103 | |
| 104 | // add new row if no available field |
| 105 | if ( ! field ) { |
| 106 | parent.$( '.acf-button:last' ).trigger( 'click' ); |
| 107 | field = getNext( this, parent ); |
| 108 | } |
| 109 | |
| 110 | // render |
| 111 | if ( field ) { |
| 112 | field.render( attachment ); |
| 113 | } |
| 114 | }, |
| 115 | |
| 116 | selectAttachment: function () { |
| 117 | // vars |
| 118 | var parent = this.parent(); |
| 119 | var multiple = parent && parent.get( 'type' ) === 'repeater'; |
| 120 | |
| 121 | // new frame |
| 122 | var frame = acf.newMediaPopup( { |
| 123 | mode: 'select', |
| 124 | type: 'image', |
| 125 | title: acf.__( 'Select Image' ), |
| 126 | field: this.get( 'key' ), |
| 127 | multiple: multiple, |
| 128 | library: this.get( 'library' ), |
| 129 | allowedTypes: this.get( 'mime_types' ), |
| 130 | select: $.proxy( function ( attachment, i ) { |
| 131 | if ( i > 0 ) { |
| 132 | this.append( attachment, parent ); |
| 133 | } else { |
| 134 | this.render( attachment ); |
| 135 | } |
| 136 | }, this ), |
| 137 | } ); |
| 138 | }, |
| 139 | |
| 140 | editAttachment: function () { |
| 141 | // vars |
| 142 | var val = this.val(); |
| 143 | |
| 144 | // bail early if no val |
| 145 | if ( ! val ) return; |
| 146 | |
| 147 | // popup |
| 148 | var frame = acf.newMediaPopup( { |
| 149 | mode: 'edit', |
| 150 | title: acf.__( 'Edit Image' ), |
| 151 | button: acf.__( 'Update Image' ), |
| 152 | attachment: val, |
| 153 | field: this.get( 'key' ), |
| 154 | select: $.proxy( function ( attachment, i ) { |
| 155 | this.render( attachment ); |
| 156 | }, this ), |
| 157 | } ); |
| 158 | }, |
| 159 | |
| 160 | removeAttachment: function () { |
| 161 | this.render( false ); |
| 162 | }, |
| 163 | |
| 164 | onClickAdd: function ( e, $el ) { |
| 165 | this.selectAttachment(); |
| 166 | }, |
| 167 | |
| 168 | onClickEdit: function ( e, $el ) { |
| 169 | this.editAttachment(); |
| 170 | }, |
| 171 | |
| 172 | onClickRemove: function ( e, $el ) { |
| 173 | this.removeAttachment(); |
| 174 | }, |
| 175 | |
| 176 | onChange: function ( e, $el ) { |
| 177 | var $hiddenInput = this.$input(); |
| 178 | |
| 179 | if ( ! $el.val() ) { |
| 180 | $hiddenInput.val( '' ); |
| 181 | } |
| 182 | |
| 183 | acf.getFileInputData( $el, function ( data ) { |
| 184 | $hiddenInput.val( $.param( data ) ); |
| 185 | } ); |
| 186 | }, |
| 187 | } ); |
| 188 | |
| 189 | acf.registerFieldType( Field ); |
| 190 | } )( jQuery ); |
| 191 |