checkbox_controller.rs 5.7 KB

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