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