Permalink
Browse files

scrolling madness

  • Loading branch information...
1 parent 6cd1f03 commit 7d34e084a44d96afd88458e2892356261a6fe97c @shatting committed Jul 17, 2011
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -36,6 +36,7 @@ var CodeMirror = (function(){
path: "",
parserfile: [],
basefiles: ["util.js", "stringstream.js", "select.js", "undo.js", "editor.js", "tokenize.js"],
+ addscripts: [],
iframeClass: null,
passDelay: 200,
passTime: 50,
@@ -102,13 +103,14 @@ var CodeMirror = (function(){
var sp = " spellcheck=\"" + (options.disableSpellcheck ? "false" : "true") + "\"";
var html = ["<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"><html" + sp + "><head>"];
+ var html = ["<!DOCTYPE HTML><html" + sp + "><head>"]; //TODO sheck if this change is ok
// Hack to work around a bunch of IE8-specific problems.
html.push("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=EmulateIE7\"/>");
var queryStr = options.noScriptCaching ? "?nocache=" + new Date().getTime().toString(16) : "";
forEach(options.stylesheet, function(file) {
html.push("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + file + queryStr + "\"/>");
});
- forEach(options.basefiles.concat(options.parserfile), function(file) {
+ forEach(options.basefiles.concat(options.parserfile).concat(options.addscripts), function(file) {
if (!/^https?:/.test(file) && !/^\//.test(file)) file = options.path + file;
html.push("<script type=\"text/javascript\" src=\"" + file + queryStr + "\"><" + "/script>");
});
@@ -0,0 +1,117 @@
+
+// SmoothScroll v1.0.1
+// Licensed under the terms of the MIT license.
+// Balázs Galambosi (c) 2011
+
+/**
+ * A module for middle mouse scrolling.
+ */
+(function(window){
+
+// local settings
+var img = document.createElement("div"); // img at the reference point
+img.setAttribute("class","scroller-vertical");
+img.setAttribute("id","scroller-vertical");
+document.body.appendChild(img);
+
+var scrolling = false; // guards one phase
+var enabled = true; // from settings
+var framerate = 400;
+
+// we check the OS for default middlemouse behavior only!
+var isLinux = (navigator.platform.indexOf("Linux") != -1);
+
+/**
+ * Shows the reference image, and binds event listeners for scrolling.
+ * It also manages the animation.
+ * @param {Object} event
+ */
+function mousedown(e) {
+ var isLink = false;
+ var elem = e.target;
+
+ // linux middle mouse shouldn't be overwritten (paste)
+ var linux = (isLinux && /input|textarea/i.test(elem.nodeName));
+
+ do {
+ isLink = isNodeName(elem, "a");
+ if (isLink) break;
+ } while (elem = elem.parentNode);
+
+ elem = overflowingAncestor(e.target);
+
+ // if it's not the middle button, or
+ // it's being used on an <a> element
+ // take the default action
+ if (!elem || !enabled || e.button !== 1 || isLink || linux) {
+ return true;
+ }
+
+ // we don't want the default by now
+ e.preventDefault();
+
+ // quit if there's an ongoing scrolling
+ if (scrolling) {
+ return false;
+ }
+
+ // set up a new scrolling phase
+ scrolling = true;
+
+ // reference point
+ img.style.left = e.clientX - 24 + "px";
+ img.style.top = e.clientY - 24 + "px";
+
+ img.style.display = "block";
+
+ var refereceX = e.clientX;
+ var refereceY = e.clientY;
+
+ var speedX = 0;
+ var speedY = 0;
+
+ // animation loop
+ var last = +new Date;
+ var delay = 1000 / framerate;
+ var finished = false;
+
+ requestFrame(function step(time){
+ var now = time || +new Date;
+ var elapsed = now - last;
+ elem.scrollLeft += (speedX * elapsed) >> 0;
+ elem.scrollTop += (speedY * elapsed) >> 0;
+ last = now;
+ if (!finished) {
+ requestFrame(step, elem, delay);
+ }
+ }, elem, delay);
+
+ var first = true;
+
+ function mousemove(e) {
+ if (first) {
+ addEvent("mouseup", remove);
+ first = false;
+ }
+ speedX = (e.clientX - refereceX) * 10 / 1000;
+ speedY = (e.clientY - refereceY) * 10 / 1000;
+ }
+
+ function remove(e) {
+ removeEvent("mousemove", mousemove);
+ removeEvent("mousedown", remove);
+ removeEvent("mouseup", remove);
+ removeEvent("keydown", remove);
+ img.style.display = "none";
+ scrolling = false;
+ finished = true;
+ }
+
+ addEvent("mousemove", mousemove);
+ addEvent("mousedown", remove);
+ addEvent("keydown", remove);
+}
+
+addEvent("mousedown", mousedown);
+
+})(window);
Oops, something went wrong.

0 comments on commit 7d34e08

Please sign in to comment.