Newer
Older
* Piwik - Web Analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
function switchSite(id, name, showAjaxLoading, idCanBeAll) {
if (id == 'all'
&& !idCanBeAll) {
broadcast.propagateNewPage('module=MultiSites&action=index');
}
else {
$('.sites_autocomplete input').val(id);
$('.custom_select_main_link')
.text(name)
.addClass('custom_select_loading');
$(function () {
var reset = function (selector) {
$('.websiteSearch', selector).val('');
$('.custom_select_ul_list', selector).show();
$(".siteSelect.ui-autocomplete,.reset", selector).hide();
};
// sets up every un-inited site selector widget
piwik.initSiteSelectors = function () {
function getUrlForWebsiteId(idSite) {
var idSiteParam = 'idSite=' + idSite;
var newParameters = 'segment=&' + idSiteParam;
var hash = broadcast.isHashExists() ? broadcast.getHashFromUrl() : "",
linkUrl = piwikHelper.getCurrentQueryStringWithParametersModified(newParameters)
+ '#' + piwikHelper.getQueryStringWithParametersModified(hash.substring(1), newParameters);
return linkUrl;
}
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
$('.sites_autocomplete').each(function () {
var selector = $(this);
if (selector.attr('data-inited') == 1) {
return;
}
selector.attr('data-inited', 1);
var websiteSearch = $('.websiteSearch', selector);
// when the search input is clicked, clear the input
websiteSearch.click(function () {
$(this).val('');
});
// when a key is released over the search input when empty, reset the selector
//
websiteSearch.keyup(function (e) {
if (e.keyCode == 27) {
$('.custom_select_block', selector).removeClass('custom_select_block_show');
return false;
}
if (!$(this).val()) {
reset(selector);
}
});
// setup the autocompleter
websiteSearch.autocomplete({
minLength: 1,
source: '?module=SitesManager&action=getSitesForAutocompleter',
appendTo: $('.custom_select_container', selector),
select: function (event, ui) {
Benaka Moorthi
a validé
event.preventDefault();
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
if (ui.item.id > 0) {
// set attributes of selected site display (what shows in the box)
$('.custom_select_main_link', selector)
.attr('siteid', ui.item.id)
.text(ui.item.name);
// hide the dropdown
$('.custom_select_block', selector).removeClass('custom_select_block_show');
// fire the site selected event
selector.trigger('piwik:siteSelected', ui.item);
}
else {
reset(selector);
}
return false;
},
focus: function (event, ui) {
$('.websiteSearch', selector).val(ui.item.name);
return false;
},
search: function (event, ui) {
$('.reset', selector).show();
$('.custom_select_main_link', selector).addClass('custom_select_loading');
},
open: function (event, ui) {
var widthSitesSelection = +$('.custom_select_ul_list', selector).width();
$('.custom_select_main_link', selector).removeClass('custom_select_loading');
var maxSitenameWidth = $('.max_sitename_width', selector);
if (widthSitesSelection > maxSitenameWidth.val()) {
maxSitenameWidth.val(widthSitesSelection);
}
else {
maxSitenameWidth = +maxSitenameWidth.val(); // convert to int
}
$('.custom_select_ul_list', selector).hide();
// customize jquery-ui's autocomplete positioning
var cssToRemove = {float: 'none', position: 'static'};
$('.siteSelect.ui-autocomplete', selector)
.show().width(widthSitesSelection).css(cssToRemove)
.find('li,a').each(function () {
$(this).css(cssToRemove);
});
$('.custom_select_block_show', selector).width(widthSitesSelection);
}
}).data("ui-autocomplete")._renderItem = function (ul, item) {
$(ul).addClass('siteSelect');
var linkUrl = getUrlForWebsiteId(item.id);
var link = $("<a></a>").html(item.label).attr('href', linkUrl),
listItem = $('<li></li>');
listItem.data("item.ui-autocomplete", item)
.append(link)
.appendTo(ul);
return listItem;
};
// when the reset button is clicked, reset the site selector
$('.reset', selector).click(reset);
// when mouse button is released on body, check if it is not over the site selector, and if not
// close it
$('body').on('mouseup', function (e) {
var closestSelector = $(e.target).closest('.sites_autocomplete');
if (!closestSelector.length || !closestSelector.is(selector)) {
if ($('.custom_select_block', selector).hasClass('custom_select_block_show')) {
reset(selector);
$('.custom_select_block', selector).removeClass('custom_select_block_show');
}
});
// set event handling code for non-jquery-autocomplete parts of widget
if ($('li', selector).length > 1) {
// event handler for when site selector is clicked. shows dropdown w/ first X sites
$(".custom_select", selector).click(function() {
$(".custom_select_block", selector).toggleClass("custom_select_block_show");
$(".websiteSearch", selector).val("").focus();
return false;
});
$('.custom_select_block', selector).on('mouseenter', function() {
$('.custom_select_ul_list > li > a', selector).each(function() {
var idSite = $(this).attr('siteid');
var linkUrl = getUrlForWebsiteId(idSite);
$(this).attr('href', linkUrl);
});
});
// change selection. fire's site selector's on select event and modifies the attributes
// of the selected link
$('.custom_select_ul_list li a', selector).each(function() {
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
$(this).click(function (e) {
var idsite = $(this).attr('siteid'),
name = $(this).text();
$(".custom_select_main_link", selector)
.attr('siteid', idsite)
.text(name);
selector.trigger('piwik:siteSelected', {id: idsite, name: name});
// close the dropdown
$(".custom_select_block", selector).removeClass("custom_select_block_show");
e.preventDefault();
});
});
var inlinePaddingWidth = 22, staticPaddingWidth = 34;
if ($(".custom_select_block ul", selector)[0]) {
var widthSitesSelection = Math.max(
$(".custom_select_block ul", selector).width() + inlinePaddingWidth,
$(".custom_select_main_link", selector).width() + staticPaddingWidth
);
$(".custom_select_block", selector).css('width', widthSitesSelection);
}
}
// handle multi-sites link click (triggers site selected event w/ id=all)
$('.custom_select_all', selector).click(function () {
$(".custom_select_block", selector).toggleClass("custom_select_block_show");
selector.trigger('piwik:siteSelected', {id: 'all', name: $('.custom_select_all>a', selector).text()});
});
// handle submit button click
$('.but', selector).on('click', function (e) {
if (websiteSearch.val() != '') {
websiteSearch.autocomplete('search', websiteSearch.val() + '%%%');
}
return false;
});
// if the data-switch-site-on-select attribute is set to 1 on the selector, set
// a default handler for piwik:siteSelected that switches the current site
// otherwise only update the input
selector.bind('piwik:siteSelected', function (e, site) {
if (1 == $(this).attr('data-switch-site-on-select')) {
if (piwik.idSite !== site.id) {
switchSite(site.id, site.name);
}
} else {
$('input', this).val(site.id);
}
});
});
};