checkbox_controller.rs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. use crate::entities::{GroupRowsNotificationPB, InsertedRowPB, RowPB};
  2. use crate::services::field::{CheckboxCellData, CheckboxCellDataParser, CheckboxTypeOptionPB, CHECK, UNCHECK};
  3. use crate::services::group::action::GroupControllerCustomActions;
  4. use crate::services::group::configuration::GroupContext;
  5. use crate::services::group::controller::{
  6. GenericGroupController, GroupController, GroupGenerator, MoveGroupRowContext,
  7. };
  8. use crate::services::cell::insert_checkbox_cell;
  9. use crate::services::group::{move_group_row, GeneratedGroupConfig, GeneratedGroupContext};
  10. use grid_rev_model::{CellRevision, CheckboxGroupConfigurationRevision, FieldRevision, GroupRevision, RowRevision};
  11. pub type CheckboxGroupController = GenericGroupController<
  12. CheckboxGroupConfigurationRevision,
  13. CheckboxTypeOptionPB,
  14. CheckboxGroupGenerator,
  15. CheckboxCellDataParser,
  16. >;
  17. pub type CheckboxGroupContext = GroupContext<CheckboxGroupConfigurationRevision>;
  18. impl GroupControllerCustomActions for CheckboxGroupController {
  19. type CellDataType = CheckboxCellData;
  20. fn default_cell_rev(&self) -> Option<CellRevision> {
  21. Some(CellRevision::new(UNCHECK.to_string()))
  22. }
  23. fn can_group(&self, content: &str, cell_data: &Self::CellDataType) -> bool {
  24. if cell_data.is_check() {
  25. content == CHECK
  26. } else {
  27. content == UNCHECK
  28. }
  29. }
  30. fn add_or_remove_row_in_groups_if_match(
  31. &mut self,
  32. row_rev: &RowRevision,
  33. cell_data: &Self::CellDataType,
  34. ) -> Vec<GroupRowsNotificationPB> {
  35. let mut changesets = vec![];
  36. self.group_ctx.iter_mut_status_groups(|group| {
  37. let mut changeset = GroupRowsNotificationPB::new(group.id.clone());
  38. let is_not_contained = !group.contains_row(&row_rev.id);
  39. if group.id == CHECK {
  40. if cell_data.is_uncheck() {
  41. // Remove the row if the group.id is CHECK but the cell_data is UNCHECK
  42. changeset.deleted_rows.push(row_rev.id.clone());
  43. group.remove_row(&row_rev.id);
  44. } else {
  45. // Add the row to the group if the group didn't contain the row
  46. if is_not_contained {
  47. let row_pb = RowPB::from(row_rev);
  48. changeset.inserted_rows.push(InsertedRowPB::new(row_pb.clone()));
  49. group.add_row(row_pb);
  50. }
  51. }
  52. }
  53. if group.id == UNCHECK {
  54. if cell_data.is_check() {
  55. // Remove the row if the group.id is UNCHECK but the cell_data is CHECK
  56. changeset.deleted_rows.push(row_rev.id.clone());
  57. group.remove_row(&row_rev.id);
  58. } else {
  59. // Add the row to the group if the group didn't contain the row
  60. if is_not_contained {
  61. let row_pb = RowPB::from(row_rev);
  62. changeset.inserted_rows.push(InsertedRowPB::new(row_pb.clone()));
  63. group.add_row(row_pb);
  64. }
  65. }
  66. }
  67. if !changeset.is_empty() {
  68. changesets.push(changeset);
  69. }
  70. });
  71. changesets
  72. }
  73. fn delete_row(&mut self, row_rev: &RowRevision, _cell_data: &Self::CellDataType) -> Vec<GroupRowsNotificationPB> {
  74. let mut changesets = vec![];
  75. self.group_ctx.iter_mut_groups(|group| {
  76. let mut changeset = GroupRowsNotificationPB::new(group.id.clone());
  77. if group.contains_row(&row_rev.id) {
  78. changeset.deleted_rows.push(row_rev.id.clone());
  79. group.remove_row(&row_rev.id);
  80. }
  81. if !changeset.is_empty() {
  82. changesets.push(changeset);
  83. }
  84. });
  85. changesets
  86. }
  87. fn move_row(
  88. &mut self,
  89. _cell_data: &Self::CellDataType,
  90. mut context: MoveGroupRowContext,
  91. ) -> Vec<GroupRowsNotificationPB> {
  92. let mut group_changeset = vec![];
  93. self.group_ctx.iter_mut_groups(|group| {
  94. if let Some(changeset) = move_group_row(group, &mut context) {
  95. group_changeset.push(changeset);
  96. }
  97. });
  98. group_changeset
  99. }
  100. }
  101. impl GroupController for CheckboxGroupController {
  102. fn will_create_row(&mut self, row_rev: &mut RowRevision, field_rev: &FieldRevision, group_id: &str) {
  103. match self.group_ctx.get_group(group_id) {
  104. None => tracing::warn!("Can not find the group: {}", group_id),
  105. Some((_, group)) => {
  106. let is_check = group.id == CHECK;
  107. let cell_rev = insert_checkbox_cell(is_check, field_rev);
  108. row_rev.cells.insert(field_rev.id.clone(), cell_rev);
  109. }
  110. }
  111. }
  112. fn did_create_row(&mut self, row_pb: &RowPB, group_id: &str) {
  113. if let Some(group) = self.group_ctx.get_mut_group(group_id) {
  114. group.add_row(row_pb.clone())
  115. }
  116. }
  117. }
  118. pub struct CheckboxGroupGenerator();
  119. impl GroupGenerator for CheckboxGroupGenerator {
  120. type Context = CheckboxGroupContext;
  121. type TypeOptionType = CheckboxTypeOptionPB;
  122. fn generate_groups(
  123. _field_rev: &FieldRevision,
  124. _group_ctx: &Self::Context,
  125. _type_option: &Option<Self::TypeOptionType>,
  126. ) -> GeneratedGroupContext {
  127. let check_group = GeneratedGroupConfig {
  128. group_rev: GroupRevision::new(CHECK.to_string(), "".to_string()),
  129. filter_content: CHECK.to_string(),
  130. };
  131. let uncheck_group = GeneratedGroupConfig {
  132. group_rev: GroupRevision::new(UNCHECK.to_string(), "".to_string()),
  133. filter_content: UNCHECK.to_string(),
  134. };
  135. GeneratedGroupContext {
  136. no_status_group: None,
  137. group_configs: vec![check_group, uncheck_group],
  138. }
  139. }
  140. }