Enhancing Custom Elements in JointJS with a Port

I followed the steps from the jointjs tutorial to create a custom element, which looks like this:


CustomRect = joint.dia.Element.define('example.CustomRect', {
  attrs: {
    rect: {
      refX: 0,
      refY: 0,
      refWidth: '116',
      refHeight: '70',
      strokeWidth: 1,
      stroke: '#000000',
      fill: '#FFFFFF'
    },
    label: {
      textAnchor: 'left',
      refX: 10,
      fill: 'black',
      fontSize: 18,
      text: 'text'
    },
    upperRect: {
      strokeWidth: 1,
      stroke: '#000000',
      fill: 'rgba(255,0,0,0.3)',
      cursor: 'pointer'
    }
  }
}, {
    markup: [{
      tagName: 'rect',
      selector: 'rect'
    },
    {
      tagName: 'text',
      selector: 'label'
    },
    {
      tagName: 'rect',
      selector: 'upperRect'
    }]
})

...

let customRect = new this.CustomRect()

customRect.attr({
  upperRect: {
    refWidth: '116',
    refHeight: '10',
    refX: 0,
    refY: -11,
    event: 'element:upperRect:pointerdown'
  }
})

In addition, I attempted to include Ports using the Port API for joint.dia.Element as mentioned in the Port Tutorial:

customRect.addPorts([
  {
    args: {
      y: 30
    },
    z: 0
  }
]);

Although Ports were successfully added to my element, they lacked the expected functionality and appeared as inactive circles.

The approach outlined in the Port Tutorial did not yield desired results since I have not utilized joint.shapes.devs.Model for my custom element, fearing limitations in customization compared to joint.dia.Element (or so I believe).

Hence, I am seeking guidance on how to effectively integrate Ports into a custom element defined with joint.dia.Element while ensuring their proper functionality as demonstrated in the Port Tutorial.

Answer №1

Regrettably, the documentation lacks clarity on this topic. To enable interactivity for ports, it is necessary to define the magnet attribute for them.

const CustomRect = joint.dia.Element.define('example.CustomRect', {
  /* ... */
  attrs: {
    root: {
      // Prevent the element's root from being a connection target
      magnet: false
    }
  },
  ports: {
    items: [{
      id: 'port1',
      attrs: {
        portBody: {
          // The port can act as a connection target
          // Users can create links by dragging from this port
          magnet: true
        }
      }
    }, {
      id: 'port2',
      attrs: {
        portBody: {
          // The port can only serve as a connection target
          // Behavior can be adjusted using paper.options.validateMagnet()
          magnet: 'passive'
        }
      }
    }]
  }
}, {
  /* ... */
  portMarkup: [{
    tagName: 'circle',
    selector: 'portBody',
    attributes: {
      'r': 10,
      'fill': '#FFFFFF',
      'stroke': '#000000'
      // If all magnets are `true` / `passive`, you can specify the magnet here in the default port markup or per a port group
      // 'magnet': true
    }
  }]
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Increase the vertical distance between rows in a table

Having some issues with customizing a data grid that I developed. Is there a way to eliminate the header bottom border and insert spacing between each row in the table? View Demo Example Code: <dx-data-grid style="margin-top:50px" class="table" [dat ...

Develop a JavaScript and Node JS controller API from scratch

As someone new to creating a REST API Controller using Javascript, I am working on a project that requires putting an API into controllers and API groups. However, I'm feeling confused about which code should go in the controllers and which should go ...

Guide to integrating numerous product filters for an ECommerce platform using Spring Boot, JavaScript, Ajax, and MySql

Currently, I am developing an E-Commerce Web App using Spring Boot which includes a feature to add multiple product filters. The user can select checkboxes corresponding to their preferences for filtering or searching products from the store. However, I am ...

Troubleshooting Issue: Data not appearing on Angular frontend when fetching from Laravel API

Utilizing Laravel for the back end and Angular for the front end development. The code segments related to Angular can be found in employee.component.ts file: import { Component, OnInit } from '@angular/core'; import { DataService } from 'sr ...

Error: React-Redux/Jest Invariant Violation - The "store" could not be located

I have a test file set up that is similar to the one used by create-react-app. Here is my App.test.js file: App.test.js import React from 'react'; import ReactDOM from 'react-dom'; import { App } from './App'; it('rend ...

Utilizing useClass in Angular's APP_INITIALIZER

Before my application starts up, I require some API data and am currently handling it with APP_INITIALIZER and useFactory. However, I aim to enhance the structure by separating all the code from app.module.ts: app.module.ts import { NgModule} from '@ ...

Deploying a Meteor and Angular2 application to Heroku

Currently working on an app using Meteor, Angular2 with the angular-meteor package, Typescript, and MongoDB. Running into issues while trying to deploy it on Heroku. Utilizing the meteor buildpack from jordansissel/heroku-buildpack-meteor. Uncertain whethe ...

Web API - Unable to retrieve resource: the server returned a status code of 404

Currently, I am working on an AngularJS project with Web API integration. One of the controllers I have is called Controller/MasterController, and this is configured in my WebApi Config: HttpConfiguration config = new HttpConfiguration(); config.Routes.M ...

Generating an Xpath for the selected element with the help of Javascript or Jquery - here's how!

On my website, I have implemented a click event on an element. When a user clicks on this element, I want to generate the XPath of that particular element starting from either the html or body tag, also known as the Absolute Xpath. For example, if I click ...

javascript code not functioning properly

Something simple! In my asp.net-MVC project, I have a button and an external JavaScript file called mydata.js. This file contains a function called checkJS(). function checkJs() { debugger; alert("your output!!!"); } Here is my code: <div id="m ...

The ng-route feature seems to be malfunctioning and I am unable to identify the error, as no information is being displayed in the console

Here is the code in my boot.php file where I have set up the links <ul class="nav nav-pills nav-stacked"> <li role="presentation"><a href="#/valley">Valley</a></li> <li role="presentation"><a href="#/beach"> ...

What are some ways to resolve this console error: "TS2307: Could not locate module '@components/common/ButtonBlock' or its corresponding type declarations."

While the project is running smoothly, I am noticing a multitude of errors appearing in the console of VS Code. How can I eliminate these error messages? It seems to be related to TypeScript. Additionally, I am encountering an error in the browser as well ...

Emailer: Missing Salutation

While attempting to send emails using Node with Nodemailer (https://github.com/nodemailer/nodemailer), the sendMail call from the Nodemailer transporter is throwing an error message of Greeting never received when connected to an Ethereal test email accoun ...

Angular does not include the ControlGroup feature in its common offerings

I am new to TypeScript and have been following tutorials in an attempt to accomplish simple tasks, but so far without success. Many tutorials seem outdated with changes in Angular or provide instructions that do not work at all. Even the tutorials that do ...

Navigating Angular's Resolve Scope Challenges

As a junior developer, I've been diving into Angular.js and exploring the resolve feature of the route provider to preload my Project data from a service before the page loads. Previously, I was fetching the data directly inside the controller. Howeve ...

Adjusting dimensions of igc-star module in Angular

Is there a way to adjust the size of the igc-star component from the igniteui-webcomponents package when using it in an Angular application? I attempted to modify the font size in the CSS, but it didn't impact the size. The only change I was able to ...

Is it possible to utilize a function to manipulate the [checked] attribute of a checkbox and toggle it between true and false?

I'm trying to update the checkbox's [checked] property by calling a method, but even though the method is being called, the checkbox status doesn't change. HTML <div*ngFor="let vest_style of VEST_STYLE"> <input type="checkbox" ...

Firebase console does not show any console.log output for TypeScript cloud functions

I encountered an issue while using Typescript to write some Firebase cloud functions. Here is a snippet of my code: index.ts export * from "./Module1"; Module1.ts import * as functions from "firebase-functions"; export const test = functions.https.onR ...

How to refresh component after clearing dates in Vuetify datepicker

Currently, I am working with a datepicker in Vuetify JS and I need to find a way to clear the dates and refresh the component. In addition, there is a v-data table that is filtered based on the dates range array. I am exploring options to either add a &ap ...

choose the text following a specific section within the class attribute of an element

In my code, I am trying to extract a number from an input text element using the jQuery method find. The class name of the input text contains a variable number denoted as x, following the substring page-id. Here is the approach I have taken: var id = ui ...