client/js/dataloader.js
author rougeronj
Fri, 05 Jun 2015 12:17:16 +0200
changeset 472 6dcff4438175
parent 471 e0c7be5dc02c
parent 458 423bdf56d103
child 481 66c3b474a331
permissions -rw-r--r--
Merge with 41325cc50574ea85384c21e54090ebfdb929f687
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
444
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
(function(root) {
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
    "use strict";
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
    var DataLoader = root.Rkns.DataLoader = {
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
        converters: {
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
            from1to2: function(data) {
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
458
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
     9
                var i, len;
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    10
                if(typeof data.nodes !== 'undefined') {
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    11
                    for(i=0, len=data.nodes.length; i<len; i++) {
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    12
                        var node = data.nodes[i];
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    13
                        if(node.color) {
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    14
                            console.log("node color : ", node.color);
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    15
                            node.style = {
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    16
                                color: node.color,
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    17
                            };
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    18
                        }
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    19
                        else {
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    20
                            node.style = {};
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    21
                        }
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    22
                    }
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    23
                }
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    24
                if(typeof data.edges !== 'undefined') {
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    25
                    for(i=0, len=data.edges.length; i<len; i++) {
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    26
                        var edge = data.edges[i];
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    27
                        if(edge.color) {
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    28
                            edge.style = {
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    29
                                color: edge.color,
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    30
                            };
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    31
                        }
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    32
                        else {
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    33
                            edge.style = {};
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    34
                        }
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    35
                    }
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    36
                }
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    37
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    38
                data.schema_version = "2";
444
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
                return data;
458
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    41
            },
444
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
        }
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
    };
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
    DataLoader.Loader = function(project, options) {
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
        this.project = project;
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
        this.dataConverters = _.defaults(options.converters || {}, DataLoader.converters);
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
    };
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
    DataLoader.Loader.prototype.convert = function(data) {
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
        var schemaVersionFrom = this.project.getSchemaVersion(data);
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
        var schemaVersionTo = this.project.getSchemaVersion();
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
        if (schemaVersionFrom !== schemaVersionTo) {
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
            var converterName = "from" + schemaVersionFrom + "to" + schemaVersionTo;
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
            if (typeof this.dataConverters[converterName] === 'function') {
458
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    59
                console.log("Apply conversion function :", converterName);
444
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
                data = this.dataConverters[converterName](data);
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
            }
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
        }
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
        return data;
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
    };
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
    DataLoader.Loader.prototype.load = function(data) {
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
        this.project.set(this.convert(data), {
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
            validate: true
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
        });
471
e0c7be5dc02c Add a router to handle fragment identifier
rougeronj
parents: 444
diff changeset
    70
        Backbone.history.start();
444
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
    };
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
})(window);