| 1 |
/* global charitable_admin, WPCharitableAdmin */ |
| 2 |
|
| 3 |
/** |
| 4 |
* WPCharitable Admin Notifications. |
| 5 |
* |
| 6 |
* @since 1.6.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
'use strict'; |
| 10 |
|
| 11 |
var WPCharitableAdminNotifications = window.WPCharitableAdminNotifications || ( function( document, window, $ ) { |
| 12 |
|
| 13 |
$('#charitable-notifications').insertAfter( $( '.wp-header-end' ) ); |
| 14 |
|
| 15 |
/** |
| 16 |
* Elements holder. |
| 17 |
* |
| 18 |
* @since 1.6.0 |
| 19 |
* |
| 20 |
* @type {object} |
| 21 |
*/ |
| 22 |
var el = { |
| 23 |
|
| 24 |
$notifications: $( '#charitable-notifications' ), |
| 25 |
$nextButton: $( '#charitable-notifications .navigation .next' ), |
| 26 |
$prevButton: $( '#charitable-notifications .navigation .prev' ), |
| 27 |
$adminBarCounter: $( '#wp-admin-bar-charitable-menu .charitable-menu-notification-counter' ), |
| 28 |
$adminBarMenuItem: $( '#wp-admin-bar-charitable-notifications' ), |
| 29 |
|
| 30 |
}; |
| 31 |
|
| 32 |
/** |
| 33 |
* Public functions and properties. |
| 34 |
* |
| 35 |
* @since 1.6.0 |
| 36 |
* |
| 37 |
* @type {object} |
| 38 |
*/ |
| 39 |
var app = { |
| 40 |
|
| 41 |
/** |
| 42 |
* Start the engine. |
| 43 |
* |
| 44 |
* @since 1.6.0 |
| 45 |
*/ |
| 46 |
init: function() { |
| 47 |
|
| 48 |
$( app.ready ); |
| 49 |
}, |
| 50 |
|
| 51 |
/** |
| 52 |
* Document ready. |
| 53 |
* |
| 54 |
* @since 1.6.0 |
| 55 |
*/ |
| 56 |
ready: function() { |
| 57 |
|
| 58 |
app.updateNavigation(); |
| 59 |
app.events(); |
| 60 |
}, |
| 61 |
|
| 62 |
/** |
| 63 |
* Register JS events. |
| 64 |
* |
| 65 |
* @since 1.6.0 |
| 66 |
*/ |
| 67 |
events: function() { |
| 68 |
|
| 69 |
el.$notifications |
| 70 |
.on( 'click', '.dismiss', app.dismiss ) |
| 71 |
.on( 'click', '.next', app.navNext ) |
| 72 |
.on( 'click', '.prev', app.navPrev ); |
| 73 |
}, |
| 74 |
|
| 75 |
/** |
| 76 |
* Click on the Dismiss notification button. |
| 77 |
* |
| 78 |
* @since 1.6.0 |
| 79 |
* |
| 80 |
* @param {object} event Event object. |
| 81 |
*/ |
| 82 |
dismiss: function( event ) { |
| 83 |
|
| 84 |
if ( el.$currentMessage.length === 0 ) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
// Update counter. |
| 89 |
var count = parseInt( el.$adminBarCounter.text(), 10 ); |
| 90 |
if ( count > 1 ) { |
| 91 |
--count; |
| 92 |
el.$adminBarCounter.html( count ); |
| 93 |
} else { |
| 94 |
el.$adminBarCounter.remove(); |
| 95 |
el.$adminBarMenuItem.remove(); |
| 96 |
} |
| 97 |
|
| 98 |
// Remove notification. |
| 99 |
var $nextMessage = el.$nextMessage.length < 1 ? el.$prevMessage : el.$nextMessage, |
| 100 |
messageId = el.$currentMessage.data( 'message-id' ); |
| 101 |
|
| 102 |
if ( $nextMessage.length === 0 ) { |
| 103 |
el.$notifications.remove(); |
| 104 |
} else { |
| 105 |
el.$currentMessage.remove(); |
| 106 |
$nextMessage.addClass( 'current' ); |
| 107 |
app.updateNavigation(); |
| 108 |
} |
| 109 |
|
| 110 |
// AJAX call - update option. |
| 111 |
var data = { |
| 112 |
action: 'charitable_notification_dismiss', |
| 113 |
nonce: charitable_admin.nonce, |
| 114 |
id: messageId, |
| 115 |
}; |
| 116 |
|
| 117 |
$.post( charitable_admin.ajax_url, data, function( res ) { |
| 118 |
|
| 119 |
if ( ! res.success ) { |
| 120 |
// WPCharitableAdmin.debug( res ); |
| 121 |
console.log( res ); |
| 122 |
} |
| 123 |
} ).fail( function( xhr, textStatus, e ) { |
| 124 |
|
| 125 |
// WPCharitableAdmin.debug( xhr.responseText ); |
| 126 |
console.log( xhr.responseText ); |
| 127 |
} ); |
| 128 |
}, |
| 129 |
|
| 130 |
/** |
| 131 |
* Click on the Next notification button. |
| 132 |
* |
| 133 |
* @since 1.6.0 |
| 134 |
* |
| 135 |
* @param {object} event Event object. |
| 136 |
*/ |
| 137 |
navNext: function( event ) { |
| 138 |
|
| 139 |
if ( el.$nextButton.hasClass( 'disabled' ) ) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
el.$currentMessage.removeClass( 'current' ); |
| 144 |
el.$nextMessage.addClass( 'current' ); |
| 145 |
|
| 146 |
app.updateNavigation(); |
| 147 |
}, |
| 148 |
|
| 149 |
/** |
| 150 |
* Click on the Previous notification button. |
| 151 |
* |
| 152 |
* @since 1.6.0 |
| 153 |
* |
| 154 |
* @param {object} event Event object. |
| 155 |
*/ |
| 156 |
navPrev: function( event ) { |
| 157 |
|
| 158 |
if ( el.$prevButton.hasClass( 'disabled' ) ) { |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
el.$currentMessage.removeClass( 'current' ); |
| 163 |
el.$prevMessage.addClass( 'current' ); |
| 164 |
|
| 165 |
app.updateNavigation(); |
| 166 |
}, |
| 167 |
|
| 168 |
/** |
| 169 |
* Update navigation buttons. |
| 170 |
* |
| 171 |
* @since 1.6.0 |
| 172 |
*/ |
| 173 |
updateNavigation: function() { |
| 174 |
|
| 175 |
el.$currentMessage = el.$notifications.find( '.charitable-notifications-message.current' ); |
| 176 |
el.$nextMessage = el.$currentMessage.next( '.charitable-notifications-message' ); |
| 177 |
el.$prevMessage = el.$currentMessage.prev( '.charitable-notifications-message' ); |
| 178 |
|
| 179 |
if ( el.$nextMessage.length === 0 ) { |
| 180 |
el.$nextButton.addClass( 'disabled' ); |
| 181 |
} else { |
| 182 |
el.$nextButton.removeClass( 'disabled' ); |
| 183 |
} |
| 184 |
|
| 185 |
if ( el.$prevMessage.length === 0 ) { |
| 186 |
el.$prevButton.addClass( 'disabled' ); |
| 187 |
} else { |
| 188 |
el.$prevButton.removeClass( 'disabled' ); |
| 189 |
} |
| 190 |
}, |
| 191 |
}; |
| 192 |
|
| 193 |
return app; |
| 194 |
|
| 195 |
}( document, window, jQuery ) ); |
| 196 |
|
| 197 |
// Initialize. |
| 198 |
WPCharitableAdminNotifications.init(); |
| 199 |
|