-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Migration to new view definition
- Loading branch information
Showing
12 changed files
with
378 additions
and
324 deletions.
There are no files selected for viewing
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
110 changes: 59 additions & 51 deletions
110
owl_tutorial_views/static/src/components/tree_item/TreeItem.js
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 |
---|---|---|
@@ -1,64 +1,72 @@ | ||
/** @odoo-module **/ | ||
const { Component } = owl; | ||
const { useState } = owl.hooks; | ||
import {Component, useState} from "@odoo/owl"; | ||
|
||
export class TreeItem extends Component { | ||
/** | ||
* @override | ||
*/ | ||
constructor(...args) { | ||
super(...args); | ||
this.state = useState({ | ||
isDraggedOn: false, | ||
}); | ||
} | ||
|
||
toggleChildren() { | ||
if (this.props.item.child_id.length > 0) { | ||
this.trigger("tree_item_clicked", { data: this.props.item }); | ||
/** | ||
* @override | ||
*/ | ||
constructor(...args) { | ||
super(...args); | ||
this.state = useState({ | ||
isDraggedOn: false, | ||
}); | ||
} | ||
} | ||
|
||
onDragstart(event) { | ||
event.dataTransfer.setData("TreeItem", JSON.stringify(this.props.item)); | ||
} | ||
toggleChildren() { | ||
if (this.props.item.child_id.length > 0) { | ||
this.props.onTreeItemClicked(this.props.item); | ||
} | ||
} | ||
|
||
onDragover() {} | ||
onDragstart(event) { | ||
event.dataTransfer.setData("TreeItem", JSON.stringify(this.props.item)); | ||
} | ||
|
||
onDragenter() { | ||
Object.assign(this.state, { isDraggedOn: true }); | ||
} | ||
onDragover() {} | ||
|
||
onDragleave() { | ||
Object.assign(this.state, { isDraggedOn: false }); | ||
} | ||
onDragenter() { | ||
Object.assign(this.state, {isDraggedOn: true}); | ||
} | ||
|
||
onDrop(event) { | ||
Object.assign(this.state, { isDraggedOn: false }); | ||
let droppedItem = JSON.parse(event.dataTransfer.getData("TreeItem")); | ||
if ( | ||
droppedItem.id == this.props.item.id || | ||
droppedItem.parent_id[0] == this.props.item.id | ||
) { | ||
console.log("Drop inside itself or same parent has no effect"); | ||
return; | ||
onDragleave() { | ||
Object.assign(this.state, {isDraggedOn: false}); | ||
} | ||
if (this.props.item.parent_path.startsWith(droppedItem.parent_path)) { | ||
console.log("Oops, drop inside child item is forbidden."); | ||
return; | ||
|
||
onDrop(event) { | ||
Object.assign(this.state, {isDraggedOn: false}); | ||
let droppedItem = JSON.parse(event.dataTransfer.getData("TreeItem")); | ||
if (droppedItem.id == this.props.item.id || droppedItem.parent_id[0] == this.props.item.id) { | ||
console.log("Drop inside itself or same parent has no effect"); | ||
return; | ||
} | ||
if (this.props.item.parent_path.startsWith(droppedItem.parent_path)) { | ||
console.log("Oops, drop inside child item is forbidden."); | ||
return; | ||
} | ||
this.props.onChangeItemTree({ | ||
itemMoved: droppedItem, | ||
newParent: this.props.item, | ||
}); | ||
} | ||
this.trigger("change_item_tree", { | ||
itemMoved: droppedItem, | ||
newParent: this.props.item, | ||
}); | ||
} | ||
} | ||
|
||
Object.assign(TreeItem, { | ||
components: { TreeItem }, | ||
props: { | ||
item: {}, | ||
countField: "", | ||
}, | ||
template: "owl_tutorial_views.TreeItem", | ||
}); | ||
TreeItem.components = {TreeItem}; | ||
TreeItem.props = { | ||
countField: { | ||
type: String, | ||
optional: true, | ||
}, | ||
onTreeItemClicked: { | ||
type: Function, | ||
optional: true, | ||
}, | ||
onChangeItemTree: { | ||
type: Function, | ||
optional: true, | ||
}, | ||
item: { | ||
type: Object, | ||
optional: true, | ||
}, | ||
}; | ||
TreeItem.template = "owl_tutorial_views.TreeItem"; |
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
23 changes: 23 additions & 0 deletions
23
owl_tutorial_views/static/src/owl_tree_view/owl_tree_arch_parser.js
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,23 @@ | ||
/** @odoo-module */ | ||
|
||
/** | ||
* | ||
* | ||
* @typedef {Object} ArchInfo | ||
* @property {string} arch | ||
* @property {string} countField | ||
*/ | ||
|
||
import {XMLParser} from "@web/core/utils/xml"; | ||
|
||
export class OWLTreeArchParser extends XMLParser { | ||
parse(arch) { | ||
const xmlDoc = this.parseXML(arch); | ||
const countField = xmlDoc.getAttribute("count_field"); | ||
/** @type { ArchInfo} */ | ||
return { | ||
arch, | ||
countField, | ||
}; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
owl_tutorial_views/static/src/owl_tree_view/owl_tree_controller.js
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,65 @@ | ||
/** @odoo-module **/ | ||
/** @typedef {import("./owl_tree_model").OWLTreeModel} OWLTreeModel */ | ||
import {Component, useState, onWillStart, onWillUpdateProps} from "@odoo/owl"; | ||
import {Layout} from "@web/search/layout"; | ||
import {useService} from "@web/core/utils/hooks"; | ||
|
||
export class OWLTreeController extends Component { | ||
/** | ||
* Standard setup function of OWL Component, here we parse the XML | ||
* template to get the options and instantiate the Model. | ||
**/ | ||
setup() { | ||
this.orm = useService("orm"); | ||
this.rpc = useService("rpc"); | ||
/** @type OWLTreeModel */ | ||
this.model = useState( | ||
new this.props.Model( | ||
this.orm, | ||
this.props.resModel, | ||
this.props.fields, | ||
this.props.archInfo, | ||
this.props.domain, | ||
this.props.context, | ||
this.rpc | ||
) | ||
); | ||
const {arch, templateDocs} = this.props.archInfo; | ||
|
||
onWillStart(async () => { | ||
await this.model.load(); | ||
}); | ||
|
||
onWillUpdateProps(async (nextProps) => { | ||
if (JSON.stringify(nextProps.domain) !== JSON.stringify(this.props.domain)) { | ||
this.model.domain = nextProps.domain; | ||
await this.model.load(); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
*/ | ||
async _onTreeItemClicked(treeItem) { | ||
if (treeItem.children === undefined) { | ||
await this.model.expandChildrenOf(treeItem.id, treeItem.parent_path); | ||
} else { | ||
this.model.toggleChildrenVisibleForItem(treeItem); | ||
} | ||
} | ||
|
||
/** | ||
*/ | ||
async _onChangeItemTree({itemMoved, newParent}) { | ||
await this.model.changeParent(itemMoved.id, newParent.id); | ||
|
||
// Refresh old parent | ||
let oldParent = await this.model.refreshNode(itemMoved.parent_id[0]); | ||
await this.model.expandChildrenOf(oldParent.id, oldParent.parent_path); | ||
|
||
// Refresh new parent | ||
await this.model.expandChildrenOf(newParent.id, newParent.parent_path); | ||
} | ||
} | ||
OWLTreeController.components = {Layout}; | ||
OWLTreeController.template = "owl_tutorial_views.View"; |
13 changes: 13 additions & 0 deletions
13
owl_tutorial_views/static/src/owl_tree_view/owl_tree_controller.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,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<templates xml:space="preserve"> | ||
<t t-name="owl_tutorial_views.View" owl="1"> | ||
<Layout display="props.display" className="'h-100 overflow-auto'"> | ||
<t | ||
t-component="props.Renderer" | ||
countField="props.archInfo.countField" | ||
onTreeItemClicked="_onTreeItemClicked.bind(this)" onChangeItemTree="_onChangeItemTree.bind(this)" | ||
items="model.data" | ||
/> | ||
</Layout> | ||
</t> | ||
</templates> |
Oops, something went wrong.