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 ( ) {
socket . emit ( threadState . locked ? 'topics.unlock' : 'topics.lock' , [ tid ] ) ;
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 ( ) {
socket . emit ( threadState . pinned ? 'topics.unpin' : 'topics.pin' , [ tid ] ) ;
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 ) ;
app . alertSuccess ( state ? '[[topic:following_topic.message]]' : '[[topic:not_following_topic.message]]' ) ;
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 ) {
socket . emit ( 'topics.' + command , [ tid ] ) ;
}
} ) ;
} ) ;
}
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 ) ) {
translator . translate ( '<i class="fa fa-fw fa-' + ( data . isLocked ? 'un' : '' ) + 'lock"></i> [[topic:thread_tools.' + ( data . isLocked ? 'un' : '' ) + 'lock]]' , function ( translated ) {
$ ( '.lock_thread' ) . html ( translated ) ;
} ) ;
threadEl . find ( '.post_reply' ) . html ( data . isLocked ? 'Locked <i class="fa fa-lock"></i>' : 'Reply <i class="fa fa-reply"></i>' ) ;
threadEl . find ( '.quote, .edit, .delete' ) . toggleClass ( 'none' , data . isLocked ) ;
$ ( '.topic-main-buttons .post_reply' ) . attr ( 'disabled' , data . isLocked ) . html ( data . isLocked ? 'Locked <i class="fa fa-lock"></i>' : 'Reply' ) ;
2014-03-17 21:23:30 -04:00
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-06-11 16:47:20 -04:00
translator . translate ( '<i class="fa fa-fw ' + ( data . isDelete ? 'fa-history' : 'fa-trash-o' ) + '"></i> [[topic:thread_tools.' + ( data . isDelete ? 'restore' : 'delete' ) + ']]' , function ( translated ) {
2014-06-10 16:56:55 -04:00
$ ( '.delete_thread span' ) . html ( translated ) ;
} ) ;
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
} ) ;
}
}
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
} ) ;