File size: 11,087 Bytes
da96562 | 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 307 308 309 310 311 312 | /*!
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
var Piwik_Popover = (function () {
var container = false;
var isOpen = false;
var closeCallback = false;
var isProgrammaticClose = false;
var scrollTopPosition = 0;
var ajaxLoadingRequest = null;
var createContainer = function () {
if (container === false) {
container = $(document.createElement('div')).attr('id', 'Piwik_Popover');
}
if (!$('#Piwik_Popover_Wrapper').length) {
$(document.createElement('div')).attr('id', 'Piwik_Popover_Wrapper').appendTo('body');
}
};
var openPopover = function (title, dialogClass) {
createContainer();
var options =
{
title: title,
modal: true,
width: 1050,
resizable: false,
autoOpen: true,
classes: {
"ui-dialog": 'ui-dialog--responsive',
},
appendTo: '#Piwik_Popover_Wrapper',
open: function (event, ui) {
if (dialogClass) {
$(this).parent().addClass(dialogClass).attr('style', '');
}
$('.ui-widget-overlay').on('click.popover', function () {
// if clicking outside of the dialog, close entire stack
broadcast.resetPopoverStack();
container.dialog('close');
});
// sometimes the modal can be displayed outside of the current viewport, in this case
// we scroll to it to make sure it's visible. this isn't a perfect workaround, since it
// doesn't center the modal.
scrollTopPosition = $(window).scrollTop();
$('#root').css({
position: 'fixed',
height: $(window).height + scrollTopPosition,
width: '100%',
top: -scrollTopPosition
});
window.scrollTo(0, 0);
},
close: function (event, ui) {
container.find('div.jqplot-target').trigger('piwikDestroyPlot');
container[0].innerHTML = '';
container.dialog('destroy').remove();
if (ajaxLoadingRequest) {
ajaxLoadingRequest.abort();
}
$('.ui-widget-overlay').off('click.popover');
isOpen = false;
require('piwik/UI').UIControl.cleanupUnusedControls();
if (typeof closeCallback == 'function') {
closeCallback();
closeCallback = false;
}
// just to avoid any annoying race conditions that cause tooltips to remain on the screen permanently,
// remove any that still exist
$('body > .ui-tooltip').remove();
// if we were not called by Piwik_Popover.close(), then the user clicked the close button or clicked
// the overlay, in which case we want to handle the popover URL as well as the actual modal.
if (!isProgrammaticClose || isEscapeKey(event)) {
broadcast.propagateNewPopoverParameter(false);
}
$('#root').css({
position: '',
height: '',
width: '',
top: ''
});
window.scrollTo(0, scrollTopPosition);
}
};
container.dialog(options);
// override the undocumented _title function to ensure that the title attribute is not escaped (according to jQueryUI bug #6016)
container.data( "uiDialog" )._title = function(title) {
title.html( this.options.title );
};
isOpen = true;
};
return {
/**
* Open the popover with a loading message
*
* @param {string} popoverName name of the popover
* @param {string} [popoverSubject] subject of the popover (e.g. url, optional)
* @param {int} [height] height of the popover in px (optional)
* @param {string} [dialogClass] css class to add to dialog
*/
showLoading: function (popoverName, popoverSubject, height, dialogClass) {
var loading = $(document.createElement('div')).addClass('Piwik_Popover_Loading');
var loadingMessage = popoverSubject ? translations.General_LoadingPopoverFor :
translations.General_LoadingPopover;
loadingMessage = sprintf(loadingMessage, popoverName);
var p1 = $(document.createElement('p')).addClass('Piwik_Popover_Loading_Name');
loading.append(p1.text(loadingMessage));
var p2;
if (popoverSubject) {
popoverSubject = piwikHelper.addBreakpointsToUrl(popoverSubject);
p1.addClass('Piwik_Popover_Loading_NameWithSubject');
p2 = $(document.createElement('p')).addClass('Piwik_Popover_Loading_Subject');
loading.append(p2.html(popoverSubject));
}
if (height) {
loading.height(height);
}
if (!isOpen) {
openPopover(null, dialogClass);
}
this.setContent(loading);
this.setTitle('');
if (height) {
var offset = loading.height() - p1.outerHeight();
if (popoverSubject) {
offset -= p2.outerHeight();
}
var spacingEl = $(document.createElement('div'));
spacingEl.height(Math.round(offset / 2));
loading.prepend(spacingEl);
}
return container;
},
/**
* Add a help button to the current popover
*
* @param {string} helpUrl
*/
addHelpButton: function (helpUrl) {
if (!isOpen) {
return;
}
var titlebar = container.parent().find('.ui-dialog-titlebar');
var button = $(document.createElement('a')).addClass('ui-dialog-titlebar-help');
button.attr({href: helpUrl, target: '_blank'});
titlebar.append(button);
},
/** Set the title of the popover */
setTitle: function (titleHtml) {
var titleText = piwikHelper.htmlDecode(titleHtml);
if (titleText.length > 60) {
titleHtml = $('<span>').attr('class', 'tooltip').attr('title', titleText).html(titleHtml);
}
container.dialog('option', 'title', titleHtml);
try {
$('.tooltip', container.parentNode).tooltip('destroy');
} catch (e) {}
if (titleText.length > 60) {
$('.tooltip', container.parentNode).tooltip({track: true, items: '.tooltip'});
}
},
/** Set inner HTML of the popover */
setContent: function (html) {
if (typeof closeCallback == 'function') {
closeCallback();
closeCallback = false;
}
container.html(html);
container.children().each(function (i, childNode) {
piwikHelper.compileVueEntryComponents(childNode);
});
},
/**
* Show an error message. All params are HTML!
*
* @param {string} title
* @param {string} [message]
* @param {string} [backLabel]
*/
showError: function (title, message, backLabel) {
var error = $(document.createElement('div')).addClass('Piwik_Popover_Error');
var p = $(document.createElement('p')).addClass('Piwik_Popover_Error_Title');
error.append(p.html(title));
if (message) {
p = $(document.createElement('p')).addClass('Piwik_Popover_Error_Message');
error.append(p.html(message));
}
if (backLabel) {
var back = $(document.createElement('a')).addClass('Piwik_Popover_Error_Back');
back.attr('href', '#').click(function () {
history.back();
return false;
});
error.append(back.html(backLabel));
}
if (!isOpen) {
openPopover();
}
this.setContent(error);
},
/**
* Add a callback for the next time the popover is closed or the content changes
*
* @param {function} callback
*/
onClose: function (callback) {
closeCallback = callback;
},
/**
* Close the popover.
*
* Note: this method shouldn't normally be used to close a popover, instead
* `broadcast.propagateNewPopoverHandler(false)` should be used.
*/
close: function () {
if (isOpen) {
isProgrammaticClose = true;
container.dialog('close');
isProgrammaticClose = false;
}
},
/**
* Create a Popover and load the specified URL in it.
*
* Note: If you want the popover to be persisted in the URL (so if the URL is copy/pasted
* to a new window/tab it will be opened there), use broadcast.propagateNewPopoverParameter
* with a popover handler function that calls this one.
*
* @param {string} url
* @param {string} loadingName
* @param {string} [dialogClass] css class to add to dialog
* @param {object} [ajaxRequest] optional instance of ajaxHelper
*/
createPopupAndLoadUrl: function (url, loadingName, dialogClass, ajaxRequest) {
// open the popover
var box = Piwik_Popover.showLoading(loadingName, null, null, dialogClass);
var callback = function (html) {
function setPopoverTitleIfOneFoundInContainer() {
var title = $('h1,h2', container);
if (title.length == 1) {
Piwik_Popover.setTitle(title.text());
$(title).hide();
}
}
Piwik_Popover.setContent(html);
setPopoverTitleIfOneFoundInContainer();
};
if ('undefined' === typeof ajaxRequest) {
ajaxRequest = new ajaxHelper();
}
ajaxRequest.addParams(piwikHelper.getArrayFromQueryString(url), 'get');
ajaxRequest.setCallback(callback);
ajaxRequest.setFormat('html');
ajaxRequest.send();
ajaxLoadingRequest = ajaxRequest;
},
isOpen: function() {
return isOpen;
}
};
})();
|