Guest User

Untitled

a guest
Apr 26th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. /**
  2. * History.Routing
  3. *
  4. * @version 2.0
  5. *
  6. * @license MIT License
  7. * @author Harald Kirschner <mail [at] digitarald.de>
  8. * @copyright 2008 Author
  9. */
  10.  
  11. History.implement(new Options());
  12.  
  13. History.implement({
  14.  
  15. options: {
  16. separator: ';'
  17. },
  18.  
  19. routes: [],
  20.  
  21. register: function(route) {
  22. if (this.routes.push(route) == 1) this.addEvent('changed', this.match);
  23. },
  24.  
  25. unregister: function(route) {
  26. this.routes.remove(route);
  27. },
  28.  
  29. match: function(state, previous, manual) {
  30. if (!manual) this.routes.each(Function.methodize('match', this.state));
  31. },
  32.  
  33. generate: function() {
  34. return this.routes.map(Function.methodize('generate')).clean().join(this.options.separator);
  35. },
  36.  
  37. update: function() {
  38. return this.setState(this.generate());
  39. }
  40.  
  41. });
  42.  
  43. History.Route = new Class({
  44.  
  45. Implements: [Events, Options],
  46.  
  47. /**
  48. * pattern: Regular expression that matches the string updated from onGenerate
  49. * defaults: Default values array, initially empty.
  50. * flags: When regexp is a String, this is the second argument for new RegExp.
  51. * skipDefaults: default true; generate is not called when current values are similar to the default values.
  52. * generate: Should return the string for the state string, values are first argument
  53. * onMatch: Will be called when the regexp matches, with the new values as argument.
  54. */
  55. options: {
  56. skipDefaults: true,
  57. defaults: [],
  58. pattern: null,
  59. flags: '',
  60. generate: function(values) {
  61. return values[0];
  62. },
  63. onMatch: $empty
  64. },
  65.  
  66. initialize: function(options){
  67. this.setOptions(options);
  68. this.pattern = this.options.pattern || '(.*)';
  69. if ($type(this.pattern) == 'string') this.pattern = new RegExp(this.pattern, this.options.flags);
  70. this.values = this.defaults = this.options.defaults.slice();
  71. History.register(this);
  72. return this;
  73. },
  74.  
  75. setValues: function(values) {
  76. if (this.values.toString() == values.toString()) return this;
  77. this.values = values;
  78. History.update();
  79. return this;
  80. },
  81.  
  82. setValue: function(index, value) {
  83. if (this.values[index] == value) return this;
  84. this.values[index] = value;
  85. History.update();
  86. return this;
  87. },
  88.  
  89. build: function(values) {
  90. var tmp = this.values.slice();
  91. this.values = values;
  92. var state = History.generate();
  93. this.values = tmp;
  94. return state;
  95. },
  96.  
  97. destroy: function() {
  98. History.unregister(this);
  99. },
  100.  
  101. generate: function() {
  102. if (this.options.skipDefaultMatch && (String(this.values) == String(this.defaults))) return null;
  103. return this.options.generate.call(this, this.values);
  104. },
  105.  
  106. match: function(state) {
  107. var bits = state.match(this.pattern);
  108. var defaults = this.defaults;
  109. if (bits) {
  110. bits.splice(0, 1);
  111. for (var i = 0, j = bits.length; i < j; i++) bits[i] = $pick(bits[i], defaults[i] || null);
  112. if (String(bits) != String(defaults)) this.values = bits;
  113. } else {
  114. this.values = this.defaults.slice();
  115. }
  116. this.fireEvent('onMatch', [this.values, this.defaults]);
  117. }
  118.  
  119. });
  120.  
  121. Function.methodize = function(name) {
  122. var args = Array.slice(arguments, 1);
  123. return function(obj) {
  124. return obj[name].apply(obj, args);
  125. };
  126. };
Add Comment
Please, Sign In to add comment