2016-08-09 06:42:21 +00:00
/ / jumpserver 自定义js 2015 - 01 - 29
/ / 此函数用于checkbox的全选和反选
2019-09-12 09:58:52 +00:00
var checked = false ;
2016-08-09 06:42:21 +00:00
function check_all ( form ) {
var checkboxes = document . getElementById ( form ) ;
2016-09-13 13:45:10 +00:00
if ( checked == = false ) {
checked = true ;
2016-08-09 06:42:21 +00:00
} else {
2016-09-13 13:45:10 +00:00
checked = false ;
2016-08-09 06:42:21 +00:00
}
for ( var i = 0 ; i < checkboxes . elements . length ; i + + ) {
if ( checkboxes . elements [ i ] . type == " checkbox " ) {
checkboxes . elements [ i ] . checked = checked ;
}
}
}
2019-09-12 09:58:52 +00:00
function checkAll ( id , name ) {
2016-08-09 06:42:21 +00:00
var checklist = document . getElementsByName ( name ) ;
2019-09-12 09:58:52 +00:00
if ( document . getElementById ( id ) . checked ) {
for ( var i = 0 ; i < checklist . length ; i + + ) {
checklist [ i ] . checked = 1 ;
2016-08-09 06:42:21 +00:00
}
2019-09-12 09:58:52 +00:00
} else {
for ( var j = 0 ; j < checklist . length ; j + + ) {
checklist [ j ] . checked = 0 ;
2016-08-09 06:42:21 +00:00
}
}
}
/ / 提取指定行的数据 , JSON格式
2019-09-12 09:58:52 +00:00
function GetRowData ( row ) {
2016-08-09 06:42:21 +00:00
var rowData = { } ;
2019-09-12 09:58:52 +00:00
for ( var j = 0 ; j < row . cells . length ; j + + ) {
2016-08-09 06:42:21 +00:00
name = row . parentNode . rows [ 0 ] . cells [ j ] . getAttribute ( " Name " ) ;
if ( name ) {
var value = row . cells [ j ] . getAttribute ( " Value " ) ;
if ( ! value ) {
value = row . cells [ j ] . innerHTML ;
}
rowData [ name ] = value ;
}
}
return rowData ;
}
/ / 此函数用于在多选提交时至少要选择一行
function GetTableDataBox ( ) {
var tabProduct = document . getElementById ( " editable " ) ;
2016-09-13 13:45:10 +00:00
var tableData = [ ] ;
var returnData = [ ] ;
2016-08-09 06:42:21 +00:00
var checkboxes = document . getElementById ( " contents_form " ) ;
2016-09-13 13:45:10 +00:00
var id_list = [ ] ;
2016-08-09 06:42:21 +00:00
len = checkboxes . elements . length ;
2019-09-12 09:58:52 +00:00
for ( var i = 0 ; i < len ; i + + ) {
2016-09-13 13:45:10 +00:00
if ( checkboxes . elements [ i ] . type == " checkbox " & & checkboxes . elements [ i ] . checked == = true & & checkboxes . elements [ i ] . value != " checkall " ) {
2016-08-09 06:42:21 +00:00
id_list . push ( i ) ;
}
2019-09-12 09:58:52 +00:00
}
2016-08-09 06:42:21 +00:00
for ( i in id_list ) {
tableData . push ( GetRowData ( tabProduct . rows [ id_list [ i ] ] ) ) ;
}
2019-09-12 09:58:52 +00:00
if ( id_list . length == = 0 ) {
2016-08-09 06:42:21 +00:00
alert ( ' 请至少选择一行! ' ) ;
}
returnData . push ( tableData ) ;
returnData . push ( id_list . length ) ;
return returnData ;
}
function move ( from , to , from_o , to_o ) {
$ ( " # " + from + " option " ) . each ( function ( ) {
2016-09-13 13:45:10 +00:00
if ( $ ( this ) . prop ( " selected " ) == = true ) {
2016-08-09 06:42:21 +00:00
$ ( " # " + to ) . append ( this ) ;
2019-09-12 09:58:52 +00:00
if ( typeof from_o != = ' undefined ' ) {
$ ( " # " + to_o ) . append ( $ ( " # " + from_o + " option[value= ' " + this . value + " ' ] " ) ) ;
2016-08-09 06:42:21 +00:00
}
}
} ) ;
}
function move_left ( from , to , from_o , to_o ) {
$ ( " # " + from + " option " ) . each ( function ( ) {
2016-09-13 13:45:10 +00:00
if ( $ ( this ) . prop ( " selected " ) == = true ) {
2016-08-09 06:42:21 +00:00
$ ( " # " + to ) . append ( this ) ;
2019-09-12 09:58:52 +00:00
if ( typeof from_o != = ' undefined ' ) {
$ ( " # " + to_o ) . append ( $ ( " # " + from_o + " option[value= ' " + this . value + " ' ] " ) ) ;
2016-08-09 06:42:21 +00:00
}
}
2019-09-12 09:58:52 +00:00
$ ( this ) . attr ( " selected " , ' true ' ) ;
2016-08-09 06:42:21 +00:00
} ) ;
}
2019-09-12 09:58:52 +00:00
function selectAll ( ) {
2016-09-15 04:20:53 +00:00
/ / Select all check box
2019-09-12 09:58:52 +00:00
$ ( ' option ' ) . each ( function ( ) {
2016-09-13 13:45:10 +00:00
$ ( this ) . attr ( ' selected ' , true ) ;
} ) ;
2016-08-09 06:42:21 +00:00
}
2016-09-15 04:20:53 +00:00
function getCookie ( name ) {
var cookieValue = null ;
if ( document . cookie & & document . cookie != = ' ' ) {
var cookies = document . cookie . split ( ' ; ' ) ;
for ( var i = 0 ; i < cookies . length ; i + + ) {
var cookie = jQuery . trim ( cookies [ i ] ) ;
/ / Does this cookie string begin with the name we want ?
if ( cookie . substring ( 0 , name . length + 1 ) == = ( name + ' = ' ) ) {
cookieValue = decodeURIComponent ( cookie . substring ( name . length + 1 ) ) ;
2017-04-05 11:09:51 +00:00
/ / break ;
2016-09-15 04:20:53 +00:00
}
}
}
return cookieValue ;
}
2016-08-09 06:42:21 +00:00
2016-09-15 04:20:53 +00:00
function csrfSafeMethod ( method ) {
/ / these HTTP methods do not require CSRF protection
return ( / ^ ( GET | HEAD | OPTIONS | TRACE ) $ / . test ( method ) ) ;
}
2016-08-09 06:42:21 +00:00
2016-09-15 04:20:53 +00:00
function setAjaxCSRFToken ( ) {
2022-04-21 08:13:03 +00:00
let prefix = getCookie ( ' SESSION_COOKIE_NAME_PREFIX ' ) ;
2023-01-30 07:19:05 +00:00
if ( ! prefix | | [ ` " " ` , ` ' ' ` ] . indexOf ( prefix ) > - 1 ) {
prefix = ' ' ;
}
2022-04-19 06:26:17 +00:00
var csrftoken = getCookie ( ` $ { prefix } csrftoken ` ) ;
var sessionid = getCookie ( ` $ { prefix } sessionid ` ) ;
2016-09-15 04:20:53 +00:00
$ . ajaxSetup ( {
2019-09-12 09:58:52 +00:00
beforeSend : function ( xhr , settings ) {
2016-09-15 04:20:53 +00:00
if ( ! csrfSafeMethod ( settings . type ) & & ! this . crossDomain ) {
xhr . setRequestHeader ( " X-CSRFToken " , csrftoken ) ;
}
}
2016-08-09 06:42:21 +00:00
} ) ;
2016-09-15 04:20:53 +00:00
}
2019-10-21 07:15:47 +00:00
function activeNav ( prefix ) {
2020-05-27 10:02:18 +00:00
if ( ! prefix ) {
prefix = ' /core '
2019-10-21 07:15:47 +00:00
}
2020-05-27 10:02:18 +00:00
var path = document . location . pathname ;
path = path . replace ( prefix , ' ' ) ;
2019-10-21 07:15:47 +00:00
var urlArray = path . split ( " / " ) ;
var app = urlArray [ 1 ] ;
var resource = urlArray [ 2 ] ;
2019-09-12 09:58:52 +00:00
if ( app == = ' ' ) {
2016-09-26 08:33:10 +00:00
$ ( ' #index ' ) . addClass ( ' active ' ) ;
2019-09-12 09:58:52 +00:00
} else if ( app == = ' xpack ' & & resource == = ' cloud ' ) {
2019-10-21 07:15:47 +00:00
var item = urlArray [ 3 ] ;
2018-10-15 02:48:24 +00:00
$ ( " # " + app ) . addClass ( ' active ' ) ;
$ ( ' # ' + app + ' # ' + resource ) . addClass ( ' active ' ) ;
$ ( ' # ' + app + ' # ' + resource + ' # ' + item + ' a ' ) . css ( ' color ' , ' #ffffff ' ) ;
2019-09-12 09:58:52 +00:00
} else if ( app == = ' settings ' ) {
2018-10-24 02:48:03 +00:00
$ ( " # " + app ) . addClass ( ' active ' ) ;
2019-09-12 09:58:52 +00:00
} else {
2016-09-15 04:20:53 +00:00
$ ( " # " + app ) . addClass ( ' active ' ) ;
$ ( ' # ' + app + ' # ' + resource ) . addClass ( ' active ' ) ;
2019-12-20 07:55:59 +00:00
$ ( ' # ' + app + ' # ' + resource . replace ( / - / g , ' _ ' ) ) . addClass ( ' active ' ) ;
2016-09-15 04:20:53 +00:00
}
2016-09-06 13:03:51 +00:00
}
2019-01-16 10:13:16 +00:00
function formSubmit ( props ) {
/ *
{
" form " : $ ( " form " ) ,
2019-06-13 10:58:43 +00:00
" data " : { } ,
2019-01-16 10:13:16 +00:00
" url " : " " ,
" method " : " POST " ,
" redirect_to " : " " ,
" success " : function ( data , textStatue , jqXHR ) { } ,
2019-06-13 10:58:43 +00:00
" error " : function ( jqXHR , textStatus , errorThrown ) { } ,
" message " : " " ,
2019-01-16 10:13:16 +00:00
}
* /
props = props | | { } ;
var data = props . data | | props . form . serializeObject ( ) ;
2019-12-16 08:53:29 +00:00
var redirectTo = props . redirect_to | | props . redirectTo ;
2019-01-16 10:13:16 +00:00
$ . ajax ( {
url : props . url ,
type : props . method | | ' POST ' ,
data : JSON . stringify ( data ) ,
contentType : props . content_type | | " application/json; charset=utf-8 " ,
dataType : props . data_type | | " json "
} ) . done ( function ( data , textState , jqXHR ) {
2019-12-16 08:53:29 +00:00
if ( redirectTo ) {
location . href = redirectTo ;
2019-01-16 10:13:16 +00:00
} else if ( typeof props . success == = ' function ' ) {
return props . success ( data , textState , jqXHR ) ;
}
2019-09-12 09:58:52 +00:00
} ) . fail ( function ( jqXHR , textStatus , errorThrown ) {
2019-01-16 10:13:16 +00:00
if ( typeof props . error == = ' function ' ) {
return props . error ( jqXHR , textStatus , errorThrown )
}
if ( ! props . form ) {
alert ( jqXHR . responseText ) ;
return
}
if ( jqXHR . status == = 400 ) {
var errors = jqXHR . responseJSON ;
var noneFieldErrorRef = props . form . children ( ' .alert-danger ' ) ;
if ( noneFieldErrorRef . length != = 1 ) {
2019-10-21 11:07:35 +00:00
props . form . prepend ( ' <div class= " alert alert-danger has-error " style= " display: none " ></div> ' ) ;
2019-01-16 10:13:16 +00:00
noneFieldErrorRef = props . form . children ( ' .alert-danger ' ) ;
}
var noneFieldErrorMsg = " " ;
noneFieldErrorRef . css ( " display " , " none " ) ;
noneFieldErrorRef . html ( " " ) ;
props . form . find ( " .help-block.error " ) . html ( " " ) ;
props . form . find ( " .form-group.has-error " ) . removeClass ( " has-error " ) ;
if ( typeof errors != = " object " ) {
noneFieldErrorMsg = errors ;
if ( noneFieldErrorRef . length == = 1 ) {
noneFieldErrorRef . css ( ' display ' , ' block ' ) ;
noneFieldErrorRef . html ( noneFieldErrorMsg ) ;
}
return
}
$ . each ( errors , function ( k , v ) {
2019-07-17 14:14:39 +00:00
var fieldRef = props . form . find ( ' [name= " ' + k + ' " ] ' ) ;
2019-01-16 10:13:16 +00:00
var formGroupRef = fieldRef . parents ( ' .form-group ' ) ;
var parentRef = fieldRef . parent ( ) ;
var helpBlockRef = parentRef . children ( ' .help-block.error ' ) ;
if ( helpBlockRef . length == = 0 ) {
parentRef . append ( ' <div class= " help-block error " ></div> ' ) ;
helpBlockRef = parentRef . children ( ' .help-block.error ' ) ;
}
if ( fieldRef . length == = 1 & & formGroupRef . length == = 1 ) {
formGroupRef . addClass ( ' has-error ' ) ;
2019-09-12 09:58:52 +00:00
var help_msg = v . join ( " <br/> " ) ;
2019-01-16 10:13:16 +00:00
helpBlockRef . html ( help_msg ) ;
} else {
2019-06-13 10:58:43 +00:00
$ . each ( v , function ( kk , vv ) {
2019-07-17 14:14:39 +00:00
if ( typeof vv == = " object " ) {
2019-06-13 10:58:43 +00:00
$ . each ( vv , function ( kkk , vvv ) {
noneFieldErrorMsg + = " " + vvv + ' <br/> ' ;
} )
2019-09-12 09:58:52 +00:00
} else {
2019-06-13 10:58:43 +00:00
noneFieldErrorMsg + = vv + ' <br/> ' ;
}
} )
2019-01-16 10:13:16 +00:00
}
} ) ;
if ( noneFieldErrorRef . length == = 1 & & noneFieldErrorMsg != = ' ' ) {
noneFieldErrorRef . css ( ' display ' , ' block ' ) ;
noneFieldErrorRef . html ( noneFieldErrorMsg ) ;
}
2020-03-12 08:24:38 +00:00
$ ( ' .has-error :visible ' ) . get ( 0 ) . scrollIntoView ( ) ;
2019-01-16 10:13:16 +00:00
}
} )
}
2019-07-12 10:31:55 +00:00
function requestApi ( props ) {
2017-02-06 15:13:27 +00:00
/ / props = { url : . . , body : , success : , error : , method : , }
props = props | | { } ;
2018-09-07 04:40:26 +00:00
var user_success_message = props . success_message ;
var default_success_message = gettext ( ' Update is successful! ' ) ;
var user_fail_message = props . fail_message ;
var default_failed_message = gettext ( ' An unknown error occurred while updating.. ' ) ;
2018-04-02 08:55:39 +00:00
var flash_message = props . flash_message | | true ;
2019-09-12 09:58:52 +00:00
if ( props . flash_message == = false ) {
2018-01-12 07:43:26 +00:00
flash_message = false ;
}
2020-03-12 08:24:38 +00:00
var dataBody = props . body | | props . data ;
2023-01-30 07:19:05 +00:00
if ( typeof ( dataBody ) == = " object " ) {
2020-03-12 08:24:38 +00:00
dataBody = JSON . stringify ( dataBody )
}
2022-08-19 06:38:59 +00:00
var headers = props . headers | | { }
2018-01-12 07:43:26 +00:00
2017-02-06 15:13:27 +00:00
$ . ajax ( {
url : props . url ,
type : props . method | | " PATCH " ,
2022-08-19 06:38:59 +00:00
headers : headers ,
2020-03-12 08:24:38 +00:00
data : dataBody ,
2017-02-06 15:13:27 +00:00
contentType : props . content_type | | " application/json; charset=utf-8 " ,
dataType : props . data_type | | " json "
2019-09-12 09:58:52 +00:00
} ) . done ( function ( data , textStatue , jqXHR ) {
2018-01-12 07:43:26 +00:00
if ( flash_message ) {
2018-09-07 04:40:26 +00:00
var msg = " " ;
2019-06-28 14:07:22 +00:00
if ( user_success_message ) {
2018-09-07 04:40:26 +00:00
msg = user_success_message ;
} else {
msg = default_success_message ;
}
toastr . success ( msg ) ;
2018-01-12 07:43:26 +00:00
}
2017-02-06 15:13:27 +00:00
if ( typeof props . success == = ' function ' ) {
return props . success ( data ) ;
2018-06-20 07:58:01 +00:00
}
2019-09-12 09:58:52 +00:00
} ) . fail ( function ( jqXHR , textStatus , errorThrown ) {
2018-01-12 07:43:26 +00:00
if ( flash_message ) {
2018-09-07 04:40:26 +00:00
var msg = " " ;
if ( user_fail_message ) {
msg = user_fail_message ;
} else if ( jqXHR . responseJSON ) {
if ( jqXHR . responseJSON . error ) {
msg = jqXHR . responseJSON . error
} else if ( jqXHR . responseJSON . msg ) {
msg = jqXHR . responseJSON . msg
2019-11-19 03:20:26 +00:00
} else if ( jqXHR . responseJSON . detail ) {
msg = jqXHR . responseJSON . detail
2018-09-07 04:40:26 +00:00
}
}
if ( msg == = " " ) {
msg = default_failed_message ;
}
toastr . error ( msg ) ;
2018-01-12 07:43:26 +00:00
}
2017-02-06 15:13:27 +00:00
if ( typeof props . error == = ' function ' ) {
2019-10-30 05:18:11 +00:00
return props . error ( jqXHR . responseText , jqXHR . responseJSON , jqXHR . status ) ;
2018-06-20 07:58:01 +00:00
}
2017-02-06 15:13:27 +00:00
} ) ;
2019-09-12 09:58:52 +00:00
/ / return true ;
2016-09-06 13:03:51 +00:00
}
2016-09-07 13:53:27 +00:00
2016-09-15 05:09:24 +00:00
/ / Sweet Alert for Delete
2019-12-09 03:50:52 +00:00
function objectDelete ( obj , name , url , redirectTo , title , success_message ) {
2016-10-24 11:32:53 +00:00
function doDelete ( ) {
var body = { } ;
2019-09-12 09:58:52 +00:00
var success = function ( ) {
2017-01-20 12:13:22 +00:00
if ( ! redirectTo ) {
$ ( obj ) . parent ( ) . parent ( ) . remove ( ) ;
} else {
2019-09-12 09:58:52 +00:00
window . location . href = redirectTo ;
2017-01-20 12:13:22 +00:00
}
2016-10-24 11:32:53 +00:00
} ;
2020-03-12 08:24:38 +00:00
var fail = function ( responseText , responseJSON , status ) {
var errorMsg = ' ' ;
if ( responseJSON & & responseJSON . error ) {
errorMsg = ' ' ;
} else if ( status == = 404 ) {
errorMsg = gettext ( " Not found " )
} else {
errorMsg = gettext ( " Server error " )
}
swal ( gettext ( ' Error ' ) , " [ " + name + " ] " + errorMsg ) ;
2016-10-24 11:32:53 +00:00
} ;
2019-07-12 10:31:55 +00:00
requestApi ( {
2016-10-24 11:32:53 +00:00
url : url ,
body : JSON . stringify ( body ) ,
method : ' DELETE ' ,
2019-12-09 03:50:52 +00:00
success_message : success_message | | gettext ( " Delete the success " ) ,
2016-10-24 11:32:53 +00:00
success : success ,
error : fail
} ) ;
}
2019-09-12 09:58:52 +00:00
2016-09-15 05:09:24 +00:00
swal ( {
2019-12-09 03:50:52 +00:00
title : title | | gettext ( ' Are you sure about deleting it? ' ) ,
2016-10-24 11:32:53 +00:00
text : " [ " + name + " ] " ,
2016-09-15 05:09:24 +00:00
type : " warning " ,
showCancelButton : true ,
2018-09-03 03:24:25 +00:00
cancelButtonText : gettext ( ' Cancel ' ) ,
2017-12-21 03:31:13 +00:00
confirmButtonColor : " #ed5565 " ,
2018-09-03 03:24:25 +00:00
confirmButtonText : gettext ( ' Confirm ' ) ,
2017-12-21 03:31:13 +00:00
closeOnConfirm : true ,
2016-09-15 05:09:24 +00:00
} , function ( ) {
2018-06-20 07:58:01 +00:00
doDelete ( )
2016-09-15 05:09:24 +00:00
} ) ;
}
2019-09-12 09:58:52 +00:00
function orgDelete ( obj , name , url , redirectTo ) {
2018-08-06 08:08:05 +00:00
function doDelete ( ) {
var body = { } ;
2019-09-12 09:58:52 +00:00
var success = function ( ) {
2018-08-06 08:08:05 +00:00
if ( ! redirectTo ) {
$ ( obj ) . parent ( ) . parent ( ) . remove ( ) ;
} else {
2019-09-12 09:58:52 +00:00
window . location . href = redirectTo ;
2018-08-06 08:08:05 +00:00
}
} ;
2019-09-12 09:58:52 +00:00
var fail = function ( responseText , status ) {
if ( status == = 400 ) {
swal ( gettext ( " Error " ) , " [ " + name + " ] " + gettext ( " The organization contains undeleted information. Please try again after deleting " ) , " error " ) ;
} else if ( status == = 405 ) {
swal ( gettext ( " Error " ) , " [ " + name + " ] " + gettext ( " Do not perform this operation under this organization. Try again after switching to another organization " ) , " error " ) ;
2018-08-06 10:17:17 +00:00
}
2018-08-06 08:08:05 +00:00
} ;
2019-07-12 10:31:55 +00:00
requestApi ( {
2018-08-06 08:08:05 +00:00
url : url ,
body : JSON . stringify ( body ) ,
method : ' DELETE ' ,
2018-09-03 03:24:25 +00:00
success_message : gettext ( " Delete the success " ) ,
2018-08-06 08:08:05 +00:00
success : success ,
error : fail
} ) ;
}
2019-09-12 09:58:52 +00:00
2018-08-06 08:08:05 +00:00
swal ( {
2018-09-03 03:24:25 +00:00
title : gettext ( " Please ensure that the following information in the organization has been deleted " ) ,
text : gettext ( " User list、User group、Asset list、Domain list、Admin user、System user、Labels、Asset permission " ) ,
2018-08-06 08:08:05 +00:00
type : " warning " ,
showCancelButton : true ,
2018-09-03 03:24:25 +00:00
cancelButtonText : gettext ( ' Cancel ' ) ,
2018-08-06 08:08:05 +00:00
confirmButtonColor : " #ed5565 " ,
2018-09-03 03:24:25 +00:00
confirmButtonText : gettext ( ' Confirm ' ) ,
2018-08-06 08:08:05 +00:00
closeOnConfirm : true
} , function ( ) {
doDelete ( ) ;
} ) ;
}
2019-09-12 09:58:52 +00:00
$ . fn . serializeObject = function ( ) {
2016-09-26 08:33:10 +00:00
var o = { } ;
var a = this . serializeArray ( ) ;
2019-09-12 09:58:52 +00:00
$ . each ( a , function ( ) {
2016-09-26 08:33:10 +00:00
if ( o [ this . name ] != = undefined ) {
if ( ! o [ this . name ] . push ) {
o [ this . name ] = [ o [ this . name ] ] ;
}
o [ this . name ] . push ( this . value | | ' ' ) ;
} else {
o [ this . name ] = this . value | | ' ' ;
}
} ) ;
return o ;
} ;
2018-04-10 12:29:06 +00:00
function makeLabel ( data ) {
2019-12-05 07:09:25 +00:00
return " <label class= ' detail-key ' ><b> " + data [ 0 ] + " : </b></label> " + data [ 1 ] + " </br> "
2018-04-10 12:29:06 +00:00
}
2019-10-25 04:08:49 +00:00
function parseTableFilter ( value ) {
var cleanValues = [ ] ;
2019-10-31 10:23:43 +00:00
if ( ! value ) {
return { }
}
2019-10-25 04:08:49 +00:00
var valuesArray = value . split ( ' : ' ) ;
2023-01-30 07:19:05 +00:00
for ( var i = 0 ; i < valuesArray . length ; i + + ) {
2019-10-25 04:08:49 +00:00
var v = valuesArray [ i ] . trim ( ) ;
if ( ! v ) {
continue
}
/ / 如果是最后一个元素 , 直接push , 不需要再处理了 , 因为最后一个肯定不是key
2023-01-30 07:19:05 +00:00
if ( i == = valuesArray . length - 1 ) {
2019-10-25 04:08:49 +00:00
cleanValues . push ( v ) ;
continue
}
v = v . split ( ' ' ) ;
/ / 如果长度是1 , 直接push上
/ / 如果长度不是1 , 根据空格分隔后 , 最后面的是key
if ( v . length == = 1 ) {
cleanValues . push ( v [ 0 ] ) ;
} else {
var leaveData = v . slice ( 0 , - 1 ) . join ( ' ' ) . trim ( ) ;
cleanValues . push ( leaveData ) ;
cleanValues . push ( v . slice ( - 1 ) [ 0 ] ) ;
}
}
var filter = { } ;
var key = ' ' ;
2023-01-30 07:19:05 +00:00
for ( i = 0 ; i < cleanValues . length ; i + + ) {
if ( i % 2 == = 0 ) {
2019-10-25 04:08:49 +00:00
key = cleanValues [ i ]
} else {
value = cleanValues [ i ] ;
filter [ key ] = value
}
}
return filter ;
}
2018-04-10 12:29:06 +00:00
2016-09-27 13:28:02 +00:00
var jumpserver = { } ;
2016-09-28 13:11:43 +00:00
jumpserver . checked = false ;
2016-11-16 09:38:03 +00:00
jumpserver . selected = { } ;
2023-01-30 07:19:05 +00:00
jumpserver . language = { } ;
2019-11-14 04:12:34 +00:00
function setDataTablePagerLength ( num ) {
$ . fn . DataTable . ext . pager . numbers_length = num ;
}
2016-09-27 13:28:02 +00:00
jumpserver . initDataTable = function ( options ) {
2019-09-12 09:58:52 +00:00
/ / options = {
/ / ele * : $ ( ' #dataTable_id ' ) ,
/ / ajax_url * : ' { % u rl ' users : user - list - api ' % } ' ,
/ / columns * : [ { data : ' ' } , . . . . ] ,
/ / dom : ' fltip ' ,
/ / i18n_url : ' { % s tatic " js/...../en-us.json " % } ' ,
/ / order : [ [ 1 , ' asc ' ] , [ 2 , ' asc ' ] , . . . ] ,
/ / buttons : [ ' excel ' , ' pdf ' , ' print ' ] ,
/ / columnDefs : [ { target : 0 , createdCell : ( ) = > { } } , . . . ] ,
/ / uc_html : ' <a>header button</a> ' ,
/ / op_html : ' div.btn-group? ' ,
/ / paging : true
/ / }
2019-11-14 04:12:34 +00:00
setDataTablePagerLength ( 5 ) ;
2019-09-12 09:58:52 +00:00
var ele = options . ele | | $ ( ' .dataTable ' ) ;
var columnDefs = [
{
targets : 0 ,
orderable : false ,
2019-10-21 11:07:35 +00:00
width : " 20px " ,
2019-09-12 09:58:52 +00:00
createdCell : function ( td , cellData ) {
$ ( td ) . html ( ' <input type= " checkbox " class= " text-center ipt_check " id=99991937> ' . replace ( ' 99991937 ' , cellData ) ) ;
}
} ,
{
className : ' text-center ' ,
render : $ . fn . dataTable . render . text ( ) ,
targets : ' _all '
}
] ;
columnDefs = options . columnDefs ? options . columnDefs . concat ( columnDefs ) : columnDefs ;
var select = {
style : ' multi ' ,
selector : ' td:first-child '
} ;
var table = ele . DataTable ( {
2016-11-06 14:45:26 +00:00
pageLength : options . pageLength | | 15 ,
2019-07-04 08:13:05 +00:00
dom : options . dom | | ' < " #uc.pull-left " >< " pull-right " < " inline " l>< " #fb.inline " >< " inline " f>< " #fa.inline " >>tr< " row m-t " < " col-md-8 " < " #op.col-md-6 " >< " col-md-6 text-center " i>>< " col-md-4 " p>> ' ,
2017-12-12 04:19:45 +00:00
order : options . order | | [ ] ,
2017-12-31 03:14:02 +00:00
/ / select : options . select | | ' multi ' ,
2019-07-03 10:03:01 +00:00
searchDelay : 800 ,
2016-11-16 09:38:03 +00:00
buttons : [ ] ,
2016-09-27 13:28:02 +00:00
columnDefs : columnDefs ,
ajax : {
2018-04-10 12:29:06 +00:00
url : options . ajax_url ,
2016-09-27 13:28:02 +00:00
dataSrc : " "
} ,
2016-11-06 14:45:26 +00:00
columns : options . columns | | [ ] ,
2017-12-31 04:20:08 +00:00
select : options . select | | select ,
2018-07-25 03:21:12 +00:00
language : jumpserver . language ,
2018-04-13 07:48:10 +00:00
lengthMenu : [ [ 10 , 15 , 25 , 50 , - 1 ] , [ 10 , 15 , 25 , 50 , " All " ] ]
2016-09-27 13:28:02 +00:00
} ) ;
2019-09-12 09:58:52 +00:00
table . on ( ' select ' , function ( e , dt , type , indexes ) {
var $ node = table [ type ] ( indexes ) . nodes ( ) . to $ ( ) ;
2016-11-14 16:48:48 +00:00
$ node . find ( ' input.ipt_check ' ) . prop ( ' checked ' , true ) ;
2016-11-16 09:38:03 +00:00
jumpserver . selected [ $ node . find ( ' input.ipt_check ' ) . prop ( ' id ' ) ] = true
2019-09-12 09:58:52 +00:00
} ) . on ( ' deselect ' , function ( e , dt , type , indexes ) {
var $ node = table [ type ] ( indexes ) . nodes ( ) . to $ ( ) ;
2018-01-15 09:41:49 +00:00
$ node . find ( ' input.ipt_check ' ) . prop ( ' checked ' , false ) ;
jumpserver . selected [ $ node . find ( ' input.ipt_check ' ) . prop ( ' id ' ) ] = false
2019-09-12 09:58:52 +00:00
} ) . on ( ' draw ' , function ( ) {
2018-01-15 09:41:49 +00:00
$ ( ' #op ' ) . html ( options . op_html | | ' ' ) ;
$ ( ' #uc ' ) . html ( options . uc_html | | ' ' ) ;
2018-01-30 10:07:51 +00:00
$ ( ' [data-toggle= " popover " ] ' ) . popover ( {
html : true ,
placement : ' bottom ' ,
2019-06-24 12:16:18 +00:00
trigger : ' click ' ,
2018-01-30 10:07:51 +00:00
container : ' body '
2019-06-24 12:16:18 +00:00
} ) . on ( ' click ' , function ( e ) {
$ ( ' [data-toggle= " popover " ] ' ) . not ( this ) . popover ( ' hide ' ) ;
2018-01-30 10:07:51 +00:00
} ) ;
2018-01-15 09:41:49 +00:00
} ) ;
2019-09-12 09:58:52 +00:00
$ ( ' .ipt_check_all ' ) . on ( ' click ' , function ( ) {
if ( $ ( this ) . prop ( " checked " ) ) {
$ ( this ) . closest ( ' table ' ) . find ( ' .ipt_check ' ) . prop ( ' checked ' , true ) ;
jumpserver . checked = true ;
table . rows ( { search : ' applied ' , page : ' current ' } ) . select ( ) ;
} else {
$ ( this ) . closest ( ' table ' ) . find ( ' .ipt_check ' ) . prop ( ' checked ' , false ) ;
jumpserver . checked = false ;
table . rows ( { search : ' applied ' , page : ' current ' } ) . deselect ( ) ;
}
2018-01-15 09:41:49 +00:00
} ) ;
return table ;
} ;
2018-07-25 03:21:12 +00:00
jumpserver . initStaticTable = function ( selector ) {
$ ( selector ) . DataTable ( {
" searching " : false ,
" bInfo " : false ,
" paging " : false ,
" order " : [ ] ,
" language " : jumpserver . language
} ) ;
} ;
2018-01-15 09:41:49 +00:00
jumpserver . initServerSideDataTable = function ( options ) {
2019-09-12 09:58:52 +00:00
/ / options = {
/ / ele * : $ ( ' #dataTable_id ' ) ,
/ / ajax_url * : ' { % u rl ' users : user - list - api ' % } ' ,
/ / select_style : ' multi ' ,
/ / columns * : [ { data : ' ' } , . . . . ] ,
/ / dom : ' fltip ' ,
/ / i18n_url : ' { % s tatic " js/...../en-us.json " % } ' ,
/ / order : [ [ 1 , ' asc ' ] , [ 2 , ' asc ' ] , . . . ] ,
/ / buttons : [ ' excel ' , ' pdf ' , ' print ' ] ,
/ / columnDefs : [ { target : 0 , createdCell : ( ) = > { } } , . . . ] ,
/ / uc_html : ' <a>header button</a> ' ,
/ / op_html : ' div.btn-group? ' ,
2019-11-14 04:12:34 +00:00
/ / paging : true ,
/ / paging_numbers_length : 5 ;
2019-12-05 07:09:25 +00:00
/ / hideDefaultDefs : false ;
2019-09-12 09:58:52 +00:00
/ / }
2019-11-14 04:12:34 +00:00
var pagingNumbersLength = 5 ;
2023-01-30 07:19:05 +00:00
if ( options . paging_numbers_length ) {
2019-11-14 04:12:34 +00:00
pagingNumbersLength = options . paging_numbers_length ;
}
setDataTablePagerLength ( pagingNumbersLength ) ;
2019-09-12 09:58:52 +00:00
var ele = options . ele | | $ ( ' .dataTable ' ) ;
var columnDefs = [
{
targets : 0 ,
orderable : false ,
2019-10-21 11:07:35 +00:00
width : " 20px " ,
2019-09-12 09:58:52 +00:00
createdCell : function ( td , cellData ) {
2019-12-05 07:09:25 +00:00
var data = ' <input type= " checkbox " class= " text-center ipt_check " id=Id> ' . replace ( ' Id ' , cellData ) ;
$ ( td ) . html ( data ) ;
2019-09-12 09:58:52 +00:00
}
} ,
{
targets : ' _all ' ,
className : ' text-center ' ,
render : $ . fn . dataTable . render . text ( )
}
] ;
2019-12-05 07:09:25 +00:00
if ( options . hideDefaultDefs ) {
columnDefs = [ ] ;
}
2019-09-12 09:58:52 +00:00
var select_style = options . select_style | | ' multi ' ;
columnDefs = options . columnDefs ? options . columnDefs . concat ( columnDefs ) : columnDefs ;
var select = {
style : select_style ,
selector : ' td:first-child '
} ;
2020-03-12 08:24:38 +00:00
var dom = ' < " #uc.pull-left " > < " pull-right " < " #lb.inline " > < " inline " l> < " #fb.inline " > < " inline " f>< " #fa.inline " >> ' +
2019-10-30 11:30:49 +00:00
' tr ' +
' < " row m-t " < " col-md-8 " < " #op.col-md-6 " >< " col-md-6 text-center " i>>< " col-md-4 " p>> ' ;
2019-09-12 09:58:52 +00:00
var table = ele . DataTable ( {
2018-07-25 07:05:28 +00:00
pageLength : options . pageLength | | 15 ,
2019-07-03 08:29:39 +00:00
/ / dom : options . dom | | ' < " #uc.pull-left " >fltr< " row m-t " < " col-md-8 " < " #op.col-md-6 " >< " col-md-6 text-center " i>>< " col-md-4 " p>> ' ,
2019-10-30 11:30:49 +00:00
/ / dom : options . dom | | ' < " #uc.pull-left " >< " pull-right " < " inline " l>< " #fb.inline " >< " inline " < " table-filter " f>>< " #fa.inline " >>tr< " row m-t " < " col-md-8 " < " #op.col-md-6 " >< " col-md-6 text-center " i>>< " col-md-4 " p>> ' ,
2019-12-05 07:09:25 +00:00
dom : options . dom | | dom ,
2018-07-25 07:05:28 +00:00
order : options . order | | [ ] ,
buttons : [ ] ,
2018-01-15 09:41:49 +00:00
columnDefs : columnDefs ,
serverSide : true ,
processing : true ,
2019-07-03 10:03:01 +00:00
searchDelay : 800 ,
2019-10-30 11:30:49 +00:00
oSearch : options . oSearch ,
2018-01-15 09:41:49 +00:00
ajax : {
2019-09-12 09:58:52 +00:00
url : options . ajax_url ,
error : function ( jqXHR , textStatus , errorThrown ) {
2023-01-30 07:19:05 +00:00
if ( jqXHR . responseText & & jqXHR . responseText . indexOf ( " %(value)s " ) != = - 1 ) {
2019-10-16 07:29:44 +00:00
return
}
2019-03-18 03:34:13 +00:00
var msg = gettext ( " Unknown error occur " ) ;
if ( jqXHR . responseJSON ) {
if ( jqXHR . responseJSON . error ) {
msg = jqXHR . responseJSON . error
} else if ( jqXHR . responseJSON . msg ) {
msg = jqXHR . responseJSON . msg
}
}
alert ( msg )
} ,
2018-01-15 09:41:49 +00:00
data : function ( data ) {
delete data . columns ;
2019-09-12 09:58:52 +00:00
if ( data . length != = null ) {
2018-01-15 09:41:49 +00:00
data . limit = data . length ;
delete data . length ;
}
if ( data . start != = null ) {
data . offset = data . start ;
delete data . start ;
}
if ( data . search != = null ) {
2019-10-25 04:08:49 +00:00
var searchValue = data . search . value ;
var searchFilter = parseTableFilter ( searchValue ) ;
2019-10-25 04:49:22 +00:00
if ( Object . keys ( searchFilter ) . length == = 0 ) {
data . search = searchValue ;
} else {
2019-10-25 04:52:21 +00:00
data . search = ' ' ;
2019-10-25 04:49:22 +00:00
$ . each ( searchFilter , function ( k , v ) {
data [ k ] = v
} )
}
2018-01-15 09:41:49 +00:00
}
if ( data . order != = null & & data . order . length == = 1 ) {
var col = data . order [ 0 ] . column ;
var order = options . columns [ col ] . data ;
2018-11-12 08:45:57 +00:00
if ( data . order [ 0 ] . dir == = " desc " ) {
2018-01-15 09:41:49 +00:00
order = " - " + order ;
}
data . order = order ;
}
} ,
2019-09-12 09:58:52 +00:00
dataFilter : function ( data ) {
var json = jQuery . parseJSON ( data ) ;
2018-01-15 10:14:58 +00:00
json . recordsTotal = json . count ;
json . recordsFiltered = json . count ;
return JSON . stringify ( json ) ; / / return JSON string
} ,
2018-01-15 09:41:49 +00:00
dataSrc : " results "
} ,
columns : options . columns | | [ ] ,
select : options . select | | select ,
2018-07-25 03:21:12 +00:00
language : jumpserver . language ,
2019-06-28 14:07:22 +00:00
lengthMenu : options . lengthMenu | | [ [ 15 , 25 , 50 , 9999 ] , [ 15 , 25 , 50 , ' All ' ] ]
2018-01-15 09:41:49 +00:00
} ) ;
2018-04-13 07:48:10 +00:00
table . selected = [ ] ;
2018-12-25 05:33:37 +00:00
table . selected_rows = [ ] ;
2019-09-12 09:58:52 +00:00
table . on ( ' select ' , function ( e , dt , type , indexes ) {
var $ node = table [ type ] ( indexes ) . nodes ( ) . to $ ( ) ;
2018-01-15 09:41:49 +00:00
$ node . find ( ' input.ipt_check ' ) . prop ( ' checked ' , true ) ;
2018-04-13 07:48:10 +00:00
jumpserver . selected [ $ node . find ( ' input.ipt_check ' ) . prop ( ' id ' ) ] = true ;
if ( type == = ' row ' ) {
var rows = table . rows ( indexes ) . data ( ) ;
$ . each ( rows , function ( id , row ) {
2019-09-12 09:58:52 +00:00
if ( row . id & & $ . inArray ( row . id , table . selected ) == = - 1 ) {
2023-01-30 07:19:05 +00:00
if ( select . style == = ' multi ' ) {
2019-12-30 08:40:38 +00:00
table . selected . push ( row . id ) ;
table . selected_rows . push ( row ) ;
2023-01-30 07:19:05 +00:00
} else {
2019-12-30 08:40:38 +00:00
table . selected = [ row . id ] ;
table . selected_rows = [ row ] ;
}
2018-04-13 07:48:10 +00:00
}
} )
}
2019-09-12 09:58:52 +00:00
} ) . on ( ' deselect ' , function ( e , dt , type , indexes ) {
var $ node = table [ type ] ( indexes ) . nodes ( ) . to $ ( ) ;
2016-11-14 16:48:48 +00:00
$ node . find ( ' input.ipt_check ' ) . prop ( ' checked ' , false ) ;
2018-04-13 07:48:10 +00:00
jumpserver . selected [ $ node . find ( ' input.ipt_check ' ) . prop ( ' id ' ) ] = false ;
if ( type == = ' row ' ) {
var rows = table . rows ( indexes ) . data ( ) ;
$ . each ( rows , function ( id , row ) {
2019-09-12 09:58:52 +00:00
if ( row . id ) {
2018-04-13 07:48:10 +00:00
var index = table . selected . indexOf ( row . id ) ;
2019-09-12 09:58:52 +00:00
if ( index > - 1 ) {
table . selected . splice ( index , 1 ) ;
table . selected_rows . splice ( index , 1 ) ;
2018-04-13 07:48:10 +00:00
}
}
} )
}
2019-09-12 09:58:52 +00:00
} ) . on ( ' draw ' , function ( ) {
2019-06-24 12:16:18 +00:00
$ ( ' [data-toggle= " popover " ] ' ) . popover ( {
html : true ,
placement : ' bottom ' ,
trigger : ' click ' ,
container : ' body '
} ) . on ( ' click ' , function ( e ) {
$ ( ' [data-toggle= " popover " ] ' ) . not ( this ) . popover ( ' hide ' ) ;
} ) ;
2018-04-13 07:48:10 +00:00
var table_data = [ ] ;
$ . each ( table . rows ( ) . data ( ) , function ( id , row ) {
if ( row . id ) {
table_data . push ( row . id )
}
} ) ;
$ . each ( table . selected , function ( id , data ) {
var index = table_data . indexOf ( data ) ;
2019-09-12 09:58:52 +00:00
if ( index > - 1 ) {
2018-04-13 07:48:10 +00:00
table . rows ( index ) . select ( )
}
} ) ;
2019-07-03 08:29:39 +00:00
} ) . on ( " init " , function ( ) {
$ ( ' #op ' ) . html ( options . op_html | | ' ' ) ;
$ ( ' #uc ' ) . html ( options . uc_html | | ' ' ) ;
$ ( ' #fb ' ) . html ( options . fb_html | | ' ' ) ;
$ ( ' #fa ' ) . html ( options . fa_html | | ' ' ) ;
2020-03-12 08:24:38 +00:00
$ ( ' #lb ' ) . html ( options . lb_html | | ' ' ) ;
2016-11-14 16:48:48 +00:00
} ) ;
2018-04-13 07:48:10 +00:00
var table_id = table . settings ( ) [ 0 ] . sTableId ;
2019-09-12 09:58:52 +00:00
$ ( ' # ' + table_id + ' .ipt_check_all ' ) . on ( ' click ' , function ( ) {
if ( select_style != = ' multi ' ) {
return
}
2018-02-06 10:32:02 +00:00
if ( $ ( this ) . prop ( " checked " ) ) {
$ ( this ) . closest ( ' table ' ) . find ( ' .ipt_check ' ) . prop ( ' checked ' , true ) ;
2019-09-12 09:58:52 +00:00
table . rows ( { search : ' applied ' , page : ' current ' } ) . select ( ) ;
2018-02-06 10:32:02 +00:00
} else {
$ ( this ) . closest ( ' table ' ) . find ( ' .ipt_check ' ) . prop ( ' checked ' , false ) ;
2019-09-12 09:58:52 +00:00
table . rows ( { search : ' applied ' , page : ' current ' } ) . deselect ( ) ;
2018-02-06 10:32:02 +00:00
}
2017-12-31 04:20:08 +00:00
} ) ;
2016-11-14 16:48:48 +00:00
2018-02-06 10:32:02 +00:00
/ / jumpserver . table = table ;
2016-09-27 13:28:02 +00:00
return table ;
2016-10-24 11:32:53 +00:00
} ;
2016-12-14 01:54:16 +00:00
/ * *
* 替换所有匹配exp的字符串为指定字符串
* @param exp 被替换部分的正则
* @param newStr 替换成的字符串
* /
String . prototype . replaceAll = function ( exp , newStr ) {
return this . replace ( new RegExp ( exp , " gm " ) , newStr ) ;
} ;
/ * *
* 原型 : 字符串格式化
* @param args 格式化参数值
* /
2019-09-12 09:58:52 +00:00
String . prototype . format = function ( args ) {
2016-12-14 01:54:16 +00:00
var result = this ;
if ( arguments . length < 1 ) {
return result ;
}
var data = arguments ;
if ( arguments . length == 1 & & typeof ( args ) == " object " ) {
data = args ;
}
2019-09-12 09:58:52 +00:00
for ( var key in data ) {
2016-12-14 01:54:16 +00:00
var value = data [ key ] ;
if ( undefined != value ) {
result = result . replaceAll ( " \\ { " + key + " \\ } " , value ) ;
}
}
return result ;
2017-12-12 06:28:12 +00:00
} ;
2017-12-21 03:31:13 +00:00
2019-05-20 04:30:55 +00:00
function setCookie ( key , value , time ) {
2017-12-21 03:31:13 +00:00
var expires = new Date ( ) ;
2019-05-20 04:30:55 +00:00
if ( ! time ) {
2019-09-12 09:58:52 +00:00
time = expires . getTime ( ) + ( 24 * 60 * 60 * 1000 ) ;
2019-05-20 04:30:55 +00:00
}
expires . setTime ( time ) ;
2018-01-02 09:02:03 +00:00
document . cookie = key + ' = ' + value + ' ;expires= ' + expires . toUTCString ( ) + ' ;path=/ ' ;
}
function delCookie ( key ) {
var expires = new Date ( ) ;
expires . setTime ( expires . getTime ( ) - 1 ) ;
var val = getCookie ( key ) ;
if ( val != = null ) {
document . cookie = key + ' = ' + val + " ;expires " + expires . toUTCString ( ) + ' ;path=/ ' ;
}
2017-12-21 03:31:13 +00:00
}
2018-01-30 10:07:51 +00:00
function createPopover ( dataset , title , callback ) {
2019-09-12 09:58:52 +00:00
if ( callback != = undefined ) {
2018-01-30 10:07:51 +00:00
var new_dataset = [ ] ;
$ . each ( dataset , function ( index , value ) {
new_dataset . push ( callback ( value ) )
} ) ;
dataset = new_dataset ;
}
2019-09-12 03:17:15 +00:00
var data_content = dataset . join ( " <br> " ) ;
2018-01-30 10:07:51 +00:00
var html = " <a data-toggle= ' popover ' data-content= ' " + data_content + " ' > " + dataset . length + " </a> " ;
return html ;
2018-02-02 09:06:08 +00:00
}
2019-09-12 09:58:52 +00:00
$ ( function ( ) {
2018-02-02 09:06:08 +00:00
( function ( $ ) {
$ . getUrlParam = function ( name ) {
var reg = new RegExp ( " (^|&) " + name + " =([^&]*)(&|$) " ) ;
var r = window . location . search . substr ( 1 ) . match ( reg ) ;
2019-09-12 09:58:52 +00:00
if ( r != null ) return unescape ( r [ 2 ] ) ;
return null ;
2018-02-02 09:06:08 +00:00
}
} ) ( jQuery ) ;
} ) ;
function getUrlParam ( name ) {
var reg = new RegExp ( " (^|&) " + name + " =([^&]*)(&|$) " ) ;
var r = window . location . search . substr ( 1 ) . match ( reg ) ;
2019-09-12 09:58:52 +00:00
if ( r != null ) return unescape ( r [ 2 ] ) ;
return null ;
2018-02-02 09:06:08 +00:00
}
function setUrlParam ( url , name , value ) {
var urlArray = url . split ( " ? " ) ;
2019-09-12 09:58:52 +00:00
if ( urlArray . length == = 1 ) {
2018-02-02 09:06:08 +00:00
url + = " ? " + name + " = " + value ;
} else {
var oriParam = urlArray [ 1 ] . split ( " & " ) ;
var oriParamMap = { } ;
$ . each ( oriParam , function ( index , value ) {
var v = value . split ( " = " ) ;
oriParamMap [ v [ 0 ] ] = v [ 1 ] ;
} ) ;
oriParamMap [ name ] = value ;
url = urlArray [ 0 ] + " ? " ;
var newParam = [ ] ;
$ . each ( oriParamMap , function ( index , value ) {
newParam . push ( index + " = " + value ) ;
} ) ;
url + = newParam . join ( " & " )
}
return url
}
2018-06-05 09:26:31 +00:00
2018-11-22 04:27:27 +00:00
/ / Password check rules
var rules_short_map_id = {
' min ' : ' id_security_password_min_length ' ,
' upper ' : ' id_security_password_upper_case ' ,
' lower ' : ' id_security_password_lower_case ' ,
' number ' : ' id_security_password_number ' ,
' special ' : ' id_security_password_special_char '
} ;
2023-06-05 07:00:25 +00:00
var rules_id_map_label = {
' id_security_password_min_length ' : gettext ( ' Password minimum length {N} bits ' ) ,
' id_security_password_upper_case ' : gettext ( ' Must contain capital letters ' ) ,
' id_security_password_lower_case ' : gettext ( ' Must contain lowercase letters ' ) ,
' id_security_password_number ' : gettext ( ' Must contain numeric characters ' ) ,
' id_security_password_special_char ' : gettext ( ' Must contain special characters ' )
} ;
2019-09-12 09:58:52 +00:00
function getRuleLabel ( rule ) {
2018-11-22 04:27:27 +00:00
var label = ' ' ;
2019-09-12 09:58:52 +00:00
if ( rule . key == = rules_short_map_id [ ' min ' ] ) {
2018-11-22 04:27:27 +00:00
label = rules_id_map_label [ rule . key ] . replace ( ' {N} ' , rule . value )
2019-09-12 09:58:52 +00:00
} else {
2018-11-22 04:27:27 +00:00
label = rules_id_map_label [ rule . key ]
}
return label
}
2018-06-05 09:26:31 +00:00
/ / 校验密码 - 改变规则颜色
function checkPasswordRules ( password , minLength ) {
if ( wordMinLength ( password , minLength ) ) {
2019-09-12 09:58:52 +00:00
$ ( ' # ' + rules_short_map_id [ ' min ' ] ) . css ( ' color ' , ' green ' )
} else {
$ ( ' # ' + rules_short_map_id [ ' min ' ] ) . css ( ' color ' , ' #908a8a ' )
2018-06-05 09:26:31 +00:00
}
if ( wordUpperCase ( password ) ) {
2019-09-12 09:58:52 +00:00
$ ( ' # ' + rules_short_map_id [ ' upper ' ] ) . css ( ' color ' , ' green ' )
} else {
$ ( ' # ' + rules_short_map_id [ ' upper ' ] ) . css ( ' color ' , ' #908a8a ' )
2018-06-05 09:26:31 +00:00
}
if ( wordLowerCase ( password ) ) {
2019-09-12 09:58:52 +00:00
$ ( ' # ' + rules_short_map_id [ ' lower ' ] ) . css ( ' color ' , ' green ' )
} else {
$ ( ' # ' + rules_short_map_id [ ' lower ' ] ) . css ( ' color ' , ' #908a8a ' )
2018-06-05 09:26:31 +00:00
}
if ( wordNumber ( password ) ) {
2019-09-12 09:58:52 +00:00
$ ( ' # ' + rules_short_map_id [ ' number ' ] ) . css ( ' color ' , ' green ' )
} else {
$ ( ' # ' + rules_short_map_id [ ' number ' ] ) . css ( ' color ' , ' #908a8a ' )
2018-06-05 09:26:31 +00:00
}
if ( wordSpecialChar ( password ) ) {
2019-09-12 09:58:52 +00:00
$ ( ' # ' + rules_short_map_id [ ' special ' ] ) . css ( ' color ' , ' green ' )
} else {
$ ( ' # ' + rules_short_map_id [ ' special ' ] ) . css ( ' color ' , ' #908a8a ' )
2018-06-05 09:26:31 +00:00
}
}
/ / 最小长度
function wordMinLength ( word , minLength ) {
/ / var minLength = { { min_length } } ;
var re = new RegExp ( " ^(. { " + minLength + " ,})$ " ) ;
return word . match ( re )
}
2019-09-12 09:58:52 +00:00
2018-06-05 09:26:31 +00:00
/ / 大写字母
function wordUpperCase ( word ) {
return word . match ( / ( [ A - Z ] + ) / )
}
2019-09-12 09:58:52 +00:00
2018-06-05 09:26:31 +00:00
/ / 小写字母
function wordLowerCase ( word ) {
return word . match ( / ( [ a - z ] + ) / )
}
2019-09-12 09:58:52 +00:00
2018-06-05 09:26:31 +00:00
/ / 数字字符
function wordNumber ( word ) {
return word . match ( / ( [ \d ] + ) / )
}
2019-09-12 09:58:52 +00:00
2018-06-05 09:26:31 +00:00
/ / 特殊字符
function wordSpecialChar ( word ) {
return word . match ( / [ ` , ~ , ! , @ , #,\$,%,\^,&,\*,\(,\),\-,_,=,\+,\{,\},\[,\],\|,\\,;,',:,",\,,\.,<,>,\/,\?]+/)
}
2018-11-22 04:27:27 +00:00
2018-06-05 09:26:31 +00:00
/ / 显示弹窗密码规则
function popoverPasswordRules ( password_check_rules , $ el ) {
var message = " " ;
2018-11-22 04:27:27 +00:00
jQuery . each ( password_check_rules , function ( idx , rule ) {
message + = " <li id= " + rule . key + " style= ' list-style-type:none; ' > <i class= ' fa fa-check-circle-o ' style= ' margin-right:10px; ' ></i> " + getRuleLabel ( rule ) + " </li> " ;
2018-06-05 09:26:31 +00:00
} ) ;
/ / $ ( ' #id_password_rules ' ) . html ( message ) ;
$ el . html ( message )
}
/ / 初始化弹窗popover
2019-09-12 09:58:52 +00:00
function initPopover ( $ container , $ progress , $ idPassword , $ el , password_check_rules , i18n_fallback ) {
2018-06-05 09:26:31 +00:00
options = { } ;
/ / User Interface
options . ui = {
container : $ container ,
viewports : {
progress : $ progress
/ / errors : $ ( ' .popover-content ' )
} ,
showProgressbar : true ,
showVerdictsInsideProgressBar : true
} ;
2018-08-01 02:44:43 +00:00
options . i18n = {
fallback : i18n_fallback ,
t : function ( key ) {
var result = ' ' ;
result = options . i18n . fallback [ key ] ;
return result == = key ? ' ' : result ;
}
} ;
2018-06-05 09:26:31 +00:00
$ idPassword . pwstrength ( options ) ;
popoverPasswordRules ( password_check_rules , $ el ) ;
}
2018-12-18 10:50:02 +00:00
2019-03-05 11:47:14 +00:00
function rootNodeAddDom ( ztree , callback ) {
var refreshIcon = " <a id= ' tree-refresh ' ><i class= ' fa fa-refresh ' ></i></a> " ;
var rootNode = ztree . getNodes ( ) [ 0 ] ;
2019-10-18 07:05:45 +00:00
if ( rootNode ) {
var $ rootNodeRef = $ ( " # " + rootNode . tId + " _a " ) ;
$ rootNodeRef . after ( refreshIcon ) ;
} else {
$ rootNodeRef = $ ( ' # ' + ztree . setting . treeId ) ;
$ rootNodeRef . html ( refreshIcon ) ;
}
2019-03-05 11:47:14 +00:00
var refreshIconRef = $ ( ' #tree-refresh ' ) ;
refreshIconRef . bind ( ' click ' , function ( ) {
ztree . destroy ( ) ;
callback ( )
} )
2019-04-25 06:31:34 +00:00
}
2019-09-12 09:58:52 +00:00
function htmlEscape ( d ) {
2019-04-25 06:31:34 +00:00
return typeof d == = ' string ' ?
d . replace ( / < / g , ' < ' ) . replace ( / > / g , ' > ' ) . replace ( / " /g, ' " ' ) :
d ;
2019-05-21 08:24:01 +00:00
}
2019-06-28 14:07:22 +00:00
function objectAttrsIsList ( obj , attrs ) {
attrs . forEach ( function ( attr ) {
2019-09-12 09:58:52 +00:00
if ( ! obj [ attr ] ) {
2019-07-05 08:34:37 +00:00
obj [ attr ] = [ ]
2019-09-12 09:58:52 +00:00
} else if ( obj [ attr ] & & ! ( obj [ attr ] instanceof Array ) ) {
2019-06-28 14:07:22 +00:00
obj [ attr ] = [ obj [ attr ] ]
}
} )
}
function objectAttrsIsDatetime ( obj , attrs ) {
attrs . forEach ( function ( attr ) {
2019-07-22 09:15:24 +00:00
obj [ attr ] = toSafeDateISOStr ( obj [ attr ] ) ;
2019-06-28 14:07:22 +00:00
} )
}
function objectAttrsIsBool ( obj , attrs ) {
attrs . forEach ( function ( attr ) {
if ( ! obj [ attr ] ) {
obj [ attr ] = false
2019-12-16 08:53:29 +00:00
} else {
obj [ attr ] = [ ' on ' , ' 1 ' , ' true ' , ' True ' ] . includes ( obj [ attr ] ) ;
2019-06-28 14:07:22 +00:00
}
} )
}
2019-07-02 14:08:50 +00:00
2020-03-12 08:24:38 +00:00
function objectAttrsIsNumber ( obj , attrs ) {
attrs . forEach ( function ( attr ) {
if ( ! obj [ attr ] ) {
obj [ attr ] = null ;
}
} )
}
2019-07-22 09:15:24 +00:00
function cleanDateStr ( d ) {
2019-09-12 09:58:52 +00:00
for ( var i = 0 ; i < 3 ; i + + ) {
if ( ! isNaN ( Date . parse ( d ) ) ) {
2019-07-22 09:15:24 +00:00
return d ;
}
if ( ! isNaN ( Number ( d ) ) ) {
return d ;
}
switch ( i ) {
case 0 :
d = d . replaceAll ( ' - ' , ' / ' ) ;
break ;
case 1 :
d = d . split ( ' + ' ) [ 0 ] . trimRight ( ) ;
break ;
2019-07-12 10:31:55 +00:00
}
}
2019-07-22 09:15:24 +00:00
return null ;
2019-07-12 10:31:55 +00:00
}
2019-07-19 04:01:17 +00:00
function safeDate ( s ) {
2019-07-22 09:15:24 +00:00
s = cleanDateStr ( s ) ;
2019-07-19 04:01:17 +00:00
return new Date ( s )
}
2019-07-22 09:15:24 +00:00
function toSafeDateISOStr ( s ) {
var d = safeDate ( s ) ;
return d . toISOString ( ) ;
}
function toSafeLocalDateStr ( d ) {
2019-07-19 04:01:17 +00:00
var date = safeDate ( d ) ;
2020-04-02 08:02:51 +00:00
/ / var date_s = date . toLocaleString ( getUserLang ( ) , { hour12 : false } ) ;
var date_s = date . toLocaleString ( getUserLang ( ) , { hourCycle : " h23 " } ) ;
2019-07-09 04:05:31 +00:00
return date_s . split ( " / " ) . join ( ' - ' )
2019-07-02 14:08:50 +00:00
}
2019-07-03 10:03:01 +00:00
function getUrlParams ( url ) {
url = url . split ( " ? " ) ;
2019-07-03 14:28:20 +00:00
var params = " " ;
2019-09-12 09:58:52 +00:00
if ( url . length == = 2 ) {
2019-07-03 10:03:01 +00:00
params = url [ 1 ] ;
}
return params
2019-07-03 14:28:20 +00:00
}
function getTimeUnits ( u ) {
var units = {
" d " : " 天 " ,
" h " : " 时 " ,
" m " : " 分 " ,
" s " : " 秒 " ,
} ;
2020-01-06 10:21:05 +00:00
if ( getUserLang ( ) == = " zh-CN " ) {
2019-07-03 14:28:20 +00:00
return units [ u ]
}
return u
}
function timeOffset ( a , b ) {
2019-07-19 05:10:34 +00:00
var start = safeDate ( a ) ;
var end = safeDate ( b ) ;
2019-09-12 09:58:52 +00:00
var offset = ( end - start ) / 1000 ;
2019-09-19 13:21:05 +00:00
return readableSecond ( offset )
}
2019-07-03 14:28:20 +00:00
2019-09-19 13:21:05 +00:00
function readableSecond ( offset ) {
2019-07-03 14:28:20 +00:00
var days = offset / 3600 / 24 ;
var hours = offset / 3600 ;
var minutes = offset / 60 ;
var seconds = offset ;
if ( days > 1 ) {
return days . toFixed ( 1 ) + " " + getTimeUnits ( " d " ) ;
} else if ( hours > 1 ) {
return hours . toFixed ( 1 ) + " " + getTimeUnits ( " h " ) ;
} else if ( minutes > 1 ) {
return minutes . toFixed ( 1 ) + " " + getTimeUnits ( " m " )
} else if ( seconds > 1 ) {
return seconds . toFixed ( 1 ) + " " + getTimeUnits ( " s " )
}
return " "
2019-07-08 07:35:20 +00:00
}
function readFile ( ref ) {
var files = ref . prop ( ' files ' ) ;
var hasFile = files & & files . length > 0 ;
if ( hasFile ) {
var reader = new FileReader ( ) ; / / 新建一个FileReader
reader . readAsText ( files [ 0 ] , " UTF-8 " ) ; / / 读取文件
2019-09-12 09:58:52 +00:00
reader . onload = function ( evt ) { / / 读取完文件之后会回来这里
2019-07-08 07:35:20 +00:00
ref . trigger ( " onload " , evt . target . result ) ;
} ;
} else {
ref . trigger ( " onload " , null ) ;
}
return ref
}
2019-12-05 07:09:25 +00:00
function select2AjaxInit ( option ) {
/ *
{
selector :
url : ,
disabledData : ,
displayFormat ,
idFormat ,
2019-09-27 06:18:51 +00:00
}
2019-12-05 07:09:25 +00:00
* /
var selector = option . selector ;
var url = option . url ;
var disabledData = option . disabledData ;
var displayFormat = option . displayFormat | | function ( data ) {
return data . name ;
} ;
var idFormat = option . idFormat | | function ( data ) {
return data . id ;
} ;
2019-08-21 12:27:21 +00:00
return $ ( selector ) . select2 ( {
closeOnSelect : false ,
ajax : {
url : url ,
data : function ( params ) {
var page = params . page | | 1 ;
var query = {
2019-09-12 09:58:52 +00:00
search : params . term ,
offset : ( page - 1 ) * 10 ,
limit : 10
2019-08-21 12:27:21 +00:00
} ;
return query
} ,
processResults : function ( data ) {
var results = $ . map ( data . results , function ( v , i ) {
2019-12-05 07:09:25 +00:00
var display = displayFormat ( v ) ;
var id = idFormat ( v ) ;
var d = { id : id , text : display } ;
if ( disabledData & & disabledData . indexOf ( v . id ) != = - 1 ) {
d . disabled = true ;
}
return d ;
2019-08-21 12:27:21 +00:00
} ) ;
var more = ! ! data . next ;
2019-09-12 09:58:52 +00:00
return { results : results , pagination : { " more " : more } }
2019-08-21 12:27:21 +00:00
}
} ,
} )
2019-12-05 07:09:25 +00:00
2019-08-21 12:27:21 +00:00
}
2019-12-05 07:09:25 +00:00
function usersSelect2Init ( selector , url , disabledData ) {
2019-09-27 06:18:51 +00:00
if ( ! url ) {
url = ' /api/v1/users/users/ '
}
2023-01-30 07:19:05 +00:00
2019-12-05 07:09:25 +00:00
function displayFormat ( v ) {
2023-01-30 07:19:05 +00:00
return v . name + ' ( ' + v . username + ' ) ' ;
2019-12-05 07:09:25 +00:00
}
2023-01-30 07:19:05 +00:00
2019-12-05 07:09:25 +00:00
var option = {
url : url ,
selector : selector ,
disabledData : disabledData ,
displayFormat : displayFormat
} ;
return select2AjaxInit ( option )
}
2019-09-27 06:18:51 +00:00
2019-12-05 07:09:25 +00:00
function nodesSelect2Init ( selector , url , disabledData ) {
if ( ! url ) {
url = ' /api/v1/assets/nodes/ '
}
2023-01-30 07:19:05 +00:00
2019-12-05 07:09:25 +00:00
function displayFormat ( v ) {
return v . full_value ;
}
2023-01-30 07:19:05 +00:00
2019-12-05 07:09:25 +00:00
var option = {
url : url ,
selector : selector ,
disabledData : disabledData ,
displayFormat : displayFormat
} ;
return select2AjaxInit ( option )
2019-09-27 06:18:51 +00:00
}
2019-09-26 11:22:17 +00:00
function showCeleryTaskLog ( taskId ) {
var url = ' /ops/celery/task/taskId/log/ ' . replace ( ' taskId ' , taskId ) ;
window . open ( url , ' ' , ' width=900,height=600 ' )
}
2019-11-05 10:46:29 +00:00
2023-01-30 07:19:05 +00:00
function getUserLang ( ) {
2020-03-12 08:24:38 +00:00
let userLangEN = document . cookie . indexOf ( ' django_language=en ' ) ;
2023-01-30 07:19:05 +00:00
if ( userLangEN == = - 1 ) {
2020-01-06 10:21:05 +00:00
return ' zh-CN '
2023-01-30 07:19:05 +00:00
} else {
2020-01-06 10:21:05 +00:00
return ' en-US '
}
}
2019-11-05 10:46:29 +00:00
function initDateRangePicker ( selector , options ) {
if ( ! options ) {
options = { }
}
var zhLocale = {
format : ' YYYY-MM-DD HH:mm ' ,
separator : ' ~ ' ,
applyLabel : " 应用 " ,
cancelLabel : " 取消 " ,
resetLabel : " 重置 " ,
daysOfWeek : [ " 日 " , " 一 " , " 二 " , " 三 " , " 四 " , " 五 " , " 六 " ] , / / 汉化处理
monthNames : [ " 一月 " , " 二月 " , " 三月 " , " 四月 " , " 五月 " , " 六月 " , " 七月 " , " 八月 " , " 九月 " , " 十月 " , " 十一月 " , " 十二月 " ] ,
} ;
2020-01-06 10:21:05 +00:00
var enLocale = {
format : " YYYY-MM-DD HH:mm " ,
separator : " - " ,
applyLabel : " Apply " ,
cancelLabel : " Cancel " ,
resetLabel : " Reset " ,
daysOfWeek : [ " Su " , " Mo " , " Tu " , " We " , " Th " , " Fr " , " Sa " ] ,
monthNames : [ " January " , " February " , " March " , " April " , " May " , " June " , " July " , " August " , " September " , " October " , " November " , " December " ] ,
} ;
2019-11-05 10:46:29 +00:00
var defaultOption = {
singleDatePicker : true ,
showDropdowns : true ,
timePicker : true ,
timePicker24Hour : true ,
autoApply : true ,
} ;
2020-01-06 10:21:05 +00:00
if ( getUserLang ( ) == = ' zh-CN ' ) {
2019-11-05 10:46:29 +00:00
defaultOption . locale = zhLocale ;
2023-01-30 07:19:05 +00:00
} else {
2020-01-06 10:21:05 +00:00
/ / en - US
defaultOption . locale = enLocale ;
}
2019-11-05 10:46:29 +00:00
options = Object . assign ( defaultOption , options ) ;
return $ ( selector ) . daterangepicker ( options ) ;
}
2019-11-08 07:48:01 +00:00
function reloadPage ( ) {
2023-01-30 07:19:05 +00:00
setTimeout ( function ( ) {
window . location . reload ( ) ;
} , 300 ) ;
2019-11-08 07:48:01 +00:00
}
2020-03-12 08:24:38 +00:00
function isEmptyObject ( obj ) {
return Object . keys ( obj ) . length == = 0
}
function getStatusIcon ( status , mapping , title ) {
var navy = ' <i class= " fa fa-circle text-navy " title= " " ></i> ' ;
var danger = ' <i class= " fa fa-circle text-danger " title= " " ></i> ' ;
var warning = ' <i class= " fa fa-circle text-warning " title= " " ></i> ' ;
var icons = {
navy : navy ,
danger : danger ,
warning : warning
} ;
var defaultMapping = {
true : ' navy ' ,
false : ' danger ' ,
1 : ' navy ' ,
0 : ' danger ' ,
default : ' navy '
} ;
if ( ! mapping ) {
2023-01-30 07:19:05 +00:00
mapping = defaultMapping ;
2020-03-12 08:24:38 +00:00
}
var name = mapping [ status ] | | mapping [ ' default ' ] ;
var icon = icons [ name ] ;
if ( title ) {
icon = icon . replace ( ' title= " " ' , ' title= " ' + title + ' " ' )
}
return icon ;
}
2022-05-07 08:20:12 +00:00
2022-05-10 09:28:10 +00:00
function fillKey ( key ) {
2022-08-05 06:53:23 +00:00
const KeyLength = 16
if ( key . length > KeyLength ) {
key = key . slice ( 0 , KeyLength )
2022-05-10 09:28:10 +00:00
}
2022-08-05 06:53:23 +00:00
const filledKey = Buffer . alloc ( KeyLength )
2022-05-10 09:28:10 +00:00
const keys = Buffer . from ( key )
for ( let i = 0 ; i < keys . length ; i + + ) {
filledKey [ i ] = keys [ i ]
}
return filledKey
}
function aesEncrypt ( text , originKey ) {
const key = CryptoJS . enc . Utf8 . parse ( fillKey ( originKey ) ) ;
return CryptoJS . AES . encrypt ( text , key , {
mode : CryptoJS . mode . ECB ,
padding : CryptoJS . pad . ZeroPadding
} ) . toString ( ) ;
}
function rsaEncrypt ( text , pubKey ) {
if ( ! text ) {
return text
}
const jsEncrypt = new JSEncrypt ( ) ;
jsEncrypt . setPublicKey ( pubKey ) ;
return jsEncrypt . encrypt ( text ) ;
}
function rsaDecrypt ( cipher , pkey ) {
const jsEncrypt = new JSEncrypt ( ) ;
jsEncrypt . setPrivateKey ( pkey ) ;
return jsEncrypt . decrypt ( cipher )
}
window . rsaEncrypt = rsaEncrypt
window . rsaDecrypt = rsaDecrypt
2022-05-07 08:20:12 +00:00
function encryptPassword ( password ) {
if ( ! password ) {
return ' '
}
2022-05-10 09:28:10 +00:00
/ / public key 是 base64 存储的
2022-09-13 03:26:26 +00:00
let rsaPublicKeyText = getCookie ( ' jms_public_key ' )
if ( ! rsaPublicKeyText ) {
return password
}
const aesKey = ( Math . random ( ) + 1 ) . toString ( 36 ) . substring ( 2 )
rsaPublicKeyText = rsaPublicKeyText . replaceAll ( ' " ' , ' ' )
2022-05-10 09:28:10 +00:00
const rsaPublicKey = atob ( rsaPublicKeyText )
const keyCipher = rsaEncrypt ( aesKey , rsaPublicKey )
const passwordCipher = aesEncrypt ( password , aesKey )
return ` $ { keyCipher } : $ { passwordCipher } `
2022-05-07 08:20:12 +00:00
}
2022-05-10 09:28:10 +00:00
function randomString ( length ) {
2023-01-30 07:19:05 +00:00
const characters = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ' ;
2022-05-10 09:28:10 +00:00
let result = ' ' ;
const charactersLength = characters . length ;
2023-01-30 07:19:05 +00:00
for ( let i = 0 ; i < length ; i + + ) {
2022-05-10 09:28:10 +00:00
result + = characters . charAt ( Math . floor ( Math . random ( ) * charactersLength ) ) ;
}
return result ;
}
function testEncrypt ( ) {
const radio = [ ]
const len2 = [ ]
2023-01-30 07:19:05 +00:00
for ( let i = 1 ; i < 4096 ; i + + ) {
2022-05-10 09:28:10 +00:00
const password = randomString ( i )
const cipher = encryptPassword ( password )
len2 . push ( [ password . length , cipher . length ] )
2023-01-30 07:19:05 +00:00
radio . push ( cipher . length / password . length )
2022-05-10 09:28:10 +00:00
}
return radio
}
2022-05-07 08:20:12 +00:00
window . encryptPassword = encryptPassword