2016-05-25 02:45:22 +00:00
define ( [
'jquery' ,
'underscore' ,
'backbone' ,
'common' ,
'moment' ,
'simplemodal' ,
'select2' ,
2016-08-03 10:17:24 +00:00
'sysadmin-app/views/share' ,
2017-08-11 09:51:11 +00:00
'app/views/dialogs/repo-history-settings' ,
'app/views/widgets/hl-item-view' ,
'app/views/widgets/dropdown'
2016-08-03 10:17:24 +00:00
] , function ( $ , _ , Backbone , Common , Moment , Simplemodal ,
2018-02-05 08:47:56 +00:00
Select2 , ShareView , HistorySettingsDialog ,
2017-08-11 09:51:11 +00:00
HLItemView , DropdownView ) {
2016-08-03 10:17:24 +00:00
2016-05-25 02:45:22 +00:00
'use strict' ;
2016-05-27 08:42:40 +00:00
var RepoView = HLItemView . extend ( {
2016-05-25 02:45:22 +00:00
tagName : 'tr' ,
template : _ . template ( $ ( '#library-item-tmpl' ) . html ( ) ) ,
transferTemplate : _ . template ( $ ( '#library-transfer-form-tmpl' ) . html ( ) ) ,
events : {
2017-08-11 09:51:11 +00:00
'click .js-repo-share' : 'share' ,
'click .js-popup-history-setting' : 'popupHistorySetting' ,
2016-05-25 02:45:22 +00:00
'click .repo-delete-btn' : 'deleteLibrary' ,
2016-06-07 09:32:01 +00:00
'click .repo-transfer-btn' : 'transferLibrary'
2016-05-25 02:45:22 +00:00
} ,
2016-08-03 10:17:24 +00:00
share : function ( ) {
var options = {
'repo_id' : this . model . get ( 'id' ) ,
'repo_name' : this . model . get ( 'name' )
} ;
new ShareView ( options ) ;
2017-08-11 09:51:11 +00:00
this . togglePopup ( ) ; // close the popup
2016-08-03 10:17:24 +00:00
return false ;
} ,
2017-08-11 09:51:11 +00:00
popupHistorySetting : function ( ) {
var options = {
'repo_name' : this . model . get ( 'name' ) ,
'repo_id' : this . model . get ( 'id' ) ,
2018-10-25 09:18:08 +00:00
'is_admin_panel' : true ,
2017-08-11 09:51:11 +00:00
'url_name' : 'admin-library-history-limit'
} ;
this . togglePopup ( ) ; // close the popup
new HistorySettingsDialog ( options ) ;
return false ;
} ,
togglePopup : function ( ) {
this . dropdown . hide ( ) ;
} ,
2016-05-25 02:45:22 +00:00
initialize : function ( ) {
HLItemView . prototype . initialize . call ( this ) ;
this . listenTo ( this . model , "change" , this . render ) ;
} ,
deleteLibrary : function ( ) {
var _this = this ;
2016-06-07 09:32:01 +00:00
var repo _name = this . model . get ( 'name' ) ;
var popupTitle = gettext ( "Delete Library" ) ;
var popupContent = gettext ( "Are you sure you want to delete %s ?" ) . replace ( '%s' , '<span class="op-target ellipsis ellipsis-op-target" title="' + Common . HTMLescape ( repo _name ) + '">' + Common . HTMLescape ( repo _name ) + '</span>' ) ;
2016-08-03 10:17:24 +00:00
var yesCallback = function ( ) {
2016-06-07 09:32:01 +00:00
$ . ajax ( {
url : Common . getUrl ( {
'name' : 'admin-library' ,
'repo_id' : _this . model . get ( 'id' )
} ) ,
type : 'DELETE' ,
cache : false ,
beforeSend : Common . prepareCSRFToken ,
dataType : 'json' ,
success : function ( ) {
_this . $el . remove ( ) ;
Common . feedback ( gettext ( "Successfully deleted." ) , 'success' ) ;
} ,
error : function ( xhr , textStatus , errorThrown ) {
Common . ajaxErrorHandler ( xhr , textStatus , errorThrown ) ;
} ,
complete : function ( ) {
$ . modal . close ( ) ;
}
} ) ;
} ;
Common . showConfirm ( popupTitle , popupContent , yesCallback ) ;
2016-05-25 02:45:22 +00:00
return false ;
} ,
transferLibrary : function ( ) {
var _this = this ;
var repo _name = this . model . get ( 'name' ) ;
var $form = $ ( this . transferTemplate ( {
title : gettext ( "Transfer Library {library_name} To" ) . replace ( '{library_name}' ,
'<span class="op-target ellipsis ellipsis-op-target" title="' + Common . HTMLescape ( repo _name ) + '">' + Common . HTMLescape ( repo _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-05-25 02:45:22 +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 ( ) ) ;
if ( ! email ) {
2016-05-25 02:45:22 +00:00
return false ;
}
if ( email == _this . model . get ( 'owner' ) ) {
return false ;
}
var url = Common . getUrl ( { 'name' : 'admin-library' , 'repo_id' : _this . model . get ( 'id' ) } ) ;
var $submitBtn = $ ( '[type="submit"]' , $ ( this ) ) ;
Common . disableButton ( $submitBtn ) ;
$ . ajax ( {
url : url ,
type : 'put' ,
dataType : 'json' ,
beforeSend : Common . prepareCSRFToken ,
data : {
'owner' : email
} ,
success : function ( ) {
$ . modal . close ( ) ;
_this . model . set ( { 'owner' : email } ) ; // it will trigger 'change' event
2016-06-07 09:32:01 +00:00
Common . feedback ( gettext ( "Successfully transferred the library." ) , 'success' ) ;
2016-05-25 02:45:22 +00:00
} ,
error : function ( xhr ) {
2018-07-31 10:15:44 +00:00
var error _msg = Common . prepareAjaxErrorMsg ( xhr ) ;
2016-05-25 02:45:22 +00:00
$ ( '.error' , $form ) . html ( error _msg ) . show ( ) ;
Common . enableButton ( $submitBtn ) ;
}
} ) ;
return false ;
} ) ;
return false ;
} ,
render : function ( ) {
var data = this . model . toJSON ( ) ,
2017-07-31 09:30:43 +00:00
icon _size = Common . isHiDPI ( ) ? 48 : 24 ,
2016-05-27 08:42:40 +00:00
icon _url = this . model . getIconUrl ( icon _size ) ,
2016-05-25 02:45:22 +00:00
last _accessed = Moment ( data [ 'last_accessed' ] ) ;
2016-05-27 08:42:40 +00:00
data [ 'icon_url' ] = icon _url ;
data [ 'icon_title' ] = this . model . getIconTitle ( ) ;
2016-05-25 02:45:22 +00:00
data [ 'enable_sys_admin_view_repo' ] = app . pageOptions . enable _sys _admin _view _repo ;
data [ 'is_pro' ] = app . pageOptions . is _pro ;
data [ 'time' ] = last _accessed . format ( 'LLLL' ) ;
data [ 'time_from_now' ] = Common . getRelativeTimeStr ( last _accessed ) ;
this . $el . html ( this . template ( data ) ) ;
2017-08-11 09:51:11 +00:00
this . dropdown = new DropdownView ( {
el : this . $ ( '.sf-dropdown' ) ,
right : 0
} ) ;
2016-05-25 02:45:22 +00:00
return this ;
}
} ) ;
2016-05-27 08:42:40 +00:00
return RepoView ;
2016-05-25 02:45:22 +00:00
} ) ;