improve view management:
authorrougeronj
Wed, 16 Sep 2015 17:36:46 +0200
changeset 524 904effa4b6d7
parent 523 dc04aa440c11
child 525 03aa989092bb
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
client/js/defaults.js
client/js/renderer/scene.js
client/js/renderer/viewrepr.js
--- a/client/js/defaults.js	Tue Sep 15 17:09:42 2015 +0200
+++ b/client/js/defaults.js	Wed Sep 16 17:36:46 2015 +0200
@@ -49,7 +49,8 @@
         /* show buttons to save view */
     default_view: false,
         /* Allows to load default view (zoom+offset) at start on read_only mode, instead of autoScale. the default_view will be the last */
-
+    default_index_view: -1,
+    
     /* URL parsing */
     update_url:true,
         /* update the url each time the paper shift or on zoom in/out, with the serialized view (offset and scale) */
--- a/client/js/renderer/scene.js	Tue Sep 15 17:09:42 2015 +0200
+++ b/client/js/renderer/scene.js	Wed Sep 16 17:36:46 2015 +0200
@@ -1201,13 +1201,19 @@
 
         },
         parameters: function(_params){
+            this.removeRepresentationsOfType("View");
             if ($.isEmptyObject(_params)){
-                this.view = this.addRepresentation("View", this.renkan.project.get("views").first());
-                this.view.autoScale();
+                this.view = this.addRepresentation("View", this.renkan.project.get("views").at(this.validViewIndex(this.renkan.options.default_index_view)));
+                if (!this.renkan.options.default_view){
+                    this.view.autoScale();
+                }
                 return;
             }
-            if (typeof _params.idView !== 'undefined' && typeof this.renkan.project.get("views").at(_params.idView) !== 'undefined'){
-                this.view = this.addRepresentation("View", this.renkan.project.get("views").at(_params.idView));
+            if (typeof _params.idView !== 'undefined'){
+                this.view = this.addRepresentation("View", this.renkan.project.get("views").at(this.validViewIndex(_params.idView)));
+                if (!this.renkan.options.default_view){
+                    this.view.autoScale();
+                }
             }
             if (typeof _params.view !== 'undefined' && _params.view.split(",").length >= 3){
                 var viewParams = _params.view.split(",");
@@ -1227,8 +1233,8 @@
                     this.view.init();                    
                 }
             }
-            if(!this.view){
-                this.view = this.addRepresentation("View", this.renkan.project.get("views").first());
+            if (!this.view){
+                this.view = this.addRepresentation("View", this.renkan.project.get("views").at(this.validViewIndex(this.renkan.options.default_index_view)));
                 this.view.autoScale();
             }
             //other parameters must go after because most of them depends on a view that must be initialize before
@@ -1237,6 +1243,20 @@
                 this.highlightModel(this.renkan.project.get("nodes").get(_params.idNode));                 
             }
         },
+        validViewIndex: function(index){
+            //check if the view index exist (negative index is from the end) and return the correct index or false if doesn't exist
+            var _index = parseInt(index);
+            var validIndex = 0;
+            if (_index < 0){
+                validIndex = this.renkan.project.get("views").length + _index;
+            } else {
+                validIndex = _index; 
+            }
+            if (typeof this.renkan.project.get("views").at(_index) === 'undefined'){
+                validIndex = 0;
+            }
+            return validIndex;
+        },
         foldBins: function() {
             var foldBinsButton = this.$.find(".Rk-Fold-Bins"),
                 bins = this.renkan.$.find(".Rk-Bins");
--- a/client/js/renderer/viewrepr.js	Tue Sep 15 17:09:42 2015 +0200
+++ b/client/js/renderer/viewrepr.js	Wed Sep 16 17:36:46 2015 +0200
@@ -46,12 +46,7 @@
                     "x": _this.offset.x,
                     "y": _this.offset.y
                 };
-                //TODO: make the if else the same function
-                if (_this.model && _this.renkan.project.get("views").indexOf(_this.model) > 0){
-                    _this.model.set( { zoom_level:_this.scale, offset:offset, hidden_nodes: _this.hiddenNodes.concat() } );
-                }else{
-                    _this.model = _this.renkan.project.addView( { zoom_level:_this.scale, offset:offset, hidden_nodes: _this.hiddenNodes.concat() } );
-                }
+                _this.model = _this.renkan.project.addView( { zoom_level:_this.scale, offset:offset, hidden_nodes: _this.hiddenNodes.concat() } );
                 _this.params = {
                         "zoom_level": _this.model.get("zoom_level"),
                         "offset": _this.model.get("offset"),
@@ -61,12 +56,19 @@
             });
             
             this.$.find(".Rk-ZoomSetSaved").click( function() {
+                _this.model = _this.renkan.project.get("views").at(_this.renkan.project.get("views").length -1);
+                _this.params = {
+                        "zoom_level": _this.model.get("zoom_level"),
+                        "offset": _this.model.get("offset"),
+                        "hidden_nodes": _this.model.get("hidden_nodes")
+                };
                 _this.setScale(_this.params.zoom_level, new paper.Point(_this.params.offset));
                 _this.showNodes(false);
                 if (_this.options.hide_nodes){
                     _this.hiddenNodes = (_this.params.hidden_nodes || []).concat();
                     _this.hideNodes();
                 }
+                _this.updateUrl();
             });
             
             this.$.find(".Rk-ShowHiddenNodes").mouseenter( function() {
@@ -89,7 +91,7 @@
         init: function(){
             var _this = this;
             _this.setScale(_this.params.zoom_level, new paper.Point(_this.params.offset));
-            _this.showNodes(false);
+            
             if (_this.options.hide_nodes){
                 _this.hiddenNodes = (_this.params.hidden_nodes || []).concat();
                 _this.hideNodes();
@@ -202,6 +204,9 @@
 
                 if (this.renkan.project.get("views").indexOf(this.model) > -1){
                     result.idView = this.renkan.project.get("views").indexOf(this.model);
+                    if (result.idView === this.renkan.project.get("views").length - 1){
+                        result.idView = -1;
+                    }
                 } else {
                     if (result.idView){
                         delete result.idView;
@@ -210,6 +215,10 @@
                 this.renkan.router.navigate("?" + decodeURIComponent($.param(result)), {trigger: false, replace: true});
             }
         },
+        destroy: function(_event) {
+            this._super("destroy");
+            this.showNodes(false);
+        }
     }).value();
 
     return ViewRepr;