server/src/main/webapp/static/js/paper-renderer.js
changeset 56 a9b9e6c7be63
parent 50 7b517a54b708
equal deleted inserted replaced
55:17ff0be41a5b 56:a9b9e6c7be63
    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) {
   322     _editor.node_representation = this;
   330     _editor.node_representation = this;
   323     _editor.draw();
   331     _editor.draw();
   324 }
   332 }
   325 
   333 
   326 Rkns.Renderer.Node.prototype.select = function() {
   334 Rkns.Renderer.Node.prototype.select = function() {
   327     this.circle.strokeWidth = 3;
   335     this.circle.strokeWidth = 4;
   328     this.edit_button.show();
   336     this.edit_button.show();
   329     this.remove_button.show();
   337     this.remove_button.show();
   330     this.link_button.show();
   338     this.link_button.show();
   331     var _uri = this.model.get("uri");
   339     var _uri = this.model.get("uri");
   332     Rkns.$('.Rk-Bin-Item').each(function() {
   340     Rkns.$('.Rk-Bin-Item').each(function() {
   340 Rkns.Renderer.Node.prototype.unselect = function(_newTarget) {
   348 Rkns.Renderer.Node.prototype.unselect = function(_newTarget) {
   341     if (!_newTarget || _newTarget.node_representation !== this) {
   349     if (!_newTarget || _newTarget.node_representation !== this) {
   342         this.edit_button.hide();
   350         this.edit_button.hide();
   343         this.remove_button.hide();
   351         this.remove_button.hide();
   344         this.link_button.hide();
   352         this.link_button.hide();
   345         this.circle.strokeWidth = 1;
   353         this.circle.strokeWidth = 2;
   346         Rkns.$('.Rk-Bin-Item').removeClass("selected");
   354         Rkns.$('.Rk-Bin-Item').removeClass("selected");
   347     }
   355     }
   348 }
   356 }
   349 
   357 
   350 Rkns.Renderer.Node.prototype.highlight = function() {
   358 Rkns.Renderer.Node.prototype.highlight = function() {
   392     this.to_representation = this.renderer.getRepresentationByModel(this.model.get("to"));
   400     this.to_representation = this.renderer.getRepresentationByModel(this.model.get("to"));
   393     this.bundle = this.renderer.addToBundles(this);
   401     this.bundle = this.renderer.addToBundles(this);
   394     this.line = new paper.Path();
   402     this.line = new paper.Path();
   395     this.line.add([0,0],[0,0],[0,0]);
   403     this.line.add([0,0],[0,0],[0,0]);
   396     this.line.__representation = this;
   404     this.line.__representation = this;
       
   405     this.line.strokeWidth = 2;
   397     this.arrow = new paper.Path();
   406     this.arrow = new paper.Path();
   398     this.arrow.add([0,0],[Rkns.Renderer._ARROW_LENGTH,Rkns.Renderer._ARROW_WIDTH / 2],[0,Rkns.Renderer._ARROW_WIDTH]);
   407     this.arrow.add([0,0],[Rkns.Renderer._ARROW_LENGTH,Rkns.Renderer._ARROW_WIDTH / 2],[0,Rkns.Renderer._ARROW_WIDTH]);
   399     this.arrow.__representation = this;
   408     this.arrow.__representation = this;
   400     this.text = new paper.PointText();
   409     this.text = new paper.PointText();
   401     this.text.characterStyle = {
   410     this.text.characterStyle = {
   421         _delta = new paper.Point([- _u.y, _u.x]).multiply( 12 * _group_pos ),
   430         _delta = new paper.Point([- _u.y, _u.x]).multiply( 12 * _group_pos ),
   422         _p0b = _p0a.add(_delta), /* Adding a 4 px difference */
   431         _p0b = _p0a.add(_delta), /* Adding a 4 px difference */
   423         _p1b = _p1a.add(_delta), /* to differentiate inbound and outbound links */
   432         _p1b = _p1a.add(_delta), /* to differentiate inbound and outbound links */
   424         _a = _v.angle,
   433         _a = _v.angle,
   425         _handle = _v.divide(3),
   434         _handle = _v.divide(3),
   426         _color = this.model.get("created_by").get("color");
   435         _color = this.model.get("color") || this.model.get("color") || (this.model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER).get("color");
   427     this.paper_coords = _p0b.add(_p1b).divide(2);
   436     this.paper_coords = _p0b.add(_p1b).divide(2);
   428     this.line.strokeColor = _color;
   437     this.line.strokeColor = _color;
   429     this.line.segments[0].point = _p0a;
   438     this.line.segments[0].point = _p0a;
   430     this.line.segments[1].point = this.paper_coords;
   439     this.line.segments[1].point = this.paper_coords;
   431     this.line.segments[1].handleIn = _handle.multiply(-1);
   440     this.line.segments[1].handleIn = _handle.multiply(-1);
   455     _editor.edge_representation = this;
   464     _editor.edge_representation = this;
   456     _editor.draw();
   465     _editor.draw();
   457 }
   466 }
   458 
   467 
   459 Rkns.Renderer.Edge.prototype.select = function() {
   468 Rkns.Renderer.Edge.prototype.select = function() {
   460     this.line.strokeWidth = 3;
   469     this.line.strokeWidth = 4;
   461     this.edit_button.show();
   470     this.edit_button.show();
   462     this.remove_button.show();
   471     this.remove_button.show();
   463 }
   472 }
   464 
   473 
   465 Rkns.Renderer.Edge.prototype.unselect = function(_newTarget) {
   474 Rkns.Renderer.Edge.prototype.unselect = function(_newTarget) {
   466     if (!_newTarget || _newTarget.edge_representation !== this) {
   475     if (!_newTarget || _newTarget.edge_representation !== this) {
   467         this.edit_button.hide();
   476         this.edit_button.hide();
   468         this.remove_button.hide();
   477         this.remove_button.hide();
   469         this.line.strokeWidth = 1;
   478         this.line.strokeWidth = 2;
   470     }
   479     }
   471 }
   480 }
   472 
   481 
   473 Rkns.Renderer.Edge.prototype.mouseup = function(_event) {
   482 Rkns.Renderer.Edge.prototype.mouseup = function(_event) {
   474     if (!this.renderer.is_dragging) {
   483     if (!this.renderer.is_dragging) {
   503 
   512 
   504 Rkns.Renderer.TempEdge.prototype._init = function() {
   513 Rkns.Renderer.TempEdge.prototype._init = function() {
   505     this.renderer.edge_layer.activate();
   514     this.renderer.edge_layer.activate();
   506     this.type = "temp-edge";
   515     this.type = "temp-edge";
   507     
   516     
   508     var _color = this.project.get("users").get(this.renderer.renkan.current_user).get("color");
   517     var _color = this.project.get("users").get(this.renderer.renkan.current_user).get("color") || "#000000";
   509     this.line = new paper.Path();
   518     this.line = new paper.Path();
   510     this.line.strokeColor = _color;
   519     this.line.strokeColor = _color;
   511     this.line.add([0,0],[0,0]);
   520     this.line.add([0,0],[0,0]);
   512     this.line.__representation = this;
   521     this.line.__representation = this;
   513     this.arrow = new paper.Path();
   522     this.arrow = new paper.Path();
   595 Rkns.Renderer.NodeEditor.prototype.template = Rkns._.template(
   604 Rkns.Renderer.NodeEditor.prototype.template = Rkns._.template(
   596     '<h2><span class="Rk-CloseX">&times;</span><%=l10n.edit_node%></span></h2>'
   605     '<h2><span class="Rk-CloseX">&times;</span><%=l10n.edit_node%></span></h2>'
   597     + '<p><label><%=l10n.edit_title%></label><input class="Rk-Edit-Title" type="text" value="<%=node.title%>"/></p>'
   606     + '<p><label><%=l10n.edit_title%></label><input class="Rk-Edit-Title" type="text" value="<%=node.title%>"/></p>'
   598     + '<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>'
   607     + '<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>'
   599     + '<p><label><%=l10n.edit_description%></label><textarea class="Rk-Edit-Description"><%=node.description%></textarea></p>'
   608     + '<p><label><%=l10n.edit_description%></label><textarea class="Rk-Edit-Description"><%=node.description%></textarea></p>'
       
   609     + '<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">'
       
   610     + '<% 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>'
   600     + '<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>'
   611     + '<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>'
   601     + '<p><label><%=l10n.created_by%></label> <span class="Rk-UserColor" style="background:<%=node.created_by_color%>;"></span><%=node.created_by_title%></p>'
   612     + '<p><label><%=l10n.created_by%></label> <span class="Rk-UserColor" style="background:<%=node.created_by_color%>;"></span><%=node.created_by_title%></p>'
   602 );
   613 );
   603 
   614 
   604 Rkns.Renderer.NodeEditor.prototype.draw = function() {
   615 Rkns.Renderer.NodeEditor.prototype.draw = function() {
   605     var _model = this.node_representation.model;
   616     var _model = this.node_representation.model,
       
   617         _created_by = _model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER;
   606     this.editor_$
   618     this.editor_$
   607         .html(this.template({
   619         .html(this.template({
   608             node: {
   620             node: {
   609                 title: _model.get("title"),
   621                 title: _model.get("title"),
   610                 uri: _model.get("uri"),
   622                 uri: _model.get("uri"),
   611                 description: _model.get("description"),
   623                 description: _model.get("description"),
   612                 image: _model.get("image") || "",
   624                 image: _model.get("image") || "",
   613                 created_by_color: _model.get("created_by").get("color"),
   625                 color: _model.get("color") || _created_by.get("color"),
   614                 created_by_title: _model.get("created_by").get("title")
   626                 created_by_color: _created_by.get("color"),
       
   627                 created_by_title: _created_by.get("title")
   615             },
   628             },
   616             l10n: this.renderer.renkan.l10n
   629             l10n: this.renderer.renkan.l10n
   617         }));
   630         }));
   618     this.redraw();
   631     this.redraw();
   619     var _this = this;
   632     var _this = this;
   637     });
   650     });
   638     this.editor_$.find("img").load(function() {
   651     this.editor_$.find("img").load(function() {
   639         _this.redraw();
   652         _this.redraw();
   640     })
   653     })
   641     this.editor_$.find(".Rk-Edit-Title")[0].focus();
   654     this.editor_$.find(".Rk-Edit-Title")[0].focus();
       
   655     this.editor_$.find(".Rk-Edit-ColorPicker-Wrapper").hover(
       
   656         function() { _this.editor_$.find(".Rk-Edit-ColorPicker").show(); },
       
   657         function() { _this.editor_$.find(".Rk-Edit-ColorPicker").hide(); }
       
   658     );
       
   659     this.editor_$.find(".Rk-Edit-ColorPicker li").hover(
       
   660         function() { _this.editor_$.find(".Rk-Edit-Color").css("background", $(this).attr("data-color")); },
       
   661         function() { _this.editor_$.find(".Rk-Edit-Color").css("background", _model.get("color") || (_model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER).get("color")) }
       
   662     ).click(function() {
       
   663         _model.set("color", $(this).attr("data-color"));
       
   664         _this.redraw();
       
   665     });
   642 }
   666 }
   643 
   667 
   644 Rkns.Renderer.NodeEditor.prototype.redraw = function() {
   668 Rkns.Renderer.NodeEditor.prototype.redraw = function() {
   645     var _coords = this.node_representation.paper_coords;
   669     var _coords = this.node_representation.paper_coords;
   646     Rkns.Renderer.Utils.drawEditBox(_coords, this.editor_block, 250, 20, this.editor_$);
   670     Rkns.Renderer.Utils.drawEditBox(_coords, this.editor_block, 250, 20, this.editor_$);
   678 
   702 
   679 Rkns.Renderer.EdgeEditor.prototype.template = Rkns._.template(
   703 Rkns.Renderer.EdgeEditor.prototype.template = Rkns._.template(
   680     '<h2><span class="Rk-CloseX">&times;</span><%=l10n.edit_edge%></span></h2>'
   704     '<h2><span class="Rk-CloseX">&times;</span><%=l10n.edit_edge%></span></h2>'
   681     + '<p><label><%=l10n.edit_title%></label><input class="Rk-Edit-Title" type="text" value="<%=edge.title%>"/></p>'
   705     + '<p><label><%=l10n.edit_title%></label><input class="Rk-Edit-Title" type="text" value="<%=edge.title%>"/></p>'
   682     + '<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>'
   706     + '<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>'
   683     + '<p><label><%=l10n.edit_from%></label><span class="Rk-UserColor" style="background:<%=edge.from_created_by_color%>;"></span><%=edge.from_title%></p>'
   707     + '<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">'
   684     + '<p><label><%=l10n.edit_to%></label><span class="Rk-UserColor" style="background:<%=edge.to_created_by_color%>;"></span><%=edge.to_title%></p>'
   708     + '<% 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>'
       
   709     + '<p><label><%=l10n.edit_from%></label><span class="Rk-UserColor" style="background:<%=edge.from_color%>;"></span><%=edge.from_title%></p>'
       
   710     + '<p><label><%=l10n.edit_to%></label><span class="Rk-UserColor" style="background:<%=edge.to_color%>;"></span><%=edge.to_title%></p>'
   685     + '<p><label><%=l10n.created_by%> </label><span class="Rk-UserColor" style="background:<%=edge.created_by_color%>;"></span><%=edge.created_by_title%></p>'
   711     + '<p><label><%=l10n.created_by%> </label><span class="Rk-UserColor" style="background:<%=edge.created_by_color%>;"></span><%=edge.created_by_title%></p>'
   686 );
   712 );
   687 
   713 
   688 Rkns.Renderer.EdgeEditor.prototype.draw = function() {
   714 Rkns.Renderer.EdgeEditor.prototype.draw = function() {
   689     var _model = this.edge_representation.model;
   715     var _model = this.edge_representation.model,
       
   716         _from_model = _model.get("from"),
       
   717         _to_model = _model.get("to"),
       
   718         _created_by = _model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER;
   690     this.editor_$
   719     this.editor_$
   691         .html(this.template({
   720         .html(this.template({
   692             edge: {
   721             edge: {
   693                 title: _model.get("title"),
   722                 title: _model.get("title"),
   694                 uri: _model.get("uri"),
   723                 uri: _model.get("uri"),
   695                 description: _model.get("description"),
   724                 description: _model.get("description"),
   696                 from_title: _model.get("from").get("title"),
   725                 color: _model.get("color") || _created_by.get("color"),
   697                 to_title: _model.get("to").get("title"),
   726                 from_title: _from_model.get("title"),
   698                 from_created_by_color: _model.get("from").get("created_by").get("color"),
   727                 to_title: _to_model.get("title"),
   699                 to_created_by_color: _model.get("to").get("created_by").get("color"),
   728                 from_color: _from_model.get("color") || (_from_model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER).get("color"),
   700                 created_by_color: _model.get("created_by").get("color"),
   729                 to_color: _to_model.get("color") || (_to_model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER).get("color"),
   701                 created_by_title: _model.get("created_by").get("title")
   730                 created_by_color: _created_by.get("color"),
       
   731                 created_by_title: _created_by.get("title")
   702             },
   732             },
   703             l10n: this.renderer.renkan.l10n
   733             l10n: this.renderer.renkan.l10n
   704         }));
   734         }));
   705     this.redraw();
   735     this.redraw();
   706     var _this = this;
   736     var _this = this;
   713         var _data = {
   743         var _data = {
   714             title: _this.editor_$.find(".Rk-Edit-Title").val(),
   744             title: _this.editor_$.find(".Rk-Edit-Title").val(),
   715             uri: _this.editor_$.find(".Rk-Edit-URI").val()
   745             uri: _this.editor_$.find(".Rk-Edit-URI").val()
   716         }
   746         }
   717         _model.set(_data);
   747         _model.set(_data);
       
   748         _this.redraw();
       
   749     });
       
   750     this.editor_$.find(".Rk-Edit-ColorPicker-Wrapper").hover(
       
   751         function() { _this.editor_$.find(".Rk-Edit-ColorPicker").show(); },
       
   752         function() { _this.editor_$.find(".Rk-Edit-ColorPicker").hide(); }
       
   753     );
       
   754     this.editor_$.find(".Rk-Edit-ColorPicker li").hover(
       
   755         function() { _this.editor_$.find(".Rk-Edit-Color").css("background", $(this).attr("data-color")); },
       
   756         function() { _this.editor_$.find(".Rk-Edit-Color").css("background", _model.get("color") || (_model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER).get("color")); }
       
   757     ).click(function() {
       
   758         _model.set("color", $(this).attr("data-color"));
   718         _this.redraw();
   759         _this.redraw();
   719     });
   760     });
   720 }
   761 }
   721 Rkns.Renderer.EdgeEditor.prototype.redraw = function() {
   762 Rkns.Renderer.EdgeEditor.prototype.redraw = function() {
   722     var _coords = this.edge_representation.paper_coords;
   763     var _coords = this.edge_representation.paper_coords;