PluginProbe
Advanced Custom Fields (ACF®) / 4.1.2
Advanced Custom Fields (ACF®) v4.1.2
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / js / input / image.js
image.js
247 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /*
2 * Image
3 *
4 * @description:
5 * @since: 3.5.8
6 * @created: 17/01/13
7 */
8
9 (function($){
10
11 var _image = acf.fields.image,
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 _image.add = function( image )
24 {
25 // vars
26 var div = _media.div;
27
28 // set atts
29 div.find('.acf-image-value').val( image.id ).trigger('change');
30 div.find('img').attr( 'src', image.src );
31
32
33 // set div class
34 div.addClass('active');
35
36
37 // validation
38 div.closest('.field').removeClass('error');
39
40 };
41
42
43 /*
44 * Edit
45 *
46 * @description:
47 * @since: 3.5.8
48 * @created: 17/01/13
49 */
50
51 _image.edit = function(){
52
53 // vars
54 var div = _media.div,
55 id = div.find('.acf-image-value').val();
56
57
58 // show edit attachment
59 tb_show( _image.text.title_edit , acf.admin_url + 'media.php?attachment_id=' + id + '&action=edit&acf_action=edit_attachment&acf_field=image&TB_iframe=1');
60
61 };
62
63
64 /*
65 * Remove
66 *
67 * @description:
68 * @since: 3.5.8
69 * @created: 17/01/13
70 */
71
72 _image.remove = function()
73 {
74 // vars
75 var div = _media.div;
76
77
78 // remove atts
79 div.find('.acf-image-value').val('').trigger('change');
80 div.find('img').attr('src', '');
81
82
83 // remove class
84 div.removeClass('active');
85
86 };
87
88
89 /*
90 * Add Button
91 *
92 * @description:
93 * @since: 3.5.8
94 * @created: 17/01/13
95 */
96
97 $('.acf-image-uploader .add-image').live('click', function(){
98
99 // vars
100 var div = _media.div = $(this).closest('.acf-image-uploader'),
101 preview_size = div.attr('data-preview_size'),
102 multiple = div.closest('.repeater').exists() ? true : false;
103
104
105 // show the thickbox
106 if( _media.type() == 'backbone' )
107 {
108 // clear the frame
109 _media.clear_frame();
110
111
112 // Create the media frame. Leave options blank for defaults
113 _media.frame = wp.media({
114 title : _image.text.title_add,
115 multiple : multiple,
116 library : {
117 type : 'image'
118 },
119 });
120
121
122 // 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
123 _media.frame.on('title:create', function(){
124 var state = _media.frame.state();
125 state.set('filterable', 'uploaded');
126 });
127
128
129 // When an image is selected, run a callback.
130 _media.frame.on( 'select', function() {
131
132 // get selected images
133 selection = _media.frame.state().get('selection');
134
135 if( selection )
136 {
137 var i = 0;
138
139 selection.each(function(attachment){
140
141 // counter
142 i++;
143
144
145 // select / add another file field?
146 if( i > 1 )
147 {
148 var tr = _media.div.closest('tr'),
149 repeater = tr.closest('.repeater');
150
151
152 if( tr.next('.row').exists() )
153 {
154 _media.div = tr.next('.row').find('.acf-image-uploader');
155 }
156 else
157 {
158 // add row
159 repeater.find('.add-row-end').trigger('click');
160
161 // set acf_div to new row file
162 _media.div = repeater.find('> table > tbody > tr.row:last .acf-image-uploader');
163 }
164 }
165
166
167 // vars
168 var image = {
169 id : attachment.id,
170 src : attachment.attributes.url
171 };
172
173
174 // is preview size available?
175 if( attachment.attributes.sizes && attachment.attributes.sizes[ preview_size ] )
176 {
177 image.src = attachment.attributes.sizes[ preview_size ].url;
178 }
179
180
181 // add image to field
182 _image.add( image );
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 }
197 else
198 {
199 tb_show( _image.text.title_add , acf.admin_url + 'media-upload.php?post_id=' + acf.post_id + '&post_ID=' + acf.post_id + '&type=image&acf_type=image&acf_preview_size=' + preview_size + 'TB_iframe=1');
200 }
201
202
203 return false;
204 });
205
206
207 /*
208 * Edit Button
209 *
210 * @description:
211 * @since: 3.5.8
212 * @created: 17/01/13
213 */
214
215 $('.acf-image-uploader .acf-button-edit').live('click', function(){
216
217 // vars
218 _media.div = $(this).closest('.acf-image-uploader');
219
220 _image.edit();
221
222 return false;
223
224 });
225
226
227 /*
228 * Remove Button
229 *
230 * @description:
231 * @since: 3.5.8
232 * @created: 17/01/13
233 */
234
235 $('.acf-image-uploader .acf-button-delete').live('click', function(){
236
237 // vars
238 _media.div = $(this).closest('.acf-image-uploader');
239
240 _image.remove();
241
242 return false;
243
244 });
245
246
247 })(jQuery);