client/js/renderer/viewrepr.js
author rougeronj
Wed, 16 Sep 2015 17:36:46 +0200
changeset 524 904effa4b6d7
parent 521 0d9b3f1b97e7
child 525 03aa989092bb
permissions -rw-r--r--
improve view management: - we can pass a negative view index, in this case we count from the end of the list of view - before creating/changing the view representation, we remove the previous one and reinitialize the node visibility - each time we save a view, it creates a new one at the end of the list - "restore" view loads the last view of the list
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
506
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
     1
define(['jquery', 'underscore', 'requtils', 'renderer/baserepresentation'], function ($, _, requtils, BaseRepresentation) {
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
     2
    'use strict';
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
     3
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
     4
    var Utils = requtils.getUtils();
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
     5
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
     6
    /* Rkns.Renderer.View Class */
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
     7
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
     8
    /* The representation for the view. */
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
     9
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    10
    var ViewRepr = Utils.inherit(BaseRepresentation);
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    11
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    12
    _(ViewRepr.prototype).extend({
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    13
        _init: function() {
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    14
            var _this = this;
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    15
            this.$ = $(".Rk-Render");
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    16
            this.type = "View";
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    17
            this.hiddenNodes = [];
508
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
    18
            this.scale = 1;
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
    19
            this.initialScale = 1;
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
    20
            this.offset = paper.view.center;
510
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    21
            this.params = {};
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    22
            
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    23
            if (this.model){
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    24
                this.params = {
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    25
                    "zoom_level": _this.model.get("zoom_level"),
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    26
                    "offset": _this.model.get("offset"),
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    27
                    "hidden_nodes": _this.model.get("hidden_nodes")
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    28
                };
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    29
            }
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    30
                
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    31
            this.init();
506
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    32
            
508
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
    33
            var bindClick = function(selector, fname) {
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
    34
                _this.$.find(selector).click(function(evt) {
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
    35
                    _this[fname](evt);
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
    36
                    return false;
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
    37
                });
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
    38
            };
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
    39
            
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
    40
            bindClick(".Rk-ZoomOut", "zoomOut");
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
    41
            bindClick(".Rk-ZoomIn", "zoomIn");
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
    42
            bindClick(".Rk-ZoomFit", "autoScale");
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
    43
            
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
    44
            this.$.find(".Rk-ZoomSave").click( function() {
510
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    45
                var offset = {
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    46
                    "x": _this.offset.x,
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    47
                    "y": _this.offset.y
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    48
                };
524
904effa4b6d7 improve view management:
rougeronj
parents: 521
diff changeset
    49
                _this.model = _this.renkan.project.addView( { zoom_level:_this.scale, offset:offset, hidden_nodes: _this.hiddenNodes.concat() } );
510
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    50
                _this.params = {
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    51
                        "zoom_level": _this.model.get("zoom_level"),
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    52
                        "offset": _this.model.get("offset"),
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    53
                        "hidden_nodes": _this.model.get("hidden_nodes")
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    54
                };
519
b94a34c139c1 update parameters function to accept idView parameter
rougeronj
parents: 517
diff changeset
    55
                _this.updateUrl();
508
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
    56
            });
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
    57
            
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
    58
            this.$.find(".Rk-ZoomSetSaved").click( function() {
524
904effa4b6d7 improve view management:
rougeronj
parents: 521
diff changeset
    59
                _this.model = _this.renkan.project.get("views").at(_this.renkan.project.get("views").length -1);
904effa4b6d7 improve view management:
rougeronj
parents: 521
diff changeset
    60
                _this.params = {
904effa4b6d7 improve view management:
rougeronj
parents: 521
diff changeset
    61
                        "zoom_level": _this.model.get("zoom_level"),
904effa4b6d7 improve view management:
rougeronj
parents: 521
diff changeset
    62
                        "offset": _this.model.get("offset"),
904effa4b6d7 improve view management:
rougeronj
parents: 521
diff changeset
    63
                        "hidden_nodes": _this.model.get("hidden_nodes")
904effa4b6d7 improve view management:
rougeronj
parents: 521
diff changeset
    64
                };
510
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    65
                _this.setScale(_this.params.zoom_level, new paper.Point(_this.params.offset));
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    66
                _this.showNodes(false);
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    67
                if (_this.options.hide_nodes){
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    68
                    _this.hiddenNodes = (_this.params.hidden_nodes || []).concat();
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    69
                    _this.hideNodes();
506
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    70
                }
524
904effa4b6d7 improve view management:
rougeronj
parents: 521
diff changeset
    71
                _this.updateUrl();
508
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
    72
            });
506
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    73
            
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    74
            this.$.find(".Rk-ShowHiddenNodes").mouseenter( function() {
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    75
                _this.showNodes(true);
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    76
                _this.$.find(".Rk-ShowHiddenNodes").mouseleave( function() {
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    77
                    _this.hideNodes();
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    78
                });
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    79
            });
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    80
            this.$.find(".Rk-ShowHiddenNodes").click( function() {
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    81
                _this.showNodes(false);
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    82
                _this.$.find(".Rk-ShowHiddenNodes").off( "mouseleave" ); 
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    83
            });
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    84
            if(this.renkan.project.get("views").length > 0 && this.renkan.options.save_view){
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    85
                this.$.find(".Rk-ZoomSetSaved").show();
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    86
            }
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    87
        },
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    88
        redraw: function(options) {
510
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    89
            //console.log("view : ", this.model.toJSON());
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    90
        },
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    91
        init: function(){
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    92
            var _this = this;
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    93
            _this.setScale(_this.params.zoom_level, new paper.Point(_this.params.offset));
524
904effa4b6d7 improve view management:
rougeronj
parents: 521
diff changeset
    94
            
510
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    95
            if (_this.options.hide_nodes){
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    96
                _this.hiddenNodes = (_this.params.hidden_nodes || []).concat();
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    97
                _this.hideNodes();
a8f02d66bf02 adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents: 508
diff changeset
    98
            }
506
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
    99
        },
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   100
        addHiddenNode: function(_model){
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   101
            this.hideNode(_model);
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   102
            this.hiddenNodes.push(_model.id);
511
4a48a1a9fd1e update the url on papershift or zoom
rougeronj
parents: 510
diff changeset
   103
            this.updateUrl();
506
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   104
        },
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   105
        hideNode: function(_model){
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   106
            if (typeof this.renderer.getRepresentationByModel(_model) !== 'undefined'){
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   107
                this.renderer.getRepresentationByModel(_model).hide();
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   108
            }
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   109
        },
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   110
        hideNodes: function(){
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   111
            var _this = this;
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   112
            this.hiddenNodes.forEach(function(_id, index){
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   113
                var node = _this.renkan.project.get("nodes").get(_id);
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   114
                if (typeof node !== 'undefined'){
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   115
                    return _this.hideNode(_this.renkan.project.get("nodes").get(_id));
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   116
                }else{
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   117
                    _this.hiddenNodes.splice(index, 1);
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   118
                }
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   119
            });
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   120
            paper.view.draw();
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   121
        },
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   122
        showNodes: function(ghost){
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   123
            var _this = this;
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   124
            this.hiddenNodes.forEach(function(_id){
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   125
                _this.renderer.getRepresentationByModel(_this.renkan.project.get("nodes").get(_id)).show(ghost);
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   126
            });
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   127
            if (!ghost){
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   128
                this.hiddenNodes = [];
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   129
            }
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   130
            paper.view.draw();
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   131
        },
508
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   132
        setScale: function(_newScale, _offset) {
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   133
            if ((_newScale/this.initialScale) > Utils._MIN_SCALE && (_newScale/this.initialScale) < Utils._MAX_SCALE) {
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   134
                this.scale = _newScale;
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   135
                if (_offset) {
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   136
                    this.offset = _offset;
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   137
                }
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   138
                this.renderer.redraw();
511
4a48a1a9fd1e update the url on papershift or zoom
rougeronj
parents: 510
diff changeset
   139
                this.updateUrl();
508
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   140
            }
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   141
        },
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   142
        zoomOut: function() {
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   143
            var _newScale = this.scale * Math.SQRT1_2,
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   144
            _offset = new paper.Point([
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   145
                                       this.renderer.canvas_$.width(),
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   146
                                       this.renderer.canvas_$.height()
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   147
                                       ]).multiply( 0.5 * ( 1 - Math.SQRT1_2 ) ).add(this.offset.multiply( Math.SQRT1_2 ));
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   148
            this.setScale( _newScale, _offset );
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   149
        },
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   150
        zoomIn: function() {
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   151
            var _newScale = this.scale * Math.SQRT2,
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   152
            _offset = new paper.Point([
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   153
                                       this.renderer.canvas_$.width(),
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   154
                                       this.renderer.canvas_$.height()
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   155
                                       ]).multiply( 0.5 * ( 1 - Math.SQRT2 ) ).add(this.offset.multiply( Math.SQRT2 ));
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   156
            this.setScale( _newScale, _offset );
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   157
        },
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   158
        resizeZoom: function(_scaleWidth, _scaleHeight, _ratio) {
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   159
            var _newScale = this.scale * _ratio,
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   160
                _offset = new paper.Point([
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   161
                                       (this.offset.x * _scaleWidth),
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   162
                                       (this.offset.y * _scaleHeight)
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   163
                                       ]);
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   164
            this.setScale( _newScale, _offset );
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   165
        },
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   166
        autoScale: function(force_view) {
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   167
            var nodes = this.renkan.project.get("nodes");
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   168
            if (nodes.length > 1) {
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   169
                var _xx = nodes.map(function(_node) { return _node.get("position").x; }),
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   170
                _yy = nodes.map(function(_node) { return _node.get("position").y; }),
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   171
                _minx = Math.min.apply(Math, _xx),
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   172
                _miny = Math.min.apply(Math, _yy),
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   173
                _maxx = Math.max.apply(Math, _xx),
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   174
                _maxy = Math.max.apply(Math, _yy);
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   175
                var _scale = Math.min( (paper.view.size.width - 2 * this.renkan.options.autoscale_padding) / (_maxx - _minx), (paper.view.size.height - 2 * this.renkan.options.autoscale_padding) / (_maxy - _miny));
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   176
                this.initialScale = _scale;
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   177
                // Override calculated scale if asked
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   178
                if((typeof force_view !== "undefined") && parseFloat(force_view.zoom_level)>0 && parseFloat(force_view.offset.x)>0 && parseFloat(force_view.offset.y)>0){
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   179
                    this.setScale(parseFloat(force_view.zoom_level), new paper.Point(parseFloat(force_view.offset.x), parseFloat(force_view.offset.y)));
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   180
                }
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   181
                else{
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   182
                    this.setScale(_scale, paper.view.center.subtract(new paper.Point([(_maxx + _minx) / 2, (_maxy + _miny) / 2]).multiply(_scale)));
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   183
                }
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   184
            }
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   185
            if (nodes.length === 1) {
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   186
                this.setScale(1, paper.view.center.subtract(new paper.Point([nodes.at(0).get("position").x, nodes.at(0).get("position").y])));
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   187
            }
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   188
        },
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   189
        paperShift: function(_delta) {
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   190
            this.offset = this.offset.add(_delta);
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   191
            this.renderer.redraw();
dd526b1b283a add shift and zoom functions to the viewRepr
rougeronj
parents: 506
diff changeset
   192
        },
511
4a48a1a9fd1e update the url on papershift or zoom
rougeronj
parents: 510
diff changeset
   193
        updateUrl: function(){
4a48a1a9fd1e update the url on papershift or zoom
rougeronj
parents: 510
diff changeset
   194
            if(this.options.update_url){
4a48a1a9fd1e update the url on papershift or zoom
rougeronj
parents: 510
diff changeset
   195
                var result = {};
4a48a1a9fd1e update the url on papershift or zoom
rougeronj
parents: 510
diff changeset
   196
                var parameters = Backbone.history.getFragment().split('?');
4a48a1a9fd1e update the url on papershift or zoom
rougeronj
parents: 510
diff changeset
   197
                if (parameters.length > 1){
4a48a1a9fd1e update the url on papershift or zoom
rougeronj
parents: 510
diff changeset
   198
                    parameters[1].split("&").forEach(function(part) {
4a48a1a9fd1e update the url on papershift or zoom
rougeronj
parents: 510
diff changeset
   199
                        var item = part.split("=");
4a48a1a9fd1e update the url on papershift or zoom
rougeronj
parents: 510
diff changeset
   200
                        result[item[0]] = decodeURIComponent(item[1]);
4a48a1a9fd1e update the url on papershift or zoom
rougeronj
parents: 510
diff changeset
   201
                    });
519
b94a34c139c1 update parameters function to accept idView parameter
rougeronj
parents: 517
diff changeset
   202
                }
511
4a48a1a9fd1e update the url on papershift or zoom
rougeronj
parents: 510
diff changeset
   203
                result.view = Math.round(this.offset.x*1000)/1000 + ',' + Math.round(this.offset.y*1000)/1000 + ',' + Math.round(this.scale*1000)/1000;
519
b94a34c139c1 update parameters function to accept idView parameter
rougeronj
parents: 517
diff changeset
   204
b94a34c139c1 update parameters function to accept idView parameter
rougeronj
parents: 517
diff changeset
   205
                if (this.renkan.project.get("views").indexOf(this.model) > -1){
b94a34c139c1 update parameters function to accept idView parameter
rougeronj
parents: 517
diff changeset
   206
                    result.idView = this.renkan.project.get("views").indexOf(this.model);
524
904effa4b6d7 improve view management:
rougeronj
parents: 521
diff changeset
   207
                    if (result.idView === this.renkan.project.get("views").length - 1){
904effa4b6d7 improve view management:
rougeronj
parents: 521
diff changeset
   208
                        result.idView = -1;
904effa4b6d7 improve view management:
rougeronj
parents: 521
diff changeset
   209
                    }
519
b94a34c139c1 update parameters function to accept idView parameter
rougeronj
parents: 517
diff changeset
   210
                } else {
b94a34c139c1 update parameters function to accept idView parameter
rougeronj
parents: 517
diff changeset
   211
                    if (result.idView){
b94a34c139c1 update parameters function to accept idView parameter
rougeronj
parents: 517
diff changeset
   212
                        delete result.idView;
b94a34c139c1 update parameters function to accept idView parameter
rougeronj
parents: 517
diff changeset
   213
                    }
b94a34c139c1 update parameters function to accept idView parameter
rougeronj
parents: 517
diff changeset
   214
                }
517
15061185cf1b prevent history to be change with the new hash parameters
rougeronj
parents: 511
diff changeset
   215
                this.renkan.router.navigate("?" + decodeURIComponent($.param(result)), {trigger: false, replace: true});
511
4a48a1a9fd1e update the url on papershift or zoom
rougeronj
parents: 510
diff changeset
   216
            }
4a48a1a9fd1e update the url on papershift or zoom
rougeronj
parents: 510
diff changeset
   217
        },
524
904effa4b6d7 improve view management:
rougeronj
parents: 521
diff changeset
   218
        destroy: function(_event) {
904effa4b6d7 improve view management:
rougeronj
parents: 521
diff changeset
   219
            this._super("destroy");
904effa4b6d7 improve view management:
rougeronj
parents: 521
diff changeset
   220
            this.showNodes(false);
904effa4b6d7 improve view management:
rougeronj
parents: 521
diff changeset
   221
        }
506
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   222
    }).value();
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   223
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   224
    return ViewRepr;
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   225
460de050f800 Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff changeset
   226
});