file.js
| 1 | /* |
| 2 | * File |
| 3 | * |
| 4 | * @description: |
| 5 | * @since: 3.5.8 |
| 6 | * @created: 17/01/13 |
| 7 | */ |
| 8 | |
| 9 | (function($){ |
| 10 | |
| 11 | var _file = acf.fields.file, |
| 12 | _media = acf.media; |
| 13 | |
| 14 | |
| 15 | /* |
| 16 | * Add |
| 17 | * |
| 18 | * @description: |
| 19 | * @since: 3.5.8 |
| 20 | * @created: 17/01/13 |
| 21 | */ |
| 22 | |
| 23 | _file.add = function( file ) |
| 24 | { |
| 25 | |
| 26 | // vars |
| 27 | var div = _media.div; |
| 28 | |
| 29 | // set atts |
| 30 | div.find('.acf-file-value').val( file.id ).trigger('change'); |
| 31 | div.find('.acf-file-icon').attr( 'src', file.icon ); |
| 32 | div.find('.acf-file-name').text( file.name ); |
| 33 | |
| 34 | |
| 35 | // set div class |
| 36 | div.addClass('active'); |
| 37 | |
| 38 | |
| 39 | // validation |
| 40 | div.closest('.field').removeClass('error'); |
| 41 | |
| 42 | }; |
| 43 | |
| 44 | |
| 45 | /* |
| 46 | * Edit |
| 47 | * |
| 48 | * @description: |
| 49 | * @since: 3.5.8 |
| 50 | * @created: 17/01/13 |
| 51 | */ |
| 52 | |
| 53 | _file.edit = function(){ |
| 54 | |
| 55 | // vars |
| 56 | var div = _media.div, |
| 57 | id = div.find('.acf-file-value').val(); |
| 58 | |
| 59 | |
| 60 | // show edit attachment |
| 61 | tb_show( _file.text.title_edit , acf.admin_url + 'media.php?attachment_id=' + id + '&action=edit&acf_action=edit_attachment&acf_field=file&TB_iframe=1'); |
| 62 | |
| 63 | }; |
| 64 | |
| 65 | |
| 66 | /* |
| 67 | * Remove |
| 68 | * |
| 69 | * @description: |
| 70 | * @since: 3.5.8 |
| 71 | * @created: 17/01/13 |
| 72 | */ |
| 73 | |
| 74 | _file.remove = function() |
| 75 | { |
| 76 | // vars |
| 77 | var div = _media.div; |
| 78 | |
| 79 | |
| 80 | // remove atts |
| 81 | div.find('.acf-file-value').val( '' ).trigger('change'); |
| 82 | div.find('.acf-file-icon').attr( 'src', '' ); |
| 83 | div.find('.acf-file-name').text( '' ); |
| 84 | |
| 85 | |
| 86 | // remove class |
| 87 | div.removeClass('active'); |
| 88 | |
| 89 | }; |
| 90 | |
| 91 | |
| 92 | /* |
| 93 | * Add Button |
| 94 | * |
| 95 | * @description: |
| 96 | * @since: 3.5.8 |
| 97 | * @created: 17/01/13 |
| 98 | */ |
| 99 | |
| 100 | $('.acf-file-uploader .add-file').live('click', function(){ |
| 101 | |
| 102 | // vars |
| 103 | var div = _media.div = $(this).closest('.acf-file-uploader'), |
| 104 | multiple = div.closest('.repeater').exists() ? true : false; |
| 105 | |
| 106 | |
| 107 | // show the thickbox |
| 108 | if( _media.type() == 'backbone' ) |
| 109 | { |
| 110 | // clear the frame |
| 111 | _media.clear_frame(); |
| 112 | |
| 113 | |
| 114 | // Create the media frame. Leave options blank for defaults |
| 115 | _media.frame = wp.media({ |
| 116 | title : _file.text.title_add, |
| 117 | multiple : multiple |
| 118 | }); |
| 119 | |
| 120 | |
| 121 | /* |
| 122 | _media.frame.on('all', function( e ){ |
| 123 | console.log( e ); |
| 124 | }); |
| 125 | */ |
| 126 | |
| 127 | |
| 128 | // add filter by overriding the option when the title is being created. This is an evet fired before the rendering / creating of the library content so it works but is a bit of a hack. In the future, this should be changed to an init / options event |
| 129 | _media.frame.on('title:create', function(){ |
| 130 | var state = _media.frame.state(); |
| 131 | state.set('filterable', 'uploaded'); |
| 132 | }); |
| 133 | |
| 134 | |
| 135 | // When an image is selected, run a callback. |
| 136 | _media.frame.on( 'select', function() { |
| 137 | |
| 138 | // get selected images |
| 139 | selection = _media.frame.state().get('selection'); |
| 140 | |
| 141 | if( selection ) |
| 142 | { |
| 143 | var i = 0; |
| 144 | |
| 145 | selection.each(function(attachment){ |
| 146 | |
| 147 | // counter |
| 148 | i++; |
| 149 | |
| 150 | |
| 151 | // select / add another file field? |
| 152 | if( i > 1 ) |
| 153 | { |
| 154 | var tr = _media.div.closest('tr'), |
| 155 | repeater = tr.closest('.repeater'); |
| 156 | |
| 157 | |
| 158 | if( tr.next('.row').exists() ) |
| 159 | { |
| 160 | _media.div = tr.next('.row').find('.acf-file-uploader'); |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | // add row |
| 165 | repeater.find('.add-row-end').trigger('click'); |
| 166 | |
| 167 | // set acf_div to new row file |
| 168 | _media.div = repeater.find('> table > tbody > tr.row:last .acf-file-uploader'); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | |
| 173 | // vars |
| 174 | var file = { |
| 175 | id : attachment.id, |
| 176 | name : attachment.attributes.filename, |
| 177 | icon : attachment.attributes.icon |
| 178 | }; |
| 179 | |
| 180 | |
| 181 | // add file to field |
| 182 | _file.add( file ); |
| 183 | |
| 184 | |
| 185 | }); |
| 186 | // selection.each(function(attachment){ |
| 187 | } |
| 188 | // if( selection ) |
| 189 | }); |
| 190 | // _media.frame.on( 'select', function() { |
| 191 | |
| 192 | |
| 193 | // Finally, open the modal |
| 194 | _media.frame.open(); |
| 195 | |
| 196 | var state = _media.frame.state(); |
| 197 | |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | tb_show( _file.text.title_add , acf.admin_url + 'media-upload.php?post_id=' + acf.post_id + '&post_ID=' + acf.post_id + '&type=file&acf_type=file&TB_iframe=1'); |
| 202 | } |
| 203 | |
| 204 | return false; |
| 205 | |
| 206 | }); |
| 207 | |
| 208 | |
| 209 | /* |
| 210 | * Edit Button |
| 211 | * |
| 212 | * @description: |
| 213 | * @since: 3.5.8 |
| 214 | * @created: 17/01/13 |
| 215 | */ |
| 216 | |
| 217 | $('.acf-file-uploader .edit-file').live('click', function(){ |
| 218 | |
| 219 | // vars |
| 220 | _media.div = $(this).closest('.acf-file-uploader'); |
| 221 | |
| 222 | _file.edit(); |
| 223 | |
| 224 | return false; |
| 225 | |
| 226 | }); |
| 227 | |
| 228 | |
| 229 | /* |
| 230 | * Remove Button |
| 231 | * |
| 232 | * @description: |
| 233 | * @since: 3.5.8 |
| 234 | * @created: 17/01/13 |
| 235 | */ |
| 236 | |
| 237 | $('.acf-file-uploader .remove-file').live('click', function(){ |
| 238 | |
| 239 | // vars |
| 240 | _media.div = $(this).closest('.acf-file-uploader'); |
| 241 | |
| 242 | _file.remove(); |
| 243 | |
| 244 | return false; |
| 245 | |
| 246 | }); |
| 247 | |
| 248 | |
| 249 | })(jQuery); |