Selaa lähdekoodia

Added page back/forward functionality as jquery plugin by hacking pathbinder.

Jeroen Vet 15 vuotta sitten
vanhempi
commit
bd0fed0890

+ 157 - 0
_attachments/scripts/jquery.tapirwiki.js

@@ -0,0 +1,157 @@
+(function($) {
+  // functions for handling the path
+  // thanks sammy.js
+ // var PATH_REPLACER = "([^\/]+)",
+//      PATH_NAME_MATCHER = /:([\w\d]+)/g,
+//      QUERY_STRING_MATCHER = /\?([^#]*)$/,
+//      SPLAT_MATCHER = /(\*)/,
+//      SPLAT_REPLACER = "(.+)",
+   var   _currentPath,
+         _lastPath,
+         _pathInterval;
+
+  function hashChanged() {
+    _currentPath = getPath();
+    // if path is actually changed from what we thought it was, then react
+    if (_lastPath != _currentPath) {
+      // return triggerOnPath(_currentPath);
+      wiki.open(_currentPath);
+    }
+  }
+  
+  $.tapirWiki = {
+//    changeFuns : [],
+//    paths : [],
+//    begin : function(defaultPath) {
+      // this should trigger the defaultPath if there's not a path in the URL
+      // otherwise it should trigger the URL's path
+//      $(function() {
+//        var loadPath = getPath();
+//        if (loadPath) {
+//          triggerOnPath(loadPath);
+//        } else {
+//          goPath(defaultPath);          
+//          triggerOnPath(defaultPath);
+//        }
+//      })
+//    },
+//    go : function(path) {
+//      goPath(path);
+//      triggerOnPath(path);
+//    },
+//    onChange : function (fun) {
+//      $.pathbinder.changeFuns.push(fun);
+//    },
+    pageChangeReset : function(page) {_lastPath=_currentPath=page;}
+  };
+
+  function pollPath(every) {
+    function hashCheck() {        
+      _currentPath = getPath();
+      // path changed if _currentPath != _lastPath
+      if (_lastPath != _currentPath) {
+        setTimeout(function() {
+          $(window).trigger('hashchange');
+        }, 1);
+      }
+    };
+    
+    hashCheck();
+    _pathInterval = setInterval(hashCheck, every);
+    $(window).bind('unload', function() {
+      clearInterval(_pathInterval);
+    });
+  }
+
+  //function triggerOnPath(path) {
+  //  $.pathbinder.changeFuns.forEach(function(fun) {fun(path)});
+  //  var pathSpec, path_params, params = {}, param_name, param;
+  //  for (var i=0; i < $.pathbinder.paths.length; i++) {
+  //    pathSpec = $.pathbinder.paths[i];
+      //$.log("pathSpec", pathSpec);
+  //    if ((path_params = pathSpec.matcher.exec(path)) !== null) {
+        //$.log("path_params", path_params);
+  //      path_params.shift();
+  //      for (var j=0; j < path_params.length; j++) {
+  //        param_name = pathSpec.param_names[j];
+  //        param = decodeURIComponent(path_params[j]);
+  //        if (param_name) {
+  //          params[param_name] = param;
+  //        } else {
+  //          if (!params.splat) params.splat = [];
+  //          params.splat.push(param);
+  //        }
+  //      };
+        // $.log("path trigger for "+path);
+  //      wiki.open(params.id);
+        // return true; // removed this to allow for multi match
+//      }
+//    };
+//  };
+
+  // bind the event
+  $(function() {
+    if ('onhashchange' in window) {
+      // we have a native event
+    } else {
+      pollPath(100);
+    }
+    // setTimeout(hashChanged,50);
+    $(window).bind('hashchange', hashChanged);
+  });
+
+//  function registerPath(pathSpec) {
+//    $.pathbinder.paths.push(pathSpec);
+//  };
+
+//  function setPath(pathSpec, params) {
+    //var newPath = $.mustache(pathSpec.template, params);
+//    var newPath = params
+//    goPath(newPath);
+//  };
+  
+  function goPath(newPath) {
+    window.location = 'wiki.html#'+newPath;
+    _lastPath = getPath();
+  };
+  
+  function getPath() {
+    var matches = window.location.toString().match(/^[^#]*#(.+)$/);
+    return matches ? matches[1] : '';
+  };
+
+//  function makePathSpec(path, callback) {
+//    var param_names = [];
+//    var template = "";
+    
+//    PATH_NAME_MATCHER.lastIndex = 0;
+    
+//    while ((path_match = PATH_NAME_MATCHER.exec(path)) !== null) {
+//      param_names.push(path_match[1]);
+//    }
+
+//    return {
+//      param_names : param_names,
+//      matcher : new RegExp(path.replace(
+//        PATH_NAME_MATCHER, PATH_REPLACER).replace(
+//        SPLAT_MATCHER, SPLAT_REPLACER) + "$"),
+//      template : path.replace(PATH_NAME_MATCHER, function(a, b) {
+//        return '{{'+b+'}}';
+//      }).replace(SPLAT_MATCHER, '{{splat}}'),
+//      callback : callback
+//    };
+//  };
+
+//  $.fn.pathbinder = function(func, path) {
+    // var self = $(this);
+//    var pathSpec = makePathSpec(path, func);
+//    self.bind(name, function(ev, params) {
+      // set the path when triggered
+      // $.log("set path", name, pathSpec)
+//      setPath(pathSpec, params);
+//    });
+    // trigger when the path matches
+//    registerPath(pathSpec);
+//  };
+})(jQuery);
+  

+ 9 - 2
_attachments/scripts/tapirwiki.js

@@ -35,6 +35,7 @@ wiki.edited_by = "";
 wiki.edited_on = "";
 wiki.type = "page";
 
+
 wiki.save = function() {
 
 	if(wiki._rev == null)
@@ -80,6 +81,7 @@ wiki.display = function() {
 	$("<li class='pageSpecific'><a href='Javascript: wiki.attachments()'>Attachments</a></li>").appendTo("#page-menu");
 	$("<li class='pageSpecific'><a href='Javascript: wiki.remove()'>Delete</a></li>").appendTo("#page-menu");
 	window.location = "wiki.html#" + this._id;
+        $.tapirWiki.pageChangeReset(this._id);
 };
 
 wiki.init = function() {
@@ -133,7 +135,7 @@ wiki.open = function(id) {
 };
 
 wiki.edit = function() {
-
+                $.tapirWiki.pageChangeReset(this._id);
 		$("#inner-content").html("");
 		$("<h2>" + this._id + "</h2>");
 
@@ -523,6 +525,7 @@ var pages;
 }
 
 
+
 //And finally...when the document is ready...
 
 $(document).ready(function()
@@ -557,6 +560,8 @@ $(document).ready(function()
 				    //If it's blank, lets get it from settings
 				    requestedPage  = settings.defaultPage;
 			    }
+                            // JV: set it as the base page for pathfinder
+                            
 
 			    //Now let's open the page
 			    wiki.open(requestedPage);
@@ -611,4 +616,6 @@ function pop(obj) {
 }
 
 
-
+//JV trial
+// $(document).bind("openwiki", function (e, params) { wiki.open(params.id);}); this is nonsense we don't want a click event
+//$(document).pathbinder(wiki.open,":id");

+ 1 - 0
_attachments/wiki.html

@@ -3,6 +3,7 @@
 <head>
 <link rel="stylesheet" href="style/tapirwiki.css" type="text/css">
 <script src="scripts/jQuery.js"></script>
+<script src="scripts/jquery.tapirwiki.js"></script>
 <script src="scripts/json2.js"></script>
 <script src="scripts/parser.js"></script>
 <script src="scripts/renderer.js"></script>