joint icon indicating copy to clipboard operation
joint copied to clipboard

angular2 and jointjs

Open jmls opened this issue 9 years ago • 30 comments

Is there any special magic to using jointjs with angular2, webpack and angular-cli ?

jmls avatar Jan 23 '17 15:01 jmls

I have npm installed jointjs and @types/jointjs - but I am struggling with the syntax for importing the library

import * as joint from 'jointjs'

gives the message

File '/home/ubuntu/workspace/node_modules/@types/jointjs/index.d.ts' is not a module.)

if I look at the file index.d.ts, there is a namespace defined, but not exported


declare namespace joint {
    export var g: any;
    export var V: any;

    namespace dia {

whereas backbone has the following

import * as Backbone from 'backbone';

and the backbone index.d.ts has


export = Backbone;
export as namespace Backbone;

import * as _ from 'underscore';

declare namespace Backbone {

and there are no errors for the backbone import

jmls avatar Jan 23 '17 16:01 jmls

so, if I add

export = joint;
export as namespace joint;

to the start of index.d.ts , the app compiles without error.

I guess this is a problem with the @type library then ?

jmls avatar Jan 23 '17 16:01 jmls

Hello there, no you just have to render all the divs necessary for joint and you are good to go.

Regarding the joint you have to use const joint = require('pathtojointjs'); or update the types as you suggested.

CoconutTheSlayer avatar Jan 23 '17 16:01 CoconutTheSlayer

mm, ok. I am getting some nasty errors while trying to use jointjs in a component. The code is :

import { Component, OnInit, Input } from '@angular/core';

import * as $ from 'jquery';

const joint = require('jointjs/dist/joint.js');

@Component({
  selector: 'my-component',
  template: `<div id="_wrapper_"><div id="_content_" ></div></div>`
})

export class MyComponent implements OnInit {
  graph:any;
  paper: any;

  constructor() {}

  ngOnInit() {
    this.graph = new joint.dia.Graph();
    
    this.paper = new joint.dia.Paper({
      el : $('#_content_'),
      width : '100%',
      height : '99%',
      model : this.graph,
      gridSize : 1
    });
  
    this.graph.addCell(new joint.shapes.uml.Class({
      size : {
        width : 200,
        height : 100
      },
      name : "foo"
    }));
  }
}

When trying to show this diagrm, I get these errors:

TypeError: this.findBySelector is not a function
    at joint.js:6872
    at lodash.js:4944
    at baseForOwn (lodash.js:3001)
    at lodash.js:4913
    at Function.forEach (lodash.js:9359)
    at child.update (joint.js:6867)
    at child.wrapper [as update] (lodash.js:4968)
    at child.render (joint.js:7192)
    at child.protoProps.render (joint.js:4260)
    at child.protoProps.render (joint.js:4260)
    at child.renderView (joint.js:9888)
    at child.onCellAdded (joint.js:9866)
    at triggerEvents (backbone.js:371)
    at triggerApi (backbone.js:356)
    at eventsApi (backbone.js:155)

Would just like to know [where I am going || what I am doing] wrong

jmls avatar Jan 23 '17 19:01 jmls

Frustrated... ok, so two weeks later I come back to the same problem.

In a nutshell, has anyone been able to get jointjs loaded into an angular-cli project ?

jmls avatar Feb 08 '17 16:02 jmls

unbelievable ... the second I post this, the bloody thing starts to work ;)

ensure all correct versions of libs are npm installed (lodash has to be v3)

import * as $ from 'jquery';
import * as _ from 'lodash';
import * as Backbone from 'backbone';
const joint = require('jointjs/dist/joint.js');
[snip]
 var graph = new joint.dia.Graph;

    var paper = new joint.dia.Paper({
        el: $('#canvas'),
        width: 600,
        height: 200,
        model: graph,
        gridSize: 1
    });

    var 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' } }
    });

    var rect2 = rect.clone();
    rect2.translate(300);

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

    graph.addCells([rect, rect2, link]);
  }

works for me ;)

jmls avatar Feb 08 '17 16:02 jmls

great to hear that:-)

CoconutTheSlayer avatar Feb 08 '17 17:02 CoconutTheSlayer

bugger spoke too soon. :(

Ok, so the basics work fine, as described above. However, there are some odd problems .

For example the available-magnet class seems to affect the connector as in

.available-magnet {
    fill: blue;
}

makes the connector blue.

Also, when defining a defaultLink, with this code:

        this.paper = new joint.dia.Paper({
            el: $(this.element),
            height: "100%",
            width: "100%",
            model: this.graph,
            gridSize: 1,
            snapLinks: { radius: 75 },
            markAvailable: true,
            
            defaultLink: () => {
                let link = new joint.dia.Link({
                    attrs: { '.marker-target': { d: 'M 10 0 L 0 5 L 10 10 z' } },
                });

                return link;
            },
           defaultConnector: { name: 'rounded', args: { radius: 20 }},
           defaultRouter: {name: "manhattan"}
        });

gives me this result when drawing a link ...

image

what's going on ?

jmls avatar Feb 10 '17 16:02 jmls

ok, so, part of what's going on is that I didn't include the jointjs.css ;) now my chart lines look right.

jmls avatar Feb 10 '17 18:02 jmls

i have a wrong. Uncaught TypeError: Cannot read property 'assign' of undefined at eval (eval at ./node_modules/jointjs/dist/joint.js (polyfills.04c45c0….js:548), <anonymous>:2995:17) at Object.eval (eval at ./node_modules/jointjs/dist/joint.js (polyfills.04c45c0….js:548), <anonymous>:18:20) at eval (eval at ./node_modules/jointjs/dist/joint.js (polyfills.04c45c0….js:548), <anonymous>:19:11) at eval (eval at ./node_modules/jointjs/dist/joint.js (polyfills.04c45c0….js:548), <anonymous>:45:2) at Object../node_modules/jointjs/dist/joint.js (polyfills.04c45c0….js:548)

i import like this: import * as _ from 'lodash'; import * as Backbone from 'backbone'; const joint = require('jointjs/dist/joint.js');

TaylorPzreal avatar Feb 28 '17 06:02 TaylorPzreal

I'm trying to integrate jointjs in an Angular 2 component, I saw your code but I have problem with it:

`import { Component } from '@angular/core';

import * as joint from 'jointjs'; import * as $ from 'jquery';

@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] })

export class AppComponent { graph:any; paper: any;

constructor() {}

ngOnInit() { this.createGraph(); //console.log(this.graph); }

private createGraph(){

this.graph = new joint.dia.Graph();

this.paper = new joint.dia.Paper({ el: $('#body'), width: 500, height: 500, model: this.graph });

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

let rect2 = new joint.shapes.basic.Rect({
  position: { x: 250, y: -100 },
  size: { width: 70, height: 30 },
  attrs: { text: { text: 'my rectangle2' } }
});

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

this.graph.addCells([rect, rect2, link]);
//this.graph.addCell(rect);

} } `

No problem with the jointjs import but it doesn't work.This is the error:

EXCEPTION: Error in :0:0 caused by: this.addCell is not a function vendor.bundle.js:81229:9 ORIGINAL EXCEPTION: this.addCell is not a function vendor.bundle.js:81231:13 ORIGINAL STACKTRACE: vendor.bundle.js:81234:13 joint.dia.Graph<.addCells/<@http://localhost:4200/vendor.bundle.js:109741:22040 arrayEach@http://localhost:4200/vendor.bundle.js:53633:11 forEach@http://localhost:4200/vendor.bundle.js:62455:14 joint.dia.Graph<.addCells@http://localhost:4200/vendor.bundle.js:109741:22006 joint.util.wrappers.cells/<@http://localhost:4200/vendor.bundle.js:109741:16545 [500]/AppComponent.prototype.createGraph@http://localhost:4200/main.bundle.js:84:9 [500]/AppComponent.prototype.ngOnInit@http://localhost:4200/main.bundle.js:64:9 anonymous/Wrapper_AppComponent.prototype.ngDoCheck@/AppModule/AppComponent/wrapper.ngfactory.js:20:40 anonymous/View_AppComponent_Host0.prototype.detectChangesInternal@/AppModule/AppComponent/host.ngfactory.js:26:3 AppView.prototype.detectChanges@http://localhost:4200/vendor.bundle.js:100746:9 DebugAppView.prototype.detectChanges@http://localhost:4200/vendor.bundle.js:100941:13 ViewRef_.prototype.detectChanges@http://localhost:4200/vendor.bundle.js:82539:9 ApplicationRef_.prototype.tick/<@http://localhost:4200/vendor.bundle.js:36983:58 ApplicationRef_.prototype.tick@http://localhost:4200/vendor.bundle.js:36983:13 ApplicationRef_.prototype._loadComponent@http://localhost:4200/vendor.bundle.js:36958:9 ApplicationRef_.prototype.bootstrap@http://localhost:4200/vendor.bundle.js:36946:9 PlatformRef_.prototype._moduleDoBootstrap/<@http://localhost:4200/vendor.bundle.js:36758:82 PlatformRef_.prototype._moduleDoBootstrap@http://localhost:4200/vendor.bundle.js:36758:13 PlatformRef_.prototype._bootstrapModuleFactoryWithZone/</</<@http://localhost:4200/vendor.bundle.js:36710:21 ZoneDelegate.prototype.invoke@http://localhost:4200/polyfills.bundle.js:6262:17 NgZone.prototype.forkInnerZoneWithAngularBehavior/this.inner<.onInvoke@http://localhost:4200/vendor.bundle.js:26951:28 ZoneDelegate.prototype.invoke@http://localhost:4200/polyfills.bundle.js:6261:17 Zone.prototype.run@http://localhost:4200/polyfills.bundle.js:6058:24 scheduleResolveOrReject/<@http://localhost:4200/polyfills.bundle.js:6611:52 ZoneDelegate.prototype.invokeTask@http://localhost:4200/polyfills.bundle.js:6295:17 NgZone.prototype.forkInnerZoneWithAngularBehavior/this.inner<.onInvokeTask@http://localhost:4200/vendor.bundle.js:26942:28 ZoneDelegate.prototype.invokeTask@http://localhost:4200/polyfills.bundle.js:6294:17 Zone.prototype.runTask@http://localhost:4200/polyfills.bundle.js:6098:28 drainMicroTaskQueue@http://localhost:4200/polyfills.bundle.js:6461:25

Can you help me?

uroti avatar Mar 01 '17 16:03 uroti

If I change from this.graph.addCells([rect,rect2,link]) to this.graph.addCell(rect) I have your old error:

TypeError: this.findBySelector is not a function at joint.js:6872 at lodash.js:4944 at baseForOwn (lodash.js:3001) at lodash.js:4913 at Function.forEach (lodash.js:9359) at child.update (joint.js:6867) at child.wrapper [as update] (lodash.js:4968) at child.render (joint.js:7192) at child.protoProps.render (joint.js:4260) at child.protoProps.render (joint.js:4260) at child.renderView (joint.js:9888) at child.onCellAdded (joint.js:9866) at triggerEvents (backbone.js:371) at triggerApi (backbone.js:356) at eventsApi (backbone.js:155)

uroti avatar Mar 01 '17 16:03 uroti

Hi @uroti checkout this repo, it's working, if you have any questions don't hesitate to ask me.

https://github.com/CoconutTheSlayer/jointangular2

CoconutTheSlayer avatar Mar 03 '17 10:03 CoconutTheSlayer

Hi @CoconutTheSlayer, thank you for the answer. I don't see difference with my code. How have you install and set jointjs? This could be the only one difference.

Regards

uroti avatar Mar 03 '17 11:03 uroti

Well, my angular class implements OnInit to be able to call ngOnInit. The most important change is in the package.json, i'm referencing the github repo of the jointjs to get the latest version with updated definition files:-) anyway, is the code I provided working for you?

CoconutTheSlayer avatar Mar 03 '17 11:03 CoconutTheSlayer

I downloaded your project, installed jointjs(I done only npm install jointjs and not npm @types/joints) and now it works!...but I don't understand which is the problem in my project.

uroti avatar Mar 03 '17 11:03 uroti

great:-)

CoconutTheSlayer avatar Mar 03 '17 11:03 CoconutTheSlayer

Referring to the problem above:-

EXCEPTION: Error in :0:0 caused by: this.addCell is not a function vendor.bundle.js:81229:9 ORIGINAL EXCEPTION: this.addCell is not a function vendor.bundle.js:81231:13 ORIGINAL STACKTRACE: vendor.bundle.js:81234:13

I created a brand new Angular2 app using the v1.0.0-rc0 cli. Copied the app.component.ts source code from https://github.com/CoconutTheSlayer/jointangular2 and the above behaviour was immediately present. Digging deeper I found that the lodash dependency that was automatically installed was v4.17.4.

To clarify, JointJS specifically asks for v3.10.1 in the dependency list. For lodash v4, 'this' in the call to _.each in the stack trace above refers to the global window object rather than the parent scope.

Googling issues with 'npm wrong version' seems to show a history of issues where npm ignores devDependencies when making version hoisting decisions and a variety of inconsistencies.

If in setting up your new project you've accidentally stored your JointJS dependency into dev dependencies (as I did) you may hit a similar issue.

I ran an npm cache clean and deleted my node_modules folder and noted that on a second npm install the correct v3 version of lodash was installed. Everything started working from there.

Other tidbits that might help

npm version: 4.1.2 dependencies added to package.json

    "jointjs": "git+https://github.com/clientIO/joint.git",
    "jquery": "3.1.1",

Hope this saves someone an hour :)

matt-psaltis avatar Mar 11 '17 12:03 matt-psaltis

@SaltyDH yes it does :) thank you!

gerasimovpk avatar Mar 22 '17 12:03 gerasimovpk

Now I have problems to use elements of joint.shapes.devs, I have this error Property 'devs' does not exist on type 'typeof shapes'

uroti avatar May 01 '17 09:05 uroti