Permalink
Browse files

Delete entry functionality

Currently, we're using the insert entry ajax
endpoint and its success/error callbacks. We can
do this, because a delete is just a new entry with
the right replaces id and with empty contents.

As a side effect, when we delete an entry, the
entry textarea is cleared.
  • Loading branch information...
1 parent 0bcc5c3 commit 28802ffe3399713cfd26d1cbc3802f5faed320fa @nb nb committed Aug 1, 2012
Showing with 43 additions and 2 deletions.
  1. +3 −0 css/liveblog.css
  2. +26 −0 js/liveblog-publisher.js
  3. +10 −2 js/liveblog.js
  4. +1 −0 liveblog.php
  5. +3 −0 templates/liveblog-single-entry.php
View
@@ -160,3 +160,6 @@ span#liveblog-submit-spinner {
text-decoration: none;
text-transform: uppercase;
}
+.liveblog-entry-actions {
+ list-style-type: none;
+}
View
@@ -14,13 +14,26 @@
liveblog.publisher.$spinner = $( '#liveblog-submit-spinner' );
liveblog.publisher.$entry_button.bind( 'click', liveblog.publisher.submit_click );
+ $( '.liveblog-entry-delete' ).click( liveblog.publisher.delete_click );
}
liveblog.publisher.submit_click = function( e ) {
e.preventDefault();
liveblog.publisher.insert_entry();
}
+ liveblog.publisher.delete_click = function( e ) {
+ e.preventDefault();
+ var id = $( e.target ).closest( '.liveblog-entry' ).attr( 'id' ).replace( 'liveblog-entry-', '' );
+ if ( !id ) {
+ return;
+ }
+ if ( !confirm( liveblog_settings.delete_confirmation ) ) {
+ return;
+ }
+ liveblog.publisher.delete_entry( id );
+ }
+
liveblog.publisher.insert_entry = function() {
var entry_content = liveblog.publisher.$entry_text.val();
@@ -54,6 +67,19 @@
liveblog.publisher.hide_spinner();
}
+ liveblog.publisher.delete_entry = function( id ) {
+ var data = {
+ action: 'liveblog_insert_entry',
+ post_id: liveblog_settings.post_id,
+ replaces: id,
+ entry_content: ''
+ }
+ data[ liveblog_settings.nonce_key ] = liveblog.publisher.$nonce.val();
+ liveblog.publisher.disable_posting_interface();
+ liveblog.publisher.show_spinner();
+ liveblog.ajax_request( liveblog_settings.ajaxurl, data, liveblog.publisher.insert_entry_success, liveblog.publisher.insert_entry_error, 'POST' );
+ }
+
liveblog.publisher.disable_posting_interface = function() {
liveblog.publisher.$entry_button.attr( 'disabled', 'disabled' );
liveblog.publisher.$entry_text.attr( 'disabled', 'disabled' );
View
@@ -175,8 +175,12 @@ var liveblog = {};
return liveblog.nag_disabled;
}
+ liveblog.get_entry_by_id = function( id ) {
+ return $( '#liveblog-entry-' + id )
+ }
+
liveblog.display_entry = function( new_entry ) {
- var $entry = $( '#liveblog-entry-' + new_entry.id );
+ var $entry = liveblog.get_entry_by_id( new_entry.id );
if ( $entry.length ) {
liveblog.update_entry( $entry, new_entry );
} else {
@@ -196,10 +200,14 @@ var liveblog = {};
if ( updated_text ) {
$( '.liveblog-entry-text', $entry ).html( updated_text );
} else {
- $entry.remove();
+ liveblog.delete_entry( $entry );
}
}
+ liveblog.delete_entry = function( $entry ) {
+ $entry.remove();
+ }
+
liveblog.get_all_entries = function() {
return liveblog.$entry_container.find( '.liveblog-entry' );
}
View
@@ -491,6 +491,7 @@ public static function enqueue_scripts() {
// i18n
'update_nag_singular' => __( '%d new update', 'liveblog' ),
'update_nag_plural' => __( '%d new updates', 'liveblog' ),
+ 'delete_confirmation' => __( 'Do you really want do delete this entry? There is no way back.', 'liveblog' ),
) )
);
}
@@ -7,4 +7,7 @@
<div class="liveblog-entry-text">
<?php echo $content; ?>
</div>
+ <ul class="liveblog-entry-actions">
+ <li><button class="liveblog-entry-delete"><?php _e( 'Delete', 'liveblog' ); ?></button></li>
+ </ul>
</div>

0 comments on commit 28802ff

Please sign in to comment.