Skip to content

Commit

Permalink
chore(project): es6ify
Browse files Browse the repository at this point in the history
* use import / export
* TOOLKIT: export individual functions from utlitites
* TEST: localize TestHelper includes

BREAKING CHANGE:

* utilities expose individual functions now
* libraries built upon diagram-js must now use a ES6
  transpiler to consume modules and resolve import/export
  declarations
* without special precautions using from CommonJS libraries
  requires loading the `default` export in most cases via
  `require('diagram-js/some-component').default`
  • Loading branch information
nikku committed Apr 2, 2018
1 parent 47ef0f8 commit e26b034
Show file tree
Hide file tree
Showing 302 changed files with 2,982 additions and 2,213 deletions.
22 changes: 13 additions & 9 deletions lib/Diagram.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

var Injector = require('didi').Injector;
import { Injector } from 'didi';

import CoreModule from './core';


/**
Expand Down Expand Up @@ -77,9 +79,7 @@ function createInjector(options) {
'config': ['value', options]
};

var coreModule = require('./core');

var modules = [ configModule, coreModule ].concat(options.modules || []);
var modules = [ configModule, CoreModule ].concat(options.modules || []);

return bootstrap(modules);
}
Expand Down Expand Up @@ -107,15 +107,21 @@ function createInjector(options) {
* }
*
* // export as module
* module.exports = {
* export default {
* __init__: [ 'myLoggingPlugin' ],
* myLoggingPlugin: [ 'type', MyLoggingPlugin ]
* };
*
*
* // instantiate the diagram with the new plug-in
*
* var diagram = new Diagram({ modules: [ require('path-to-my-logging-plugin') ] });
* import MyLoggingModule from 'path-to-my-logging-plugin';
*
* var diagram = new Diagram({
* modules: [
* MyLoggingModule
* ]
* });
*
* diagram.invoke([ 'canvas', function(canvas) {
* // add shape to drawing canvas
Expand All @@ -128,7 +134,7 @@ function createInjector(options) {
* @param {Array<didi.Module>} [options.modules] external modules to instantiate with the diagram
* @param {didi.Injector} [injector] an (optional) injector to bootstrap the diagram with
*/
function Diagram(options, injector) {
export default function Diagram(options, injector) {

// create injector unless explicitly specified
this.injector = injector = injector || createInjector(options);
Expand Down Expand Up @@ -180,8 +186,6 @@ function Diagram(options, injector) {
this.get('eventBus').fire('diagram.init');
}

module.exports = Diagram;


/**
* Destroys the diagram
Expand Down
4 changes: 1 addition & 3 deletions lib/command/CommandHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
* A command handler that may be registered with the
* {@link CommandStack} via {@link CommandStack#registerHandler}.
*/
function CommandHandler() {}

module.exports = CommandHandler;
export default function CommandHandler() {}


/**
Expand Down
24 changes: 10 additions & 14 deletions lib/command/CommandInterceptor.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
'use strict';

var forEach = require('min-dash').forEach,
isFunction = require('min-dash').isFunction,
isArray = require('min-dash').isArray,
isNumber = require('min-dash').isNumber;
import {
forEach,
isFunction,
isArray,
isNumber,
isObject
} from 'min-dash';


var DEFAULT_PRIORITY = 1000;


function isObject(element) {
return typeof element === 'object';
}

/**
* A utility that can be used to plug-in into the command execution for
* extension and/or validation.
Expand All @@ -21,9 +19,9 @@ function isObject(element) {
*
* @example
*
* var inherits = require('inherits');
* import inherits from 'inherits';
*
* var CommandInterceptor = require('diagram-js/lib/command/CommandInterceptor');
* import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
*
* function CommandLogger(eventBus) {
* CommandInterceptor.call(this, eventBus);
Expand All @@ -36,14 +34,12 @@ function isObject(element) {
* inherits(CommandLogger, CommandInterceptor);
*
*/
function CommandInterceptor(eventBus) {
export default function CommandInterceptor(eventBus) {
this._eventBus = eventBus;
}

CommandInterceptor.$inject = [ 'eventBus' ];

module.exports = CommandInterceptor;

function unwrapEvent(fn, that) {
return function(event) {
return fn.call(that || null, event.context, event.command, event);
Expand Down
10 changes: 5 additions & 5 deletions lib/command/CommandStack.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';

var uniqueBy = require('min-dash').uniqueBy,
isArray = require('min-dash').isArray;
import {
uniqueBy,
isArray
} from 'min-dash';


/**
Expand Down Expand Up @@ -80,7 +82,7 @@ var uniqueBy = require('min-dash').uniqueBy,
* @param {EventBus} eventBus
* @param {Injector} injector
*/
function CommandStack(eventBus, injector) {
export default function CommandStack(eventBus, injector) {

/**
* A map of all registered command handlers.
Expand Down Expand Up @@ -129,8 +131,6 @@ function CommandStack(eventBus, injector) {

CommandStack.$inject = [ 'eventBus', 'injector' ];

module.exports = CommandStack;


/**
* Execute a command
Expand Down
6 changes: 4 additions & 2 deletions lib/command/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module.exports = {
commandStack: [ 'type', require('./CommandStack') ]
import CommandStack from './CommandStack';

export default {
commandStack: [ 'type', CommandStack ]
};
63 changes: 38 additions & 25 deletions lib/core/Canvas.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
'use strict';

var isNumber = require('min-dash').isNumber,
assign = require('min-dash').assign,
forEach = require('min-dash').forEach,
every = require('min-dash').every,
debounce = require('min-dash').debounce,
bind = require('min-dash').bind,
reduce = require('min-dash').reduce;

var Collections = require('../util/Collections'),
Elements = require('../util/Elements');

var svgAppend = require('tiny-svg').append,
svgAttr = require('tiny-svg').attr,
svgClasses = require('tiny-svg').classes,
svgCreate = require('tiny-svg').create,
svgTransform = require('tiny-svg').transform;

var createMatrix = require('tiny-svg').createMatrix;
import {
isNumber,
assign,
forEach,
every,
debounce,
bind,
reduce
} from 'min-dash';

import {
add as collectionAdd,
remove as collectionRemove
} from '../util/Collections';

import {
getType
} from '../util/Elements';

import {
append as svgAppend,
attr as svgAttr,
classes as svgClasses,
create as svgCreate,
transform as svgTransform
} from 'tiny-svg';

import { createMatrix as createMatrix } from 'tiny-svg';


function round(number, resolution) {
Expand Down Expand Up @@ -91,7 +101,7 @@ var REQUIRED_MODEL_ATTRS = {
* @param {GraphicsFactory} graphicsFactory
* @param {ElementRegistry} elementRegistry
*/
function Canvas(config, eventBus, graphicsFactory, elementRegistry) {
export default function Canvas(config, eventBus, graphicsFactory, elementRegistry) {

this._eventBus = eventBus;
this._elementRegistry = elementRegistry;
Expand All @@ -100,9 +110,12 @@ function Canvas(config, eventBus, graphicsFactory, elementRegistry) {
this._init(config || {});
}

Canvas.$inject = [ 'config.canvas', 'eventBus', 'graphicsFactory', 'elementRegistry' ];

module.exports = Canvas;
Canvas.$inject = [
'config.canvas',
'eventBus',
'graphicsFactory',
'elementRegistry'
];


Canvas.prototype._init = function(config) {
Expand Down Expand Up @@ -202,7 +215,7 @@ Canvas.prototype._clear = function() {

// remove all elements
allElements.forEach(function(element) {
var type = Elements.getType(element);
var type = getType(element);

if (type === 'root') {
self.setRootElement(null, true);
Expand Down Expand Up @@ -494,7 +507,7 @@ Canvas.prototype._ensureValid = function(type, element) {
};

Canvas.prototype._setParent = function(element, parent, parentIndex) {
Collections.add(parent.children, element, parentIndex);
collectionAdd(parent.children, element, parentIndex);
element.parent = parent;
};

Expand Down Expand Up @@ -592,7 +605,7 @@ Canvas.prototype._removeElement = function(element, type) {
graphicsFactory.remove(element);

// unset parent <-> child relationship
Collections.remove(element.parent && element.parent.children, element);
collectionRemove(element.parent && element.parent.children, element);
element.parent = null;

eventBus.fire(type + '.removed', { element: element });
Expand Down
12 changes: 6 additions & 6 deletions lib/core/ElementFactory.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
'use strict';

var Model = require('../model');
import {
create
} from '../model';

var assign = require('min-dash').assign;
import { assign } from 'min-dash';

/**
* A factory for diagram-js shapes
*/
function ElementFactory() {
export default function ElementFactory() {
this._uid = 12;
}

module.exports = ElementFactory;


ElementFactory.prototype.createRoot = function(attrs) {
return this.create('root', attrs);
Expand Down Expand Up @@ -46,5 +46,5 @@ ElementFactory.prototype.create = function(type, attrs) {
attrs.id = type + '_' + (this._uid++);
}

return Model.create(type, attrs);
return create(type, attrs);
};
6 changes: 2 additions & 4 deletions lib/core/ElementRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@

var ELEMENT_ID = 'data-element-id';

var svgAttr = require('tiny-svg').attr;
import { attr as svgAttr } from 'tiny-svg';


/**
* @class
*
* A registry that keeps track of all shapes in the diagram.
*/
function ElementRegistry(eventBus) {
export default function ElementRegistry(eventBus) {
this._elements = {};

this._eventBus = eventBus;
}

ElementRegistry.$inject = [ 'eventBus' ];

module.exports = ElementRegistry;

/**
* Register a pair of (element, gfx, (secondaryGfx)).
*
Expand Down
16 changes: 8 additions & 8 deletions lib/core/EventBus.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict';

var isFunction = require('min-dash').isFunction,
isArray = require('min-dash').isArray,
isNumber = require('min-dash').isNumber,
bind = require('min-dash').bind,
assign = require('min-dash').assign;
import {
isFunction,
isArray,
isNumber,
bind,
assign
} from 'min-dash';

var FN_REF = '__fn';

Expand Down Expand Up @@ -95,16 +97,14 @@ var slice = Array.prototype.slice;
* console.log(sum); // 3
* ```
*/
function EventBus() {
export default function EventBus() {
this._listeners = {};

// cleanup on destroy on lowest priority to allow
// message passing until the bitter end
this.on('diagram.destroy', 1, this._destroy, this);
}

module.exports = EventBus;


/**
* Register an event listener for events with the given name.
Expand Down
Loading

0 comments on commit e26b034

Please sign in to comment.