VAHistory.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. VAHistory = function(w, changed) {
  2. this.window = w ? w : window;
  3. this.changed = changed;
  4. this.backs = [];
  5. this.forwards = [];
  6. this.last = null;
  7. this.isBack = false;
  8. this.isForward = false;
  9. this.isRefresh = false;
  10. this.back = jQuery.proxy(this.back, this);
  11. this.forward = jQuery.proxy(this.forward, this);
  12. }
  13. VAHistory.prototype = {
  14. active: function () {
  15. var title = this.window.document.title;
  16. var url = this.window.location.href;
  17. if (this.last && url != this.last.url) {
  18. this.backs.push(this.last);
  19. }
  20. this.last = { title: title, url: url };
  21. if (!this.isBack && !this.isForward && !this.isRefresh) {
  22. this.forwards = [];
  23. }
  24. if (jQuery.isFunction(this.changed)) {
  25. this.changed();
  26. }
  27. this.isBack = false;
  28. this.isForward = false;
  29. this.isRefresh = false;
  30. },
  31. canBack: function() {
  32. return this.backs.length > 0;
  33. },
  34. canForward: function() {
  35. return this.forwards.length > 0;
  36. },
  37. back: function() {
  38. if (this.canBack()) {
  39. if (this.last) {
  40. this.forwards.push(this.last);
  41. }
  42. this.last = this.backs.pop();
  43. this.isBack = true;
  44. this.window.location.href = this.last.url;
  45. }
  46. },
  47. forward: function() {
  48. if (this.canForward()) {
  49. if (this.last) {
  50. this.backs.push(this.last);
  51. }
  52. this.last = this.forwards.pop();
  53. this.isForward = true;
  54. this.window.location.href = this.last.url;
  55. }
  56. }
  57. }