Skip to content

Commit

Permalink
add event and document
Browse files Browse the repository at this point in the history
  • Loading branch information
plantain-00 committed Feb 5, 2017
1 parent f36831a commit 9a3cea3
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 16 deletions.
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,60 @@

# tree-component
A reactjs, angular2 and vuejs tree component.

#### install

`npm i tree-component`

#### vuejs component demo

```ts
import "tree-component/dist/vue";
```

```html
<tree :data="data"
@toggle="toggle(arguments[0])"
@change="change(arguments[0])"></tree>
```

the online demo: https://plantain-00.github.io/tree-component/demo/vue/index.html

the source code of the demo: https://github.com/plantain-00/tree-component/tree/master/demo/vue

#### properties and events of the component

name | type | description
--- | --- | ---
data | property | the data of the tree, [the structure](#tree-data-structure)
toggle | event | triggered when opening or closing a node, [the structure of first parameter](#event-data-structure)
change | event | triggered when selecting or deselecting a node, [the structure first parameter](#event-data-structure)

#### tree data structure

```ts
type TreeData = {
text: string;
value?: any;
state?: {
opened?: boolean;
selected?: boolean;
disabled?: boolean;
};
children?: TreeData[];
};
```

#### event data structure

```ts
type EventData = {
data: TreeData;
};
```

#### features

+ vuejs component
+ reactjs component
+ angular2 component
6 changes: 3 additions & 3 deletions demo/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ export const data: TreeData = {
children: [
{
text: "Child node 1",
state: {
selected: true,
},
},
{
text: "Child node 2",
Expand All @@ -26,6 +23,9 @@ export const data: TreeData = {
},
{
text: "Child node 4",
state: {
disabled: true,
},
},
],
},
Expand Down
4 changes: 3 additions & 1 deletion demo/vue/index.ejs.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

<body>
<div id="container">
<tree :data="data"></tree>
<tree :data="data"
@toggle="toggle(arguments[0])"
@change="change(arguments[0])"></tree>
</div>
<script src="../<%=demoVueBundleJs %>" crossOrigin="anonymous" integrity="<%=sri.demoVueBundleJs %>"></script>
</body>
Expand Down
10 changes: 9 additions & 1 deletion demo/vue/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as Vue from "vue";
import "../../dist/vue";
import { data } from "../data";
import * as common from "../../dist/common";

/* tslint:disable:no-unused-new */
/* tslint:disable:object-literal-shorthand */

new Vue({
el: "#container",
Expand All @@ -12,4 +12,12 @@ new Vue({
data,
};
},
methods: {
toggle(eventData: common.EventData) {
eventData.data.state!.opened = !eventData.data.state!.opened;
},
change(eventData: common.EventData) {
eventData.data.state!.selected = !eventData.data.state!.selected;
},
},
});
5 changes: 5 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
export type TreeData = {
text: string;
value?: any;
state?: {
opened?: boolean;
selected?: boolean;
disabled?: boolean;
};
children?: TreeData[];
};

export type EventData = {
data: TreeData;
};
51 changes: 40 additions & 11 deletions src/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ import * as common from "./common";
@Component({
template: `
<li role="treeitem" :class="nodeClassName">
<i class="jstree-icon jstree-ocl" role="presentation" @click="openOrClose()"></i><a :class="anchorClassName" href="javascript:void(0)" @click="click()" @mouseenter="hover(true)" @mouseleave="hover(false)"><i class="jstree-icon jstree-themeicon" role="presentation"></i>{{data.text}}</a>
<i class="jstree-icon jstree-ocl" role="presentation" @click="toggle()"></i><a :class="anchorClassName" href="javascript:void(0)" @click="change()" @mouseenter="hover(true)" @mouseleave="hover(false)"><i class="jstree-icon jstree-themeicon" role="presentation"></i>{{data.text}}</a>
<ul v-if="data.children" role="group" class="jstree-children">
<node v-for="(child, i) in data.children" :data="child" :last="i === data.children.length - 1"></node>
<node v-for="(child, i) in data.children"
:data="child"
:last="i === data.children.length - 1"
@toggle="toggle(arguments[0])"
@change="change(arguments[0])">
</node>
</ul>
</li>
`,
Expand All @@ -16,6 +21,7 @@ import * as common from "./common";
class Node extends Vue {
data: common.TreeData;
last: boolean;

hovered = false;

get nodeClassName() {
Expand All @@ -37,8 +43,13 @@ class Node extends Vue {

get anchorClassName() {
const values = ["jstree-anchor"];
if (this.data.state && this.data.state.selected) {
values.push("jstree-clicked");
if (this.data.state) {
if (this.data.state.selected) {
values.push("jstree-clicked");
}
if (this.data.state.disabled) {
values.push("jstree-disabled");
}
}
if (this.hovered) {
values.push("jstree-hovered");
Expand All @@ -55,13 +66,21 @@ class Node extends Vue {
hover(hovered: boolean) {
this.hovered = hovered;
}
openOrClose() {
if (this.last) {
this.data.state!.opened = !this.data.state!.opened;
toggle(eventData?: common.EventData) {
if (eventData) {
this.$emit("toggle", eventData);
} else {
if (this.last) {
this.$emit("toggle", { data: this.data });
}
}
}
click() {
this.data.state!.selected = !this.data.state!.selected;
change(eventData?: common.EventData) {
if (eventData) {
this.$emit("change", eventData);
} else {
this.$emit("change", { data: this.data });
}
}
}

Expand All @@ -71,14 +90,24 @@ Vue.component("node", Node);
template: `
<div class="jstree jstree-default jstree-default-dark" role="tree">
<ul class="jstree-container-ul jstree-children" role="group">
<node :data="data" :last="true"></node>
<node :data="data"
:last="true"
@toggle="toggle(arguments[0])"
@change="change(arguments[0])"></node>
</ul>
</div>
`,
props: ["data"],
})
class Tree extends Vue {
export class Tree extends Vue {
data: common.TreeData;

toggle(eventData: common.EventData) {
this.$emit("toggle", eventData);
}
change(eventData: common.EventData) {
this.$emit("change", eventData);
}
}

Vue.component("tree", Tree);

0 comments on commit 9a3cea3

Please sign in to comment.