client/js/paper-renderer.js
changeset 37 db991a757015
parent 36 d249d36ecc37
child 38 092fc99f7558
equal deleted inserted replaced
36:d249d36ecc37 37:db991a757015
     1 Rkns.Renderer = {
     1 Rkns.Renderer = {
     2     _MARGIN_X: 80,
     2     _MARGIN_X: 80,
     3     _MARGIN_Y: 50,
     3     _MARGIN_Y: 50,
     4     _MIN_DRAG_DISTANCE: 2,
     4     _MIN_DRAG_DISTANCE: 2,
     5     _NODE_RADIUS: 15,
     5     _NODE_RADIUS: 25,
     6     _NODE_BUTTON_INNER: 16,
     6     _NODE_BUTTON_INNER: 26,
     7     _NODE_BUTTON_OUTER: 50,
     7     _NODE_BUTTON_OUTER: 60,
     8     _EDGE_BUTTON_INNER: 1,
     8     _EDGE_BUTTON_INNER: 1,
     9     _EDGE_BUTTON_OUTER: 40,
     9     _EDGE_BUTTON_OUTER: 40,
    10     _NODE_FONT_SIZE: 10,
    10     _NODE_FONT_SIZE: 10,
    11     _EDGE_FONT_SIZE: 9,
    11     _EDGE_FONT_SIZE: 9,
    12     _NODE_MAX_CHAR: 50,
    12     _NODE_MAX_CHAR: 50,
   203     this.remove_button = new Rkns.Renderer.NodeRemoveButton(this.renderer, {});
   203     this.remove_button = new Rkns.Renderer.NodeRemoveButton(this.renderer, {});
   204     this.remove_button.node_representation = this;
   204     this.remove_button.node_representation = this;
   205     this.link_button = new Rkns.Renderer.NodeLinkButton(this.renderer, {});
   205     this.link_button = new Rkns.Renderer.NodeLinkButton(this.renderer, {});
   206     this.link_button.node_representation = this;
   206     this.link_button.node_representation = this;
   207     this.title.paragraphStyle.justification = 'center';
   207     this.title.paragraphStyle.justification = 'center';
   208     this.title.__representation = this;
       
   209 }
   208 }
   210 
   209 
   211 Rkns.Renderer.Node.prototype.redraw = function() {
   210 Rkns.Renderer.Node.prototype.redraw = function() {
   212     var _model_coords = new paper.Point(this.model.get("position"));
   211     var _model_coords = new paper.Point(this.model.get("position"));
   213     this.paper_coords = this.renderer.toPaperCoords(_model_coords);
   212     this.paper_coords = this.renderer.toPaperCoords(_model_coords);
   214     this.circle.position = this.paper_coords;
   213     this.circle.position = this.paper_coords;
   215     this.title.content = this.model.get("title");
   214     this.title.content = this.model.get("title");
   216     this.title.position = this.paper_coords.add([0, 2 * Rkns.Renderer._NODE_RADIUS]);
   215     this.title.position = this.paper_coords.add([0, Rkns.Renderer._NODE_RADIUS + 1.5 *Rkns.Renderer._NODE_FONT_SIZE]);
   217     this.circle.strokeColor = this.model.get("created_by").get("color");
   216     this.circle.strokeColor = this.model.get("created_by").get("color");
   218     this.edit_button.moveTo(this.paper_coords);
   217     this.edit_button.moveTo(this.paper_coords);
   219     this.remove_button.moveTo(this.paper_coords);
   218     this.remove_button.moveTo(this.paper_coords);
   220     this.link_button.moveTo(this.paper_coords);
   219     this.link_button.moveTo(this.paper_coords);
       
   220     var _img = this.model.get("image");
       
   221     if (_img && _img !== this.img) {
       
   222         var _image = new Image(),
       
   223             _this = this;
       
   224         _image.onload = function() {
       
   225             if (_this.node_image) {
       
   226                 _this.node_image.remove();
       
   227             }
       
   228             _this.renderer.node_layer.activate();
       
   229             var _ratio = Math.min(1, 2 * Rkns.Renderer._NODE_RADIUS / _image.width, 2 * Rkns.Renderer._NODE_RADIUS / _image.height );
       
   230             var _raster = new paper.Raster(_image);
       
   231             var _clip = new paper.Path.Circle([0, 0], Rkns.Renderer._NODE_RADIUS);
       
   232             _raster.scale(_ratio);
       
   233             _this.node_image = new paper.Group(_clip, _raster);
       
   234             _this.node_image.opacity = .9;
       
   235             /* This is a workaround to allow clipping at group level */
       
   236             _this.node_image.clipped = true;
       
   237             _this.node_image.position = _this.paper_coords;
       
   238             _this.node_image.__representation = _this;
       
   239             paper.view.draw();
       
   240         }
       
   241         _image.src = _img;
       
   242     }
       
   243     this.img = _img;
       
   244     if (this.node_image) {
       
   245         if (!this.img) {
       
   246             this.node_image.remove();
       
   247             delete this.node_image;
       
   248         } else {
       
   249             this.node_image.position = this.paper_coords;
       
   250         }
       
   251     }
   221 }
   252 }
   222 
   253 
   223 Rkns.Renderer.Node.prototype.paperShift = function(_delta) {
   254 Rkns.Renderer.Node.prototype.paperShift = function(_delta) {
   224     var _coords = this.renderer.toModelCoords(this.paper_coords.add(_delta)),
   255     var _coords = this.renderer.toModelCoords(this.paper_coords.add(_delta)),
   225         _data = {
   256         _data = {
   234 
   265 
   235 Rkns.Renderer.Node.prototype.openEditor = function() {
   266 Rkns.Renderer.Node.prototype.openEditor = function() {
   236     this.renderer.removeRepresentationsOfType("editor");
   267     this.renderer.removeRepresentationsOfType("editor");
   237     var _editor = this.renderer.addRepresentation("NodeEditor",null);
   268     var _editor = this.renderer.addRepresentation("NodeEditor",null);
   238     _editor.node_representation = this;
   269     _editor.node_representation = this;
   239     _editor.redraw();
   270     _editor.draw();
   240 }
   271 }
   241 
   272 
   242 Rkns.Renderer.Node.prototype.select = function() {
   273 Rkns.Renderer.Node.prototype.select = function() {
   243     this.circle.strokeWidth = 3;
   274     this.circle.strokeWidth = 3;
   244     this.edit_button.show();
   275     this.edit_button.show();
   262         Rkns.$('.Rk-Bin-Item').removeClass("selected");
   293         Rkns.$('.Rk-Bin-Item').removeClass("selected");
   263     }
   294     }
   264 }
   295 }
   265 
   296 
   266 Rkns.Renderer.Node.prototype.highlight = function() {
   297 Rkns.Renderer.Node.prototype.highlight = function() {
   267     this.circle.fillColor = "#ffffc0";
   298     this.circle.fillColor = "#ffff80";
       
   299     if (this.node_image) {
       
   300         this.node_image.opacity = .5;
       
   301     }
   268 }
   302 }
   269 
   303 
   270 Rkns.Renderer.Node.prototype.unhighlight = function(_newTarget) {
   304 Rkns.Renderer.Node.prototype.unhighlight = function(_newTarget) {
   271     this.circle.fillColor = "#ffffff";
   305     this.circle.fillColor = "#ffffff";
       
   306     if (this.node_image) {
       
   307         this.node_image.opacity = .9;
       
   308     }
   272 }
   309 }
   273 
   310 
   274 Rkns.Renderer.Node.prototype.mouseup = function(_event) {
   311 Rkns.Renderer.Node.prototype.mouseup = function(_event) {
   275     if (!this.renderer.is_dragging) {
   312     if (!this.renderer.is_dragging) {
   276         this.openEditor();
   313         this.openEditor();
   283     this.edit_button.destroy();
   320     this.edit_button.destroy();
   284     this.remove_button.destroy();
   321     this.remove_button.destroy();
   285     this.link_button.destroy();
   322     this.link_button.destroy();
   286     this.circle.remove();
   323     this.circle.remove();
   287     this.title.remove();
   324     this.title.remove();
       
   325     if (this.node_image) {
       
   326         this.node_image.remove();
       
   327     }
   288 }
   328 }
   289 
   329 
   290 /* */
   330 /* */
   291 
   331 
   292 Rkns.Renderer.Edge = Rkns.Utils.inherit(Rkns.Renderer._BaseRepresentation);
   332 Rkns.Renderer.Edge = Rkns.Utils.inherit(Rkns.Renderer._BaseRepresentation);
   307     this.text.characterStyle = {
   347     this.text.characterStyle = {
   308         fontSize: Rkns.Renderer._EDGE_FONT_SIZE,
   348         fontSize: Rkns.Renderer._EDGE_FONT_SIZE,
   309         fillColor: 'black'
   349         fillColor: 'black'
   310     };
   350     };
   311     this.text.paragraphStyle.justification = 'center';
   351     this.text.paragraphStyle.justification = 'center';
   312     this.text.__representation = this;
       
   313     this.text_angle = 0;
   352     this.text_angle = 0;
   314     this.arrow_angle = 0;
   353     this.arrow_angle = 0;
   315     this.edit_button = new Rkns.Renderer.EdgeEditButton(this.renderer, {});
   354     this.edit_button = new Rkns.Renderer.EdgeEditButton(this.renderer, {});
   316     this.edit_button.edge_representation = this;
   355     this.edit_button.edge_representation = this;
   317     this.remove_button = new Rkns.Renderer.EdgeRemoveButton(this.renderer, {});
   356     this.remove_button = new Rkns.Renderer.EdgeRemoveButton(this.renderer, {});
   358 
   397 
   359 Rkns.Renderer.Edge.prototype.openEditor = function() {
   398 Rkns.Renderer.Edge.prototype.openEditor = function() {
   360     this.renderer.removeRepresentationsOfType("editor");
   399     this.renderer.removeRepresentationsOfType("editor");
   361     var _editor = this.renderer.addRepresentation("EdgeEditor",null);
   400     var _editor = this.renderer.addRepresentation("EdgeEditor",null);
   362     _editor.edge_representation = this;
   401     _editor.edge_representation = this;
   363     _editor.redraw();
   402     _editor.draw();
   364 }
   403 }
   365 
   404 
   366 Rkns.Renderer.Edge.prototype.select = function() {
   405 Rkns.Renderer.Edge.prototype.select = function() {
   367     this.line.strokeWidth = 3;
   406     this.line.strokeWidth = 3;
   368     this.edit_button.show();
   407     this.edit_button.show();
   501 Rkns.Renderer.NodeEditor.prototype.template = Rkns._.template(
   540 Rkns.Renderer.NodeEditor.prototype.template = Rkns._.template(
   502     '<h2><span class="Rk-CloseX">&times;</span><%=l10n.edit_node%></span></h2>'
   541     '<h2><span class="Rk-CloseX">&times;</span><%=l10n.edit_node%></span></h2>'
   503     + '<p><label><%=l10n.edit_title%></label><input class="Rk-Edit-Title" type="text" value="<%=node.title%>"/></p>'
   542     + '<p><label><%=l10n.edit_title%></label><input class="Rk-Edit-Title" type="text" value="<%=node.title%>"/></p>'
   504     + '<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>'
   543     + '<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>'
   505     + '<p><label><%=l10n.edit_description%></label><textarea class="Rk-Edit-Description"><%=node.description%></textarea></p>'
   544     + '<p><label><%=l10n.edit_description%></label><textarea class="Rk-Edit-Description"><%=node.description%></textarea></p>'
       
   545     + '<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>'
   506     + '<p><label><%=l10n.created_by%></label> <span class="Rk-UserColor" style="background:<%=node.created_by_color%>;"></span><%=node.created_by_title%></p>'
   546     + '<p><label><%=l10n.created_by%></label> <span class="Rk-UserColor" style="background:<%=node.created_by_color%>;"></span><%=node.created_by_title%></p>'
   507 );
   547 );
   508 
   548 
   509 Rkns.Renderer.NodeEditor.prototype.redraw = function() {
   549 Rkns.Renderer.NodeEditor.prototype.draw = function() {
   510     var _coords = this.node_representation.paper_coords,
   550     var _model = this.node_representation.model;
   511         _model = this.node_representation.model;
       
   512     this.editor_$
   551     this.editor_$
   513         .html(this.template({
   552         .html(this.template({
   514             node: {
   553             node: {
   515                 title: _model.get("title"),
   554                 title: _model.get("title"),
   516                 uri: _model.get("uri"),
   555                 uri: _model.get("uri"),
   517                 description: _model.get("description"),
   556                 description: _model.get("description"),
       
   557                 image: _model.get("image") || "",
   518                 created_by_color: _model.get("created_by").get("color"),
   558                 created_by_color: _model.get("created_by").get("color"),
   519                 created_by_title: _model.get("created_by").get("title")
   559                 created_by_title: _model.get("created_by").get("title")
   520             },
   560             },
   521             l10n: this.renderer.renkan.l10n
   561             l10n: this.renderer.renkan.l10n
   522         }))
   562         }));
   523         .show();
   563     this.redraw();
   524     Rkns.Renderer.Utils.drawEditBox(_coords, this.editor_block, 250, 20, this.editor_$);
       
   525     var _this = this;
   564     var _this = this;
   526     this.editor_$.find(".Rk-CloseX").click(function() {
   565     this.editor_$.find(".Rk-CloseX").click(function() {
   527         _this.renderer.removeRepresentation(_this);
   566         _this.renderer.removeRepresentation(_this);
   528         paper.view.draw();
   567         paper.view.draw();
   529     });
   568     });
   530     this.editor_$.find("input, textarea").bind("keyup change", function() {
   569     this.editor_$.find("input, textarea").bind("keyup change", function() {
   531         _this.editor_$.find(".Rk-Edit-Goto").attr("href",_this.editor_$.find(".Rk-Edit-URI").val());
   570         var _uri = _this.editor_$.find(".Rk-Edit-URI").val(),
       
   571             _image = _this.editor_$.find(".Rk-Edit-Image").val();
       
   572         _this.editor_$.find(".Rk-Edit-ImgPreview").attr("src", _image);
       
   573         _this.editor_$.find(".Rk-Edit-Goto").attr("href",_uri);
   532         var _data = {
   574         var _data = {
   533             title: _this.editor_$.find(".Rk-Edit-Title").val(),
   575             title: _this.editor_$.find(".Rk-Edit-Title").val(),
   534             description: _this.editor_$.find(".Rk-Edit-Description").val(),
   576             description: _this.editor_$.find(".Rk-Edit-Description").val(),
   535             uri: _this.editor_$.find(".Rk-Edit-URI").val()
   577             uri: _uri,
       
   578             image: _image
   536         }
   579         }
   537         _model.set(_data);
   580         _model.set(_data);
   538         paper.view.draw();
   581         _this.redraw();
   539     });
   582     });
   540     this.editor_$.find(".Rk-Edit-Title")[0].focus();
   583     this.editor_$.find(".Rk-Edit-Title")[0].focus();
       
   584 }
       
   585 
       
   586 Rkns.Renderer.NodeEditor.prototype.redraw = function() {
       
   587     var _coords = this.node_representation.paper_coords;
       
   588     Rkns.Renderer.Utils.drawEditBox(_coords, this.editor_block, 250, 20, this.editor_$);
       
   589     this.editor_$.show();
   541     paper.view.draw();
   590     paper.view.draw();
   542 }
   591 }
   543 
   592 
   544 Rkns.Renderer.NodeEditor.prototype.destroy = function() {
   593 Rkns.Renderer.NodeEditor.prototype.destroy = function() {
   545     this.editor_block.remove();
   594     this.editor_block.remove();
   576     + '<p><label><%=l10n.edit_from%></label><span class="Rk-UserColor" style="background:<%=edge.from_created_by_color%>;"></span><%=edge.from_title%></p>'
   625     + '<p><label><%=l10n.edit_from%></label><span class="Rk-UserColor" style="background:<%=edge.from_created_by_color%>;"></span><%=edge.from_title%></p>'
   577     + '<p><label><%=l10n.edit_to%></label><span class="Rk-UserColor" style="background:<%=edge.to_created_by_color%>;"></span><%=edge.to_title%></p>'
   626     + '<p><label><%=l10n.edit_to%></label><span class="Rk-UserColor" style="background:<%=edge.to_created_by_color%>;"></span><%=edge.to_title%></p>'
   578     + '<p><label><%=l10n.created_by%> </label><span class="Rk-UserColor" style="background:<%=edge.created_by_color%>;"></span><%=edge.created_by_title%></p>'
   627     + '<p><label><%=l10n.created_by%> </label><span class="Rk-UserColor" style="background:<%=edge.created_by_color%>;"></span><%=edge.created_by_title%></p>'
   579 );
   628 );
   580 
   629 
   581 Rkns.Renderer.EdgeEditor.prototype.redraw = function() {
   630 Rkns.Renderer.EdgeEditor.prototype.draw = function() {
   582     var _coords = this.edge_representation.paper_coords,
   631     var _model = this.edge_representation.model;
   583         _model = this.edge_representation.model;
       
   584     this.editor_$
   632     this.editor_$
   585         .html(this.template({
   633         .html(this.template({
   586             edge: {
   634             edge: {
   587                 title: _model.get("title"),
   635                 title: _model.get("title"),
   588                 uri: _model.get("uri"),
   636                 uri: _model.get("uri"),
   593                 to_created_by_color: _model.get("to").get("created_by").get("color"),
   641                 to_created_by_color: _model.get("to").get("created_by").get("color"),
   594                 created_by_color: _model.get("created_by").get("color"),
   642                 created_by_color: _model.get("created_by").get("color"),
   595                 created_by_title: _model.get("created_by").get("title")
   643                 created_by_title: _model.get("created_by").get("title")
   596             },
   644             },
   597             l10n: this.renderer.renkan.l10n
   645             l10n: this.renderer.renkan.l10n
   598         }))
   646         }));
   599         .show();
   647     this.redraw();
   600     Rkns.Renderer.Utils.drawEditBox(_coords, this.editor_block, 250, 5, this.editor_$);
       
   601     var _this = this;
   648     var _this = this;
   602     this.editor_$.find(".Rk-CloseX").click(function() {
   649     this.editor_$.find(".Rk-CloseX").click(function() {
   603         _this.renderer.removeRepresentation(_this);
   650         _this.renderer.removeRepresentation(_this);
   604         paper.view.draw();
   651         paper.view.draw();
   605     });
   652     });
   608         var _data = {
   655         var _data = {
   609             title: _this.editor_$.find(".Rk-Edit-Title").val(),
   656             title: _this.editor_$.find(".Rk-Edit-Title").val(),
   610             uri: _this.editor_$.find(".Rk-Edit-URI").val()
   657             uri: _this.editor_$.find(".Rk-Edit-URI").val()
   611         }
   658         }
   612         _model.set(_data);
   659         _model.set(_data);
   613         paper.view.draw();
   660         _this.redraw();
   614     });
   661     });
       
   662 }
       
   663 Rkns.Renderer.EdgeEditor.prototype.redraw = function() {
       
   664     var _coords = this.edge_representation.paper_coords;
       
   665     Rkns.Renderer.Utils.drawEditBox(_coords, this.editor_block, 250, 5, this.editor_$);
       
   666     this.editor_$.show();
   615     paper.view.draw();
   667     paper.view.draw();
   616 }
   668 }
   617 
   669 
   618 Rkns.Renderer.EdgeEditor.prototype.destroy = function() {
   670 Rkns.Renderer.EdgeEditor.prototype.destroy = function() {
   619     this.editor_block.remove();
   671     this.editor_block.remove();
  1286                 id: Rkns.Utils.getUID('node'),
  1338                 id: Rkns.Utils.getUID('node'),
  1287                 created_by: this.renkan.current_user,
  1339                 created_by: this.renkan.current_user,
  1288                 uri: _newEl.uri,
  1340                 uri: _newEl.uri,
  1289                 title: _newEl.title,
  1341                 title: _newEl.title,
  1290                 description: _newEl.description,
  1342                 description: _newEl.description,
       
  1343                 image: _newEl.image,
  1291                 position: {
  1344                 position: {
  1292                     x: _coords.x,
  1345                     x: _coords.x,
  1293                     y: _coords.y
  1346                     y: _coords.y
  1294                 }
  1347                 }
  1295             };
  1348             };