jqueryui
4 years ago
select2
6 years ago
wp-color-picker-alpha
5 years ago
autocomplete.js
6 years ago
autosave.js
7 years ago
button-group.js
6 years ago
clone.js
4 years ago
color.js
6 years ago
date.js
6 years ago
datetime.js
6 years ago
file-input.js
4 years ago
file-upload.js
4 years ago
file.js
4 years ago
image-advanced.js
4 years ago
image-select.js
6 years ago
image-upload.js
4 years ago
input-list.js
6 years ago
map-frontend.js
4 years ago
map.js
4 years ago
media.js
3 years ago
notification.js
6 years ago
oembed.js
6 years ago
osm-frontend.js
5 years ago
osm.js
5 years ago
range.js
4 years ago
script.js
6 years ago
select-advanced.js
3 years ago
select-tree.js
5 years ago
select.js
6 years ago
slider.js
6 years ago
taxonomy.js
3 years ago
time.js
6 years ago
validation.min.js
3 years ago
video.js
6 years ago
wysiwyg.js
5 years ago
file-upload.js
104 lines
| 1 | ( function ( $, wp, rwmb ) { |
| 2 | 'use strict'; |
| 3 | |
| 4 | var views = rwmb.views = rwmb.views || {}, |
| 5 | MediaField = views.MediaField, |
| 6 | FileUploadField, UploadButton; |
| 7 | |
| 8 | FileUploadField = views.FileUploadField = MediaField.extend( { |
| 9 | createAddButton: function () { |
| 10 | this.addButton = new UploadButton( {controller: this.controller} ); |
| 11 | } |
| 12 | } ); |
| 13 | |
| 14 | UploadButton = views.UploadButton = Backbone.View.extend( { |
| 15 | className: 'rwmb-upload-area', |
| 16 | tagName: 'div', |
| 17 | template: wp.template( 'rwmb-upload-area' ), |
| 18 | render: function () { |
| 19 | this.$el.html( this.template( {} ) ); |
| 20 | return this; |
| 21 | }, |
| 22 | |
| 23 | initialize: function ( options ) { |
| 24 | this.controller = options.controller; |
| 25 | this.el.id = _.uniqueId( 'rwmb-upload-area-' ); |
| 26 | this.render(); |
| 27 | |
| 28 | // Auto hide if you reach the max number of media |
| 29 | this.listenTo( this.controller, 'change:full', function () { |
| 30 | this.$el.toggle( ! this.controller.get( 'full' ) ); |
| 31 | } ); |
| 32 | }, |
| 33 | |
| 34 | // Initializes plupload using code from wp.Uploader (wp-includes/js/plupload/wp-plupload.js) |
| 35 | initUploader: function ( $this ) { |
| 36 | var self = this, |
| 37 | extensions = this.getExtensions().join( ',' ), |
| 38 | maxFileSize = this.controller.get( 'maxFileSize' ), |
| 39 | options = { |
| 40 | container: this.el, |
| 41 | dropzone: this.el, |
| 42 | browser: this.$( '.rwmb-browse-button' ), |
| 43 | params: { |
| 44 | post_id : $( '#post_ID' ).val() |
| 45 | }, |
| 46 | added: function( attachment ) { |
| 47 | self.controller.get( 'items' ).add( [attachment] ); |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | // Initialize the plupload instance. |
| 52 | this.uploader = new wp.Uploader( options ); |
| 53 | |
| 54 | var filters = this.uploader.uploader.getOption( 'filters' ); |
| 55 | if ( maxFileSize ) { |
| 56 | filters.max_file_size = maxFileSize; |
| 57 | } |
| 58 | if ( extensions ) { |
| 59 | filters.mime_types = [{title: i18nRwmbMedia.select, extensions: extensions}]; |
| 60 | } |
| 61 | this.uploader.uploader.setOption( 'filters', filters ); |
| 62 | $this.data( 'uploader', this.uploader ); |
| 63 | }, |
| 64 | |
| 65 | getExtensions: function () { |
| 66 | var mimeTypes = this.controller.get( 'mimeType' ).split( ',' ), |
| 67 | exts = []; |
| 68 | |
| 69 | _.each( mimeTypes, function ( current, index ) { |
| 70 | if ( i18nRwmbMedia.extensions[current] ) { |
| 71 | exts = exts.concat( i18nRwmbMedia.extensions[current] ); |
| 72 | } |
| 73 | } ); |
| 74 | return exts; |
| 75 | } |
| 76 | } ); |
| 77 | |
| 78 | function initFileUpload() { |
| 79 | var $this = $( this ), |
| 80 | view = $this.data( 'view' ); |
| 81 | |
| 82 | if ( view ) { |
| 83 | return; |
| 84 | } |
| 85 | view = new FileUploadField( { input: this } ); |
| 86 | |
| 87 | $this.siblings( '.rwmb-media-view' ).remove(); |
| 88 | $this.after( view.el ); |
| 89 | |
| 90 | // Init uploader after view is inserted to make wp.Uploader works. |
| 91 | view.addButton.initUploader( $this ); |
| 92 | |
| 93 | $this.data( 'view', view ); |
| 94 | } |
| 95 | |
| 96 | function init( e ) { |
| 97 | $( e.target ).find( '.rwmb-file_upload' ).each( initFileUpload ); |
| 98 | } |
| 99 | |
| 100 | rwmb.$document |
| 101 | .on( 'mb_ready', init ) |
| 102 | .on( 'clone', '.rwmb-file_upload', initFileUpload ) |
| 103 | } )( jQuery, wp, rwmb ); |
| 104 |