|
| 1 | +import { BuilderComponent } from "@html_builder/core/building_blocks/builder_component"; |
| 2 | +import { |
| 3 | + basicContainerBuilderComponentProps, |
| 4 | + useBuilderComponent, |
| 5 | + useInputBuilderComponent, |
| 6 | +} from "@html_builder/core/utils"; |
| 7 | +import { Component, useRef } from "@odoo/owl"; |
| 8 | +import { _t } from "@web/core/l10n/translation"; |
| 9 | +import { useSortable } from "@web/core/utils/sortable_owl"; |
| 10 | + |
| 11 | +export class BuilderList extends Component { |
| 12 | + static template = "html_builder.BuilderList"; |
| 13 | + static props = { |
| 14 | + ...basicContainerBuilderComponentProps, |
| 15 | + id: { type: String, optional: true }, |
| 16 | + addItemTitle: { type: String, optional: true }, |
| 17 | + itemShape: { |
| 18 | + type: Object, |
| 19 | + values: [{ value: "number" }, { value: "text" }], |
| 20 | + validate: (value) => |
| 21 | + // is not empty object and doesn't include reserved fields |
| 22 | + Object.keys(value).length > 0 && !Object.keys(value).includes("_id"), |
| 23 | + optional: true, |
| 24 | + }, |
| 25 | + default: { optional: true }, |
| 26 | + sortable: { optional: true }, |
| 27 | + hiddenProperties: { type: Array, optional: true }, |
| 28 | + }; |
| 29 | + static defaultProps = { |
| 30 | + addItemTitle: _t("Add"), |
| 31 | + itemShape: { value: "text" }, |
| 32 | + default: { value: _t("Item") }, |
| 33 | + sortable: true, |
| 34 | + hiddenProperties: [], |
| 35 | + }; |
| 36 | + static components = { BuilderComponent }; |
| 37 | + |
| 38 | + setup() { |
| 39 | + this.validateProps(); |
| 40 | + |
| 41 | + useBuilderComponent(); |
| 42 | + const { state, commit, preview } = useInputBuilderComponent({ |
| 43 | + id: this.props.id, |
| 44 | + defaultValue: this.parseDisplayValue([this.makeDefaultItem()]), |
| 45 | + parseDisplayValue: this.parseDisplayValue, |
| 46 | + formatRawValue: this.formatRawValue, |
| 47 | + }); |
| 48 | + this.state = state; |
| 49 | + this.commit = commit; |
| 50 | + this.preview = preview; |
| 51 | + |
| 52 | + if (this.props.sortable) { |
| 53 | + useSortable({ |
| 54 | + enable: () => this.props.sortable, |
| 55 | + ref: useRef("table"), |
| 56 | + elements: ".o_row_draggable", |
| 57 | + handle: ".o_handle_cell", |
| 58 | + cursor: "grabbing", |
| 59 | + placeholderClasses: ["d-table-row"], |
| 60 | + onDrop: (params) => { |
| 61 | + const { element, previous } = params; |
| 62 | + this.reorderItem(element.dataset.id, previous?.dataset.id); |
| 63 | + }, |
| 64 | + }); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + validateProps() { |
| 69 | + // keys match |
| 70 | + const itemShapeKeys = Object.keys(this.props.itemShape); |
| 71 | + const defaultKeys = Object.keys(this.props.default); |
| 72 | + const allKeys = new Set([...itemShapeKeys, ...defaultKeys]); |
| 73 | + if (allKeys.size !== itemShapeKeys.length) { |
| 74 | + throw new Error("default properties don't match itemShape"); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + parseDisplayValue(displayValue) { |
| 79 | + return JSON.stringify(displayValue); |
| 80 | + } |
| 81 | + |
| 82 | + formatRawValue(rawValue) { |
| 83 | + const items = rawValue ? JSON.parse(rawValue) : []; |
| 84 | + for (const item of items) { |
| 85 | + if (!("_id" in item)) { |
| 86 | + item._id = this.getNextAvailableItemId(items); |
| 87 | + } |
| 88 | + } |
| 89 | + return items; |
| 90 | + } |
| 91 | + |
| 92 | + addItem() { |
| 93 | + const items = this.formatRawValue(this.state.value); |
| 94 | + items.push(this.makeDefaultItem()); |
| 95 | + this.commit(items); |
| 96 | + } |
| 97 | + |
| 98 | + deleteItem(e) { |
| 99 | + const itemId = e.target.dataset.id; |
| 100 | + const items = this.formatRawValue(this.state.value); |
| 101 | + this.commit(items.filter((item) => item._id !== itemId)); |
| 102 | + } |
| 103 | + |
| 104 | + reorderItem(itemId, previousId) { |
| 105 | + let items = this.formatRawValue(this.state.value); |
| 106 | + const itemToReorder = items.find((item) => item._id === itemId); |
| 107 | + items = items.filter((item) => item._id !== itemId); |
| 108 | + |
| 109 | + const previousItem = items.find((item) => item._id === previousId); |
| 110 | + const previousItems = items.slice(0, items.indexOf(previousItem) + 1); |
| 111 | + |
| 112 | + const nextItems = items.slice(items.indexOf(previousItem) + 1, items.length); |
| 113 | + |
| 114 | + const newItems = [...previousItems, itemToReorder, ...nextItems]; |
| 115 | + this.commit(newItems); |
| 116 | + } |
| 117 | + |
| 118 | + makeDefaultItem() { |
| 119 | + return { |
| 120 | + ...this.props.default, |
| 121 | + _id: this.getNextAvailableItemId(), |
| 122 | + }; |
| 123 | + } |
| 124 | + |
| 125 | + getNextAvailableItemId(items) { |
| 126 | + items = items || this.formatRawValue(this.state?.value); |
| 127 | + const biggestId = items |
| 128 | + .map((item) => parseInt(item._id)) |
| 129 | + .reduce((acc, id) => (id > acc ? id : acc), -1); |
| 130 | + const nextAvailableId = biggestId + 1; |
| 131 | + return nextAvailableId.toString(); |
| 132 | + } |
| 133 | + |
| 134 | + onInput(e) { |
| 135 | + this.handleValueChange(e.target, false); |
| 136 | + } |
| 137 | + |
| 138 | + onChange(e) { |
| 139 | + this.handleValueChange(e.target, true); |
| 140 | + } |
| 141 | + |
| 142 | + handleValueChange(targetInputEl, commitToHistory) { |
| 143 | + const id = targetInputEl.dataset.id; |
| 144 | + const propertyName = targetInputEl.name; |
| 145 | + const value = targetInputEl.value; |
| 146 | + |
| 147 | + const items = this.formatRawValue(this.state.value); |
| 148 | + const item = items.find((item) => item._id === id); |
| 149 | + item[propertyName] = value; |
| 150 | + |
| 151 | + if (commitToHistory) { |
| 152 | + this.commit(items); |
| 153 | + } else { |
| 154 | + this.preview(items); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments