Skip to content

Commit

Permalink
Merge branch 'dev' into update-js-packages
Browse files Browse the repository at this point in the history
  • Loading branch information
mbraak committed Nov 14, 2023
2 parents 99ff271 + ee1a97f commit 3264845
Show file tree
Hide file tree
Showing 23 changed files with 1,158 additions and 408 deletions.
63 changes: 44 additions & 19 deletions src/dataLoader.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,46 @@
import { Node } from "./node";
import { JqTreeWidget } from "./tree.jquery";
import { DataFilter, OnLoadFailed, OnLoading } from "./jqtreeOptions";
import { LoadData, TriggerEvent } from "./jqtreeMethodTypes";

export type HandleFinishedLoading = () => void;

export default class DataLoader {
private treeWidget: JqTreeWidget;
interface DataLoaderParams {
dataFilter?: DataFilter;
loadData: LoadData;
onLoadFailed?: OnLoadFailed;
onLoading?: OnLoading;
$treeElement: JQuery<HTMLElement>;
triggerEvent: TriggerEvent;
}

constructor(treeWidget: JqTreeWidget) {
this.treeWidget = treeWidget;
export default class DataLoader {
private dataFilter?: DataFilter;
private loadData: LoadData;
private onLoadFailed?: OnLoadFailed;
private onLoading?: OnLoading;
private $treeElement: JQuery<HTMLElement>;
private triggerEvent: TriggerEvent;

constructor({
dataFilter,
loadData,
onLoadFailed,
onLoading,
$treeElement,
triggerEvent,
}: DataLoaderParams) {
this.dataFilter = dataFilter;
this.loadData = loadData;
this.onLoadFailed = onLoadFailed;
this.onLoading = onLoading;
this.$treeElement = $treeElement;
this.triggerEvent = triggerEvent;
}

public loadFromUrl(
urlInfo: string | JQuery.AjaxSettings | null,
parentNode: Node | null,
onFinished: HandleFinishedLoading | null
onFinished: HandleFinishedLoading | null,
): void {
if (!urlInfo) {
return;
Expand All @@ -30,7 +57,7 @@ export default class DataLoader {

const handleSuccess = (data: string | NodeData[]): void => {
stopLoading();
this.treeWidget.loadData(this.parseData(data), parentNode);
this.loadData(this.parseData(data), parentNode);

if (onFinished && typeof onFinished === "function") {
onFinished();
Expand All @@ -40,8 +67,8 @@ export default class DataLoader {
const handleError = (jqXHR: JQuery.jqXHR): void => {
stopLoading();

if (this.treeWidget.options.onLoadFailed) {
this.treeWidget.options.onLoadFailed(jqXHR);
if (this.onLoadFailed) {
this.onLoadFailed(jqXHR);
}
};

Expand All @@ -64,20 +91,20 @@ export default class DataLoader {
if (parentNode) {
return jQuery(parentNode.element);
} else {
return this.treeWidget.element;
return this.$treeElement;
}
}

private notifyLoading(
isLoading: boolean,
node: Node | null,
$el: JQuery
$el: JQuery,
): void {
if (this.treeWidget.options.onLoading) {
this.treeWidget.options.onLoading(isLoading, node, $el);
if (this.onLoading) {
this.onLoading(isLoading, node, $el);
}

this.treeWidget._triggerEvent("tree.loading_data", {
this.triggerEvent("tree.loading_data", {
isLoading,
node,
$el,
Expand All @@ -87,7 +114,7 @@ export default class DataLoader {
private submitRequest(
urlInfoInput: string | JQuery.AjaxSettings,
handleSuccess: JQuery.Ajax.SuccessCallback<any>,
handleError: JQuery.Ajax.ErrorCallback<any>
handleError: JQuery.Ajax.ErrorCallback<any>,
): void {
const urlInfo =
typeof urlInfoInput === "string"
Expand All @@ -109,8 +136,6 @@ export default class DataLoader {
}

private parseData(data: string | NodeData[]): NodeData[] {
const { dataFilter } = this.treeWidget.options;

const getParsedData = () => {
if (typeof data === "string") {
return JSON.parse(data) as NodeData[];
Expand All @@ -121,8 +146,8 @@ export default class DataLoader {

const parsedData = getParsedData();

if (dataFilter) {
return dataFilter(parsedData);
if (this.dataFilter) {
return this.dataFilter(parsedData);
} else {
return parsedData;
}
Expand Down
2 changes: 1 addition & 1 deletion src/dragAndDropHandler/hitAreasGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HitArea } from "../types";
import { HitArea } from "./types";
import { Node, Position } from "../node";
import { getOffsetTop } from "../util";
import VisibleNodeIterator from "./visibleNodeIterator";
Expand Down
Loading

0 comments on commit 3264845

Please sign in to comment.