File size: 16,006 Bytes
a63a502 a2f6883 76a5158 9eea97b a2f6883 9eea97b a2f6883 8a554f1 bdfd65a a63a502 76763d7 a47eab3 76a5158 76763d7 586b47c 76a5158 da9a4c9 10f92b3 76a5158 e09eecc 76a5158 586b47c 76a5158 76763d7 a47eab3 3ce1b66 a47eab3 ad0769b 6a7ee1e e5643f8 6a7ee1e e5643f8 fa4fbb8 76a5158 76763d7 0faac17 3ce1b66 76763d7 3ce1b66 9eea97b 76763d7 6a7ee1e 76763d7 a63a502 76763d7 76a5158 a47eab3 3ce1b66 76a5158 76763d7 e5643f8 9eea97b 76763d7 fa4fbb8 76a5158 7b286e7 fa4fbb8 ad0769b ae92478 fa4fbb8 e57d498 fa4fbb8 074db26 edc3df5 fa4fbb8 e57d498 fa4fbb8 e57d498 d5be768 fa4fbb8 5ddc2a6 fa4fbb8 5ddc2a6 fa4fbb8 1d58af6 fa4fbb8 1d58af6 b9e11b1 88eeb96 fa4fbb8 e5643f8 76a5158 3ce1b66 76a5158 ad0769b 76a5158 10f92b3 76a5158 e09eecc 76a5158 fa4fbb8 a47eab3 3ce1b66 fa4fbb8 da9a4c9 fa4fbb8 edc3df5 da9a4c9 edc3df5 074db26 da9a4c9 074db26 da9a4c9 074db26 fa4fbb8 a47eab3 3ce1b66 fa4fbb8 da9a4c9 fa4fbb8 e57d498 da9a4c9 e57d498 da9a4c9 e57d498 d5be768 da9a4c9 d5be768 fa4fbb8 a47eab3 3ce1b66 fa4fbb8 da9a4c9 fa4fbb8 a2f6883 5ddc2a6 da9a4c9 5ddc2a6 da9a4c9 5ddc2a6 fa4fbb8 a47eab3 3ce1b66 fa4fbb8 da9a4c9 fa4fbb8 a2f6883 fa4fbb8 1d58af6 da9a4c9 1d58af6 b9e11b1 5ddc2a6 b9e11b1 da9a4c9 5ddc2a6 b9e11b1 1d58af6 88eeb96 fa4fbb8 76a5158 a47eab3 3ce1b66 fa4fbb8 76763d7 fa4fbb8 76763d7 fa4fbb8 76763d7 fa4fbb8 76763d7 fa4fbb8 76763d7 |
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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
import Element from './element';
import {
CLASS_BTN_DISABLED,
CLASS_CLOSE_BTN,
CLASS_NEXT_STEP_BTN,
CLASS_POPOVER_DESCRIPTION,
CLASS_POPOVER_FOOTER,
CLASS_POPOVER_TIP,
CLASS_POPOVER_TITLE,
CLASS_PREV_STEP_BTN,
ID_POPOVER,
POPOVER_HTML,
} from '../common/constants';
import { createNodeFromString } from '../common/utils';
/**
* Popover that is displayed on top of the highlighted element
*/
export default class Popover extends Element {
/**
* @param {Object} options
* @param {Window} window
* @param {Document} document
*/
constructor(options, window, document) {
super();
this.options = {
isFirst: true,
isLast: true,
totalCount: 1,
currentIndex: 0,
offset: 0,
showButtons: true,
closeBtnText: 'Close',
doneBtnText: 'Done',
startBtnText: 'Next →',
nextBtnText: 'Next →',
prevBtnText: '← Previous',
...options,
};
this.window = window;
this.document = document;
}
/**
* Prepares the dom element for popover
* @private
*/
attachNode() {
let popover = this.document.getElementById(ID_POPOVER);
if (popover) {
popover.parentElement.removeChild(popover);
}
popover = createNodeFromString(POPOVER_HTML(this.options.className));
document.body.appendChild(popover);
this.node = popover;
this.tipNode = popover.querySelector(`.${CLASS_POPOVER_TIP}`);
this.titleNode = popover.querySelector(`.${CLASS_POPOVER_TITLE}`);
this.descriptionNode = popover.querySelector(`.${CLASS_POPOVER_DESCRIPTION}`);
this.footerNode = popover.querySelector(`.${CLASS_POPOVER_FOOTER}`);
this.nextBtnNode = popover.querySelector(`.${CLASS_NEXT_STEP_BTN}`);
this.prevBtnNode = popover.querySelector(`.${CLASS_PREV_STEP_BTN}`);
this.closeBtnNode = popover.querySelector(`.${CLASS_CLOSE_BTN}`);
}
/**
* Gets the title node for the popover
* @returns {Element | null | *}
* @public
*/
getTitleNode() {
return this.titleNode;
}
/**
* Gets the description node for the popover
* @returns {Element | null | *}
* @public
*/
getDescriptionNode() {
return this.descriptionNode;
}
/**
* Hides the popover
* @public
*/
hide() {
this.node.style.display = 'none';
}
/**
* Sets the default state for the popover
* @private
*/
setInitialState() {
this.node.style.display = 'block';
this.node.style.left = '0';
this.node.style.top = '0';
this.node.style.bottom = '';
this.node.style.right = '';
// Remove the positional classes from tip
this.node
.querySelector(`.${CLASS_POPOVER_TIP}`)
.className = CLASS_POPOVER_TIP;
}
/**
* Shows the popover at the given position
* @param {Position} position
* @public
*/
show(position) {
this.attachNode();
this.setInitialState();
// Set the title and descriptions
this.titleNode.innerHTML = this.options.title;
this.descriptionNode.innerHTML = this.options.description || '';
this.renderFooter();
// Position the popover around the given position
switch (this.options.position) {
case 'left':
case 'left-top':
this.positionOnLeft(position);
break;
case 'left-center':
this.positionOnLeftCenter(position);
break;
case 'left-bottom':
this.positionOnLeftBottom(position);
break;
case 'right':
case 'right-top':
this.positionOnRight(position);
break;
case 'right-center':
this.positionOnRightCenter(position);
break;
case 'right-bottom':
this.positionOnRightBottom(position);
break;
case 'top':
case 'top-left':
this.positionOnTop(position);
break;
case 'top-center':
this.positionOnTopCenter(position);
break;
case 'top-right':
this.positionOnTopRight(position);
break;
case 'bottom':
case 'bottom-left':
this.positionOnBottom(position);
break;
case 'bottom-center':
this.positionOnBottomCenter(position);
break;
case 'bottom-right':
this.positionOnBottomRight(position);
break;
case 'mid-center':
this.positionOnMidCenter(position);
break;
case 'auto':
default:
this.autoPosition(position);
break;
}
// Bring the popover in view port once it is displayed
this.bringInView();
}
/**
* Enables, disables buttons, sets the text and
* decides if to show them or not
* @private
*/
renderFooter() {
this.nextBtnNode.innerHTML = this.options.nextBtnText;
this.prevBtnNode.innerHTML = this.options.prevBtnText;
this.closeBtnNode.innerHTML = this.options.closeBtnText;
// If there was only one item, hide the buttons
if (!this.options.showButtons || !this.options.totalCount || this.options.totalCount === 1) {
this.footerNode.style.display = 'none';
return;
}
this.footerNode.style.display = 'block';
if (this.options.isFirst) {
this.prevBtnNode.classList.add(CLASS_BTN_DISABLED);
this.nextBtnNode.innerHTML = this.options.startBtnText;
} else {
this.prevBtnNode.classList.remove(CLASS_BTN_DISABLED);
}
if (this.options.isLast) {
this.nextBtnNode.innerHTML = this.options.doneBtnText;
} else {
this.nextBtnNode.innerHTML = this.options.nextBtnText;
}
}
/**
* Shows the popover on the left of the given position
* @param {Position} elementPosition
* @private
*/
positionOnLeft(elementPosition) {
const popoverWidth = this.getSize().width;
const popoverMargin = this.options.padding + 10; // adding 10 to give it a little distance from the element
this.node.style.left = `${elementPosition.left - popoverWidth - popoverMargin}px`;
this.node.style.top = `${(elementPosition.top + this.options.offset) - this.options.padding}px`;
this.node.style.right = '';
this.node.style.bottom = '';
this.tipNode.classList.add('right');
}
/**
* Shows the popover on the left of the given position
* @param {Position} elementPosition
* @private
*/
positionOnLeftBottom(elementPosition) {
const popoverDimensions = this.getSize();
const popoverWidth = popoverDimensions.width;
const popoverMargin = this.options.padding + 10; // adding 10 to give it a little distance from the element
this.node.style.left = `${elementPosition.left - popoverWidth - popoverMargin}px`;
this.node.style.top = `${(elementPosition.bottom + this.options.padding + this.options.offset) - popoverDimensions.height}px`;
this.node.style.bottom = '';
this.node.style.right = '';
this.tipNode.classList.add('right', 'position-bottom');
}
/**
* Shows the popover on the left center of the given position
* @param {Position} elementPosition
* @private
*/
positionOnLeftCenter(elementPosition) {
const popoverDimensions = this.getSize();
const popoverWidth = popoverDimensions.width;
const popoverHeight = popoverDimensions.height;
const popoverCenter = popoverHeight / 2;
const popoverMargin = this.options.padding + 10; // adding 10 to give it a little distance from the element
const elementCenter = (elementPosition.bottom - elementPosition.top) / 2;
const topCenterPosition = (elementPosition.top - popoverCenter) + elementCenter + this.options.offset;
this.node.style.left = `${elementPosition.left - popoverWidth - popoverMargin}px`;
this.node.style.top = `${topCenterPosition}px`;
this.node.style.right = '';
this.node.style.bottom = '';
this.tipNode.classList.add('right', 'position-center');
}
/**
* Shows the popover on the right of the given position
* @param {Position} elementPosition
* @private
*/
positionOnRight(elementPosition) {
const popoverMargin = this.options.padding + 10; // adding 10 to give it a little distance from the element
this.node.style.left = `${elementPosition.right + popoverMargin}px`;
this.node.style.top = `${(elementPosition.top + this.options.offset) - this.options.padding}px`;
this.node.style.right = '';
this.node.style.bottom = '';
this.tipNode.classList.add('left');
}
/**
* Shows the popover on the right of the given position
* @param {Position} elementPosition
* @private
*/
positionOnRightCenter(elementPosition) {
const popoverDimensions = this.getSize();
const popoverMargin = this.options.padding + 10; // adding 10 to give it a little distance from the element
const popoverHeight = popoverDimensions.height;
const popoverCenter = popoverHeight / 2;
const elementCenter = (elementPosition.bottom - elementPosition.top) / 2;
const topCenterPosition = (elementPosition.top - popoverCenter) + elementCenter + this.options.offset;
this.node.style.left = `${elementPosition.right + popoverMargin}px`;
this.node.style.top = `${topCenterPosition}px`;
this.node.style.right = '';
this.node.style.bottom = '';
this.tipNode.classList.add('left', 'position-center');
}
/**
* Shows the popover on the right of the given position
* @param {Position} elementPosition
* @private
*/
positionOnRightBottom(elementPosition) {
const popoverMargin = this.options.padding + 10; // adding 10 to give it a little distance from the element
const popoverDimensions = this.getSize();
this.node.style.left = `${elementPosition.right + popoverMargin}px`;
this.node.style.top = `${(elementPosition.bottom + this.options.padding + this.options.offset) - popoverDimensions.height}px`;
this.node.style.bottom = '';
this.node.style.right = '';
this.tipNode.classList.add('left', 'position-bottom');
}
/**
* Shows the popover on the top of the given position
* @param {Position} elementPosition
* @private
*/
positionOnTop(elementPosition) {
const popoverHeight = this.getSize().height;
const popoverMargin = this.options.padding + 10; // adding 10 to give it a little distance from the element
this.node.style.top = `${elementPosition.top - popoverHeight - popoverMargin}px`;
this.node.style.left = `${(elementPosition.left - this.options.padding) + this.options.offset}px`;
this.node.style.right = '';
this.node.style.bottom = '';
this.tipNode.classList.add('bottom');
}
/**
* Shows the popover on the top center of the given position
* @param {Position} elementPosition
* @private
*/
positionOnTopCenter(elementPosition) {
const dimensions = this.getSize();
const popoverHeight = dimensions.height;
const popoverWidth = dimensions.width / 2;
const popoverMargin = this.options.padding + 10; // adding 10 to give it a little distance from the element
const nodeCenter = this.options.offset + elementPosition.left + ((elementPosition.right - elementPosition.left) / 2);
this.node.style.top = `${elementPosition.top - popoverHeight - popoverMargin}px`;
this.node.style.left = `${nodeCenter - popoverWidth - this.options.padding}px`;
this.node.style.right = '';
this.node.style.bottom = '';
// Add the tip at the top center
this.tipNode.classList.add('bottom', 'position-center');
}
/**
* Shows the popover on the top right of the given position
* @param {Position} elementPosition
* @private
*/
positionOnTopRight(elementPosition) {
const dimensions = this.getSize();
const popoverHeight = dimensions.height;
const popoverMargin = this.options.padding + 10; // adding 10 to give it a little distance from the element
this.node.style.top = `${elementPosition.top - popoverHeight - popoverMargin}px`;
this.node.style.left = `${(elementPosition.right + this.options.padding + this.options.offset) - dimensions.width}px`;
this.node.style.right = '';
this.node.style.bottom = '';
// Add the tip at the top center
this.tipNode.classList.add('bottom', 'position-right');
}
/**
* Shows the popover on the bottom of the given position
* @param {Position} elementPosition
* @private
*/
positionOnBottom(elementPosition) {
const popoverMargin = this.options.padding + 10; // adding 10 to give it a little distance from the element
this.node.style.top = `${elementPosition.bottom + popoverMargin}px`;
this.node.style.left = `${(elementPosition.left - this.options.padding) + this.options.offset}px`;
this.node.style.right = '';
this.node.style.bottom = '';
this.tipNode.classList.add('top');
}
/**
* Shows the popover on the bottom-center of the given position
* @param {Position} elementPosition
* @private
*/
positionOnBottomCenter(elementPosition) {
const popoverWidth = this.getSize().width / 2;
const popoverMargin = this.options.padding + 10; // adding 10 to give it a little distance from the element
const nodeCenter = this.options.offset + elementPosition.left + ((elementPosition.right - elementPosition.left) / 2);
this.node.style.top = `${elementPosition.bottom + popoverMargin}px`;
this.node.style.left = `${nodeCenter - popoverWidth - this.options.padding}px`;
this.node.style.right = '';
this.node.style.bottom = '';
// Add the tip at the top center
this.tipNode.classList.add('top', 'position-center');
}
/**
* Shows the popover on the bottom-right of the given position
* @param {Position} elementPosition
* @private
*/
positionOnBottomRight(elementPosition) {
const dimensions = this.getSize();
const popoverMargin = this.options.padding + 10; // adding 10 to give it a little distance from the element
this.node.style.top = `${elementPosition.bottom + popoverMargin}px`;
this.node.style.left = `${(elementPosition.right + this.options.padding + this.options.offset) - dimensions.width}px`;
this.node.style.right = '';
this.node.style.bottom = '';
// Add the tip at the top center
this.tipNode.classList.add('top', 'position-right');
}
/**
* Shows the popover on the mid-center of the given position
* @param {Position} elementPosition
* @private
*/
positionOnMidCenter(elementPosition) {
const popoverDimensions = this.getSize();
const popoverHeight = popoverDimensions.height;
const popoverWidth = popoverDimensions.width / 2;
const popoverCenter = popoverHeight / 2;
const elementCenter = (elementPosition.bottom - elementPosition.top) / 2;
const topCenterPosition = (elementPosition.top - popoverCenter) + elementCenter + this.options.offset;
const nodeCenter = this.options.offset + elementPosition.left + ((elementPosition.right - elementPosition.left) / 2);
this.node.style.top = `${topCenterPosition}px`;
this.node.style.left = `${nodeCenter - popoverWidth - this.options.padding}px`;
this.node.style.right = '';
this.node.style.bottom = '';
// Add the tip at the top center
this.tipNode.classList.add('mid-center');
}
/**
* Automatically positions the popover around the given position
* such that the element and popover remain in view
* @todo add the left and right positioning decisions
* @param {Position} elementPosition
* @private
*/
autoPosition(elementPosition) {
const pageSize = this.getFullPageSize();
const popoverSize = this.getSize();
const pageHeight = pageSize.height;
const popoverHeight = popoverSize.height;
const popoverMargin = this.options.padding + 10; // adding 10 to give it a little distance from the element
const pageHeightAfterPopOver = elementPosition.bottom + popoverHeight + popoverMargin;
// If adding popover would go out of the window height, then show it to the top
if (pageHeightAfterPopOver >= pageHeight) {
this.positionOnTop(elementPosition);
} else {
this.positionOnBottom(elementPosition);
}
}
}
|