all files / diagram/entity-relationship/ er-columnar-layout.js

97.45% Statements 191/196
86.13% Branches 149/173
100% Functions 23/23
98.43% Lines 188/191
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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299   847× 847× 107×   740× 740× 2377× 2377× 2371× 2371×       2371× 2371× 2371×     740× 740×   862× 862× 1302×   780× 1539× 1680× 2176×   862×   563×           563× 563× 263× 263× 263× 263×     563× 563× 563× 563× 443×     563× 438× 438× 438× 159×     563× 164×   563×   839× 839× 839× 839× 357× 357×   839× 839× 839×   839× 598× 598×   839× 200× 200×   839× 613×   839×   262×   258× 111×   147× 26×   121×   556× 556× 556× 556× 556×             556× 2260× 2260× 2260× 2260× 157×     2103×   2260× 2260× 2055×   2260×     2260×                 2260× 852× 852× 852× 852×   2260×   556× 262× 262× 262× 262× 262×     556× 556× 556× 437× 437×     556× 433× 433× 153× 153×     556× 157× 157× 19×   138× 60×   78× 22×   157×   556×   260×     260× 820× 820× 216×     44×   38×             19× 19×         19×     19× 19× 19×   19× 19×     19× 19× 19× 19×   555× 555× 555× 555× 555×           555× 555× 555× 555×     15×               284× 284× 284×           284×        
define(["require", "exports", "../utility/base-util", "../enum/enum", "../core/elements/text-element", "../utility/dom-util"], function (require, exports, base_util_1, enum_1, text_element_1, dom_util_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var MARGIN_LEFT = 5;
    var MARGIN_RIGHT = 5;
    var SEPARATOR_WIDTH = 8;
    var NAME_WIDTH = 55;
    var STATIC_KEY_WIDTH = 15;
    var STATIC_TYPE_WIDTH = 50;
    var STATIC_CONSTRAINT_WIDTH = 25;
    var BUFFER_VALUE = -60;
    var DUAL_KEY_EXTRA = 12;
    function measureNameColumnWidth(entityShape) {
        var fields = entityShape.fields || [];
        if (fields.length === 0) {
            return NAME_WIDTH;
        }
        var maxNameWidth = 0;
        for (var _i = 0, fields_1 = fields; _i < fields_1.length; _i++) {
            var field = fields_1[_i];
            if (field.name) {
                var tempTextElement = new text_element_1.TextElement();
                tempTextElement.style = {
                    fontSize: 11,
                    fontFamily: 'Arial'
                };
                tempTextElement.content = field.name;
                var measuredSize = dom_util_1.measureText(tempTextElement, tempTextElement.style, field.name, 200);
                maxNameWidth = Math.max(maxNameWidth, measuredSize.width);
            }
        }
        var MIN_NAME_WIDTH = 45;
        return Math.max(maxNameWidth + BUFFER_VALUE, MIN_NAME_WIDTH);
    }
    function analyzeColumns(entityShape) {
        var fields = entityShape.fields || [];
        var config = {
            hasKey: fields.some(function (f) { return f.isPrimaryKey === true || f.isForeignKey === true; }),
            hasName: true,
            hasType: fields.some(function (f) { return !!f.dataType; }),
            hasNotNull: fields.some(function (f) { return f.constraints && f.constraints.indexOf('NotNull') !== -1; }),
            hasUnique: fields.some(function (f) { return f.constraints && f.constraints.indexOf('Unique') !== -1; }),
            hasDualKey: fields.some(function (f) { return f.isPrimaryKey === true && f.isForeignKey === true; })
        };
        return config;
    }
    function calculateColumnPositions(config, columnWidths, fieldWidth) {
        var positions = {
            key: 0,
            name: 0,
            type: 0,
            constraint: 0
        };
        var currentX = MARGIN_LEFT;
        if (config.hasKey) {
            positions.key = currentX;
            currentX += columnWidths.keyWidth;
            Eif (config.hasName || config.hasType || config.hasNotNull || config.hasUnique) {
                currentX += SEPARATOR_WIDTH;
            }
        }
        Eif (config.hasName) {
            positions.name = currentX;
            currentX += columnWidths.nameWidth;
            if (config.hasType || config.hasNotNull || config.hasUnique) {
                currentX += SEPARATOR_WIDTH;
            }
        }
        if (config.hasType) {
            positions.type = currentX;
            currentX += columnWidths.typeWidth;
            if (config.hasNotNull || config.hasUnique) {
                currentX += SEPARATOR_WIDTH;
            }
        }
        if (config.hasNotNull || config.hasUnique) {
            positions.constraint = -columnWidths.constraintWidth;
        }
        return positions;
    }
    function calculateMinimumEntityWidth(config, columnWidths) {
        var MARGIN = MARGIN_LEFT + MARGIN_RIGHT;
        var totalWidth = MARGIN;
        var hasContentColumns = 0;
        if (config.hasKey) {
            totalWidth += columnWidths.keyWidth;
            hasContentColumns++;
        }
        Eif (config.hasName) {
            totalWidth += columnWidths.nameWidth;
            hasContentColumns++;
        }
        if (config.hasType) {
            totalWidth += columnWidths.typeWidth;
            hasContentColumns++;
        }
        if (config.hasNotNull || config.hasUnique) {
            totalWidth += columnWidths.constraintWidth;
            hasContentColumns++;
        }
        if (hasContentColumns > 1) {
            totalWidth += SEPARATOR_WIDTH * (hasContentColumns - 1);
        }
        return totalWidth;
    }
    exports.calculateMinimumEntityWidth = calculateMinimumEntityWidth;
    function getKeyIndicator(field) {
        if (field.isPrimaryKey && field.isForeignKey) {
            return 'PK, FK';
        }
        if (field.isPrimaryKey) {
            return 'PK';
        }
        if (field.isForeignKey) {
            return 'FK';
        }
        return '';
    }
    function createFieldAnnotations(field, config, positions, fieldWidth, columnWidths, separatorColor) {
        if (separatorColor === void 0) { separatorColor = '#9c9c9c'; }
        var annotations = [];
        var FONT_SIZE = 11;
        var SEPARATOR_FONT_SIZE = 29;
        if (!columnWidths) {
            columnWidths = {
                keyWidth: STATIC_KEY_WIDTH,
                nameWidth: NAME_WIDTH,
                typeWidth: STATIC_TYPE_WIDTH,
                constraintWidth: STATIC_CONSTRAINT_WIDTH
            };
        }
        var addAnnotation = function (field, content, xPixel, id, isSeparator, isRightAligned, customMargin) {
            Iif (isSeparator === void 0) { isSeparator = false; }
            Iif (isRightAligned === void 0) { isRightAligned = false; }
            var xOffset;
            if (isRightAligned || xPixel < 0) {
                xOffset = 1 + (xPixel / fieldWidth);
            }
            else {
                xOffset = xPixel / fieldWidth;
            }
            var fieldAnnotationStyle = {};
            if (field.annotation && field.annotation.style) {
                fieldAnnotationStyle = field.annotation.style;
            }
            var margin = customMargin ?
                { left: customMargin.left || 3, right: customMargin.right || 3, top: 0, bottom: 0 } :
                { left: 3, right: 3, top: 0, bottom: 0 };
            var annotationObj = {
                id: "er" + id.charAt(0).toUpperCase() + id.slice(1),
                content: content,
                offset: { x: xOffset, y: 0.5 },
                style: base_util_1.cloneObject(fieldAnnotationStyle),
                horizontalAlignment: isRightAligned ? 'Right' : 'Left',
                verticalAlignment: 'Center',
                margin: margin
            };
            if (isSeparator) {
                annotationObj.constraints = enum_1.AnnotationConstraints.ReadOnly;
                annotationObj.style.fontSize = SEPARATOR_FONT_SIZE;
                annotationObj.style.color = separatorColor;
                annotationObj.style.fontFamily = 'Source Code Pro';
            }
            annotations.push(annotationObj);
        };
        if (config.hasKey) {
            var keyIndicator = getKeyIndicator(field);
            addAnnotation(field, keyIndicator, positions.key, 'Key', false, false);
            Eif (config.hasName || config.hasType || config.hasNotNull || config.hasUnique) {
                var separatorPos = positions.key + columnWidths.keyWidth;
                addAnnotation(field, '|', separatorPos, 'SepAfterKey', true, false);
            }
        }
        Eif (config.hasName) {
            addAnnotation(field, field.name, positions.name, 'Name', false, false);
            if (config.hasType || config.hasNotNull || config.hasUnique) {
                var separatorPos = positions.name + columnWidths.nameWidth;
                addAnnotation(field, '|', separatorPos, 'SepAfterName', true, false);
            }
        }
        if (config.hasType) {
            addAnnotation(field, field.dataType || '', positions.type, 'Type', false, false);
            if (config.hasNotNull || config.hasUnique) {
                var separatorPos = positions.type + columnWidths.typeWidth;
                addAnnotation(field, '|', separatorPos, 'SepAfterType', true, false);
            }
        }
        if (config.hasNotNull || config.hasUnique) {
            var constraintText = '';
            if (field.constraints && field.constraints.indexOf('NotNull') !== -1 && field.constraints.indexOf('Unique') !== -1) {
                constraintText = 'NN, U';
            }
            else if (field.constraints && field.constraints.indexOf('NotNull') !== -1) {
                constraintText = 'NN';
            }
            else if (field.constraints && field.constraints.indexOf('Unique') !== -1) {
                constraintText = 'U';
            }
            addAnnotation(field, constraintText, positions.constraint, 'Constraint', false, false, { left: 3, right: 5 });
        }
        return annotations;
    }
    exports.createFieldAnnotations = createFieldAnnotations;
    function getAnnotationOffsetX(annotations, annotationId) {
        Iif (!annotations) {
            return undefined;
        }
        for (var i = 0; i < annotations.length; i++) {
            var annotation = annotations[parseInt(i.toString(), 10)];
            if (annotation && annotation.id === annotationId && annotation.offset && annotation.offset.x !== undefined) {
                return annotation.offset.x;
            }
        }
        return undefined;
    }
    function getFieldAnnotationPositions(annotations) {
        return {
            key: getAnnotationOffsetX(annotations, 'erKey') !== undefined ? getAnnotationOffsetX(annotations, 'erKey') : 0,
            name: getAnnotationOffsetX(annotations, 'erName') !== undefined ? getAnnotationOffsetX(annotations, 'erName') : 0,
            type: getAnnotationOffsetX(annotations, 'erType') !== undefined ? getAnnotationOffsetX(annotations, 'erType') : 0,
            constraint: getAnnotationOffsetX(annotations, 'erConstraint') !== undefined ? getAnnotationOffsetX(annotations, 'erConstraint') : 0
        };
    }
    function arePositionsEqual(positionA, positionB) {
        var TOLERANCE = 0.0001;
        return Math.abs(positionA.key - positionB.key) < TOLERANCE &&
            Math.abs(positionA.name - positionB.name) < TOLERANCE &&
            Math.abs(positionA.type - positionB.type) < TOLERANCE &&
            Math.abs(positionA.constraint - positionB.constraint) < TOLERANCE;
    }
    function areFieldRowPositionsEqual(fieldNode, parentEntity, diagram) {
        Iif (!fieldNode || !fieldNode.annotations || !parentEntity || !parentEntity.shape) {
            return true;
        }
        var erEntity = parentEntity.shape;
        var fieldNodeAsAny = fieldNode;
        var fieldIndex = (fieldNodeAsAny.umlIndex !== undefined && fieldNodeAsAny.umlIndex !== null)
            ? fieldNodeAsAny.umlIndex - 1 : -1;
        var fields = erEntity.fields || [];
        Iif (fieldIndex < 0 || fieldIndex >= fields.length) {
            return true;
        }
        var expectedAnnotations = generateFieldRowAnnotations(parentEntity, fields[parseInt(fieldIndex.toString(), 10)], diagram, '#cccccc');
        var expectedPositions = getFieldAnnotationPositions(expectedAnnotations);
        var actualPositions = getFieldAnnotationPositions(fieldNode.annotations);
        return arePositionsEqual(expectedPositions, actualPositions);
    }
    exports.areFieldRowPositionsEqual = areFieldRowPositionsEqual;
    function generateFieldRowAnnotations(parentEntity, field, diagram, separatorColor) {
        if (separatorColor === void 0) { separatorColor = '#cccccc'; }
        var entityShape = parentEntity.shape;
        var config = analyzeColumns(entityShape);
        var measuredNameWidth = measureNameColumnWidth(entityShape);
        var columnWidths = {
            keyWidth: STATIC_KEY_WIDTH + (config.hasDualKey ? DUAL_KEY_EXTRA : 0),
            nameWidth: measuredNameWidth,
            typeWidth: STATIC_TYPE_WIDTH,
            constraintWidth: STATIC_CONSTRAINT_WIDTH
        };
        var fieldWidth = calculateMinimumEntityWidth(config, columnWidths);
        var positions = calculateColumnPositions(config, columnWidths, fieldWidth);
        var annotations = createFieldAnnotations(field, config, positions, fieldWidth, columnWidths, separatorColor);
        return annotations;
    }
    exports.generateFieldRowAnnotations = generateFieldRowAnnotations;
    var ERColumnarLayoutFactory = (function () {
        function ERColumnarLayoutFactory() {
        }
        ERColumnarLayoutFactory.getColumnConfig = function (entityShape) {
            return analyzeColumns(entityShape);
        };
        ERColumnarLayoutFactory.getColumnPositions = function (entityShape, fieldWidth) {
            Eif (fieldWidth === void 0) { fieldWidth = 220; }
            var config = analyzeColumns(entityShape);
            var measuredNameWidth = measureNameColumnWidth(entityShape);
            var columnWidths = {
                keyWidth: STATIC_KEY_WIDTH + (config.hasDualKey ? DUAL_KEY_EXTRA : 0),
                nameWidth: measuredNameWidth,
                typeWidth: STATIC_TYPE_WIDTH,
                constraintWidth: STATIC_CONSTRAINT_WIDTH
            };
            return calculateColumnPositions(config, columnWidths, fieldWidth);
        };
        ERColumnarLayoutFactory.calculateMinimumWidth = function (entityShape) {
            var config = analyzeColumns(entityShape);
            var measuredNameWidth = measureNameColumnWidth(entityShape);
            var columnWidths = {
                keyWidth: STATIC_KEY_WIDTH + (config.hasDualKey ? DUAL_KEY_EXTRA : 0),
                nameWidth: measuredNameWidth,
                typeWidth: STATIC_TYPE_WIDTH,
                constraintWidth: STATIC_CONSTRAINT_WIDTH
            };
            return calculateMinimumEntityWidth(config, columnWidths);
        };
        return ERColumnarLayoutFactory;
    }());
    exports.ERColumnarLayoutFactory = ERColumnarLayoutFactory;
});