| 1 |
/* global jQuery, friends */ |
| 2 |
jQuery( function ( $ ) { |
| 3 |
$( document ).on( 'click', 'input#require_codeword', function () { |
| 4 |
if ( this.checked ) { |
| 5 |
$( '#codeword_options' ).removeClass( 'hidden' ); |
| 6 |
$( '#codeword' ).focus(); |
| 7 |
} else { |
| 8 |
$( '#codeword_options' ).addClass( 'hidden' ); |
| 9 |
} |
| 10 |
} ); |
| 11 |
|
| 12 |
$( document ).on( 'click', 'a#send-friends-advanced', function () { |
| 13 |
$( 'tr.friends-advanced' ) |
| 14 |
.removeClass( 'hidden' ) |
| 15 |
.first() |
| 16 |
.find( 'input:visible:first' ) |
| 17 |
.focus(); |
| 18 |
$( this ).remove(); |
| 19 |
return false; |
| 20 |
} ); |
| 21 |
|
| 22 |
$( document ).on( 'click', 'a#add-another-feed', function () { |
| 23 |
$( 'tr.another-feed' ) |
| 24 |
.removeClass( 'hidden' ) |
| 25 |
.find( 'input:visible:first' ) |
| 26 |
.focus(); |
| 27 |
$( this ).remove(); |
| 28 |
return false; |
| 29 |
} ); |
| 30 |
|
| 31 |
$( document ).on( 'click', 'a#show-details', function () { |
| 32 |
$( '.details' ).toggleClass( 'hidden' ).focus(); |
| 33 |
return false; |
| 34 |
} ); |
| 35 |
|
| 36 |
$( document ).on( 'click', 'a#show-alternate-feeds', function () { |
| 37 |
$( 'li.rel-alternate' ).toggleClass( 'hidden' ); |
| 38 |
return false; |
| 39 |
} ); |
| 40 |
|
| 41 |
$( document ).on( 'click', 'a#show-unsupported-feeds', function () { |
| 42 |
$( '#unsupported-feeds' ).toggleClass( 'hidden' ); |
| 43 |
return false; |
| 44 |
} ); |
| 45 |
|
| 46 |
$( document ).on( 'click', '#friends_retention_days', function ( e ) { |
| 47 |
e.stopPropagation(); |
| 48 |
} ); |
| 49 |
|
| 50 |
$( document ).on( |
| 51 |
'click', |
| 52 |
'#friends_enable_retention_days, #friends_enable_retention_days_line', |
| 53 |
function () { |
| 54 |
const el = document.getElementById( |
| 55 |
'friends_enable_retention_days' |
| 56 |
); |
| 57 |
if ( this.id !== el.id ) { |
| 58 |
$( '#friends_enable_retention_days' ).attr( |
| 59 |
'checked', |
| 60 |
! el.checked |
| 61 |
); |
| 62 |
} |
| 63 |
$( '#friends_retention_days' ).attr( 'disabled', ! el.checked ); |
| 64 |
$( '#friends_enable_retention_days_line' ).attr( |
| 65 |
'class', |
| 66 |
el.checked ? '' : 'disabled' |
| 67 |
); |
| 68 |
} |
| 69 |
); |
| 70 |
|
| 71 |
$( document ).on( 'click', '#friends_retention_number', function ( e ) { |
| 72 |
e.stopPropagation(); |
| 73 |
} ); |
| 74 |
|
| 75 |
$( document ).on( |
| 76 |
'click', |
| 77 |
'#friends_enable_retention_number, #friends_enable_retention_number_line', |
| 78 |
function () { |
| 79 |
const el = document.getElementById( |
| 80 |
'friends_enable_retention_number' |
| 81 |
); |
| 82 |
if ( this.id !== el.id ) { |
| 83 |
$( '#friends_enable_retention_number' ).attr( |
| 84 |
'checked', |
| 85 |
! el.checked |
| 86 |
); |
| 87 |
} |
| 88 |
$( '#friends_retention_number' ).attr( 'disabled', ! el.checked ); |
| 89 |
$( '#friends_enable_retention_number_line' ).attr( |
| 90 |
'class', |
| 91 |
el.checked ? '' : 'disabled' |
| 92 |
); |
| 93 |
} |
| 94 |
); |
| 95 |
|
| 96 |
$( document ).on( 'click', 'a#toggle-raw-rules-data', function () { |
| 97 |
$( '#raw-rules-data' ).toggle(); |
| 98 |
return false; |
| 99 |
} ); |
| 100 |
|
| 101 |
const previewRules = function () { |
| 102 |
const $this = $( 'button#refresh-preview-rules' ); |
| 103 |
let url = friends.ajax_url + '?user=' + $this.data( 'friend' ); |
| 104 |
if ( $this.data( 'post' ) ) { |
| 105 |
url += '&post=' + $this.data( 'post' ); |
| 106 |
} |
| 107 |
$.post( |
| 108 |
url, |
| 109 |
$( 'form#edit-rules [name^=rules]' ) |
| 110 |
.add( 'form#edit-rules [name=catch_all]' ) |
| 111 |
.serialize() + |
| 112 |
'&_ajax_nonce=' + |
| 113 |
$this.data( 'nonce' ) + |
| 114 |
'&action=friends_preview_rules', |
| 115 |
function ( response ) { |
| 116 |
$( '#preview-rules' ).html( response ); |
| 117 |
} |
| 118 |
); |
| 119 |
}; |
| 120 |
|
| 121 |
$( document ).on( 'change', 'select.rule-action', function () { |
| 122 |
const tdReplace = $( this ).closest( 'tr' ).find( 'td.replace-with' ); |
| 123 |
if ( 'replace' === $( this ).val() ) { |
| 124 |
tdReplace.show(); |
| 125 |
} else { |
| 126 |
tdReplace.hide(); |
| 127 |
} |
| 128 |
previewRules(); |
| 129 |
} ); |
| 130 |
|
| 131 |
$( document ).on( |
| 132 |
'keyup click', |
| 133 |
'#edit-rules input, #edit-rules select', |
| 134 |
previewRules |
| 135 |
); |
| 136 |
|
| 137 |
$( document ).on( 'click', 'button#refresh-preview-rules', function () { |
| 138 |
previewRules(); |
| 139 |
return false; |
| 140 |
} ); |
| 141 |
|
| 142 |
$( document ).on( 'click', 'a.preview-parser', function () { |
| 143 |
const url = $( this ).closest( 'tbody' ).find( 'input.url' ).val(); |
| 144 |
if ( ! url ) { |
| 145 |
return false; |
| 146 |
} |
| 147 |
this.href = this.href.replace( |
| 148 |
/&parser(=[^&$]+)?([&$])/, |
| 149 |
'&parser=' + |
| 150 |
encodeURIComponent( |
| 151 |
$( this ).closest( 'tr' ).find( 'select' ).val() |
| 152 |
) + |
| 153 |
'&' |
| 154 |
); |
| 155 |
this.href = this.href.replace( |
| 156 |
/&preview(=[^&$]+)?([&$])/, |
| 157 |
'&preview=' + encodeURIComponent( url ) + '&' |
| 158 |
); |
| 159 |
} ); |
| 160 |
|
| 161 |
$( document ).on( 'click', 'a.show-inactive-feeds', function () { |
| 162 |
$( 'ul.feeds' ).show().find( 'li.inactive' ).toggleClass( 'hidden' ); |
| 163 |
return false; |
| 164 |
} ); |
| 165 |
|
| 166 |
$( document ).on( 'click', 'a.show-log-lines', function () { |
| 167 |
$( 'ul.feeds li.active p.lastlog' ).toggleClass( 'hidden' ); |
| 168 |
return false; |
| 169 |
} ); |
| 170 |
|
| 171 |
$( document ).on( 'click', 'a.add-feed', function () { |
| 172 |
$( 'ul.feeds' ) |
| 173 |
.removeClass( 'hidden' ) |
| 174 |
.find( 'li.template' ) |
| 175 |
.removeClass( 'hidden' ) |
| 176 |
.find( 'input:first' ) |
| 177 |
.focus(); |
| 178 |
$( this ).remove(); |
| 179 |
return false; |
| 180 |
} ); |
| 181 |
|
| 182 |
$( |
| 183 |
'<a href="' + |
| 184 |
friends.add_friend_url + |
| 185 |
'" class="page-title-action">' + |
| 186 |
friends.add_friend_text + |
| 187 |
'</a>' |
| 188 |
).insertAfter( 'a.page-title-action[href$="user-new.php"]' ); |
| 189 |
|
| 190 |
$( document ).on( 'click', '#admin-add-emoji', function () { |
| 191 |
$( '#friends-reaction-picker' ).toggle(); |
| 192 |
return false; |
| 193 |
} ); |
| 194 |
|
| 195 |
$( document ).on( 'click', '.delete-emoji', function () { |
| 196 |
$( this ).closest( 'li' ).remove(); |
| 197 |
return false; |
| 198 |
} ); |
| 199 |
|
| 200 |
$( document ).on( 'click', '.delete-feed', function () { |
| 201 |
// eslint-disable-next-line no-alert |
| 202 |
if ( window.confirm( friends.delete_feed_question ) ) { |
| 203 |
$( this ).closest( 'li' ).remove(); |
| 204 |
} |
| 205 |
return false; |
| 206 |
} ); |
| 207 |
|
| 208 |
$( '#friends-reaction-picker' ).on( 'click', 'button', function () { |
| 209 |
const id = $( this ).data( 'emoji' ); |
| 210 |
if ( $( '#emoji-' + id ).length ) { |
| 211 |
return; |
| 212 |
} |
| 213 |
$( '#available-emojis' ).append( |
| 214 |
$( '#available-emojis-template' ) |
| 215 |
.html() |
| 216 |
.replace( /%1\$s/g, id ) |
| 217 |
.replace( /%2\$s/g, $( this ).html() ) |
| 218 |
); |
| 219 |
return false; |
| 220 |
} ); |
| 221 |
|
| 222 |
$( document ).on( 'click', '#admin-add-keyword', function () { |
| 223 |
$( '#keyword-notifications' ).append( |
| 224 |
'<li>' + |
| 225 |
$( '#keyword-template' ) |
| 226 |
.html() |
| 227 |
.replace( |
| 228 |
/\[0\]/g, |
| 229 |
'[' + $( '#keyword-notifications li' ).length + ']' |
| 230 |
) |
| 231 |
); |
| 232 |
$( '#keyword-notifications input:last ' ).focus(); |
| 233 |
return false; |
| 234 |
} ); |
| 235 |
|
| 236 |
$( document ).on( 'click', '#friends-bulk-publish', function () { |
| 237 |
$( this ) |
| 238 |
.closest( 'div' ) |
| 239 |
.find( 'select option[value=publish]' ) |
| 240 |
.prop( 'selected', true ); |
| 241 |
} ); |
| 242 |
|
| 243 |
$( document ).on( |
| 244 |
'click', |
| 245 |
'a.friends-auth-link, button.comments.friends-auth-link', |
| 246 |
function () { |
| 247 |
const $this = jQuery( this ); |
| 248 |
const token = $this.data( 'token' ); |
| 249 |
|
| 250 |
if ( ! token ) { |
| 251 |
return; |
| 252 |
} |
| 253 |
let href = $this.attr( 'href' ); |
| 254 |
|
| 255 |
const parts = token.split( /-/ ); |
| 256 |
const now = new Date(); |
| 257 |
if ( now / 1000 > parts[ 1 ] ) { |
| 258 |
wp.ajax |
| 259 |
.post( 'friends_refresh_link_token', { |
| 260 |
_ajax_nonce: $this.data( 'nonce' ), |
| 261 |
friend: $this.data( 'friend' ), |
| 262 |
url: href, |
| 263 |
} ) |
| 264 |
.done( function ( response ) { |
| 265 |
if ( response.data ) { |
| 266 |
$this.data( 'token', response.data.token ); |
| 267 |
} |
| 268 |
} ); |
| 269 |
|
| 270 |
// eslint-disable-next-line no-alert |
| 271 |
window.alert( friends.text_link_expired ); |
| 272 |
return false; |
| 273 |
} |
| 274 |
|
| 275 |
if ( href && href.indexOf( 'friend_auth=' ) < 0 ) { |
| 276 |
let hash = href.indexOf( '#' ); |
| 277 |
if ( hash >= 0 ) { |
| 278 |
hash = href.substr( hash ); |
| 279 |
href = href.substr( 0, href.length - hash.length ); |
| 280 |
} else { |
| 281 |
hash = ''; |
| 282 |
} |
| 283 |
|
| 284 |
if ( href.indexOf( '?' ) >= 0 ) { |
| 285 |
href += '&'; |
| 286 |
} else { |
| 287 |
href += '?'; |
| 288 |
} |
| 289 |
href += 'friend_auth=' + token + hash; |
| 290 |
$this.attr( 'href', href ); |
| 291 |
} |
| 292 |
|
| 293 |
if ( $this.is( 'button' ) ) { |
| 294 |
window.location.href = href; |
| 295 |
return false; |
| 296 |
} |
| 297 |
} |
| 298 |
); |
| 299 |
|
| 300 |
$( document ).on( 'click', 'a.set-avatar', function () { |
| 301 |
setAvatarUrl( |
| 302 |
$( this ).find( 'img' ).prop( 'src' ), |
| 303 |
$( this ).data( 'id' ), |
| 304 |
$( this ).data( 'nonce' ) |
| 305 |
); |
| 306 |
return false; |
| 307 |
} ); |
| 308 |
|
| 309 |
const setAvatarUrl = function ( url, user, nonce ) { |
| 310 |
if ( ! url || ! url.match( /^https?:\/\// ) ) { |
| 311 |
return; |
| 312 |
} |
| 313 |
$.post( |
| 314 |
friends.ajax_url, |
| 315 |
{ |
| 316 |
user, |
| 317 |
avatar: url, |
| 318 |
_ajax_nonce: nonce, |
| 319 |
action: 'friends_set_avatar', |
| 320 |
}, |
| 321 |
function ( response ) { |
| 322 |
if ( response.success ) { |
| 323 |
window.location.reload(); |
| 324 |
} else { |
| 325 |
$( '#friend-avatar-setting p.description' ).text( |
| 326 |
'⚠️ ' + response.data |
| 327 |
); |
| 328 |
} |
| 329 |
} |
| 330 |
); |
| 331 |
}; |
| 332 |
|
| 333 |
$( document ).on( 'click', 'button#set-avatar-url', function () { |
| 334 |
const url = $( '#new-avatar-url' ); |
| 335 |
setAvatarUrl( url.val(), url.data( 'id' ), url.data( 'nonce' ) ); |
| 336 |
return false; |
| 337 |
} ); |
| 338 |
|
| 339 |
$( document ).on( 'keyup', 'input#new-avatar-url', function ( e ) { |
| 340 |
const url = $( '#new-avatar-url' ); |
| 341 |
if ( e.keyCode === 13 ) { |
| 342 |
setAvatarUrl( url.val(), url.data( 'id' ), url.data( 'nonce' ) ); |
| 343 |
return false; |
| 344 |
} |
| 345 |
} ); |
| 346 |
|
| 347 |
setTimeout( function () { |
| 348 |
if ( $( '#fetch-feeds' ).length ) { |
| 349 |
$( '#fetch-feeds' ).append( ' <i class="friends-loading"></i>' ); |
| 350 |
$.post( |
| 351 |
friends.ajax_url, |
| 352 |
{ |
| 353 |
friend: $( '#fetch-feeds' ).data( 'friend' ), |
| 354 |
_ajax_nonce: $( '#fetch-feeds' ).data( 'nonce' ), |
| 355 |
action: 'friends_fetch_feeds', |
| 356 |
}, |
| 357 |
function ( response ) { |
| 358 |
if ( response.success ) { |
| 359 |
$( '#fetch-feeds i' ) |
| 360 |
.removeClass( 'friends-loading' ) |
| 361 |
.addClass( 'dashicons dashicons-saved' ); |
| 362 |
} else { |
| 363 |
$( '#fetch-feeds i' ) |
| 364 |
.removeClass( 'friends-loading' ) |
| 365 |
.addClass( 'dashicons dashicons-warning' ) |
| 366 |
.prop( 'title', response.data ); |
| 367 |
} |
| 368 |
} |
| 369 |
); |
| 370 |
} |
| 371 |
}, 500 ); |
| 372 |
} ); |
| 373 |
|