1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | 1× 148× 148× 148× 148× 16× 16× 16× 16× 16× 148× 37× 37× 37× 37× 37× 37× 37× 37× 16× 37× 37× 16× 1× 37× 1× 37× 36× 1× 1× 1× 1× 1× 1× 1× 1× 148× 16× 16× 16× 1× 1× 16× 16× 2× 2× 2× 1× 2× | import { append as svgAppend, attr as svgAttr, classes as svgClasses, create as svgCreate, remove as svgRemove, clear as svgClear } from 'tiny-svg'; import { isObject } from 'min-dash'; var LOW_PRIORITY = 740; /** * Shows connection preview during create. * * @param {didi.Injector} injector * @param {EventBus} eventBus * @param {Canvas} canvas * @param {GraphicsFactory} graphicsFactory * @param {ElementFactory} elementFactory */ export default function CreateConnectPreview( canvas, elementFactory, eventBus, graphicsFactory, injector ) { var self = this; this._elementFactory = elementFactory; var connectionDocking = injector.get('connectionDocking', false), layouter = injector.get('layouter', false); /** * Add and return preview graphics. * * @returns {SVGElement} */ function createConnectionPreviewGfx() { var gfx = svgCreate('g'); svgAttr(gfx, { pointerEvents: 'none' }); svgClasses(gfx).add('djs-dragger'); svgAppend(canvas.getDefaultLayer(), gfx); return gfx; } eventBus.on('create.move', LOW_PRIORITY, function(event) { var context = event.context, source = context.source, shape = context.shape, connectionPreviewGfx = context.connectionPreviewGfx, canExecute = context.canExecute, canConnect = canExecute && canExecute.connect, getConnection = context.getConnection, connection; if (!connectionPreviewGfx) { connectionPreviewGfx = context.connectionPreviewGfx = createConnectionPreviewGfx(); } svgClear(connectionPreviewGfx); if (!getConnection) { getConnection = context.getConnection = cacheReturnValues(function(canConnect, source, shape) { return self.getConnection(canConnect, source, shape); }); } if (canConnect) { connection = getConnection(canConnect, source, shape); } if (!connection) { return; } shape.x = Math.round(event.x - shape.width / 2); shape.y = Math.round(event.y - shape.height / 2); Eif (layouter) { connection.waypoints = []; connection.waypoints = layouter.layoutConnection(connection, { source: source, target: shape }); } else { connection.waypoints = [ { x: source.x + source.width / 2, y: source.y + source.height / 2 }, { x: event.x, y: event.y } ]; } Eif (connectionDocking) { connection.waypoints = connectionDocking.getCroppedWaypoints(connection, source, shape); } graphicsFactory.drawConnection(connectionPreviewGfx, connection); }); eventBus.on('create.cleanup', function(event) { var context = event.context; Eif (context.connectionPreviewGfx) { svgRemove(context.connectionPreviewGfx); } }); } CreateConnectPreview.$inject = [ 'canvas', 'elementFactory', 'eventBus', 'graphicsFactory', 'injector' ]; /** * Get connection that connect source and target once connect is finished. * * @param {djs.model.shape} source * @param {djs.model.shape} target * @param {Object|boolean} canConnect * * @returns {djs.model.connection} */ CreateConnectPreview.prototype.getConnection = function(canConnect, source, target) { var attrs = ensureConnectionAttrs(canConnect); return this._elementFactory.createConnection(attrs); }; // helpers ////////// /** * Returns function that returns cached return values referenced by stringfied first argument. * * @param {Function} fn * * @return {Function} */ function cacheReturnValues(fn) { var returnValues = {}; /** * Return cached return value referenced by stringified first argument. * * @returns {*} */ return function(firstArgument) { var key = JSON.stringify(firstArgument); var returnValue = returnValues[key]; if (!returnValue) { returnValue = returnValues[key] = fn.apply(null, arguments); } return returnValue; }; } /** * Ensure connection attributes is object. * * @param {Object|boolean} canConnect * * @returns {Object} */ function ensureConnectionAttrs(canConnect) { if (isObject(canConnect)) { return canConnect; } else { return {}; } } |