2013-09-11 13:27:11 +00:00
/ * *
2017-05-19 22:12:53 +00:00
* DO NOT EDIT THIS FILE .
* See the following change record for more information ,
2017-05-23 14:30:14 +00:00
* https : //www.drupal.org/node/2815083
2017-05-19 22:12:53 +00:00
* @ preserve
* * /
2015-06-05 20:17:55 +00:00
2016-02-29 03:25:57 +00:00
( function ( $ , Drupal , drupalSettings ) {
2014-01-27 21:41:32 +00:00
function hide ( $placeholder ) {
2017-05-19 22:12:53 +00:00
return $placeholder . closest ( '.comment-new-comments' ) . prev ( ) . addClass ( 'last' ) . end ( ) . hide ( ) ;
2013-09-11 13:27:11 +00:00
}
2014-01-27 21:41:32 +00:00
function remove ( $placeholder ) {
hide ( $placeholder ) . remove ( ) ;
}
2013-09-11 13:27:11 +00:00
2014-01-27 21:41:32 +00:00
function show ( $placeholder ) {
2017-05-19 22:12:53 +00:00
return $placeholder . closest ( '.comment-new-comments' ) . prev ( ) . removeClass ( 'last' ) . end ( ) . show ( ) ;
2014-01-27 21:41:32 +00:00
}
2013-09-11 13:27:11 +00:00
2021-08-10 10:05:02 +00:00
function processNodeNewCommentLinks ( placeholders ) {
2021-12-18 06:12:16 +00:00
const $placeholdersToUpdate = { } ;
let fieldName = 'comment' ;
let $placeholder ;
placeholders . forEach ( placeholder => {
2014-01-27 21:41:32 +00:00
$placeholder = $ ( placeholder ) ;
2021-12-18 06:12:16 +00:00
const timestamp = parseInt ( $placeholder . attr ( 'data-history-node-last-comment-timestamp' ) , 10 ) ;
2014-01-27 21:41:32 +00:00
fieldName = $placeholder . attr ( 'data-history-node-field-name' ) ;
2021-12-18 06:12:16 +00:00
const nodeID = $placeholder . closest ( '[data-history-node-id]' ) . attr ( 'data-history-node-id' ) ;
const lastViewTimestamp = Drupal . history . getLastRead ( nodeID ) ;
2013-09-11 13:27:11 +00:00
2014-01-27 21:41:32 +00:00
if ( timestamp > lastViewTimestamp ) {
$placeholdersToUpdate [ nodeID ] = $placeholder ;
2017-05-19 22:12:53 +00:00
} else {
2021-10-27 20:02:41 +00:00
remove ( $placeholder ) ;
}
2014-01-27 21:41:32 +00:00
} ) ;
2021-12-18 06:12:16 +00:00
const nodeIDs = Object . keys ( $placeholdersToUpdate ) ;
2020-01-30 09:08:38 +00:00
2014-01-27 21:41:32 +00:00
if ( nodeIDs . length === 0 ) {
return ;
2013-09-11 13:27:11 +00:00
}
2014-01-27 21:41:32 +00:00
function render ( results ) {
2021-12-18 06:12:16 +00:00
Object . keys ( results || { } ) . forEach ( nodeID => {
2018-01-03 23:25:46 +00:00
if ( $placeholdersToUpdate . hasOwnProperty ( nodeID ) ) {
2017-05-19 22:12:53 +00:00
$placeholdersToUpdate [ nodeID ] . attr ( 'href' , results [ nodeID ] . first _new _comment _link ) . text ( Drupal . formatPlural ( results [ nodeID ] . new _comment _count , '1 new comment' , '@count new comments' ) ) . removeClass ( 'hidden' ) ;
2014-01-27 21:41:32 +00:00
show ( $placeholdersToUpdate [ nodeID ] ) ;
}
2018-01-03 23:25:46 +00:00
} ) ;
2013-09-11 13:27:11 +00:00
}
2014-07-02 18:48:06 +00:00
2014-01-27 21:41:32 +00:00
if ( drupalSettings . comment && drupalSettings . comment . newCommentsLinks ) {
render ( drupalSettings . comment . newCommentsLinks . node [ fieldName ] ) ;
2017-05-19 22:12:53 +00:00
} else {
2014-01-27 21:41:32 +00:00
$ . ajax ( {
url : Drupal . url ( 'comments/render_new_comments_node_links' ) ,
type : 'POST' ,
2020-01-30 09:08:38 +00:00
data : {
'node_ids[]' : nodeIDs ,
field _name : fieldName
} ,
2014-01-27 21:41:32 +00:00
dataType : 'json' ,
success : render
} ) ;
}
2014-01-16 13:18:41 +00:00
}
2018-03-05 09:14:08 +00:00
Drupal . behaviors . nodeNewCommentsLink = {
2021-12-18 06:12:16 +00:00
attach ( context ) {
const nodeIDs = [ ] ;
const placeholders = once ( 'history' , '[data-history-node-last-comment-timestamp]' , context ) . filter ( placeholder => {
const $placeholder = $ ( placeholder ) ;
const lastCommentTimestamp = parseInt ( $placeholder . attr ( 'data-history-node-last-comment-timestamp' ) , 10 ) ;
const nodeID = $placeholder . closest ( '[data-history-node-id]' ) . attr ( 'data-history-node-id' ) ;
2020-01-30 09:08:38 +00:00
2018-03-05 09:14:08 +00:00
if ( Drupal . history . needsServerCheck ( nodeID , lastCommentTimestamp ) ) {
nodeIDs . push ( nodeID ) ;
hide ( $placeholder ) ;
return true ;
}
remove ( $placeholder ) ;
return false ;
} ) ;
2021-08-10 10:05:02 +00:00
if ( placeholders . length === 0 ) {
2018-03-05 09:14:08 +00:00
return ;
}
2021-12-18 06:12:16 +00:00
Drupal . history . fetchTimestamps ( nodeIDs , ( ) => {
2021-08-10 10:05:02 +00:00
processNodeNewCommentLinks ( placeholders ) ;
2018-03-05 09:14:08 +00:00
} ) ;
}
2021-12-18 06:12:16 +00:00
2018-03-05 09:14:08 +00:00
} ;
2017-05-19 22:12:53 +00:00
} ) ( jQuery , Drupal , drupalSettings ) ;