| 1 |
/* |
| 2 |
* WpStream admin utility helpers. |
| 3 |
* |
| 4 |
* Exposes a small shared namespace (window.WpStreamUtils) of reusable admin-side |
| 5 |
* actions for managing stored cloud recordings/media: requesting a temporary |
| 6 |
* download link for a recording and deleting a recording file. Both talk to |
| 7 |
* admin-ajax.php and update the surrounding markup in place. |
| 8 |
*/ |
| 9 |
|
| 10 |
// Reuse an existing namespace object if present, otherwise create a fresh one. |
| 11 |
window.WpStreamUtils = window.WpStreamUtils || {}; |
| 12 |
|
| 13 |
/** |
| 14 |
* Wire up "get download link" buttons: on click, ask the server for a |
| 15 |
* downloadable URL for the associated recording and swap the button for a link. |
| 16 |
* |
| 17 |
* @return {void} |
| 18 |
*/ |
| 19 |
WpStreamUtils.generate_download_link = function(){ |
| 20 |
// Bind the click handler to every download-link trigger on the page. |
| 21 |
jQuery('.wpstream_get_download_link').on('click',function(){ |
| 22 |
// Build the admin-ajax endpoint URL from the localized admin base URL. |
| 23 |
var ajaxurl = wpstream_admin_control_vars.admin_url + 'admin-ajax.php'; |
| 24 |
// The recording's file name is stored on the clicked element's data attribute. |
| 25 |
var video_name = jQuery(this).attr('data-filename'); |
| 26 |
// Cache the parent element so we can update its contents after the request. |
| 27 |
var parent = jQuery(this).parent(); |
| 28 |
|
| 29 |
// Remove the button so it cannot be clicked twice while we wait. |
| 30 |
jQuery(this).remove(); |
| 31 |
// Show a temporary "please wait" message in place of the link. |
| 32 |
parent.find('.wpstream_download_link').show().text('please wait...'); |
| 33 |
|
| 34 |
// Request the download URL for this recording. |
| 35 |
jQuery.ajax({ |
| 36 |
type: 'POST', |
| 37 |
url: ajaxurl, |
| 38 |
dataType: 'json', |
| 39 |
data: { |
| 40 |
'action': 'wpstream_get_download_link', |
| 41 |
'video_name': video_name, |
| 42 |
'security': wpstream_admin_control_vars.recordings_nonce |
| 43 |
}, |
| 44 |
success: function (data) { |
| 45 |
// Server returned a usable URL: turn the element into a real download link. |
| 46 |
if( data.success === true ){ |
| 47 |
parent.find('.wpstream_download_link').show().text(wpstream_admin_control_vars.download_mess); |
| 48 |
parent.find('.wpstream_download_link').show().attr('href',data.url); |
| 49 |
}else{ |
| 50 |
// Otherwise surface the returned error message to the user. |
| 51 |
var error_message = data.error; |
| 52 |
// Translate the known "not enough traffic" code into a friendly message. |
| 53 |
if( data.error === 'NOT_ENOUGH_TRAFFIC' ) { |
| 54 |
error_message = 'Not Enough data to download!'; |
| 55 |
} |
| 56 |
|
| 57 |
// Display whichever error message we ended up with. |
| 58 |
parent.find('.wpstream_download_link').show().text(error_message); |
| 59 |
} |
| 60 |
}, |
| 61 |
error: function (errorThrown) { |
| 62 |
// error state |
| 63 |
} |
| 64 |
}); |
| 65 |
}); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Wire up "delete media" buttons: on click, ask the server to delete the |
| 70 |
* associated recording file and remove its row from the DOM on success. |
| 71 |
* |
| 72 |
* @return {void} |
| 73 |
*/ |
| 74 |
WpStreamUtils.generate_delete_link = function() { |
| 75 |
// Bind the click handler to every delete-media trigger on the page. |
| 76 |
jQuery('.wpstream_delete_media').on('click',function(){ |
| 77 |
// Build the admin-ajax endpoint URL from the localized admin base URL. |
| 78 |
var ajaxurl = wpstream_admin_control_vars.admin_url + 'admin-ajax.php'; |
| 79 |
// The recording's file name (trimmed) is stored on the clicked element's data attribute. |
| 80 |
var video_name = jQuery(this).attr('data-filename').trim(); |
| 81 |
// Cache the parent element so we can remove the whole row on success. |
| 82 |
var parent = jQuery(this).parent(); |
| 83 |
|
| 84 |
// Keep filenames as inert DOM data and let this one shared action own |
| 85 |
// confirmation. Browser attribute decoding restores names such as |
| 86 |
// O'Reilly.mp4 before the prompt and request are built. |
| 87 |
if ( ! window.confirm('Are you sure you wish to delete ' + video_name + '?') ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
// Ask the server to delete this recording file. |
| 92 |
jQuery.ajax({ |
| 93 |
type: 'POST', |
| 94 |
url: ajaxurl, |
| 95 |
dataType: 'json', |
| 96 |
data: { |
| 97 |
'action': 'wpstream_get_delete_file', |
| 98 |
'video_name': video_name, |
| 99 |
'security': wpstream_admin_control_vars.recordings_nonce |
| 100 |
|
| 101 |
}, |
| 102 |
success: function (data) { |
| 103 |
// On confirmed deletion, remove the recording's row from the page. |
| 104 |
if( data.success === true ){ |
| 105 |
parent.remove(); |
| 106 |
} |
| 107 |
}, |
| 108 |
error: function (errorThrown) { |
| 109 |
// error state |
| 110 |
} |
| 111 |
}); |
| 112 |
}); |
| 113 |
} |
| 114 |
|