Skip to content

Commit

Permalink
feat(base tree, draggable plugin): RTL support
Browse files Browse the repository at this point in the history
fix #17
  • Loading branch information
phphe committed Jun 11, 2020
1 parent dc8a52d commit bbdcba4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/components/Tree.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const template = function (h) {
const noUndefined = (str) => str ? str : ''
// tree tpl, to render recursively
const childrenListTpl = (nodes, parent, parentPath) => {
const indentStyle = {paddingLeft: parentPath.length * this.indent + 'px'}
const indentStyle = {[!this.rtl ? 'paddingLeft' : 'paddingRight']: parentPath.length * this.indent + 'px'}
const branchTpl = (node, index) => {
const path = [...parentPath, index]
const transitionComponent = this.foldingTransition || 'transition'
Expand Down Expand Up @@ -52,7 +52,7 @@ const template = function (h) {
{nodes.map(branchTpl)}
</div>
}
return <div class={`he-tree ${this.treeClass}`} data-tree-id={this.treeId}>
return <div class={`he-tree ${this.treeClass} ${noUndefined(this.rtl && 'he-tree--rtl')}`} data-tree-id={this.treeId}>
{this.blockHeader && this.blockHeader()}
{childrenListTpl(this.rootNode.children, this.rootNode, [])}
{this.blockFooter && this.blockFooter()}
Expand All @@ -71,6 +71,7 @@ const Tree = {
],
props: {
indent: {type: Number, default: 20},
rtl: {type: Boolean},
rootNode: {default: is => ({})},
},
// components: {},
Expand Down Expand Up @@ -169,4 +170,7 @@ export default Tree
.he-tree--hidden{
display: none;
}
.he-tree--rtl{
direction: rtl;
}
</style>
4 changes: 3 additions & 1 deletion src/plugins/draggable/Draggable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export default {
unfoldWhenDragoverDelay: this.unfoldWhenDragoverDelay,
draggingNodePositionMode: this.draggingNodePositionMode,
cloneWhenDrag: this.cloneWhenDrag,
rtl: this.rtl,
treeClass: 'he-tree',
rootClass: 'tree-root',
childrenClass: 'tree-children',
Expand Down Expand Up @@ -303,7 +304,8 @@ export default {
'unfoldWhenDragover',
'unfoldWhenDragoverDelay',
'draggingNodePositionMode',
'cloneWhenDrag', ].forEach(name => {
'rtl'
].forEach(name => {
this.$watch(name, (value) => {
_makeTreeDraggable_obj.options[name] = value
_makeTreeDraggable_obj.optionsUpdated()
Expand Down
26 changes: 24 additions & 2 deletions src/plugins/draggable/draggable.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default function makeTreeDraggable(treeEl, options = {}) {
// placeholderId
// unfoldTargetNodeByEl
// getPathByBranchEl
// rtl: false
...options,
treeEl,
}
Expand All @@ -32,6 +33,7 @@ export default function makeTreeDraggable(treeEl, options = {}) {
triggerBySelf: options.triggerBySelf,
draggingClassName: options.draggingClass,
clone: options.cloneWhenDrag,
rtl: options.rtl,
updateMovedElementStyleManually: true,
getMovedOrClonedElement: (directTriggerElement, store) => {
// find closest branch from parents
Expand Down Expand Up @@ -92,12 +94,18 @@ export default function makeTreeDraggable(treeEl, options = {}) {
// find closest branch and hovering tree
let tree
const movingNode = movingEl.querySelector(`.${options.nodeClass}`)
// movingNodeOf and movingNodeRect are not always real. when RTL, there 'x' is top right. when draggingNodePositionMode is mouse, there x and y are mouse position. So don't calc them with their width or height.
// movingNodeOf 和 movingNodeRect并非一直如字面意义是movingNode真实坐标. RTL时, x坐标是右上角. draggingNodePositionMode是mouse时, x和y是鼠标坐标.
let movingNodeOf = hp.getOffset(movingNode)
let movingNodeRect = hp.getBoundingClientRect(movingNode)
if (options.draggingNodePositionMode === 'mouse') {
// use mouse position as dragging node position
const {moveEvent} = store
movingNodeOf = {x: moveEvent.pageX, y: moveEvent.pageY}
movingNodeRect = {x: moveEvent.clientX, y: moveEvent.clientY}
} else if (options.rtl) {
movingNodeOf.x += movingNode.offsetWidth
movingNodeRect.x += movingNode.offsetWidth
}
// find tree with elementsFromPoint
let found
Expand Down Expand Up @@ -217,8 +225,14 @@ export default function makeTreeDraggable(treeEl, options = {}) {
//
while (cur) {
const curNode = cur.querySelector(`.${options.nodeClass}`)
if (hp.getOffset(curNode).x <= movingNodeOf.x) {
break
if (!options.rtl) {
if (hp.getOffset(curNode).x <= movingNodeOf.x) {
break
}
} else {
if (hp.getOffset(curNode).x + curNode.offsetWidth >= movingNodeOf.x) {
break
}
}
let hasNextBranch
let t = cur.nextSibling
Expand Down Expand Up @@ -261,6 +275,13 @@ export default function makeTreeDraggable(treeEl, options = {}) {
}
},
}
// fix for rtl
if (options.rtl) {
Object.assign(conditions, {
'at closest indent right': () => movingNodeOf.x < info.closestNodeOffset.x + info.closestNode.offsetWidth - options.indent, // at indent left
'at closest left': () => movingNodeOf.x > info.closestNodeOffset.x + info.closestNode.offsetWidth, // at right
})
}
// convert conditions result to Boolean
Object.keys(conditions).forEach(key => {
const old = conditions[key]
Expand Down Expand Up @@ -476,6 +497,7 @@ export default function makeTreeDraggable(treeEl, options = {}) {
triggerBySelf: options.triggerBySelf,
draggingClassName: options.draggingClass,
clone: options.cloneWhenDrag,
rtl: options.rtl,
})
}
}
Expand Down

0 comments on commit bbdcba4

Please sign in to comment.