client/js/paper-renderer.js
changeset 68 803dbeb7c919
parent 67 d341117f9370
child 69 f0873867143a
equal deleted inserted replaced
67:d341117f9370 68:803dbeb7c919
   286     	this.circle.position = this.paper_coords;
   286     	this.circle.position = this.paper_coords;
   287     	if (this.node_image) {
   287     	if (this.node_image) {
   288             this.node_image.position = this.paper_coords;
   288             this.node_image.position = this.paper_coords;
   289         }
   289         }
   290     }
   290     }
       
   291     this.last_circle_radius = this.circle_radius;
   291     
   292     
   292     this.title.content = this.model.get("title");
   293     this.title.content = this.model.get("title") || this.renderer.renkan.translate("(untitled)");
   293     this.title.position = this.paper_coords.add([0, this.circle_radius + 1.5 *Rkns.Renderer._NODE_FONT_SIZE]);
   294     this.title.position = this.paper_coords.add([0, this.circle_radius + 1.5 *Rkns.Renderer._NODE_FONT_SIZE]);
   294     this.circle.strokeColor = this.model.get("color") || (this.model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER).get("color");
   295     this.circle.strokeColor = this.model.get("color") || (this.model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER).get("color");
   295     this.edit_button.moveTo(this.paper_coords);
   296     this.edit_button.moveTo(this.paper_coords);
   296     this.remove_button.moveTo(this.paper_coords);
   297     this.remove_button.moveTo(this.paper_coords);
   297     this.link_button.moveTo(this.paper_coords);
   298     this.link_button.moveTo(this.paper_coords);
   302         _image.onload = function() {
   303         _image.onload = function() {
   303             if (_this.node_image) {
   304             if (_this.node_image) {
   304                 _this.node_image.remove();
   305                 _this.node_image.remove();
   305             }
   306             }
   306             _this.renderer.node_layer.activate();
   307             _this.renderer.node_layer.activate();
   307             var _ratio = Math.min(1, 2 * _this.circle_radius / _image.width, 2 * _this.circle_radius / _image.height );
   308             var _ratio = Math.min(2 / _image.width, 2 / _image.height );
   308             var _raster = new paper.Raster(_image);
   309             var _raster = new paper.Raster(_image);
   309             var _clip = new paper.Path.Circle([0, 0], _this.circle_radius);
   310             var _clip = new paper.Path.Circle([0, 0], 1);
   310             _raster.scale(_ratio);
   311             _raster.scale(_ratio);
   311             _this.node_image = new paper.Group(_clip, _raster);
   312             _this.node_image = new paper.Group(_clip, _raster);
   312             _this.node_image.opacity = .9;
   313             _this.node_image.opacity = .9;
   313             /* This is a workaround to allow clipping at group level
   314             /* This is a workaround to allow clipping at group level
   314              * If opacity was set to 1, paper.js would merge all clipping groups in one (known bug).
   315              * If opacity was set to 1, paper.js would merge all clipping groups in one (known bug).
   315             */
   316             */
   316             _this.node_image.clipped = true;
   317             _this.node_image.clipped = true;
   317             _this.node_image.position = _this.paper_coords;
       
   318             _this.node_image.__representation = _this;
   318             _this.node_image.__representation = _this;
       
   319 		    var square = new paper.Size(_this.circle_radius, _this.circle_radius),
       
   320 		    	topleft = _this.paper_coords.subtract(square),
       
   321 		    	bounds = new paper.Rectangle(topleft, square.multiply(2));
       
   322 		    _this.node_image.fitBounds(bounds);
   319             _clip.__representation = _this;
   323             _clip.__representation = _this;
   320             paper.view.draw();
   324             paper.view.draw();
   321         }
   325         }
   322         _image.src = _img;
   326         _image.src = _img;
   323     }
   327     }
   653     + '<p><label><%-translate("Title:")%></label><input class="Rk-Edit-Title" type="text" value="<%=node.title%>"/></p>'
   657     + '<p><label><%-translate("Title:")%></label><input class="Rk-Edit-Title" type="text" value="<%=node.title%>"/></p>'
   654     + '<p><label><%-translate("URI:")%></label><input class="Rk-Edit-URI" type="text" value="<%=node.uri%>"/><a class="Rk-Edit-Goto" href="<%=node.uri%>" target="_blank"></a></p>'
   658     + '<p><label><%-translate("URI:")%></label><input class="Rk-Edit-URI" type="text" value="<%=node.uri%>"/><a class="Rk-Edit-Goto" href="<%=node.uri%>" target="_blank"></a></p>'
   655     + '<p><label><%-translate("Description:")%></label><textarea class="Rk-Edit-Description"><%=node.description%></textarea></p>'
   659     + '<p><label><%-translate("Description:")%></label><textarea class="Rk-Edit-Description"><%=node.description%></textarea></p>'
   656     + '<p><span class="Rk-Editor-Label"><%-translate("Size:")%></span><a href="#" class="Rk-Edit-Size-Down">-</a><span class="Rk-Edit-Size-Value"><%=node.size%></span><a href="#" class="Rk-Edit-Size-Up">+</a></p>'
   660     + '<p><span class="Rk-Editor-Label"><%-translate("Size:")%></span><a href="#" class="Rk-Edit-Size-Down">-</a><span class="Rk-Edit-Size-Value"><%=node.size%></span><a href="#" class="Rk-Edit-Size-Up">+</a></p>'
   657     + '<div class="Rk-Editor-p"><span class="Rk-Editor-Label"><%-translate("Node color:")%></span><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">'
   661     + '<div class="Rk-Editor-p"><span class="Rk-Editor-Label"><%-translate("Node color:")%></span><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">'
   658     + '<% 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>'
   662     + '<% _(Rkns.pickerColors).each(function(c) { %><li data-color="<%=c%>" style="background: <%=c%>"></li><% }); %></ul><span class="Rk-Edit-ColorPicker-Text">Choose color</span></div></div>'
   659     + '<img class="Rk-Edit-ImgPreview" src="<%=node.image || node.image_placeholder%>" />'
   663     + '<img class="Rk-Edit-ImgPreview" src="<%=node.image || node.image_placeholder%>" />'
   660     + '<p><label><%-translate("Image URL:")%></label><input class="Rk-Edit-Image" type="text" value="<%=node.image%>"/></p>'
   664     + '<p><label><%-translate("Image URL:")%></label><input class="Rk-Edit-Image" type="text" value="<%=node.image%>"/></p>'
   661     + '<p><label><%-translate("Choose Image File:")%></label><input class="Rk-Edit-Image-File" type="file"/></p>'    
   665     + '<p><label><%-translate("Choose Image File:")%></label><input class="Rk-Edit-Image-File" type="file" accept="image/*"/></p>'    
   662     + '<p><span class="Rk-Editor-Label"><%-translate("Created by:")%></span> <span class="Rk-UserColor" style="background:<%=node.created_by_color%>;"></span><%=node.created_by_title%></p>'
   666     + '<p><span class="Rk-Editor-Label"><%-translate("Created by:")%></span> <span class="Rk-UserColor" style="background:<%=node.created_by_color%>;"></span><%=node.created_by_title%></p>'
   663 );
   667 );
   664 
   668 
   665 Rkns.Renderer.NodeEditor.prototype.readOnlyTemplate = Rkns._.template(
   669 Rkns.Renderer.NodeEditor.prototype.readOnlyTemplate = Rkns._.template(
   666     '<h2><span class="Rk-CloseX">&times;</span><span class="Rk-UserColor" style="background:<%=node.color%>;"></span><%-node.title%></span></h2>'
   670     '<h2><span class="Rk-CloseX">&times;</span><span class="Rk-UserColor" style="background:<%=node.color%>;"></span><%-node.title%></span></h2>'
   750         
   754         
   751         function shiftSize(n) {
   755         function shiftSize(n) {
   752         	var _newsize = n+(_model.get("size") || 0);
   756         	var _newsize = n+(_model.get("size") || 0);
   753         	_this.editor_$.find(".Rk-Edit-Size-Value").text((_newsize > 0 ? "+" : "") + _newsize);
   757         	_this.editor_$.find(".Rk-Edit-Size-Value").text((_newsize > 0 ? "+" : "") + _newsize);
   754         	_model.set("size", _newsize);
   758         	_model.set("size", _newsize);
       
   759 			paper.view.draw();
   755         }
   760         }
   756         
   761         
   757         this.editor_$.find(".Rk-Edit-Size-Down").click(function() {
   762         this.editor_$.find(".Rk-Edit-Size-Down").click(function() {
   758         	shiftSize(-1);
   763         	shiftSize(-1);
   759         	return false;
   764         	return false;
   805 
   810 
   806 Rkns.Renderer.EdgeEditor.prototype.template = Rkns._.template(
   811 Rkns.Renderer.EdgeEditor.prototype.template = Rkns._.template(
   807     '<h2><span class="Rk-CloseX">&times;</span><%-translate("Edit Edge")%></span></h2>'
   812     '<h2><span class="Rk-CloseX">&times;</span><%-translate("Edit Edge")%></span></h2>'
   808     + '<p><label><%-translate("Title:")%></label><input class="Rk-Edit-Title" type="text" value="<%=edge.title%>"/></p>'
   813     + '<p><label><%-translate("Title:")%></label><input class="Rk-Edit-Title" type="text" value="<%=edge.title%>"/></p>'
   809     + '<p><label><%-translate("URI:")%></label><input class="Rk-Edit-URI" type="text" value="<%=edge.uri%>"/><a class="Rk-Edit-Goto" href="<%=edge.uri%>" target="_blank"></a></p>'
   814     + '<p><label><%-translate("URI:")%></label><input class="Rk-Edit-URI" type="text" value="<%=edge.uri%>"/><a class="Rk-Edit-Goto" href="<%=edge.uri%>" target="_blank"></a></p>'
       
   815     + '<% if (properties.length) { %><p><label><%-translate("Choose from vocabulary:")%></label><select class="Rk-Edit-Vocabulary">'
       
   816     + '<% _(properties).each(function(ontology) { %><option class="Rk-Edit-Vocabulary-Class" value=""><%- translate(ontology.label) %></option>'
       
   817     + '<% _(ontology.properties).each(function(property) { var uri = ontology["base-uri"] + property.uri; %><option class="Rk-Edit-Vocabulary-Property" value="<%- uri %>'
       
   818     + '"<% if (uri === edge.uri) { %> selected<% } %>><%- translate(property.label) %></option>'
       
   819     + '<% }) %><% }) %></select></p><% } %>'
   810     + '<div class="Rk-Editor-p"><span class="Rk-Editor-Label"><%-translate("Edge color:")%></span><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">'
   820     + '<div class="Rk-Editor-p"><span class="Rk-Editor-Label"><%-translate("Edge color:")%></span><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">'
   811     + '<% 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>'
   821     + '<% _(Rkns.pickerColors).each(function(c) { %><li data-color="<%=c%>" style="background: <%=c%>"></li><% }); %></ul><span class="Rk-Edit-ColorPicker-Text">Choose color</span></div></div>'
   812     + '<p><span class="Rk-Editor-Label"><%-translate("From:")%></span><span class="Rk-UserColor" style="background:<%=edge.from_color%>;"></span><%=edge.from_title%></p>'
   822     + '<p><span class="Rk-Editor-Label"><%-translate("From:")%></span><span class="Rk-UserColor" style="background:<%=edge.from_color%>;"></span><%=edge.from_title%></p>'
   813     + '<p><span class="Rk-Editor-Label"><%-translate("To:")%></span><span class="Rk-UserColor" style="background:<%=edge.to_color%>;"></span><%=edge.to_title%></p>'
   823     + '<p><span class="Rk-Editor-Label"><%-translate("To:")%></span><span class="Rk-UserColor" style="background:<%=edge.to_color%>;"></span><%=edge.to_title%></p>'
   814     + '<p><span class="Rk-Editor-Label"><%-translate("Created by:")%></span><span class="Rk-UserColor" style="background:<%=edge.created_by_color%>;"></span><%=edge.created_by_title%></p>'
   824     + '<p><span class="Rk-Editor-Label"><%-translate("Created by:")%></span><span class="Rk-UserColor" style="background:<%=edge.created_by_color%>;"></span><%=edge.created_by_title%></p>'
   815 );
   825 );
   816 
   826 
   841                 from_color: _from_model.get("color") || (_from_model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER).get("color"),
   851                 from_color: _from_model.get("color") || (_from_model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER).get("color"),
   842                 to_color: _to_model.get("color") || (_to_model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER).get("color"),
   852                 to_color: _to_model.get("color") || (_to_model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER).get("color"),
   843                 created_by_color: _created_by.get("color"),
   853                 created_by_color: _created_by.get("color"),
   844                 created_by_title: _created_by.get("title")
   854                 created_by_title: _created_by.get("title")
   845             },
   855             },
   846             translate: this.renderer.renkan.translate
   856             translate: this.renderer.renkan.translate,
       
   857             properties: this.renderer.renkan.properties
   847         }));
   858         }));
   848     this.redraw();
   859     this.redraw();
   849     var _this = this;
   860     var _this = this;
   850     this.editor_$.find(".Rk-CloseX").click(function() {
   861     this.editor_$.find(".Rk-CloseX").click(function() {
   851         _this.renderer.removeRepresentation(_this);
   862         _this.renderer.removeRepresentation(_this);
   859 	            var _data = {
   870 	            var _data = {
   860 	                title: _this.editor_$.find(".Rk-Edit-Title").val(),
   871 	                title: _this.editor_$.find(".Rk-Edit-Title").val(),
   861 	                uri: _this.editor_$.find(".Rk-Edit-URI").val()
   872 	                uri: _this.editor_$.find(".Rk-Edit-URI").val()
   862 	            }
   873 	            }
   863 	            _model.set(_data);
   874 	            _model.set(_data);
   864 	            _this.redraw();
   875 	            _this.edge_representation.redraw();
       
   876 	            paper.view.draw();
   865     		}).defer();
   877     		}).defer();
   866     	}).throttle(500);
   878     	}).throttle(500);
   867     	
   879     	
   868         this.editor_$.find("input, textarea").bind("keyup change paste", onFieldChange);
   880         this.editor_$.find("input").bind("keyup change paste", onFieldChange);
       
   881         this.editor_$.find(".Rk-Edit-Vocabulary").change(function() {
       
   882         	var e = $(this),
       
   883         		v = e.val();
       
   884         	if (v) {
       
   885         		_this.editor_$.find(".Rk-Edit-Title").val(e.find(":selected").text());
       
   886         		_this.editor_$.find(".Rk-Edit-URI").val(v);
       
   887         		onFieldChange();
       
   888         	}
       
   889         });
   869         this.editor_$.find(".Rk-Edit-ColorPicker-Wrapper").hover(
   890         this.editor_$.find(".Rk-Edit-ColorPicker-Wrapper").hover(
   870             function() { _this.editor_$.find(".Rk-Edit-ColorPicker").show(); },
   891             function() { _this.editor_$.find(".Rk-Edit-ColorPicker").show(); },
   871             function() { _this.editor_$.find(".Rk-Edit-ColorPicker").hide(); }
   892             function() { _this.editor_$.find(".Rk-Edit-ColorPicker").hide(); }
   872         );
   893         );
   873         this.editor_$.find(".Rk-Edit-ColorPicker li").hover(
   894         this.editor_$.find(".Rk-Edit-ColorPicker li").hover(
  1175         _this.onScroll(_event, _delta);
  1196         _this.onScroll(_event, _delta);
  1176     });
  1197     });
  1177     this.canvas_$.dblclick(function(_event) {
  1198     this.canvas_$.dblclick(function(_event) {
  1178         _this.onDoubleClick(_event);
  1199         _this.onDoubleClick(_event);
  1179     });
  1200     });
  1180     this.canvas_$.mouseenter(function(_event) {
       
  1181         _this.onMouseEnter(_event);
       
  1182     });
       
  1183     this.canvas_$.on("dragover", function(_event) {
  1201     this.canvas_$.on("dragover", function(_event) {
  1184     	_event.stopPropagation();
  1202     	_event.stopPropagation();
  1185     	_event.preventDefault();
  1203     	_event.preventDefault();
  1186     })
  1204     })
  1187     this.canvas_$.on("drop", function(_event) {
  1205     this.canvas_$.on("drop", function(_event) {
  1197     	}
  1215     	}
  1198     	if (res["text/html"]) {
  1216     	if (res["text/html"]) {
  1199     		var snippet = Rkns.$('<div>').html(res["text/html"]);
  1217     		var snippet = Rkns.$('<div>').html(res["text/html"]);
  1200     		newNode.image = snippet.find("img").attr("src") || '';
  1218     		newNode.image = snippet.find("img").attr("src") || '';
  1201     		newNode.uri = snippet.find("a").attr("href");
  1219     		newNode.uri = snippet.find("a").attr("href");
       
  1220     		newNode.title = snippet.find("[title]").attr("title");
  1202     	}
  1221     	}
  1203     	if (res["text/uri-list"]) {
  1222     	if (res["text/uri-list"]) {
  1204     		newNode.uri = res["text/uri-list"];
  1223     		newNode.uri = res["text/uri-list"];
  1205     	}
  1224     	}
  1206     	if (res["text/x-moz-url"]) {
  1225     	if (res["text/x-moz-url"]) {
  1207     		newNode.title = (res["text/x-moz-url"].split("\n")[1] || "").trim();
  1226     		newNode.title = (res["text/x-moz-url"].split("\n")[1] || "").trim();
  1208     		if (newNode.title === newNode.uri) {
  1227     		if (newNode.title === newNode.uri) {
  1209     			newNode.title = "";
  1228     			newNode.title = "";
  1210     		}
  1229     		}
  1211     	}
  1230     	}
       
  1231     	var fields = ["title", "description", "uri", "image"];
       
  1232     	for (var i = 0; i < fields.length; i++) {
       
  1233     		var f = fields[i];
       
  1234     		if (res["text/x-iri-" + f]) {
       
  1235     			newNode[f] = res["text/x-iri-" + f];
       
  1236     		}
       
  1237     	}
  1212     	if (newNode.title || newNode.description || newNode.uri) {
  1238     	if (newNode.title || newNode.description || newNode.uri) {
  1213     		console.log(newNode);
       
  1214     		var _off = _this.canvas_$.offset(),
  1239     		var _off = _this.canvas_$.offset(),
  1215             _point = new paper.Point([
  1240             _point = new paper.Point([
  1216                 _event.originalEvent.pageX - _off.left,
  1241                 _event.originalEvent.pageX - _off.left,
  1217                 _event.originalEvent.pageY - _off.top
  1242                 _event.originalEvent.pageY - _off.top
  1218             ]),
  1243             ]),
  1656             _node = this.renkan.project.addNode(_data);
  1681             _node = this.renkan.project.addNode(_data);
  1657             this.getRepresentationByModel(_node).openEditor();
  1682             this.getRepresentationByModel(_node).openEditor();
  1658     }
  1683     }
  1659     paper.view.draw();
  1684     paper.view.draw();
  1660 }
  1685 }
  1661 
       
  1662 Rkns.Renderer.Scene.prototype.onMouseEnter = function(_event) {
       
  1663     var _newEl = this.renkan.selected_bin_item;
       
  1664     if (_newEl && !this.renkan.read_only) {
       
  1665         var _off = this.canvas_$.offset(),
       
  1666             _point = new paper.Point([
       
  1667                 _event.pageX - _off.left,
       
  1668                 _event.pageY - _off.top
       
  1669             ]),
       
  1670             _coords = this.toModelCoords(_point),
       
  1671             _data = {
       
  1672                 id: Rkns.Utils.getUID('node'),
       
  1673                 created_by: this.renkan.current_user,
       
  1674                 uri: _newEl.uri,
       
  1675                 title: _newEl.title,
       
  1676                 description: _newEl.description,
       
  1677                 image: _newEl.image,
       
  1678                 position: {
       
  1679                     x: _coords.x,
       
  1680                     y: _coords.y
       
  1681                 }
       
  1682             };
       
  1683         var _node = this.renkan.project.addNode(_data);
       
  1684         this.renkan.selected_bin_item = null;
       
  1685         this.is_dragging = true;
       
  1686         this.click_target = this.getRepresentationByModel(_node);
       
  1687     }
       
  1688 }