address.js
4 years ago
admin-settings.js
4 years ago
count.js
4 years ago
file_upload.js
4 years ago
image.js
4 years ago
infinite_table.js
4 years ago
opening-hours.js
4 years ago
ordering.js
4 years ago
scheduler.js
4 years ago
spectrum.js
4 years ago
image.js
54 lines
| 1 | /** |
| 2 | * Javascript functions for Image component |
| 3 | * |
| 4 | * @package Simple Admin Pages |
| 5 | */ |
| 6 | |
| 7 | jQuery(document).ready(function ($) { |
| 8 | |
| 9 | var current_setting_id; |
| 10 | |
| 11 | function openMediaManager(e) { |
| 12 | e.stopPropagation(); |
| 13 | e.preventDefault(); |
| 14 | current_setting_id = $( this ).parents( '.sap-image-wrapper' ).data( 'id' ); |
| 15 | wp.media.frames.sap_frame.open(); |
| 16 | } |
| 17 | |
| 18 | function setImage( setting_id, image_id, image_url ) { |
| 19 | var $control = $( '.sap-image-wrapper[data-id="' + setting_id + '"]' ); |
| 20 | $control.find( 'img' ).attr( 'src', image_url ); |
| 21 | $control.find( '#' + setting_id ).val( image_id ); |
| 22 | $control.removeClass( 'sap-image-wrapper-no-image' ).addClass( 'sap-image-wrapper-has-image' ); |
| 23 | } |
| 24 | |
| 25 | function removeImage(e) { |
| 26 | e.stopPropagation(); |
| 27 | e.preventDefault(); |
| 28 | var $control = $( this ).parents( '.sap-image-wrapper' ); |
| 29 | $control.find( 'img' ).attr( 'src', '' ); |
| 30 | $control.find( '#' + $control.data( 'id' ) ).val( '' ); |
| 31 | $control.removeClass( 'sap-image-wrapper-has-image' ).addClass( 'sap-image-wrapper-no-image' ); |
| 32 | } |
| 33 | |
| 34 | wp.media.frames.sap_frame = wp.media( { |
| 35 | title: 'Select image', |
| 36 | multiple: false, |
| 37 | library: { |
| 38 | type: 'image', |
| 39 | }, |
| 40 | button: { |
| 41 | text: 'Use selected image', |
| 42 | }, |
| 43 | } ); |
| 44 | |
| 45 | wp.media.frames.sap_frame.on( 'select', function() { |
| 46 | var image = wp.media.frames.sap_frame.state().get( 'selection' ).first().toJSON(); |
| 47 | setImage( current_setting_id, image.id, image.url ); |
| 48 | }); |
| 49 | |
| 50 | $( '.sap-image-wrapper .sap-image-btn-add, .sap-image-wrapper .sap-image-btn-change' ).click( openMediaManager ); |
| 51 | |
| 52 | $( '.sap-image-wrapper .sap-image-btn-remove' ).click( removeImage ); |
| 53 | }); |
| 54 |