Skip to content

Commit

Permalink
support loading
Browse files Browse the repository at this point in the history
  • Loading branch information
plantain-00 committed Feb 7, 2017
1 parent 4f5902f commit 0b9e7bc
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 120 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type TreeData = {
opened: boolean;
selected: boolean;
disabled: boolean;
loading: boolean;
};
children?: TreeData[];
};
Expand All @@ -111,3 +112,7 @@ type EventData = {
+ vuejs component
+ reactjs component
+ angular2 component
+ open and close
+ select and deselect
+ disabled
+ loading
23 changes: 5 additions & 18 deletions demo/angular/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ enableProdMode();

import { Component } from "@angular/core";

import { data } from "../data";
import { data, clearSelectionOfTree, toggle } from "../common";
import * as common from "../../dist/common";

@Component({
Expand All @@ -25,29 +25,16 @@ export class MainComponent {
data = data;
selectedId: number | null = null;
ontoggle(eventData: common.EventData) {
eventData.data.state.opened = !eventData.data.state.opened;
toggle(eventData);
}
onchange(eventData: common.EventData) {
this.selectedId = eventData.data.state.selected ? null : eventData.data.value.id;
if (!eventData.data.state.selected) {
this.clearSelection();
}
eventData.data.state.selected = !eventData.data.state.selected;
}
clearSelectionOfTree(tree: common.TreeData) {
if (tree.state.selected) {
tree.state.selected = false;
}
if (tree.children) {
for (const child of tree.children) {
this.clearSelectionOfTree(child);
for (const child of this.data) {
clearSelectionOfTree(child);
}
}
}
clearSelection() {
for (const child of this.data) {
this.clearSelectionOfTree(child);
}
eventData.data.state.selected = !eventData.data.state.selected;
}
}

Expand Down
114 changes: 114 additions & 0 deletions demo/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { TreeData, EventData } from "../dist/common";

const rawData: Data[] = [
{
text: "Root node 1",
value: { id: 1 },
state: {
opened: true,
},
children: [
{
text: "Child node 11",
value: { id: 11 },
},
{
text: "Disabled Child node 12",
value: { id: 12 },
state: {
opened: true,
disabled: true,
},
children: [
{
text: "Child node 121",
value: { id: 121 },
},
{
text: "Disabled Child node 122",
value: { id: 122 },
state: {
disabled: true,
},
},
],
},
],
},
{
text: "Loading Root node 2",
value: { id: 2 },
children: [
{
text: "Child node 21",
value: { id: 21 },
},
],
},
];

function standardize(treeData: Data) {
if (treeData.state === undefined) {
treeData.state = {};
}
if (treeData.state.opened === undefined) {
treeData.state.opened = false;
}
if (treeData.state.selected === undefined) {
treeData.state.selected = false;
}
if (treeData.state.disabled === undefined) {
treeData.state.disabled = false;
}
if (treeData.state.loading === undefined) {
treeData.state.loading = false;
}
if (treeData.children) {
for (const child of treeData.children) {
standardize(child);
}
}
}

for (const child of rawData) {
standardize(child);
}

console.log(rawData);

export const data: TreeData[] = rawData as any;

export function clearSelectionOfTree(tree: TreeData) {
if (tree.state.selected) {
tree.state.selected = false;
}
if (tree.children) {
for (const child of tree.children) {
clearSelectionOfTree(child);
}
}
}

export function toggle(eventData: EventData) {
if (eventData.data.value.id === 2 && !eventData.data.state.opened) {
eventData.data.state.loading = true;
setTimeout(() => {
eventData.data.state.loading = false;
eventData.data.state.opened = !eventData.data.state.opened;
}, 2000);
} else {
eventData.data.state.opened = !eventData.data.state.opened;
}
}

type Data = {
text: string;
value?: any;
state?: {
opened?: boolean;
selected?: boolean;
disabled?: boolean;
loading?: boolean;
};
children?: Data[];
};
62 changes: 0 additions & 62 deletions demo/data.ts

This file was deleted.

24 changes: 5 additions & 19 deletions demo/react/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from "react";
import * as ReactDOM from "react-dom";
import { Tree } from "../../dist/react";
import { data } from "../data";
import { data, clearSelectionOfTree, toggle } from "../common";
import * as common from "../../dist/common";

class Main extends React.Component<{}, {}> {
Expand All @@ -21,32 +21,18 @@ class Main extends React.Component<{}, {}> {
}

toggle(eventData: common.EventData) {
eventData.data.state.opened = !eventData.data.state.opened;
toggle(eventData);
this.setState({ data: this.data });
}
change(eventData: common.EventData) {
this.selectedId = eventData.data.state!.selected ? null : eventData.data.value.id;
this.setState({ selectedId: this.selectedId });
if (!eventData.data.state.selected) {
this.clearSelection();
}
eventData.data.state.selected = !eventData.data.state.selected;
this.setState({ data: this.data });
}
clearSelectionOfTree(tree: common.TreeData) {
if (tree.state.selected) {
tree.state.selected = false;
}
if (tree.children) {
for (const child of tree.children) {
this.clearSelectionOfTree(child);
for (const child of this.data) {
clearSelectionOfTree(child);
}
}
}
clearSelection() {
for (const child of this.data) {
this.clearSelectionOfTree(child);
}
eventData.data.state.selected = !eventData.data.state.selected;
this.setState({ data: this.data });
}
}
Expand Down
26 changes: 6 additions & 20 deletions demo/vue/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Vue from "vue";
import "../../dist/vue";
import { data } from "../data";
import { data, clearSelectionOfTree, toggle } from "../common";
import * as common from "../../dist/common";

/* tslint:disable:no-unused-new */
Expand All @@ -15,36 +15,22 @@ new Vue({
},
methods: {
toggle(eventData: common.EventData) {
eventData.data.state.opened = !eventData.data.state.opened;
toggle(eventData);
},
change(this: This, eventData: common.EventData) {
this.selectedId = eventData.data.state.selected ? null : eventData.data.value.id;
if (!eventData.data.state.selected) {
this.clearSelection();
}
eventData.data.state.selected = !eventData.data.state.selected;
},
clearSelectionOfTree(this: This, tree: common.TreeData) {
if (tree.state.selected) {
tree.state.selected = false;
}
if (tree.children) {
for (const child of tree.children) {
this.clearSelectionOfTree(child);
for (const child of this.data) {
clearSelectionOfTree(child);
}
}
eventData.data.state.selected = !eventData.data.state.selected;
},
clearSelection(this: This) {
for (const child of this.data) {
this.clearSelectionOfTree(child);
}
},

},
});

type This = {
data: common.TreeData[];
selectedId: null | number;
clearSelectionOfTree: (tree: common.TreeData) => void;
clearSelection: () => void;
} & Vue;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tree-component",
"version": "1.0.1",
"version": "1.1.0",
"description": "A reactjs, angular2 and vuejs tree component.",
"main": "index.js",
"scripts": {
Expand Down
4 changes: 4 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type TreeData = {
opened: boolean;
selected: boolean;
disabled: boolean;
loading: boolean;
};
children?: TreeData[];
};
Expand Down Expand Up @@ -46,6 +47,9 @@ export function getNodeClassName(data: TreeData, last: boolean) {
} else {
values.push("jstree-closed");
}
if (data.state.loading) {
values.push("jstree-loading");
}
} else {
values.push("jstree-leaf");
}
Expand Down

0 comments on commit 0b9e7bc

Please sign in to comment.