| Conditions | 96 |
| Total Lines | 580 |
| Code Lines | 370 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like viewer.js ➔ Viewer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | ViewerJS_version = '0.7.0'; |
||
| 59 | function Viewer( viewerPlugin, parameters ) { |
||
| 60 | "use strict"; |
||
| 61 | |||
| 62 | var self = this, |
||
| 63 | kScrollbarPadding = 40, |
||
| 64 | kMinScale = 0.25, |
||
| 65 | kMaxScale = 4.0, |
||
| 66 | kDefaultScaleDelta = 1.1, |
||
| 67 | kDefaultScale = 'auto', |
||
| 68 | presentationMode = false, |
||
| 69 | isFullScreen = false, |
||
| 70 | initialized = false, |
||
| 71 | url, |
||
| 72 | viewerElement = document.getElementById('viewer'), |
||
| 73 | canvasContainer = document.getElementById('canvasContainer'), |
||
| 74 | overlayNavigator = document.getElementById('overlayNavigator'), |
||
| 75 | titlebar = document.getElementById('titlebar'), |
||
| 76 | toolbar = document.getElementById('toolbarContainer'), |
||
| 77 | pageSwitcher = document.getElementById('toolbarLeft'), |
||
| 78 | zoomWidget = document.getElementById('toolbarMiddleContainer'), |
||
| 79 | scaleSelector = document.getElementById('scaleSelect'), |
||
| 80 | dialogOverlay = document.getElementById('dialogOverlay'), |
||
| 81 | toolbarRight = document.getElementById('toolbarRight'), |
||
| 82 | pages = [], |
||
| 83 | currentPage, |
||
| 84 | scaleChangeTimer, |
||
| 85 | touchTimer, |
||
| 86 | toolbarTouchTimer, |
||
| 87 | UI_FADE_DURATION = 5000; |
||
| 88 | |||
| 89 | function isBlankedOut() { |
||
| 90 | return (blanked.style.display === 'block'); |
||
| 91 | } |
||
| 92 | |||
| 93 | function selectScaleOption( value ) { |
||
| 94 | // Retrieve the options from the zoom level <select> element |
||
| 95 | var options = scaleSelector.options, |
||
| 96 | option, |
||
| 97 | predefinedValueFound = false, |
||
| 98 | i; |
||
| 99 | |||
| 100 | for ( i = 0; i < options.length; i += 1 ) { |
||
| 101 | option = options[i]; |
||
| 102 | if ( option.value !== value ) { |
||
| 103 | option.selected = false; |
||
| 104 | continue; |
||
| 105 | } |
||
| 106 | option.selected = true; |
||
| 107 | predefinedValueFound = true; |
||
| 108 | } |
||
| 109 | return predefinedValueFound; |
||
| 110 | } |
||
| 111 | |||
| 112 | function getPages() { |
||
| 113 | return viewerPlugin.getPages(); |
||
| 114 | } |
||
| 115 | |||
| 116 | function setScale( val, resetAutoSettings ) { |
||
| 117 | if ( val === self.getZoomLevel() ) { |
||
| 118 | return; |
||
| 119 | } |
||
| 120 | |||
| 121 | self.setZoomLevel(val); |
||
| 122 | |||
| 123 | var event = document.createEvent('UIEvents'); |
||
| 124 | event.initUIEvent('scalechange', false, false, window, 0); |
||
| 125 | event.scale = val; |
||
| 126 | event.resetAutoSettings = resetAutoSettings; |
||
| 127 | window.dispatchEvent(event); |
||
| 128 | } |
||
| 129 | |||
| 130 | function onScroll() { |
||
| 131 | var pageNumber; |
||
| 132 | |||
| 133 | if ( viewerPlugin.onScroll ) { |
||
| 134 | viewerPlugin.onScroll(); |
||
| 135 | } |
||
| 136 | if ( viewerPlugin.getPageInView ) { |
||
| 137 | pageNumber = viewerPlugin.getPageInView(); |
||
| 138 | if ( pageNumber ) { |
||
| 139 | currentPage = pageNumber; |
||
| 140 | document.getElementById('pageNumber').value = pageNumber; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | function delayedRefresh( milliseconds ) { |
||
| 146 | window.clearTimeout(scaleChangeTimer); |
||
| 147 | scaleChangeTimer = window.setTimeout(function () { |
||
| 148 | onScroll(); |
||
| 149 | }, milliseconds); |
||
| 150 | } |
||
| 151 | |||
| 152 | function parseScale( value, resetAutoSettings ) { |
||
| 153 | var scale, |
||
| 154 | maxWidth, |
||
| 155 | maxHeight; |
||
| 156 | |||
| 157 | if ( value === 'custom' ) { |
||
| 158 | scale = parseFloat(document.getElementById('customScaleOption').textContent) / 100; |
||
| 159 | } else { |
||
| 160 | scale = parseFloat(value); |
||
| 161 | } |
||
| 162 | |||
| 163 | if ( scale ) { |
||
| 164 | setScale(scale, true); |
||
| 165 | delayedRefresh(300); |
||
| 166 | return; |
||
| 167 | } |
||
| 168 | |||
| 169 | maxWidth = canvasContainer.clientWidth - kScrollbarPadding; |
||
| 170 | maxHeight = canvasContainer.clientHeight - kScrollbarPadding; |
||
| 171 | |||
| 172 | switch ( value ) { |
||
| 173 | case 'page-actual': |
||
| 174 | setScale(1, resetAutoSettings); |
||
| 175 | break; |
||
| 176 | case 'page-width': |
||
| 177 | viewerPlugin.fitToWidth(maxWidth); |
||
| 178 | break; |
||
| 179 | case 'page-height': |
||
| 180 | viewerPlugin.fitToHeight(maxHeight); |
||
| 181 | break; |
||
| 182 | case 'page-fit': |
||
| 183 | viewerPlugin.fitToPage(maxWidth, maxHeight); |
||
| 184 | break; |
||
| 185 | case 'auto': |
||
| 186 | if ( viewerPlugin.isSlideshow() ) { |
||
| 187 | viewerPlugin.fitToPage(maxWidth + kScrollbarPadding, maxHeight + kScrollbarPadding); |
||
| 188 | } else { |
||
| 189 | viewerPlugin.fitSmart(maxWidth); |
||
| 190 | } |
||
| 191 | break; |
||
| 192 | } |
||
| 193 | |||
| 194 | selectScaleOption(value); |
||
| 195 | delayedRefresh(300); |
||
| 196 | } |
||
| 197 | |||
| 198 | function readZoomParameter( zoom ) { |
||
| 199 | var validZoomStrings = ["auto", "page-actual", "page-width"], |
||
| 200 | number; |
||
| 201 | |||
| 202 | if ( validZoomStrings.indexOf(zoom) !== -1 ) { |
||
| 203 | return zoom; |
||
| 204 | } |
||
| 205 | number = parseFloat(zoom); |
||
| 206 | if ( number && kMinScale <= number && number <= kMaxScale ) { |
||
| 207 | return zoom; |
||
| 208 | } |
||
| 209 | return kDefaultScale; |
||
| 210 | } |
||
| 211 | |||
| 212 | function readStartPageParameter( startPage ) { |
||
| 213 | var result = parseInt(startPage, 10); |
||
| 214 | return isNaN(result) ? 1 : result; |
||
| 215 | } |
||
| 216 | |||
| 217 | this.initialize = function () { |
||
| 218 | var initialScale; |
||
| 219 | |||
| 220 | initialScale = readZoomParameter(parameters.zoom); |
||
| 221 | |||
| 222 | url = parameters.documentUrl; |
||
| 223 | document.title = parameters.title; |
||
| 224 | var documentName = document.getElementById('documentName'); |
||
| 225 | documentName.innerHTML = ""; |
||
| 226 | documentName.appendChild(documentName.ownerDocument.createTextNode(parameters.title)); |
||
| 227 | |||
| 228 | viewerPlugin.onLoad = function () { |
||
| 229 | |||
| 230 | if ( viewerPlugin.isSlideshow() ) { |
||
| 231 | // Slideshow pages should be centered |
||
| 232 | canvasContainer.classList.add("slideshow"); |
||
| 233 | // Show page nav controls only for presentations |
||
| 234 | pageSwitcher.style.visibility = 'visible'; |
||
| 235 | } else { |
||
| 236 | // For text documents, show the zoom widget. |
||
| 237 | zoomWidget.style.visibility = 'visible'; |
||
| 238 | // Only show the page switcher widget if the plugin supports page numbers |
||
| 239 | if ( viewerPlugin.getPageInView ) { |
||
| 240 | pageSwitcher.style.visibility = 'visible'; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | initialized = true; |
||
| 245 | pages = getPages(); |
||
| 246 | document.getElementById('numPages').innerHTML = 'of ' + pages.length; |
||
| 247 | |||
| 248 | self.showPage(readStartPageParameter(parameters.startpage)); |
||
| 249 | |||
| 250 | // Set default scale |
||
| 251 | parseScale(initialScale); |
||
| 252 | |||
| 253 | canvasContainer.onscroll = onScroll; |
||
| 254 | delayedRefresh(); |
||
| 255 | // Doesn't work in older browsers: document.getElementById('loading-document').remove(); |
||
| 256 | var loading = document.getElementById('loading-document'); |
||
| 257 | loading.parentNode.removeChild(loading); |
||
| 258 | }; |
||
| 259 | |||
| 260 | viewerPlugin.initialize(canvasContainer, url); |
||
| 261 | }; |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Shows the 'n'th page. If n is larger than the page count, |
||
| 265 | * shows the last page. If n is less than 1, shows the first page. |
||
| 266 | * @return {undefined} |
||
| 267 | */ |
||
| 268 | this.showPage = function ( n ) { |
||
| 269 | if ( n <= 0 ) { |
||
| 270 | n = 1; |
||
| 271 | } else if ( n > pages.length ) { |
||
| 272 | n = pages.length; |
||
| 273 | } |
||
| 274 | |||
| 275 | viewerPlugin.showPage(n); |
||
| 276 | |||
| 277 | currentPage = n; |
||
| 278 | document.getElementById('pageNumber').value = currentPage; |
||
| 279 | }; |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Shows the next page. If there is no subsequent page, does nothing. |
||
| 283 | * @return {undefined} |
||
| 284 | */ |
||
| 285 | this.showNextPage = function () { |
||
| 286 | self.showPage(currentPage + 1); |
||
| 287 | }; |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Shows the previous page. If there is no previous page, does nothing. |
||
| 291 | * @return {undefined} |
||
| 292 | */ |
||
| 293 | this.showPreviousPage = function () { |
||
| 294 | self.showPage(currentPage - 1); |
||
| 295 | }; |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Attempts to 'download' the file. |
||
| 299 | * @return {undefined} |
||
| 300 | */ |
||
| 301 | this.download = function () { |
||
| 302 | var documentUrl = url.split('#')[0]; |
||
| 303 | if ( documentUrl.indexOf('?') !== -1 ) { |
||
| 304 | documentUrl += '&contentDispositionType=attachment'; |
||
| 305 | } else { |
||
| 306 | documentUrl += '?contentDispositionType=attachment'; |
||
| 307 | } |
||
| 308 | window.open(documentUrl, '_parent'); |
||
| 309 | }; |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Prints the document canvas. |
||
| 313 | * @return {undefined} |
||
| 314 | */ |
||
| 315 | this.printDocument = function () { |
||
| 316 | window.print(); |
||
| 317 | }; |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Toggles the fullscreen state of the viewer |
||
| 321 | * @return {undefined} |
||
| 322 | */ |
||
| 323 | this.toggleFullScreen = function () { |
||
| 324 | var elem = viewerElement; |
||
| 325 | if ( !isFullScreen ) { |
||
| 326 | if ( elem.requestFullscreen ) { |
||
| 327 | elem.requestFullscreen(); |
||
| 328 | } else if ( elem.mozRequestFullScreen ) { |
||
| 329 | elem.mozRequestFullScreen(); |
||
| 330 | } else if ( elem.webkitRequestFullscreen ) { |
||
| 331 | elem.webkitRequestFullscreen(); |
||
| 332 | } else if ( elem.webkitRequestFullScreen ) { |
||
| 333 | elem.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT); |
||
| 334 | } else if ( elem.msRequestFullscreen ) { |
||
| 335 | elem.msRequestFullscreen(); |
||
| 336 | } |
||
| 337 | } else { |
||
| 338 | if ( document.exitFullscreen ) { |
||
| 339 | document.exitFullscreen(); |
||
| 340 | } else if ( document.cancelFullScreen ) { |
||
| 341 | document.cancelFullScreen(); |
||
| 342 | } else if ( document.mozCancelFullScreen ) { |
||
| 343 | document.mozCancelFullScreen(); |
||
| 344 | } else if ( document.webkitExitFullscreen ) { |
||
| 345 | document.webkitExitFullscreen(); |
||
| 346 | } else if ( document.webkitCancelFullScreen ) { |
||
| 347 | document.webkitCancelFullScreen(); |
||
| 348 | } else if ( document.msExitFullscreen ) { |
||
| 349 | document.msExitFullscreen(); |
||
| 350 | } |
||
| 351 | } |
||
| 352 | }; |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Toggles the presentation mode of the viewer. |
||
| 356 | * Presentation mode involves fullscreen + hidden UI controls |
||
| 357 | */ |
||
| 358 | this.togglePresentationMode = function () { |
||
| 359 | var overlayCloseButton = document.getElementById('overlayCloseButton'); |
||
| 360 | |||
| 361 | if ( !presentationMode ) { |
||
| 362 | titlebar.style.display = toolbar.style.display = 'none'; |
||
| 363 | overlayCloseButton.style.display = 'block'; |
||
| 364 | canvasContainer.classList.add('presentationMode'); |
||
| 365 | canvasContainer.onmousedown = function ( event ) { |
||
| 366 | event.preventDefault(); |
||
| 367 | }; |
||
| 368 | canvasContainer.oncontextmenu = function ( event ) { |
||
| 369 | event.preventDefault(); |
||
| 370 | }; |
||
| 371 | canvasContainer.onmouseup = function ( event ) { |
||
| 372 | event.preventDefault(); |
||
| 373 | if ( event.which === 1 ) { |
||
| 374 | self.showNextPage(); |
||
| 375 | } else { |
||
| 376 | self.showPreviousPage(); |
||
| 377 | } |
||
| 378 | }; |
||
| 379 | parseScale('page-fit'); |
||
| 380 | } else { |
||
| 381 | if ( isBlankedOut() ) { |
||
| 382 | leaveBlankOut(); |
||
| 383 | } |
||
| 384 | titlebar.style.display = toolbar.style.display = 'block'; |
||
| 385 | overlayCloseButton.style.display = 'none'; |
||
| 386 | canvasContainer.classList.remove('presentationMode'); |
||
| 387 | canvasContainer.onmouseup = function () { |
||
| 388 | }; |
||
| 389 | canvasContainer.oncontextmenu = function () { |
||
| 390 | }; |
||
| 391 | canvasContainer.onmousedown = function () { |
||
| 392 | }; |
||
| 393 | parseScale('auto'); |
||
| 394 | } |
||
| 395 | |||
| 396 | presentationMode = !presentationMode; |
||
| 397 | }; |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Gets the zoom level of the document |
||
| 401 | * @return {!number} |
||
| 402 | */ |
||
| 403 | this.getZoomLevel = function () { |
||
| 404 | return viewerPlugin.getZoomLevel(); |
||
| 405 | }; |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Set the zoom level of the document |
||
| 409 | * @param {!number} value |
||
| 410 | * @return {undefined} |
||
| 411 | */ |
||
| 412 | this.setZoomLevel = function ( value ) { |
||
| 413 | viewerPlugin.setZoomLevel(value); |
||
| 414 | }; |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Zoom out by 10 % |
||
| 418 | * @return {undefined} |
||
| 419 | */ |
||
| 420 | this.zoomOut = function () { |
||
| 421 | // 10 % decrement |
||
| 422 | var newScale = (self.getZoomLevel() / kDefaultScaleDelta).toFixed(2); |
||
| 423 | newScale = Math.max(kMinScale, newScale); |
||
| 424 | parseScale(newScale, true); |
||
| 425 | }; |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Zoom in by 10% |
||
| 429 | * @return {undefined} |
||
| 430 | */ |
||
| 431 | this.zoomIn = function () { |
||
| 432 | // 10 % increment |
||
| 433 | var newScale = (self.getZoomLevel() * kDefaultScaleDelta).toFixed(2); |
||
| 434 | newScale = Math.min(kMaxScale, newScale); |
||
| 435 | parseScale(newScale, true); |
||
| 436 | }; |
||
| 437 | |||
| 438 | function cancelPresentationMode() { |
||
| 439 | if ( presentationMode && !isFullScreen ) { |
||
| 440 | self.togglePresentationMode(); |
||
| 441 | } |
||
| 442 | } |
||
| 443 | |||
| 444 | function handleFullScreenChange() { |
||
| 445 | isFullScreen = !isFullScreen; |
||
| 446 | cancelPresentationMode(); |
||
| 447 | } |
||
| 448 | |||
| 449 | function showOverlayNavigator() { |
||
| 450 | if ( presentationMode || viewerPlugin.isSlideshow() ) { |
||
| 451 | overlayNavigator.className = 'viewer-touched'; |
||
| 452 | window.clearTimeout(touchTimer); |
||
| 453 | touchTimer = window.setTimeout(function () { |
||
| 454 | overlayNavigator.className = ''; |
||
| 455 | }, UI_FADE_DURATION); |
||
| 456 | } |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | */ |
||
| 461 | function showToolbars() { |
||
| 462 | titlebar.classList.add('viewer-touched'); |
||
| 463 | toolbar.classList.add('viewer-touched'); |
||
| 464 | window.clearTimeout(toolbarTouchTimer); |
||
| 465 | toolbarTouchTimer = window.setTimeout(function () { |
||
| 466 | hideToolbars(); |
||
| 467 | }, UI_FADE_DURATION); |
||
| 468 | } |
||
| 469 | |||
| 470 | function hideToolbars() { |
||
| 471 | titlebar.classList.remove('viewer-touched'); |
||
| 472 | toolbar.classList.remove('viewer-touched'); |
||
| 473 | } |
||
| 474 | |||
| 475 | function toggleToolbars() { |
||
| 476 | if ( titlebar.classList.contains('viewer-touched') ) { |
||
| 477 | hideToolbars(); |
||
| 478 | } else { |
||
| 479 | showToolbars(); |
||
| 480 | } |
||
| 481 | } |
||
| 482 | |||
| 483 | function blankOut( value ) { |
||
| 484 | blanked.style.display = 'block'; |
||
| 485 | blanked.style.backgroundColor = value; |
||
| 486 | hideToolbars(); |
||
| 487 | } |
||
| 488 | |||
| 489 | function leaveBlankOut() { |
||
| 490 | blanked.style.display = 'none'; |
||
| 491 | toggleToolbars(); |
||
| 492 | } |
||
| 493 | |||
| 494 | function setButtonClickHandler( buttonId, handler ) { |
||
| 495 | var button = document.getElementById(buttonId); |
||
| 496 | |||
| 497 | button.addEventListener('click', function () { |
||
| 498 | handler(); |
||
| 499 | button.blur(); |
||
| 500 | }); |
||
| 501 | } |
||
| 502 | |||
| 503 | function init() { |
||
| 504 | |||
| 505 | if ( viewerPlugin ) { |
||
| 506 | self.initialize(); |
||
| 507 | |||
| 508 | if ( !(document.exitFullscreen || document.cancelFullScreen || document.mozCancelFullScreen || document.webkitExitFullscreen || document.webkitCancelFullScreen || document.msExitFullscreen) ) { |
||
| 509 | document.getElementById('fullscreen').style.visibility = 'hidden'; |
||
| 510 | document.getElementById('presentation').style.visibility = 'hidden'; |
||
| 511 | } |
||
| 512 | |||
| 513 | setButtonClickHandler('overlayCloseButton', self.toggleFullScreen); |
||
| 514 | setButtonClickHandler('fullscreen', self.toggleFullScreen); |
||
| 515 | setButtonClickHandler('print', self.printDocument); |
||
| 516 | setButtonClickHandler('presentation', function () { |
||
| 517 | if ( !isFullScreen ) { |
||
| 518 | self.toggleFullScreen(); |
||
| 519 | } |
||
| 520 | self.togglePresentationMode(); |
||
| 521 | }); |
||
| 522 | |||
| 523 | document.addEventListener('fullscreenchange', handleFullScreenChange); |
||
| 524 | document.addEventListener('webkitfullscreenchange', handleFullScreenChange); |
||
| 525 | document.addEventListener('mozfullscreenchange', handleFullScreenChange); |
||
| 526 | document.addEventListener('MSFullscreenChange', handleFullScreenChange); |
||
| 527 | |||
| 528 | setButtonClickHandler('download', self.download); |
||
| 529 | |||
| 530 | setButtonClickHandler('zoomOut', self.zoomOut); |
||
| 531 | setButtonClickHandler('zoomIn', self.zoomIn); |
||
| 532 | |||
| 533 | setButtonClickHandler('previous', self.showPreviousPage); |
||
| 534 | setButtonClickHandler('next', self.showNextPage); |
||
| 535 | |||
| 536 | setButtonClickHandler('previousPage', self.showPreviousPage); |
||
| 537 | setButtonClickHandler('nextPage', self.showNextPage); |
||
| 538 | |||
| 539 | document.getElementById('pageNumber').addEventListener('change', function () { |
||
| 540 | self.showPage(this.value); |
||
| 541 | }); |
||
| 542 | |||
| 543 | document.getElementById('scaleSelect').addEventListener('change', function () { |
||
| 544 | parseScale(this.value); |
||
| 545 | }); |
||
| 546 | |||
| 547 | canvasContainer.addEventListener('click', showOverlayNavigator); |
||
| 548 | overlayNavigator.addEventListener('click', showOverlayNavigator); |
||
| 549 | canvasContainer.addEventListener('click', toggleToolbars); |
||
| 550 | titlebar.addEventListener('click', showToolbars); |
||
| 551 | toolbar.addEventListener('click', showToolbars); |
||
| 552 | |||
| 553 | window.addEventListener('scalechange', function ( evt ) { |
||
| 554 | var customScaleOption = document.getElementById('customScaleOption'), |
||
| 555 | predefinedValueFound = selectScaleOption(String(evt.scale)); |
||
| 556 | |||
| 557 | customScaleOption.selected = false; |
||
| 558 | |||
| 559 | if ( !predefinedValueFound ) { |
||
| 560 | customScaleOption.textContent = Math.round(evt.scale * 10000) / 100 + '%'; |
||
| 561 | customScaleOption.selected = true; |
||
| 562 | } |
||
| 563 | }, true); |
||
| 564 | |||
| 565 | window.addEventListener('resize', function () { |
||
| 566 | if ( initialized && |
||
| 567 | (document.getElementById('pageWidthOption').selected || |
||
| 568 | document.getElementById('pageAutoOption').selected) ) { |
||
| 569 | parseScale(document.getElementById('scaleSelect').value); |
||
| 570 | } |
||
| 571 | showOverlayNavigator(); |
||
| 572 | }); |
||
| 573 | |||
| 574 | window.addEventListener('keydown', function ( evt ) { |
||
| 575 | var key = evt.keyCode, |
||
| 576 | shiftKey = evt.shiftKey; |
||
| 577 | |||
| 578 | // blanked-out mode? |
||
| 579 | if ( isBlankedOut() ) { |
||
| 580 | switch ( key ) { |
||
| 581 | case 16: // Shift |
||
| 582 | case 17: // Ctrl |
||
| 583 | case 18: // Alt |
||
| 584 | case 91: // LeftMeta |
||
| 585 | case 93: // RightMeta |
||
| 586 | case 224: // MetaInMozilla |
||
| 587 | case 225: // AltGr |
||
| 588 | // ignore modifier keys alone |
||
| 589 | break; |
||
| 590 | default: |
||
| 591 | leaveBlankOut(); |
||
| 592 | break; |
||
| 593 | } |
||
| 594 | } else { |
||
| 595 | switch ( key ) { |
||
| 596 | case 8: // backspace |
||
| 597 | case 33: // pageUp |
||
| 598 | case 37: // left arrow |
||
| 599 | case 38: // up arrow |
||
| 600 | case 80: // key 'p' |
||
| 601 | self.showPreviousPage(); |
||
| 602 | break; |
||
| 603 | case 13: // enter |
||
| 604 | case 34: // pageDown |
||
| 605 | case 39: // right arrow |
||
| 606 | case 40: // down arrow |
||
| 607 | case 78: // key 'n' |
||
| 608 | self.showNextPage(); |
||
| 609 | break; |
||
| 610 | case 32: // space |
||
| 611 | shiftKey ? self.showPreviousPage() : self.showNextPage(); |
||
| 612 | break; |
||
| 613 | case 66: // key 'b' blanks screen (to black) or returns to the document |
||
| 614 | case 190: // and so does the key '.' (dot) |
||
| 615 | if ( presentationMode ) { |
||
| 616 | blankOut('#000'); |
||
| 617 | } |
||
| 618 | break; |
||
| 619 | case 87: // key 'w' blanks page (to white) or returns to the document |
||
| 620 | case 188: // and so does the key ',' (comma) |
||
| 621 | if ( presentationMode ) { |
||
| 622 | blankOut('#FFF'); |
||
| 623 | } |
||
| 624 | break; |
||
| 625 | case 36: // key 'Home' goes to first page |
||
| 626 | self.showPage(1); |
||
| 627 | break; |
||
| 628 | case 35: // key 'End' goes to last page |
||
| 629 | self.showPage(pages.length); |
||
| 630 | break; |
||
| 631 | } |
||
| 632 | } |
||
| 633 | }); |
||
| 634 | } |
||
| 635 | } |
||
| 636 | |||
| 637 | init(); |
||
| 638 | } |
||
| 639 | /** |
||
| 956 |