diff --git a/oform/Document.js b/oform/Document.js index fa46cbc..1258f32 100644 --- a/oform/Document.js +++ b/oform/Document.js @@ -366,6 +366,18 @@ return this.FieldMasters[index]; }; + CDocument.prototype.getMaxWeight = function() + { + let max = -1; + for (let index = 0, count = this.FieldGroups.length; index < count; ++index) + { + let curWeight = this.FieldGroups[index].getWeight(); + if (max > curWeight) + max = curWeight; + } + + return max; + }; CDocument.prototype.onChangeFieldGroups = function() { if (!this.OForm) diff --git a/oform/FieldGroup.js b/oform/FieldGroup.js index cf91c99..7c35346 100644 --- a/oform/FieldGroup.js +++ b/oform/FieldGroup.js @@ -43,6 +43,7 @@ this.Weight = null; this.Fields = []; + this.Users = []; } AscFormat.InitClass(CFieldGroup, AscFormat.CBaseFormatObject, AscDFH.historyitem_type_OForm_FieldGroup); CFieldGroup.prototype.setWeight = function(value) @@ -53,6 +54,10 @@ AscCommon.History.Add(new AscDFH.CChangesOFormFieldGroupWeight(this, this.Weight, value)); this.Weight = value; }; + CFieldGroup.prototype.getWeight = function() + { + return this.Weight; + }; CFieldGroup.prototype.addField = function(field) { if (!field || -1 !== this.Fields.indexOf(field)) @@ -73,6 +78,51 @@ AscCommon.History.Add(new AscDFH.CChangesOFormFieldGroupAddRemoveField(this, field.GetId(), false)); this.Fields.splice(index, 1); }; + CFieldGroup.prototype.addUser = function(user) + { + if (!user || -1 !== this.Users.indexOf(user)) + return; + + AscCommon.History.Add(new AscDFH.CChangesOFormFieldGroupAddRemoveUser(this, user.GetId(), true)); + this.Users.push(user); + }; + CFieldGroup.prototype.removeUser = function(user) + { + if (!user) + return; + + let index = this.Users.indexOf(user); + if (-1 === index) + return; + + AscCommon.History.Add(new AscDFH.CChangesOFormFieldGroupAddRemoveUser(this, user.GetId(), false)); + this.Users.splice(index, 1); + }; + CFieldGroup.prototype.getFirstUser = function() + { + let user = null; + for (let index = 0, userCount = this.Users.length; index < userCount; ++index) + { + let curUser = this.Users[index]; + if (!user || user.compare(curUser) < 0) + user = curUser; + } + + if (!user) + { + for (let fieldIndex = 0, fieldCount = this.Fields.length; fieldIndex < fieldCount; ++fieldIndex) + { + let fieldMaster = this.Fields[fieldIndex]; + for (let userIndex = 0, userCount = fieldMaster.getUserCount(); userIndex < userCount; ++userIndex) + { + let curUser = fieldMaster.getUser(userIndex); + if (!user || user.compare(curUser) < 0) + user = curUser; + } + } + } + return user; + }; CFieldGroup.prototype.readChildXml = function(name, reader) { // TODO: implement diff --git a/oform/FieldMaster.js b/oform/FieldMaster.js index a6f4409..3770155 100644 --- a/oform/FieldMaster.js +++ b/oform/FieldMaster.js @@ -84,6 +84,17 @@ AscCommon.History.Add(new AscDFH.CChangesOFormFieldMasterAddRemoveUser(this, user.GetId(), false)); this.Users.splice(index, 1); }; + CFieldMaster.prototype.getUserCount = function() + { + return this.Users.length; + }; + CFieldMaster.prototype.getUser = function(index) + { + if (index < 0 || index >= this.Users.length) + return null; + + return this.Users[index]; + }; CFieldMaster.prototype.addSigner = function(user) { if (-1 !== this.Signers.indexOf(user)) diff --git a/oform/OForm.js b/oform/OForm.js index 2e8fecc..7ac65d4 100644 --- a/oform/OForm.js +++ b/oform/OForm.js @@ -73,38 +73,43 @@ */ OForm.prototype.addRole = function(roleSettings) { + let name = roleSettings instanceof AscOForm.CRoleSettings ? roleSettings.getName() : roleSettings; + if (this.haveRole(name)) + return false; + if (!this.startAction(AscDFH.historydescription_OForm_AddRole)) return false; let userMaster = new AscOForm.CUserMaster(true); userMaster.setRole(name); + let color = roleSettings.getColor(); + if (color) + userMaster.setColor(color.r, color.g, color.b); + let fieldGroup = new AscOForm.CFieldGroup(); - fieldGroup.setWeight(weight); + fieldGroup.setWeight(this.Format.getMaxWeight() + 1); + fieldGroup.addUser(userMaster); - return new CRole(fieldGroup, userMaster); + this.Format.addFieldGroup(fieldGroup); + this.Format.addUserMaster(userMaster); - let role; - if (roleSettings instanceof AscOForm.CRoleSettings) - role = AscOForm.create(roleSettings); - else - role = new AscOForm.CRole(); - - this.updateRoles(); this.endAction(); return true; }; - OForm.prototype.removeRole = function(name) + OForm.prototype.removeRole = function(name, delegateName) { let roleIndex = this.getRoleIndex(name); if (-1 === roleIndex) return false; + + let delegateIndex = this.getRoleIndex(delegateName); if (!this.startAction(AscDFH.historydescription_OForm_RemoveRole)) return false; - - this.updateRoles(); + + this.endAction(); return true; }; @@ -140,6 +145,19 @@ return role.getSettings(); }; + OForm.prototype.getDefaultRole = function() + { + this.updateRoles(); + + let defaultUser = this.Format.getDefaultUser(); + for (let index = 0, count = this.Roles.length; index < count; ++index) + { + if (defaultUser === this.Roles[index].getUserMaster()) + return this.Roles[index]; + } + + return null; + }; OForm.prototype.onChangeRoles = function() { this.NeedUpdateRoles = true; @@ -148,8 +166,49 @@ { if (!this.NeedUpdateRoles) return; + + this.Roles = []; + for (let fgIndex = 0, fgCount = this.Format.getFieldGroupsCount(); fgIndex < fgCount; ++fgIndex) + { + let fieldGroup = this.Format.getFieldGroup(fgIndex); + let user = fieldGroup.getFirstUser(); + if (!user) + { + // TODO: Разобраться с такими группами + } - // TODO: Обновить состояние ролей в соответствии с форматом + let haveRole = false; + for (let roleIndex = 0, roleCount = this.Roles.length; roleIndex < roleCount; ++roleIndex) + { + if (this.Roles[roleIndex].getUserMaster() === user) + { + haveRole = true; + break; + } + } + + if (haveRole) + { + // TODO: Разобраться с такими ситуациями + } + + let weight = fieldGroup.getWeight(); + let newRoleIndex = this.Roles.length; + for (let roleIndex = 0, roleCount = this.Roles.length; roleIndex < roleCount; ++roleIndex) + { + if (weight < this.Roles[roleIndex].getWeight()) + { + newRoleIndex = roleIndex; + break + } + } + + let newRole = new CRole(fieldGroup, user); + if (newRoleIndex === this.Roles.length) + this.Roles.push(newRole); + else + this.Roles.splice(newRoleIndex, 0, newRole); + } this.NeedUpdateRoles = false; @@ -175,6 +234,8 @@ if (!logicDocument) return; + this.updateRoles(); + logicDocument.UpdateInterface(); logicDocument.FinalizeAction(); }; diff --git a/oform/Role.js b/oform/Role.js index 46206e3..d55be89 100644 --- a/oform/Role.js +++ b/oform/Role.js @@ -46,21 +46,25 @@ this.FieldGroup = fieldGroup; this.UserMaster = userMaster; } - CRole.create = function(name, weight) - { - let userMaster = new AscOForm.CUserMaster(true); - userMaster.setRole(name); - - let fieldGroup = new AscOForm.CFieldGroup(); - fieldGroup.setWeight(weight); - - return new CRole(fieldGroup, userMaster); - }; CRole.prototype.getSettings = function() { - // TODO: Fix me + let r = new CRoleSettings(); + r.setName(this.UserMaster.getRole()); + r.setColor(this.UserMaster.getColor()); return new CRoleSettings(); }; + CRole.prototype.getRole = function() + { + return this.UserMaster.getName(); + }; + CRole.prototype.getUserMaster = function() + { + return this.UserMaster; + }; + CRole.prototype.getWeight = function() + { + return this.FieldGroup.getWeight(); + }; /** * Класс для задания настроек роли из интерфейса @@ -80,11 +84,22 @@ { this.Name = name; }; - CRoleSettings.prototype.setColor = function(r, g, b) + CRoleSettings.prototype.setColor = function(color) { - this.Color = new AscWord.CDocumentColor(r, g, b); + this.Color = color ? color.Copy() : null; }; CRoleSettings.prototype.getColor = function() + { + return this.Color; + }; + CRoleSettings.prototype.setAscColor = function(r, g, b) + { + if (undefined === r || null === r) + this.Color = null; + else + this.Color = new AscWord.CDocumentColor(r, g, b); + } + CRoleSettings.prototype.getAscColor = function() { // TODO: Надо отдавать в интерфейс цвет AscColor return this.Color; @@ -102,9 +117,13 @@ return this.Index; }; //--------------------------------------------------------export---------------------------------------------------- - AscOForm.CRole = CRole; + AscOForm.CRole = CRole; AscOForm.CRoleSettings = CRoleSettings; //---------------------------------------------interface export----------------------------------------------------- - window['AscCommon']["CRoleSettings"] = CRoleSettings; - + CRole.prototype['asc_getSettings'] = CRole.prototype.getSettings; + window['AscCommon']["CRoleSettings"] = CRoleSettings; + CRoleSettings.prototype["asc_getName"] = CRoleSettings.prototype.getName; + CRoleSettings.prototype["asc_putName"] = CRoleSettings.prototype.setName; + CRoleSettings.prototype["asc_getColor"] = CRoleSettings.prototype.getAscColor; + CRoleSettings.prototype["asc_putColor"] = CRoleSettings.prototype.setAscColor; })(window); diff --git a/oform/UserMaster.js b/oform/UserMaster.js index 9120a0f..0a30d5c 100644 --- a/oform/UserMaster.js +++ b/oform/UserMaster.js @@ -99,6 +99,40 @@ this.setRole("Anyone"); this.setColor(128, 128, 128); }; + CUserMaster.prototype.compare = function(user) + { + if (this.Role < user.Role) + return -1; + else if (this.Role > user.Role) + return 1; + + if (this.UserId < user.UserId) + return -1; + else if (this.UserId > user.UserId) + return 1; + + if (!this.Color && !user.Color) + return 0; + else if (!this.Color && user.Color) + return -1; + else if (this.Color && !user.Color) + return 1; + + if (color.r < otherColor.r) + return -1; + else if (color.r > otherColor.r) + return 1; + else if (color.g < otherColor.g) + return -1; + else if (color.g > otherColor.g) + return 1; + else if (color.b < otherColor.b) + return -1; + else if (color.b > otherColor.b) + return 1; + + return 0; + }; CUserMaster.prototype.readChildXml = function(name, reader) { let bRead = false; diff --git a/oform/changes/FieldGroupChanges.js b/oform/changes/FieldGroupChanges.js index b039307..195055a 100644 --- a/oform/changes/FieldGroupChanges.js +++ b/oform/changes/FieldGroupChanges.js @@ -36,6 +36,7 @@ { window['AscDFH'].historyitem_OForm_FieldGroup_Weight = window['AscDFH'].historyitem_type_OForm_FieldGroup | 1; window['AscDFH'].historyitem_OForm_FieldGroup_AddRemoveField = window['AscDFH'].historyitem_type_OForm_FieldGroup | 2; + window['AscDFH'].historyitem_OForm_FieldGroup_AddRemoveUser = window['AscDFH'].historyitem_type_OForm_FieldGroup | 3; /** * @constructor @@ -83,5 +84,32 @@ } ); window['AscDFH'].CChangesOFormFieldGroupAddRemoveField = CChangesOFormFieldGroupAddRemoveField; + + /** + * @constructor + * @extends {window['AscDFH'].CChangesDictionaryBase} + */ + function CChangesOFormFieldGroupAddRemoveUser(Class, userMasterId, isAdd) + { + window['AscDFH'].CChangesDictionaryBase.call(this, Class, userMasterId, isAdd); + } + window['AscDFH'].InheritDictionaryChange( + CChangesOFormFieldGroupAddRemoveField, + window['AscDFH'].historyitem_OForm_FieldGroup_AddRemoveUser, + function() + { + let userMaster = AscCommon.g_oTableId.GetById(this.Key); + if (-1 === this.Class.Users.indexOf(userMaster)) + this.Class.Users.push(userMaster); + }, + function() + { + let userMaster = AscCommon.g_oTableId.GetById(this.Key); + let index = this.Class.Fields.indexOf(userMaster); + if (-1 !== index) + this.Class.Users.splice(index, 1); + } + ); + window['AscDFH'].CChangesOFormFieldGroupAddRemoveUser = CChangesOFormFieldGroupAddRemoveUser; })(window);