Skip to content

Commit 367893a

Browse files
committed
refactor: finish high-level refactoring of the 2nd half of styleguide.js's existing logic
1 parent 8e6d824 commit 367893a

1 file changed

Lines changed: 39 additions & 34 deletions

File tree

packages/uikit-workshop/src/scripts/components/styleguide.js

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ import { patternFinder } from './pattern-finder';
334334
//'size' is the target size of the viewport
335335
//'animate' is a boolean for switching the CSS animation on or off. 'animate' is true by default, but can be set to false for things like nudging and dragging
336336
function sizeiframe(size, animate) {
337-
var theSize;
337+
let theSize;
338338

339339
if (size > maxViewportWidth) {
340340
//If the entered size is larger than the max allowed viewport size, cap value at max vp size
@@ -356,11 +356,11 @@ import { patternFinder } from './pattern-finder';
356356
$('.pl-js-vp-iframe-container').width(theSize + viewportResizeHandleWidth); //Resize viewport wrapper to desired size + size of drag resize handler
357357
$sgIframe.width(theSize); //Resize viewport to desired size
358358

359-
var targetOrigin =
359+
const targetOrigin =
360360
window.location.protocol === 'file:'
361361
? '*'
362362
: window.location.protocol + '//' + window.location.host;
363-
var obj = JSON.stringify({
363+
const obj = JSON.stringify({
364364
event: 'patternLab.resize',
365365
resize: 'true',
366366
});
@@ -375,11 +375,11 @@ import { patternFinder } from './pattern-finder';
375375
$('.pl-js-vp-iframe-container').on(
376376
'transitionend webkitTransitionEnd',
377377
function(e) {
378-
var targetOrigin =
378+
const targetOrigin =
379379
window.location.protocol === 'file:'
380380
? '*'
381381
: window.location.protocol + '//' + window.location.host;
382-
var obj = JSON.stringify({
382+
const obj = JSON.stringify({
383383
event: 'patternLab.resize',
384384
resize: 'true',
385385
});
@@ -402,7 +402,7 @@ import { patternFinder } from './pattern-finder';
402402
//'unit' is the type of unit: either px or em. Default is px. Accepted values are 'px' and 'em'
403403
//'target' is what inputs to update. Defaults to both
404404
function updateSizeReading(size, unit, target) {
405-
var emSize, pxSize;
405+
let emSize, pxSize;
406406

407407
if (unit === 'em') {
408408
//If size value is in em units
@@ -445,19 +445,17 @@ import { patternFinder } from './pattern-finder';
445445
// 3. on "mousemove" calculate the math, save the results to a cookie, and update the viewport
446446
$('.pl-js-resize-handle').mousedown(function(event) {
447447
// capture default data
448-
var origClientX = event.clientX;
449-
var origViewportWidth = $sgIframe.width();
448+
const origClientX = event.clientX;
449+
const origViewportWidth = $sgIframe.width();
450450

451451
fullMode = false;
452452

453453
// show the cover
454454
$('.pl-js-viewport-cover').css('display', 'block');
455455

456456
// add the mouse move event and capture data. also update the viewport width
457-
$('.pl-js-viewport-cover').mousemove(function(event) {
458-
var viewportWidth;
459-
460-
viewportWidth = origViewportWidth + 2 * (event.clientX - origClientX);
457+
$('.pl-js-viewport-cover').mousemove(function(e) {
458+
const viewportWidth = origViewportWidth + 2 * (e.clientX - origClientX);
461459

462460
if (viewportWidth > minViewportWidth) {
463461
if (!DataSaver.findValue('vpWidth')) {
@@ -480,16 +478,16 @@ import { patternFinder } from './pattern-finder';
480478
});
481479

482480
// capture the viewport width that was loaded and modify it so it fits with the pull bar
483-
var origViewportWidth = $('.pl-js-iframe').width();
481+
const origViewportWidth = $('.pl-js-iframe').width();
484482
$('.pl-js-vp-iframe-container').width(origViewportWidth);
485483

486-
var testWidth = window.screen.width;
484+
let testWidth = window.screen.width;
487485
if (window.orientation !== undefined) {
488486
testWidth =
489487
window.orientation === 0 ? window.screen.width : window.screen.height;
490488
}
491489
if (
492-
$(window).width() == testWidth &&
490+
$(window).width() === testWidth &&
493491
'ontouchstart' in document.documentElement &&
494492
$(window).width() <= 1024
495493
) {
@@ -500,11 +498,11 @@ import { patternFinder } from './pattern-finder';
500498
updateSizeReading($('.pl-js-iframe').width());
501499

502500
// get the request vars
503-
var oGetVars = urlHandler.getRequestVars();
501+
const oGetVars = urlHandler.getRequestVars();
504502

505503
// pre-load the viewport width
506-
var vpWidth = 0;
507-
var trackViewportWidth = true; // can toggle this feature on & off
504+
let vpWidth = 0;
505+
const trackViewportWidth = true; // can toggle this feature on & off
508506

509507
if (oGetVars.h !== undefined || oGetVars.hay !== undefined) {
510508
startHay();
@@ -523,18 +521,18 @@ import { patternFinder } from './pattern-finder';
523521
}
524522

525523
// set up the defaults for the
526-
var baseIframePath =
524+
const baseIframePath =
527525
window.location.protocol +
528526
'//' +
529527
window.location.host +
530528
window.location.pathname.replace('index.html', '');
531-
var patternName =
529+
let patternName =
532530
window.config.defaultPattern !== undefined &&
533531
typeof window.config.defaultPattern === 'string' &&
534532
window.config.defaultPattern.trim().length > 0
535533
? window.config.defaultPattern
536534
: 'all';
537-
var iFramePath =
535+
let iFramePath =
538536
baseIframePath + 'styleguide/html/styleguide.html?' + Date.now();
539537
if (oGetVars.p !== undefined || oGetVars.pattern !== undefined) {
540538
patternName = oGetVars.p !== undefined ? oGetVars.p : oGetVars.pattern;
@@ -582,7 +580,7 @@ import { patternFinder } from './pattern-finder';
582580
$('a[data-patternpartial]').on('click', function(e) {
583581
e.preventDefault();
584582
// update the iframe via the history api handler
585-
var obj = JSON.stringify({
583+
const obj = JSON.stringify({
586584
event: 'patternLab.updatePath',
587585
path: urlHandler.getFileName($(this).attr('data-patternpartial')),
588586
});
@@ -599,7 +597,7 @@ import { patternFinder } from './pattern-finder';
599597

600598
// Listen for resize changes
601599
if (window.orientation !== undefined) {
602-
var origOrientation = window.orientation;
600+
let origOrientation = window.orientation;
603601
window.addEventListener(
604602
'orientationchange',
605603
function() {
@@ -619,17 +617,21 @@ import { patternFinder } from './pattern-finder';
619617
function receiveIframeMessage(event) {
620618
// does the origin sending the message match the current host? if not dev/null the request
621619
if (
622-
window.location.protocol !== 'file:' &&
623-
event.origin !== window.location.protocol + '//' + window.location.host
620+
(window.location.protocol !== 'file:' &&
621+
event.origin !==
622+
window.location.protocol + '//' + window.location.host) ||
623+
event.data === '' // message received, but no data included; prevents JSON.parse error below
624624
) {
625625
return;
626626
}
627627

628-
var data = {};
628+
let data = {};
629629
try {
630630
data =
631631
typeof event.data !== 'string' ? event.data : JSON.parse(event.data);
632-
} catch (e) {}
632+
} catch (e) {
633+
throw Error(e);
634+
}
633635

634636
if (data.event !== undefined) {
635637
if (data.event === 'patternLab.pageLoad') {
@@ -673,14 +675,17 @@ import { patternFinder } from './pattern-finder';
673675
}
674676
} else if (data.keyPress === 'ctrl+shift+0') {
675677
sizeiframe(320, true);
676-
} else if (found == data.keyPress.match(/ctrl\+shift\+([1-9])/)) {
677-
var val = mqs[found[1] - 1];
678-
var type = val.indexOf('px') !== -1 ? 'px' : 'em';
679-
val = val.replace(type, '');
680-
var width = type === 'px' ? val * 1 : val * $bodySize;
681-
sizeiframe(width, true);
682678
}
683-
return false;
679+
680+
// @todo: chat with Brian on if this code is still used and necessary; both the `mqs` and `found` variables are both currently undefined.
681+
// else if (found === data.keyPress.match(/ctrl\+shift\+([1-9])/)) {
682+
// let val = mqs[found[1] - 1];
683+
// const type = val.indexOf('px') !== -1 ? 'px' : 'em';
684+
// val = val.replace(type, '');
685+
// const width = type === 'px' ? val * 1 : val * $bodySize;
686+
// sizeiframe(width, true);
687+
// }
688+
// return false;
684689
}
685690
}
686691
}

0 commit comments

Comments
 (0)