2022-07-10 17:06:36 +08:00
|
|
|
use crate::revision::{FieldRevision, FieldTypeRevision};
|
2022-06-19 21:10:07 +08:00
|
|
|
use indexmap::IndexMap;
|
2022-06-21 16:56:50 +08:00
|
|
|
use nanoid::nanoid;
|
2022-06-15 17:24:46 +08:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
use serde_repr::*;
|
2022-07-10 17:06:36 +08:00
|
|
|
use std::collections::HashMap;
|
2022-06-30 23:00:03 +08:00
|
|
|
use std::sync::Arc;
|
2022-06-21 16:56:50 +08:00
|
|
|
|
|
|
|
|
pub fn gen_grid_filter_id() -> String {
|
|
|
|
|
nanoid!(6)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn gen_grid_group_id() -> String {
|
|
|
|
|
nanoid!(6)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn gen_grid_sort_id() -> String {
|
|
|
|
|
nanoid!(6)
|
|
|
|
|
}
|
2022-06-15 17:24:46 +08:00
|
|
|
|
2022-07-10 17:06:36 +08:00
|
|
|
/// Each layout contains multiple key/value.
|
|
|
|
|
/// Key: field_id
|
|
|
|
|
/// Value: this value also contains key/value.
|
|
|
|
|
/// Key: FieldType,
|
|
|
|
|
/// Value: the corresponding filter.
|
|
|
|
|
///
|
|
|
|
|
/// This overall struct is described below:
|
|
|
|
|
/// GridSettingRevision
|
|
|
|
|
/// layout:
|
|
|
|
|
/// field_id:
|
|
|
|
|
/// FieldType: GridFilterRevision
|
|
|
|
|
/// FieldType: GridFilterRevision
|
|
|
|
|
/// field_id:
|
|
|
|
|
/// FieldType: GridFilterRevision
|
|
|
|
|
/// FieldType: GridFilterRevision
|
|
|
|
|
/// layout:
|
|
|
|
|
/// field_id:
|
|
|
|
|
/// FieldType: GridFilterRevision
|
|
|
|
|
/// FieldType: GridFilterRevision
|
|
|
|
|
///
|
|
|
|
|
/// Group and sorts will be the same structure as filters.
|
2022-06-20 10:24:43 +08:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Default, Eq, PartialEq)]
|
2022-06-20 09:37:52 +08:00
|
|
|
pub struct GridSettingRevision {
|
2022-06-24 18:13:40 +08:00
|
|
|
pub layout: GridLayoutRevision,
|
2022-07-10 17:06:36 +08:00
|
|
|
|
2022-06-19 21:10:07 +08:00
|
|
|
#[serde(with = "indexmap::serde_seq")]
|
2022-07-10 17:06:36 +08:00
|
|
|
filters: IndexMap<GridLayoutRevision, IndexMap<String, GridFilterRevisionMap>>,
|
2022-06-15 17:24:46 +08:00
|
|
|
|
2022-06-21 16:56:50 +08:00
|
|
|
#[serde(skip, with = "indexmap::serde_seq")]
|
2022-06-29 16:55:52 +08:00
|
|
|
pub groups: IndexMap<GridLayoutRevision, Vec<GridGroupRevision>>,
|
2022-06-19 21:10:07 +08:00
|
|
|
|
2022-06-21 16:56:50 +08:00
|
|
|
#[serde(skip, with = "indexmap::serde_seq")]
|
2022-06-29 16:55:52 +08:00
|
|
|
pub sorts: IndexMap<GridLayoutRevision, Vec<GridSortRevision>>,
|
2022-06-15 17:24:46 +08:00
|
|
|
}
|
|
|
|
|
|
2022-07-10 17:06:36 +08:00
|
|
|
pub type FiltersByFieldId = HashMap<String, Vec<Arc<GridFilterRevision>>>;
|
|
|
|
|
pub type GroupsByFieldId = HashMap<String, Vec<Arc<GridGroupRevision>>>;
|
|
|
|
|
pub type SortsByFieldId = HashMap<String, Vec<Arc<GridSortRevision>>>;
|
2022-06-30 23:00:03 +08:00
|
|
|
impl GridSettingRevision {
|
2022-07-10 17:06:36 +08:00
|
|
|
pub fn get_all_group(&self) -> Option<GroupsByFieldId> {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_all_sort(&self) -> Option<SortsByFieldId> {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Return the Filters of the current layout
|
|
|
|
|
pub fn get_all_filter(&self, field_revs: &[Arc<FieldRevision>]) -> Option<FiltersByFieldId> {
|
|
|
|
|
let layout = &self.layout;
|
|
|
|
|
// Acquire the read lock of the filters.
|
|
|
|
|
let filter_rev_map_by_field_id = self.filters.get(layout)?;
|
|
|
|
|
// Get the filters according to the FieldType, so we need iterate the field_revs.
|
|
|
|
|
let filters_by_field_id = field_revs
|
|
|
|
|
.iter()
|
|
|
|
|
.flat_map(|field_rev| {
|
|
|
|
|
let field_type = &field_rev.field_type_rev;
|
|
|
|
|
let field_id = &field_rev.id;
|
|
|
|
|
|
|
|
|
|
let filter_rev_map: &GridFilterRevisionMap = filter_rev_map_by_field_id.get(field_id)?;
|
|
|
|
|
let filters: Vec<Arc<GridFilterRevision>> = filter_rev_map.get(field_type)?.clone();
|
|
|
|
|
Some((field_rev.id.clone(), filters))
|
|
|
|
|
})
|
|
|
|
|
.collect::<FiltersByFieldId>();
|
|
|
|
|
Some(filters_by_field_id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_filter_rev_map(&self, layout: &GridLayoutRevision, field_id: &str) -> Option<&GridFilterRevisionMap> {
|
|
|
|
|
let filter_rev_map_by_field_id = self.filters.get(layout)?;
|
|
|
|
|
filter_rev_map_by_field_id.get(field_id)
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-30 23:00:03 +08:00
|
|
|
pub fn get_mut_filters(
|
|
|
|
|
&mut self,
|
|
|
|
|
layout: &GridLayoutRevision,
|
|
|
|
|
field_id: &str,
|
2022-07-01 20:32:11 +08:00
|
|
|
field_type: &FieldTypeRevision,
|
2022-06-30 23:00:03 +08:00
|
|
|
) -> Option<&mut Vec<Arc<GridFilterRevision>>> {
|
|
|
|
|
self.filters
|
|
|
|
|
.get_mut(layout)
|
|
|
|
|
.and_then(|filter_rev_map_by_field_id| filter_rev_map_by_field_id.get_mut(field_id))
|
|
|
|
|
.and_then(|filter_rev_map| filter_rev_map.get_mut(field_type))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_filters(
|
|
|
|
|
&self,
|
|
|
|
|
layout: &GridLayoutRevision,
|
|
|
|
|
field_id: &str,
|
2022-07-09 23:28:15 +08:00
|
|
|
field_type_rev: &FieldTypeRevision,
|
2022-06-30 23:00:03 +08:00
|
|
|
) -> Option<Vec<Arc<GridFilterRevision>>> {
|
|
|
|
|
self.filters
|
|
|
|
|
.get(layout)
|
|
|
|
|
.and_then(|filter_rev_map_by_field_id| filter_rev_map_by_field_id.get(field_id))
|
2022-07-09 23:28:15 +08:00
|
|
|
.and_then(|filter_rev_map| filter_rev_map.get(field_type_rev))
|
2022-06-30 23:00:03 +08:00
|
|
|
.cloned()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn insert_filter(
|
|
|
|
|
&mut self,
|
|
|
|
|
layout: &GridLayoutRevision,
|
|
|
|
|
field_id: &str,
|
2022-07-01 20:32:11 +08:00
|
|
|
field_type: &FieldTypeRevision,
|
2022-06-30 23:00:03 +08:00
|
|
|
filter_rev: GridFilterRevision,
|
|
|
|
|
) {
|
|
|
|
|
let filter_rev_map_by_field_id = self.filters.entry(layout.clone()).or_insert_with(IndexMap::new);
|
|
|
|
|
let filter_rev_map = filter_rev_map_by_field_id
|
|
|
|
|
.entry(field_id.to_string())
|
|
|
|
|
.or_insert_with(GridFilterRevisionMap::new);
|
|
|
|
|
|
|
|
|
|
filter_rev_map
|
2022-07-01 20:32:11 +08:00
|
|
|
.entry(field_type.to_owned())
|
2022-06-30 23:00:03 +08:00
|
|
|
.or_insert_with(Vec::new)
|
|
|
|
|
.push(Arc::new(filter_rev))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Default, Eq, PartialEq)]
|
|
|
|
|
#[serde(transparent)]
|
|
|
|
|
pub struct GridFilterRevisionMap {
|
|
|
|
|
#[serde(with = "indexmap::serde_seq")]
|
2022-07-01 20:32:11 +08:00
|
|
|
pub filter_by_field_type: IndexMap<FieldTypeRevision, Vec<Arc<GridFilterRevision>>>,
|
2022-06-30 23:00:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl GridFilterRevisionMap {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
GridFilterRevisionMap::default()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::ops::Deref for GridFilterRevisionMap {
|
2022-07-01 20:32:11 +08:00
|
|
|
type Target = IndexMap<FieldTypeRevision, Vec<Arc<GridFilterRevision>>>;
|
2022-06-30 23:00:03 +08:00
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
|
&self.filter_by_field_type
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::ops::DerefMut for GridFilterRevisionMap {
|
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
|
&mut self.filter_by_field_type
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-19 21:10:07 +08:00
|
|
|
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize_repr, Deserialize_repr)]
|
2022-06-15 17:24:46 +08:00
|
|
|
#[repr(u8)]
|
2022-06-19 21:10:07 +08:00
|
|
|
pub enum GridLayoutRevision {
|
2022-06-15 17:24:46 +08:00
|
|
|
Table = 0,
|
|
|
|
|
Board = 1,
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-19 21:10:07 +08:00
|
|
|
impl ToString for GridLayoutRevision {
|
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
|
let layout_rev = self.clone() as u8;
|
|
|
|
|
layout_rev.to_string()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::default::Default for GridLayoutRevision {
|
2022-06-15 17:24:46 +08:00
|
|
|
fn default() -> Self {
|
2022-06-19 21:10:07 +08:00
|
|
|
GridLayoutRevision::Table
|
2022-06-15 17:24:46 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-30 23:00:03 +08:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq, Hash)]
|
2022-06-15 17:24:46 +08:00
|
|
|
pub struct GridFilterRevision {
|
2022-06-21 16:56:50 +08:00
|
|
|
pub id: String,
|
|
|
|
|
pub field_id: String,
|
2022-06-22 17:11:56 +08:00
|
|
|
pub condition: u8,
|
|
|
|
|
pub content: Option<String>,
|
2022-06-15 17:24:46 +08:00
|
|
|
}
|
|
|
|
|
|
2022-06-20 10:24:43 +08:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
|
2022-06-15 17:24:46 +08:00
|
|
|
pub struct GridGroupRevision {
|
2022-06-21 16:56:50 +08:00
|
|
|
pub id: String,
|
|
|
|
|
pub field_id: Option<String>,
|
|
|
|
|
pub sub_field_id: Option<String>,
|
2022-06-15 17:24:46 +08:00
|
|
|
}
|
|
|
|
|
|
2022-06-20 10:24:43 +08:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
|
2022-06-15 17:24:46 +08:00
|
|
|
pub struct GridSortRevision {
|
2022-06-21 16:56:50 +08:00
|
|
|
pub id: String,
|
2022-06-20 09:37:52 +08:00
|
|
|
pub field_id: Option<String>,
|
2022-06-15 17:24:46 +08:00
|
|
|
}
|