| 1 |
/* global jQuery */ |
| 2 |
( function ( $, wp, friends ) { |
| 3 |
const $document = $( document ); |
| 4 |
let alreadySearching = ''; |
| 5 |
let alreadyLoading = false; |
| 6 |
const bottomOffsetLoadNext = 2000; |
| 7 |
let searchResultFocused = false; |
| 8 |
|
| 9 |
wp = wp || { ajax: { send() {}, post() {} } }; |
| 10 |
|
| 11 |
$document.on( |
| 12 |
'keydown', |
| 13 |
'input#master-search, .form-autocomplete a', |
| 14 |
function ( e ) { |
| 15 |
const code = e.keyCode ? e.keyCode : e.which; |
| 16 |
const results = $( this ) |
| 17 |
.closest( '.form-autocomplete' ) |
| 18 |
.find( '.menu .menu-item a' ); |
| 19 |
if ( 40 === code ) { |
| 20 |
if ( false === searchResultFocused ) { |
| 21 |
searchResultFocused = 0; |
| 22 |
} else if ( searchResultFocused + 1 < results.length ) { |
| 23 |
searchResultFocused += 1; |
| 24 |
} |
| 25 |
} else if ( 38 === code ) { |
| 26 |
if ( false === searchResultFocused ) { |
| 27 |
searchResultFocused = -1; |
| 28 |
} else { |
| 29 |
searchResultFocused -= 1; |
| 30 |
if ( -1 === searchResultFocused ) { |
| 31 |
$( 'input#master-search' ).focus(); |
| 32 |
return false; |
| 33 |
} |
| 34 |
} |
| 35 |
} else { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
const el = results.get( searchResultFocused ); |
| 40 |
if ( el ) { |
| 41 |
el.focus(); |
| 42 |
} |
| 43 |
return false; |
| 44 |
} |
| 45 |
); |
| 46 |
|
| 47 |
$document.on( 'keyup', 'input#master-search', function () { |
| 48 |
const input = $( this ); |
| 49 |
const query = input.val().trim(); |
| 50 |
|
| 51 |
if ( alreadySearching === query ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
const menu = input.closest( '.form-autocomplete' ).find( '.menu' ); |
| 55 |
|
| 56 |
if ( ! query ) { |
| 57 |
menu.hide(); |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
const searchIndicator = input |
| 62 |
.closest( '.form-autocomplete-input' ) |
| 63 |
.find( '.form-icon' ); |
| 64 |
|
| 65 |
wp.ajax.send( 'friends-autocomplete', { |
| 66 |
data: { |
| 67 |
q: query, |
| 68 |
}, |
| 69 |
beforeSend() { |
| 70 |
searchIndicator.addClass( 'loading' ); |
| 71 |
alreadySearching = query; |
| 72 |
}, |
| 73 |
success( results ) { |
| 74 |
searchIndicator.removeClass( 'loading' ); |
| 75 |
searchResultFocused = false; |
| 76 |
if ( results ) { |
| 77 |
menu.html( results ).show(); |
| 78 |
} else { |
| 79 |
menu.hide(); |
| 80 |
} |
| 81 |
}, |
| 82 |
} ); |
| 83 |
} ); |
| 84 |
$document.on( |
| 85 |
'click', |
| 86 |
'a.friends-auth-link, button.comments.friends-auth-link', |
| 87 |
function () { |
| 88 |
const $this = jQuery( this ); |
| 89 |
const token = $this.data( 'token' ); |
| 90 |
|
| 91 |
if ( ! token ) { |
| 92 |
return; |
| 93 |
} |
| 94 |
let href = $this.attr( 'href' ); |
| 95 |
|
| 96 |
const parts = token.split( /-/ ); |
| 97 |
const now = new Date(); |
| 98 |
if ( now / 1000 > parts[ 1 ] ) { |
| 99 |
wp.ajax |
| 100 |
.post( 'friends_refresh_link_token', { |
| 101 |
_ajax_nonce: $this.data( 'nonce' ), |
| 102 |
friend: $this.data( 'friend' ), |
| 103 |
url: href, |
| 104 |
} ) |
| 105 |
.done( function ( response ) { |
| 106 |
if ( response.data ) { |
| 107 |
$this.data( 'token', response.data.token ); |
| 108 |
} |
| 109 |
} ); |
| 110 |
|
| 111 |
// eslint-disable-next-line no-alert |
| 112 |
window.alert( friends.text_link_expired ); |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
if ( href && href.indexOf( 'friend_auth=' ) < 0 ) { |
| 117 |
let hash = href.indexOf( '#' ); |
| 118 |
if ( hash >= 0 ) { |
| 119 |
hash = href.substr( hash ); |
| 120 |
href = href.substr( 0, href.length - hash.length ); |
| 121 |
} else { |
| 122 |
hash = ''; |
| 123 |
} |
| 124 |
|
| 125 |
if ( href.indexOf( '?' ) >= 0 ) { |
| 126 |
href += '&'; |
| 127 |
} else { |
| 128 |
href += '?'; |
| 129 |
} |
| 130 |
href += 'friend_auth=' + token + hash; |
| 131 |
$this.attr( 'href', href ); |
| 132 |
} |
| 133 |
|
| 134 |
if ( $this.is( 'button' ) ) { |
| 135 |
window.location.href = href; |
| 136 |
return false; |
| 137 |
} |
| 138 |
} |
| 139 |
); |
| 140 |
|
| 141 |
$( function () { |
| 142 |
$( '#only_subscribe' ).on( 'change', function () { |
| 143 |
$( this ) |
| 144 |
.closest( 'form' ) |
| 145 |
.find( '#submit' ) |
| 146 |
.text( this.checked ? '1' : '2' ); |
| 147 |
} ); |
| 148 |
$( '#post-format-switcher select' ).on( 'change', function () { |
| 149 |
const re = new RegExp( '/type/[^/$]+' ), |
| 150 |
v = $( this ).val(); |
| 151 |
let url; |
| 152 |
const urlPart = v ? '/type/' + v + '/' : '/'; |
| 153 |
if ( window.location.href.match( re ) ) { |
| 154 |
url = window.location.href.replace( re, urlPart ); |
| 155 |
} else { |
| 156 |
url = window.location.href.replace( /\/$/, '' ) + urlPart; |
| 157 |
} |
| 158 |
window.location.href = url; |
| 159 |
} ); |
| 160 |
} ); |
| 161 |
|
| 162 |
$document.on( 'click', 'a.friends-trash-post', function () { |
| 163 |
const button = $( this ); |
| 164 |
|
| 165 |
wp.ajax.send( 'trash-post', { |
| 166 |
data: { |
| 167 |
_ajax_nonce: button.data( 'trash-nonce' ), |
| 168 |
id: button.data( 'id' ), |
| 169 |
}, |
| 170 |
success() { |
| 171 |
button |
| 172 |
.text( friends.text_undo ) |
| 173 |
.attr( 'class', 'friends-untrash-post' ); |
| 174 |
}, |
| 175 |
} ); |
| 176 |
return false; |
| 177 |
} ); |
| 178 |
|
| 179 |
$document.on( 'click', 'a.friends-untrash-post', function () { |
| 180 |
const button = $( this ); |
| 181 |
|
| 182 |
wp.ajax.send( 'untrash-post', { |
| 183 |
data: { |
| 184 |
_ajax_nonce: button.data( 'untrash-nonce' ), |
| 185 |
id: button.data( 'id' ), |
| 186 |
}, |
| 187 |
success() { |
| 188 |
button |
| 189 |
.text( friends.text_trash_post ) |
| 190 |
.attr( 'class', 'friends-trash-post' ); |
| 191 |
}, |
| 192 |
} ); |
| 193 |
return false; |
| 194 |
} ); |
| 195 |
|
| 196 |
$document.on( 'click', 'a.collapse-post', function ( e ) { |
| 197 |
if ( e.metaKey || e.altKey || e.shiftKey ) { |
| 198 |
$( this ).trigger( 'dblclick' ); |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
const card = $( this ).closest( 'article' ); |
| 203 |
let collapsed; |
| 204 |
if ( card.closest( 'section.all-collapsed' ).length ) { |
| 205 |
card.toggleClass( 'uncollapsed' ); |
| 206 |
collapsed = ! card.is( '.uncollapsed' ); |
| 207 |
} else { |
| 208 |
card.toggleClass( 'collapsed' ); |
| 209 |
collapsed = card.is( '.collapsed' ); |
| 210 |
} |
| 211 |
if ( collapsed ) { |
| 212 |
$( this ) |
| 213 |
.find( 'i' ) |
| 214 |
.removeClass( 'dashicons-fullscreen-exit-alt' ) |
| 215 |
.addClass( 'dashicons-fullscreen-alt' ); |
| 216 |
} else { |
| 217 |
$( this ) |
| 218 |
.find( 'i' ) |
| 219 |
.removeClass( 'dashicons-fullscreen-exit-alt' ) |
| 220 |
.addClass( 'dashicons-fullscreen-alt' ); |
| 221 |
} |
| 222 |
|
| 223 |
return false; |
| 224 |
} ); |
| 225 |
|
| 226 |
$document.on( 'dblclick', 'a.collapse-post', function () { |
| 227 |
// Collapse-toggle all visible. |
| 228 |
$( this ).closest( 'section' ).toggleClass( 'all-collapsed' ); |
| 229 |
$( this )[ 0 ].scrollIntoView(); |
| 230 |
$( window ).trigger( 'scroll' ); |
| 231 |
return false; |
| 232 |
} ); |
| 233 |
|
| 234 |
$document.on( 'click', 'article a.comments', function ( e ) { |
| 235 |
if ( e.metaKey || e.altKey || e.shiftKey ) { |
| 236 |
return; |
| 237 |
} |
| 238 |
|
| 239 |
const $this = $( this ); |
| 240 |
const content = $this.closest( 'article' ).find( '.comments-content' ); |
| 241 |
if ( content.data( 'loaded' ) ) { |
| 242 |
content.toggle(); |
| 243 |
} else { |
| 244 |
content.show(); |
| 245 |
wp.ajax.send( 'friends-load-comments', { |
| 246 |
data: { |
| 247 |
_ajax_nonce: $this.data( 'cnonce' ), |
| 248 |
post_id: $this.data( 'id' ), |
| 249 |
}, |
| 250 |
success( comments ) { |
| 251 |
content.html( comments ).data( 'loaded', true ); |
| 252 |
}, |
| 253 |
error( message ) { |
| 254 |
content.html( message ); |
| 255 |
}, |
| 256 |
} ); |
| 257 |
} |
| 258 |
return false; |
| 259 |
} ); |
| 260 |
|
| 261 |
$document.on( 'change', 'select.friends-change-post-format', function () { |
| 262 |
const select = $( this ); |
| 263 |
|
| 264 |
wp.ajax.send( 'friends-change-post-format', { |
| 265 |
data: { |
| 266 |
_ajax_nonce: select.data( 'change-post-format-nonce' ), |
| 267 |
id: select.data( 'id' ), |
| 268 |
format: select.val(), |
| 269 |
}, |
| 270 |
success() { |
| 271 |
// TODO: add some visual indication. |
| 272 |
}, |
| 273 |
} ); |
| 274 |
return false; |
| 275 |
} ); |
| 276 |
|
| 277 |
$( window ).scroll( function () { |
| 278 |
if ( |
| 279 |
$( document ).scrollTop() < |
| 280 |
$( document ).height() - bottomOffsetLoadNext |
| 281 |
) { |
| 282 |
return; |
| 283 |
} |
| 284 |
nextPage(); |
| 285 |
} ); |
| 286 |
|
| 287 |
$( document ).on( 'click', '.nav-previous', function () { |
| 288 |
nextPage(); |
| 289 |
return false; |
| 290 |
} ); |
| 291 |
|
| 292 |
function nextPage() { |
| 293 |
if ( alreadyLoading ) { |
| 294 |
return; |
| 295 |
} |
| 296 |
|
| 297 |
if ( friends.current_page >= friends.max_page ) { |
| 298 |
return; |
| 299 |
} |
| 300 |
|
| 301 |
wp.ajax.send( 'friends-load-next-page', { |
| 302 |
data: { |
| 303 |
query_vars: friends.query_vars, |
| 304 |
page: friends.current_page, |
| 305 |
qv_sign: friends.qv_sign, |
| 306 |
}, |
| 307 |
beforeSend() { |
| 308 |
$( 'section.posts .posts-navigation .nav-previous' ).addClass( |
| 309 |
'loading' |
| 310 |
); |
| 311 |
alreadyLoading = true; |
| 312 |
}, |
| 313 |
success( newPosts ) { |
| 314 |
$( |
| 315 |
'section.posts .posts-navigation .nav-previous' |
| 316 |
).removeClass( 'loading' ); |
| 317 |
if ( newPosts ) { |
| 318 |
$( 'section.posts' ) |
| 319 |
.find( 'article:last-of-type' ) |
| 320 |
.after( newPosts ); |
| 321 |
if ( |
| 322 |
++friends.current_page < friends.max_page && |
| 323 |
/<article/.test( newPosts ) |
| 324 |
) { |
| 325 |
alreadyLoading = false; |
| 326 |
} else { |
| 327 |
$( 'section.posts .posts-navigation' ).text( |
| 328 |
friends.text_no_more_posts |
| 329 |
); |
| 330 |
} |
| 331 |
} |
| 332 |
}, |
| 333 |
} ); |
| 334 |
} |
| 335 |
|
| 336 |
/* Reactions */ |
| 337 |
$( function () { |
| 338 |
$document.on( 'click', 'button.friends-reaction', function () { |
| 339 |
wp.ajax.send( 'friends-toggle-react', { |
| 340 |
data: { |
| 341 |
_ajax_nonce: $( this ).data( 'nonce' ), |
| 342 |
post_id: $( this ).data( 'id' ), |
| 343 |
reaction: $( this ).data( 'emoji' ), |
| 344 |
}, |
| 345 |
success() { |
| 346 |
window.location.reload(); |
| 347 |
}, |
| 348 |
} ); |
| 349 |
return false; |
| 350 |
} ); |
| 351 |
|
| 352 |
$( '.friends-reaction-picker' ).on( 'click', 'button', function () { |
| 353 |
wp.ajax.send( 'friends-toggle-react', { |
| 354 |
data: { |
| 355 |
_ajax_nonce: $( this ) |
| 356 |
.closest( '.friends-reaction-picker' ) |
| 357 |
.data( 'nonce' ), |
| 358 |
post_id: $( this ) |
| 359 |
.closest( '.friends-reaction-picker' ) |
| 360 |
.data( 'id' ), |
| 361 |
reaction: $( this ).data( 'emoji' ), |
| 362 |
}, |
| 363 |
success() { |
| 364 |
window.location.reload(); |
| 365 |
}, |
| 366 |
} ); |
| 367 |
return false; |
| 368 |
} ); |
| 369 |
} ); |
| 370 |
|
| 371 |
/* ActivityPub */ |
| 372 |
$( function () { |
| 373 |
$document.on( 'click', 'a.friends-reblog', function () { |
| 374 |
const $this = $( this ); |
| 375 |
wp.ajax.send( 'friends-reblog', { |
| 376 |
data: { |
| 377 |
_ajax_nonce: $this.data( 'nonce' ), |
| 378 |
post_id: $this.data( 'id' ), |
| 379 |
}, |
| 380 |
success() { |
| 381 |
$this |
| 382 |
.find( 'i.friends-reblog-status' ) |
| 383 |
.addClass( 'dashicons dashicons-saved' ); |
| 384 |
}, |
| 385 |
} ); |
| 386 |
return false; |
| 387 |
} ); |
| 388 |
} ); |
| 389 |
|
| 390 |
$document.on( 'click', 'a.send-new-message', function () { |
| 391 |
$( '#friends-send-new-message' ) |
| 392 |
.toggle() |
| 393 |
.find( |
| 394 |
'textarea:visible, .block-editor-default-block-appender__content' |
| 395 |
) |
| 396 |
.focus(); |
| 397 |
return false; |
| 398 |
} ); |
| 399 |
|
| 400 |
$document.on( 'click', 'button.delete-conversation', function () { |
| 401 |
// eslint-disable-next-line no-alert |
| 402 |
return window.confirm( friends.text_del_convers ); |
| 403 |
} ); |
| 404 |
|
| 405 |
$document.on( 'click', 'a.display-message', function () { |
| 406 |
const $this = $( this ).closest( 'div' ); |
| 407 |
const conversation = $this.find( 'div.conversation' ); |
| 408 |
if ( conversation.is( ':visible' ) ) { |
| 409 |
conversation.hide(); |
| 410 |
} else { |
| 411 |
conversation.show(); |
| 412 |
const messages = conversation.find( '.messages' ).get( 0 ); |
| 413 |
messages.scrollTop = messages.scrollHeight; |
| 414 |
$this.find( 'a.display-message' ).removeClass( 'unread' ); |
| 415 |
wp.ajax.send( 'friends-mark-read', { |
| 416 |
data: { |
| 417 |
_ajax_nonce: $this.data( 'nonce' ), |
| 418 |
post_id: $this.data( 'id' ), |
| 419 |
}, |
| 420 |
success() {}, |
| 421 |
} ); |
| 422 |
conversation |
| 423 |
.find( |
| 424 |
'textarea:visible, .block-editor-default-block-appender__content' |
| 425 |
) |
| 426 |
.focus(); |
| 427 |
} |
| 428 |
|
| 429 |
return false; |
| 430 |
} ); |
| 431 |
|
| 432 |
$document.on( 'mouseenter', 'h2#page-title a.dashicons', function () { |
| 433 |
if ( $( this ).hasClass( 'not-starred' ) ) { |
| 434 |
if ( $( this ).hasClass( 'dashicons-star-empty' ) ) { |
| 435 |
$( this ) |
| 436 |
.removeClass( 'dashicons-star-empty' ) |
| 437 |
.addClass( 'dashicons-star-filled' ); |
| 438 |
} |
| 439 |
} else if ( $( this ).hasClass( 'starred' ) ) { |
| 440 |
if ( $( this ).hasClass( 'dashicons-star-filled' ) ) { |
| 441 |
$( this ) |
| 442 |
.removeClass( 'dashicons-star-filled' ) |
| 443 |
.addClass( 'dashicons-star-empty' ); |
| 444 |
} |
| 445 |
} |
| 446 |
} ); |
| 447 |
|
| 448 |
$document.on( 'mouseleave', 'h2#page-title a.dashicons', function () { |
| 449 |
if ( $( this ).hasClass( 'not-starred' ) ) { |
| 450 |
if ( $( this ).hasClass( 'dashicons-star-filled' ) ) { |
| 451 |
$( this ) |
| 452 |
.removeClass( 'dashicons-star-filled' ) |
| 453 |
.addClass( 'dashicons-star-empty' ); |
| 454 |
} |
| 455 |
} else if ( $( this ).hasClass( 'starred' ) ) { |
| 456 |
if ( $( this ).hasClass( 'dashicons-star-empty' ) ) { |
| 457 |
$( this ) |
| 458 |
.removeClass( 'dashicons-star-empty' ) |
| 459 |
.addClass( 'dashicons-star-filled' ); |
| 460 |
} |
| 461 |
} |
| 462 |
} ); |
| 463 |
|
| 464 |
$document.on( |
| 465 |
'click', |
| 466 |
'h2#page-title a.dashicons.starred, h2#page-title a.dashicons.not-starred', |
| 467 |
function () { |
| 468 |
let removeClass = 'dashicons-star-filled starred'; |
| 469 |
let addClass = 'dashicons-star-empty not-starred'; |
| 470 |
const $this = $( this ); |
| 471 |
if ( $this.hasClass( 'not-starred' ) ) { |
| 472 |
const s = removeClass; |
| 473 |
removeClass = addClass; |
| 474 |
addClass = s; |
| 475 |
} |
| 476 |
wp.ajax.send( 'friends-star', { |
| 477 |
data: { |
| 478 |
friend_id: $this.data( 'id' ), |
| 479 |
_ajax_nonce: $this.data( 'nonce' ), |
| 480 |
starred: $this.hasClass( 'starred' ) ? 0 : 1, |
| 481 |
}, |
| 482 |
beforeSend() { |
| 483 |
$this |
| 484 |
.removeClass( |
| 485 |
removeClass + |
| 486 |
' dashicons-star-filled dashicons-star-empty' |
| 487 |
) |
| 488 |
.addClass( 'form-icon loading' ); |
| 489 |
}, |
| 490 |
success() { |
| 491 |
$this |
| 492 |
.removeClass( 'form-icon loading' ) |
| 493 |
.addClass( addClass ); |
| 494 |
}, |
| 495 |
} ); |
| 496 |
return false; |
| 497 |
} |
| 498 |
); |
| 499 |
|
| 500 |
function getAcct( href ) { |
| 501 |
const url = new URL( href ); |
| 502 |
const username = url.pathname.replace( /\/$/, '' ).split( '/' ).pop(); |
| 503 |
return '@' + username.replace( /^@/, '' ) + '@' + url.hostname; |
| 504 |
} |
| 505 |
|
| 506 |
function insertTextInGutenberg( text ) { |
| 507 |
const element = jQuery( '.is-root-container p' )[ 0 ]; |
| 508 |
const len = element.textContent.length; |
| 509 |
element.focus(); |
| 510 |
window.getSelection().collapse( element.firstChild, len ); |
| 511 |
document.execCommand( 'insertText', false, text + ' ' ); |
| 512 |
} |
| 513 |
|
| 514 |
$document.on( 'click', '#in_reply_to_preview a', function () { |
| 515 |
if ( ! $( this ).hasClass( 'mention' ) ) { |
| 516 |
return true; |
| 517 |
} |
| 518 |
insertTextInGutenberg( getAcct( $( this ).attr( 'href' ) ) ); |
| 519 |
return false; |
| 520 |
} ); |
| 521 |
|
| 522 |
$document.on( 'keyup', 'input#friends_in_reply_to', function () { |
| 523 |
const input = $( this ); |
| 524 |
const url = input.val().trim(); |
| 525 |
|
| 526 |
if ( alreadySearching === url ) { |
| 527 |
return; |
| 528 |
} |
| 529 |
const searchIndicator = input |
| 530 |
.closest( '.form-autocomplete-input' ) |
| 531 |
.find( '.form-icon' ); |
| 532 |
|
| 533 |
wp.ajax.send( 'friends-in-reply-to-preview', { |
| 534 |
data: { |
| 535 |
url, |
| 536 |
}, |
| 537 |
beforeSend() { |
| 538 |
searchIndicator.addClass( 'loading' ); |
| 539 |
alreadySearching = url; |
| 540 |
}, |
| 541 |
success( results ) { |
| 542 |
searchIndicator.removeClass( 'loading' ); |
| 543 |
if ( results ) { |
| 544 |
$( '#in_reply_to_preview' ).html( results.html ).show(); |
| 545 |
insertTextInGutenberg( getAcct( results.author ) ); |
| 546 |
} else { |
| 547 |
$( '#in_reply_to_preview' ).hide(); |
| 548 |
} |
| 549 |
}, |
| 550 |
} ); |
| 551 |
} ); |
| 552 |
|
| 553 |
$document.on( 'click', '.quick-reply', function () { |
| 554 |
$( '#quick-post-panel' ).addClass( 'open' ); |
| 555 |
$( '#quick-post-panel input#friends_in_reply_to' ) |
| 556 |
.val( $( this ).data( 'url' ) ) |
| 557 |
.trigger( 'keyup' ); |
| 558 |
|
| 559 |
$( '#quick-post-panel' )[ 0 ].scrollIntoView(); |
| 560 |
return false; |
| 561 |
} ); |
| 562 |
|
| 563 |
$document.on( 'click', '.quick-post-panel-toggle', function () { |
| 564 |
$( '#quick-post-panel' ).toggleClass( 'open' ); |
| 565 |
return false; |
| 566 |
} ); |
| 567 |
} )( jQuery, window.wp, window.friends ); |
| 568 |
|