all files / montage/core/serialization/serializer/ montage-labeler.js

90.48% Statements 57/63
95.83% Branches 23/24
93.75% Functions 15/16
90.48% Lines 57/63
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                             173× 173× 173× 173× 173×                           210×     210× 158×   52× 52×     210×           208×   208×     204×           56×     56× 54× 43× 43×   11× 10×     10×       56×                   106× 121× 121×                                 73×     73× 94× 25× 25×   69× 69×       73×           18×           237×                                 18×           53×   53×           188× 187×   187× 187× 187×                    
var Montage = require("../../core").Montage;
 
exports.MontageLabeler = Montage.specialize({
    _labelRegexp: {value: /^[a-zA-Z_$][0-9a-zA-Z_$]*$/},
    _labels: {value: null},
    // hash(object) -> label
    _objectsLabels: {value: null},
    _objects: {value: null},
    // Labels generation sequence is "label", "label2", "label3", ..., hence
    // starting at 2.
    _INITIAL_LABEL_NUMBER: {value: 2},
    _baseNamesIndex: {value: null},
    _userDefinedLabels: {value: null},
 
    constructor: {
        value: function MontageLabeler() {
            this._labels = Object.create(null);
            this._objectsLabels = Object.create(null);
            this._objects = Object.create(null);
            this._baseNamesIndex = Object.create(null);
            this._userDefinedLabels = Object.create(null);
        }
    },
 
    getTemplatePropertyLabel: {
        value: function (object) {
            var label = this._getObjectLabel(object);
 
            if (label[0] !== ":") {
                throw new Error("Template property's labels need to start with a colon (:), (\"" + label + "\").");
            }
 
            return label;
        }
    },
 
    _getObjectLabel: {
        value: function(object) {
            var hash = Object.hash(object),
                label;
 
            if (hash in this._objectsLabels) {
                label = this._objectsLabels[hash];
            } else {
                label = this.generateObjectLabel(object);
                this.setObjectLabel(object, label);
            }
 
            return label;
        }
    },
 
    getObjectLabel: {
        value: function (object) {
            var label = this._getObjectLabel(object);
 
            if (label[0] === ":") {
                throw new Error("Labels starting with colon (:) can only be used for template properties, (\"" + label + "\").");
            }
 
            return label;
        }
    },
 
    getObjectName: {
        value: function (object) {
            var identifier = object.identifier,
                objectName;
 
            if (identifier && this._labelRegexp.test(identifier)) {
                objectName = object.identifier;
            } else if (object && typeof object === "object" && "getInfoForObject" in object || "getInfoForObject" in object.constructor ) {
                objectName = Montage.getInfoForObject(object).objectName;
                objectName = objectName.toLowerCase();
            } else {
                if (Array.isArray(object)) {
                    objectName = "array";
                } else Iif (RegExp.isRegExp(object)) {
                    objectName = "regexp";
                } else {
                    objectName =  typeof object;
                }
            }
 
            return objectName;
        }
    },
 
    /**
     * When the labeler is initialized with objects these objects are
     * considered user defined objects.
     */
    initWithObjects: {
        value: function(labels) {
            for (var label in labels) {
                this.setObjectLabel(labels[label], label);
                this._userDefinedLabels[label] = true;
            }
        }
    },
 
    cleanup: {
        value: function() {
            this._labels = null;
            this._objectsLabels = null;
            this._objects = null;
            this._baseNamesIndex = null;
            this._userDefinedLabels = null;
        }
    },
 
    generateLabel: {
        value: function(baseName) {
            var index = this._baseNamesIndex[baseName],
                label;
 
            do {
                if (index) {
                    label = baseName + index;
                    this._baseNamesIndex[baseName] = index = index + 1;
                } else {
                    label = baseName;
                    this._baseNamesIndex[baseName] = index = this._INITIAL_LABEL_NUMBER;
                }
            } while (label in this._labels);
 
            return label;
        }
    },
 
    getLabelBaseName: {
        value: function(label) {
            return label.replace(/\d*$/, "");
        }
    },
 
    addLabel: {
        value: function(label) {
            this._labels[label] = true;
        }
    },
 
    addLabels: {
        value: function(labels) {
            for (var i = 0, ii = labels.length; i < ii; i++) {
                this.addLabel(labels[i]);
            }
        }
    },
 
    isLabelDefined: {
        value: function(label) {
            return label in this._labels;
        }
    },
 
    isUserDefinedLabel: {
        value: function(label) {
            return label in this._userDefinedLabels;
        }
    },
 
    generateObjectLabel: {
        value: function(object) {
            var objectName = this.getObjectName(object);
 
            return this.generateLabel(objectName);
        }
    },
 
    setObjectLabel: {
        value: function(object, label) {
            if (typeof object !== "undefined") {
                var hash = Object.hash(object);
 
                this.addLabel(label);
                this._objectsLabels[hash] = label;
                this._objects[label] = object;
            }
        }
    },
 
    getObjectByLabel: {
        value: function(label) {
            return this._objects[label];
        }
    }
});