checkbox_controller.rs 5.2 KB

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