7
* @author Harald Kirschner <mail [at] digitarald.de>
8
* @copyright 2008 Author
11
History.implement(new Options());
21
register: function(route) {
22
if (this.routes.push(route) == 1) this.addEvent('changed', this.match);
25
unregister: function(route) {
26
this.routes.remove(route);
29
match: function(state, previous, manual) {
30
if (!manual) this.routes.each(Function.methodize('match', this.state));
33
generate: function() {
34
return this.routes.map(Function.methodize('generate')).clean().join(this.options.separator);
38
return this.setState(this.generate());
43
History.Route = new Class({
45
Implements: [Events, Options],
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.
60
generate: function(values) {
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);
75
setValues: function(values) {
76
if (this.values.toString() == values.toString()) return this;
82
setValue: function(index, value) {
83
if (this.values[index] == value) return this;
84
this.values[index] = value;
89
build: function(values) {
90
var tmp = this.values.slice();
92
var state = History.generate();
98
History.unregister(this);
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);
106
match: function(state) {
107
var bits = state.match(this.pattern);
108
var defaults = this.defaults;
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;
114
this.values = this.defaults.slice();
116
this.fireEvent('onMatch', [this.values, this.defaults]);
121
Function.methodize = function(name) {
122
var args = Array.slice(arguments, 1);
123
return function(obj) {
124
return obj[name].apply(obj, args);