diff -r eb97351d5e9b -r f978d70a9e63 client/js/paper-renderer.js --- a/client/js/paper-renderer.js Tue Apr 23 22:14:59 2013 +0200 +++ b/client/js/paper-renderer.js Tue Apr 23 22:15:10 2013 +0200 @@ -12,28 +12,28 @@ _MIN_SCALE: 1/20, _MAX_SCALE: 20, _AUTOSCALE_MARGIN: 50, + _MOUSEMOVE_RATE: 80, + _DOUBLETAP_DELAY: 800, + _DOUBLETAP_DISTANCE: 20*20, _USER_PLACEHOLDER : function(_renkan) { - return { - color: _renkan.options.default_user_color, - title: _renkan.translate("(unknown user)"), - get: function(attr) { - return this[attr] || false; - } - } + return { + color: _renkan.options.default_user_color, + title: _renkan.translate("(unknown user)"), + get: function(attr) { + return this[attr] || false; + } + } }, _BOOKMARKLET_CODE: function(_renkan) { - return "(function(a,b,c,d,e,f,h,i,j,k,l,m,n,o,p,q,r){a=document;b=a.body;c=a.location.href;j='draggable';m='text/x-iri-';d=a.createElement('div');d.innerHTML='" - + _renkan.translate("Drag items from this website, drop them in Renkan").replace(/ /g,"_") - + "

'.replace(/_/g,String.fromCharCode(32));b.appendChild(d);e=[{r:/https?:\\/\\/[^\\/]*twitter\\.com\\//,s:'.tweet',n:'twitter'},{r:/https?:\\/\\/[^\\/]*google\\.[^\\/]+\\//,s:'.g',n:'google'},{r:/https?:\\/\\/[^\\/]*lemonde\\.fr\\//,s:'[data-vr-contentbox]',n:'lemonde'}];f=false;e.forEach(function(g){if(g.r.test(c)){f=g;}});if(f){h=function(){Array.prototype.forEach.call(a.querySelectorAll(f.s),function(i){i[j]=true;k=i.style;k.borderWidth='2px';k.borderColor='#909';k.borderStyle='solid';k.backgroundColor='rgba(200,0,180,.1)';})};window.setInterval(h,500);h();};a.addEventListener('dragstart',function(k){l=k.dataTransfer;l.setData(m+'source-uri',c);l.setData(m+'source-title',a.title);n=k.target;if(f){o=n;while(!o.attributes[j]){o=o.parentNode;if(o==b){break;}}}if(f&&o.attributes[j]){p=o.cloneNode(true);l.setData(m+'specific-site',f.n)}else{q=a.getSelection();if(q.type==='Range'||!q.type){p=q.getRangeAt(0).cloneContents();}else{p=n.cloneNode();}}r=a.createElement('div');r.appendChild(p);l.setData('text/x-iri-selected-text',r.textContent.trim());l.setData('text/x-iri-selected-html',r.innerHTML);},false);})();" - } -} - -Rkns.Renderer.Utils = { - shortenText : function(_text, _maxlength) { - return (_text.length > _maxlength ? (_text.substr(0,_maxlength) + '…') : _text); - }, + return "(function(a,b,c,d,e,f,h,i,j,k,l,m,n,o,p,q,r){a=document;b=a.body;c=a.location.href;j='draggable';m='text/x-iri-';d=a.createElement('div');d.innerHTML='" + + _renkan.translate("Drag items from this website, drop them in Renkan").replace(/ /g,"_") + + "

'.replace(/_/g,String.fromCharCode(32));b.appendChild(d);e=[{r:/https?:\\/\\/[^\\/]*twitter\\.com\\//,s:'.tweet',n:'twitter'},{r:/https?:\\/\\/[^\\/]*google\\.[^\\/]+\\//,s:'.g',n:'google'},{r:/https?:\\/\\/[^\\/]*lemonde\\.fr\\//,s:'[data-vr-contentbox]',n:'lemonde'}];f=false;e.forEach(function(g){if(g.r.test(c)){f=g;}});if(f){h=function(){Array.prototype.forEach.call(a.querySelectorAll(f.s),function(i){i[j]=true;k=i.style;k.borderWidth='2px';k.borderColor='#909';k.borderStyle='solid';k.backgroundColor='rgba(200,0,180,.1)';})};window.setInterval(h,500);h();};a.addEventListener('dragstart',function(k){l=k.dataTransfer;l.setData(m+'source-uri',c);l.setData(m+'source-title',a.title);n=k.target;if(f){o=n;while(!o.attributes[j]){o=o.parentNode;if(o==b){break;}}}if(f&&o.attributes[j]){p=o.cloneNode(true);l.setData(m+'specific-site',f.n)}else{q=a.getSelection();if(q.type==='Range'||!q.type){p=q.getRangeAt(0).cloneContents();}else{p=n.cloneNode();}}r=a.createElement('div');r.appendChild(p);l.setData('text/x-iri-selected-text',r.textContent.trim());l.setData('text/x-iri-selected-html',r.innerHTML);},false);})();" + }, + shortenText : function(_text, _maxlength) { + return (_text.length > _maxlength ? (_text.substr(0,_maxlength) + '…') : _text); + }, drawEditBox : function(_options, _coords, _path, _xmargin, _selector) { - _selector.css({ + _selector.css({ width: ( _options.tooltip_width - 2* _options.tooltip_padding ), }) var _height = _selector.outerHeight() + 2* _options.tooltip_padding, @@ -41,14 +41,13 @@ _left = _coords.x + _isLeft * ( _xmargin + _options.tooltip_arrow_length ), _right = _coords.x + _isLeft * ( _xmargin + _options.tooltip_arrow_length + _options.tooltip_width ), _top = _coords.y - _height / 2; + if (_top + _height > (paper.view.size.height - _options.tooltip_margin)) { + _top = Math.max( paper.view.size.height - _options.tooltip_margin, _coords.y + _options.tooltip_arrow_width / 2 ) - _height; + } if (_top < _options.tooltip_margin) { _top = Math.min( _options.tooltip_margin, _coords.y - _options.tooltip_arrow_width / 2 ); } var _bottom = _top + _height; - if (_bottom > (paper.view.size.height - _options.tooltip_margin)) { - _bottom = Math.max( paper.view.size.height - _options.tooltip_margin, _coords.y + _options.tooltip_arrow_width / 2 ); - _top = _bottom - _height; - } _path.segments[0].point = _path.segments[7].point = _coords.add([_isLeft * _xmargin, 0]); @@ -74,105 +73,7 @@ left: (_options.tooltip_padding + Math.min(_left, _right)), top: (_options.tooltip_padding + _top) }); - }, - sector : function(_repr, _inR, _outR, _startAngle, _endAngle, _padding, _imgsrc, _caption) { - var _options = _repr.renderer.renkan.options, - _startRads = _startAngle * Math.PI / 180, - _endRads = _endAngle * Math.PI / 180, - _img = new Image(), - _span = _endRads - _startRads, - _startdx = - Math.sin(_startRads), - _startdy = Math.cos(_startRads), - _startXIn = Math.cos(_startRads) * _inR + _padding * _startdx, - _startYIn = Math.sin(_startRads) * _inR + _padding * _startdy, - _startXOut = Math.cos(_startRads) * _outR + _padding * _startdx, - _startYOut = Math.sin(_startRads) * _outR + _padding * _startdy, - _enddx = - Math.sin(_endRads), - _enddy = Math.cos(_endRads), - _endXIn = Math.cos(_endRads) * _inR - _padding * _enddx, - _endYIn = Math.sin(_endRads) * _inR - _padding * _enddy, - _endXOut = Math.cos(_endRads) * _outR - _padding * _enddx, - _endYOut = Math.sin(_endRads) * _outR - _padding * _enddy, - _centerR = (_inR + _outR)/2, - _centerRads = (_startRads + _endRads) / 2, - _centerX = Math.cos(_centerRads) * _centerR, - _centerY = Math.sin(_centerRads) * _centerR, - _centerXIn = Math.cos(_centerRads) * _inR, - _centerXOut = Math.cos(_centerRads) * _outR, - _centerYIn = Math.sin(_centerRads) * _inR, - _centerYOut = Math.sin(_centerRads) * _outR, - _textX = Math.cos(_centerRads) * (_outR + 3), - _textY = Math.sin(_centerRads) * (_outR + _options.buttons_label_font_size) + _options.buttons_label_font_size / 2, - _segments = []; - _repr.renderer.buttons_layer.activate(); - var _path = new paper.Path(); - _path.add([_startXIn, _startYIn]); - _path.arcTo([_centerXIn, _centerYIn], [_endXIn, _endYIn]); - _path.lineTo([_endXOut, _endYOut]); - _path.arcTo([_centerXOut, _centerYOut], [_startXOut, _startYOut]); - _path.fillColor = _options.buttons_background; - _path.opacity = .5; - _path.closed = true; - _path.__representation = _repr; - var _text = new paper.PointText(_textX,_textY); - _text.characterStyle = { - fontSize: _options.buttons_label_font_size, - fillColor: _options.buttons_label_color - }; - if (_textX > 2) { - _text.paragraphStyle.justification = 'left'; - } else if (_textX < -2) { - _text.paragraphStyle.justification = 'right'; - } else { - _text.paragraphStyle.justification = 'center'; - } - _text.visible = false; - var _visible = false, - _restPos = new paper.Point(-200, -200), - _grp = new paper.Group([_path, _text]), - _delta = _grp.position, - _imgdelta = new paper.Point([_centerX, _centerY]), - _currentPos = new paper.Point(0,0); - _text.content = _caption; - _grp.visible = false; - _grp.position = _restPos; - var _res = { - show: function() { - _visible = true; - _grp.position = _currentPos.add(_delta); - _grp.visible = true; - }, - moveTo: function(_point) { - _currentPos = _point; - if (_visible) { - _grp.position = _point.add(_delta); - } - }, - hide: function() { - _visible = false; - _grp.visible = false; - _grp.position = _restPos; - }, - select: function() { - _path.opacity = .8; - _text.visible = true; - }, - unselect: function() { - _path.opacity = .5; - _text.visible = false; - }, - destroy: function() { - _grp.remove(); - } - } - _img.onload = function() { - var _h = _img.height; - var _raster = new paper.Raster(_img); - _raster.position = _imgdelta.add(_grp.position).subtract(_delta); - _grp.addChild(_raster); - } - _img.src = _imgsrc; - return _res + return _path; } } @@ -191,11 +92,19 @@ this._removeBinding = function() { _renderer.removeRepresentation(_this); _(function() { - _renderer.redraw() - }).defer(); + _renderer.redraw() + }).defer(); + } + this._selectBinding = function() { + _this.select(); + } + this._unselectBinding = function() { + _this.unselect(); } this.model.on("change", this._changeBinding ); this.model.on("remove", this._removeBinding ); + this.model.on("select", this._selectBinding ); + this.model.on("unselect", this._unselectBinding ); } } } @@ -212,20 +121,36 @@ Rkns.Renderer._BaseRepresentation.prototype.hide = function() {} -Rkns.Renderer._BaseRepresentation.prototype.select = function() {} +Rkns.Renderer._BaseRepresentation.prototype.select = function() { + if (this.model) { + this.model.trigger("selected"); + } +} -Rkns.Renderer._BaseRepresentation.prototype.unselect = function() {} +Rkns.Renderer._BaseRepresentation.prototype.unselect = function() { + if (this.model) { + this.model.trigger("unselected"); + } +} Rkns.Renderer._BaseRepresentation.prototype.highlight = function() {} Rkns.Renderer._BaseRepresentation.prototype.unhighlight = function() {} -Rkns.Renderer._BaseRepresentation.prototype.mouseup = function() {} +Rkns.Renderer._BaseRepresentation.prototype.mousedown = function() {} + +Rkns.Renderer._BaseRepresentation.prototype.mouseup = function() { + if (this.model) { + this.model.trigger("clicked"); + } +} Rkns.Renderer._BaseRepresentation.prototype.destroy = function() { if (this.model) { this.model.off("change", this._changeBinding ); this.model.off("remove", this._removeBinding ); + this.model.off("select", this._selectBinding ); + this.model.off("unselect", this._unselectBinding ); } } @@ -269,83 +194,113 @@ this.type = "Node"; this.circle = new paper.Path.Circle([0, 0], 1); this.circle.__representation = this; - this.circle.fillColor = this.options.node_fill_color; if (this.options.show_node_circles) { - this.circle.strokeWidth = this.options.node_stroke_width; - this.h_ratio = 1; + this.circle.strokeWidth = this.options.node_stroke_width; + this.h_ratio = 1; } else { - this.circle.opacity = .01; - this.h_ratio = 0; + this.h_ratio = 0; } - this.title = new paper.PointText([0,0]); - this.title.characterStyle = { - font: this.options.node_label_font, - fontSize: this.options.node_label_font_size, - fillColor: this.options.node_label_color - }; + this.title = Rkns.$('
').appendTo(this.renderer.labels_$); if (this.options.editor_mode) { - this.buttons = [ - new Rkns.Renderer.NodeEditButton(this.renderer, null), - new Rkns.Renderer.NodeRemoveButton(this.renderer, null), - new Rkns.Renderer.NodeLinkButton(this.renderer, null), - new Rkns.Renderer.NodeEnlargeButton(this.renderer, null), - new Rkns.Renderer.NodeShrinkButton(this.renderer, null) - ]; - for (var i = 0; i < this.buttons.length; i++) { - this.buttons[i].source_representation = this; - } + this.normal_buttons = [ + new Rkns.Renderer.NodeEditButton(this.renderer, null), + new Rkns.Renderer.NodeRemoveButton(this.renderer, null), + new Rkns.Renderer.NodeLinkButton(this.renderer, null), + new Rkns.Renderer.NodeEnlargeButton(this.renderer, null), + new Rkns.Renderer.NodeShrinkButton(this.renderer, null) + ]; + this.pending_delete_buttons = [ + new Rkns.Renderer.NodeRevertButton(this.renderer, null) + ]; + this.all_buttons = this.normal_buttons.concat(this.pending_delete_buttons) + for (var i = 0; i < this.all_buttons.length; i++) { + this.all_buttons[i].source_representation = this; + } + this.active_buttons = []; } else { - this.buttons = []; + this.active_buttons = this.all_buttons = []; } this.last_circle_radius = 1; - this.title.paragraphStyle.justification = 'center'; if (this.renderer.minimap) { - this.renderer.minimap.node_layer.activate(); - this.minimap_circle = new paper.Path.Circle([0, 0], 1); - this.renderer.minimap.node_group.addChild(this.minimap_circle); + this.renderer.minimap.node_layer.activate(); + this.minimap_circle = new paper.Path.Circle([0, 0], 1); + this.minimap_circle.__representation = this.renderer.minimap.miniframe.__representation; + this.renderer.minimap.node_group.addChild(this.minimap_circle); } } Rkns.Renderer.Node.prototype.redraw = function(_dontRedrawEdges) { var _model_coords = new paper.Point(this.model.get("position")), - _baseRadius = this.options.node_size_base * Math.exp((this.model.get("size") || 0) * Rkns.Renderer._NODE_SIZE_STEP); + _baseRadius = this.options.node_size_base * Math.exp((this.model.get("size") || 0) * Rkns.Renderer._NODE_SIZE_STEP); if (!this.is_dragging || !this.paper_coords) { this.paper_coords = this.renderer.toPaperCoords(_model_coords); } this.circle_radius = _baseRadius * this.renderer.scale; if (this.last_circle_radius !== this.circle_radius) { - this.buttons.forEach(function(b) { - b.setSectorSize(); - }); - var square = new paper.Size(this.circle_radius, this.circle_radius), - topleft = this.paper_coords.subtract(square), - bounds = new paper.Rectangle(topleft, square.multiply(2)); - this.circle.fitBounds(bounds); - if (this.node_image) { - this.node_image.fitBounds(bounds); - } + this.all_buttons.forEach(function(b) { + b.setSectorSize(); + }); + var square = new paper.Size(this.circle_radius, this.circle_radius), + topleft = this.paper_coords.subtract(square), + bounds = new paper.Rectangle(topleft, square.multiply(2)); + this.circle.fitBounds(bounds); + if (this.node_image) { + this.node_image.fitBounds(bounds); + } } else { - this.circle.position = this.paper_coords; - if (this.node_image) { + this.circle.position = this.paper_coords; + if (this.node_image) { this.node_image.position = this.paper_coords; } } this.last_circle_radius = this.circle_radius; - var _text = this.model.get("title") || this.renkan.translate("(untitled)"); - this.title.content = Rkns.Renderer.Utils.shortenText(_text, this.options.node_label_max_length); - - this.title.position = this.paper_coords.add([ - 0, - this.circle_radius * this.h_ratio + this.options.node_label_font_size + this.options.node_label_distance - ]); + var old_act_btn = this.active_buttons; + + if (this.model.get("delete_scheduled")) { + var opacity = .5; + this.active_buttons = this.pending_delete_buttons; + this.circle.dashArray = [2,2]; + } else { + var opacity = 1; + this.active_buttons = this.normal_buttons; + this.circle.dashArray = null; + } + + if (this.selected && this.renderer.isEditable()) { + if (old_act_btn !== this.active_buttons) { + old_act_btn.forEach(function(b) { + b.hide(); + }); + } + this.active_buttons.forEach(function(b) { + b.show(); + }); + } + + if (this.node_image) { + this.node_image.opacity = this.highlighted ? opacity * .5 : (opacity - .01); + } + + this.circle.fillColor = this.highlighted ? this.options.highlighted_node_fill_color : this.options.node_fill_color; + + this.circle.opacity = this.options.show_node_circles ? opacity : .01; + + var _text = this.model.get("title") || this.renkan.translate(this.options.label_untitled_nodes) || ""; + _text = Rkns.Renderer.shortenText(_text, this.options.node_label_max_length); + this.title.text(_text); + this.title.css({ + left: this.paper_coords.x, + top: this.paper_coords.y + this.circle_radius * this.h_ratio + this.options.node_label_distance, + opacity: opacity + }) var _color = this.model.get("color") || (this.model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER(this.renkan)).get("color"); this.circle.strokeColor = _color; var _pc = this.paper_coords; - this.buttons.forEach(function(b) { - b.moveTo(_pc); - }); + this.all_buttons.forEach(function(b) { + b.moveTo(_pc); + }); var _img = this.model.get("image"); if (_img && _img !== this.img) { var _image = new Image(), @@ -357,29 +312,29 @@ _this.renderer.node_layer.activate(); var _ratio = Math.min(2 / _image.width, 2 / _image.height ); if (!_this.options.show_node_circles) { - _this.h_ratio = Math.min(1, _image.height / _image.width); + _this.h_ratio = Math.min(1, _image.height / _image.width); } var _raster = new paper.Raster(_image); if (_this.options.clip_node_images) { - var _clip = new paper.Path.Circle([0, 0], 1); - _raster.scale(_ratio); - _this.node_image = new paper.Group(_clip, _raster); - _this.node_image.opacity = .99; - /* This is a workaround to allow clipping at group level - * If opacity was set to 1, paper.js would merge all clipping groups in one (known bug). - */ - _this.node_image.clipped = true; - _clip.__representation = _this; + var _clip = new paper.Path.Circle([0, 0], 1); + _raster.scale(_ratio); + _this.node_image = new paper.Group(_clip, _raster); + _this.node_image.opacity = .99; + /* This is a workaround to allow clipping at group level + * If opacity was set to 1, paper.js would merge all clipping groups in one (known bug). + */ + _this.node_image.clipped = true; + _clip.__representation = _this; } else { - _this.node_image = _raster; + _this.node_image = _raster; } _this.node_image.__representation = _this; - var square = new paper.Size(_this.circle_radius, _this.circle_radius), - topleft = _this.paper_coords.subtract(square), - bounds = new paper.Rectangle(topleft, square.multiply(2)); - _this.node_image.fitBounds(bounds); + var square = new paper.Size(_this.circle_radius, _this.circle_radius), + topleft = _this.paper_coords.subtract(square), + bounds = new paper.Rectangle(topleft, square.multiply(2)); + _this.node_image.fitBounds(bounds); _this.redraw(); - paper.view.draw(); + paper.view.draw(); } _image.src = _img; } @@ -390,34 +345,34 @@ } if (this.renderer.minimap) { - this.minimap_circle.fillColor = _color; - var minipos = this.renderer.toMinimapCoords(_model_coords), - miniradius = this.renderer.minimap.scale * _baseRadius, - minisize = new paper.Size([miniradius, miniradius]); - this.minimap_circle.fitBounds(minipos.subtract(minisize), minisize.multiply(2)); + this.minimap_circle.fillColor = _color; + var minipos = this.renderer.toMinimapCoords(_model_coords), + miniradius = this.renderer.minimap.scale * _baseRadius, + minisize = new paper.Size([miniradius, miniradius]); + this.minimap_circle.fitBounds(minipos.subtract(minisize), minisize.multiply(2)); } if (!_dontRedrawEdges) { - Rkns._.each(this.project.get("edges").filter(function (ed) { return ((ed.to === this.model) || (ed.from === this.model));}), function(edge, index, list){ - var repr = this.renderer.getRepresentationByModel(edge); - if(repr != null && typeof repr.from_representation.paper_coords !== "undefined" && typeof repr.to_representation.paper_coords !== "undefined") { - repr.redraw(); - } - }, this); + Rkns._.each(this.project.get("edges").filter(function (ed) { return ((ed.to === this.model) || (ed.from === this.model));}), function(edge, index, list){ + var repr = this.renderer.getRepresentationByModel(edge); + if(repr != null && typeof repr.from_representation.paper_coords !== "undefined" && typeof repr.to_representation.paper_coords !== "undefined") { + repr.redraw(); + } + }, this); } } Rkns.Renderer.Node.prototype.paperShift = function(_delta) { - if (this.options.editor_mode) { - if (!this.renkan.read_only) { - this.is_dragging = true; - this.paper_coords = this.paper_coords.add(_delta); - this.redraw(); - } - } else { - this.renderer.paperShift(_delta); - } + if (this.options.editor_mode) { + if (!this.renkan.read_only) { + this.is_dragging = true; + this.paper_coords = this.paper_coords.add(_delta); + this.redraw(); + } + } else { + this.renderer.paperShift(_delta); + } } Rkns.Renderer.Node.prototype.openEditor = function() { @@ -428,56 +383,64 @@ } Rkns.Renderer.Node.prototype.select = function() { + this.selected = true; this.circle.strokeWidth = this.options.selected_node_stroke_width; if (this.renderer.isEditable()) { - this.buttons.forEach(function(b) { - b.show(); - }); + this.active_buttons.forEach(function(b) { + b.show(); + }); } var _uri = this.model.get("uri"); if (_uri) { - Rkns.$('.Rk-Bin-Item').each(function() { - var _el = Rkns.$(this); - if (_el.attr("data-uri") == _uri) { - _el.addClass("selected"); - } - }); + Rkns.$('.Rk-Bin-Item').each(function() { + var _el = Rkns.$(this); + if (_el.attr("data-uri") == _uri) { + _el.addClass("selected"); + } + }); } if (!this.options.editor_mode) { this.openEditor(); } if (this.renderer.minimap) { - this.minimap_circle.strokeWidth = this.options.minimap_highlight_weight; - this.minimap_circle.strokeColor = this.options.minimap_highlight_color; + this.minimap_circle.strokeWidth = this.options.minimap_highlight_weight; + this.minimap_circle.strokeColor = this.options.minimap_highlight_color; } + this.super("select"); } Rkns.Renderer.Node.prototype.unselect = function(_newTarget) { if (!_newTarget || _newTarget.source_representation !== this) { - this.buttons.forEach(function(b) { - b.hide(); - }); - this.circle.strokeWidth = this.options.node_stroke_width; + this.selected = false; + this.all_buttons.forEach(function(b) { + b.hide(); + }); + this.circle.strokeWidth = this.options.node_stroke_width; Rkns.$('.Rk-Bin-Item').removeClass("selected"); - if (this.renderer.minimap) { - this.minimap_circle.strokeColor = undefined; - } + if (this.renderer.minimap) { + this.minimap_circle.strokeColor = undefined; + } + this.super("unselect"); } } + +Rkns.Renderer.Node.prototype.highlight = function() { + if (this.highlighted) { + return; + } + this.highlighted = true; + this.redraw(); + this.renderer.throttledPaperDraw(); +} -Rkns.Renderer.Node.prototype.highlight = function() { - this.circle.fillColor = this.options.highlighted_node_fill_color; - if (this.node_image) { - this.node_image.opacity = .5; +Rkns.Renderer.Node.prototype.unhighlight = function() { + if (!this.highlighted) { + return; } -} - -Rkns.Renderer.Node.prototype.unhighlight = function(_newTarget) { - this.circle.fillColor = this.options.node_fill_color; - if (this.node_image) { - this.node_image.opacity = .99; - } + this.highlighted = false; + this.redraw(); + this.renderer.throttledPaperDraw(); } Rkns.Renderer.Node.prototype.saveCoords = function() { @@ -489,16 +452,25 @@ } }; if (this.renderer.isEditable()) { - this.model.set(_data); + this.model.set(_data); } } -Rkns.Renderer.Node.prototype.mouseup = function(_event) { - if (this.renderer.isEditable() && this.renderer.is_dragging) { +Rkns.Renderer.Node.prototype.mousedown = function(_event, _isTouch) { + if (_isTouch) { + this.renderer.unselectAll(); + this.select(); + } +} + +Rkns.Renderer.Node.prototype.mouseup = function(_event, _isTouch) { + if (this.renderer.is_dragging && this.renderer.isEditable()) { this.saveCoords(); - } - else { - this.openEditor(); + } else { + if (!_isTouch && !this.model.get("delete_scheduled")) { + this.openEditor(); + } + this.model.trigger("clicked"); } this.renderer.click_target = null; this.renderer.is_dragging = false; @@ -507,13 +479,13 @@ Rkns.Renderer.Node.prototype.destroy = function(_event) { this.super("destroy"); - this.buttons.forEach(function(b) { - b.destroy(); - }); + this.all_buttons.forEach(function(b) { + b.destroy(); + }); this.circle.remove(); this.title.remove(); if (this.renderer.minimap) { - this.minimap_circle.remove(); + this.minimap_circle.remove(); } if (this.node_image) { this.node_image.remove(); @@ -536,32 +508,36 @@ this.line.strokeWidth = this.options.edge_stroke_width; this.arrow = new paper.Path(); this.arrow.add( - [ 0, 0 ], - [ this.options.edge_arrow_length, this.options.edge_arrow_width / 2 ], - [ 0, this.options.edge_arrow_width ] - ); + [ 0, 0 ], + [ this.options.edge_arrow_length, this.options.edge_arrow_width / 2 ], + [ 0, this.options.edge_arrow_width ] + ); this.arrow.__representation = this; - this.text = new paper.PointText(); - this.text.characterStyle = { - font: this.options.edge_label_font, - fontSize: this.options.edge_label_font_size, - fillColor: this.options.edge_label_color - }; - this.text.paragraphStyle.justification = 'center'; - this.text_angle = 0; + this.text = Rkns.$('
').appendTo(this.renderer.labels_$); this.arrow_angle = 0; if (this.options.editor_mode) { - this.edit_button = new Rkns.Renderer.EdgeEditButton(this.renderer, null); - this.edit_button.source_representation = this; - this.remove_button = new Rkns.Renderer.EdgeRemoveButton(this.renderer, null); - this.remove_button.source_representation = this; + this.normal_buttons = [ + new Rkns.Renderer.EdgeEditButton(this.renderer, null), + new Rkns.Renderer.EdgeRemoveButton(this.renderer, null), + ]; + this.pending_delete_buttons = [ + new Rkns.Renderer.EdgeRevertButton(this.renderer, null) + ]; + this.all_buttons = this.normal_buttons.concat(this.pending_delete_buttons) + for (var i = 0; i < this.all_buttons.length; i++) { + this.all_buttons[i].source_representation = this; + } + this.active_buttons = []; + } else { + this.active_buttons = this.all_buttons = []; } if (this.renderer.minimap) { - this.renderer.minimap.edge_layer.activate(); - this.minimap_line = new paper.Path(); - this.minimap_line.add([0,0],[0,0]); - this.minimap_line.strokeWidth = 1; + this.renderer.minimap.edge_layer.activate(); + this.minimap_line = new paper.Path(); + this.minimap_line.add([0,0],[0,0]); + this.minimap_line.__representation = this.renderer.minimap.miniframe.__representation; + this.minimap_line.strokeWidth = 1; } } @@ -569,7 +545,7 @@ this.from_representation = this.renderer.getRepresentationByModel(this.model.get("from")); this.to_representation = this.renderer.getRepresentationByModel(this.model.get("to")); if (!this.from_representation || !this.to_representation) { - return; + return; } var _p0a = this.from_representation.paper_coords, _p1a = this.to_representation.paper_coords, @@ -585,8 +561,31 @@ _textdelta = _ortho.multiply(this.options.edge_label_distance), _handle = _v.divide(3), _color = this.model.get("color") || this.model.get("color") || (this.model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER(this.renkan)).get("color"); + + if (this.model.get("delete_scheduled") || this.from_representation.model.get("delete_scheduled") || this.to_representation.model.get("delete_scheduled")) { + var opacity = .5; + this.line.dashArray = [2, 2]; + } else { + var opacity = 1; + this.line.dashArray = null; + } + + var old_act_btn = this.active_buttons; + + this.active_buttons = this.model.get("delete_scheduled") ? this.pending_delete_buttons : this.normal_buttons; + + if (this.selected && this.renderer.isEditable() && old_act_btn !== this.active_buttons) { + old_act_btn.forEach(function(b) { + b.hide(); + }); + this.active_buttons.forEach(function(b) { + b.show(); + }); + } + this.paper_coords = _p0b.add(_p1b).divide(2); this.line.strokeColor = _color; + this.line.opacity = opacity; this.line.segments[0].point = _p0a; this.line.segments[1].point = this.paper_coords; this.line.segments[1].handleIn = _handle.multiply(-1); @@ -594,6 +593,7 @@ this.line.segments[2].point = _p1a; this.arrow.rotate(_a - this.arrow_angle); this.arrow.fillColor = _color; + this.arrow.opacity = opacity; this.arrow.position = this.paper_coords; this.arrow_angle = _a; if (_a > 90) { @@ -604,20 +604,29 @@ _a += 180; _textdelta = _textdelta.multiply(-1); } - this.text.rotate(_a - this.text_angle); - var _text = this.model.get("title"); - this.text.content = Rkns.Renderer.Utils.shortenText(_text, this.options.edge_label_max_length); - this.text.position = this.paper_coords.add(_textdelta); + var _text = this.model.get("title") || this.renkan.translate(this.options.label_untitled_edges) || ""; + _text = Rkns.Renderer.shortenText(_text, this.options.node_label_max_length); + this.text.text(_text); + var _textpos = this.paper_coords.add(_textdelta); + this.text.css({ + left: _textpos.x, + top: _textpos.y, + transform: "rotate(" + _a + "deg)", + "-moz-transform": "rotate(" + _a + "deg)", + "-webkit-transform": "rotate(" + _a + "deg)", + opacity: opacity + }); this.text_angle = _a; - if (this.options.editor_mode) { - this.edit_button.moveTo(this.paper_coords); - this.remove_button.moveTo(this.paper_coords); - } + + var _pc = this.paper_coords; + this.all_buttons.forEach(function(b) { + b.moveTo(_pc); + }); if (this.renderer.minimap) { - this.minimap_line.strokeColor = _color; - this.minimap_line.segments[0].point = this.renderer.toMinimapCoords(new paper.Point(this.from_representation.model.get("position"))); - this.minimap_line.segments[1].point = this.renderer.toMinimapCoords(new paper.Point(this.to_representation.model.get("position"))); + this.minimap_line.strokeColor = _color; + this.minimap_line.segments[0].point = this.renderer.toMinimapCoords(new paper.Point(this.from_representation.model.get("position"))); + this.minimap_line.segments[1].point = this.renderer.toMinimapCoords(new paper.Point(this.to_representation.model.get("position"))); } } @@ -629,48 +638,64 @@ } Rkns.Renderer.Edge.prototype.select = function() { + this.selected = true; this.line.strokeWidth = this.options.selected_edge_stroke_width; if (this.renderer.isEditable()) { - this.edit_button.show(); - this.remove_button.show(); - } + this.active_buttons.forEach(function(b) { + b.show(); + }); + } if (!this.options.editor_mode) { this.openEditor(); } + this.super("select"); } Rkns.Renderer.Edge.prototype.unselect = function(_newTarget) { if (!_newTarget || _newTarget.source_representation !== this) { - if (this.options.editor_mode) { - this.edit_button.hide(); - this.remove_button.hide(); + this.selected = false; + if (this.options.editor_mode) { + this.all_buttons.forEach(function(b) { + b.hide(); + }); } this.line.strokeWidth = this.options.edge_stroke_width; + this.super("unselect"); } } -Rkns.Renderer.Edge.prototype.mouseup = function(_event) { - if (!this.renkan.read_only) { - if (this.renderer.is_dragging) { - this.from_representation.saveCoords(); - this.to_representation.saveCoords(); - } else { +Rkns.Renderer.Edge.prototype.mousedown = function(_event, _isTouch) { + if (_isTouch) { + this.renderer.unselectAll(); + this.select(); + } +} + +Rkns.Renderer.Edge.prototype.mouseup = function(_event, _isTouch) { + if (!this.renkan.read_only && this.renderer.is_dragging) { + this.from_representation.saveCoords(); + this.to_representation.saveCoords(); + this.from_representation.is_dragging = false; + this.to_representation.is_dragging = false; + } else { + if (!_isTouch) { this.openEditor(); } + this.model.trigger("clicked"); } this.renderer.click_target = null; this.renderer.is_dragging = false; } Rkns.Renderer.Edge.prototype.paperShift = function(_delta) { - if (this.options.editor_mode) { - if (!this.options.read_only) { - this.from_representation.paperShift(_delta); - this.to_representation.paperShift(_delta); - } - } else { - this.renderer.paperShift(_delta); - } + if (this.options.editor_mode) { + if (!this.options.read_only) { + this.from_representation.paperShift(_delta); + this.to_representation.paperShift(_delta); + } + } else { + this.renderer.paperShift(_delta); + } } Rkns.Renderer.Edge.prototype.destroy = function() { @@ -679,12 +704,11 @@ this.arrow.remove(); this.text.remove(); if (this.renderer.minimap) { - this.minimap_line.remove(); + this.minimap_line.remove(); } - if (this.options.editor_mode) { - this.edit_button.destroy(); - this.remove_button.destroy(); - } + this.all_buttons.forEach(function(b) { + b.destroy(); + }); var _this = this; this.bundle.edges = Rkns._(this.bundle.edges).reject(function(_edge) { return _edge === _this; @@ -697,20 +721,22 @@ Rkns.Renderer.TempEdge.prototype._init = function() { this.renderer.edge_layer.activate(); - this.type = "temp-edge"; + this.type = "Temp-edge"; var _color = (this.project.get("users").get(this.renkan.current_user) || Rkns.Renderer._USER_PLACEHOLDER(this.renkan)).get("color"); this.line = new paper.Path(); this.line.strokeColor = _color; + this.line.dashArray = [4, 2]; + this.line.strokeWidth = this.options.selected_edge_stroke_width; this.line.add([0,0],[0,0]); this.line.__representation = this; this.arrow = new paper.Path(); this.arrow.fillColor = _color; this.arrow.add( - [ 0, 0 ], - [ this.options.edge_arrow_length, this.options.edge_arrow_width / 2 ], - [ 0, this.options.edge_arrow_width ] - ); + [ 0, 0 ], + [ this.options.edge_arrow_length, this.options.edge_arrow_width / 2 ], + [ 0, this.options.edge_arrow_width ] + ); this.arrow.__representation = this; this.arrow_angle = 0; } @@ -728,34 +754,38 @@ } Rkns.Renderer.TempEdge.prototype.paperShift = function(_delta) { - if (!this.renderer.isEditable()) { - this.renderer.removeRepresentation(_this); - paper.view.draw(); - return; - } + if (!this.renderer.isEditable()) { + this.renderer.removeRepresentation(_this); + paper.view.draw(); + return; + } this.end_pos = this.end_pos.add(_delta); var _hitResult = paper.project.hitTest(this.end_pos); this.renderer.findTarget(_hitResult); this.redraw(); } -Rkns.Renderer.TempEdge.prototype.mouseup = function(_event) { +Rkns.Renderer.TempEdge.prototype.mouseup = function(_event, _isTouch) { var _hitResult = paper.project.hitTest(_event.point), _model = this.from_representation.model, _endDrag = true; if (_hitResult && typeof _hitResult.item.__representation !== "undefined") { var _target = _hitResult.item.__representation; - if (_target.type === "Node" && _model !== _target.model) { - var _data = { - id: Rkns.Utils.getUID('edge'), - created_by: this.renkan.current_user, - from: _model.get("_id"), - to: _target.model.get("_id") - }; - if (this.renderer.isEditable()) { - this.project.addEdge(_data); + if (_target.type.substr(0,4) === "Node") { + var _destmodel = _target.model || _target.source_representation.model; + if (_model !== _destmodel) { + var _data = { + id: Rkns.Utils.getUID('edge'), + created_by: this.renkan.current_user, + from: _model, + to: _destmodel + }; + if (this.renderer.isEditable()) { + this.project.addEdge(_data); + } } } + if (_model === _target.model || (_target.source_representation && _target.source_representation.model === _model)) { _endDrag = false; this.renderer.is_dragging = true; @@ -776,184 +806,9 @@ /* */ -Rkns.Renderer.NodeEditor = Rkns.Utils.inherit(Rkns.Renderer._BaseRepresentation); - -Rkns.Renderer.NodeEditor.prototype._init = function() { - this.renderer.buttons_layer.activate(); - this.type = "editor"; - this.editor_block = new paper.Path(); - var _pts = Rkns._(Rkns._.range(8)).map(function() {return [0,0]}); - this.editor_block.add.apply(this.editor_block, _pts); - this.editor_block.strokeWidth = this.options.tooltip_border_width; - this.editor_block.strokeColor = this.options.tooltip_border_color; - this.editor_block.opacity = .8; - this.editor_$ = Rkns.$('
') - .appendTo(this.renderer.editor_$) - .css({ - position: "absolute", - opacity: .8 - }) - .hide(); -} - -Rkns.Renderer.NodeEditor.prototype.template = Rkns._.template( - '

×<%-translate("Edit Node")%>

' - + '

' - + '

' - + '

' - + '

<%-translate("Size:")%>-<%-node.size%>+

' - + '
<%-translate("Node color:")%>
    ' - + '<% _(Rkns.pickerColors).each(function(c) { %>
  • <% }); %>
<%- translate("Choose color") %>
' - + '' - + '

' - + '

' - + '<% if (node.has_creator) { %>

<%-translate("Created by:")%> <%- Rkns.Renderer.Utils.shortenText(node.created_by_title, 25) %>

<% } %>' -); - -Rkns.Renderer.NodeEditor.prototype.readOnlyTemplate = Rkns._.template( - '

×' - + '<% if (node.uri) { %><% } %><%-node.title%><% if (node.uri) { %><% } %>

' - + '<% if (node.uri) { %>

<%-node.short_uri%>

<% } %>' - + '

<%-node.description%>

' - + '<% if (node.has_creator) { %>

<%-translate("Created by:")%><%- Rkns.Renderer.Utils.shortenText(node.created_by_title, 25) %>

<% } %>' -); +Rkns.Renderer._BaseEditor = Rkns.Utils.inherit(Rkns.Renderer._BaseRepresentation); -Rkns.Renderer.NodeEditor.prototype.draw = function() { - var _model = this.source_representation.model, - _created_by = _model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER(this.renkan), - _template = (this.renderer.isEditable() ? this.template : this.readOnlyTemplate ), - _image_placeholder = this.options.static_url + "img/image-placeholder.png", - _size = (_model.get("size") || 0); - this.editor_$ - .html(_template({ - node: { - has_creator: !!_model.get("created_by"), - title: _model.get("title"), - uri: _model.get("uri"), - short_uri: Rkns.Renderer.Utils.shortenText((_model.get("uri") || "").replace(/^(https?:\/\/)?(www\.)?/,'').replace(/\/$/,''),40), - description: _model.get("description"), - image: _model.get("image") || "", - image_placeholder: _image_placeholder, - color: _model.get("color") || _created_by.get("color"), - created_by_color: _created_by.get("color"), - created_by_title: _created_by.get("title"), - size: (_size > 0 ? "+" : "") + _size - }, - translate: this.renkan.translate - })); - this.redraw(); - var _this = this, - closeEditor = function() { - _this.renderer.removeRepresentation(_this); - paper.view.draw(); - } - - this.editor_$.find(".Rk-CloseX").click(closeEditor); - - if (this.renderer.isEditable()) { - - var onFieldChange = Rkns._(function() { - Rkns._(function() { - if (_this.renderer.isEditable()) { - var _uri = _this.editor_$.find(".Rk-Edit-URI").val(), - _image = _this.editor_$.find(".Rk-Edit-Image").val(); - _this.editor_$.find(".Rk-Edit-ImgPreview").attr("src", _image || _image_placeholder); - _this.editor_$.find(".Rk-Edit-Goto").attr("href",_uri); - var _data = { - title: _this.editor_$.find(".Rk-Edit-Title").val(), - description: _this.editor_$.find(".Rk-Edit-Description").val(), - uri: _uri, - image: _image - } - _model.set(_data); - _this.redraw(); - } else { - closeEditor(); - } - - }).defer(); - }).throttle(500); - - this.editor_$.find("input, textarea").bind("change keyup paste", onFieldChange); - - this.editor_$.find(".Rk-Edit-Image-File").bind("change", function() { - if (this.files.length) { - var f = this.files[0], - fr = new FileReader(); - if (f.type.substr(0,5) !== "image") { - alert(_this.renkan.translate("This file is not an image")); - return; - } - if (f.size > (Rkns.Renderer._IMAGE_MAX_KB * 1024)) { - alert(_this.renkan.translate("Image size must be under ")+Rkns.Renderer._IMAGE_MAX_KB+_this.renkan.translate("KB")); - return; - } - fr.onload = function(e) { - _this.editor_$.find(".Rk-Edit-Image").val(e.target.result); - onFieldChange(); - } - fr.readAsDataURL(f); - } - }); - this.editor_$.find(".Rk-Edit-Title")[0].focus(); - this.editor_$.find(".Rk-Edit-ColorPicker-Wrapper").hover( - function() { _this.editor_$.find(".Rk-Edit-ColorPicker").show(); }, - function() { _this.editor_$.find(".Rk-Edit-ColorPicker").hide(); } - ); - this.editor_$.find(".Rk-Edit-ColorPicker li").hover( - function() { _this.editor_$.find(".Rk-Edit-Color").css("background", $(this).attr("data-color")); }, - function() { _this.editor_$.find(".Rk-Edit-Color").css("background", _model.get("color") || (_model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER(this.renkan)).get("color")) } - ).click(function() { - if (_this.renderer.isEditable()) { - _model.set("color", $(this).attr("data-color")); - paper.view.draw(); - } else { - closeEditor(); - } - }); - - function shiftSize(n) { - if (_this.renderer.isEditable()) { - var _newsize = n+(_model.get("size") || 0); - _this.editor_$.find(".Rk-Edit-Size-Value").text((_newsize > 0 ? "+" : "") + _newsize); - _model.set("size", _newsize); - paper.view.draw(); - } else { - closeEditor(); - } - } - - this.editor_$.find(".Rk-Edit-Size-Down").click(function() { - shiftSize(-1); - return false; - }); - this.editor_$.find(".Rk-Edit-Size-Up").click(function() { - shiftSize(1); - return false; - }); - } - this.editor_$.find("img").load(function() { - _this.redraw(); - }); -} - -Rkns.Renderer.NodeEditor.prototype.redraw = function() { - var _coords = this.source_representation.paper_coords; - Rkns.Renderer.Utils.drawEditBox(this.options, _coords, this.editor_block, this.source_representation.circle_radius * .75, this.editor_$); - this.editor_$.show(); - paper.view.draw(); -} - -Rkns.Renderer.NodeEditor.prototype.destroy = function() { - this.editor_block.remove(); - this.editor_$.detach(); -} - -/* */ - -Rkns.Renderer.EdgeEditor = Rkns.Utils.inherit(Rkns.Renderer._BaseRepresentation); - -Rkns.Renderer.EdgeEditor.prototype._init = function() { +Rkns.Renderer._BaseEditor.prototype._init = function() { this.renderer.buttons_layer.activate(); this.type = "editor"; this.editor_block = new paper.Path(); @@ -971,21 +826,207 @@ .hide(); } +Rkns.Renderer._BaseEditor.prototype.destroy = function() { + this.editor_block.remove(); + this.editor_$.remove(); +} + +/* */ + +Rkns.Renderer.NodeEditor = Rkns.Utils.inherit(Rkns.Renderer._BaseEditor); + +Rkns.Renderer.NodeEditor.prototype.template = Rkns._.template( + '

×<%-renkan.translate("Edit Node")%>

' + + '

' + + '

' + + '

' + + '

<%-renkan.translate("Size:")%>-<%-node.size%>+

' + + '
<%-renkan.translate("Node color:")%>
    ' + + '<% _(Rkns.pickerColors).each(function(c) { %>
  • <% }); %>
<%- renkan.translate("Choose color") %>
' + + '' + + '

' + + '

' + + '<% if (node.has_creator) { %>

<%-renkan.translate("Created by:")%> <%- Rkns.Renderer.shortenText(node.created_by_title, 25) %>

<% } %>' +); + +Rkns.Renderer.NodeEditor.prototype.readOnlyTemplate = Rkns._.template( + '

×' + + '<% if (node.uri) { %><% } %><%-node.title%><% if (node.uri) { %><% } %>

' + + '<% if (node.uri) { %>

<%-node.short_uri%>

<% } %>' + + '

<%-node.description%>

' + + '<% if (node.image) { %><% } %>' + + '<% if (node.has_creator) { %>

<%-renkan.translate("Created by:")%><%- Rkns.Renderer.shortenText(node.created_by_title, 25) %>

<% } %>' +); + +Rkns.Renderer.NodeEditor.prototype.draw = function() { + var _model = this.source_representation.model, + _created_by = _model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER(this.renkan), + _template = (this.renderer.isEditable() ? this.template : this.readOnlyTemplate ), + _image_placeholder = this.options.static_url + "img/image-placeholder.png", + _size = (_model.get("size") || 0); + this.editor_$ + .html(_template({ + node: { + has_creator: !!_model.get("created_by"), + title: _model.get("title"), + uri: _model.get("uri"), + short_uri: Rkns.Renderer.shortenText((_model.get("uri") || "").replace(/^(https?:\/\/)?(www\.)?/,'').replace(/\/$/,''),40), + description: _model.get("description"), + image: _model.get("image") || "", + image_placeholder: _image_placeholder, + color: _model.get("color") || _created_by.get("color"), + created_by_color: _created_by.get("color"), + created_by_title: _created_by.get("title"), + size: (_size > 0 ? "+" : "") + _size + }, + renkan: this.renkan + })); + this.redraw(); + var _this = this, + closeEditor = function() { + _this.renderer.removeRepresentation(_this); + paper.view.draw(); + } + + this.editor_$.find(".Rk-CloseX").click(closeEditor); + + if (this.renderer.isEditable()) { + + var onFieldChange = Rkns._(function() { + Rkns._(function() { + if (_this.renderer.isEditable()) { + var _uri = _this.editor_$.find(".Rk-Edit-URI").val(), + _image = _this.editor_$.find(".Rk-Edit-Image").val(); + _this.editor_$.find(".Rk-Edit-ImgPreview").attr("src", _image || _image_placeholder); + _this.editor_$.find(".Rk-Edit-Goto").attr("href",_uri); + var _data = { + title: _this.editor_$.find(".Rk-Edit-Title").val(), + description: _this.editor_$.find(".Rk-Edit-Description").val(), + uri: _uri, + image: _image + } + _model.set(_data); + _this.redraw(); + } else { + closeEditor(); + } + + }).defer(); + }).throttle(500); + + this.editor_$.on("keyup", function(_e) { + if (_e.keyCode === 27) { + closeEditor(); + } + }); + + this.editor_$.find("input, textarea").on("change keyup paste", onFieldChange); + + this.editor_$.find(".Rk-Edit-Image-File").change(function() { + if (this.files.length) { + var f = this.files[0], + fr = new FileReader(); + if (f.type.substr(0,5) !== "image") { + alert(_this.renkan.translate("This file is not an image")); + return; + } + if (f.size > (Rkns.Renderer._IMAGE_MAX_KB * 1024)) { + alert(_this.renkan.translate("Image size must be under ")+Rkns.Renderer._IMAGE_MAX_KB+_this.renkan.translate("KB")); + return; + } + fr.onload = function(e) { + _this.editor_$.find(".Rk-Edit-Image").val(e.target.result); + onFieldChange(); + } + fr.readAsDataURL(f); + } + }); + this.editor_$.find(".Rk-Edit-Title")[0].focus(); + + var _picker = _this.editor_$.find(".Rk-Edit-ColorPicker"); + + this.editor_$.find(".Rk-Edit-ColorPicker-Wrapper").hover( + function(_e) { + _e.preventDefault(); + _picker.show(); + }, + function(_e) { + _e.preventDefault(); + _picker.hide(); + } + ); + + _picker.find("li").hover( + function(_e) { + _e.preventDefault(); + _this.editor_$.find(".Rk-Edit-Color").css("background", $(this).attr("data-color")); + }, + function(_e) { + _e.preventDefault(); + _this.editor_$.find(".Rk-Edit-Color").css("background", _model.get("color") || (_model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER(_this.renkan)).get("color")) + } + ).click(function(_e) { + _e.preventDefault(); + if (_this.renderer.isEditable()) { + _model.set("color", $(this).attr("data-color")); + _picker.hide(); + paper.view.draw(); + } else { + closeEditor(); + } + }); + + function shiftSize(n) { + if (_this.renderer.isEditable()) { + var _newsize = n+(_model.get("size") || 0); + _this.editor_$.find(".Rk-Edit-Size-Value").text((_newsize > 0 ? "+" : "") + _newsize); + _model.set("size", _newsize); + paper.view.draw(); + } else { + closeEditor(); + } + } + + this.editor_$.find(".Rk-Edit-Size-Down").click(function() { + shiftSize(-1); + return false; + }); + this.editor_$.find(".Rk-Edit-Size-Up").click(function() { + shiftSize(1); + return false; + }); + } + this.editor_$.find("img").load(function() { + _this.redraw(); + }); +} + +Rkns.Renderer.NodeEditor.prototype.redraw = function() { + var _coords = this.source_representation.paper_coords; + Rkns.Renderer.drawEditBox(this.options, _coords, this.editor_block, this.source_representation.circle_radius * .75, this.editor_$); + this.editor_$.show(); + paper.view.draw(); +} + +/* */ + +Rkns.Renderer.EdgeEditor = Rkns.Utils.inherit(Rkns.Renderer._BaseEditor); + Rkns.Renderer.EdgeEditor.prototype.template = Rkns._.template( - '

×<%-translate("Edit Edge")%>

' - + '

' - + '

' - + '<% if (properties.length) { %>

' + + '

' + + '<% if (properties.length) { %>

<% } %>' - + '
<%-translate("Edge color:")%>
    ' - + '<% _(Rkns.pickerColors).each(function(c) { %>
  • <% }); %>
<%- translate("Choose color") %>
' - + '

<%- translate("Change edge direction") %>

' - + '

<%-translate("From:")%><%- Rkns.Renderer.Utils.shortenText(edge.from_title, 25) %>

' - + '

<%-translate("To:")%><%- Rkns.Renderer.Utils.shortenText(edge.to_title, 25) %>

' - + '<% if (edge.has_creator) { %>

<%-translate("Created by:")%><%- Rkns.Renderer.Utils.shortenText(edge.created_by_title, 25) %>

<% } %>' + + '
<%-renkan.translate("Edge color:")%>
    ' + + '<% _(Rkns.pickerColors).each(function(c) { %>
  • <% }); %>
<%- renkan.translate("Choose color") %>
' + + '

<%- renkan.translate("Change edge direction") %>

' + + '

<%-renkan.translate("From:")%><%- Rkns.Renderer.shortenText(edge.from_title, 25) %>

' + + '

<%-renkan.translate("To:")%><%- Rkns.Renderer.shortenText(edge.to_title, 25) %>

' + + '<% if (edge.has_creator) { %>

<%-renkan.translate("Created by:")%><%- Rkns.Renderer.shortenText(edge.created_by_title, 25) %>

<% } %>' ); Rkns.Renderer.EdgeEditor.prototype.readOnlyTemplate = Rkns._.template( @@ -993,9 +1034,9 @@ + '<% if (edge.uri) { %><% } %><%-edge.title%><% if (edge.uri) { %><% } %>' + '<% if (edge.uri) { %>

<%-edge.short_uri%>

<% } %>' + '

<%-edge.description%>

' - + '

<%-translate("From:")%><%- Rkns.Renderer.Utils.shortenText(edge.from_title, 25) %>

' - + '

<%-translate("To:")%><%- Rkns.Renderer.Utils.shortenText(edge.to_title, 25) %>

' - + '<% if (edge.has_creator) { %>

<%-translate("Created by:")%><%- Rkns.Renderer.Utils.shortenText(edge.created_by_title, 25) %>

<% } %>' + + '

<%-renkan.translate("From:")%><%- Rkns.Renderer.shortenText(edge.from_title, 25) %>

' + + '

<%-renkan.translate("To:")%><%- Rkns.Renderer.shortenText(edge.to_title, 25) %>

' + + '<% if (edge.has_creator) { %>

<%-renkan.translate("Created by:")%><%- Rkns.Renderer.shortenText(edge.created_by_title, 25) %>

<% } %>' ); Rkns.Renderer.EdgeEditor.prototype.draw = function() { @@ -1007,10 +1048,10 @@ this.editor_$ .html(_template({ edge: { - has_creator: !!_model.get("created_by"), + has_creator: !!_model.get("created_by"), title: _model.get("title"), uri: _model.get("uri"), - short_uri: Rkns.Renderer.Utils.shortenText((_model.get("uri") || "").replace(/^(https?:\/\/)?(www\.)?/,'').replace(/\/$/,''),40), + short_uri: Rkns.Renderer.shortenText((_model.get("uri") || "").replace(/^(https?:\/\/)?(www\.)?/,'').replace(/\/$/,''),40), description: _model.get("description"), color: _model.get("color") || _created_by.get("color"), from_title: _from_model.get("title"), @@ -1020,107 +1061,127 @@ created_by_color: _created_by.get("color"), created_by_title: _created_by.get("title") }, - translate: this.renkan.translate, + renkan: this.renkan, properties: this.options.properties })); this.redraw(); var _this = this, - closeEditor = function() { - _this.renderer.removeRepresentation(_this); - paper.view.draw(); - } + closeEditor = function() { + _this.renderer.removeRepresentation(_this); + paper.view.draw(); + } this.editor_$.find(".Rk-CloseX").click(closeEditor); if (this.renderer.isEditable()) { - - var onFieldChange = Rkns._(function() { - Rkns._(function() { - if (_this.renderer.isEditable()) { - _this.editor_$.find(".Rk-Edit-Goto").attr("href",_this.editor_$.find(".Rk-Edit-URI").val()); - var _data = { - title: _this.editor_$.find(".Rk-Edit-Title").val(), - uri: _this.editor_$.find(".Rk-Edit-URI").val() - } - _model.set(_data); - paper.view.draw(); - } else { - closeEditor(); - } - }).defer(); - }).throttle(500); - - this.editor_$.find("input").bind("keyup change paste", onFieldChange); + + var onFieldChange = Rkns._(function() { + Rkns._(function() { + if (_this.renderer.isEditable()) { + _this.editor_$.find(".Rk-Edit-Goto").attr("href",_this.editor_$.find(".Rk-Edit-URI").val()); + var _data = { + title: _this.editor_$.find(".Rk-Edit-Title").val(), + uri: _this.editor_$.find(".Rk-Edit-URI").val() + } + _model.set(_data); + paper.view.draw(); + } else { + closeEditor(); + } + }).defer(); + }).throttle(500); + + this.editor_$.on("keyup", function(_e) { + if (_e.keyCode === 27) { + closeEditor(); + } + }); + + this.editor_$.find("input").on("keyup change paste", onFieldChange); + this.editor_$.find(".Rk-Edit-Vocabulary").change(function() { - var e = $(this), - v = e.val(); - if (v) { - _this.editor_$.find(".Rk-Edit-Title").val(e.find(":selected").text()); - _this.editor_$.find(".Rk-Edit-URI").val(v); - onFieldChange(); - } + var e = $(this), + v = e.val(); + if (v) { + _this.editor_$.find(".Rk-Edit-Title").val(e.find(":selected").text()); + _this.editor_$.find(".Rk-Edit-URI").val(v); + onFieldChange(); + } }); this.editor_$.find(".Rk-Edit-Direction").click(function() { - if (_this.renderer.isEditable()) { - _model.set({ - from: _model.get("to"), - to: _model.get("from") - }); - _this.draw(); - } else { - closeEditor(); - } + if (_this.renderer.isEditable()) { + _model.set({ + from: _model.get("to"), + to: _model.get("from") + }); + _this.draw(); + } else { + closeEditor(); + } }); + + var _picker = _this.editor_$.find(".Rk-Edit-ColorPicker"); + this.editor_$.find(".Rk-Edit-ColorPicker-Wrapper").hover( - function() { _this.editor_$.find(".Rk-Edit-ColorPicker").show(); }, - function() { _this.editor_$.find(".Rk-Edit-ColorPicker").hide(); } + function(_e) { + _e.preventDefault(); + _picker.show(); + }, + function(_e) { + _e.preventDefault(); + _picker.hide(); + } ); - this.editor_$.find(".Rk-Edit-ColorPicker li").hover( - function() { _this.editor_$.find(".Rk-Edit-Color").css("background", $(this).attr("data-color")); }, - function() { _this.editor_$.find(".Rk-Edit-Color").css("background", _model.get("color") || (_model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER(this.renkan)).get("color")); } - ).click(function() { - if (_this.renderer.isEditable()) { - _model.set("color", $(this).attr("data-color")); - paper.view.draw(); + + _picker.find("li").hover( + function(_e) { + _e.preventDefault(); + _this.editor_$.find(".Rk-Edit-Color").css("background", $(this).attr("data-color")); + }, + function(_e) { + _e.preventDefault(); + _this.editor_$.find(".Rk-Edit-Color").css("background", _model.get("color") || (_model.get("created_by") || Rkns.Renderer._USER_PLACEHOLDER(_this.renkan)).get("color")) + } + ).click(function(_e) { + _e.preventDefault(); + if (_this.renderer.isEditable()) { + _model.set("color", $(this).attr("data-color")); + _picker.hide(); + paper.view.draw(); } else { - closeEditor(); - } + closeEditor(); + } }); } } Rkns.Renderer.EdgeEditor.prototype.redraw = function() { var _coords = this.source_representation.paper_coords; - Rkns.Renderer.Utils.drawEditBox(this.options, _coords, this.editor_block, 5, this.editor_$); + Rkns.Renderer.drawEditBox(this.options, _coords, this.editor_block, 5, this.editor_$); this.editor_$.show(); paper.view.draw(); } -Rkns.Renderer.EdgeEditor.prototype.destroy = function() { - this.editor_block.remove(); - this.editor_$.detach(); -} - /* */ Rkns.Renderer._NodeButton = Rkns.Utils.inherit(Rkns.Renderer._BaseButton); Rkns.Renderer._NodeButton.prototype.setSectorSize = function() { - var sectorInner = this.source_representation.circle_radius; - if (sectorInner !== this.lastSectorInner) { - if (this.sector) { - this.sector.destroy(); - } - this.sector = Rkns.Renderer.Utils.sector( - this, 1 + sectorInner, - Rkns.Renderer._NODE_BUTTON_WIDTH + sectorInner, - this.startAngle, - this.endAngle, - 1, - this.options.static_url + this.imageFile, - this.renkan.translate(this.text) - ); - this.lastSectorInner = sectorInner; - } + var sectorInner = this.source_representation.circle_radius; + if (sectorInner !== this.lastSectorInner) { + if (this.sector) { + this.sector.destroy(); + } + this.sector = this.renderer.drawSector( + this, 1 + sectorInner, + Rkns.Renderer._NODE_BUTTON_WIDTH + sectorInner, + this.startAngle, + this.endAngle, + 1, + this.imageName, + this.renkan.translate(this.text) + ); + this.lastSectorInner = sectorInner; + } } /* */ @@ -1130,9 +1191,9 @@ Rkns.Renderer.NodeEditButton.prototype._init = function() { this.type = "Node-edit-button"; this.lastSectorInner = 0; - this.startAngle = -225; - this.endAngle = -135; - this.imageFile = 'img/edit.png'; + this.startAngle = -135; + this.endAngle = -45; + this.imageName = "edit"; this.text = "Edit"; } @@ -1149,16 +1210,44 @@ Rkns.Renderer.NodeRemoveButton.prototype._init = function() { this.type = "Node-remove-button"; this.lastSectorInner = 0; - this.startAngle = -45; - this.endAngle = 45; - this.imageFile = 'img/remove.png'; + this.startAngle = 0; + this.endAngle = 90; + this.imageName = "remove"; this.text = "Remove"; } Rkns.Renderer.NodeRemoveButton.prototype.mouseup = function() { + this.renderer.click_target = null; + this.renderer.is_dragging = false; this.renderer.removeRepresentationsOfType("editor"); - if (this.renderer.isEditable() && confirm(this.renkan.translate('Do you really wish to remove node ') + '"' + this.source_representation.model.get("title") + '"?')) { - this.project.removeNode(this.source_representation.model); + if (this.renderer.isEditable()) { + var delid = Rkns.Utils.getUID("delete"); + this.renderer.delete_list.push({ + id: delid, + time: new Date().valueOf() + this.options.element_delete_delay + }); + this.source_representation.model.set("delete_scheduled", delid); + } +} + +/* */ + +Rkns.Renderer.NodeRevertButton = Rkns.Utils.inherit(Rkns.Renderer._NodeButton); + +Rkns.Renderer.NodeRevertButton.prototype._init = function() { + this.type = "Node-revert-button"; + this.lastSectorInner = 0; + this.startAngle = -135; + this.endAngle = 135; + this.imageName = "revert"; + this.text = "Cancel deletion"; +} + +Rkns.Renderer.NodeRevertButton.prototype.mouseup = function() { + this.renderer.click_target = null; + this.renderer.is_dragging = false; + if (this.renderer.isEditable()) { + this.source_representation.model.unset("delete_scheduled"); } } @@ -1169,12 +1258,25 @@ Rkns.Renderer.NodeLinkButton.prototype._init = function() { this.type = "Node-link-button"; this.lastSectorInner = 0; - this.startAngle = -135; - this.endAngle = -45; - this.imageFile = 'img/link.png'; + this.startAngle = 90; + this.endAngle = 180; + this.imageName = "link"; this.text = "Link to another node"; } +Rkns.Renderer.NodeLinkButton.prototype.mousedown = function(_event, _isTouch) { + if (this.renderer.isEditable()) { + var _off = this.renderer.canvas_$.offset(), + _point = new paper.Point([ + _event.pageX - _off.left, + _event.pageY - _off.top + ]); + this.renderer.click_target = null; + this.renderer.removeRepresentationsOfType("editor"); + this.renderer.addTempEdge(this.source_representation, _point); + } +} + /* */ Rkns.Renderer.NodeEnlargeButton = Rkns.Utils.inherit(Rkns.Renderer._NodeButton); @@ -1182,18 +1284,18 @@ Rkns.Renderer.NodeEnlargeButton.prototype._init = function() { this.type = "Node-enlarge-button"; this.lastSectorInner = 0; - this.startAngle = 45; - this.endAngle = 90; - this.imageFile = 'img/enlarge.png'; + this.startAngle = -45; + this.endAngle = 0; + this.imageName = "enlarge"; this.text = "Enlarge"; } Rkns.Renderer.NodeEnlargeButton.prototype.mouseup = function() { - var _newsize = 1 + (this.source_representation.model.get("size") || 0); - this.source_representation.model.set("size", _newsize); - this.source_representation.select(); - this.select(); - paper.view.draw(); + var _newsize = 1 + (this.source_representation.model.get("size") || 0); + this.source_representation.model.set("size", _newsize); + this.source_representation.select(); + this.select(); + paper.view.draw(); } /* */ @@ -1203,18 +1305,18 @@ Rkns.Renderer.NodeShrinkButton.prototype._init = function() { this.type = "Node-shrink-button"; this.lastSectorInner = 0; - this.startAngle = 90; - this.endAngle = 135; - this.imageFile = 'img/shrink.png'; + this.startAngle = -180; + this.endAngle = -135; + this.imageName = "shrink"; this.text = "Shrink"; } Rkns.Renderer.NodeShrinkButton.prototype.mouseup = function() { - var _newsize = -1 + (this.source_representation.model.get("size") || 0); - this.source_representation.model.set("size", _newsize); - this.source_representation.select(); - this.select(); - paper.view.draw(); + var _newsize = -1 + (this.source_representation.model.get("size") || 0); + this.source_representation.model.set("size", _newsize); + this.source_representation.select(); + this.select(); + paper.view.draw(); } /* */ @@ -1223,7 +1325,7 @@ Rkns.Renderer.EdgeEditButton.prototype._init = function() { this.type = "Edge-edit-button"; - this.sector = Rkns.Renderer.Utils.sector(this, Rkns.Renderer._EDGE_BUTTON_INNER, Rkns.Renderer._EDGE_BUTTON_OUTER, - 90, 90, 1, this.options.static_url+'img/edit.png', this.renkan.translate("Edit")); + this.sector = this.renderer.drawSector(this, Rkns.Renderer._EDGE_BUTTON_INNER, Rkns.Renderer._EDGE_BUTTON_OUTER, -270, -90, 1, "edit", this.renkan.translate("Edit")); } Rkns.Renderer.EdgeEditButton.prototype.mouseup = function() { @@ -1238,13 +1340,37 @@ Rkns.Renderer.EdgeRemoveButton.prototype._init = function() { this.type = "Edge-remove-button"; - this.sector = Rkns.Renderer.Utils.sector(this, Rkns.Renderer._EDGE_BUTTON_INNER, Rkns.Renderer._EDGE_BUTTON_OUTER, - 270, -90, 1, this.options.static_url+'img/remove.png', this.renkan.translate("Remove")); + this.sector = this.renderer.drawSector(this, Rkns.Renderer._EDGE_BUTTON_INNER, Rkns.Renderer._EDGE_BUTTON_OUTER, -90, 90, 1, "remove", this.renkan.translate("Remove")); } Rkns.Renderer.EdgeRemoveButton.prototype.mouseup = function() { + this.renderer.click_target = null; + this.renderer.is_dragging = false; this.renderer.removeRepresentationsOfType("editor"); - if (this.renderer.isEditable() && confirm(this.renkan.translate('Do you really wish to remove edge ') + '"' + this.source_representation.model.get("title") + '"?')) { - this.project.removeEdge(this.source_representation.model); + if (this.renderer.isEditable()) { + var delid = Rkns.Utils.getUID("delete"); + this.renderer.delete_list.push({ + id: delid, + time: new Date().valueOf() + this.options.element_delete_delay + }); + this.source_representation.model.set("delete_scheduled", delid); + } +} + +/* */ + +Rkns.Renderer.EdgeRevertButton = Rkns.Utils.inherit(Rkns.Renderer._BaseButton); + +Rkns.Renderer.EdgeRevertButton.prototype._init = function() { + this.type = "Edge-revert-button"; + this.sector = this.renderer.drawSector(this, Rkns.Renderer._EDGE_BUTTON_INNER, Rkns.Renderer._EDGE_BUTTON_OUTER, -135, 135, 1, "revert", this.renkan.translate("Cancel deletion")); +} + +Rkns.Renderer.EdgeRevertButton.prototype.mouseup = function() { + this.renderer.click_target = null; + this.renderer.is_dragging = false; + if (this.renderer.isEditable()) { + this.source_representation.model.unset("delete_scheduled"); } } @@ -1253,12 +1379,12 @@ Rkns.Renderer.MiniFrame = Rkns.Utils.inherit(Rkns.Renderer._BaseRepresentation); Rkns.Renderer.MiniFrame.prototype.paperShift = function(_delta) { - this.renderer.offset = this.renderer.offset.subtract(_delta.divide(this.renderer.minimap.scale).multiply(this.renderer.scale)); + this.renderer.offset = this.renderer.offset.subtract(_delta.divide(this.renderer.minimap.scale).multiply(this.renderer.scale)); this.renderer.redraw(); } Rkns.Renderer.MiniFrame.prototype.mouseup = function(_delta) { - this.renderer.click_target = null; + this.renderer.click_target = null; this.renderer.is_dragging = false; } @@ -1271,235 +1397,224 @@ this.$.html(this.template(_renkan)); this.onStatusChange(); this.canvas_$ = this.$.find(".Rk-Canvas"); + this.labels_$ = this.$.find(".Rk-Labels"); this.editor_$ = this.$.find(".Rk-Editor"); this.notif_$ = this.$.find(".Rk-Notifications"); paper.setup(this.canvas_$[0]); this.scale = 1; this.offset = paper.view.center; this.totalScroll = 0; + this.mouse_down = false; this.click_target = null; this.selected_target = null; this.edge_layer = new paper.Layer(); this.node_layer = new paper.Layer(); this.buttons_layer = new paper.Layer(); + this.delete_list = []; if (_renkan.options.show_minimap) { - this.minimap = { - background_layer: new paper.Layer(), - edge_layer: new paper.Layer(), - node_layer: new paper.Layer(), - node_group: new paper.Group(), - size: new paper.Size( _renkan.options.minimap_width, _renkan.options.minimap_height ) - } - - this.minimap.background_layer.activate(); - this.minimap.topleft = paper.view.bounds.bottomRight.subtract(this.minimap.size); - this.minimap.rectangle = new paper.Path.Rectangle(this.minimap.topleft.subtract([2,2]), this.minimap.size.add([4,4])); - this.minimap.rectangle.fillColor = _renkan.options.minimap_background_color; - this.minimap.rectangle.strokeColor = _renkan.options.minimap_border_color; - this.minimap.rectangle.strokeWidth = 4; - this.minimap.offset = new paper.Point(this.minimap.size.divide(2)); - this.minimap.scale = .1; - - this.minimap.node_layer.activate(); - this.minimap.cliprectangle = new paper.Path.Rectangle(this.minimap.topleft, this.minimap.size); - this.minimap.node_group.addChild(this.minimap.cliprectangle); - this.minimap.node_group.clipped = true; - this.minimap.miniframe = new paper.Path.Rectangle(this.minimap.topleft, this.minimap.size); - this.minimap.node_group.addChild(this.minimap.miniframe); - this.minimap.miniframe.fillColor = '#c0c0ff'; - this.minimap.miniframe.opacity = .3; - this.minimap.miniframe.strokeColor = '#000080'; - this.minimap.miniframe.strokeWidth = 3; - this.minimap.miniframe.__representation = new Rkns.Renderer.MiniFrame(this, null); + this.minimap = { + background_layer: new paper.Layer(), + edge_layer: new paper.Layer(), + node_layer: new paper.Layer(), + node_group: new paper.Group(), + size: new paper.Size( _renkan.options.minimap_width, _renkan.options.minimap_height ) + } + + this.minimap.background_layer.activate(); + this.minimap.topleft = paper.view.bounds.bottomRight.subtract(this.minimap.size); + this.minimap.rectangle = new paper.Path.Rectangle(this.minimap.topleft.subtract([2,2]), this.minimap.size.add([4,4])); + this.minimap.rectangle.fillColor = _renkan.options.minimap_background_color; + this.minimap.rectangle.strokeColor = _renkan.options.minimap_border_color; + this.minimap.rectangle.strokeWidth = 4; + this.minimap.offset = new paper.Point(this.minimap.size.divide(2)); + this.minimap.scale = .1; + + this.minimap.node_layer.activate(); + this.minimap.cliprectangle = new paper.Path.Rectangle(this.minimap.topleft, this.minimap.size); + this.minimap.node_group.addChild(this.minimap.cliprectangle); + this.minimap.node_group.clipped = true; + this.minimap.miniframe = new paper.Path.Rectangle(this.minimap.topleft, this.minimap.size); + this.minimap.node_group.addChild(this.minimap.miniframe); + this.minimap.miniframe.fillColor = '#c0c0ff'; + this.minimap.miniframe.opacity = .3; + this.minimap.miniframe.strokeColor = '#000080'; + this.minimap.miniframe.strokeWidth = 3; + this.minimap.miniframe.__representation = new Rkns.Renderer.MiniFrame(this, null); } + this.throttledPaperDraw = Rkns._(function() { + paper.view.draw(); + }).throttle(100); + this.bundles = []; this.click_mode = false; - var _tool = new paper.Tool(), - _this = this, - _allowScroll = true; + + var _this = this, + _allowScroll = true, + _originalScale, + _zooming = false, + _lastTapDate, + _lastTapX, + _lastTapY; + + this.imageCache = {}; + + ['edit', 'remove', 'link', 'enlarge', 'shrink', 'revert' ].forEach(function(imgname) { + var img = new Image(); + img.src = _renkan.options.static_url + 'img/' + imgname + '.png'; + _this.imageCache[imgname] = img; + }); + + var throttledMouseMove = _.throttle(function(_event, _isTouch) { + _this.onMouseMove(_event, _isTouch); + }, Rkns.Renderer._MOUSEMOVE_RATE); - _tool.minDistance = Rkns.Renderer._MIN_DRAG_DISTANCE; - _tool.onMouseMove = function(_event) { - _this.onMouseMove(_event); - } - _tool.onMouseDown = function(_event) { - _this.onMouseDown(_event); - } - _tool.onMouseDrag = function(_event) { - _this.onMouseDrag(_event); - } - this.canvas_$.mouseup(function(_event) { - _this.onMouseUp(_event); - }); - this.canvas_$.mousewheel(function(_event, _delta) { - if (_allowScroll) { - _this.onScroll(_event, _delta); + this.canvas_$.on({ + mousedown: function(_event) { + _event.preventDefault(); + _this.onMouseDown(_event, false); + }, + mousemove: function(_event) { + _event.preventDefault(); + throttledMouseMove(_event, false); + }, + mouseup: function(_event) { + _event.preventDefault(); + _this.onMouseUp(_event, false); + }, + mousewheel: function(_event, _delta) { + _event.preventDefault(); + if (_allowScroll) { + _this.onScroll(_event, _delta); + } + }, + touchstart: function(_event) { + _event.preventDefault(); + var _touches = _event.originalEvent.touches[0]; + if ( + _renkan.options.allow_double_click + && new Date() - _lastTap < Rkns.Renderer._DOUBLETAP_DELAY + && ( Math.pow(_lastTapX - _touches.pageX, 2) + Math.pow(_lastTapY - _touches.pageY, 2) < Rkns.Renderer._DOUBLETAP_DISTANCE ) + ) { + _lastTap = 0; + _this.onDoubleClick(_touches); + } else { + _lastTap = new Date(); + _lastTapX = _touches.pageX; + _lastTapY = _touches.pageY; + _originalScale = _this.scale; + _zooming = false; + _this.onMouseDown(_touches, true); + } + }, + touchmove: function(_event) { + _event.preventDefault(); + _lastTap = 0; + if (_event.originalEvent.touches.length == 1) { + _this.onMouseMove(_event.originalEvent.touches[0], true); + } else { + if (!_zooming) { + _this.onMouseUp(_event.originalEvent.touches[0], true); + _this.click_target = null; + _this.is_dragging = false; + _zooming = true; + } + if (_event.originalEvent.scale === "undefined") { + return; + } + var _newScale = _event.originalEvent.scale * _originalScale, + _scaleRatio = _newScale / _this.scale, + _newOffset = new paper.Point([ + _this.canvas_$.width(), + _this.canvas_$.height() + ]).multiply( .5 * ( 1 - _scaleRatio ) ).add(_this.offset.multiply( _scaleRatio )); + _this.setScale(_newScale, _this.offset); + } + }, + touchend: function(_event) { + _event.preventDefault(); + _this.onMouseUp(_event.originalEvent.changedTouches[0], true); + }, + dblclick: function(_event) { + _event.preventDefault(); + if (_renkan.options.allow_double_click) { + _this.onDoubleClick(_event); + } + }, + mouseleave: function(_event) { + _event.preventDefault(); + _this.onMouseUp(_event, false); + _this.click_target = null; + _this.is_dragging = false; + }, + dragover: function(_event) { + _event.preventDefault(); + }, + dragenter: function(_event) { + _event.preventDefault(); + _allowScroll = false; + }, + dragleave: function(_event) { + _event.preventDefault(); + _allowScroll = true; + }, + drop: function(_event) { + _event.preventDefault(); + _allowScroll = true; + var res = {}; + Rkns._(_event.originalEvent.dataTransfer.types).each(function(t) { + try { + res[t] = _event.originalEvent.dataTransfer.getData(t); + } catch(e) {} + }); + var text = _event.originalEvent.dataTransfer.getData("Text"); + if (typeof text === "string") { + switch(text[0]) { + case "{": + case "[": + try { + var data = JSON.parse(text); + _(res).extend(data); + } + catch(e) { + if (!res["text/plain"]) { + res["text/plain"] = text; + } + } + break; + case "<": + if (!res["text/html"]) { + res["text/html"] = text; + } + break; + default: + if (!res["text/plain"]) { + res["text/plain"] = text; + } + } + } + var url = _event.originalEvent.dataTransfer.getData("URL"); + if (url && !res["text/uri-list"]) { + res["text/uri-list"] = url; + } + _this.dropData(res, _event.originalEvent); } }); - this.canvas_$.dblclick(function(_event) { - _this.onDoubleClick(_event); - }); - this.canvas_$.on("dragover", function(_event) { - _event.stopPropagation(); - _event.preventDefault(); - }); - this.canvas_$.on("dragenter", function(_event) { - _allowScroll = false; - _event.stopPropagation(); - _event.preventDefault(); - }); - this.canvas_$.on("dragleave", function(_event) { - _allowScroll = true; - _event.stopPropagation(); - _event.preventDefault(); - }); - this.canvas_$.on("drop", function(_event) { - _event.stopPropagation(); - _event.preventDefault(); - _allowScroll = true; - if (!_this.isEditable()) { - return; - } - var res = {}; - Rkns._(_event.originalEvent.dataTransfer.types).each(function(t) { - try { - res[t] = _event.originalEvent.dataTransfer.getData(t); - } catch(e) {} - }); - var text = _event.originalEvent.dataTransfer.getData("Text"); - if (typeof text === "string") { - switch(text[0]) { - case "{": - case "[": - try { - var data = JSON.parse(text); - _(res).extend(data); - } - catch(e) { - if (!res["text/plain"]) { - res["text/plain"] = text; - } - } - break; - case "<": - if (!res["text/html"]) { - res["text/html"] = text; - } - break; - default: - if (!res["text/plain"]) { - res["text/plain"] = text; - } - } - } - var url = _event.originalEvent.dataTransfer.getData("URL"); - if (url && !res["text/uri-list"]) { - res["text/uri-list"] = url; - } - if (res["text/json"] || res["application/json"]) { - try { - var data = JSON.parse(res["text/json"] || res["application/json"]); - _(res).extend(data); - } - catch(e) {} - } - var newNode = {}; - switch(res["text/x-iri-specific-site"]) { - case "twitter": - var snippet = Rkns.$('
').html(res["text/x-iri-selected-html"]), - tweetdiv = snippet.find(".tweet") - newNode.title = _renkan.translate("Tweet by ") + tweetdiv.attr("data-name"); - newNode.uri = "http://twitter.com/" + tweetdiv.attr("data-screen-name") + "/status/" + tweetdiv.attr("data-tweet-id"); - newNode.image = tweetdiv.find(".avatar").attr("src"); - newNode.description = tweetdiv.find(".js-tweet-text:first").text(); - break; - case "google": - var snippet = Rkns.$('
').html(res["text/x-iri-selected-html"]); - newNode.title = snippet.find("h3:first").text().trim(); - newNode.uri = snippet.find("h3 a").attr("href"); - newNode.description = snippet.find(".st:first").text().trim(); - break; - case undefined: - default: - if (res["text/x-iri-source-uri"]) { - newNode.uri = res["text/x-iri-source-uri"]; - } - if (res["text/plain"] || res["text/x-iri-selected-text"]) { - newNode.description = (res["text/plain"] || res["text/x-iri-selected-text"]).replace(/[\s\n]+/gm,' ').trim(); - } - if (res["text/html"] || res["text/x-iri-selected-html"]) { - var snippet = Rkns.$('
').html(res["text/html"] || res["text/x-iri-selected-html"]); - newNode.image = snippet.find("img").attr("src") || ''; - newNode.uri = snippet.find("a").attr("href") || newNode.uri; - newNode.title = snippet.find("[title]").attr("title"); - newNode.description = snippet.text().replace(/[\s\n]+/gm,' ').trim(); - } - if (res["text/uri-list"]) { - newNode.uri = res["text/uri-list"]; - } - if (res["text/x-moz-url"] && !newNode.title) { - newNode.title = (res["text/x-moz-url"].split("\n")[1] || "").trim(); - if (newNode.title === newNode.uri) { - newNode.title = false; - } - } - if (res["text/x-iri-source-title"] && !newNode.title) { - newNode.title = res["text/x-iri-source-title"]; - } - if (res["text/html"] || res["text/x-iri-selected-html"]) { - newNode.image = snippet.find("[data-image]").attr("data-image") || newNode.image; - newNode.uri = snippet.find("[data-uri]").attr("data-uri") || newNode.uri; - newNode.title = snippet.find("[data-title]").attr("data-title") || newNode.title; - newNode.description = snippet.find("[data-description]").attr("data-description") || newNode.description; - } - } - var fields = ["title", "description", "uri", "image"]; - for (var i = 0; i < fields.length; i++) { - var f = fields[i]; - if (res["text/x-iri-" + f] || res[f]) { - newNode[f] = res["text/x-iri-" + f] || res[f]; - } - } - if (newNode.title || newNode.description || newNode.uri) { - var _off = _this.canvas_$.offset(), - _point = new paper.Point([ - _event.originalEvent.pageX - _off.left, - _event.originalEvent.pageY - _off.top - ]), - _coords = _this.toModelCoords(_point), - _data = { - id: Rkns.Utils.getUID('node'), - created_by: _this.renkan.current_user, - uri: newNode.uri || "", - title: newNode.title || _this.renkan.translate("Dragged resource"), - description: newNode.description || "", - image: newNode.image || "", - color: newNode.color || undefined, - position: { - x: _coords.x, - y: _coords.y - } - }; - var _node = _this.renkan.project.addNode(_data); - _this.getRepresentationByModel(_node).openEditor(); - } - }) this.editor_$.find(".Rk-ZoomOut").click(function() { - var _newScale = _this.scale * Math.SQRT1_2, - _offset = new paper.Point([ - _this.canvas_$.width(), - _this.canvas_$.height() - ]).multiply( .5 * ( 1 - Math.SQRT1_2 ) ).add(_this.offset.multiply( Math.SQRT1_2 )); + var _newScale = _this.scale * Math.SQRT1_2, + _offset = new paper.Point([ + _this.canvas_$.width(), + _this.canvas_$.height() + ]).multiply( .5 * ( 1 - Math.SQRT1_2 ) ).add(_this.offset.multiply( Math.SQRT1_2 )); _this.setScale( _newScale, _offset ); }); this.editor_$.find(".Rk-ZoomIn").click(function() { - var _newScale = _this.scale * Math.SQRT2, - _offset = new paper.Point([ - _this.canvas_$.width(), - _this.canvas_$.height() - ]).multiply( .5 * ( 1 - Math.SQRT2 ) ).add(_this.offset.multiply( Math.SQRT2 )); + var _newScale = _this.scale * Math.SQRT2, + _offset = new paper.Point([ + _this.canvas_$.width(), + _this.canvas_$.height() + ]).multiply( .5 * ( 1 - Math.SQRT2 ) ).add(_this.offset.multiply( Math.SQRT2 )); _this.setScale( _newScale, _offset ); }); this.$.find(".Rk-CurrentUser").mouseenter( @@ -1548,45 +1663,45 @@ } }); this.$.find(".Rk-Bookmarklet-Button") - .attr("href","javascript:" + Rkns.Renderer._BOOKMARKLET_CODE(_renkan)) - .click(function(){ - _this.notif_$ - .text(_renkan.translate("Drag this button to your bookmark bar. When on a third-party website, click it to enable drag-and-drop from the website to Renkan.")) - .fadeIn() - .delay(5000) - .fadeOut(); - return false; - }); + .attr("href","javascript:" + Rkns.Renderer._BOOKMARKLET_CODE(_renkan)) + .click(function(){ + _this.notif_$ + .text(_renkan.translate("Drag this button to your bookmark bar. When on a third-party website, click it to enable drag-and-drop from the website to Renkan.")) + .fadeIn() + .delay(5000) + .fadeOut(); + return false; + }); this.$.find(".Rk-TopBar-Button").mouseover(function() { Rkns.$(this).find(".Rk-TopBar-Tooltip").show(); }).mouseout(function() { Rkns.$(this).find(".Rk-TopBar-Tooltip").hide(); }); this.$.find(".Rk-Fold-Bins").click(function() { - var bins = _renkan.$.find(".Rk-Bins"); - if (bins.offset().left < 0) { - bins.animate({left: 0},250); - _this.$.animate({left: 300},250,function() { - var w = _this.$.width(); - paper.view.viewSize = new paper.Size([w, _this.canvas_$.height()]); - }); - $(this).html("«"); - } else { - bins.animate({left: -300},250); - _this.$.animate({left: 0},250,function() { - var w = _this.$.width(); - paper.view.viewSize = new paper.Size([w, _this.canvas_$.height()]); - }); - $(this).html("»"); - } + var bins = _renkan.$.find(".Rk-Bins"); + if (bins.offset().left < 0) { + bins.animate({left: 0},250); + _this.$.animate({left: 300},250,function() { + var w = _this.$.width(); + paper.view.viewSize = new paper.Size([w, _this.canvas_$.height()]); + }); + $(this).html("«"); + } else { + bins.animate({left: -300},250); + _this.$.animate({left: 0},250,function() { + var w = _this.$.width(); + paper.view.viewSize = new paper.Size([w, _this.canvas_$.height()]); + }); + $(this).html("»"); + } }); paper.view.onResize = function(_event) { - _this.offset = _this.offset.add(_event.delta.divide(2)); - if (_this.minimap) { - _this.minimap.topleft = paper.view.bounds.bottomRight.subtract(_this.minimap.size) - _this.minimap.rectangle.fitBounds(_this.minimap.topleft.subtract([2,2]), _this.minimap.size.add([4,4])); - _this.minimap.cliprectangle.fitBounds(_this.minimap.topleft, _this.minimap.size); + _this.offset = _this.offset.add(_event.delta.divide(2)); + if (_this.minimap) { + _this.minimap.topleft = paper.view.bounds.bottomRight.subtract(_this.minimap.size) + _this.minimap.rectangle.fitBounds(_this.minimap.topleft.subtract([2,2]), _this.minimap.size.add([4,4])); + _this.minimap.cliprectangle.fitBounds(_this.minimap.topleft, _this.minimap.size); } _this.redraw(); } @@ -1621,7 +1736,7 @@ _thRedraw(); }); this.renkan.project.on("change:title", function(_model, _title) { - var el = $(".Rk-PadTitle"); + var el = _this.$.find(".Rk-PadTitle"); if (el.is("input")) { if (el.val() !== _title) { el.val(_title); @@ -1631,13 +1746,53 @@ } }); + if (_renkan.options.size_bug_fix) { + var _delay = ( + typeof _renkan.options.size_bug_fix === "number" + ? _renkan.options.size_bug_fix + : 500 + ); + window.setTimeout( + function() { + _this.fixSize(true); + }, + _delay + ); + } + + if (_renkan.options.force_resize) { + $(window).resize(function() { + _this.fixSize(false); + }); + } + this.redraw(); + window.setInterval(function() { + var _now = new Date().valueOf(); + _this.delete_list.forEach(function(d) { + if (_now >= d.time) { + var el = _renkan.project.get("nodes").findWhere({"delete_scheduled":d.id}); + if (el) { + project.removeNode(el); + } + el = _renkan.project.get("edges").findWhere({"delete_scheduled":d.id}); + if (el) { + project.removeEdge(el); + } + } + }); + _this.delete_list = _this.delete_list.filter(function(d) { + return _renkan.project.get("nodes").findWhere({"delete_scheduled":d.id}) || _renkan.project.get("edges").findWhere({"delete_scheduled":d.id}) + }); + }, 500); + if (this.minimap) { - window.setInterval(function() { - _this.rescaleMinimap() - }, 2000); + window.setInterval(function() { + _this.rescaleMinimap() + }, 2000); } + } Rkns.Renderer.Scene.prototype.template = Rkns._.template( @@ -1651,14 +1806,137 @@ + '
' + '
' + '<%-translate("Renkan \'Drag-to-Add\' bookmarklet")%>
' - + '
' - + '<% } } %>' - + '
' + + '
' + + '<% } %>
<% } %>' + + '
' + + '
' + '<% if (options.show_bins) { %>
«
<% } %>' + '
">
">
' + '
' ); +Rkns.Renderer.Scene.prototype.fixSize = function(_autoscale) { + var w = this.$.width(), + h = this.$.height(); + if (this.renkan.options.show_top_bar) { + h -= this.$.find(".Rk-TopBar").height(); + } + this.canvas_$.attr({ + width: w, + height: h + }); + + paper.view.viewSize = new paper.Size([w, h]); + + if (_autoscale) { + this.autoScale(); + } +} + +Rkns.Renderer.Scene.prototype.drawSector = function(_repr, _inR, _outR, _startAngle, _endAngle, _padding, _imgname, _caption) { + var _options = this.renkan.options, + _startRads = _startAngle * Math.PI / 180, + _endRads = _endAngle * Math.PI / 180, + _img = this.imageCache[_imgname], + _span = _endRads - _startRads, + _startdx = - Math.sin(_startRads), + _startdy = Math.cos(_startRads), + _startXIn = Math.cos(_startRads) * _inR + _padding * _startdx, + _startYIn = Math.sin(_startRads) * _inR + _padding * _startdy, + _startXOut = Math.cos(_startRads) * _outR + _padding * _startdx, + _startYOut = Math.sin(_startRads) * _outR + _padding * _startdy, + _enddx = - Math.sin(_endRads), + _enddy = Math.cos(_endRads), + _endXIn = Math.cos(_endRads) * _inR - _padding * _enddx, + _endYIn = Math.sin(_endRads) * _inR - _padding * _enddy, + _endXOut = Math.cos(_endRads) * _outR - _padding * _enddx, + _endYOut = Math.sin(_endRads) * _outR - _padding * _enddy, + _centerR = (_inR + _outR)/2, + _centerRads = (_startRads + _endRads) / 2, + _centerX = Math.cos(_centerRads) * _centerR, + _centerY = Math.sin(_centerRads) * _centerR, + _centerXIn = Math.cos(_centerRads) * _inR, + _centerXOut = Math.cos(_centerRads) * _outR, + _centerYIn = Math.sin(_centerRads) * _inR, + _centerYOut = Math.sin(_centerRads) * _outR, + _textX = Math.cos(_centerRads) * (_outR + 3), + _textY = Math.sin(_centerRads) * (_outR + _options.buttons_label_font_size) + _options.buttons_label_font_size / 2, + _segments = []; + this.buttons_layer.activate(); + var _path = new paper.Path(); + _path.add([_startXIn, _startYIn]); + _path.arcTo([_centerXIn, _centerYIn], [_endXIn, _endYIn]); + _path.lineTo([_endXOut, _endYOut]); + _path.arcTo([_centerXOut, _centerYOut], [_startXOut, _startYOut]); + _path.fillColor = _options.buttons_background; + _path.opacity = .5; + _path.closed = true; + _path.__representation = _repr; + var _text = new paper.PointText(_textX,_textY); + _text.characterStyle = { + fontSize: _options.buttons_label_font_size, + fillColor: _options.buttons_label_color + }; + if (_textX > 2) { + _text.paragraphStyle.justification = 'left'; + } else if (_textX < -2) { + _text.paragraphStyle.justification = 'right'; + } else { + _text.paragraphStyle.justification = 'center'; + } + _text.visible = false; + var _visible = false, + _restPos = new paper.Point(-200, -200), + _grp = new paper.Group([_path, _text]), + _delta = _grp.position, + _imgdelta = new paper.Point([_centerX, _centerY]), + _currentPos = new paper.Point(0,0); + _text.content = _caption; + _grp.visible = false; + _grp.position = _restPos; + var _res = { + show: function() { + _visible = true; + _grp.position = _currentPos.add(_delta); + _grp.visible = true; + }, + moveTo: function(_point) { + _currentPos = _point; + if (_visible) { + _grp.position = _point.add(_delta); + } + }, + hide: function() { + _visible = false; + _grp.visible = false; + _grp.position = _restPos; + }, + select: function() { + _path.opacity = .8; + _text.visible = true; + }, + unselect: function() { + _path.opacity = .5; + _text.visible = false; + }, + destroy: function() { + _grp.remove(); + } + } + function showImage() { + var _raster = new paper.Raster(_img); + _raster.position = _imgdelta.add(_grp.position).subtract(_delta); + _grp.addChild(_raster); + } + if (_img.width) { + showImage(); + } else { + Rkns.$(_img).on("load",showImage); + } + + return _res +} + Rkns.Renderer.Scene.prototype.addToBundles = function(_edgeRepr) { var _bundle = Rkns._(this.bundles).find(function(_bundle) { return ( @@ -1684,33 +1962,33 @@ } Rkns.Renderer.Scene.prototype.isEditable = function() { - return (this.renkan.options.editor_mode && !this.renkan.read_only) + return (this.renkan.options.editor_mode && !this.renkan.read_only) } Rkns.Renderer.Scene.prototype.onStatusChange = function() { - var savebtn = this.$.find(".Rk-Save-Button"), - tip = savebtn.find(".Rk-TopBar-Tooltip-Contents"); - if (this.renkan.read_only) { - savebtn.removeClass("disabled Rk-Save-Online").addClass("Rk-Save-ReadOnly"); - tip.text(this.renkan.translate("Connection lost")); - } else { - if (this.renkan.options.snapshot_mode) { - savebtn.removeClass("Rk-Save-ReadOnly Rk-Save-Online"); - tip.text(this.renkan.translate("Archive Project")); - } else { - savebtn.removeClass("disabled Rk-Save-ReadOnly").addClass("Rk-Save-Online"); - tip.text(this.renkan.translate("Auto-save enabled")); - } - } + var savebtn = this.$.find(".Rk-Save-Button"), + tip = savebtn.find(".Rk-TopBar-Tooltip-Contents"); + if (this.renkan.read_only) { + savebtn.removeClass("disabled Rk-Save-Online").addClass("Rk-Save-ReadOnly"); + tip.text(this.renkan.translate("Connection lost")); + } else { + if (this.renkan.options.snapshot_mode) { + savebtn.removeClass("Rk-Save-ReadOnly Rk-Save-Online"); + tip.text(this.renkan.translate("Archive Project")); + } else { + savebtn.removeClass("disabled Rk-Save-ReadOnly").addClass("Rk-Save-Online"); + tip.text(this.renkan.translate("Auto-save enabled")); + } + } } Rkns.Renderer.Scene.prototype.setScale = function(_newScale, _offset) { - if (_newScale > Rkns.Renderer._MIN_SCALE && _newScale < Rkns.Renderer._MAX_SCALE) { - this.scale = _newScale; - if (_offset) { - this.offset = _offset - } - this.redraw(); + if (_newScale > Rkns.Renderer._MIN_SCALE && _newScale < Rkns.Renderer._MAX_SCALE) { + this.scale = _newScale; + if (_offset) { + this.offset = _offset + } + this.redraw(); } } @@ -1724,7 +2002,7 @@ _maxx = Math.max.apply(Math, _xx), _maxy = Math.max.apply(Math, _yy); var _scale = Math.max(Rkns.Renderer._MIN_SCALE, Math.min(Rkns.Renderer._MAX_SCALE, (paper.view.size.width - 2 * Rkns.Renderer._AUTOSCALE_MARGIN) / (_maxx - _minx), (paper.view.size.height - 2 * Rkns.Renderer._AUTOSCALE_MARGIN) / (_maxy - _miny))); - this.setScale(_scale, paper.view.center.subtract(new paper.Point([(_maxx + _minx) / 2, (_maxy + _miny) / 2]).multiply(_scale))); + this.setScale(_scale, paper.view.center.subtract(new paper.Point([(_maxx + _minx) / 2, (_maxy + _miny) / 2]).multiply(_scale))); } if (nodes.length === 1) { this.setScale(1, paper.view.center.subtract(new paper.Point([nodes.at(0).get("position").x, nodes.at(0).get("position").y]))); @@ -1732,9 +2010,9 @@ } Rkns.Renderer.Scene.prototype.redrawMiniframe = function() { - var topleft = this.toMinimapCoords(this.toModelCoords(new paper.Point([0,0]))), - bottomright = this.toMinimapCoords(this.toModelCoords(paper.view.bounds.bottomRight)); - this.minimap.miniframe.fitBounds(topleft, bottomright); + var topleft = this.toMinimapCoords(this.toModelCoords(new paper.Point([0,0]))), + bottomright = this.toMinimapCoords(this.toModelCoords(paper.view.bounds.bottomRight)); + this.minimap.miniframe.fitBounds(topleft, bottomright); } Rkns.Renderer.Scene.prototype.rescaleMinimap = function() { @@ -1747,16 +2025,16 @@ _maxx = Math.max.apply(Math, _xx), _maxy = Math.max.apply(Math, _yy); var _scale = Math.min( - this.scale * .8 * this.renkan.options.minimap_width / paper.view.bounds.width, - this.scale * .8 * this.renkan.options.minimap_height / paper.view.bounds.height, - ( this.renkan.options.minimap_width - 2 * Rkns.Renderer._MINIMAP_MARGIN ) / (_maxx - _minx), - ( this.renkan.options.minimap_height - 2 * Rkns.Renderer._MINIMAP_MARGIN ) / (_maxy - _miny) - ); + this.scale * .8 * this.renkan.options.minimap_width / paper.view.bounds.width, + this.scale * .8 * this.renkan.options.minimap_height / paper.view.bounds.height, + ( this.renkan.options.minimap_width - 2 * Rkns.Renderer._MINIMAP_MARGIN ) / (_maxx - _minx), + ( this.renkan.options.minimap_height - 2 * Rkns.Renderer._MINIMAP_MARGIN ) / (_maxy - _miny) + ); this.minimap.offset = this.minimap.size.divide(2).subtract(new paper.Point([(_maxx + _minx) / 2, (_maxy + _miny) / 2]).multiply(_scale)); this.minimap.scale = _scale; } if (nodes.length === 1) { - this.minimap.scale = .1; + this.minimap.scale = .1; this.minimap.offset = this.minimap.size.divide(2).subtract(new paper.Point([nodes.at(0).get("position").x, nodes.at(0).get("position").y]).multiply(this.minimap.scale)); } this.redraw(); @@ -1845,12 +2123,18 @@ }); } +Rkns.Renderer.Scene.prototype.unselectAll = function(_model) { + Rkns._(this.representations).each(function(_repr) { + _repr.unselect(); + }); +} + Rkns.Renderer.Scene.prototype.redraw = function() { Rkns._(this.representations).each(function(_representation) { - _representation.redraw(true); + _representation.redraw(true); }); if (this.minimap) { - this.redrawMiniframe(); + this.redrawMiniframe(); } paper.view.draw(); } @@ -1875,45 +2159,60 @@ } } else { if (this.selected_target) { - this.selected_target.unselect(null); + this.selected_target.unselect(); } this.selected_target = null; } } Rkns.Renderer.Scene.prototype.paperShift = function(_delta) { - this.offset = this.offset.add(_delta); + this.offset = this.offset.add(_delta); this.redraw(); } Rkns.Renderer.Scene.prototype.onMouseMove = function(_event) { - var _hitResult = paper.project.hitTest(_event.point); + var _off = this.canvas_$.offset(), + _point = new paper.Point([ + _event.pageX - _off.left, + _event.pageY - _off.top + ]), + _delta = _point.subtract(this.last_point); + this.last_point = _point; + if (!this.is_dragging && this.mouse_down && _delta.length > Rkns.Renderer._MIN_DRAG_DISTANCE) { + this.is_dragging = true; + } + var _hitResult = paper.project.hitTest(_point); if (this.is_dragging) { if (this.click_target && typeof this.click_target.paperShift === "function") { - this.click_target.paperShift(_event.delta); + this.click_target.paperShift(_delta); } else { - this.paperShift(_event.delta); + this.paperShift(_delta); } } else { this.findTarget(_hitResult); } + paper.view.draw(); } -Rkns.Renderer.Scene.prototype.onMouseDown = function(_event) { - if (!this.click_target || this.click_target.type !== "temp-edge") { +Rkns.Renderer.Scene.prototype.onMouseDown = function(_event, _isTouch) { + var _off = this.canvas_$.offset(), + _point = new paper.Point([ + _event.pageX - _off.left, + _event.pageY - _off.top + ]); + this.last_point = _point; + this.mouse_down = true; + if (!this.click_target || this.click_target.type !== "Temp-edge") { this.removeRepresentationsOfType("editor"); this.is_dragging = false; - var _hitResult = paper.project.hitTest(_event.point); - if (this.isEditable() && _hitResult && typeof _hitResult.item.__representation !== "undefined") { + var _hitResult = paper.project.hitTest(_point); + if (_hitResult && typeof _hitResult.item.__representation !== "undefined") { this.click_target = _hitResult.item.__representation; - if (this.click_target.type === "Node-link-button") { - this.removeRepresentationsOfType("editor"); - this.addTempEdge(this.click_target.source_representation, _event.point); - } + this.click_target.mousedown(_event, _isTouch); } else { this.click_target = null; if (this.isEditable() && this.click_mode === Rkns.Renderer._CLICKMODE_ADDNODE) { - var _coords = this.toModelCoords(_event.point), + var _coords = this.toModelCoords(_point), _data = { id: Rkns.Utils.getUID('node'), created_by: this.renkan.current_user, @@ -1930,7 +2229,7 @@ if (this.click_mode) { if (this.isEditable() && this.click_mode === Rkns.Renderer._CLICKMODE_STARTEDGE && this.click_target && this.click_target.type === "Node") { this.removeRepresentationsOfType("editor"); - this.addTempEdge(this.click_target, _event.point); + this.addTempEdge(this.click_target, _point); this.click_mode = Rkns.Renderer._CLICKMODE_ENDEDGE; this.notif_$.fadeOut(function() { Rkns.$(this).html(_renkan.translate("Click on a second node to complete the edge")).fadeIn(); @@ -1940,14 +2239,11 @@ this.click_mode = false; } } + paper.view.draw(); } -Rkns.Renderer.Scene.prototype.onMouseDrag = function(_event) { - this.is_dragging = true; - this.onMouseMove(_event); -} - -Rkns.Renderer.Scene.prototype.onMouseUp = function(_event) { +Rkns.Renderer.Scene.prototype.onMouseUp = function(_event, _isTouch) { + this.mouse_down = false; if (this.click_target) { var _off = this.canvas_$.offset(); this.click_target.mouseup( @@ -1956,12 +2252,17 @@ _event.pageX - _off.left, _event.pageY - _off.top ]) - } + }, + _isTouch ); } else { this.click_target = null; this.is_dragging = false; + if (_isTouch) { + this.unselectAll(); + } } + paper.view.draw(); } Rkns.Renderer.Scene.prototype.onScroll = function(_event, _scrolldelta) { @@ -1973,9 +2274,9 @@ _event.pageY - _off.top ]).subtract(this.offset).multiply( Math.SQRT2 - 1 ); if (this.totalScroll > 0) { - this.setScale( this.scale * Math.SQRT2, this.offset.subtract(_delta) ); + this.setScale( this.scale * Math.SQRT2, this.offset.subtract(_delta) ); } else { - this.setScale( this.scale * Math.SQRT1_2, this.offset.add(_delta.divide(Math.SQRT2))); + this.setScale( this.scale * Math.SQRT1_2, this.offset.add(_delta.divide(Math.SQRT2))); } this.totalScroll = 0; } @@ -2006,3 +2307,109 @@ } paper.view.draw(); } + +Rkns.Renderer.Scene.prototype.dropData = function(_data, _event) { + if (!this.isEditable()) { + return; + } + if (_data["text/json"] || _data["application/json"]) { + try { + var jsondata = JSON.parse(_data["text/json"] || _data["application/json"]); + _(_data).extend(jsondata); + } + catch(e) {} + } + var newNode = {}; + switch(_data["text/x-iri-specific-site"]) { + case "twitter": + var snippet = Rkns.$('
').html(_data["text/x-iri-selected-html"]), + tweetdiv = snippet.find(".tweet") + newNode.title = _renkan.translate("Tweet by ") + tweetdiv.attr("data-name"); + newNode.uri = "http://twitter.com/" + tweetdiv.attr("data-screen-name") + "/status/" + tweetdiv.attr("data-tweet-id"); + newNode.image = tweetdiv.find(".avatar").attr("src"); + newNode.description = tweetdiv.find(".js-tweet-text:first").text(); + break; + case "google": + var snippet = Rkns.$('
').html(_data["text/x-iri-selected-html"]); + newNode.title = snippet.find("h3:first").text().trim(); + newNode.uri = snippet.find("h3 a").attr("href"); + newNode.description = snippet.find(".st:first").text().trim(); + break; + case undefined: + default: + if (_data["text/x-iri-source-uri"]) { + newNode.uri = _data["text/x-iri-source-uri"]; + } + if (_data["text/plain"] || _data["text/x-iri-selected-text"]) { + newNode.description = (_data["text/plain"] || _data["text/x-iri-selected-text"]).replace(/[\s\n]+/gm,' ').trim(); + } + if (_data["text/html"] || _data["text/x-iri-selected-html"]) { + var snippet = Rkns.$('
').html(_data["text/html"] || _data["text/x-iri-selected-html"]); + var _imgs = snippet.find("img"); + if (_imgs.length) { + newNode.image = _imgs[0].src; + } + var _as = snippet.find("a"); + if (_as.length) { + newNode.uri = _as[0].href; + } + newNode.title = snippet.find("[title]").attr("title") || newNode.title; + newNode.description = snippet.text().replace(/[\s\n]+/gm,' ').trim(); + } + if (_data["text/uri-list"]) { + newNode.uri = _data["text/uri-list"]; + } + if (_data["text/x-moz-url"] && !newNode.title) { + newNode.title = (_data["text/x-moz-url"].split("\n")[1] || "").trim(); + if (newNode.title === newNode.uri) { + newNode.title = false; + } + } + if (_data["text/x-iri-source-title"] && !newNode.title) { + newNode.title = _data["text/x-iri-source-title"]; + } + if (_data["text/html"] || _data["text/x-iri-selected-html"]) { + newNode.image = snippet.find("[data-image]").attr("data-image") || newNode.image; + newNode.uri = snippet.find("[data-uri]").attr("data-uri") || newNode.uri; + newNode.title = snippet.find("[data-title]").attr("data-title") || newNode.title; + newNode.description = snippet.find("[data-description]").attr("data-description") || newNode.description; + } + } + if (!newNode.title) { + newNode.title = this.renkan.translate("Dragged resource"); + } + var fields = ["title", "description", "uri", "image"]; + for (var i = 0; i < fields.length; i++) { + var f = fields[i]; + if (_data["text/x-iri-" + f] || _data[f]) { + newNode[f] = _data["text/x-iri-" + f] || _data[f]; + } + if (newNode[f] === "none" || newNode[f] === "null") { + newNode[f] = undefined; + } + } + var _off = this.canvas_$.offset(), + _point = new paper.Point([ + _event.pageX - _off.left, + _event.pageY - _off.top + ]), + _coords = this.toModelCoords(_point), + _nodedata = { + id: Rkns.Utils.getUID('node'), + created_by: this.renkan.current_user, + uri: newNode.uri || "", + title: newNode.title || "", + description: newNode.description || "", + image: newNode.image || "", + color: newNode.color || undefined, + position: { + x: _coords.x, + y: _coords.y + } + }; + var _node = this.renkan.project.addNode(_nodedata), + _repr = this.getRepresentationByModel(_node); + if (_event.type === "drop") { + _repr.openEditor(); + } +}