forked from jupiterjs/jquerymx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistory.js
More file actions
172 lines (144 loc) · 4.48 KB
/
history.js
File metadata and controls
172 lines (144 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
steal.plugins('jquery/controller/subscribe',
'jquery/event/hashchange').then(function($){
/**
* @class
* The controller history plugin adds browser hash (#) based history support.
* This class itself helps break up parts of the hash of the url
* @constructor
* Takes a url and extracts information out of it.
* @param {Object} path
*/
var keyBreaker = /([^\[\]]+)|(\[\])/g;
$.Controller.History = {
/**
*
* returns the pathname part
*
* @codestart
* "#foo/bar&foo=bar" -> 'foo/bar'
* @codeend
*/
pathname : function(path) {
var parts = path.match(/#([^&]*)/);
return parts ? parts[1] : null
},
/**
* returns the search part, but without the first &
* @codestart
* "#foo/bar&foo=bar" -> 'foo=barr'
* @codeend
*/
search : function(path) {
var parts = path.match(/#[^&]*&(.*)/);
return parts ? parts[1] : null
},
getData: function(path) {
var search = $.Controller.History.search(path),
digitTest = /^\d+$/;
if(! search || ! search.match(/([^?#]*)(#.*)?$/) ) {
return {};
}
// Support the legacy format that used MVC.Object.to_query_string that used %20 for
// spaces and not the '+' sign;
search = search.replace(/\+/g,"%20")
var data = {},
pairs = search.split('&'),
current;
for(var i=0; i < pairs.length; i++){
current = data;
var pair = pairs[i].split('=');
// if we find foo=1+1=2
if(pair.length != 2) {
pair = [pair[0], pair.slice(1).join("=")]
}
var key = decodeURIComponent(pair[0]),
value = decodeURIComponent(pair[1]),
parts = key.match(keyBreaker);
for ( var j = 0; j < parts.length - 1; j++ ) {
var part = parts[j];
if (!current[part] ) {
current[part] = digitTest.test(part) || parts[j+1] == "[]" ? [] : {}
}
current = current[part];
}
lastPart = parts[parts.length - 1];
if(lastPart == "[]"){
current.push(value)
}else{
current[lastPart] = value;
}
}
return data;
}
};
jQuery(function($) {
$(window).bind('hashchange',function() {
var data = $.Controller.History.getData(location.href),
folders = $.Controller.History.pathname(location.href) || 'index',
hasSlash = (folders.indexOf('/') != -1);
if( !hasSlash && folders != 'index' ) {
folders += '/index';
}
OpenAjax.hub.publish("history."+folders.replace("/","."), data);
});
setTimeout(function(){
$(window).trigger('hashchange')
},1) //immediately after ready
})
$.extend($.Controller.prototype, {
/**
* Redirects to another page.
* @plugin 'dom/history'
* @param {Object} options an object that will turned into a url like #controller/action¶m1=value1
*/
redirectTo: function(options){
var point = this._get_history_point(options);
location.hash = point;
},
/**
* Redirects to another page by replacing current URL with the given one. This
* call will not create a new entry in the history.
* @plugin 'dom/history'
* @param {Object} options an object that will turned into a url like #controller/action¶m1=value1
*/
replaceWith: function(options){
var point = this._get_history_point(options);
location.replace(location.href.split('#')[0] + point);
},
/**
* Adds history point to browser history.
* @plugin 'dom/history'
* @param {Object} options an object that will turned into a url like #controller/action¶m1=value1
* @param {Object} data extra data saved in history -- NO LONGER SUPPORTED
*/
historyAdd : function(options, data) {
var point = this._get_history_point(options);
location.hash = point;
},
/**
* Creates a history point from given options. Resultant history point is like #controller/action¶m1=value1
* @plugin 'dom/history'
* @param {Object} options an object that will turned into history point
*/
_get_history_point: function(options) {
var controller_name = options.controller || this.Class.underscoreName;
var action_name = options.action || 'index';
/* Convert the options to parameters (removing controller and action if needed) */
if(options.controller)
delete options.controller;
if(options.action)
delete options.action;
var paramString = (options) ? $.param(options) : '';
if(paramString.length)
paramString = '&' + paramString;
return '#' + controller_name + '/' + action_name + paramString;
},
/**
* Provides current window.location parameters as object properties.
* @plugin 'dom/history'
*/
pathData :function() {
return $.Controller.History.getData(location.href);
}
});
});