项目作者: zewa666

项目描述 :
Aurelia wrapper for jsTree
高级语言: TypeScript
项目地址: git://github.com/zewa666/aurelia-jstree.git
创建时间: 2017-10-22T11:21:18Z
项目社区:https://github.com/zewa666/aurelia-jstree

开源协议:MIT License

下载


aurelia-jstree

An Aurelia wrapper component for jsTree.

Install

Make sure to npm install jQuery and jsTree alongside this wrapper.

  1. npm install jquery jstree aurelia-jstree --save

Aurelia CLI Support

If your Aurelia CLI build is based on RequireJS or SystemJS you can setup the plugin using the following dependency declaration:

  1. ...
  2. "dependencies": [
  3. {
  4. "name":"jquery",
  5. "path":"../node_modules/jquery/dist",
  6. "main":"jquery.min",
  7. "export": "$"
  8. },
  9. {
  10. "name":"jstree",
  11. "path":"../node_modules/jstree/dist",
  12. "main":"jstree.min"
  13. },
  14. {
  15. "name": "aurelia-jstree",
  16. "path": "../node_modules/aurelia-jstree/dist/amd",
  17. "main": "index"
  18. }
  19. ]

Configuration

In your main.ts you’ll have to load jstree and register the plugin:

  1. import {Aurelia} from 'aurelia-framework'
  2. import environment from './environment';
  3. import "jstree"; // <------------ MAKE SURE TO IMPORT JSTREE
  4. import 'jstree/themes/default/style.min.css'; // <----- IMPORT STYLES OR INCLUDE BY ANY OTHER MEANS (SCSS, direct include ...)
  5. export function configure(aurelia: Aurelia) {
  6. aurelia.use
  7. .standardConfiguration()
  8. .feature('resources');
  9. ...
  10. aurelia.use.plugin("aurelia-jstree"); // <----- REGISTER THE PLUGIN
  11. aurelia.start().then(() => aurelia.setRoot());
  12. }

Usage

Once the plugin is installed and configured you can use the au-js-tree custom component.
An example for a simple filebrowser is provided below:

  1. <au-js-tree settings.bind="jstreeConfig"
  2. data.bind="data"
  3. selection-changed.bind="onSelectionChanged"
  4. node-moved.bind="onNodeMoved"></au-js-tree>

The settings.core should not contain the data property. It should be provided
separately via the data binding to ensure proper re-renders on prop changes.

  1. export class App {
  2. public jstreeConfig = {
  3. plugins: ["dnd"],
  4. core: {
  5. check_callback: function (operation, node, node_parent, node_position, more) {
  6. // operation can be 'create_node', 'rename_node', 'delete_node', 'move_node' or 'copy_node'
  7. console.log(operation);
  8. if (operation === "move_node") {
  9. console.group("D&D");
  10. console.log("node", node);
  11. console.log("parent", node_parent);
  12. console.log("position", node_position);
  13. console.log("more", more);
  14. console.log(node_parent.original.isFolder);
  15. console.groupEnd();
  16. return node_parent.original.isFolder === true; //only allow dropping inside folders
  17. }
  18. return true; //allow all other operations
  19. }
  20. },
  21. dnd: {
  22. check_while_dragging: true
  23. }
  24. }
  25. public data = [
  26. {
  27. text: "Root folder",
  28. state: { opened: true },
  29. isFolder: true,
  30. children: [
  31. {
  32. text: "File 1",
  33. state: { selected: true },
  34. icon: "jstree-file"
  35. },
  36. {
  37. text: "File 2",
  38. icon: "jstree-file"
  39. },
  40. {
  41. text: "Subfolder",
  42. state: { opened: false },
  43. icon: "jstree-folder",
  44. children: [],
  45. isFolder: true
  46. }
  47. ]
  48. }
  49. ];
  50. onSelectionChanged = (e: JQueryEventObject, data: any) => {
  51. console.group("Selection was changed");
  52. console.log(this);
  53. console.log(e);
  54. console.log(data);
  55. console.groupEnd();
  56. }
  57. onNodeMoved = (e: JQueryEventObject, data: JsTreeNodeMovedData) => {
  58. console.group("Node was moved");
  59. console.log(e);
  60. console.log(data);
  61. console.groupEnd();
  62. }
  63. }

When binding a function to either selection-changed or node-moved instead of a lambda expression, please note that this inside your function is going to refer to the element vs your expected class instance. To avoid that bind with a proper context

  1. <au-js-tree settings.bind="jstreeConfig"
  2. data.bind="data"
  3. selection-changed.bind="onSelectionChanged.bind($this)"
  4. node-moved.bind="onNodeMoved.bind($this)"></au-js-tree>

Acknowledgement

Thanks goes to Dwayne Charrington for his Aurelia-TypeScript starter package https://github.com/Vheissu/aurelia-typescript-plugin