diff options
Diffstat (limited to 'plugins/CoreHome/templates/broadcast.js')
-rw-r--r-- | plugins/CoreHome/templates/broadcast.js | 449 |
1 files changed, 212 insertions, 237 deletions
diff --git a/plugins/CoreHome/templates/broadcast.js b/plugins/CoreHome/templates/broadcast.js index d57bca82d9..9b5a9ffdf8 100644 --- a/plugins/CoreHome/templates/broadcast.js +++ b/plugins/CoreHome/templates/broadcast.js @@ -27,25 +27,25 @@ var broadcast = { */ _isInit: false, - /** - * Last known hash url without popover parameter - */ - currentHashUrl: false, - - /** - * Last known popover parameter - */ - currentPopoverParameter: false, - - /** - * Callbacks for popover parameter change - */ - popoverHandlers: [], - - /** - * Force reload once - */ - forceReload: false, + /** + * Last known hash url without popover parameter + */ + currentHashUrl: false, + + /** + * Last known popover parameter + */ + currentPopoverParameter: false, + + /** + * Callbacks for popover parameter change + */ + popoverHandlers: [], + + /** + * Force reload once + */ + forceReload: false, /** * Suppress content update on hash changing @@ -56,8 +56,8 @@ var broadcast = { * Initializes broadcast object * @return {void} */ - init: function() { - if(broadcast._isInit) { + init: function () { + if (broadcast._isInit) { return; } broadcast._isInit = true; @@ -75,15 +75,14 @@ var broadcast = { * 1. after calling $.history.init(); * 2. after calling $.history.load(); //look at broadcast.changeParameter(); * 3. after pushing "Go Back" button of a browser - * - * * Note: the method is manipulated in Overlay/templates/index.js - keep this in mind when making changes. + * + * * Note: the method is manipulated in Overlay/templates/index.js - keep this in mind when making changes. * * @param {string} hash to load page with * @return {void} */ - pageload: function( hash ) - { - broadcast.init(); + pageload: function (hash) { + broadcast.init(); // Unbind any previously attached resize handlers $(window).off('resize'); @@ -93,66 +92,66 @@ var broadcast = { broadcast.updateHashOnly = false; return; } - - // hash doesn't contain the first # character. - if( hash ) { - - var hashParts = hash.split('&popover='); - var hashUrl = hashParts[0]; - var popoverParam = ''; - if (hashParts.length > 1) { - popoverParam = hashParts[1]; - // in case the $ was encoded (e.g. when using copy&paste on urls in some browsers) - popoverParam = decodeURIComponent(popoverParam); - // revert special encoding from broadcast.propagateNewPopoverParameter() - popoverParam = popoverParam.replace(/\$/g, '%'); - popoverParam = decodeURIComponent(popoverParam); - } - - var pageUrlUpdated = (popoverParam == '' || - (broadcast.currentHashUrl !== false && broadcast.currentHashUrl != hashUrl)); - - var popoverParamUpdated = (popoverParam != '' && hashUrl == broadcast.currentHashUrl); - - if (broadcast.currentHashUrl === false) { - // new page load - pageUrlUpdated = true; - popoverParamUpdated = (popoverParam != ''); - } - - if (pageUrlUpdated || broadcast.forceReload) { - Piwik_Popover.close(); - - if (hashUrl != broadcast.currentHashUrl || broadcast.forceReload) { - // restore ajax loaded state - broadcast.loadAjaxContent(hashUrl); - - // make sure the "Widgets & Dashboard" is deleted on reload - $('#dashboardSettings').remove(); - $('#dashboardWidgetsArea').dashboard('destroy'); - } - } - - broadcast.forceReload = false; - broadcast.currentHashUrl = hashUrl; - broadcast.currentPopoverParameter = popoverParam; - - if (popoverParamUpdated && popoverParam == '') { - Piwik_Popover.close(); - } else if (popoverParamUpdated) { - var popoverParamParts = popoverParam.split(':'); - var handlerName = popoverParamParts[0]; - popoverParamParts.shift(); - var param = popoverParamParts.join(':'); - if (typeof broadcast.popoverHandlers[handlerName] != 'undefined') { - broadcast.popoverHandlers[handlerName](param); - } - } - - } else { - // start page - $('#content').empty(); - } + + // hash doesn't contain the first # character. + if (hash) { + + var hashParts = hash.split('&popover='); + var hashUrl = hashParts[0]; + var popoverParam = ''; + if (hashParts.length > 1) { + popoverParam = hashParts[1]; + // in case the $ was encoded (e.g. when using copy&paste on urls in some browsers) + popoverParam = decodeURIComponent(popoverParam); + // revert special encoding from broadcast.propagateNewPopoverParameter() + popoverParam = popoverParam.replace(/\$/g, '%'); + popoverParam = decodeURIComponent(popoverParam); + } + + var pageUrlUpdated = (popoverParam == '' || + (broadcast.currentHashUrl !== false && broadcast.currentHashUrl != hashUrl)); + + var popoverParamUpdated = (popoverParam != '' && hashUrl == broadcast.currentHashUrl); + + if (broadcast.currentHashUrl === false) { + // new page load + pageUrlUpdated = true; + popoverParamUpdated = (popoverParam != ''); + } + + if (pageUrlUpdated || broadcast.forceReload) { + Piwik_Popover.close(); + + if (hashUrl != broadcast.currentHashUrl || broadcast.forceReload) { + // restore ajax loaded state + broadcast.loadAjaxContent(hashUrl); + + // make sure the "Widgets & Dashboard" is deleted on reload + $('#dashboardSettings').remove(); + $('#dashboardWidgetsArea').dashboard('destroy'); + } + } + + broadcast.forceReload = false; + broadcast.currentHashUrl = hashUrl; + broadcast.currentPopoverParameter = popoverParam; + + if (popoverParamUpdated && popoverParam == '') { + Piwik_Popover.close(); + } else if (popoverParamUpdated) { + var popoverParamParts = popoverParam.split(':'); + var handlerName = popoverParamParts[0]; + popoverParamParts.shift(); + var param = popoverParamParts.join(':'); + if (typeof broadcast.popoverHandlers[handlerName] != 'undefined') { + broadcast.popoverHandlers[handlerName](param); + } + } + + } else { + // start page + $('#content').empty(); + } }, /** @@ -166,11 +165,10 @@ var broadcast = { * NOTE: this method will only make ajax call and replacing main content. * * @param {string} ajaxUrl querystring with parameters to be updated - * @param {boolean} disableHistory the hash change won't be available in the browser history + * @param {boolean} disableHistory the hash change won't be available in the browser history * @return {void} */ - propagateAjax: function (ajaxUrl, disableHistory) - { + propagateAjax: function (ajaxUrl, disableHistory) { broadcast.init(); // abort all existing ajax requests @@ -179,40 +177,35 @@ var broadcast = { // available in global scope var currentHashStr = broadcast.getHash(); - ajaxUrl = ajaxUrl.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); + for (var i = 0; i < params_vals.length; i++) { + currentHashStr = broadcast.updateParamValue(params_vals[i], currentHashStr); } // if the module is not 'Goals', we specifically unset the 'idGoal' parameter // this is to ensure that the URLs are clean (and that clicks on graphs work as expected - they are broken with the extra parameter) var action = broadcast.getParamValue('action', currentHashStr); - if( action != 'goalReport' && action != 'ecommerceReport') - { + if (action != 'goalReport' && action != 'ecommerceReport') { currentHashStr = broadcast.updateParamValue('idGoal=', currentHashStr); } // unset idDashboard if use doesn't display a dashboard var module = broadcast.getParamValue('module', currentHashStr); - if( module != 'Dashboard') - { + if (module != 'Dashboard') { currentHashStr = broadcast.updateParamValue('idDashboard=', currentHashStr); } - - if (disableHistory) - { - var newLocation = window.location.href.split('#')[0] + '#' + currentHashStr; - // window.location.replace changes the current url without pushing it on the browser's history stack - window.location.replace(newLocation); - } - else - { - // Let history know about this new Hash and load it. - broadcast.forceReload = true; - $.history.load(currentHashStr); - } + + if (disableHistory) { + var newLocation = window.location.href.split('#')[0] + '#' + currentHashStr; + // window.location.replace changes the current url without pushing it on the browser's history stack + window.location.replace(newLocation); + } + else { + // Let history know about this new Hash and load it. + broadcast.forceReload = true; + $.history.load(currentHashStr); + } }, /** @@ -238,14 +231,12 @@ var broadcast = { * @param {bool} showAjaxLoading whether to show the ajax loading gif or not. * @return {void} */ - propagateNewPage: function (str, showAjaxLoading) - { + propagateNewPage: function (str, showAjaxLoading) { // abort all existing ajax requests globalAjaxQueue.abort(); - - if (typeof showAjaxLoading === 'undefined' || showAjaxLoading) - { - piwikHelper.showAjaxLoading(); + + if (typeof showAjaxLoading === 'undefined' || showAjaxLoading) { + piwikHelper.showAjaxLoading(); } var params_vals = str.split("&"); @@ -255,22 +246,22 @@ var broadcast = { var currentHashStr = broadcast.getHashFromUrl(); var oldUrl = currentSearchStr + currentHashStr; - for( var i=0; i<params_vals.length; i++ ) { + 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); + currentSearchStr = broadcast.updateParamValue(params_vals[i], currentSearchStr); - if(currentHashStr.length != 0 ) { - currentHashStr = broadcast.updateParamValue(params_vals[i],currentHashStr); + if (currentHashStr.length != 0) { + currentHashStr = broadcast.updateParamValue(params_vals[i], currentHashStr); } } // Now load the new page. var newUrl = currentSearchStr + currentHashStr; - if(oldUrl == newUrl) { + if (oldUrl == newUrl) { window.location.reload(); } else { - this.forceReload = true; + this.forceReload = true; window.location.href = newUrl; } return false; @@ -296,79 +287,75 @@ var broadcast = { * @param {string} urlStr url to be updated * @return {string} urlStr with updated param */ - updateParamValue: function(newParamValue, urlStr) - { + updateParamValue: function (newParamValue, urlStr) { var p_v = newParamValue.split("="); var paramName = p_v[0]; - var valFromUrl = broadcast.getParamValue(paramName,urlStr); + var valFromUrl = broadcast.getParamValue(paramName, urlStr); // if set 'idGoal=' then we remove the parameter from the URL automatically (rather than passing an empty value) var paramValue = p_v[1]; - if(paramValue == '') - { + if (paramValue == '') { newParamValue = ''; } - if( valFromUrl != '') { + if (valFromUrl != '') { // replacing current param=value to newParamValue; - valFromUrl = valFromUrl.replace(/\$/g, '\\$'); - valFromUrl = valFromUrl.replace(/\./g, '\\.'); + valFromUrl = valFromUrl.replace(/\$/g, '\\$'); + valFromUrl = valFromUrl.replace(/\./g, '\\.'); var regToBeReplace = new RegExp(paramName + '=' + valFromUrl, 'ig'); - if(newParamValue == '') { + if (newParamValue == '') { // if new value is empty remove leading &, aswell regToBeReplace = new RegExp('[\&]?' + paramName + '=' + valFromUrl, 'ig'); } - urlStr = urlStr.replace( regToBeReplace, newParamValue ); - } else if(newParamValue != '') { + urlStr = urlStr.replace(regToBeReplace, newParamValue); + } else if (newParamValue != '') { urlStr += (urlStr == '') ? newParamValue : '&' + newParamValue; } return urlStr; }, - /** - * Update the part after the second hash - */ - propagateNewPopoverParameter: function(handlerName, value) - { - var hash = broadcast.getHashFromUrl(window.location.href); - var hashParts = hash.split('&popover='); - - var newHash = hashParts[0]; - if (handlerName) { - var popover = handlerName + ':' + value; - - // between jquery.history and different browser bugs, it's impossible to ensure - // that the parameter is en- and decoded the same number of times. in order to - // make sure it doesn't change, we have to manipulate the url encoding a bit. - popover = encodeURIComponent(popover); - popover = popover.replace(/%/g, '\$'); - newHash = hashParts[0] + '&popover=' + popover; - } - - window.location.href = 'index.php' + window.location.search + newHash; - }, - - /** - * Add a handler for the popover parameter - */ - addPopoverHandler: function(handlerName, callback) { - this.popoverHandlers[handlerName] = callback; - }, + /** + * Update the part after the second hash + */ + propagateNewPopoverParameter: function (handlerName, value) { + var hash = broadcast.getHashFromUrl(window.location.href); + var hashParts = hash.split('&popover='); + + var newHash = hashParts[0]; + if (handlerName) { + var popover = handlerName + ':' + value; + + // between jquery.history and different browser bugs, it's impossible to ensure + // that the parameter is en- and decoded the same number of times. in order to + // make sure it doesn't change, we have to manipulate the url encoding a bit. + popover = encodeURIComponent(popover); + popover = popover.replace(/%/g, '\$'); + newHash = hashParts[0] + '&popover=' + popover; + } + + window.location.href = 'index.php' + window.location.search + newHash; + }, + + /** + * Add a handler for the popover parameter + */ + addPopoverHandler: function (handlerName, callback) { + this.popoverHandlers[handlerName] = callback; + }, /** * Loads the given url with ajax and replaces the content - * - * Note: the method is replaced in Overlay/templates/index.js - keep this in mind when making changes. + * + * Note: the method is replaced in Overlay/templates/index.js - keep this in mind when making changes. * * @param {string} urlAjax url to load * @return {Boolean} */ - loadAjaxContent: function(urlAjax) - { + loadAjaxContent: function (urlAjax) { piwikMenu.activateMenu( - broadcast.getParamValue('module', urlAjax), - broadcast.getParamValue('action', urlAjax), - broadcast.getParamValue('idGoal', urlAjax) || broadcast.getParamValue('idDashboard', urlAjax) + broadcast.getParamValue('module', urlAjax), + broadcast.getParamValue('action', urlAjax), + broadcast.getParamValue('idGoal', urlAjax) || broadcast.getParamValue('idDashboard', urlAjax) ); piwikHelper.hideAjaxError('loadingError'); @@ -378,21 +365,20 @@ var broadcast = { urlAjax = urlAjax.match(/^\?/) ? urlAjax : "?" + urlAjax; broadcast.lastUrlRequested = urlAjax; - function sectionLoaded(content) - { - // if content is whole HTML document, do not show it, otherwise recursive page load could occur - var htmlDocType = '<!DOCTYPE'; - if (content.substring(0, htmlDocType.length) == htmlDocType) - { - return; - } - - if(urlAjax == broadcast.lastUrlRequested) { - $('#content').html( content ).show(); + function sectionLoaded(content) { + // if content is whole HTML document, do not show it, otherwise recursive page load could occur + var htmlDocType = '<!DOCTYPE'; + if (content.substring(0, htmlDocType.length) == htmlDocType) { + return; + } + + if (urlAjax == broadcast.lastUrlRequested) { + $('#content').html(content).show(); piwikHelper.hideAjaxLoading(); broadcast.lastUrlRequested = null; } } + var ajaxRequest = { type: 'POST', url: urlAjax, @@ -402,7 +388,7 @@ var broadcast = { success: sectionLoaded, // Callback when the request succeeds data: new Object }; - globalAjaxQueue.push( $.ajax(ajaxRequest) ); + globalAjaxQueue.push($.ajax(ajaxRequest)); return false; }, @@ -412,8 +398,7 @@ var broadcast = { * @param {string} status * @return {void} */ - customAjaxHandleError: function (deferred, status) - { + customAjaxHandleError: function (deferred, status) { broadcast.lastUrlRequested = null; piwikHelper.ajaxHandleError(deferred, status); }, @@ -424,11 +409,10 @@ var broadcast = { * * @return {string|false} */ - isHashExists: function() - { + isHashExists: function () { var hashStr = broadcast.getHashFromUrl(); - if ( hashStr != "" ) { + if (hashStr != "") { return hashStr; } else { return false; @@ -442,12 +426,11 @@ var broadcast = { * @param {string} url * @return {string} the hash part of the given url */ - getHashFromUrl: function(url) - { + 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); + if (url && url.match('#')) { + hashStr = url.substring(url.indexOf("#"), url.length); } else { hashStr = decodeURIComponent(location.hash); @@ -463,37 +446,34 @@ var broadcast = { * @param {string} url * @return {string} the query part of the given url */ - getSearchFromUrl: function(url) - { + getSearchFromUrl: function (url) { var searchStr = ""; // If url provided, give back the query string from url, else get query string from current address. - if( url && url.match(/\?/) ) { - searchStr = url.substring(url.indexOf("?"),url.length); + if (url && url.match(/\?/)) { + searchStr = url.substring(url.indexOf("?"), url.length); } else { searchStr = location.search; } return searchStr; }, - + /** * Returns all key-value pairs in query string of url. - * + * * @param {string} url url to check. if undefined, null or empty, current url is used. * @return {object} key value pair describing query string parameters */ - getValuesFromUrl: function(url) - { - var searchString = this._removeHashFromUrl(url).split('?')[1] || '', - pairs = searchString.split('&'); - - var result = {}; - for (var i = 0; i != pairs.length; ++i) - { - var pair = pairs[i].split(/=(.+)?/); // split only on first '=' - result[pair[0]] = pair[1]; - } - return result; + getValuesFromUrl: function (url) { + var searchString = this._removeHashFromUrl(url).split('?')[1] || '', + pairs = searchString.split('&'); + + var result = {}; + for (var i = 0; i != pairs.length; ++i) { + var pair = pairs[i].split(/=(.+)?/); // split only on first '=' + result[pair[0]] = pair[1]; + } + return result; }, /** @@ -506,10 +486,9 @@ var broadcast = { * @param {string} url url to check * @return {string} value of the given param within the given url */ - getValueFromUrl: function (param, url) - { + getValueFromUrl: function (param, url) { var searchString = this._removeHashFromUrl(url); - return broadcast.getParamValue(param,searchString); + return broadcast.getParamValue(param, searchString); }, /** @@ -519,15 +498,14 @@ var broadcast = { * @param {string} url url to check * @return {string} value of the given param within the hash part of the given url */ - getValueFromHash: function(param, url) - { + getValueFromHash: function (param, url) { var hashStr = broadcast.getHashFromUrl(url); - if (hashStr.substr(0, 1) == '#') { - hashStr = hashStr.substr(1); - } - hashStr = hashStr.split('#')[0]; + if (hashStr.substr(0, 1) == '#') { + hashStr = hashStr.substr(1); + } + hashStr = hashStr.split('#')[0]; - return broadcast.getParamValue(param,hashStr); + return broadcast.getParamValue(param, hashStr); }, @@ -541,17 +519,16 @@ var broadcast = { * @param {string} url url to check * @return {string} value of the given param within the given url */ - getParamValue: function (param, url) - { + getParamValue: function (param, url) { var lookFor = param + '='; var startStr = url.indexOf(lookFor); - if( startStr >= 0 ) { + if (startStr >= 0) { var endStr = url.indexOf("&", startStr); - if( endStr == -1 ) { + if (endStr == -1) { endStr = url.length; } - var value = url.substring(startStr + param.length +1,endStr); + var value = url.substring(startStr + param.length + 1, endStr); // sanitize values value = value.replace(/[^_%\+\-\<\>!@\$\.=,;0-9a-zA-Z]/gi, ''); @@ -565,26 +542,24 @@ var broadcast = { * Returns the hash without the starting # * @return {string} hash part of the current url */ - getHash: function () - { + getHash: function () { return broadcast.getHashFromUrl().replace(/^#/, '').split('#')[0]; }, - - /** - * Removes the hash portion of a URL and returns the rest. - * - * @param {string} url - * @return {string} url w/o hash - */ - _removeHashFromUrl: function(url) - { + + /** + * Removes the hash portion of a URL and returns the rest. + * + * @param {string} url + * @return {string} url w/o hash + */ + _removeHashFromUrl: function (url) { var searchString = ''; - if( url ) { + if (url) { var urlParts = url.split('#'); searchString = urlParts[0]; } else { searchString = location.search; } return searchString; - } + } }; |