2014-03-17 21:23:30 -04:00
'use strict' ;
2014-03-28 13:02:13 -04:00
/* globals define, app, translator, ajaxify, socket, bootbox */
2014-03-17 21:23:30 -04:00
2014-05-29 17:24:38 -04:00
define ( 'forum/topic/threadTools' , [ 'forum/topic/fork' , 'forum/topic/move' ] , function ( fork , move ) {
2014-03-17 21:23:30 -04:00
var ThreadTools = { } ;
ThreadTools . init = function ( tid , threadState ) {
2014-05-03 20:56:26 -04:00
ThreadTools . threadState = threadState ;
2014-06-10 16:56:55 -04:00
if ( threadState . locked ) {
2014-05-03 20:56:26 -04:00
ThreadTools . setLockedState ( { tid : tid , isLocked : true } ) ;
}
2014-06-10 16:56:55 -04:00
if ( threadState . deleted ) {
2014-05-03 20:56:26 -04:00
ThreadTools . setDeleteState ( { tid : tid , isDelete : true } ) ;
}
2014-06-10 16:56:55 -04:00
if ( threadState . pinned ) {
2014-05-03 20:56:26 -04:00
ThreadTools . setPinnedState ( { tid : tid , isPinned : true } ) ;
}
2014-03-17 21:23:30 -04:00
2014-06-10 16:56:55 -04:00
$ ( '.delete_thread' ) . on ( 'click' , function ( ) {
topicCommand ( threadState . deleted ? 'restore' : 'delete' , tid ) ;
return false ;
} ) ;
2014-03-17 21:23:30 -04:00
2014-06-10 16:56:55 -04:00
$ ( '.purge_thread' ) . on ( 'click' , function ( ) {
topicCommand ( 'purge' , tid ) ;
2014-05-15 10:38:02 -04:00
return false ;
} ) ;
2014-03-17 21:23:30 -04:00
2014-06-10 16:56:55 -04:00
$ ( '.lock_thread' ) . on ( 'click' , function ( ) {
2014-06-22 17:26:43 -04:00
socket . emit ( threadState . locked ? 'topics.unlock' : 'topics.lock' , { tids : [ tid ] , cid : ajaxify . variables . get ( 'category_id' ) } ) ;
2014-05-15 10:38:02 -04:00
return false ;
} ) ;
2014-03-18 16:46:07 -04:00
2014-06-10 16:56:55 -04:00
$ ( '.pin_thread' ) . on ( 'click' , function ( ) {
2014-06-22 17:26:43 -04:00
socket . emit ( threadState . pinned ? 'topics.unpin' : 'topics.pin' , { tids : [ tid ] , cid : ajaxify . variables . get ( 'category_id' ) } ) ;
2014-05-15 10:38:02 -04:00
return false ;
} ) ;
2014-03-18 16:46:07 -04:00
2014-05-15 10:38:02 -04:00
$ ( '.markAsUnreadForAll' ) . on ( 'click' , function ( ) {
var btn = $ ( this ) ;
socket . emit ( 'topics.markAsUnreadForAll' , [ tid ] , function ( err ) {
if ( err ) {
return app . alertError ( err . message ) ;
}
app . alertSuccess ( '[[topic:markAsUnreadForAll.success]]' ) ;
btn . parents ( '.thread-tools.open' ) . find ( '.dropdown-toggle' ) . trigger ( 'click' ) ;
2014-04-17 20:07:23 -04:00
} ) ;
2014-05-15 10:38:02 -04:00
return false ;
} ) ;
2014-03-18 16:46:07 -04:00
2014-05-15 10:38:02 -04:00
$ ( '.move_thread' ) . on ( 'click' , function ( e ) {
move . init ( [ tid ] , ajaxify . variables . get ( 'category_id' ) ) ;
return false ;
} ) ;
fork . init ( ) ;
2014-03-18 16:46:07 -04:00
socket . emit ( 'topics.followCheck' , tid , function ( err , state ) {
2014-05-03 20:56:26 -04:00
setFollowState ( state ) ;
2014-03-17 21:23:30 -04:00
} ) ;
2014-03-18 16:46:07 -04:00
$ ( '.posts' ) . on ( 'click' , '.follow' , function ( ) {
socket . emit ( 'topics.follow' , tid , function ( err , state ) {
2014-03-17 21:23:30 -04:00
if ( err ) {
2014-03-18 16:46:07 -04:00
return app . alert ( {
type : 'danger' ,
alert _id : 'topic_follow' ,
title : '[[global:please_log_in]]' ,
message : '[[topic:login_to_subscribe]]' ,
timeout : 5000
} ) ;
2014-03-17 21:23:30 -04:00
}
2014-03-18 16:46:07 -04:00
2014-05-03 20:56:26 -04:00
setFollowState ( state ) ;
2014-06-21 11:51:59 -04:00
app . alert ( {
alert _id : 'follow_thread' ,
message : state ? '[[topic:following_topic.message]]' : '[[topic:not_following_topic.message]]' ,
type : 'success' ,
timeout : 5000
} ) ;
2014-03-17 21:23:30 -04:00
} ) ;
2014-03-18 16:46:07 -04:00
2014-03-17 21:23:30 -04:00
return false ;
} ) ;
2014-05-03 20:56:26 -04:00
} ;
2014-06-10 16:56:55 -04:00
function topicCommand ( command , tid ) {
translator . translate ( '[[topic:thread_tools.' + command + '_confirm]]' , function ( msg ) {
bootbox . confirm ( msg , function ( confirm ) {
if ( confirm ) {
2014-06-22 17:26:43 -04:00
socket . emit ( 'topics.' + command , { tids : [ tid ] , cid : ajaxify . variables . get ( 'category_id' ) } ) ;
2014-06-10 16:56:55 -04:00
}
} ) ;
} ) ;
}
2014-05-03 20:56:26 -04:00
ThreadTools . setLockedState = function ( data ) {
var threadEl = $ ( '#post-container' ) ;
if ( parseInt ( data . tid , 10 ) === parseInt ( threadEl . attr ( 'data-tid' ) , 10 ) ) {
2014-06-22 17:26:43 -04:00
var isLocked = data . isLocked && ! app . isAdmin ;
2014-05-03 20:56:26 -04:00
2014-07-05 16:58:57 -04:00
$ ( '.lock_thread' ) . translateHtml ( '<i class="fa fa-fw fa-' + ( data . isLocked ? 'un' : '' ) + 'lock"></i> [[topic:thread_tools.' + ( data . isLocked ? 'un' : '' ) + 'lock]]' ) ;
2014-03-17 21:23:30 -04:00
2014-06-24 14:09:12 -04:00
translator . translate ( isLocked ? '[[topic:locked]]' : '[[topic:reply]]' , function ( translated ) {
var className = isLocked ? 'fa-lock' : 'fa-reply' ;
threadEl . find ( '.post_reply' ) . html ( '<i class="fa ' + className + '"></i> ' + translated ) ;
$ ( '.topic-main-buttons .post_reply' ) . attr ( 'disabled' , isLocked ) . html ( isLocked ? '<i class="fa fa-lock"></i> ' + translated : translated ) ;
} ) ;
threadEl . find ( '.quote, .edit, .delete' ) . toggleClass ( 'none' , isLocked ) ;
$ ( '.topic-title i.fa-lock' ) . toggleClass ( 'hide' , ! data . isLocked ) ;
2014-06-10 16:56:55 -04:00
ThreadTools . threadState . locked = data . isLocked ;
2014-05-03 20:56:26 -04:00
}
2014-03-17 21:23:30 -04:00
} ;
2014-05-03 20:56:26 -04:00
ThreadTools . setDeleteState = function ( data ) {
var threadEl = $ ( '#post-container' ) ;
2014-06-10 16:56:55 -04:00
if ( parseInt ( data . tid , 10 ) !== parseInt ( threadEl . attr ( 'data-tid' ) , 10 ) ) {
return ;
}
2014-03-18 16:46:07 -04:00
2014-07-05 16:58:57 -04:00
$ ( '.delete_thread span' ) . translateHtml ( '<i class="fa fa-fw ' + ( data . isDelete ? 'fa-history' : 'fa-trash-o' ) + '"></i> [[topic:thread_tools.' + ( data . isDelete ? 'restore' : 'delete' ) + ']]' ) ;
2014-06-10 16:56:55 -04:00
threadEl . toggleClass ( 'deleted' , data . isDelete ) ;
ThreadTools . threadState . deleted = data . isDelete ;
$ ( '.purge_thread' ) . toggleClass ( 'none' , ! data . isDelete ) ;
2014-03-18 16:46:07 -04:00
2014-06-10 16:56:55 -04:00
if ( data . isDelete ) {
translator . translate ( '[[topic:deleted_message]]' , function ( translated ) {
$ ( '<div id="thread-deleted" class="alert alert-warning">' + translated + '</div>' ) . insertBefore ( threadEl ) ;
} ) ;
} else {
$ ( '#thread-deleted' ) . remove ( ) ;
2014-03-18 16:46:07 -04:00
}
2014-05-03 20:56:26 -04:00
} ;
ThreadTools . setPinnedState = function ( data ) {
var threadEl = $ ( '#post-container' ) ;
if ( parseInt ( data . tid , 10 ) === parseInt ( threadEl . attr ( 'data-tid' ) , 10 ) ) {
translator . translate ( '<i class="fa fa-fw fa-thumb-tack"></i> [[topic:thread_tools.' + ( data . isPinned ? 'unpin' : 'pin' ) + ']]' , function ( translated ) {
$ ( '.pin_thread' ) . html ( translated ) ;
2014-06-10 16:56:55 -04:00
ThreadTools . threadState . pinned = data . isPinned ;
2014-05-03 20:56:26 -04:00
} ) ;
2014-06-24 14:09:12 -04:00
$ ( '.topic-title i.fa-thumb-tack' ) . toggleClass ( 'hide' , ! data . isPinned ) ;
2014-05-03 20:56:26 -04:00
}
}
function setFollowState ( state ) {
$ ( '.posts .follow' ) . toggleClass ( 'btn-success' , state ) . attr ( 'title' , state ? 'You are currently receiving updates to this topic' : 'Be notified of new replies in this topic' ) ;
2014-03-18 16:46:07 -04:00
}
2014-03-17 21:23:30 -04:00
return ThreadTools ;
2014-04-10 20:31:57 +01:00
} ) ;