const kPermissionTypeCam=0;
const kPermissionTypeLive=1;
const kPermissionTypeLiveCaptures=2;
const kPermissionTypeLiveCapturesControl=3;
const kPermissionTypeAdmin=4;
const kPermissionTypeGroup=5;
const permission_type_descs=[Loc('Custom'),Loc('Live'),Loc('Live, Captures'),Loc('Live, Captures, Control'),Loc('Administrator'),Loc('Custom')];
class AccountPicker{
constructor(json,callback){
this.camSel=document.getElementById('cams');
this.groupSel=document.getElementById('groups');
this.usernameTxt=document.getElementById('username');
this.passwordTxt=document.getElementById('password');
this.maxSecondsTxt=document.getElementById('maxSeconds');
this.maxFpsTxt=document.getElementById('maxFps');
this.maxAgeTxt=document.getElementById('maxAge');
this.maxResSel=document.getElementById('maxRes');
this.startTimeTxt=document.getElementById('startTime');
this.endTimeTxt=document.getElementById('endTime');
this.permissionSel=document.getElementById('permissionType');
this.timedAccessCheck=document.getElementById('timedAccess');
this.camSel.onchange=()=>this.setCheckboxes();
this.groupSel.onchange=()=>this.setCheckboxes();
this.permissionSel.onchange=()=>this.typeChanged();
let feedback=document.getElementById('feedbackDiv');
document.getElementById('accountSave').onclick=()=>{
if(this.usernameTxt.value==''){feedback.textContent=Loc('Please enter a username');feedback.style.display='block';}
else if(this.passwordTxt.value==''){feedback.textContent=Loc('Please enter a password');feedback.style.display='block';}
else{
document.getElementById('accountPanel').style.display='none';
let displayName=(this.usernameTxt.value?this.usernameTxt.value:Loc('untitled'))+' ('+permission_type_descs[parseInt(this.permissionSel.value)]+')';
callback(displayName,this.json());
}
}
document.getElementById('accountCancel').onclick=()=>{
document.getElementById('accountPanel').style.display='none';
callback(null,null);
}
for(let i=0;i<32;i++){
let box=document.getElementById('p'+i);
if(box)box.onchange=()=>{this.boxChanged(box)};
}
this.setJson(json);
document.getElementById('accountPanel').style.display='flex';
}
setJson(json){
let d=JSON.parse(json);
this.usernameTxt.value=d.username?d.username:'';
this.passwordTxt.value=d.password?d.password:'';
this.permissionSel.value=(d.permissionType!=null)?d.permissionType:1;
this.maxSecondsTxt.value=d.maxSeconds?d.maxSeconds:'';
this.maxFpsTxt.value=d.maxFps?d.maxFps:'';
this.maxAgeTxt.value=d.maxAge?d.maxAge:'';
this.maxResSel.value=d.maxRes?d.maxRes:'0';
this.timedAccessCheck.checked=d.timedAccess?true:false;
this.startTimeTxt.value=d.startTime?d.startTime:'00:00';
this.endTimeTxt.value=d.endTime?d.endTime:'00:00';
this.camPerms=(d.permissionType==kPermissionTypeCam && d.permissions!=null)?d.permissions:[];
this.groupPerms=(d.permissionType==kPermissionTypeGroup && d.permissions!=null)?d.permissions:[];
while(this.camPerms.length<257)this.camPerms.push(0);
while(this.groupPerms.length<257)this.groupPerms.push(0);
this.camSel.selectedIndex=-1;
this.groupSel.selectedIndex=-1;
this.typeChanged();
}
json(){
let d={
username:this.usernameTxt.value,
password:this.passwordTxt.value,
permissionType:parseInt(this.permissionSel.value),
maxSeconds:this.maxSecondsTxt.value?parseInt(this.maxSecondsTxt.value):0,
maxFps:this.maxFpsTxt.value?parseInt(this.maxFpsTxt.value):0,
maxAge:this.maxAgeTxt.value?parseInt(this.maxAgeTxt.value):0,
maxRes:parseInt(this.maxResSel.value),
timedAccess:this.timedAccessCheck.checked,
startTime:this.startTimeTxt.value,
endTime:this.endTimeTxt.value,
};
if(d.permissionType==kPermissionTypeCam)d.permissions=this.camPerms;
else if(d.permissionType==kPermissionTypeGroup)d.permissions=this.groupPerms;
else d.permissions=[];
return JSON.stringify(d);
}
boxChanged(box){
let type=parseInt(this.permissionSel.value);
if(type!=kPermissionTypeCam && type!=kPermissionTypeGroup)return;
let sel=(type==kPermissionTypeCam)?this.camSel:this.groupSel;
let perms=(type==kPermissionTypeCam)?this.camPerms:this.groupPerms;
box.indeterminate=false;
let bit=1<<parseInt(box.id.slice(1));
for(let opt of sel.options){
if(opt.selected){
if(box.checked)perms[opt.value]|=bit;
else perms[opt.value]&=~bit;
}
}
}
setCheckboxes(){
let type=parseInt(this.permissionSel.value);
if(type!=kPermissionTypeCam && type!=kPermissionTypeGroup)return;
let sel=(type==kPermissionTypeCam)?this.camSel:this.groupSel;
let perms=(type==kPermissionTypeCam)?this.camPerms:this.groupPerms;
let arr=new Array(32).fill(0);
let selectedCount=0;
for(let opt of sel.options){
if(opt.selected){
if(perms[opt.value])for(let j=0;j<32;j++)if(perms[opt.value]&(1<<j))arr[j]++;
selectedCount++;
}
}
for(let i=0;i<32;i++){
let box=document.getElementById('p'+i);
if(box){
box.checked=arr[i]?true:false;
box.indeterminate=(arr[i] && arr[i]<selectedCount)?true:false;
box.disabled=selectedCount?false:true;
}
}
}
typeChanged(){
let type=parseInt(this.permissionSel.value);
if(type==kPermissionTypeCam){
document.getElementById('accountTable').style.display='';
document.getElementById('camTd').style.display='';
document.getElementById('groupTd').style.display='none';
}else if(type==kPermissionTypeGroup){
document.getElementById('accountTable').style.display='';
document.getElementById('camTd').style.display='none';
document.getElementById('groupTd').style.display='';
}else{
document.getElementById('accountTable').style.display='none';
}
this.setCheckboxes();
}
}
