Add custom dragging event support for all HTMLElement
All checks were successful
gitea-sync/antos/pipeline/head This commit looks good

This commit is contained in:
DanyLE 2023-01-07 22:57:00 +01:00
parent be78fab8ae
commit cb744f31c1
3 changed files with 76 additions and 48 deletions

24
d.ts/antos.d.ts vendored
View File

@ -3629,6 +3629,22 @@ interface HTMLElement {
* @memberof HTMLElement * @memberof HTMLElement
*/ */
afxml(o: OS.API.Announcer): void; afxml(o: OS.API.Announcer): void;
/**
* Enable the drag event dispatching on this
* element
*
* This will trigger the `drag` event on the enabled
* on the element when the mouse is down, then move
*
* The event can be listened using the traditional way,
* Example:
* ```
* elem.addEventListener('drag', (e) => { }, false);
* ```
*
* @meberof HTMLElement
*/
enable_drag(): void;
/** /**
* Perform DOM generation ([[afxml]]) then mount ([[sync]]) all the * Perform DOM generation ([[afxml]]) then mount ([[sync]]) all the
* elements. * elements.
@ -8763,14 +8779,6 @@ declare namespace OS {
* @memberof FloatListTag * @memberof FloatListTag
*/ */
push(v: GenericObject<any>): ListViewItemTag; push(v: GenericObject<any>): ListViewItemTag;
/**
* Enable drag and drop on the list
*
* @private
* @param {ListViewItemTag} el the list item DOM element
* @memberof FloatListTag
*/
private enable_drag;
/** /**
* Calibrate the view of the list * Calibrate the view of the list
* *

View File

@ -123,49 +123,26 @@ namespace OS {
*/ */
push(v: GenericObject<any>) { push(v: GenericObject<any>) {
const el = super.push(v); const el = super.push(v);
this.enable_drag(el);
return el;
}
/**
* Enable drag and drop on the list
*
* @private
* @param {ListViewItemTag} el the list item DOM element
* @memberof FloatListTag
*/
private enable_drag(el: ListViewItemTag): void {
$(el) $(el)
.css("user-select", "none") .css("user-select", "none")
.css("cursor", "default") .css("cursor", "default")
.css("display", "block") .css("display", "block")
.css("position", "absolute") .css("position", "absolute");
.on("pointerdown", (evt) => { el.enable_drag();
$(el).on("dragging", (evt) => {
const e = evt.originalEvent as CustomEvent;
const globalof = $(this.refs.mlist).offset(); const globalof = $(this.refs.mlist).offset();
//evt.preventDefault(); const offset = e.detail.offset;
const offset = $(el).offset(); let top = e.detail.current.clientY - offset.top - globalof.top;
offset.top = evt.clientY - offset.top;
offset.left = evt.clientX - offset.left;
const mouse_move = function (
e: JQuery.MouseEventBase
) {
let top = e.clientY - offset.top - globalof.top;
let left = let left =
e.clientX - globalof.left - offset.left; e.detail.current.clientX - globalof.left - offset.left;
left = left < 0 ? 0 : left; left = left < 0 ? 0 : left;
top = top < 0 ? 0 : top; top = top < 0 ? 0 : top;
return $(el) $(el)
.css("top", `${top}px`) .css("top", `${top}px`)
.css("left", `${left}px`); .css("left", `${left}px`);
}; })
return el;
var mouse_up = function (e: JQuery.MouseEventBase) {
$(window).off("pointermove", mouse_move);
return $(window).off("pointerup", mouse_up);
};
$(window).on("pointermove", mouse_move);
return $(window).on("pointerup", mouse_up);
});
} }
/** /**

View File

@ -50,6 +50,23 @@ interface HTMLElement {
*/ */
afxml(o: OS.API.Announcer): void; afxml(o: OS.API.Announcer): void;
/**
* Enable the drag event dispatching on this
* element
*
* This will trigger the `dragging` event on the enabled
* on the element when the mouse is down, then move
*
* The event can be listened using the traditional way,
* Example:
* ```
* elem.addEventListener('dragging', (e) => { }, false);
* ```
*
* @meberof HTMLElement
*/
enable_drag():void;
/** /**
* Perform DOM generation ([[afxml]]) then mount ([[sync]]) all the * Perform DOM generation ([[afxml]]) then mount ([[sync]]) all the
* elements. * elements.
@ -596,6 +613,32 @@ namespace OS {
} }
} }
HTMLElement.prototype.enable_drag = function()
{
$(this)
.on("pointerdown", (evt: JQuery.MouseEventBase) => {
const offset = $(this).offset();
offset.top = evt.clientY - offset.top;
offset.left = evt.clientX - offset.left;
const mouse_move = (
e: JQuery.MouseEventBase
) => {
const custom_event = new CustomEvent('dragging', { detail:{
origin: evt,
current: e,
offset: offset
}});
this.dispatchEvent(custom_event);
};
var mouse_up = function (e: JQuery.MouseEventBase) {
$(window).off("pointermove", mouse_move);
return $(window).off("pointerup", mouse_up);
};
$(window).on("pointermove", mouse_move);
$(window).on("pointerup", mouse_up);
});
}
HTMLElement.prototype.update = function (d): void { HTMLElement.prototype.update = function (d): void {
$(this) $(this)