| author | rougeronj |
| Thu, 25 Jun 2015 18:09:49 +0200 | |
| changeset 510 | a8f02d66bf02 |
| parent 508 | dd526b1b283a |
| child 511 | 4a48a1a9fd1e |
| permissions | -rw-r--r-- |
|
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 | 18 |
this.scale = 1; |
19 |
this.initialScale = 1; |
|
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 | 33 |
var bindClick = function(selector, fname) { |
34 |
_this.$.find(selector).click(function(evt) { |
|
35 |
_this[fname](evt); |
|
36 |
return false; |
|
37 |
}); |
|
38 |
}; |
|
39 |
|
|
40 |
bindClick(".Rk-ZoomOut", "zoomOut"); |
|
41 |
bindClick(".Rk-ZoomIn", "zoomIn"); |
|
42 |
bindClick(".Rk-ZoomFit", "autoScale"); |
|
43 |
|
|
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 |
}; |
|
a8f02d66bf02
adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents:
508
diff
changeset
|
49 |
//TODO: make the if else the same function |
|
a8f02d66bf02
adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents:
508
diff
changeset
|
50 |
if (_this.model){ |
|
a8f02d66bf02
adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents:
508
diff
changeset
|
51 |
_this.model.set( { zoom_level:_this.scale, offset:offset, hidden_nodes: _this.hiddenNodes.concat() } ); |
|
a8f02d66bf02
adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents:
508
diff
changeset
|
52 |
}else{ |
|
a8f02d66bf02
adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents:
508
diff
changeset
|
53 |
_this.model = _this.renkan.project.addView( { zoom_level:_this.scale, offset:offset, hidden_nodes: _this.hiddenNodes.concat() } ); |
|
a8f02d66bf02
adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents:
508
diff
changeset
|
54 |
} |
|
a8f02d66bf02
adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents:
508
diff
changeset
|
55 |
_this.params = { |
|
a8f02d66bf02
adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents:
508
diff
changeset
|
56 |
"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
|
57 |
"offset": _this.model.get("offset"), |
|
a8f02d66bf02
adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents:
508
diff
changeset
|
58 |
"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
|
59 |
}; |
| 508 | 60 |
}); |
61 |
|
|
62 |
this.$.find(".Rk-ZoomSetSaved").click( function() { |
|
|
510
a8f02d66bf02
adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents:
508
diff
changeset
|
63 |
_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
|
64 |
_this.showNodes(false); |
|
a8f02d66bf02
adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents:
508
diff
changeset
|
65 |
if (_this.options.hide_nodes){ |
|
a8f02d66bf02
adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents:
508
diff
changeset
|
66 |
_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
|
67 |
_this.hideNodes(); |
|
506
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
68 |
} |
| 508 | 69 |
}); |
|
506
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
70 |
|
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
71 |
this.$.find(".Rk-ShowHiddenNodes").mouseenter( function() { |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
72 |
_this.showNodes(true); |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
73 |
_this.$.find(".Rk-ShowHiddenNodes").mouseleave( function() { |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
74 |
_this.hideNodes(); |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
75 |
}); |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
76 |
}); |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
77 |
this.$.find(".Rk-ShowHiddenNodes").click( function() { |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
78 |
_this.showNodes(false); |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
79 |
_this.$.find(".Rk-ShowHiddenNodes").off( "mouseleave" ); |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
80 |
}); |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
81 |
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
|
82 |
this.$.find(".Rk-ZoomSetSaved").show(); |
|
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 |
}, |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
85 |
redraw: function(options) { |
|
510
a8f02d66bf02
adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents:
508
diff
changeset
|
86 |
//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
|
87 |
}, |
|
a8f02d66bf02
adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents:
508
diff
changeset
|
88 |
init: function(){ |
|
a8f02d66bf02
adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents:
508
diff
changeset
|
89 |
var _this = this; |
|
a8f02d66bf02
adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents:
508
diff
changeset
|
90 |
_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
|
91 |
_this.showNodes(false); |
|
a8f02d66bf02
adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents:
508
diff
changeset
|
92 |
if (_this.options.hide_nodes){ |
|
a8f02d66bf02
adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents:
508
diff
changeset
|
93 |
_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
|
94 |
_this.hideNodes(); |
|
a8f02d66bf02
adapt the viewRepr to support a temp View (a representation without a model)
rougeronj
parents:
508
diff
changeset
|
95 |
} |
|
506
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
96 |
}, |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
97 |
addHiddenNode: function(_model){ |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
98 |
this.hideNode(_model); |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
99 |
this.hiddenNodes.push(_model.id); |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
100 |
}, |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
101 |
hideNode: function(_model){ |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
102 |
if (typeof this.renderer.getRepresentationByModel(_model) !== 'undefined'){ |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
103 |
this.renderer.getRepresentationByModel(_model).hide(); |
|
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 |
}, |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
106 |
hideNodes: function(){ |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
107 |
var _this = this; |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
108 |
this.hiddenNodes.forEach(function(_id, index){ |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
109 |
var node = _this.renkan.project.get("nodes").get(_id); |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
110 |
if (typeof node !== 'undefined'){ |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
111 |
return _this.hideNode(_this.renkan.project.get("nodes").get(_id)); |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
112 |
}else{ |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
113 |
_this.hiddenNodes.splice(index, 1); |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
114 |
} |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
115 |
}); |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
116 |
paper.view.draw(); |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
117 |
}, |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
118 |
showNodes: function(ghost){ |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
119 |
var _this = this; |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
120 |
this.hiddenNodes.forEach(function(_id){ |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
121 |
_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
|
122 |
}); |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
123 |
if (!ghost){ |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
124 |
this.hiddenNodes = []; |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
125 |
} |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
126 |
paper.view.draw(); |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
127 |
}, |
| 508 | 128 |
setScale: function(_newScale, _offset) { |
129 |
if ((_newScale/this.initialScale) > Utils._MIN_SCALE && (_newScale/this.initialScale) < Utils._MAX_SCALE) { |
|
130 |
this.scale = _newScale; |
|
131 |
if (_offset) { |
|
132 |
this.offset = _offset; |
|
133 |
} |
|
134 |
this.renderer.redraw(); |
|
135 |
} |
|
136 |
}, |
|
137 |
zoomOut: function() { |
|
138 |
var _newScale = this.scale * Math.SQRT1_2, |
|
139 |
_offset = new paper.Point([ |
|
140 |
this.renderer.canvas_$.width(), |
|
141 |
this.renderer.canvas_$.height() |
|
142 |
]).multiply( 0.5 * ( 1 - Math.SQRT1_2 ) ).add(this.offset.multiply( Math.SQRT1_2 )); |
|
143 |
this.setScale( _newScale, _offset ); |
|
144 |
}, |
|
145 |
zoomIn: function() { |
|
146 |
var _newScale = this.scale * Math.SQRT2, |
|
147 |
_offset = new paper.Point([ |
|
148 |
this.renderer.canvas_$.width(), |
|
149 |
this.renderer.canvas_$.height() |
|
150 |
]).multiply( 0.5 * ( 1 - Math.SQRT2 ) ).add(this.offset.multiply( Math.SQRT2 )); |
|
151 |
this.setScale( _newScale, _offset ); |
|
152 |
}, |
|
153 |
resizeZoom: function(_scaleWidth, _scaleHeight, _ratio) { |
|
154 |
var _newScale = this.scale * _ratio, |
|
155 |
_offset = new paper.Point([ |
|
156 |
(this.offset.x * _scaleWidth), |
|
157 |
(this.offset.y * _scaleHeight) |
|
158 |
]); |
|
159 |
this.setScale( _newScale, _offset ); |
|
160 |
}, |
|
161 |
autoScale: function(force_view) { |
|
162 |
var nodes = this.renkan.project.get("nodes"); |
|
163 |
if (nodes.length > 1) { |
|
164 |
var _xx = nodes.map(function(_node) { return _node.get("position").x; }), |
|
165 |
_yy = nodes.map(function(_node) { return _node.get("position").y; }), |
|
166 |
_minx = Math.min.apply(Math, _xx), |
|
167 |
_miny = Math.min.apply(Math, _yy), |
|
168 |
_maxx = Math.max.apply(Math, _xx), |
|
169 |
_maxy = Math.max.apply(Math, _yy); |
|
170 |
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)); |
|
171 |
this.initialScale = _scale; |
|
172 |
// Override calculated scale if asked |
|
173 |
if((typeof force_view !== "undefined") && parseFloat(force_view.zoom_level)>0 && parseFloat(force_view.offset.x)>0 && parseFloat(force_view.offset.y)>0){ |
|
174 |
this.setScale(parseFloat(force_view.zoom_level), new paper.Point(parseFloat(force_view.offset.x), parseFloat(force_view.offset.y))); |
|
175 |
} |
|
176 |
else{ |
|
177 |
this.setScale(_scale, paper.view.center.subtract(new paper.Point([(_maxx + _minx) / 2, (_maxy + _miny) / 2]).multiply(_scale))); |
|
178 |
} |
|
179 |
} |
|
180 |
if (nodes.length === 1) { |
|
181 |
this.setScale(1, paper.view.center.subtract(new paper.Point([nodes.at(0).get("position").x, nodes.at(0).get("position").y]))); |
|
182 |
} |
|
183 |
}, |
|
184 |
paperShift: function(_delta) { |
|
185 |
this.offset = this.offset.add(_delta); |
|
186 |
this.renderer.offset = this.renderer.offset.add(_delta); |
|
187 |
this.renderer.redraw(); |
|
188 |
}, |
|
|
506
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
189 |
}).value(); |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
190 |
|
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
191 |
return ViewRepr; |
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
192 |
|
|
460de050f800
Create Viewrepr which handle the representation of the View.
rougeronj
parents:
diff
changeset
|
193 |
}); |