2016-07-20 04:26:54 +00:00
define ( [
'jquery' ,
'underscore' ,
'backbone' ,
'common' ,
'moment' ,
'app/views/widgets/hl-item-view'
2016-07-21 07:13:48 +00:00
] , function ( $ , _ , Backbone , Common , Moment , HLItemView ) {
2016-07-20 04:26:54 +00:00
'use strict' ;
var GroupView = HLItemView . extend ( {
tagName : 'tr' ,
template : _ . template ( $ ( '#group-item-tmpl' ) . html ( ) ) ,
transferTemplate : _ . template ( $ ( '#group-transfer-form-tmpl' ) . html ( ) ) ,
events : {
'click .group-delete-btn' : 'deleteGroup' ,
'click .group-transfer-btn' : 'transferGroup'
} ,
initialize : function ( ) {
HLItemView . prototype . initialize . call ( this ) ;
this . listenTo ( this . model , "change" , this . render ) ;
} ,
deleteGroup : function ( ) {
var _this = this ;
var group _name = this . model . get ( 'name' ) ;
var popupTitle = gettext ( "Delete Group" ) ;
var popupContent = gettext ( "Are you sure you want to delete %s ?" ) . replace ( '%s' , '<span class="op-target ellipsis ellipsis-op-target" title="' + Common . HTMLescape ( group _name ) + '">' + Common . HTMLescape ( group _name ) + '</span>' ) ;
var yesCallback = function ( ) {
$ . ajax ( {
url : Common . getUrl ( {
'name' : 'admin-group' ,
'group_id' : _this . model . get ( 'id' )
} ) ,
type : 'DELETE' ,
cache : false ,
beforeSend : Common . prepareCSRFToken ,
dataType : 'json' ,
success : function ( ) {
_this . $el . remove ( ) ;
2016-07-21 07:13:48 +00:00
Common . feedback ( gettext ( "Successfully deleted 1 item." ) , 'success' ) ;
2016-07-20 04:26:54 +00:00
} ,
error : function ( xhr , textStatus , errorThrown ) {
Common . ajaxErrorHandler ( xhr , textStatus , errorThrown ) ;
} ,
complete : function ( ) {
$ . modal . close ( ) ;
}
} ) ;
} ;
Common . showConfirm ( popupTitle , popupContent , yesCallback ) ;
return false ;
} ,
transferGroup : function ( ) {
var _this = this ;
var group _name = this . model . get ( 'name' ) ;
2016-07-21 07:13:48 +00:00
var group _id = this . model . get ( 'id' ) ;
var cur _owner = this . model . get ( 'owner' ) ;
2016-07-20 04:26:54 +00:00
var $form = $ ( this . transferTemplate ( {
title : gettext ( "Transfer Group {group_name} To" ) . replace ( '{group_name}' ,
'<span class="op-target ellipsis ellipsis-op-target" title="' + Common . HTMLescape ( group _name ) + '">' + Common . HTMLescape ( group _name ) + '</span>' )
} ) ) ;
$form . modal ( { focus : false } ) ;
$ ( '#simplemodal-container' ) . css ( { 'width' : 'auto' , 'height' : 'auto' } ) ;
$ ( '[name="email"]' , $form ) . select2 ( $ . extend (
Common . contactInputOptionsForSelect2 ( ) , {
width : '300px' ,
2018-08-03 08:08:29 +00:00
maximumSelectionSize : 1 ,
placeholder : gettext ( "Search user or enter email and press Enter" ) , // to override 'placeholder' returned by `Common.conta...`
formatSelectionTooBig : gettext ( "You cannot select any more choices" )
2016-07-20 04:26:54 +00:00
} ) ) ;
2018-02-05 08:47:56 +00:00
$form . on ( 'submit' , function ( ) {
2018-08-03 08:08:29 +00:00
var email = $ . trim ( $ ( '[name="email"]' , $ ( this ) ) . val ( ) ) ;
2016-07-21 07:13:48 +00:00
var $submitBtn = $ ( '[type="submit"]' , $ ( this ) ) ;
2018-08-03 08:08:29 +00:00
if ( ! email ) {
2016-07-20 04:26:54 +00:00
return false ;
}
2016-07-21 07:13:48 +00:00
if ( email == cur _owner ) {
2016-07-20 04:26:54 +00:00
return false ;
}
Common . disableButton ( $submitBtn ) ;
$ . ajax ( {
2016-07-21 07:13:48 +00:00
url : Common . getUrl ( { 'name' : 'admin-group' , 'group_id' : group _id } ) ,
2016-07-20 04:26:54 +00:00
type : 'put' ,
dataType : 'json' ,
beforeSend : Common . prepareCSRFToken ,
data : {
2018-08-03 08:08:29 +00:00
'new_owner' : email
2016-07-20 04:26:54 +00:00
} ,
success : function ( ) {
$ . modal . close ( ) ;
_this . model . set ( { 'owner' : email } ) ; // it will trigger 'change' event
Common . feedback ( gettext ( "Successfully transferred the group." ) , 'success' ) ;
} ,
error : function ( xhr ) {
2018-07-31 10:15:44 +00:00
var error _msg = Common . prepareAjaxErrorMsg ( xhr ) ;
2016-07-20 04:26:54 +00:00
$ ( '.error' , $form ) . html ( error _msg ) . show ( ) ;
Common . enableButton ( $submitBtn ) ;
}
} ) ;
return false ;
} ) ;
return false ;
} ,
render : function ( ) {
var data = this . model . toJSON ( ) ,
created _at = Moment ( data [ 'created_at' ] ) ;
data [ 'time' ] = created _at . format ( 'LLLL' ) ;
data [ 'time_from_now' ] = Common . getRelativeTimeStr ( created _at ) ;
this . $el . html ( this . template ( data ) ) ;
return this ;
}
} ) ;
return GroupView ;
} ) ;