-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[IMP] pivot: add icon to sort pivot #5363
Open
hokolomopo
wants to merge
1
commit into
master
Choose a base branch
from
master-pivot-sort-icon-adrm
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
34 changes: 0 additions & 34 deletions
34
src/components/data_validation_overlay/data_validation_overlay.ts
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
src/components/data_validation_overlay/data_validation_overlay.xml
This file was deleted.
Oops, something went wrong.
18 changes: 0 additions & 18 deletions
18
src/components/filters/filter_icons_overlay/filter_icons_overlay.ts
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
src/components/filters/filter_icons_overlay/filter_icons_overlay.xml
This file was deleted.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
src/components/grid_cell_icon_overlay/grid_cell_icon_overlay.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Component } from "@odoo/owl"; | ||
import { isDefined } from "../../helpers"; | ||
import { Store, useStore } from "../../store_engine"; | ||
import { SpreadsheetChildEnv } from "../../types"; | ||
import { DataValidationCheckbox } from "../data_validation_overlay/dv_checkbox/dv_checkbox"; | ||
import { DataValidationListIcon } from "../data_validation_overlay/dv_list_icon/dv_list_icon"; | ||
import { FilterIcon } from "../filters/filter_icon/filter_icon"; | ||
import { GridCellIcon } from "../grid_cell_icon/grid_cell_icon"; | ||
import { GridCellIconStore } from "./grid_cell_icon_overlay_store"; | ||
|
||
export class GridCellIconOverlay extends Component<{}, SpreadsheetChildEnv> { | ||
static template = "o-spreadsheet-GridCellIconOverlay"; | ||
static props = {}; | ||
static components = { GridCellIcon }; | ||
|
||
store!: Store<GridCellIconStore>; | ||
|
||
setup() { | ||
this.store = useStore(GridCellIconStore); | ||
this.store.addIconProvider({ | ||
component: FilterIcon, | ||
hasIcon: (getters, cellPosition) => getters.isFilterHeader(cellPosition), | ||
type: "rightIcon", | ||
}); | ||
this.store.addIconProvider({ | ||
component: DataValidationCheckbox, | ||
hasIcon: (getters, cellPosition) => getters.isCellValidCheckbox(cellPosition), | ||
type: "exclusiveIcon", | ||
}); | ||
this.store.addIconProvider({ | ||
component: DataValidationListIcon, | ||
hasIcon: (getters, cellPosition) => | ||
!getters.isReadonly() && getters.cellHasListDataValidationIcon(cellPosition), | ||
type: "rightIcon", | ||
}); | ||
} | ||
|
||
get icons() { | ||
return Object.values(this.store.icons).filter(isDefined); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/components/grid_cell_icon_overlay/grid_cell_icon_overlay.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<templates> | ||
<t t-name="o-spreadsheet-GridCellIconOverlay"> | ||
<t t-foreach="icons" t-as="icon" t-key="icon_index"> | ||
<GridCellIcon cellPosition="icon.position" horizontalAlign="icon.horizontalAlign"> | ||
<t t-component="icon.component" cellPosition="icon.position"/> | ||
</GridCellIcon> | ||
</t> | ||
</t> | ||
</templates> |
62 changes: 62 additions & 0 deletions
62
src/components/grid_cell_icon_overlay/grid_cell_icon_overlay_store.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { ComponentConstructor } from "@odoo/owl"; | ||
import { CellPosition, Getters, SpreadsheetChildEnv } from "../.."; | ||
import { positionToXc } from "../../helpers"; | ||
import { SpreadsheetStore } from "../../stores"; | ||
|
||
export interface GridCellIconProvider { | ||
component: ComponentConstructor<{ cellPosition: CellPosition }, SpreadsheetChildEnv>; | ||
hasIcon: (getters: Getters, cellPosition: CellPosition) => boolean; | ||
type: "exclusiveIcon" | "rightIcon"; | ||
} | ||
|
||
interface GridCellIcon extends Omit<GridCellIconProvider, "hasIcon"> { | ||
position: CellPosition; | ||
horizontalAlign: "right" | undefined; | ||
} | ||
|
||
export class GridCellIconStore extends SpreadsheetStore { | ||
mutators = ["addIconProvider", "removeIconProvider"] as const; | ||
|
||
private iconProviders: GridCellIconProvider[] = []; | ||
|
||
private iconCache: Record<string, GridCellIcon | undefined> | undefined = undefined; | ||
|
||
handle() { | ||
this.iconCache = undefined; | ||
} | ||
|
||
addIconProvider(iconProvider: GridCellIconProvider) { | ||
this.iconProviders.push(iconProvider); | ||
this.iconCache = undefined; | ||
} | ||
|
||
removeIconProvider(iconProvider: GridCellIconProvider) { | ||
this.iconProviders = this.iconProviders.filter((provider) => provider !== iconProvider); | ||
this.iconCache = undefined; | ||
} | ||
|
||
getIcon(position: CellPosition): GridCellIcon | undefined { | ||
for (const iconMatcher of this.iconProviders) { | ||
if (iconMatcher.hasIcon(this.getters, position)) { | ||
return { | ||
...iconMatcher, | ||
position, | ||
horizontalAlign: iconMatcher.type === "rightIcon" ? "right" : undefined, | ||
}; | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
get icons(): Record<string, GridCellIcon | undefined> { | ||
if (!this.iconCache) { | ||
this.iconCache = {}; | ||
for (const position of this.getters.getVisibleCellPositions()) { | ||
const xc = positionToXc(position); | ||
this.iconCache[xc] = this.getIcon(position); | ||
} | ||
} | ||
|
||
return this.iconCache; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { CellPosition, Command, HeaderIndex, UID } from "../.."; | ||
import { GRID_ICON_MARGIN, ICON_EDGE_LENGTH, PADDING_AUTORESIZE_HORIZONTAL } from "../../constants"; | ||
import { | ||
computeIconWidth, | ||
computeTextWidth, | ||
largeMax, | ||
positionToXc, | ||
positions, | ||
splitTextToWidth, | ||
} from "../../helpers"; | ||
import { SpreadsheetStore } from "../../stores"; | ||
import { GridCellIconStore } from "../grid_cell_icon_overlay/grid_cell_icon_overlay_store"; | ||
|
||
export class AutoresizeStore extends SpreadsheetStore { | ||
private ctx = document.createElement("canvas").getContext("2d")!; | ||
|
||
protected gridCellIconStore = this.get(GridCellIconStore); | ||
|
||
handle(cmd: Command) { | ||
switch (cmd.type) { | ||
case "AUTORESIZE_COLUMNS": | ||
for (let col of cmd.cols) { | ||
const size = this.getColMaxWidth(cmd.sheetId, col); | ||
if (size !== 0) { | ||
this.model.dispatch("RESIZE_COLUMNS_ROWS", { | ||
elements: [col], | ||
dimension: "COL", | ||
size, | ||
sheetId: cmd.sheetId, | ||
}); | ||
} | ||
} | ||
break; | ||
case "AUTORESIZE_ROWS": | ||
for (let row of cmd.rows) { | ||
this.model.dispatch("RESIZE_COLUMNS_ROWS", { | ||
elements: [row], | ||
dimension: "ROW", | ||
size: null, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't we compute the size according to the content, as done for the columns ? |
||
sheetId: cmd.sheetId, | ||
}); | ||
} | ||
break; | ||
} | ||
} | ||
|
||
getCellWidth(position: CellPosition): number { | ||
const style = this.getters.getCellComputedStyle(position); | ||
|
||
let contentWidth = 0; | ||
|
||
const content = this.getters.getEvaluatedCell(position).formattedValue; | ||
if (content) { | ||
const multiLineText = splitTextToWidth(this.ctx, content, style, undefined); | ||
contentWidth += Math.max( | ||
...multiLineText.map((line) => computeTextWidth(this.ctx, line, style)) | ||
); | ||
} | ||
|
||
const icon = this.getters.getCellIconSrc(position); | ||
if (icon) { | ||
contentWidth += computeIconWidth(style); | ||
} | ||
|
||
const xc = positionToXc(position); | ||
const cellIcon = this.gridCellIconStore.icons[xc]; | ||
if (cellIcon) { | ||
contentWidth += ICON_EDGE_LENGTH + GRID_ICON_MARGIN; | ||
} | ||
|
||
if (contentWidth === 0) { | ||
return 0; | ||
} | ||
|
||
contentWidth += 2 * PADDING_AUTORESIZE_HORIZONTAL; | ||
if (style.wrapping === "wrap") { | ||
const colWidth = this.getters.getColSize(this.getters.getActiveSheetId(), position.col); | ||
return Math.min(colWidth, contentWidth); | ||
} | ||
|
||
return contentWidth; | ||
} | ||
|
||
private getColMaxWidth(sheetId: UID, index: HeaderIndex): number { | ||
const cellsPositions = positions(this.getters.getColsZone(sheetId, index, index)); | ||
const sizes = cellsPositions.map((position) => this.getCellWidth({ sheetId, ...position })); | ||
return Math.max(0, largeMax(sizes)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having the overlay having to register its own providers is not the best, ideally it shouldn't have to know about them ... But we need to register them somewhere (in a component) 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can have a register of icon providers somewhere and then the overlay can add all of the entries of this register in the store ?