2018-01-04 09:06:34 +00:00
define ( [
'jquery' ,
'underscore' ,
'backbone' ,
'common' ,
'moment' ,
2018-04-24 12:08:20 +00:00
'simplemodal' ,
2018-01-04 09:06:34 +00:00
'app/views/widgets/hl-item-view'
2018-04-24 12:08:20 +00:00
] , function ( $ , _ , Backbone , Common , Moment , Simplemodal , HLItemView ) {
2018-01-04 09:06:34 +00:00
'use strict' ;
var GroupView = HLItemView . extend ( {
tagName : 'tr' ,
template : _ . template ( $ ( '#address-book-group-item-tmpl' ) . html ( ) ) ,
2018-04-24 12:08:20 +00:00
setQuotaFormTemplate : _ . template ( $ ( '#address-book-group-quota-set-form-tmpl' ) . html ( ) ) ,
2018-01-04 09:06:34 +00:00
events : {
2018-04-24 12:08:20 +00:00
'click .group-delete-btn' : 'deleteGroup' ,
'click .quota-edit-icon' : 'setQuota'
2018-01-04 09:06:34 +00:00
} ,
initialize : function ( ) {
HLItemView . prototype . initialize . call ( this ) ;
2018-04-24 12:08:20 +00:00
this . listenTo ( this . model , "change" , this . render ) ;
2018-01-04 09:06:34 +00:00
} ,
deleteGroup : function ( ) {
var _this = this ;
var group _name = this . model . get ( 'name' ) ;
2018-08-07 09:05:41 +00:00
var url _options = {
'group_id' : _this . model . get ( 'id' )
} ;
if ( app . pageOptions . org _id ) { // org admin
$ . extend ( url _options , {
'name' : 'org-admin-address-book-group' ,
'org_id' : app . pageOptions . org _id
} ) ;
} else {
$ . extend ( url _options , {
'name' : 'admin-address-book-group'
} ) ;
}
2018-05-07 11:55:41 +00:00
var popupTitle = gettext ( "Delete Department" ) ;
2018-01-04 09:06:34 +00:00
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 ( {
2018-08-07 09:05:41 +00:00
url : Common . getUrl ( url _options ) ,
2018-01-04 09:06:34 +00:00
type : 'DELETE' ,
cache : false ,
beforeSend : Common . prepareCSRFToken ,
dataType : 'json' ,
success : function ( ) {
_this . $el . remove ( ) ;
Common . feedback ( gettext ( "Successfully deleted 1 item." ) , 'success' ) ;
} ,
error : function ( xhr , textStatus , errorThrown ) {
Common . ajaxErrorHandler ( xhr , textStatus , errorThrown ) ;
} ,
complete : function ( ) {
$ . modal . close ( ) ;
}
} ) ;
} ;
Common . showConfirm ( popupTitle , popupContent , yesCallback ) ;
return false ;
} ,
2018-04-24 12:08:20 +00:00
setQuota : function ( ) {
var model = this . model ;
var $form = $ ( this . setQuotaFormTemplate ( ) ) ;
$form . modal ( ) ;
$ ( '#simplemodal-container' ) . css ( { 'width' : 'auto' , 'height' : 'auto' } ) ;
$form . on ( 'submit' , function ( ) {
var $error = $ ( '.error' , $form ) ;
var $submitBtn = $ ( '[type="submit"]' , $form ) ;
var quota = $ . trim ( $ ( '[name="quota"]' , $form ) . val ( ) ) ;
2018-05-09 10:01:02 +00:00
var quota _int = parseInt ( quota ) ;
2018-04-24 12:08:20 +00:00
if ( ! quota ) {
$error . html ( gettext ( "It is required." ) ) . show ( ) ;
return false ;
}
2018-05-09 10:01:02 +00:00
if ( ! ( quota _int == quota &&
( quota _int > 0 || quota _int == - 2 ) ) ) {
2018-05-17 09:42:27 +00:00
$error . html ( gettext ( "Invalid quota." ) ) . show ( ) ;
2018-05-09 10:01:02 +00:00
return false ;
}
2018-08-07 09:05:41 +00:00
var url _options ;
if ( app . pageOptions . org _id ) { // org admin
url _options = {
'name' : 'org-admin-group' ,
'org_id' : app . pageOptions . org _id ,
'group_id' : model . get ( 'id' )
} ;
} else {
url _options = {
2018-04-25 08:19:15 +00:00
'name' : 'admin-group' ,
2018-04-24 12:08:20 +00:00
'group_id' : model . get ( 'id' )
2018-08-07 09:05:41 +00:00
} ;
}
Common . disableButton ( $submitBtn ) ;
$ . ajax ( {
url : Common . getUrl ( url _options ) ,
2018-04-24 12:08:20 +00:00
type : 'PUT' ,
cache : false ,
beforeSend : Common . prepareCSRFToken ,
data : { 'quota' : quota == - 2 ? - 2 : quota * 1000000 } ,
dataType : 'json' ,
success : function ( data ) {
model . set ( { 'quota' : data . quota } ) ;
$ . modal . close ( ) ;
} ,
error : function ( xhr ) {
2018-07-31 10:15:44 +00:00
var error _msg = Common . prepareAjaxErrorMsg ( xhr ) ;
$error . html ( error _msg ) . show ( ) ;
2018-04-24 12:08:20 +00:00
Common . enableButton ( $submitBtn ) ;
}
} ) ;
return false ;
} ) ;
} ,
2018-01-04 09:06:34 +00:00
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 ) ;
2018-05-10 04:13:47 +00:00
data [ 'quota_error' ] = false ;
2018-04-25 08:19:15 +00:00
switch ( data [ 'quota' ] ) {
case - 2 : // no limit
data [ 'quota_shown' ] = '--' ;
break ;
case - 1 : // not set or error
2018-05-10 04:13:47 +00:00
data [ 'quota_shown' ] = gettext ( "Error" ) ;
data [ 'quota_error' ] = true ;
2018-04-25 08:19:15 +00:00
break ;
default :
2018-05-10 04:13:47 +00:00
if ( data [ 'quota' ] > 0 ) {
data [ 'quota_shown' ] = Common . quotaSizeFormat ( data [ 'quota' ] ) ;
} else {
data [ 'quota_shown' ] = gettext ( "Error" ) ;
data [ 'quota_error' ] = true ;
}
2018-04-25 08:19:15 +00:00
}
2018-04-24 12:08:20 +00:00
2018-01-04 09:06:34 +00:00
this . $el . html ( this . template ( data ) ) ;
return this ;
}
} ) ;
return GroupView ;
} ) ;