client/js/renkan-publish.js
changeset 37 db991a757015
parent 31 5e37072ec7b7
child 38 092fc99f7558
equal deleted inserted replaced
36:d249d36ecc37 37:db991a757015
   123             id: this.get("_id"),
   123             id: this.get("_id"),
   124             title: this.get("title"),
   124             title: this.get("title"),
   125             uri: this.get("uri"),
   125             uri: this.get("uri"),
   126             description: this.get("description"),
   126             description: this.get("description"),
   127             position: this.get("position"),
   127             position: this.get("position"),
       
   128             image: this.get("image"),
   128             created_by: this.get("created_by").get("_id")
   129             created_by: this.get("created_by").get("_id")
   129         }
   130         }
   130     },
   131     },
   131 });
   132 });
   132 
   133 
   289 
   290 
   290 Rkns.Renderer = {
   291 Rkns.Renderer = {
   291     _MARGIN_X: 80,
   292     _MARGIN_X: 80,
   292     _MARGIN_Y: 50,
   293     _MARGIN_Y: 50,
   293     _MIN_DRAG_DISTANCE: 2,
   294     _MIN_DRAG_DISTANCE: 2,
   294     _NODE_RADIUS: 15,
   295     _NODE_RADIUS: 20,
   295     _NODE_FONT_SIZE: 10,
   296     _NODE_FONT_SIZE: 10,
   296     _EDGE_FONT_SIZE: 9,
   297     _EDGE_FONT_SIZE: 9,
   297     _NODE_MAX_CHAR: 30,
   298     _NODE_MAX_CHAR: 30,
   298     _EDGE_MAX_CHAR: 20,
   299     _EDGE_MAX_CHAR: 20,
   299     _ARROW_LENGTH: 16,
   300     _ARROW_LENGTH: 16,
   406     this.paper_coords = this.renderer.toPaperCoords(this.model_coords);
   407     this.paper_coords = this.renderer.toPaperCoords(this.model_coords);
   407     this.circle.position = this.paper_coords;
   408     this.circle.position = this.paper_coords;
   408     this.title.content = Rkns.Renderer.Utils.shortenText(this.model.get("title"), Rkns.Renderer._NODE_MAX_CHAR);
   409     this.title.content = Rkns.Renderer.Utils.shortenText(this.model.get("title"), Rkns.Renderer._NODE_MAX_CHAR);
   409     this.title.position = this.paper_coords.add([0, 2 * Rkns.Renderer._NODE_RADIUS]);
   410     this.title.position = this.paper_coords.add([0, 2 * Rkns.Renderer._NODE_RADIUS]);
   410     this.circle.strokeColor = this.model.get("created_by").get("color");
   411     this.circle.strokeColor = this.model.get("created_by").get("color");
       
   412     var _img = this.model.get("image");
       
   413     if (_img && _img !== this.img) {
       
   414         var _image = new Image(),
       
   415             _this = this;
       
   416         _image.onload = function() {
       
   417             if (_this.node_image) {
       
   418                 _this.node_image.remove();
       
   419             }
       
   420             _this.renderer.node_layer.activate();
       
   421             var _ratio = Math.min(1, 2 * Rkns.Renderer._NODE_RADIUS / _image.width, 2 * Rkns.Renderer._NODE_RADIUS / _image.height );
       
   422             var _raster = new paper.Raster(_image);
       
   423             var _clip = new paper.Path.Circle([0, 0], Rkns.Renderer._NODE_RADIUS);
       
   424             _raster.scale(_ratio);
       
   425             _this.node_image = new paper.Group(_clip, _raster);
       
   426             _this.node_image.opacity = .9;
       
   427             /* This is a workaround to allow clipping at group level */
       
   428             _this.node_image.clipped = true;
       
   429             _this.node_image.position = _this.paper_coords;
       
   430             _this.node_image.__representation = _this;
       
   431             paper.view.draw();
       
   432         }
       
   433         _image.src = _img;
       
   434     }
       
   435     this.img = _img;
       
   436     if (this.node_image) {
       
   437         if (!this.img) {
       
   438             this.node_image.remove();
       
   439             delete this.node_image;
       
   440         } else {
       
   441             this.node_image.position = this.paper_coords;
       
   442         }
       
   443     }
   411 }
   444 }
   412 
   445 
   413 Rkns.Renderer.Node.prototype.paperShift = function(_delta) {
   446 Rkns.Renderer.Node.prototype.paperShift = function(_delta) {
   414     this.paper_coords = this.paper_coords.add(_delta);
   447     this.paper_coords = this.paper_coords.add(_delta);
   415     this.model_coords = this.renderer.toModelCoords(this.paper_coords);
   448     this.model_coords = this.renderer.toModelCoords(this.paper_coords);
   418 
   451 
   419 Rkns.Renderer.Node.prototype.openTooltip = function() {
   452 Rkns.Renderer.Node.prototype.openTooltip = function() {
   420     this.renderer.removeRepresentationsOfType("tooltip");
   453     this.renderer.removeRepresentationsOfType("tooltip");
   421     var _tooltip = this.renderer.addRepresentation("NodeTooltip",null);
   454     var _tooltip = this.renderer.addRepresentation("NodeTooltip",null);
   422     _tooltip.node_representation = this;
   455     _tooltip.node_representation = this;
   423     _tooltip.redraw();
   456     _tooltip.draw();
   424 }
   457 }
   425 
   458 
   426 Rkns.Renderer.Node.prototype.select = function() {
   459 Rkns.Renderer.Node.prototype.select = function() {
   427     this.circle.strokeWidth = 3;
   460     this.circle.strokeWidth = 3;
   428     this.circle.fillColor = "#ffffc0";
   461     this.circle.fillColor = "#ffff80";
       
   462     if (this.node_image) {
       
   463         this.node_image.opacity = .5;
       
   464     }
   429     paper.view.draw();
   465     paper.view.draw();
   430 }
   466 }
   431 
   467 
   432 Rkns.Renderer.Node.prototype.unselect = function() {
   468 Rkns.Renderer.Node.prototype.unselect = function() {
   433     this.circle.strokeWidth = 1;
   469     this.circle.strokeWidth = 1;
   434     this.circle.fillColor = "#ffffff";
   470     this.circle.fillColor = "#ffffff";
       
   471     if (this.node_image) {
       
   472         this.node_image.opacity = .9;
       
   473     }
   435     paper.view.draw();
   474     paper.view.draw();
   436 }
   475 }
   437 
   476 
   438 Rkns.Renderer.Node.prototype.mouseup = function(_event) {
   477 Rkns.Renderer.Node.prototype.mouseup = function(_event) {
   439 }
   478 }
   440 
   479 
   441 Rkns.Renderer.Node.prototype.destroy = function(_event) {
   480 Rkns.Renderer.Node.prototype.destroy = function(_event) {
   442     this.circle.remove();
   481     this.circle.remove();
   443     this.title.remove();
   482     this.title.remove();
       
   483     if (this.node_image) {
       
   484         this.node_image.remove();
       
   485     }
   444 }
   486 }
   445 
   487 
   446 /* */
   488 /* */
   447 
   489 
   448 Rkns.Renderer.Edge = Rkns.Utils.inherit(Rkns.Renderer._BaseRepresentation);
   490 Rkns.Renderer.Edge = Rkns.Utils.inherit(Rkns.Renderer._BaseRepresentation);
   508 
   550 
   509 Rkns.Renderer.Edge.prototype.openTooltip = function() {
   551 Rkns.Renderer.Edge.prototype.openTooltip = function() {
   510     this.renderer.removeRepresentationsOfType("tooltip");
   552     this.renderer.removeRepresentationsOfType("tooltip");
   511     var _tooltip = this.renderer.addRepresentation("EdgeTooltip",null);
   553     var _tooltip = this.renderer.addRepresentation("EdgeTooltip",null);
   512     _tooltip.edge_representation = this;
   554     _tooltip.edge_representation = this;
   513     _tooltip.redraw();
   555     _tooltip.draw();
   514 }
   556 }
   515 
   557 
   516 Rkns.Renderer.Edge.prototype.select = function() {
   558 Rkns.Renderer.Edge.prototype.select = function() {
   517     this.line.strokeWidth = 3;
   559     this.line.strokeWidth = 3;
   518     this.openTooltip();
   560     this.openTooltip();
   569 Rkns.Renderer.NodeTooltip.prototype.template = Rkns._.template(
   611 Rkns.Renderer.NodeTooltip.prototype.template = Rkns._.template(
   570     '<h2><span class="Rk-CloseX">&times;</span><%=a%></h2>'
   612     '<h2><span class="Rk-CloseX">&times;</span><%=a%></h2>'
   571     + '<p><%=description%></p>'
   613     + '<p><%=description%></p>'
   572 );
   614 );
   573 
   615 
   574 Rkns.Renderer.NodeTooltip.prototype.redraw = function() {
   616 Rkns.Renderer.NodeTooltip.prototype.draw = function() {
   575     var _coords = this.node_representation.paper_coords,
   617     var _model = this.node_representation.model,
   576         _model = this.node_representation.model,
       
   577         _title = _model.get("title"),
   618         _title = _model.get("title"),
   578         _uri = _model.get("uri");
   619         _uri = _model.get("uri");
   579     this.tooltip_$
   620     this.tooltip_$
   580         .html(this.template({
   621         .html(this.template({
   581             a: (_uri ? '<a href="' + _uri + '" target="_blank">' : '' ) + _title + (_uri ? '</a>' : '' ),
   622             a: (_uri ? '<a href="' + _uri + '" target="_blank">' : '' ) + _title + (_uri ? '</a>' : '' ),
   582             description: _model.get("description").replace(/(\n|\r|\r\n)/mg,' ').substr(0,180).replace(/(^.{150,179})[\s].+$/m,'$1&hellip;')
   623             description: _model.get("description").replace(/(\n|\r|\r\n)/mg,' ').substr(0,180).replace(/(^.{150,179})[\s].+$/m,'$1&hellip;')
   583         }))
   624         }))
   584         .show();
   625     this.redraw();
   585     Rkns.Renderer.Utils.drawTooltip(_coords, this.tooltip_block, 250, 15, this.tooltip_$);
       
   586     var _this = this;
   626     var _this = this;
   587     this.tooltip_$.find(".Rk-CloseX").click(function() {
   627     this.tooltip_$.find(".Rk-CloseX").click(function() {
   588         _this.renderer.removeRepresentation(_this);
   628         _this.renderer.removeRepresentation(_this);
   589         paper.view.draw();
   629         paper.view.draw();
   590     });
   630     });
   591     this.tooltip_$.find("input, textarea").bind("keyup change", function() {
   631 }
   592         _this.tooltip_$.find(".Rk-Edit-Goto").attr("href",_this.tooltip_$.find(".Rk-Edit-URI").val());
   632 
   593     });
   633 Rkns.Renderer.NodeTooltip.prototype.redraw = function() {
   594     paper.view.draw();
   634     var _coords = this.node_representation.paper_coords;
       
   635     Rkns.Renderer.Utils.drawTooltip(_coords, this.tooltip_block, 250, 10, this.tooltip_$);
       
   636     this.tooltip_$.show();
   595 }
   637 }
   596 
   638 
   597 Rkns.Renderer.NodeTooltip.prototype.destroy = function() {
   639 Rkns.Renderer.NodeTooltip.prototype.destroy = function() {
   598     this.tooltip_block.remove();
   640     this.tooltip_block.remove();
   599     this.tooltip_$.detach();
   641     this.tooltip_$.detach();
   625 Rkns.Renderer.EdgeTooltip.prototype.template = Rkns._.template(
   667 Rkns.Renderer.EdgeTooltip.prototype.template = Rkns._.template(
   626     '<h2><span class="Rk-CloseX">&times;</span><%=a%></h2>'
   668     '<h2><span class="Rk-CloseX">&times;</span><%=a%></h2>'
   627     + '<p><%=description%></p>'
   669     + '<p><%=description%></p>'
   628 );
   670 );
   629 
   671 
   630 Rkns.Renderer.EdgeTooltip.prototype.redraw = function() {
   672 Rkns.Renderer.EdgeTooltip.prototype.draw = function() {
   631     var _coords = this.edge_representation.paper_coords,
   673     var _model = this.edge_representation.model,
   632         _model = this.edge_representation.model,
       
   633         _title = _model.get("title"),
   674         _title = _model.get("title"),
   634         _uri = _model.get("uri");
   675         _uri = _model.get("uri");
   635     this.tooltip_$
   676     this.tooltip_$
   636         .html(this.template({
   677         .html(this.template({
   637             a: (_uri ? '<a href="' + _uri + '" target="_blank">' : '' ) + _title + (_uri ? '</a>' : '' ),
   678             a: (_uri ? '<a href="' + _uri + '" target="_blank">' : '' ) + _title + (_uri ? '</a>' : '' ),
   638             description: _model.get("description").replace(/(\n|\r|\r\n)/mg,' ').substr(0,180).replace(/(^.{150,179})[\s].+$/m,'$1&hellip;')
   679             description: _model.get("description").replace(/(\n|\r|\r\n)/mg,' ').substr(0,180).replace(/(^.{150,179})[\s].+$/m,'$1&hellip;')
   639         }))
   680         }));
   640         .show();
   681     this.redraw();
   641     Rkns.Renderer.Utils.drawTooltip(_coords, this.tooltip_block, 250, 5, this.tooltip_$);
       
   642     var _this = this;
   682     var _this = this;
   643     this.tooltip_$.find(".Rk-CloseX").click(function() {
   683     this.tooltip_$.find(".Rk-CloseX").click(function() {
   644         _this.renderer.removeRepresentation(_this);
   684         _this.renderer.removeRepresentation(_this);
   645         paper.view.draw();
   685         paper.view.draw();
   646     });
   686     });
       
   687 }
       
   688 
       
   689 Rkns.Renderer.EdgeTooltip.prototype.redraw = function() {
       
   690     var _coords = this.edge_representation.paper_coords;
       
   691     Rkns.Renderer.Utils.drawTooltip(_coords, this.tooltip_block, 250, 5, this.tooltip_$);
       
   692     this.tooltip_$.show();
   647     paper.view.draw();
   693     paper.view.draw();
   648 }
   694 }
   649 
   695 
   650 Rkns.Renderer.EdgeTooltip.prototype.destroy = function() {
   696 Rkns.Renderer.EdgeTooltip.prototype.destroy = function() {
   651     this.tooltip_block.remove();
   697     this.tooltip_block.remove();