[oform] Implement method to remove role

Also implement method to count the number of fields in field group
This commit is contained in:
KirillovIlya
2022-11-30 15:56:26 +03:00
parent b2c1f43e3b
commit f6c07aa139
5 changed files with 132 additions and 16 deletions

View File

@ -272,7 +272,7 @@
/**
* @returns {AscOForm.CUserMaster}
*/
CDocument.prototype.getDefaultUser = function()
CDocument.prototype.getDefaultUserMaster = function()
{
return this.DefaultUser;
};
@ -394,6 +394,18 @@
return max;
};
CDocument.prototype.getAllFieldsByUserMaster = function(userMaster)
{
let fields = [];
for (let index = 0, count = this.FieldMasters.length; index < count; ++index)
{
let fieldMaster = this.FieldMasters[index];
if (fieldMaster.checkUser(userMaster))
fields.push(fieldMaster);
}
return fields;
};
CDocument.prototype.onChangeFieldGroups = function()
{
if (!this.OForm)

View File

@ -109,6 +109,18 @@
this.Users.splice(index, 1);
this.onChange();
};
CFieldGroup.prototype.clear = function()
{
while (this.Users.length)
{
this.removeUser(this.Users[this.Users.length - 1]);
}
while (this.Fields.length)
{
this.removeField(this.Fields[this.Fields.length - 1]);
}
};
CFieldGroup.prototype.onChange = function()
{
if (!this.Parent)
@ -116,6 +128,24 @@
this.Parent.onChangeFieldGroup(this);
};
CFieldGroup.prototype.getAllFields = function()
{
let fields = this.Fields.slice();
if (this.Users.length && this.Parent)
{
for (let index = 0, count = this.Users.length; index < count; ++index)
{
let userFields = this.Parent.getAllFieldsByUserMaster(this.Users[index]);
for (let fieldIndex = 0, fieldCount = userFields.length; fieldIndex < fieldCount; ++fieldIndex)
{
if (-1 === fields.indexOf(userFields[fieldIndex]))
fields.push(userFields[fieldIndex]);
}
}
}
return fields;
};
CFieldGroup.prototype.getFirstUser = function()
{
let user = null;

View File

@ -112,6 +112,16 @@
AscCommon.History.Add(new AscDFH.CChangesOFormFieldMasterAddRemoveSigner(this, user.GetId(), false));
this.Signers.splice(index, 1);
};
CFieldMaster.prototype.checkUser = function(user)
{
for (let index = 0, count = this.Users.length; index < count; ++index)
{
if (this.Users[index] === user)
return true;
}
return false;
};
CFieldMaster.prototype.readAttrXml = function(name, reader)
{
switch (name)

View File

@ -42,7 +42,7 @@
function OForm(document)
{
this.Format = new AscOForm.CDocument(this);
this.DefaultUser = this.Format.getDefaultUser();
this.DefaultUser = this.Format.getDefaultUserMaster();
this.Document = document;
// Сейчас у нас роль - это ровно один userMaster и ровно одна группа полей
@ -111,16 +111,72 @@
};
OForm.prototype.removeRole = function(name, delegateName)
{
this.updateRoles();
let roleIndex = this.getRoleIndex(name);
if (-1 === roleIndex)
return false;
let userMaster = this.Roles[roleIndex].getUserMaster();
let fieldGroup = this.Roles[roleIndex].getFieldGroup();
let fields = fieldGroup.getAllFields();
let delegateIndex = this.getRoleIndex(delegateName);
// На самом деле можно убрать эту проверку, но тогда мы просто удалим группу по умолчнию и заново её добавим
if (this.Roles.length <= 1
&& this.Roles[roleIndex].getUserMaster() === this.Format.getDefaultUserMaster()
&& -1 === delegateIndex)
return false;
if (!this.startAction(AscDFH.historydescription_OForm_RemoveRole))
return false;
fieldGroup.clear();
this.Format.removeFieldGroup(fieldGroup);
this.Format.removeUserMaster(userMaster);
if (fields.length > 0)
{
// TODO: При удалении все поля переназначить на заданную роль
let delegateUserMaster, delegateFieldGroup;
if (-1 === delegateIndex || delegateIndex === roleIndex)
{
let defaultRole = this.getDefaultRole();
if (defaultRole)
{
delegateUserMaster = defaultRole.getUserMaster();
delegateFieldGroup = defaultRole.getFieldGroup();
}
else
{
let defaultGroup = new AscOForm.CFieldGroup();
defaultGroup.setWeight(this.Format.getMaxWeight() + 1);
this.Format.addFieldGroup(defaultGroup);
defaultGroup.addUser(this.Format.getDefaultUserMaster());
delegateUserMaster = this.Format.getDefaultUserMaster();
delegateFieldGroup = defaultGroup;
}
}
else
{
delegateUserMaster = this.Roles[delegateIndex].getUserMaster();
delegateFieldGroup = this.Roles[delegateIndex].getFieldGroup();
}
for (let index = 0, count = fields.length; index < count; ++index)
{
let fieldMaster = fields[index];
fieldMaster.removeUser(userMaster);
fieldMaster.addUser(delegateUserMaster);
}
delegateFieldGroup.addUser(delegateUserMaster);
this.Format.addFieldGroup(delegateFieldGroup);
}
this.endAction();
return true;
@ -319,7 +375,7 @@
{
this.updateRoles();
let defaultUser = this.Format.getDefaultUser();
let defaultUser = this.Format.getDefaultUserMaster();
for (let index = 0, count = this.Roles.length; index < count; ++index)
{
if (defaultUser === this.Roles[index].getUserMaster())
@ -402,7 +458,7 @@
let defaultGroup = new AscOForm.CFieldGroup();
defaultGroup.setWeight(this.Format.getMaxWeight() + 1);
this.Format.addFieldGroup(defaultGroup);
defaultGroup.addUser(this.Format.getDefaultUser());
defaultGroup.addUser(this.Format.getDefaultUserMaster());
};
OForm.prototype.onEndLoad = function()
{

View File

@ -49,8 +49,16 @@
CRole.prototype.getSettings = function()
{
let r = new CRoleSettings();
r.setName(this.UserMaster.getRole());
r.setColor(this.UserMaster.getColor());
if (this.UserMaster)
{
r.setName(this.UserMaster.getRole());
r.setColor(this.UserMaster.getColor());
}
if (this.FieldGroup)
r.setFieldCount(this.FieldGroup.getAllFields().length)
return r;
};
CRole.prototype.getRole = function()
@ -70,6 +78,10 @@
if (this.FieldGroup)
this.FieldGroup.setWeight(weight);
};
CRole.prototype.getFieldGroup = function()
{
return this.FieldGroup;
};
/**
* Класс для задания настроек роли из интерфейса
@ -77,9 +89,9 @@
*/
function CRoleSettings()
{
this.Name = "";
this.Color = null;
this.Index = 0;
this.Name = "";
this.Color = null;
this.FieldCount = 0;
}
CRoleSettings.prototype.getName = function()
{
@ -115,17 +127,13 @@
{
return (!!this.Color);
};
CRoleSettings.prototype.setIndex = function(index)
CRoleSettings.prototype.setFieldCount = function(count)
{
this.Index = index;
};
CRoleSettings.prototype.getIndex = function()
{
return this.Index;
this.FieldCount = count;
};
CRoleSettings.prototype.getFieldCount = function()
{
return 0;
return this.FieldCount;
};
//--------------------------------------------------------export----------------------------------------------------
AscOForm.CRole = CRole;