all files / montage/core/serialization/ bindings.js

72.34% Statements 34/47
58.33% Branches 14/24
100% Functions 2/2
72.34% Lines 34/47
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                                                                                       11×           11× 11×   11×         11×                                     11×                      
var Bindings = require("frb"),
    stringify = require("frb/stringify"),
    expand = require("frb/expand"),
    Scope = require("frb/scope"),
    Serializer = require("../serialization/serializer/montage-serializer").MontageSerializer,
    Deserializer = require("../serialization/deserializer/montage-deserializer").MontageDeserializer,
    BOUND_OBJECT  = "boundObject",
    ONE_WAY = "<-",
    TWO_WAY = "<->";
 
 
Serializer.defineSerializationUnit("bindings", function (serializer, object) {
    var inputs = Bindings.getBindings(object),
        outputs = {},
        hasBindings,
        mapIter = inputs.keys(),
        targetPath;
 
    while (targetPath = mapIter.next().value) {
    //for (var targetPath in inputs) {
        var input = inputs.get(targetPath);
 
        var output = {};
 
        Iif (("serializable" in input) && !input.serializable) {
            continue;
        } else Iif (!input.sourceSyntax) {
            continue;
        }
 
        var sourcePath = input.sourcePath;
        var syntax = input.sourceSyntax;
        if (input.source && input.source !== object) {
            var label = serializer.getObjectLabel(input.source);
            var scope = new Scope({
                type: "component",
                label: label
            });
            scope.components = serializer;
            syntax = expand(syntax, scope);
        }
 
        var scope = new Scope();
        scope.components = serializer;
        sourcePath = stringify(syntax, scope);
 
        Iif (input.twoWay) {
            output[TWO_WAY] = sourcePath;
        } else {
            output[ONE_WAY] = sourcePath;
        }
 
        Iif (input.converter) {
            output.converter = input.converter;
        } else {
            output.convert = input.convert;
            output.revert = input.revert;
        }
 
        Iif (input.trace) {
            output.trace = true;
        }
 
        outputs[targetPath] = output;
        hasBindings = true;
    }
 
    return hasBindings ? outputs : undefined;
});
 
Deserializer.defineDeserializationUnit("bindings", function (deserializer, object, bindings) {
    var commonDescriptor = {
            components: deserializer
        },
        targetPath,
        descriptor;
    // normalize old and busted bindings
    for (targetPath in bindings) {
        descriptor = bindings[targetPath];
 
        Iif (typeof descriptor !== "object") {
            throw new Error("Binding descriptor must be an object, not " + typeof descriptor);
            // TODO isolate the source document and produce a more useful error
        }
 
        Iif (BOUND_OBJECT in descriptor) {
            descriptor.source = deserializer.getObjectByLabel(descriptor.boundObject);
            if (descriptor.oneway) {
                descriptor[ONE_WAY] = descriptor.boundPropertyPath;
            } else {
                descriptor[TWO_WAY] = descriptor.boundPropertyPath;
            }
            delete descriptor.boundObject;
            delete descriptor.boundObjectPropertyPath;
            delete descriptor.oneway;
        }
        // else {
        //     if (descriptor["<<->"]) {
        //         console.warn("WARNING: <<-> in bindings is deprectated, use <-> only, please update now.");
        //         descriptor[TWO_WAY] = descriptor["<<->"];
        //         delete descriptor["<<->"];
        //     }
        // }
 
        Bindings.defineBinding(object, targetPath, descriptor, commonDescriptor);
 
    }
 
    // Bindings.defineBindings(object, bindings, {
    //     components: deserializer
    // });
 
 
});