162 |
162 |
163 Rkns.Renderer.Node = Rkns.Utils.inherit(Rkns.Renderer._BaseController); |
163 Rkns.Renderer.Node = Rkns.Utils.inherit(Rkns.Renderer._BaseController); |
164 |
164 |
165 Rkns.Renderer.Node.prototype._init = function() { |
165 Rkns.Renderer.Node.prototype._init = function() { |
166 this._renderer.node_layer.activate(); |
166 this._renderer.node_layer.activate(); |
167 this.type = "node"; |
167 this.type = "Node"; |
168 this.circle = new paper.Path.Circle([0, 0], Rkns._NODE_RADIUS); |
168 this.circle = new paper.Path.Circle([0, 0], Rkns._NODE_RADIUS); |
169 this.circle.fillColor = '#ffffff'; |
169 this.circle.fillColor = '#ffffff'; |
170 this.circle.__controller = this; |
170 this.circle.__controller = this; |
171 this.title = new paper.PointText([0,0]); |
171 this.title = new paper.PointText([0,0]); |
172 this.title.characterStyle = { |
172 this.title.characterStyle = { |
247 |
247 |
248 Rkns.Renderer.Edge = Rkns.Utils.inherit(Rkns.Renderer._BaseController); |
248 Rkns.Renderer.Edge = Rkns.Utils.inherit(Rkns.Renderer._BaseController); |
249 |
249 |
250 Rkns.Renderer.Edge.prototype._init = function() { |
250 Rkns.Renderer.Edge.prototype._init = function() { |
251 this._renderer.edge_layer.activate(); |
251 this._renderer.edge_layer.activate(); |
252 this.type = "edge"; |
252 this.type = "Edge"; |
253 this.from_controller = this._element.from.__controller; |
253 this.from_controller = this._element.from.__controller; |
254 this.to_node_controller = this._element.to.__controller; |
254 this.to_node_controller = this._element.to.__controller; |
255 this.line = new paper.Path(); |
255 this.line = new paper.Path(); |
256 this.line.add([0,0],[0,0]); |
256 this.line.add([0,0],[0,0]); |
257 this.line.__controller = this; |
257 this.line.__controller = this; |
386 |
386 |
387 Rkns.Renderer.TempEdge.prototype.mouseup = function(_event) { |
387 Rkns.Renderer.TempEdge.prototype.mouseup = function(_event) { |
388 var _hitResult = paper.project.hitTest(_event.point); |
388 var _hitResult = paper.project.hitTest(_event.point); |
389 if (_hitResult && typeof _hitResult.item.__controller !== "undefined") { |
389 if (_hitResult && typeof _hitResult.item.__controller !== "undefined") { |
390 var _target = _hitResult.item.__controller; |
390 var _target = _hitResult.item.__controller; |
391 if (_target.type === "node" && this.from_controller._element.id !== _target._element.id) { |
391 if (_target.type === "Node" && this.from_controller._element.id !== _target._element.id) { |
392 this._project.addEdge({ |
392 this._project.addEdge({ |
393 from: this.from_controller._element.id, |
393 from: this.from_controller._element.id, |
394 to: _target._element.id |
394 to: _target._element.id |
395 }, Rkns._RENDER_AND_SAVE) |
395 }, Rkns._RENDER_AND_SAVE) |
396 } |
396 } |
454 var _data = { |
454 var _data = { |
455 title: _this.editor_$.find(".Rk-Edit-Title").val(), |
455 title: _this.editor_$.find(".Rk-Edit-Title").val(), |
456 description: _this.editor_$.find(".Rk-Edit-Description").val(), |
456 description: _this.editor_$.find(".Rk-Edit-Description").val(), |
457 uri: _this.editor_$.find(".Rk-Edit-URI").val() |
457 uri: _this.editor_$.find(".Rk-Edit-URI").val() |
458 } |
458 } |
459 _this._project.updateElement( |
459 _this._project.updateNode( |
460 _element, |
460 _element, |
461 _data, |
461 _data, |
462 Rkns._SAVE |
462 Rkns._SAVE |
463 ); |
463 ); |
464 _this.node_controller.redraw(); |
464 _this.node_controller.redraw(); |
523 this.editor_$.find("input, textarea").bind("keyup change", function() { |
523 this.editor_$.find("input, textarea").bind("keyup change", function() { |
524 var _data = { |
524 var _data = { |
525 title: _this.editor_$.find(".Rk-Edit-Title").val(), |
525 title: _this.editor_$.find(".Rk-Edit-Title").val(), |
526 uri: _this.editor_$.find(".Rk-Edit-URI").val() |
526 uri: _this.editor_$.find(".Rk-Edit-URI").val() |
527 } |
527 } |
528 _this._project.updateElement( |
528 _this._project.updateEdge( |
529 _element, |
529 _element, |
530 _data, |
530 _data, |
531 Rkns._SAVE |
531 Rkns._SAVE |
532 ); |
532 ); |
533 _this.edge_controller.redraw(); |
533 _this.edge_controller.redraw(); |
544 |
544 |
545 Rkns.Renderer.NodeEditButton = Rkns.Utils.inherit(Rkns.Renderer._BaseController); |
545 Rkns.Renderer.NodeEditButton = Rkns.Utils.inherit(Rkns.Renderer._BaseController); |
546 |
546 |
547 Rkns.Renderer.NodeEditButton.prototype._init = function() { |
547 Rkns.Renderer.NodeEditButton.prototype._init = function() { |
548 this._renderer.node_layer.activate(); |
548 this._renderer.node_layer.activate(); |
549 this.type = "node-edit-button"; |
549 this.type = "Node-edit-button"; |
550 this.sector = Rkns.Renderer.Utils.sector(this, 1 + Rkns._NODE_RADIUS, 3 * Rkns._NODE_RADIUS, - 90, 30, 2, 'img/edit.png'); |
550 this.sector = Rkns.Renderer.Utils.sector(this, 1 + Rkns._NODE_RADIUS, 3 * Rkns._NODE_RADIUS, - 90, 30, 2, 'img/edit.png'); |
551 } |
551 } |
552 |
552 |
553 Rkns.Renderer.NodeEditButton.prototype.moveTo = function(_pos) { |
553 Rkns.Renderer.NodeEditButton.prototype.moveTo = function(_pos) { |
554 this.sector.moveTo(_pos); |
554 this.sector.moveTo(_pos); |
587 |
587 |
588 Rkns.Renderer.NodeRemoveButton = Rkns.Utils.inherit(Rkns.Renderer._BaseController); |
588 Rkns.Renderer.NodeRemoveButton = Rkns.Utils.inherit(Rkns.Renderer._BaseController); |
589 |
589 |
590 Rkns.Renderer.NodeRemoveButton.prototype._init = function() { |
590 Rkns.Renderer.NodeRemoveButton.prototype._init = function() { |
591 this._renderer.node_layer.activate(); |
591 this._renderer.node_layer.activate(); |
592 this.type = "node-remove-button"; |
592 this.type = "Node-remove-button"; |
593 this.sector = Rkns.Renderer.Utils.sector(this, 1 + Rkns._NODE_RADIUS, 3 * Rkns._NODE_RADIUS, -210, -90, 2, 'img/remove.png'); |
593 this.sector = Rkns.Renderer.Utils.sector(this, 1 + Rkns._NODE_RADIUS, 3 * Rkns._NODE_RADIUS, -210, -90, 2, 'img/remove.png'); |
594 } |
594 } |
595 |
595 |
596 Rkns.Renderer.NodeRemoveButton.prototype.moveTo = function(_pos) { |
596 Rkns.Renderer.NodeRemoveButton.prototype.moveTo = function(_pos) { |
597 this.sector.moveTo(_pos); |
597 this.sector.moveTo(_pos); |
630 |
630 |
631 Rkns.Renderer.NodeLinkButton = Rkns.Utils.inherit(Rkns.Renderer._BaseController); |
631 Rkns.Renderer.NodeLinkButton = Rkns.Utils.inherit(Rkns.Renderer._BaseController); |
632 |
632 |
633 Rkns.Renderer.NodeLinkButton.prototype._init = function() { |
633 Rkns.Renderer.NodeLinkButton.prototype._init = function() { |
634 this._renderer.node_layer.activate(); |
634 this._renderer.node_layer.activate(); |
635 this.type = "node-link-button"; |
635 this.type = "Node-link-button"; |
636 this.sector = Rkns.Renderer.Utils.sector(this, 1 + Rkns._NODE_RADIUS , 3 * Rkns._NODE_RADIUS, 30, 150, 2, 'img/link.png'); |
636 this.sector = Rkns.Renderer.Utils.sector(this, 1 + Rkns._NODE_RADIUS , 3 * Rkns._NODE_RADIUS, 30, 150, 2, 'img/link.png'); |
637 } |
637 } |
638 |
638 |
639 Rkns.Renderer.NodeLinkButton.prototype.moveTo = function(_pos) { |
639 Rkns.Renderer.NodeLinkButton.prototype.moveTo = function(_pos) { |
640 this.sector.moveTo(_pos); |
640 this.sector.moveTo(_pos); |
667 |
667 |
668 Rkns.Renderer.EdgeEditButton = Rkns.Utils.inherit(Rkns.Renderer._BaseController); |
668 Rkns.Renderer.EdgeEditButton = Rkns.Utils.inherit(Rkns.Renderer._BaseController); |
669 |
669 |
670 Rkns.Renderer.EdgeEditButton.prototype._init = function() { |
670 Rkns.Renderer.EdgeEditButton.prototype._init = function() { |
671 this._renderer.edge_layer.activate(); |
671 this._renderer.edge_layer.activate(); |
672 this.type = "edge-edit-button"; |
672 this.type = "Edge-edit-button"; |
673 this.sector = Rkns.Renderer.Utils.sector(this, 5 , 2 * Rkns._NODE_RADIUS, - 60, 60, 2, 'img/edit.png'); |
673 this.sector = Rkns.Renderer.Utils.sector(this, 5 , 2 * Rkns._NODE_RADIUS, - 60, 60, 2, 'img/edit.png'); |
674 } |
674 } |
675 |
675 |
676 Rkns.Renderer.EdgeEditButton.prototype.moveTo = function(_pos) { |
676 Rkns.Renderer.EdgeEditButton.prototype.moveTo = function(_pos) { |
677 this.sector.moveTo(_pos); |
677 this.sector.moveTo(_pos); |
709 |
709 |
710 Rkns.Renderer.EdgeRemoveButton = Rkns.Utils.inherit(Rkns.Renderer._BaseController); |
710 Rkns.Renderer.EdgeRemoveButton = Rkns.Utils.inherit(Rkns.Renderer._BaseController); |
711 |
711 |
712 Rkns.Renderer.EdgeRemoveButton.prototype._init = function() { |
712 Rkns.Renderer.EdgeRemoveButton.prototype._init = function() { |
713 this._renderer.edge_layer.activate(); |
713 this._renderer.edge_layer.activate(); |
714 this.type = "edge-remove-button"; |
714 this.type = "Edge-remove-button"; |
715 this.sector = Rkns.Renderer.Utils.sector(this, 5, 2 * Rkns._NODE_RADIUS, - 240, -120, 2, 'img/remove.png'); |
715 this.sector = Rkns.Renderer.Utils.sector(this, 5, 2 * Rkns._NODE_RADIUS, - 240, -120, 2, 'img/remove.png'); |
716 } |
716 } |
717 Rkns.Renderer.EdgeRemoveButton.prototype.moveTo = function(_pos) { |
717 Rkns.Renderer.EdgeRemoveButton.prototype.moveTo = function(_pos) { |
718 this.sector.moveTo(_pos); |
718 this.sector.moveTo(_pos); |
719 } |
719 } |
750 |
750 |
751 Rkns.Renderer.Scene = function(_project) { |
751 Rkns.Renderer.Scene = function(_project) { |
752 this._project = _project; |
752 this._project = _project; |
753 this._MARGIN_X = 80; |
753 this._MARGIN_X = 80; |
754 this._MARGIN_Y = 50; |
754 this._MARGIN_Y = 50; |
755 var _canvas_id = this._project._opts.canvas_id; |
755 this.$ = Rkns.$(".Rk-Render"); |
756 this.$ = Rkns.$("#"+_canvas_id); |
756 this.$.html(this.template({ |
757 this.editor_$ = Rkns.$(".Rk-Editor"); |
|
758 this.editor_$.html(this.editorTemplate({ |
|
759 l10n: this._project.l10n |
757 l10n: this._project.l10n |
760 })); |
758 })) |
761 paper.setup(document.getElementById(_canvas_id)); |
759 this.canvas_$ = this.$.find(".Rk-Canvas"); |
|
760 this.editor_$ = this.$.find(".Rk-Editor"); |
|
761 paper.setup(this.canvas_$[0]); |
762 this.scale = 1; |
762 this.scale = 1; |
763 this.offset = paper.view.center; |
763 this.offset = paper.view.center; |
764 this.totalScroll = 0; |
764 this.totalScroll = 0; |
765 this.click_target = null; |
765 this.click_target = null; |
766 this.selected_target = null; |
766 this.selected_target = null; |
780 _this.onMouseDrag(_event); |
780 _this.onMouseDrag(_event); |
781 } |
781 } |
782 _tool.onMouseUp = function(_event) { |
782 _tool.onMouseUp = function(_event) { |
783 _this.onMouseUp(_event); |
783 _this.onMouseUp(_event); |
784 } |
784 } |
785 this.$.mousewheel(function(_event, _delta) { |
785 this.canvas_$.mousewheel(function(_event, _delta) { |
786 _this.onScroll(_event, _delta); |
786 _this.onScroll(_event, _delta); |
787 }) |
787 }) |
788 this.$.dblclick(function(_event) { |
788 this.canvas_$.dblclick(function(_event) { |
789 _this.onDoubleClick(_event); |
789 _this.onDoubleClick(_event); |
790 }); |
790 }); |
791 this.editor_$.find(".Rk-ZoomOut").click(function() { |
791 this.editor_$.find(".Rk-ZoomOut").click(function() { |
792 _this.offset = new paper.Point([ |
792 _this.offset = new paper.Point([ |
793 _this.$.width(), |
793 _this.canvas_$.width(), |
794 _this.$.height() |
794 _this.canvas_$.height() |
795 ]).multiply( .5 * ( 1 - Math.SQRT1_2 ) ).add(_this.offset.multiply( Math.SQRT1_2 )); |
795 ]).multiply( .5 * ( 1 - Math.SQRT1_2 ) ).add(_this.offset.multiply( Math.SQRT1_2 )); |
796 _this.scale *= Math.SQRT1_2; |
796 _this.scale *= Math.SQRT1_2; |
797 _this.redraw(); |
797 _this.redraw(); |
798 }); |
798 }); |
799 this.editor_$.find(".Rk-ZoomIn").click(function() { |
799 this.editor_$.find(".Rk-ZoomIn").click(function() { |
800 _this.offset = new paper.Point([ |
800 _this.offset = new paper.Point([ |
801 _this.$.width(), |
801 _this.canvas_$.width(), |
802 _this.$.height() |
802 _this.canvas_$.height() |
803 ]).multiply( .5 * ( 1 - Math.SQRT2 ) ).add(_this.offset.multiply( Math.SQRT2 )); |
803 ]).multiply( .5 * ( 1 - Math.SQRT2 ) ).add(_this.offset.multiply( Math.SQRT2 )); |
804 _this.scale *= Math.SQRT2; |
804 _this.scale *= Math.SQRT2; |
805 _this.redraw(); |
805 _this.redraw(); |
806 }); |
806 }); |
807 paper.view.onResize = function(_event) { |
807 paper.view.onResize = function(_event) { |
808 _this.offset = _this.offset.add(_event.delta.divide(2)); |
808 _this.offset = _this.offset.add(_event.delta.divide(2)); |
809 _this.redraw(); |
809 _this.redraw(); |
810 } |
810 } |
811 } |
811 } |
812 |
812 |
813 Rkns.Renderer.Scene.prototype.editorTemplate = Rkns._.template( |
813 Rkns.Renderer.Scene.prototype.template = Rkns._.template( |
814 '<div class="Rk-ZoomButtons"><div class="Rk-ZoomIn" title="<%=l10n.zoom_in%>"></div><div class="Rk-ZoomOut" title="<%=l10n.zoom_out%>"></div></div>' |
814 '<canvas class="Rk-Canvas" resize></canvas><div class="Rk-Editor">' |
|
815 + '<div class="Rk-ZoomButtons"><div class="Rk-ZoomIn" title="<%=l10n.zoom_in%>"></div><div class="Rk-ZoomOut" title="<%=l10n.zoom_out%>"></div></div>' |
|
816 + '</div>' |
815 ); |
817 ); |
816 |
818 |
817 Rkns.Renderer.Scene.prototype.toPaperCoords = function(_point) { |
819 Rkns.Renderer.Scene.prototype.toPaperCoords = function(_point) { |
818 return _point.multiply(this.scale).add(this.offset); |
820 return _point.multiply(this.scale).add(this.offset); |
819 } |
821 } |
831 _miny = Math.min.apply(Math, _yy), |
833 _miny = Math.min.apply(Math, _yy), |
832 _maxx = Math.max.apply(Math, _xx), |
834 _maxx = Math.max.apply(Math, _xx), |
833 _maxy = Math.max.apply(Math, _yy); |
835 _maxy = Math.max.apply(Math, _yy); |
834 this.scale = Math.min((paper.view.size.width - 2 * this._MARGIN_X) / (_maxx - _minx), (paper.view.size.height - 2 * this._MARGIN_Y) / (_maxy - _miny)); |
836 this.scale = Math.min((paper.view.size.width - 2 * this._MARGIN_X) / (_maxx - _minx), (paper.view.size.height - 2 * this._MARGIN_Y) / (_maxy - _miny)); |
835 this.offset = paper.view.center.subtract(new paper.Point([(_maxx + _minx) / 2, (_maxy + _miny) / 2]).multiply(this.scale)); |
837 this.offset = paper.view.center.subtract(new paper.Point([(_maxx + _minx) / 2, (_maxy + _miny) / 2]).multiply(this.scale)); |
836 this.controllers = new Rkns.Model.List(); |
838 this.controllers = new Rkns.ViewModel.List(); |
837 this._project.nodes.forEach(function(_node) { |
839 this._project.nodes.forEach(function(_node) { |
838 _this.addController("Node", _node); |
840 _this.addController("Node", _node); |
839 }); |
841 }); |
840 this._project.edges.forEach(function(_edge) { |
842 this._project.edges.forEach(function(_edge) { |
841 _this.addController("Edge", _edge); |
843 _this.addController("Edge", _edge); |
902 Rkns.Renderer.Scene.prototype.onMouseDown = function(_event) { |
904 Rkns.Renderer.Scene.prototype.onMouseDown = function(_event) { |
903 this.is_dragging = false; |
905 this.is_dragging = false; |
904 var _hitResult = paper.project.hitTest(_event.point); |
906 var _hitResult = paper.project.hitTest(_event.point); |
905 if (_hitResult && typeof _hitResult.item.__controller !== "undefined") { |
907 if (_hitResult && typeof _hitResult.item.__controller !== "undefined") { |
906 this.click_target = _hitResult.item.__controller; |
908 this.click_target = _hitResult.item.__controller; |
907 if (this.click_target.type === "node" && _hitResult.type === "stroke") { |
909 if (this.click_target.type === "Node" && _hitResult.type === "stroke") { |
908 this.addTempEdge(this.click_target, _event.point); |
910 this.addTempEdge(this.click_target, _event.point); |
909 } |
911 } |
910 if (this.click_target.type === "node-link-button") { |
912 if (this.click_target.type === "Node-link-button") { |
911 this.addTempEdge(this.click_target.node_controller, _event.point); |
913 this.addTempEdge(this.click_target.node_controller, _event.point); |
912 } |
914 } |
913 } else { |
915 } else { |
914 this.click_target = null; |
916 this.click_target = null; |
915 } |
917 } |
934 } |
936 } |
935 |
937 |
936 Rkns.Renderer.Scene.prototype.onScroll = function(_event, _scrolldelta) { |
938 Rkns.Renderer.Scene.prototype.onScroll = function(_event, _scrolldelta) { |
937 this.totalScroll += _scrolldelta; |
939 this.totalScroll += _scrolldelta; |
938 if (Math.abs(this.totalScroll) >= 1) { |
940 if (Math.abs(this.totalScroll) >= 1) { |
939 var _off = this.$.offset(), |
941 var _off = this.canvas_$.offset(), |
940 _delta = new paper.Point([ |
942 _delta = new paper.Point([ |
941 _event.pageX - _off.left, |
943 _event.pageX - _off.left, |
942 _event.pageY - _off.top |
944 _event.pageY - _off.top |
943 ]).subtract(this.offset).multiply( Math.SQRT2 - 1 ); |
945 ]).subtract(this.offset).multiply( Math.SQRT2 - 1 ); |
944 if (this.totalScroll > 0) { |
946 if (this.totalScroll > 0) { |
952 this.redraw(); |
954 this.redraw(); |
953 } |
955 } |
954 } |
956 } |
955 |
957 |
956 Rkns.Renderer.Scene.prototype.onDoubleClick = function(_event) { |
958 Rkns.Renderer.Scene.prototype.onDoubleClick = function(_event) { |
957 var _off = this.$.offset(), |
959 var _off = this.canvas_$.offset(), |
958 _point = new paper.Point([ |
960 _point = new paper.Point([ |
959 _event.pageX - _off.left, |
961 _event.pageX - _off.left, |
960 _event.pageY - _off.top |
962 _event.pageY - _off.top |
961 ]); |
963 ]); |
962 var _hitResult = paper.project.hitTest(_point); |
964 var _hitResult = paper.project.hitTest(_point); |