client/js/main.js
changeset 284 fa8035885814
parent 242 570e18094e87
child 290 8a6eb26ac87f
--- a/client/js/main.js	Tue Apr 29 23:53:13 2014 +0200
+++ b/client/js/main.js	Mon May 05 17:43:37 2014 +0200
@@ -374,6 +374,7 @@
             }
         };
         _(_class.prototype).extend(_baseClass.prototype);
+        
         return _class;
         
     },
@@ -452,7 +453,94 @@
                 }
             }
         }
-    })()
+    })(),
+    /* The minimum distance (in pixels) the mouse has to move to consider an element was dragged */
+    _MIN_DRAG_DISTANCE: 2,
+    /* Distance between the inner and outer radius of buttons that appear when hovering on a node */
+    _NODE_BUTTON_WIDTH: 40,
+
+    _EDGE_BUTTON_INNER: 2,
+    _EDGE_BUTTON_OUTER: 40,
+    /* Constants used to know if a specific action is to be performed when clicking on the canvas */
+    _CLICKMODE_ADDNODE: 1,
+    _CLICKMODE_STARTEDGE: 2,
+    _CLICKMODE_ENDEDGE: 3,
+    /* Node size step: Used to calculate the size change when clicking the +/- buttons */
+    _NODE_SIZE_STEP: Math.LN2/4,
+    _MIN_SCALE: 1/20,
+    _MAX_SCALE: 20,
+    _MOUSEMOVE_RATE: 80,
+    _DOUBLETAP_DELAY: 800,
+    /* Maximum distance in pixels (squared, to reduce calculations)
+     * between two taps when double-tapping on a touch terminal */
+    _DOUBLETAP_DISTANCE: 20*20,
+    /* A placeholder so a default colour is displayed when a node has a null value for its user property */
+    _USER_PLACEHOLDER: function(_renkan) {
+        return {
+            color: _renkan.options.default_user_color,
+            title: _renkan.translate("(unknown user)"),
+            get: function(attr) {
+                return this[attr] || false;
+            }
+        };
+    },
+    /* The code for the "Drag and Add Bookmarklet", slightly minified and with whitespaces removed, though
+     * it doesn't seem that it's still a requirement in newer browsers (i.e. the ones compatibles with canvas drawing)
+     */
+    _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='<p_style=\"position:fixed;top:0;right:0;font:bold_18px_sans-serif;color:#fff;background:#909;padding:10px;z-index:100000;\">"
+        + _renkan.translate("Drag items from this website, drop them in Renkan").replace(/ /g,"_")
+        + "</p>'.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);})();";
+    },
+    /* Shortens text to the required length then adds ellipsis */
+    shortenText: function(_text, _maxlength) {
+        return (_text.length > _maxlength ? (_text.substr(0,_maxlength) + '…') : _text);
+    },
+    /* Drawing an edit box with an arrow and positioning the edit box according to the position of the node/edge being edited
+     * Called by Rkns.Renderer.NodeEditor and Rkns.Renderer.EdgeEditor */
+    drawEditBox: function(_options, _coords, _path, _xmargin, _selector) {
+        _selector.css({
+            width: ( _options.tooltip_width - 2* _options.tooltip_padding )
+        });
+        var _height = _selector.outerHeight() + 2* _options.tooltip_padding,
+        _isLeft = (_coords.x < paper.view.center.x ? 1 : -1),
+        _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;
+        _path.segments[0].point
+        = _path.segments[7].point
+        = _coords.add([_isLeft * _xmargin, 0]);
+        _path.segments[1].point.x
+        = _path.segments[2].point.x
+        = _path.segments[5].point.x
+        = _path.segments[6].point.x
+        = _left;
+        _path.segments[3].point.x
+        = _path.segments[4].point.x
+        = _right;
+        _path.segments[2].point.y
+        = _path.segments[3].point.y
+        = _top;
+        _path.segments[4].point.y
+        = _path.segments[5].point.y
+        = _bottom;
+        _path.segments[1].point.y = _coords.y - _options.tooltip_arrow_width / 2;
+        _path.segments[6].point.y = _coords.y + _options.tooltip_arrow_width / 2;
+        _path.closed = true;
+        _path.fillColor = new paper.GradientColor(new paper.Gradient([_options.tooltip_top_color, _options.tooltip_bottom_color]), [0,_top], [0, _bottom]);
+        _selector.css({
+            left: (_options.tooltip_padding + Math.min(_left, _right)),
+            top: (_options.tooltip_padding + _top)
+        });
+        return _path;
+    }
 };
 })(window);