client/js/paper-renderer.js
changeset 278 b45dda454dd6
parent 277 1a54f4e626df
child 281 9ff388c9bc8d
equal deleted inserted replaced
277:1a54f4e626df 278:b45dda454dd6
   268             this.circle_radius = _baseRadius * this.renderer.scale;
   268             this.circle_radius = _baseRadius * this.renderer.scale;
   269             if (this.last_circle_radius !== this.circle_radius) {
   269             if (this.last_circle_radius !== this.circle_radius) {
   270                 this.all_buttons.forEach(function(b) {
   270                 this.all_buttons.forEach(function(b) {
   271                     b.setSectorSize();
   271                     b.setSectorSize();
   272                 });
   272                 });
   273                 var square = new paper.Size(this.circle_radius, this.circle_radius),
       
   274                 topleft = this.paper_coords.subtract(square),
       
   275                 bounds = new paper.Rectangle(topleft, square.multiply(2));
       
   276                 this.circle.scale(this.circle_radius / this.last_circle_radius);
   273                 this.circle.scale(this.circle_radius / this.last_circle_radius);
   277                 if (this.node_image) {
   274                 if (this.node_image) {
   278                     this.node_image.scale(this.circle_radius / this.last_circle_radius);
   275                     this.node_image.scale(this.circle_radius / this.last_circle_radius);
   279                 }
   276                 }
   280             }
   277             }
   284             }
   281             }
   285             this.last_circle_radius = this.circle_radius;
   282             this.last_circle_radius = this.circle_radius;
   286 
   283 
   287             var old_act_btn = this.active_buttons;
   284             var old_act_btn = this.active_buttons;
   288 
   285 
       
   286             var opacity = 1;
   289             if (this.model.get("delete_scheduled")) {
   287             if (this.model.get("delete_scheduled")) {
   290                 var opacity = .5;
   288                 opacity = .5;
   291                 this.active_buttons = this.pending_delete_buttons;
   289                 this.active_buttons = this.pending_delete_buttons;
   292                 this.circle.dashArray = [2,2];
   290                 this.circle.dashArray = [2,2];
   293             } else {
   291             } else {
   294                 var opacity = 1;
   292                 opacity = 1;
   295                 this.active_buttons = this.normal_buttons;
   293                 this.active_buttons = this.normal_buttons;
   296                 this.circle.dashArray = null;
   294                 this.circle.dashArray = null;
   297             }
   295             }
   298 
   296 
   299             if (this.selected && this.renderer.isEditable()) {
   297             if (this.selected && this.renderer.isEditable()) {
   370                 );
   368                 );
   371             }
   369             }
   372 
   370 
   373         },
   371         },
   374         showImage: function() {
   372         showImage: function() {
       
   373             var _image = null;
   375             if (typeof this.renderer.image_cache[this.img] === "undefined") {
   374             if (typeof this.renderer.image_cache[this.img] === "undefined") {
   376                 var _image = new Image();
   375                 _image = new Image();
   377                 this.renderer.image_cache[this.img] = _image;
   376                 this.renderer.image_cache[this.img] = _image;
   378                 _image.src = this.img;
   377                 _image.src = this.img;
   379             } else {
   378             } else {
   380                 var _image = this.renderer.image_cache[this.img];
   379                 _image = this.renderer.image_cache[this.img];
   381             }
   380             }
   382             if (_image.width) {
   381             if (_image.width) {
   383                 if (this.node_image) {
   382                 if (this.node_image) {
   384                     this.node_image.remove();
   383                     this.node_image.remove();
   385                 }
   384                 }
   386                 this.renderer.node_layer.activate();
   385                 this.renderer.node_layer.activate();
   387                 var width = _image.width,
   386                 var width = _image.width,
   388                 height = _image.height,
   387                     height = _image.height,
   389                 clipPath = this.model.get("clip_path"),
   388                     clipPath = this.model.get("clip_path"),
   390                 hasClipPath = (typeof clipPath !== "undefined" && clipPath);
   389                     hasClipPath = (typeof clipPath !== "undefined" && clipPath),
       
   390                     _clip = null,
       
   391                     baseRadius = null,
       
   392                     centerPoint = null;
       
   393                 
   391                 if (hasClipPath) {
   394                 if (hasClipPath) {
   392                     var _clip = new paper.Path(),
   395                     _clip = new paper.Path();
   393                     instructions = clipPath.match(/[a-z][^a-z]+/gi) || [],
   396                     var instructions = clipPath.match(/[a-z][^a-z]+/gi) || [],
   394                     lastCoords = [0,0],
   397                         lastCoords = [0,0],
   395                     minX = Infinity,
   398                         minX = Infinity,
   396                     minY = Infinity,
   399                         minY = Infinity,
   397                     maxX = -Infinity,
   400                         maxX = -Infinity,
   398                     maxY = -Infinity;
   401                         maxY = -Infinity;
   399 
   402 
   400                     var transformCoords = function(tabc, relative) {
   403                     var transformCoords = function(tabc, relative) {
   401                         var newCoords = tabc.slice(1).map(function(v, k) {
   404                         var newCoords = tabc.slice(1).map(function(v, k) {
   402                             var res = parseFloat(v),
   405                             var res = parseFloat(v),
   403                             isY = k % 2;
   406                             isY = k % 2;
   450                             _clip.quadraticCurveTo(transformCoords(coords, true));
   453                             _clip.quadraticCurveTo(transformCoords(coords, true));
   451                             break;
   454                             break;
   452                         }
   455                         }
   453                     });
   456                     });
   454 
   457 
   455                     var baseRadius = Math[this.options.node_images_fill_mode ? "min" : "max"](maxX - minX, maxY - minY) / 2,
   458                     baseRadius = Math[this.options.node_images_fill_mode ? "min" : "max"](maxX - minX, maxY - minY) / 2;
   456                     centerPoint = new paper.Point((maxX + minX) / 2, (maxY + minY) / 2);
   459                     centerPoint = new paper.Point((maxX + minX) / 2, (maxY + minY) / 2);
   457                     if (!this.options.show_node_circles) {
   460                     if (!this.options.show_node_circles) {
   458                         this.h_ratio = (maxY - minY) / (2 * baseRadius);
   461                         this.h_ratio = (maxY - minY) / (2 * baseRadius);
   459                     }
   462                     }
   460                 } else {
   463                 } else {
   461                     var baseRadius = Math[this.options.node_images_fill_mode ? "min" : "max"](width, height) / 2,
   464                     baseRadius = Math[this.options.node_images_fill_mode ? "min" : "max"](width, height) / 2;
   462                     centerPoint = new paper.Point(0,0);
   465                     centerPoint = new paper.Point(0,0);
   463                     if (!this.options.show_node_circles) {
   466                     if (!this.options.show_node_circles) {
   464                         this.h_ratio = height / (2 * baseRadius);
   467                         this.h_ratio = height / (2 * baseRadius);
   465                     }
   468                     }
   466                 }
   469                 }
   677             this.to_representation = this.renderer.getRepresentationByModel(to);
   680             this.to_representation = this.renderer.getRepresentationByModel(to);
   678             if (typeof this.from_representation === "undefined" || typeof this.to_representation === "undefined") {
   681             if (typeof this.from_representation === "undefined" || typeof this.to_representation === "undefined") {
   679                 return;
   682                 return;
   680             }
   683             }
   681             var _p0a = this.from_representation.paper_coords,
   684             var _p0a = this.from_representation.paper_coords,
   682             _p1a = this.to_representation.paper_coords,
   685                 _p1a = this.to_representation.paper_coords,
   683             _v = _p1a.subtract(_p0a),
   686                 _v = _p1a.subtract(_p0a),
   684             _r = _v.length,
   687                 _r = _v.length,
   685             _u = _v.divide(_r),
   688                 _u = _v.divide(_r),
   686             _ortho = new paper.Point([- _u.y, _u.x]),
   689                 _ortho = new paper.Point([- _u.y, _u.x]),
   687             _group_pos = this.bundle.getPosition(this),
   690                 _group_pos = this.bundle.getPosition(this),
   688             _delta = _ortho.multiply( this.options.edge_gap_in_bundles * _group_pos ),
   691                 _delta = _ortho.multiply( this.options.edge_gap_in_bundles * _group_pos ),
   689             _p0b = _p0a.add(_delta), /* Adding a 4 px difference */
   692                 _p0b = _p0a.add(_delta), /* Adding a 4 px difference */
   690             _p1b = _p1a.add(_delta), /* to differentiate bundled links */
   693                 _p1b = _p1a.add(_delta), /* to differentiate bundled links */
   691             _a = _v.angle,
   694                 _a = _v.angle,
   692             _textdelta = _ortho.multiply(this.options.edge_label_distance),
   695                 _textdelta = _ortho.multiply(this.options.edge_label_distance),
   693             _handle = _v.divide(3),
   696                 _handle = _v.divide(3),
   694             _color = this.model.get("color") || this.model.get("color") || (this.model.get("created_by") || _USER_PLACEHOLDER(this.renkan)).get("color");
   697                 _color = this.model.get("color") || this.model.get("color") || (this.model.get("created_by") || _USER_PLACEHOLDER(this.renkan)).get("color"),
       
   698                 opacity = 1;
   695 
   699 
   696             if (this.model.get("delete_scheduled") || this.from_representation.model.get("delete_scheduled") || this.to_representation.model.get("delete_scheduled")) {
   700             if (this.model.get("delete_scheduled") || this.from_representation.model.get("delete_scheduled") || this.to_representation.model.get("delete_scheduled")) {
   697                 var opacity = .5;
   701                 opacity = .5;
   698                 this.line.dashArray = [2, 2];
   702                 this.line.dashArray = [2, 2];
   699             } else {
   703             } else {
   700                 var opacity = 1;
   704                 opacity = 1;
   701                 this.line.dashArray = null;
   705                 this.line.dashArray = null;
   702             }
   706             }
   703 
   707 
   704             var old_act_btn = this.active_buttons;
   708             var old_act_btn = this.active_buttons;
   705 
   709 
   833             this.all_buttons.forEach(function(b) {
   837             this.all_buttons.forEach(function(b) {
   834                 b.destroy();
   838                 b.destroy();
   835             });
   839             });
   836             var _this = this;
   840             var _this = this;
   837             this.bundle.edges = _(this.bundle.edges).reject(function(_edge) {
   841             this.bundle.edges = _(this.bundle.edges).reject(function(_edge) {
   838                 return _edge === _this;
   842                 return _this === _edge;
   839             });
   843             });
   840         }
   844         }
   841     });
   845     });
   842 
   846 
   843     /* */
   847     /* */
  1626 
  1630 
  1627         this.bundles = [];
  1631         this.bundles = [];
  1628         this.click_mode = false;
  1632         this.click_mode = false;
  1629 
  1633 
  1630         var _this = this,
  1634         var _this = this,
  1631         _allowScroll = true,
  1635             _allowScroll = true,
  1632         _originalScale,
  1636             _originalScale = 1,
  1633         _zooming = false,
  1637             _zooming = false,
  1634         _lastTapDate,
  1638             _lastTapX = 0,
  1635         _lastTapX,
  1639             _lastTapY = 0;
  1636         _lastTapY;
       
  1637 
  1640 
  1638         this.image_cache = {};
  1641         this.image_cache = {};
  1639         this.icon_cache = {};
  1642         this.icon_cache = {};
  1640 
  1643 
  1641         ['edit', 'remove', 'link', 'enlarge', 'shrink', 'revert' ].forEach(function(imgname) {
  1644         ['edit', 'remove', 'link', 'enlarge', 'shrink', 'revert' ].forEach(function(imgname) {
  1707                     _scaleRatio = _newScale / _this.scale,
  1710                     _scaleRatio = _newScale / _this.scale,
  1708                     _newOffset = new paper.Point([
  1711                     _newOffset = new paper.Point([
  1709                                                   _this.canvas_$.width(),
  1712                                                   _this.canvas_$.width(),
  1710                                                   _this.canvas_$.height()
  1713                                                   _this.canvas_$.height()
  1711                                                   ]).multiply( .5 * ( 1 - _scaleRatio ) ).add(_this.offset.multiply( _scaleRatio ));
  1714                                                   ]).multiply( .5 * ( 1 - _scaleRatio ) ).add(_this.offset.multiply( _scaleRatio ));
  1712                     _this.setScale(_newScale, _this.offset);
  1715                     _this.setScale(_newScale, _newOffset);
  1713                 }
  1716                 }
  1714             },
  1717             },
  1715             touchend: function(_event) {
  1718             touchend: function(_event) {
  1716                 _event.preventDefault();
  1719                 _event.preventDefault();
  1717                 _this.onMouseUp(_event.originalEvent.changedTouches[0], true);
  1720                 _this.onMouseUp(_event.originalEvent.changedTouches[0], true);
  2040                 }
  2043                 }
  2041             }
  2044             }
  2042         },
  2045         },
  2043         drawSector: function(_repr, _inR, _outR, _startAngle, _endAngle, _padding, _imgname, _caption) {
  2046         drawSector: function(_repr, _inR, _outR, _startAngle, _endAngle, _padding, _imgname, _caption) {
  2044             var _options = this.renkan.options,
  2047             var _options = this.renkan.options,
  2045             _startRads = _startAngle * Math.PI / 180,
  2048                 _startRads = _startAngle * Math.PI / 180,
  2046             _endRads = _endAngle * Math.PI / 180,
  2049                 _endRads = _endAngle * Math.PI / 180,
  2047             _img = this.icon_cache[_imgname],
  2050                 _img = this.icon_cache[_imgname],
  2048             _span = _endRads - _startRads,
  2051                 _startdx = - Math.sin(_startRads),
  2049             _startdx = - Math.sin(_startRads),
  2052                 _startdy = Math.cos(_startRads),
  2050             _startdy = Math.cos(_startRads),
  2053                 _startXIn = Math.cos(_startRads) * _inR + _padding * _startdx,
  2051             _startXIn = Math.cos(_startRads) * _inR + _padding * _startdx,
  2054                 _startYIn = Math.sin(_startRads) * _inR + _padding * _startdy,
  2052             _startYIn = Math.sin(_startRads) * _inR + _padding * _startdy,
  2055                 _startXOut = Math.cos(_startRads) * _outR + _padding * _startdx,
  2053             _startXOut = Math.cos(_startRads) * _outR + _padding * _startdx,
  2056                 _startYOut = Math.sin(_startRads) * _outR + _padding * _startdy,
  2054             _startYOut = Math.sin(_startRads) * _outR + _padding * _startdy,
  2057                 _enddx = - Math.sin(_endRads),
  2055             _enddx = - Math.sin(_endRads),
  2058                 _enddy = Math.cos(_endRads),
  2056             _enddy = Math.cos(_endRads),
  2059                 _endXIn = Math.cos(_endRads) * _inR - _padding * _enddx,
  2057             _endXIn = Math.cos(_endRads) * _inR - _padding * _enddx,
  2060                 _endYIn = Math.sin(_endRads) * _inR - _padding * _enddy,
  2058             _endYIn = Math.sin(_endRads) * _inR - _padding * _enddy,
  2061                 _endXOut = Math.cos(_endRads) * _outR - _padding * _enddx,
  2059             _endXOut = Math.cos(_endRads) * _outR - _padding * _enddx,
  2062                 _endYOut = Math.sin(_endRads) * _outR - _padding * _enddy,
  2060             _endYOut = Math.sin(_endRads) * _outR - _padding * _enddy,
  2063                 _centerR = (_inR + _outR)/2,
  2061             _centerR = (_inR + _outR)/2,
  2064                 _centerRads = (_startRads + _endRads) / 2,
  2062             _centerRads = (_startRads + _endRads) / 2,
  2065                 _centerX = Math.cos(_centerRads) * _centerR,
  2063             _centerX = Math.cos(_centerRads) * _centerR,
  2066                 _centerY = Math.sin(_centerRads) * _centerR,
  2064             _centerY = Math.sin(_centerRads) * _centerR,
  2067                 _centerXIn = Math.cos(_centerRads) * _inR,
  2065             _centerXIn = Math.cos(_centerRads) * _inR,
  2068                 _centerXOut = Math.cos(_centerRads) * _outR,
  2066             _centerXOut = Math.cos(_centerRads) * _outR,
  2069                 _centerYIn = Math.sin(_centerRads) * _inR,
  2067             _centerYIn = Math.sin(_centerRads) * _inR,
  2070                 _centerYOut = Math.sin(_centerRads) * _outR,
  2068             _centerYOut = Math.sin(_centerRads) * _outR,
  2071                 _textX = Math.cos(_centerRads) * (_outR + 3),
  2069             _textX = Math.cos(_centerRads) * (_outR + 3),
  2072                 _textY = Math.sin(_centerRads) * (_outR + _options.buttons_label_font_size) + _options.buttons_label_font_size / 2;
  2070             _textY = Math.sin(_centerRads) * (_outR + _options.buttons_label_font_size) + _options.buttons_label_font_size / 2,
       
  2071             _segments = [];
       
  2072             this.buttons_layer.activate();
  2073             this.buttons_layer.activate();
  2073             var _path = new paper.Path();
  2074             var _path = new paper.Path();
  2074             _path.add([_startXIn, _startYIn]);
  2075             _path.add([_startXIn, _startYIn]);
  2075             _path.arcTo([_centerXIn, _centerYIn], [_endXIn, _endYIn]);
  2076             _path.arcTo([_centerXIn, _centerYIn], [_endXIn, _endYIn]);
  2076             _path.lineTo([_endXOut,  _endYOut]);
  2077             _path.lineTo([_endXOut,  _endYOut]);
  2091             } else {
  2092             } else {
  2092                 _text.paragraphStyle.justification = 'center';
  2093                 _text.paragraphStyle.justification = 'center';
  2093             }
  2094             }
  2094             _text.visible = false;
  2095             _text.visible = false;
  2095             var _visible = false,
  2096             var _visible = false,
  2096             _restPos = new paper.Point(-200, -200),
  2097                 _restPos = new paper.Point(-200, -200),
  2097             _grp = new paper.Group([_path, _text]),
  2098                 _grp = new paper.Group([_path, _text]),
  2098             _delta = _grp.position,
  2099                 _delta = _grp.position,
  2099             _imgdelta = new paper.Point([_centerX, _centerY]),
  2100                 _imgdelta = new paper.Point([_centerX, _centerY]),
  2100             _currentPos = new paper.Point(0,0);
  2101                 _currentPos = new paper.Point(0,0);
  2101             _text.content = _caption;
  2102             _text.content = _caption;
  2102             _grp.visible = false;
  2103             _grp.visible = false;
  2103             _grp.position = _restPos;
  2104             _grp.position = _restPos;
  2104             var _res = {
  2105             var _res = {
  2105                     show: function() {
  2106                     show: function() {
  2128                     },
  2129                     },
  2129                     destroy: function() {
  2130                     destroy: function() {
  2130                         _grp.remove();
  2131                         _grp.remove();
  2131                     }
  2132                     }
  2132             };
  2133             };
  2133             function showImage() {
  2134             var showImage = function() {
  2134                 var _raster = new paper.Raster(_img);
  2135                 var _raster = new paper.Raster(_img);
  2135                 _raster.position = _imgdelta.add(_grp.position).subtract(_delta);
  2136                 _raster.position = _imgdelta.add(_grp.position).subtract(_delta);
  2136                 _raster.locked = true; // Disable mouse events on icon
  2137                 _raster.locked = true; // Disable mouse events on icon
  2137                 _grp.addChild(_raster);
  2138                 _grp.addChild(_raster);
  2138             }
  2139             };
  2139             if (_img.width) {
  2140             if (_img.width) {
  2140                 showImage();
  2141                 showImage();
  2141             } else {
  2142             } else {
  2142                 $(_img).on("load",showImage);
  2143                 $(_img).on("load",showImage);
  2143             }
  2144             }
  2277             }
  2278             }
  2278             var allUsers = [].concat((this.renkan.project.current_user_list || {}).models || [], (this.renkan.project.get("users") || {}).models || []),
  2279             var allUsers = [].concat((this.renkan.project.current_user_list || {}).models || [], (this.renkan.project.get("users") || {}).models || []),
  2279             ulistHtml = '',
  2280             ulistHtml = '',
  2280             $userpanel = this.$.find(".Rk-Users"),
  2281             $userpanel = this.$.find(".Rk-Users"),
  2281             $name = $userpanel.find(".Rk-CurrentUser-Name"),
  2282             $name = $userpanel.find(".Rk-CurrentUser-Name"),
  2282             $cpwrapper = $userpanel.find(".Rk-Edit-ColorPicker-Wrapper"),
       
  2283             $cpitems = $userpanel.find(".Rk-Edit-ColorPicker li"),
  2283             $cpitems = $userpanel.find(".Rk-Edit-ColorPicker li"),
  2284             $colorsquare = $userpanel.find(".Rk-CurrentUser-Color"),
  2284             $colorsquare = $userpanel.find(".Rk-CurrentUser-Color"),
  2285             _this = this;
  2285             _this = this;
  2286             $name.off("click").text(this.renkan.translate("<unknown user>"));
  2286             $name.off("click").text(this.renkan.translate("<unknown user>"));
  2287             $cpitems.off("mouseleave click");
  2287             $cpitems.off("mouseleave click");