-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ui] Replace up/down buttons with a drag handle that allows user to s…
…ort aggregations. #5563
- Loading branch information
Showing
11 changed files
with
166 additions
and
28 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
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
91 changes: 91 additions & 0 deletions
91
src/plugins/kibana/public/visualize/editor/draggable_container.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,91 @@ | ||
import _ from 'lodash'; | ||
import $ from 'jquery'; | ||
import dragula from 'dragula'; | ||
import uiModules from 'ui/modules'; | ||
|
||
uiModules | ||
.get('app/visualize') | ||
.directive('draggableContainer', function () { | ||
|
||
return { | ||
restrict: 'A', | ||
scope: true, | ||
controllerAs: 'draggableContainerCtrl', | ||
controller($scope, $attrs, $parse) { | ||
this.getList = () => $parse($attrs.draggableContainer)($scope); | ||
}, | ||
link($scope, $el, attr) { | ||
const drake = dragula({ | ||
containers: $el.toArray(), | ||
moves(el, source, handle) { | ||
const itemScope = $(el).scope(); | ||
if (!('draggableItemCtrl' in itemScope)) { | ||
return; // only [draggable-item] is draggable | ||
} | ||
const $handle = $(handle); | ||
const $anywhereInParentChain = $handle.parents().addBack(); | ||
const scope = $handle.scope(); | ||
const movable = scope.dragHandles.is($anywhereInParentChain); | ||
return movable; | ||
} | ||
}); | ||
|
||
const drakeEvents = [ | ||
'cancel', | ||
'cloned', | ||
'drag', | ||
'dragend', | ||
'drop', | ||
'out', | ||
'over', | ||
'remove', | ||
'shadow' | ||
]; | ||
const prettifiedDrakeEvents = { | ||
drag: 'start', | ||
dragend: 'end' | ||
}; | ||
|
||
drakeEvents.forEach(type => { | ||
drake.on(type, (el, ...args) => forwardEvent(type, el, ...args)); | ||
}); | ||
drake.on('drag', markDragging(true)); | ||
drake.on('dragend', markDragging(false)); | ||
drake.on('drop', drop); | ||
$scope.$on('$destroy', drake.destroy); | ||
|
||
function markDragging(isDragging) { | ||
return el => { | ||
const scope = $(el).scope(); | ||
scope.isDragging = isDragging; | ||
scope.$apply(); | ||
}; | ||
} | ||
|
||
function forwardEvent(type, el, ...args) { | ||
const name = `drag-${prettifiedDrakeEvents[type] || type}`; | ||
const scope = $(el).scope(); | ||
scope.$broadcast(name, el, ...args); | ||
} | ||
|
||
function drop(el, target, source, sibling) { | ||
const list = $scope.draggableContainerCtrl.getList(); | ||
const itemScope = $(el).scope(); | ||
const item = itemScope.draggableItemCtrl.getItem(); | ||
const toIndex = getSiblingItemIndex(list, sibling); | ||
_.move(list, item, toIndex); | ||
} | ||
|
||
function getSiblingItemIndex(list, sibling) { | ||
if (!sibling) { // means the item was dropped at the end of the list | ||
return list.length - 1; | ||
} | ||
const siblingScope = $(sibling).scope(); | ||
const siblingItem = siblingScope.draggableItemCtrl.getItem(); | ||
const siblingIndex = list.indexOf(siblingItem); | ||
return siblingIndex; | ||
} | ||
} | ||
}; | ||
|
||
}); |
15 changes: 15 additions & 0 deletions
15
src/plugins/kibana/public/visualize/editor/draggable_handle.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,15 @@ | ||
import uiModules from 'ui/modules'; | ||
|
||
uiModules | ||
.get('app/visualize') | ||
.directive('draggableHandle', function () { | ||
return { | ||
restrict: 'A', | ||
require: '^draggableItem', | ||
link($scope, $el, attr, ctrl) { | ||
ctrl.registerHandle($el); | ||
$el.addClass('gu-handle'); | ||
} | ||
}; | ||
|
||
}); |
24 changes: 24 additions & 0 deletions
24
src/plugins/kibana/public/visualize/editor/draggable_item.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,24 @@ | ||
import $ from 'jquery'; | ||
import uiModules from 'ui/modules'; | ||
|
||
uiModules | ||
.get('app/visualize') | ||
.directive('draggableItem', function () { | ||
|
||
return { | ||
restrict: 'A', | ||
require: '^draggableContainer', | ||
scope: true, | ||
controllerAs: 'draggableItemCtrl', | ||
controller($scope, $attrs, $parse) { | ||
this.getItem = () => $parse($attrs.draggableItem)($scope); | ||
this.registerHandle = $el => { | ||
$scope.dragHandles.push(...$el); | ||
}; | ||
}, | ||
link($scope, $el, attr, ctrl) { | ||
$scope.dragHandles = $(); | ||
} | ||
}; | ||
|
||
}); |
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,13 @@ | ||
.gu-handle { | ||
cursor: move; | ||
cursor: grab; | ||
cursor: -moz-grab; | ||
cursor: -webkit-grab; | ||
} | ||
|
||
.gu-mirror, | ||
.gu-mirror .gu-handle { | ||
cursor: grabbing; | ||
cursor: -moz-grabbing; | ||
cursor: -webkit-grabbing; | ||
} |
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 |
---|---|---|
|
@@ -600,3 +600,5 @@ fieldset { | |
} | ||
} | ||
} | ||
|
||
@import (reference) "~dragula/dist/dragula.css"; |