action-localize-helper.js
4 years ago
admin.js
4 years ago
editor.js
4 years ago
file-upload.js
4 years ago
form-block.js
4 years ago
frontend-forms.js
4 years ago
package.js
4 years ago
file-upload.js
329 lines
| 1 | ( function( $ ) { |
| 2 | |
| 3 | "use strict"; |
| 4 | |
| 5 | var JetFormBuilderFileUpload = { |
| 6 | |
| 7 | init: function() { |
| 8 | $( document ) |
| 9 | .on( 'change', '.jet-form-builder-file-upload__input', JetFormBuilderFileUpload.processUpload ) |
| 10 | .on( 'click', '.jet-form-builder-file-upload__file-remove', JetFormBuilderFileUpload.deleteUpload ); |
| 11 | |
| 12 | $( '.jet-form-builder-file-upload__files' ).sortable( { |
| 13 | items: '.jet-form-builder-file-upload__file', |
| 14 | forcePlaceholderSize: true, |
| 15 | } ).bind( 'sortupdate', JetFormBuilderFileUpload.onSortCallback ); |
| 16 | }, |
| 17 | |
| 18 | onSortCallback: function( e, ui ) { |
| 19 | var $upload = ui.item.closest( '.jet-form-builder-file-upload' ), |
| 20 | $files = $( e.target ), |
| 21 | $valueInput = $upload.find( '.jet-form-builder-file-upload__value' ), |
| 22 | args = $files.data( 'args' ), |
| 23 | maxFiles = parseInt( args.max_files, 10 ), |
| 24 | values = []; |
| 25 | |
| 26 | if ( 1 === maxFiles || ! args.insert_attachment ) { |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | $files.find( '.jet-form-builder-file-upload__file' ).each( function() { |
| 31 | |
| 32 | var $file = $( this ); |
| 33 | |
| 34 | switch ( args.value_format ) { |
| 35 | case 'id': |
| 36 | values.push( $file.data( 'id' ) ); |
| 37 | break; |
| 38 | |
| 39 | case 'both': |
| 40 | values.push( { |
| 41 | id: $file.data( 'id' ), |
| 42 | url: $file.data( 'file' ), |
| 43 | } ); |
| 44 | break; |
| 45 | |
| 46 | default: |
| 47 | values.push( $file.data( 'file' ) ); |
| 48 | break; |
| 49 | } |
| 50 | |
| 51 | } ); |
| 52 | |
| 53 | $valueInput.val( JSON.stringify( values ) ).trigger( 'change.JetFormBuilderMain' ); |
| 54 | |
| 55 | }, |
| 56 | |
| 57 | deleteUpload: function() { |
| 58 | var $this = $( this ), |
| 59 | $file = $this.closest( '.jet-form-builder-file-upload__file' ), |
| 60 | $upload = $this.closest( '.jet-form-builder-file-upload' ), |
| 61 | $value = $upload.find( '.jet-form-builder-file-upload__value' ), |
| 62 | val = $value.val(), |
| 63 | fileURL = $file.data( 'file' ), |
| 64 | fileID = $file.data( 'id' ), |
| 65 | $errors = $upload.find( '.jet-form-builder-file-upload__errors' ), |
| 66 | format = $file.data( 'format' ); |
| 67 | |
| 68 | if ( ! $errors.hasClass( 'is-hidden' ) ) { |
| 69 | $errors.html( '' ).addClass( 'is-hidden' ); |
| 70 | } |
| 71 | |
| 72 | if ( ! val ) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | val = JSON.parse( val ); |
| 77 | |
| 78 | if ( fileID ) { |
| 79 | fileID = parseInt( fileID, 10 ); |
| 80 | } |
| 81 | |
| 82 | if ( val.constructor === Array ) { |
| 83 | |
| 84 | var index = -1; |
| 85 | |
| 86 | switch ( format ) { |
| 87 | |
| 88 | case 'url': |
| 89 | index = val.indexOf( fileURL ); |
| 90 | break; |
| 91 | |
| 92 | case 'id': |
| 93 | |
| 94 | for ( var i = 0; i < val.length; i++ ) { |
| 95 | val[ i ] = parseInt( val[ i ], 10 ); |
| 96 | } |
| 97 | |
| 98 | index = val.indexOf( fileID ); |
| 99 | break; |
| 100 | |
| 101 | case 'both': |
| 102 | |
| 103 | for ( var i = 0; i < val.length; i++ ) { |
| 104 | if ( fileURL === val[ i ].url ) { |
| 105 | index = i; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | break; |
| 110 | } |
| 111 | |
| 112 | if ( 0 <= index ) { |
| 113 | val.splice( index, 1 ); |
| 114 | } |
| 115 | |
| 116 | if ( ! val[ 0 ] ) { |
| 117 | val = ''; |
| 118 | } |
| 119 | |
| 120 | } else { |
| 121 | val = ''; |
| 122 | } |
| 123 | |
| 124 | if ( val ) { |
| 125 | val = JSON.stringify( val ) |
| 126 | } |
| 127 | |
| 128 | $value.trigger( 'jet-form-builder/on-remove-media-item', [ fileURL, fileID ] ); |
| 129 | |
| 130 | $file.remove(); |
| 131 | $value.val( val ).trigger( 'change.JetFormBuilderMain' ); |
| 132 | |
| 133 | }, |
| 134 | |
| 135 | processUpload: function( event ) { |
| 136 | |
| 137 | var files = event.target.files, |
| 138 | $errors = $( event.target ).closest( '.jet-form-builder-file-upload' ).find( '.jet-form-builder-file-upload__errors' ); |
| 139 | |
| 140 | $errors.html( '' ).addClass( 'is-hidden' ); |
| 141 | |
| 142 | try { |
| 143 | JetFormBuilderFileUpload.uploadFiles( files, event.target ); |
| 144 | } catch ( error ) { |
| 145 | |
| 146 | if ( window.JetFormBuilderFileUploadConfig.errors[ error ] ) { |
| 147 | $errors.html( window.JetFormBuilderFileUploadConfig.errors[ error ] ).removeClass( 'is-hidden' ); |
| 148 | } else { |
| 149 | $errors.html( error ).removeClass( 'is-hidden' ); |
| 150 | } |
| 151 | |
| 152 | event.target.value = ''; |
| 153 | |
| 154 | } |
| 155 | |
| 156 | }, |
| 157 | |
| 158 | lockButtons: function( $upload ) { |
| 159 | |
| 160 | var $form = $upload.closest( 'form.jet-form-builder' ), |
| 161 | $buttons = $form.find( '.jet-form-builder__submit, .jet-form-builder__next-page, .jet-form-builder__prev-page' ); |
| 162 | |
| 163 | $buttons.attr( 'disabled', true ); |
| 164 | |
| 165 | }, |
| 166 | |
| 167 | unlockButtons: function( $upload ) { |
| 168 | |
| 169 | var $form = $upload.closest( 'form.jet-form-builder' ), |
| 170 | $buttons = $form.find( '.jet-form-builder__submit, .jet-form-builder__next-page, .jet-form-builder__prev-page' ); |
| 171 | |
| 172 | $buttons.attr( 'disabled', false ); |
| 173 | |
| 174 | }, |
| 175 | |
| 176 | uploadFiles: function( files, input ) { |
| 177 | |
| 178 | if ( ! files.length ) { |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | var file, formData, formID, field, xhr, limit, allowedTypes, maxSize; |
| 183 | var $input = $( input ), |
| 184 | $upload = $input.closest( '.jet-form-builder-file-upload' ), |
| 185 | $valueInput = $upload.find( '.jet-form-builder-file-upload__value' ), |
| 186 | $errors = $upload.find( '.jet-form-builder-file-upload__errors' ), |
| 187 | currentVal = $valueInput.val(); |
| 188 | |
| 189 | limit = input.dataset.max_files || 1; |
| 190 | limit = parseInt( limit, 10 ); |
| 191 | |
| 192 | allowedTypes = input.dataset.allowed_types || false; |
| 193 | formID = input.dataset.form_id || false; |
| 194 | field = input.dataset.field || false; |
| 195 | maxSize = input.dataset.max_size || window.JetFormBuilderFileUploadConfig.max_upload_size; |
| 196 | maxSize = parseInt( maxSize, 10 ); |
| 197 | |
| 198 | if ( currentVal && '' !== currentVal ) { |
| 199 | currentVal = JSON.parse( currentVal ); |
| 200 | |
| 201 | if ( Array.isArray( currentVal ) && currentVal.length && limit < ( files.length + currentVal.length ) ) { |
| 202 | throw 'upload_limit'; |
| 203 | } |
| 204 | |
| 205 | } |
| 206 | |
| 207 | if ( limit < files.length ) { |
| 208 | throw 'upload_limit'; |
| 209 | } |
| 210 | |
| 211 | formData = new FormData(); |
| 212 | |
| 213 | formData.append( 'action', window.JetFormBuilderFileUploadConfig.action ); |
| 214 | formData.append( 'nonce', window.JetFormBuilderFileUploadConfig.nonce ); |
| 215 | formData.append( 'form_id', formID ); |
| 216 | formData.append( 'field', field ); |
| 217 | |
| 218 | for ( var i = 0; i < files.length; i++ ) { |
| 219 | |
| 220 | file = files.item( i ); |
| 221 | |
| 222 | if ( allowedTypes && 0 > allowedTypes.indexOf( file.type ) ) { |
| 223 | throw 'file_type'; |
| 224 | } |
| 225 | |
| 226 | if ( file[ 'size' ] > maxSize ) { |
| 227 | throw 'file_size'; |
| 228 | } |
| 229 | |
| 230 | formData.append( 'file_' + i, file ); |
| 231 | |
| 232 | } |
| 233 | |
| 234 | xhr = new XMLHttpRequest(); |
| 235 | |
| 236 | $upload.addClass( 'is-loading' ); |
| 237 | JetFormBuilderFileUpload.lockButtons( $upload ); |
| 238 | |
| 239 | xhr.open( 'POST', window.JetFormBuilderFileUploadConfig.ajaxurl, true ); |
| 240 | |
| 241 | xhr.onload = function( e, r ) { |
| 242 | |
| 243 | $upload.removeClass( 'is-loading' ); |
| 244 | JetFormBuilderFileUpload.unlockButtons( $upload ); |
| 245 | |
| 246 | if ( xhr.status === 200 ) { |
| 247 | var response = e.currentTarget.response; |
| 248 | |
| 249 | try { |
| 250 | response = JSON.parse( response ); |
| 251 | } catch ( e ) { |
| 252 | $errors.html( window.JetFormBuilderFileUploadConfig.errors.internal ).removeClass( 'is-hidden' ); |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | if ( ! response.success ) { |
| 257 | $errors.html( response.data ).removeClass( 'is-hidden' ); |
| 258 | return; |
| 259 | } else { |
| 260 | JetFormBuilderFileUpload.updateResults( $upload, response.data ); |
| 261 | if ( response.data.errors ) { |
| 262 | $errors.html( response.data.errors ).removeClass( 'is-hidden' ); |
| 263 | } else { |
| 264 | $errors.html( '' ).addClass( 'is-hidden' ); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | } else { |
| 269 | $errors.html( xhr.status ).removeClass( 'is-hidden' ); |
| 270 | } |
| 271 | |
| 272 | }; |
| 273 | |
| 274 | xhr.send( formData ); |
| 275 | |
| 276 | input.value = ''; |
| 277 | |
| 278 | }, |
| 279 | |
| 280 | updateResults: function( $upload, responseData ) { |
| 281 | |
| 282 | var $filesContainer = $upload.find( '.jet-form-builder-file-upload__files' ), |
| 283 | $input = $upload.find( '.jet-form-builder-file-upload__value' ), |
| 284 | args = $filesContainer.data( 'args' ), |
| 285 | values = [], |
| 286 | inputValues = false, |
| 287 | limit = args.max_files || 1; |
| 288 | |
| 289 | limit = parseInt( limit, 10 ); |
| 290 | |
| 291 | const oldInputVal = $input.val(); |
| 292 | |
| 293 | if ( 1 === limit ) { |
| 294 | $filesContainer.html( responseData.html ); |
| 295 | $input.val( JSON.stringify( responseData.value ) ).trigger( 'change.JetFormBuilderMain' ); |
| 296 | } else { |
| 297 | |
| 298 | inputValues = $input.val(); |
| 299 | |
| 300 | if ( inputValues ) { |
| 301 | values = JSON.parse( inputValues ); |
| 302 | if ( ! values ) { |
| 303 | values = []; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | for ( var i = 0; i < responseData.value.length; i++ ) { |
| 308 | values.push( responseData.value[ i ] ); |
| 309 | } |
| 310 | |
| 311 | $input.val( JSON.stringify( values ) ).trigger( 'change.JetFormBuilderMain', [ JSON.parse( inputValues ) ] ); |
| 312 | |
| 313 | $filesContainer.append( responseData.html ); |
| 314 | $filesContainer.sortable( 'destroy' ); |
| 315 | |
| 316 | $filesContainer.sortable( { |
| 317 | items: '.jet-form-builder-file-upload__file', |
| 318 | forcePlaceholderSize: true, |
| 319 | } ).bind( 'sortupdate', JetFormBuilderFileUpload.onSortCallback ); |
| 320 | } |
| 321 | |
| 322 | $input.trigger( 'jet-form-builder/on-upload-media', [ responseData.value, JSON.parse( oldInputVal || '{}' ) ] ) |
| 323 | }, |
| 324 | |
| 325 | }; |
| 326 | |
| 327 | JetFormBuilderFileUpload.init(); |
| 328 | |
| 329 | }( jQuery ) ); |