client/js/dataloader.js
author ymh <ymh.work@gmail.com>
Thu, 05 Jan 2017 19:34:53 +0100
changeset 648 e388117572d8
parent 543 5f7bebdcfc0d
permissions -rw-r--r--
Improve display of editor tooltip. Add a vertical scroll if they too tall.
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
                            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
    15
                                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
    16
                            };
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
                        else {
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    19
                            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
    20
                        }
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
                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
    24
                    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
    25
                        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
    26
                        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
    27
                            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
    28
                                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
    29
                            };
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
                        else {
423bdf56d103 migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents: 444
diff changeset
    32
                            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
    33
                        }
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
                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
    38
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
                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
    40
            },
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
    41
        }
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
    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
    46
        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
    47
        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
    48
    };
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
    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
    52
        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
    53
        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
    54
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
        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
    56
            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
    57
            if (typeof this.dataConverters[converterName] === 'function') {
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
                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
    59
            }
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
        }
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
        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
    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
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
    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
    65
        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
    66
            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
    67
        });
543
5f7bebdcfc0d Improve the way we init the view. The data loader send a "loaded" event, hooked by the scene.py and initializing the backbone.history and the view.
rougeronj
parents: 485
diff changeset
    68
        this.project.trigger('loaded', true);
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
    69
    };
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
    70
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
})(window);