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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
/* broadcast object is to help maintain a hash for link clicks and ajax calls
* so we can have back button and refresh button working.
*
* Other file that currently depending on this are:
* calendar.js
* period_select.tpl
* sites_selections.tpl
* menu.js, ...etc
*/
// Load this once and only once.
broadcast = {};
broadcast.init = function() {
if(typeof broadcast.isInit != 'undefined') {
return;
}
broadcast.isInit = true;
// Initialize history plugin.
// The callback is called at once by present location.hash
$.historyInit(broadcast.pageload);
}
/************************************************
*
* Broadcast Main Methods:
*
************************************************/
/**========== PageLoad function =================
* This function is called when:
* 1. after calling $.historyInit();
* 2. after calling $.historyLoad(); //look at broadcast.changeParameter();
* 3. after pushing "Go Back" button of a browser
*/
broadcast.pageload = function( hash ) {
broadcast.init();
// hash doesn't contain the first # character.
if( hash ) {
// restore ajax loaded state
broadcast.loadAjaxContent(hash);
} else {
// start page
$('#content').empty();
}
};
/* ============================================
* propagateAjax -- update hash values then make ajax calls.
* example :
* 1) <a href="javascript:broadcast.propagateAjax('module=Referers&action=getKeywords')">View keywords report</a>
* 2) Main menu li also goes through this function. check out onClickLi();
*
* Will propagate your new value into the current hash string and make ajax calls.
*
* NOTE: this method will only make ajax call and replacing main content.
*/
broadcast.propagateAjax = function (ajaxUrl)
{
broadcast.init();
// available in global scope
var currentHashStr = window.location.hash;
// Because $.history plugin doens't care about # or ? sign infront of the query string
// We take it out if exist;
currentHashStr = currentHashStr.replace(/^\?|^#/,'');
ajaxUrl = ajaxUrl.replace(/^\?|&#/,'');
var params_vals = ajaxUrl.split("&");
for( var i=0; i<params_vals.length; i++ ) {
currentHashStr = broadcast.updateParamValue(params_vals[i],currentHashStr);
}
// Let history know about this new Hash and load it.
$.historyLoad(currentHashStr);
};
/*
* propagateNewPage() -- update url value and load new page,
* Example:
* 1) We want to update idSite to both search query and hash then reload the page,
* 2) update period to both search query and hash then reload page.
*
* ** If you'd like to make ajax call with new values then use propagateAjax ** *
*
* Expecting:
* str = "param1=newVal1¶m2=newVal2";
*
* Currently being use by:
*
* handlePeriodClick,
* calendar.js,
* sites_seletion.tpl
*
* NOTE: This method will refresh the page with new values.
*/
broadcast.propagateNewPage = function (str)
{
broadcast.init();
var params_vals = str.split("&");
// available in global scope
var currentSearchStr = window.location.search;
var currentHashStr = window.location.hash;
for( var i=0; i<params_vals.length; i++ ) {
// update both the current search query and hash string
currentSearchStr = broadcast.updateParamValue(params_vals[i],currentSearchStr);
if(currentHashStr.length != 0 ) {
currentHashStr = broadcast.updateParamValue(params_vals[i],currentHashStr);
}
}
// Now load the new page.
window.location.href = currentSearchStr + currentHashStr;
};
/*************************************************
*
* Broadcast Supporter Methods:
*
*************************************************/
/*
* updateParamValue(newParamValue,urlStr) -- Helping propagate funtions to update value to url string.
* eg. I want to update date value to search query or hash query
*
* Expecting:
* urlStr : A Hash or search query string. e.g: module=whatever&action=index=date=yesterday
* newParamValue : A param value pair: e.g: date=2009-05-02
*
* Return module=whatever&action=index&date=2009-05-02
*/
broadcast.updateParamValue = function(newParamValue,urlStr)
{
var p_v = newParamValue.split("=");
var paramName = p_v[0];
var valFromUrl = broadcast.getParamValue(paramName,urlStr);
if( valFromUrl != '') {
// replacing current param=value to newParamValue;
var regToBeReplace = new RegExp(paramName + '=' + valFromUrl, 'ig');
urlStr = urlStr.replace( regToBeReplace, newParamValue );
} else {
urlStr += (urlStr == '') ? newParamValue : '&' + newParamValue;
}
return urlStr;
};
/*
* broadcast.loadAjaxContent
*/
broadcast.loadAjaxContent = function(urlAjax)
{
urlAjax = urlAjax.match(/^\?/) ? urlAjax : "?" + urlAjax;
// showing loading...
$('#loadingPiwik').show();
$('#content').hide();
$("object").remove();
broadcast.lastUrlRequested = urlAjax;
function sectionLoaded(content)
{
if(content.substring(0, 14) == '<!DOCTYPE html') {
window.location.reload();
return;
}
if(urlAjax == broadcast.lastUrlRequested) {
$('#content').html( content ).show();
$('#loadingPiwik').hide();
broadcast.lastUrlRequested = null;
}
}
piwikMenu.activateMenu(
broadcast.getParamValue('module', urlAjax),
broadcast.getParamValue('action', urlAjax),
broadcast.getParamValue('idGoal', urlAjax)
);
ajaxRequest = {
type: 'GET',
url: urlAjax,
dataType: 'html',
async: true,
error: broadcast.customAjaxHandleError, // Callback when the request fails
success: sectionLoaded, // Callback when the request succeeds
data: new Object
};
$.ajax(ajaxRequest);
return false;
};
broadcast.customAjaxHandleError = function ()
{
broadcast.lastUrlRequested = null;
ajaxHandleError();
};
/*
* Return hash string if hash exists on address bar.
* else return false;
*/
broadcast.isHashExists = function()
{
var hashStr = broadcast.getHashFromUrl();
if ( hashStr != "" ) {
return hashStr;
} else {
return false;
}
},
/*
* Get Hash from given url or from current location.
* return empty string if no hash present.
*/
broadcast.getHashFromUrl = function(url)
{
var hashStr = "";
// If url provided, give back the hash from url, else get hash from current address.
if( url && url.match('#') ) {
hashStr = url.substring(url.indexOf("#"),url.length);
}
else {
hashStr = location.hash;
}
return hashStr;
};
/*
* Get search query from given url or from current location.
* return empty string if no search query present.
*/
broadcast.getSearchFromUrl = function(url)
{
var searchStr = "";
// If url provided, give back the hash from url, else get hash from current address.
if( url && url.match(/\?/) ) {
searchStr = url.substring(url.indexOf("?"),url.length);
} else {
searchStr = location.search;
}
return searchStr;
};
/*
* help to get param value for any given url string with provided param name
* if no url is provided, it will get param from current address.
* return:
* Empty String if param is not found.
*/
broadcast.getValueFromUrl = function (param, url)
{
var searchString = '';
if( url ) {
var urlParts = url.split('#');
searchString = urlParts[0];
} else {
searchString = location.search;
}
return broadcast.getParamValue(param,searchString);
};
/*
* help to get value from hash parameter for any given url string with provided param name
* if no url is provided, it will get param from current address.
* return:
* Empty String if param is not found.
*/
broadcast.getValueFromHash = function(param, url)
{
var hashStr = this.getHashFromUrl(url);
return broadcast.getParamValue(param,hashStr);
};
/*
* return value for the requested param, will return the first match.
* out side of this class should use getValueFromHash() or getValueFromUrl() instead.
* return:
* Empty String if param is not found.
*/
broadcast.getParamValue = function (param, url)
{
var startStr = url.indexOf(param);
if( startStr >= 0 ) {
var endStr = url.indexOf("&", startStr);
if( endStr == -1 ) {
endStr = url.length;
}
return url.substring(startStr + param.length +1,endStr);
} else {
return '';
}
};
|