| 1 |
/* global waNotifier */ |
| 2 |
(function($) { |
| 3 |
|
| 4 |
// Uploading media to WP using wp.media |
| 5 |
var file_frame; |
| 6 |
function uploadMediaFile( button, preview_media ) { |
| 7 |
var button_id = button.attr('id'); |
| 8 |
var field_id = button_id.replace( '_button', '' ); |
| 9 |
var preview_id = button_id.replace( '_button', '_preview' ); |
| 10 |
|
| 11 |
// Create the media frame. |
| 12 |
file_frame = wp.media.frames.file_frame = wp.media({ |
| 13 |
title: button.data( 'uploader_title' ), |
| 14 |
button: { |
| 15 |
text: button.data( 'uploader_button_text' ), |
| 16 |
}, |
| 17 |
library: { |
| 18 |
type: button.data( 'uploader_supported_file_types' ).split(',') |
| 19 |
}, |
| 20 |
multiple: false |
| 21 |
}); |
| 22 |
|
| 23 |
// When an image is selected, run a callback. |
| 24 |
file_frame.on( 'select', function() { |
| 25 |
attachment = file_frame.state().get('selection').first().toJSON(); |
| 26 |
jQuery("#"+field_id).attr('data-type', attachment.type); |
| 27 |
jQuery("#"+field_id).attr('data-subtype', attachment.subtype); |
| 28 |
jQuery("#"+field_id).siblings('.notifier-media-preview').find('.notifier-media-preview-item').addClass('hide'); |
| 29 |
if( preview_media ) { |
| 30 |
if('image' == attachment.type || ('application' == attachment.type && 'pdf' == attachment.subtype) ) { |
| 31 |
jQuery("#"+field_id).attr('data-url', attachment.sizes.full.url); |
| 32 |
jQuery("#"+preview_id+'_image').removeClass('hide').attr('src', attachment.sizes.thumbnail.url); |
| 33 |
} |
| 34 |
else if ('video' == attachment.type) { |
| 35 |
jQuery("#"+field_id).attr('data-url', attachment.url); |
| 36 |
jQuery("#"+preview_id+'_video').removeClass('hide').find('source').attr('src', attachment.url); |
| 37 |
jQuery("#"+preview_id+'_video')[0].load(); |
| 38 |
} |
| 39 |
} |
| 40 |
jQuery("#"+field_id).val(attachment.id).change(); |
| 41 |
}); |
| 42 |
|
| 43 |
// Finally, open the modal |
| 44 |
file_frame.open(); |
| 45 |
} |
| 46 |
|
| 47 |
$(document).on('ready', function() { |
| 48 |
|
| 49 |
/***************** |
| 50 |
* Dashboard page |
| 51 |
****************/ |
| 52 |
|
| 53 |
// Toggle steps on dashboard page |
| 54 |
$('.toggle-step').on('click', function() { |
| 55 |
$(this).closest('.step').toggleClass('active'); |
| 56 |
$(this).closest('.step').siblings().removeClass('active'); |
| 57 |
}); |
| 58 |
|
| 59 |
// Highlight menu items |
| 60 |
const wan_menu_elem = $('#toplevel_page_notifier'); |
| 61 |
const notifier_cpts = ['wa_message_template', 'wa_contact', 'wa_notification']; |
| 62 |
const current_cpt = $('#notifier-admin-header').data('post-type') || ''; |
| 63 |
if (notifier_cpts.includes(current_cpt)) { |
| 64 |
wan_menu_elem |
| 65 |
.removeClass('wp-not-current-submenu') |
| 66 |
.addClass('wp-has-current-submenu') |
| 67 |
.children('a') |
| 68 |
.removeClass('wp-not-current-submenu') |
| 69 |
.addClass('wp-has-current-submenu'); |
| 70 |
wan_menu_elem.find('a[href*="' + current_cpt + '"]').addClass('current').closest('li').addClass('current'); |
| 71 |
} |
| 72 |
|
| 73 |
// Toggle trigger info |
| 74 |
$('.notifier-show-trigger-info').click(function(e){ |
| 75 |
e.preventDefault(); |
| 76 |
$(this).closest('.notifier-trigger-wrap').find('.notifier-trigger-info').toggleClass('hide'); |
| 77 |
}); |
| 78 |
|
| 79 |
/***************** |
| 80 |
* Global |
| 81 |
****************/ |
| 82 |
|
| 83 |
// Make the top admin header sticky |
| 84 |
var wpcontent_top = $('#wpcontent').offset().top; |
| 85 |
$(window).scroll( function() { |
| 86 |
if (window.pageYOffset > wpcontent_top) { |
| 87 |
$('#notifier-admin-header').addClass('sticky'); |
| 88 |
} else { |
| 89 |
$('#notifier-admin-header').removeClass('sticky'); |
| 90 |
} |
| 91 |
}); |
| 92 |
|
| 93 |
// Show feilds conditionally |
| 94 |
conditionallyShowFields(); |
| 95 |
$(document).on('change keyup', '.meta-fields :input', function() { |
| 96 |
conditionallyShowFields(); |
| 97 |
}); |
| 98 |
|
| 99 |
/***************** |
| 100 |
* Settings page |
| 101 |
****************/ |
| 102 |
|
| 103 |
$('.notifier-media-upload-button').click(function() { |
| 104 |
uploadMediaFile( $(this), true ); |
| 105 |
}); |
| 106 |
|
| 107 |
$('.notifier-media-delete-button').click(function() { |
| 108 |
$(this).next( '.notifier-media-attachment-id' ).val( '' ).attr('data-tyoe', '').attr('data-subtyoe', '').attr('data-url', '').change(); |
| 109 |
$(this).siblings( '.notifier-media-preview' ).find('img').attr('src', '').addClass('hide'); |
| 110 |
$(this).siblings( '.notifier-media-preview' ).find('source').attr('src', '').parent().addClass('hide'); |
| 111 |
return false; |
| 112 |
}); |
| 113 |
|
| 114 |
|
| 115 |
}); |
| 116 |
|
| 117 |
})(jQuery); |
| 118 |
|