How can I use JointJS with an application built with Angular CLI?
Asked Answered
V

4

5

I have installed jointjs via npm and also installed typings and code compiles/builds fine.

Code

import { Component } from '@angular/core';
import  * as joint from '../../node_modules/jointjs/dist/joint.min';


export class AppComponent {
  title = 'app works!';
  constructor(
  ) {
    // console.log(joint)// private jint: joint,
  }

  ngOnInit() {
    var graph = new joint.dia.Graph;
  }
}

Error shows up on the browser:

Failed to compile.

/home/vinay/angularapps/jjs/src/app/app.component.ts (17,31): Property 'Graph' does not exist on type '{}'.)

my cli.json file.. added the scripts and styles for jointjs

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "version": "1.0.0-beta.32.3",
    "name": "jjs"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "app",
      "styles": [
        "styles.css",
        "../node_modules/jointjs/css/layout.css"
      ],
      "scripts": ["../node_modules/jointjs/dist/joint.js"],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "files": "src/**/*.ts",
      "project": "src/tsconfig.json"
    },
    {
      "files": "e2e/**/*.ts",
      "project": "e2e/tsconfig.json"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "component": {}
  }
}

my tsconfig.json file where i added allowJs to true

{
  "compilerOptions": {
    "baseUrl": "",
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "es2016",
      "dom"
    ],
    "mapRoot": "./",
    "module": "es2015",
    "moduleResolution": "node",
    "outDir": "../dist/out-tsc",
    "sourceMap": true,
    "allowJs": true,
    "target": "es5",
    "typeRoots": [
      "../node_modules/@types"
    ]
  }
}

I am not able to figure out how to create a simple hello world application provided on this link http://resources.jointjs.com/tutorial/hello-world

Valeda answered 19/2, 2017 at 14:17 Comment(3)
format your post and ad the list of errors you get.Dispassion
Can you share your full angular-cli.json?Horary
shared the full angular cliValeda
V
20

ok i finally got to get jointjs working with @angular cli and below are the steps

in the command prompt within the angular project directory

  1. ng new your-app
  2. cd into your-app
  3. npm install jquery --save npm install @types/jquery --save-dev
  4. npm install backbone --save npm install @types/backbone --save-dev
  5. npm install jointjs --save npm install @types/jointjs --save-dev
  6. npm install [email protected] --save npm install @types/[email protected] --save-dev

Make sure in package.json the entries show up under dependencies and devDepencies for backbone, jquery, jointjs, lodash and make sure they are of the versions

jointjs 1.0.3, backbome = 1.3.3, jquery = 3.1.1, lodash = 3.10.1

in angular-cli.json file

in the styles property and scripts;[ ] property add the joint js details like below

  "styles": [
    "styles.css",
    "../node_modules/jointjs/css/layout.css"
  ],
  "scripts": [
    "../node_modules/jquery/dist/jquery.min.js",
    "../node_modules/jointjs/dist/joint.js"
  ],

in your component.html

 <div id="paper"></div>

in your component.ts

import * as jQuery from 'jquery';
import * as _ from 'lodash';
import * as $ from 'backbone';
const joint = require('../../../../node_modules/jointjs/dist/joint.js');

ngOnInit() {
    let graph = new joint.dia.Graph;

    let paper = new joint.dia.Paper({
      el: jQuery("#paper"),
      width: 600,
      height: 200,
      model: graph,
      gridSize: 1
    });

    let rect = new joint.shapes.basic.Rect({
      position: { x: 100, y: 30 },
      size: { width: 100, height: 30 },
      attrs: { rect: { fill: 'blue' }, text: { text: 'my box', fill: 'white' } }
    });

    let rect2 = rect.clone() as joint.shapes.basic.Rect;
    rect2.translate(300);

    var link = new joint.dia.Link({
      source: { id: rect.id },
      target: { id: rect2.id }
    });

    graph.addCells([rect, rect2, link]);
}
Valeda answered 2/3, 2017 at 6:56 Comment(3)
please note if lodash is not the version that jointjs recommends it will throw errors so i had to install lodash with the version number. the others like backbone, jquery etc installed with the right version without having to mention themValeda
In the official documentation the rect.translate is there, but when i try to ng serve it says, property translate doesnt exist on type 'Cell'. How do I fix this error?Licentiate
See JointJS Official docs for the latest recommended versions of dependencies for the current jointjs.Oxytocin
J
5

I'm using @angular/cli : 1.0.4 version and im going to sum everything you need to run JointJS without a problem.

Use these commands to install npm packages correctly and make sure the right versions are present. Types on devdependencies and the libraries on normal dependencies thats important.

npm install --save [email protected] [email protected] backbone jointjs
npm install --save-dev @types/[email protected] @types/jquery @types/backbone @types/jointjs

This is my scripts in the angular-cli.json file:

"scripts": [
    "../node_modules/jquery/dist/jquery.js",
    "../node_modules/lodash/index.js",
    "../node_modules/backbone/backbone.js",
    "../node_modules/jointjs/dist/joint.js"
  ]

This is my styles in the angular-cli.json file to work properly:

"styles": [
    "styles.scss",
    "../node_modules/jointjs/css/layout.css",
    "../node_modules/jointjs/css/themes/material.css",
    "../node_modules/jointjs/css/themes/default.css"
  ],

And the examples are same as above. Hope i could help. Happy Coding!

Jut answered 25/5, 2017 at 8:0 Comment(1)
"scripts" part here is the correct one! Guys, don't forget about backbone/lodash inclusion as it leads to 'Uncaught TypeError: Cannot set property '$' of undefined' issue.Horoscope
C
4

Just to mention there are a few things wrong or missing in the accepted answer:

  1. includes in component.ts should be:

    declare var $:JQueryStatic;
    import * as _ from 'lodash';
    import * as backbone from 'backbone';
    import * as joint from 'jointjs';
    
  2. you should add the version on the npm install commands for jquery and backbone to ensure to use the suggestioned version as mentioned.

  3. you also (additionaly) must add following 2 css files into angular-cli.json:

    "../node_modules/jointjs/css/themes/material.css",
    "../node_modules/jointjs/css/themes/default.css"
    
  4. use $ in the code rather than jQuery:

    let paper = new joint.dia.Paper({
       el: $('#paper'),
       ...
     });
    

hth some one,

PS: i tested it with angular 4.1 and angular-cli 1.0.3

Coltson answered 12/5, 2017 at 13:24 Comment(1)
It works. The only issue is that application throwing exception for Backbone: scripts.bundle.js:40 Uncaught TypeError: Cannot set property '$' of undefined Related piece of code: var Backbone = root.Backbone; var _ = root._; var $ = Backbone.$ = root.jQuery || root.$; // <-- this is reported as an issueHoroscope
W
1
  1. npm install jointjs --save
  2. at angular.cli.json insert at scripts : ./node_modules/jointjs/dist/jointjs.js
  3. at angular.cli.json insert at styles : ./node_modules/jointjs/css/layout.css
Wizardly answered 19/2, 2017 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.