| 1 |
jQuery(function(){ |
| 2 |
(function( blocks, element ) { |
| 3 |
var el = element.createElement; |
| 4 |
|
| 5 |
/* Hide the button required by the classic editor */ |
| 6 |
jQuery(function(){ |
| 7 |
jQuery('.cpm-classic-editor').hide(); |
| 8 |
jQuery('.cpm-gutenberg-editor').css({display:'block',width:'100%', padding:0}); |
| 9 |
jQuery('.cpm-gutenberg-editor select').css({height:'100px'}); |
| 10 |
}); |
| 11 |
|
| 12 |
/* Plugin Category */ |
| 13 |
blocks.getCategories().push({slug: 'cpgm', title: 'CP Google Maps'}); |
| 14 |
|
| 15 |
/* ICONS */ |
| 16 |
const iconCPGM = el('img', { width: 20, height: 20, src: "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIGZpbGw9ImN1cnJlbnRDb2xvciIgY2xhc3M9Imljb24gaWNvbi10YWJsZXIgaWNvbnMtdGFibGVyLWZpbGxlZCBpY29uLXRhYmxlci1tYXAtcGluIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjxwYXRoIGQ9Ik0wIDBoMjR2MjRIMHoiIGZpbGw9Im5vbmUiLz48cGF0aCBkPSJNMTguMzY0IDQuNjM2YTkgOSAwIDAgMSAuMjAzIDEyLjUxOWwtLjIwMy4yMS00LjI0MyA0LjI0MmEzIDMgMCAwIDEtNC4wOTcuMTM1bC0uMTQ0LS4xMzUtNC4yNDQtNC4yNDNBOSA5IDAgMCAxIDE4LjM2NCA0LjYzNnpNMTIgOGEzIDMgMCAxIDAgMCA2IDMgMyAwIDAgMCAwLTZ6IiBzdHlsZT0iZmlsbDojZmYyYTJhO2ZpbGwtb3BhY2l0eToxO3N0cm9rZTojMDAwO3N0cm9rZS1vcGFjaXR5OjEiLz48L3N2Zz4=" } ); |
| 17 |
|
| 18 |
/* CP Google Maps Code */ |
| 19 |
blocks.registerBlockType( 'cpgm/map', { |
| 20 |
title: 'CP Google Maps', |
| 21 |
icon: iconCPGM, |
| 22 |
category: 'cpgm', |
| 23 |
supports: { |
| 24 |
customClassName: false, |
| 25 |
className: false |
| 26 |
}, |
| 27 |
|
| 28 |
attributes: { |
| 29 |
shortcode : { |
| 30 |
type : 'text' |
| 31 |
} |
| 32 |
}, |
| 33 |
|
| 34 |
edit: function( props ) |
| 35 |
{ |
| 36 |
var children = [], postID = wp.data.select("core/editor").getCurrentPostId(); |
| 37 |
function onChangeMap(evt) |
| 38 |
{ |
| 39 |
props.setAttributes({shortcode: evt.target.value}); |
| 40 |
}; |
| 41 |
|
| 42 |
if(typeof props.attributes.shortcode == 'undefined') |
| 43 |
{ |
| 44 |
props.attributes.shortcode = cpm_generate_shortcode(); |
| 45 |
} |
| 46 |
|
| 47 |
return [ |
| 48 |
el( |
| 49 |
'textarea', |
| 50 |
{ |
| 51 |
key : 'cpgm-shortcode', |
| 52 |
onChange: onChangeMap, |
| 53 |
value : props.attributes.shortcode, |
| 54 |
style : {width:"100%", resize: "vertical"} |
| 55 |
} |
| 56 |
), |
| 57 |
el( |
| 58 |
'div', {className: 'cpm-iframe-container', key: 'cpm_iframe_container'}, |
| 59 |
el('div', {className: 'cpm-iframe-overlay', key: 'cpm_iframe_overlay'}), |
| 60 |
el('iframe', |
| 61 |
{ |
| 62 |
key: 'cpm_store_iframe', |
| 63 |
src: cpm_ge_config.url+encodeURIComponent(props.attributes.shortcode)+'&post-id='+encodeURIComponent(postID), |
| 64 |
height: 0, |
| 65 |
width: 500, |
| 66 |
scrolling: 'no' |
| 67 |
} |
| 68 |
) |
| 69 |
) |
| 70 |
]; |
| 71 |
}, |
| 72 |
|
| 73 |
save: function( props ) { |
| 74 |
return props.attributes.shortcode || '[codepeople-post-map]'; |
| 75 |
} |
| 76 |
}); |
| 77 |
} )( |
| 78 |
window.wp.blocks, |
| 79 |
window.wp.element |
| 80 |
); |
| 81 |
|
| 82 |
// Autoupdate |
| 83 |
function autoupdate() |
| 84 |
{ |
| 85 |
const isSavingPost = wp.data.select('core/editor').isSavingPost(); |
| 86 |
if (isSavingPost) return; |
| 87 |
wp.data.dispatch('core/editor').savePost({ waitForResponse: false }).then(()=>{ |
| 88 |
jQuery('.cpm-iframe-container iframe').each(function(){ |
| 89 |
this.contentWindow.location.reload(); |
| 90 |
}); |
| 91 |
}); |
| 92 |
}; |
| 93 |
|
| 94 |
jQuery(document).on('change click', '#codepeople_post_map_form :input, [id*="cpm_point"]', |
| 95 |
function(evt) |
| 96 |
{ |
| 97 |
var t = jQuery(evt.target); |
| 98 |
if(evt.type == 'click' && t.is('input[type="button"]') || evt.type == 'change') autoupdate(); |
| 99 |
} |
| 100 |
); |
| 101 |
}); |