client/js/paper-renderer.js
changeset 55 17ff0be41a5b
parent 53 bb63da144239
child 56 a9b9e6c7be63
equal deleted inserted replaced
54:43c35d6ca3f0 55:17ff0be41a5b
    18     _EDITOR_MARGIN : 15,
    18     _EDITOR_MARGIN : 15,
    19     _EDITOR_PADDING : 10,
    19     _EDITOR_PADDING : 10,
    20     _EDITOR_GRADIENT : new paper.Gradient(['#f0f0f0', '#d0d0d0']),
    20     _EDITOR_GRADIENT : new paper.Gradient(['#f0f0f0', '#d0d0d0']),
    21     _CLICKMODE_ADDNODE : 1,
    21     _CLICKMODE_ADDNODE : 1,
    22     _CLICKMODE_STARTEDGE : 2,
    22     _CLICKMODE_STARTEDGE : 2,
    23     _CLICKMODE_ENDEDGE : 3
    23     _CLICKMODE_ENDEDGE : 3,
       
    24     _USER_PLACEHOLDER : {
       
    25         color: "#000000",
       
    26         title: "(unknown user)",
       
    27         get: function(attr) {
       
    28             return this[attr] || false;
       
    29         }
       
    30     }
    24 }
    31 }
    25 
    32 
    26 Rkns.Renderer.Utils = {
    33 Rkns.Renderer.Utils = {
    27     drawEditBox : function(_coords, _path, _width, _xmargin, _selector) {
    34     drawEditBox : function(_coords, _path, _width, _xmargin, _selector) {
    28         _selector.css({
    35         _selector.css({
   234     this.renderer.node_layer.activate();
   241     this.renderer.node_layer.activate();
   235     this.type = "Node";
   242     this.type = "Node";
   236     this.circle = new paper.Path.Circle([0, 0], Rkns.Renderer._NODE_RADIUS);
   243     this.circle = new paper.Path.Circle([0, 0], Rkns.Renderer._NODE_RADIUS);
   237     this.circle.fillColor = '#ffffff';
   244     this.circle.fillColor = '#ffffff';
   238     this.circle.__representation = this;
   245     this.circle.__representation = this;
       
   246     this.circle.strokeWidth = 2;
   239     this.title = new paper.PointText([0,0]);
   247     this.title = new paper.PointText([0,0]);
   240     this.title.characterStyle = {
   248     this.title.characterStyle = {
   241         fontSize: Rkns.Renderer._NODE_FONT_SIZE,
   249         fontSize: Rkns.Renderer._NODE_FONT_SIZE,
   242         fillColor: 'black'
   250         fillColor: 'black'
   243     };
   251     };
   254     var _model_coords = new paper.Point(this.model.get("position"));
   262     var _model_coords = new paper.Point(this.model.get("position"));
   255     this.paper_coords = this.renderer.toPaperCoords(_model_coords);
   263     this.paper_coords = this.renderer.toPaperCoords(_model_coords);
   256     this.circle.position = this.paper_coords;
   264     this.circle.position = this.paper_coords;
   257     this.title.content = this.model.get("title");
   265     this.title.content = this.model.get("title");
   258     this.title.position = this.paper_coords.add([0, Rkns.Renderer._NODE_RADIUS + 1.5 *Rkns.Renderer._NODE_FONT_SIZE]);
   266     this.title.position = this.paper_coords.add([0, Rkns.Renderer._NODE_RADIUS + 1.5 *Rkns.Renderer._NODE_FONT_SIZE]);
   259     this.circle.strokeColor = this.model.get("created_by").get("color");
   267     this.circle.strokeColor = this.model.get("color") || (this.model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER).get("color");
   260     this.edit_button.moveTo(this.paper_coords);
   268     this.edit_button.moveTo(this.paper_coords);
   261     this.remove_button.moveTo(this.paper_coords);
   269     this.remove_button.moveTo(this.paper_coords);
   262     this.link_button.moveTo(this.paper_coords);
   270     this.link_button.moveTo(this.paper_coords);
   263     var _img = this.model.get("image");
   271     var _img = this.model.get("image");
   264     if (_img && _img !== this.img) {
   272     if (_img && _img !== this.img) {
   315     _editor.node_representation = this;
   323     _editor.node_representation = this;
   316     _editor.draw();
   324     _editor.draw();
   317 }
   325 }
   318 
   326 
   319 Rkns.Renderer.Node.prototype.select = function() {
   327 Rkns.Renderer.Node.prototype.select = function() {
   320     this.circle.strokeWidth = 3;
   328     this.circle.strokeWidth = 4;
   321     this.edit_button.show();
   329     this.edit_button.show();
   322     this.remove_button.show();
   330     this.remove_button.show();
   323     this.link_button.show();
   331     this.link_button.show();
   324     var _uri = this.model.get("uri");
   332     var _uri = this.model.get("uri");
   325     Rkns.$('.Rk-Bin-Item').each(function() {
   333     Rkns.$('.Rk-Bin-Item').each(function() {
   333 Rkns.Renderer.Node.prototype.unselect = function(_newTarget) {
   341 Rkns.Renderer.Node.prototype.unselect = function(_newTarget) {
   334     if (!_newTarget || _newTarget.node_representation !== this) {
   342     if (!_newTarget || _newTarget.node_representation !== this) {
   335         this.edit_button.hide();
   343         this.edit_button.hide();
   336         this.remove_button.hide();
   344         this.remove_button.hide();
   337         this.link_button.hide();
   345         this.link_button.hide();
   338         this.circle.strokeWidth = 1;
   346         this.circle.strokeWidth = 2;
   339         Rkns.$('.Rk-Bin-Item').removeClass("selected");
   347         Rkns.$('.Rk-Bin-Item').removeClass("selected");
   340     }
   348     }
   341 }
   349 }
   342 
   350 
   343 Rkns.Renderer.Node.prototype.highlight = function() {
   351 Rkns.Renderer.Node.prototype.highlight = function() {
   385     this.to_representation = this.renderer.getRepresentationByModel(this.model.get("to"));
   393     this.to_representation = this.renderer.getRepresentationByModel(this.model.get("to"));
   386     this.bundle = this.renderer.addToBundles(this);
   394     this.bundle = this.renderer.addToBundles(this);
   387     this.line = new paper.Path();
   395     this.line = new paper.Path();
   388     this.line.add([0,0],[0,0],[0,0]);
   396     this.line.add([0,0],[0,0],[0,0]);
   389     this.line.__representation = this;
   397     this.line.__representation = this;
       
   398     this.line.strokeWidth = 2;
   390     this.arrow = new paper.Path();
   399     this.arrow = new paper.Path();
   391     this.arrow.add([0,0],[Rkns.Renderer._ARROW_LENGTH,Rkns.Renderer._ARROW_WIDTH / 2],[0,Rkns.Renderer._ARROW_WIDTH]);
   400     this.arrow.add([0,0],[Rkns.Renderer._ARROW_LENGTH,Rkns.Renderer._ARROW_WIDTH / 2],[0,Rkns.Renderer._ARROW_WIDTH]);
   392     this.arrow.__representation = this;
   401     this.arrow.__representation = this;
   393     this.text = new paper.PointText();
   402     this.text = new paper.PointText();
   394     this.text.characterStyle = {
   403     this.text.characterStyle = {
   414         _delta = new paper.Point([- _u.y, _u.x]).multiply( 12 * _group_pos ),
   423         _delta = new paper.Point([- _u.y, _u.x]).multiply( 12 * _group_pos ),
   415         _p0b = _p0a.add(_delta), /* Adding a 4 px difference */
   424         _p0b = _p0a.add(_delta), /* Adding a 4 px difference */
   416         _p1b = _p1a.add(_delta), /* to differentiate inbound and outbound links */
   425         _p1b = _p1a.add(_delta), /* to differentiate inbound and outbound links */
   417         _a = _v.angle,
   426         _a = _v.angle,
   418         _handle = _v.divide(3),
   427         _handle = _v.divide(3),
   419         _color = this.model.get("created_by").get("color");
   428         _color = this.model.get("color") || this.model.get("color") || (this.model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER).get("color");;
   420     this.paper_coords = _p0b.add(_p1b).divide(2);
   429     this.paper_coords = _p0b.add(_p1b).divide(2);
   421     this.line.strokeColor = _color;
   430     this.line.strokeColor = _color;
   422     this.line.segments[0].point = _p0a;
   431     this.line.segments[0].point = _p0a;
   423     this.line.segments[1].point = this.paper_coords;
   432     this.line.segments[1].point = this.paper_coords;
   424     this.line.segments[1].handleIn = _handle.multiply(-1);
   433     this.line.segments[1].handleIn = _handle.multiply(-1);
   448     _editor.edge_representation = this;
   457     _editor.edge_representation = this;
   449     _editor.draw();
   458     _editor.draw();
   450 }
   459 }
   451 
   460 
   452 Rkns.Renderer.Edge.prototype.select = function() {
   461 Rkns.Renderer.Edge.prototype.select = function() {
   453     this.line.strokeWidth = 3;
   462     this.line.strokeWidth = 4;
   454     this.edit_button.show();
   463     this.edit_button.show();
   455     this.remove_button.show();
   464     this.remove_button.show();
   456 }
   465 }
   457 
   466 
   458 Rkns.Renderer.Edge.prototype.unselect = function(_newTarget) {
   467 Rkns.Renderer.Edge.prototype.unselect = function(_newTarget) {
   459     if (!_newTarget || _newTarget.edge_representation !== this) {
   468     if (!_newTarget || _newTarget.edge_representation !== this) {
   460         this.edit_button.hide();
   469         this.edit_button.hide();
   461         this.remove_button.hide();
   470         this.remove_button.hide();
   462         this.line.strokeWidth = 1;
   471         this.line.strokeWidth = 2;
   463     }
   472     }
   464 }
   473 }
   465 
   474 
   466 Rkns.Renderer.Edge.prototype.mouseup = function(_event) {
   475 Rkns.Renderer.Edge.prototype.mouseup = function(_event) {
   467     if (!this.renderer.is_dragging) {
   476     if (!this.renderer.is_dragging) {
   496 
   505 
   497 Rkns.Renderer.TempEdge.prototype._init = function() {
   506 Rkns.Renderer.TempEdge.prototype._init = function() {
   498     this.renderer.edge_layer.activate();
   507     this.renderer.edge_layer.activate();
   499     this.type = "temp-edge";
   508     this.type = "temp-edge";
   500     
   509     
   501     var _color = this.project.get("users").get(this.renderer.renkan.current_user).get("color");
   510     var _color = this.project.get("users").get(this.renderer.renkan.current_user).get("color") || "#000000";
   502     this.line = new paper.Path();
   511     this.line = new paper.Path();
   503     this.line.strokeColor = _color;
   512     this.line.strokeColor = _color;
   504     this.line.add([0,0],[0,0]);
   513     this.line.add([0,0],[0,0]);
   505     this.line.__representation = this;
   514     this.line.__representation = this;
   506     this.arrow = new paper.Path();
   515     this.arrow = new paper.Path();
   588 Rkns.Renderer.NodeEditor.prototype.template = Rkns._.template(
   597 Rkns.Renderer.NodeEditor.prototype.template = Rkns._.template(
   589     '<h2><span class="Rk-CloseX">&times;</span><%=l10n.edit_node%></span></h2>'
   598     '<h2><span class="Rk-CloseX">&times;</span><%=l10n.edit_node%></span></h2>'
   590     + '<p><label><%=l10n.edit_title%></label><input class="Rk-Edit-Title" type="text" value="<%=node.title%>"/></p>'
   599     + '<p><label><%=l10n.edit_title%></label><input class="Rk-Edit-Title" type="text" value="<%=node.title%>"/></p>'
   591     + '<p><label><%=l10n.edit_uri%></label><input class="Rk-Edit-URI" type="text" value="<%=node.uri%>"/><a class="Rk-Edit-Goto" href="<%=node.uri%>" target="_blank"></a></p>'
   600     + '<p><label><%=l10n.edit_uri%></label><input class="Rk-Edit-URI" type="text" value="<%=node.uri%>"/><a class="Rk-Edit-Goto" href="<%=node.uri%>" target="_blank"></a></p>'
   592     + '<p><label><%=l10n.edit_description%></label><textarea class="Rk-Edit-Description"><%=node.description%></textarea></p>'
   601     + '<p><label><%=l10n.edit_description%></label><textarea class="Rk-Edit-Description"><%=node.description%></textarea></p>'
       
   602     + '<div class="Rk-Editor-p"><label>Node color</label><div class="Rk-Edit-ColorPicker-Wrapper"><span class="Rk-Edit-Color" style="background:<%=node.color%>;"><span class="Rk-Edit-ColorTip"></span></span><ul class="Rk-Edit-ColorPicker">'
       
   603     + '<% print(Rkns.pickerColors.reduce(function(m,c) { return m + "<li data-color=\'" + c + "\' style=\'background: " + c + "\'></li>"},"")); %></ul><span class="Rk-Edit-ColorPicker-Text">Choose color</span></div></div>'
   593     + '<p><label><%=l10n.edit_image%></label><input class="Rk-Edit-Image" type="text" value="<%=node.image%>"/><img class="Rk-Edit-ImgPreview" src="<%=node.image%>" /></p>'
   604     + '<p><label><%=l10n.edit_image%></label><input class="Rk-Edit-Image" type="text" value="<%=node.image%>"/><img class="Rk-Edit-ImgPreview" src="<%=node.image%>" /></p>'
   594     + '<p><label><%=l10n.created_by%></label> <span class="Rk-UserColor" style="background:<%=node.created_by_color%>;"></span><%=node.created_by_title%></p>'
   605     + '<p><label><%=l10n.created_by%></label> <span class="Rk-UserColor" style="background:<%=node.created_by_color%>;"></span><%=node.created_by_title%></p>'
   595 );
   606 );
   596 
   607 
   597 Rkns.Renderer.NodeEditor.prototype.draw = function() {
   608 Rkns.Renderer.NodeEditor.prototype.draw = function() {
   598     var _model = this.node_representation.model;
   609     var _model = this.node_representation.model,
       
   610         _created_by = _model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER;
   599     this.editor_$
   611     this.editor_$
   600         .html(this.template({
   612         .html(this.template({
   601             node: {
   613             node: {
   602                 title: _model.get("title"),
   614                 title: _model.get("title"),
   603                 uri: _model.get("uri"),
   615                 uri: _model.get("uri"),
   604                 description: _model.get("description"),
   616                 description: _model.get("description"),
   605                 image: _model.get("image") || "",
   617                 image: _model.get("image") || "",
   606                 created_by_color: _model.get("created_by").get("color"),
   618                 color: _model.get("color") || _created_by.get("color"),
   607                 created_by_title: _model.get("created_by").get("title")
   619                 created_by_color: _created_by.get("color"),
       
   620                 created_by_title: _created_by.get("title")
   608             },
   621             },
   609             l10n: this.renderer.renkan.l10n
   622             l10n: this.renderer.renkan.l10n
   610         }));
   623         }));
   611     this.redraw();
   624     this.redraw();
   612     var _this = this;
   625     var _this = this;
   630     });
   643     });
   631     this.editor_$.find("img").load(function() {
   644     this.editor_$.find("img").load(function() {
   632         _this.redraw();
   645         _this.redraw();
   633     })
   646     })
   634     this.editor_$.find(".Rk-Edit-Title")[0].focus();
   647     this.editor_$.find(".Rk-Edit-Title")[0].focus();
       
   648     this.editor_$.find(".Rk-Edit-ColorPicker-Wrapper").hover(
       
   649         function() { _this.editor_$.find(".Rk-Edit-ColorPicker").show(); },
       
   650         function() { _this.editor_$.find(".Rk-Edit-ColorPicker").hide(); }
       
   651     );
       
   652     this.editor_$.find(".Rk-Edit-ColorPicker li").hover(
       
   653         function() { _this.editor_$.find(".Rk-Edit-Color").css("background", $(this).attr("data-color")); },
       
   654         function() { _this.editor_$.find(".Rk-Edit-Color").css("background", _model.get("color") || (_model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER).get("color")) }
       
   655     ).click(function() {
       
   656         _model.set("color", $(this).attr("data-color"));
       
   657         _this.redraw();
       
   658     });
   635 }
   659 }
   636 
   660 
   637 Rkns.Renderer.NodeEditor.prototype.redraw = function() {
   661 Rkns.Renderer.NodeEditor.prototype.redraw = function() {
   638     var _coords = this.node_representation.paper_coords;
   662     var _coords = this.node_representation.paper_coords;
   639     Rkns.Renderer.Utils.drawEditBox(_coords, this.editor_block, 250, 20, this.editor_$);
   663     Rkns.Renderer.Utils.drawEditBox(_coords, this.editor_block, 250, 20, this.editor_$);
   671 
   695 
   672 Rkns.Renderer.EdgeEditor.prototype.template = Rkns._.template(
   696 Rkns.Renderer.EdgeEditor.prototype.template = Rkns._.template(
   673     '<h2><span class="Rk-CloseX">&times;</span><%=l10n.edit_edge%></span></h2>'
   697     '<h2><span class="Rk-CloseX">&times;</span><%=l10n.edit_edge%></span></h2>'
   674     + '<p><label><%=l10n.edit_title%></label><input class="Rk-Edit-Title" type="text" value="<%=edge.title%>"/></p>'
   698     + '<p><label><%=l10n.edit_title%></label><input class="Rk-Edit-Title" type="text" value="<%=edge.title%>"/></p>'
   675     + '<p><label><%=l10n.edit_uri%></label><input class="Rk-Edit-URI" type="text" value="<%=edge.uri%>"/><a class="Rk-Edit-Goto" href="<%=edge.uri%>" target="_blank"></a></p>'
   699     + '<p><label><%=l10n.edit_uri%></label><input class="Rk-Edit-URI" type="text" value="<%=edge.uri%>"/><a class="Rk-Edit-Goto" href="<%=edge.uri%>" target="_blank"></a></p>'
   676     + '<p><label><%=l10n.edit_from%></label><span class="Rk-UserColor" style="background:<%=edge.from_created_by_color%>;"></span><%=edge.from_title%></p>'
   700     + '<div class="Rk-Editor-p"><label>Edge color:</label><div class="Rk-Edit-ColorPicker-Wrapper"><span class="Rk-Edit-Color" style="background:<%=edge.color%>;"><span class="Rk-Edit-ColorTip"></span></span><ul class="Rk-Edit-ColorPicker">'
   677     + '<p><label><%=l10n.edit_to%></label><span class="Rk-UserColor" style="background:<%=edge.to_created_by_color%>;"></span><%=edge.to_title%></p>'
   701     + '<% print(Rkns.pickerColors.reduce(function(m,c) { return m + "<li data-color=\'" + c + "\' style=\'background: " + c + "\'></li>"},"")); %></ul><span class="Rk-Edit-ColorPicker-Text">Choose color</span></div></div>'
       
   702     + '<p><label><%=l10n.edit_from%></label><span class="Rk-UserColor" style="background:<%=edge.from_color%>;"></span><%=edge.from_title%></p>'
       
   703     + '<p><label><%=l10n.edit_to%></label><span class="Rk-UserColor" style="background:<%=edge.to_color%>;"></span><%=edge.to_title%></p>'
   678     + '<p><label><%=l10n.created_by%> </label><span class="Rk-UserColor" style="background:<%=edge.created_by_color%>;"></span><%=edge.created_by_title%></p>'
   704     + '<p><label><%=l10n.created_by%> </label><span class="Rk-UserColor" style="background:<%=edge.created_by_color%>;"></span><%=edge.created_by_title%></p>'
   679 );
   705 );
   680 
   706 
   681 Rkns.Renderer.EdgeEditor.prototype.draw = function() {
   707 Rkns.Renderer.EdgeEditor.prototype.draw = function() {
   682     var _model = this.edge_representation.model;
   708     var _model = this.edge_representation.model,
       
   709         _from_model = _model.get("from"),
       
   710         _to_model = _model.get("to"),
       
   711         _created_by = _model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER;
   683     this.editor_$
   712     this.editor_$
   684         .html(this.template({
   713         .html(this.template({
   685             edge: {
   714             edge: {
   686                 title: _model.get("title"),
   715                 title: _model.get("title"),
   687                 uri: _model.get("uri"),
   716                 uri: _model.get("uri"),
   688                 description: _model.get("description"),
   717                 description: _model.get("description"),
   689                 from_title: _model.get("from").get("title"),
   718                 color: _model.get("color") || _created_by.get("color"),
   690                 to_title: _model.get("to").get("title"),
   719                 from_title: _from_model.get("title"),
   691                 from_created_by_color: _model.get("from").get("created_by").get("color"),
   720                 to_title: _to_model.get("title"),
   692                 to_created_by_color: _model.get("to").get("created_by").get("color"),
   721                 from_color: _from_model.get("color") || (_from_model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER).get("color"),
   693                 created_by_color: _model.get("created_by").get("color"),
   722                 to_color: _to_model.get("color") || (_to_model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER).get("color"),
   694                 created_by_title: _model.get("created_by").get("title")
   723                 created_by_color: _created_by.get("color"),
       
   724                 created_by_title: _created_by.get("title")
   695             },
   725             },
   696             l10n: this.renderer.renkan.l10n
   726             l10n: this.renderer.renkan.l10n
   697         }));
   727         }));
   698     this.redraw();
   728     this.redraw();
   699     var _this = this;
   729     var _this = this;
   706         var _data = {
   736         var _data = {
   707             title: _this.editor_$.find(".Rk-Edit-Title").val(),
   737             title: _this.editor_$.find(".Rk-Edit-Title").val(),
   708             uri: _this.editor_$.find(".Rk-Edit-URI").val()
   738             uri: _this.editor_$.find(".Rk-Edit-URI").val()
   709         }
   739         }
   710         _model.set(_data);
   740         _model.set(_data);
       
   741         _this.redraw();
       
   742     });
       
   743     this.editor_$.find(".Rk-Edit-ColorPicker-Wrapper").hover(
       
   744         function() { _this.editor_$.find(".Rk-Edit-ColorPicker").show(); },
       
   745         function() { _this.editor_$.find(".Rk-Edit-ColorPicker").hide(); }
       
   746     );
       
   747     this.editor_$.find(".Rk-Edit-ColorPicker li").hover(
       
   748         function() { _this.editor_$.find(".Rk-Edit-Color").css("background", $(this).attr("data-color")); },
       
   749         function() { _this.editor_$.find(".Rk-Edit-Color").css("background", _model.get("color") || (_model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER).get("color")); }
       
   750     ).click(function() {
       
   751         _model.set("color", $(this).attr("data-color"));
   711         _this.redraw();
   752         _this.redraw();
   712     });
   753     });
   713 }
   754 }
   714 Rkns.Renderer.EdgeEditor.prototype.redraw = function() {
   755 Rkns.Renderer.EdgeEditor.prototype.redraw = function() {
   715     var _coords = this.edge_representation.paper_coords;
   756     var _coords = this.edge_representation.paper_coords;