Skip to content

Commit 8245e27

Browse files
committed
refactor: finishing up refactoring remaining JS utils to address eslint and prettier errors
1 parent 5e38f56 commit 8245e27

File tree

3 files changed

+76
-67
lines changed

3 files changed

+76
-67
lines changed

packages/uikit-workshop/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
"access": "public"
6262
},
6363
"dependencies": {
64+
"deepmerge": "^2.1.1",
65+
"js-cookie": "^2.2.0",
6466
"clipboard": "^2.0.1",
6567
"fg-loadcss": "^2.0.1",
6668
"fg-loadjs": "^1.0.0",

packages/uikit-workshop/src/scripts/utils/postmessage.js

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,19 @@
99
*/
1010

1111
// alert the iframe parent that the pattern has loaded assuming this view was loaded in an iframe
12-
if (self != top) {
12+
if (window.self !== window.top) {
1313
// handle the options that could be sent to the parent window
1414
// - all get path
1515
// - pattern & view all get a pattern partial, styleguide gets all
1616
// - pattern shares lineage
17-
var path = window.location.toString();
18-
var parts = path.split('?');
19-
var options = {
20-
event: 'patternLab.pageLoad',
21-
path: parts[0],
22-
};
17+
const path = window.location.toString();
18+
const parts = path.split('?');
19+
const options = { event: 'patternLab.pageLoad', path: parts[0] };
2320

2421
window.patternData = document.getElementById(
2522
'pl-pattern-data-footer'
2623
).innerHTML;
27-
window.patternData = JSON.parse(patternData);
24+
window.patternData = JSON.parse(window.patternData);
2825
options.patternpartial =
2926
window.patternData.patternPartial !== undefined
3027
? window.patternData.patternPartial
@@ -33,19 +30,22 @@ if (self != top) {
3330
options.lineage = window.patternData.lineage;
3431
}
3532

36-
var targetOrigin =
37-
window.location.protocol == 'file:'
33+
const targetOrigin =
34+
window.location.protocol === 'file:'
3835
? '*'
3936
: window.location.protocol + '//' + window.location.host;
40-
parent.postMessage(options, targetOrigin);
37+
window.parent.postMessage(options, targetOrigin);
4138

4239
// find all links and add an onclick handler for replacing the iframe address so the history works
43-
var aTags = document.getElementsByTagName('a');
44-
for (var i = 0; i < aTags.length; i++) {
40+
const aTags = document.getElementsByTagName('a');
41+
for (let i = 0; i < aTags.length; i++) {
4542
aTags[i].onclick = function(e) {
46-
var href = this.getAttribute('href');
47-
var target = this.getAttribute('target');
48-
if (target !== undefined && (target == '_parent' || target == '_blank')) {
43+
const href = this.getAttribute('href');
44+
const target = this.getAttribute('target');
45+
if (
46+
target !== undefined &&
47+
(target === '_parent' || target === '_blank')
48+
) {
4949
// just do normal stuff
5050
} else if (href && href !== '#') {
5151
e.preventDefault();
@@ -54,6 +54,8 @@ if (self != top) {
5454
e.preventDefault();
5555
return false;
5656
}
57+
58+
return true;
5759
};
5860
}
5961
}
@@ -62,22 +64,24 @@ if (self != top) {
6264
function receiveIframeMessage(event) {
6365
// does the origin sending the message match the current host? if not dev/null the request
6466
if (
65-
window.location.protocol != 'file:' &&
67+
window.location.protocol !== 'file:' &&
6668
event.origin !== window.location.protocol + '//' + window.location.host
6769
) {
6870
return;
6971
}
7072

71-
var path;
72-
var data = {};
73+
let path;
74+
let data = {};
7375
try {
7476
data = typeof event.data !== 'string' ? event.data : JSON.parse(event.data);
75-
} catch (e) {}
77+
} catch (e) {
78+
// @todo: how do we want to handle exceptions like these?
79+
}
7680

77-
if (data.event !== undefined && data.event == 'patternLab.updatePath') {
81+
if (data.event !== undefined && data.event === 'patternLab.updatePath') {
7882
if (window.patternData.patternPartial !== undefined) {
7983
// handle patterns and the view all page
80-
var re = /(patterns|snapshots)\/(.*)$/;
84+
const re = /(patterns|snapshots)\/(.*)$/;
8185
path =
8286
window.location.protocol +
8387
'//' +
@@ -102,7 +106,7 @@ function receiveIframeMessage(event) {
102106
Date.now();
103107
window.location.replace(path);
104108
}
105-
} else if (data.event !== undefined && data.event == 'patternLab.reload') {
109+
} else if (data.event !== undefined && data.event === 'patternLab.reload') {
106110
// reload the location if there was a message to do so
107111
window.location.reload();
108112
}

packages/uikit-workshop/src/scripts/utils/url-handler.js

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const urlHandler = {
1414
// set-up some default vars
1515
skipBack: false,
1616
targetOrigin:
17-
window.location.protocol == 'file:'
17+
window.location.protocol === 'file:'
1818
? '*'
1919
: window.location.protocol + '//' + window.location.host,
2020

@@ -25,9 +25,9 @@ export const urlHandler = {
2525
*
2626
* @return {String} the real file path
2727
*/
28-
getFileName: function(name, withRenderedSuffix) {
29-
var baseDir = 'patterns';
30-
var fileName = '';
28+
getFileName(name, withRenderedSuffix) {
29+
const baseDir = 'patterns';
30+
let fileName = '';
3131

3232
if (name === undefined) {
3333
return fileName;
@@ -37,28 +37,31 @@ export const urlHandler = {
3737
withRenderedSuffix = true;
3838
}
3939

40-
if (name == 'all') {
40+
if (name === 'all') {
4141
return 'styleguide/html/styleguide.html';
42-
} else if (name == 'snapshots') {
42+
} else if (name === 'snapshots') {
4343
return 'snapshots/index.html';
4444
}
4545

46-
var paths = name.indexOf('viewall-') != -1 ? viewAllPaths : patternPaths;
47-
var nameClean = name.replace('viewall-', '');
46+
const paths =
47+
name.indexOf('viewall-') !== -1
48+
? window.viewAllPaths
49+
: window.patternPaths;
50+
const nameClean = name.replace('viewall-', '');
4851

4952
// look at this as a regular pattern
50-
var bits = this.getPatternInfo(nameClean, paths);
51-
var patternType = bits[0];
52-
var pattern = bits[1];
53+
const bits = this.getPatternInfo(nameClean, paths);
54+
const patternType = bits[0];
55+
const pattern = bits[1];
5356

5457
if (
5558
paths[patternType] !== undefined &&
5659
paths[patternType][pattern] !== undefined
5760
) {
5861
fileName = paths[patternType][pattern];
5962
} else if (paths[patternType] !== undefined) {
60-
for (var patternMatchKey in paths[patternType]) {
61-
if (patternMatchKey.indexOf(pattern) != -1) {
63+
for (const patternMatchKey in paths[patternType]) {
64+
if (patternMatchKey.indexOf(pattern) !== -1) {
6265
fileName = paths[patternType][patternMatchKey];
6366
break;
6467
}
@@ -69,7 +72,7 @@ export const urlHandler = {
6972
return fileName;
7073
}
7174

72-
var regex = /\//g;
75+
const regex = /\//g;
7376
if (
7477
name.indexOf('viewall-') !== -1 &&
7578
name.indexOf('viewall-') === 0 &&
@@ -84,10 +87,10 @@ export const urlHandler = {
8487
'/' +
8588
fileName.replace(regex, '-');
8689
if (withRenderedSuffix) {
87-
var fileSuffixRendered =
88-
config.outputFileSuffixes !== undefined &&
89-
config.outputFileSuffixes.rendered !== undefined
90-
? config.outputFileSuffixes.rendered
90+
const fileSuffixRendered =
91+
window.config.outputFileSuffixes !== undefined &&
92+
window.config.outputFileSuffixes.rendered !== undefined
93+
? window.config.outputFileSuffixes.rendered
9194
: '';
9295
fileName = fileName + fileSuffixRendered + '.html';
9396
}
@@ -103,19 +106,19 @@ export const urlHandler = {
103106
*
104107
* @return {Array} the pattern type and pattern name
105108
*/
106-
getPatternInfo: function(name, paths) {
107-
var patternBits = name.split('-');
109+
getPatternInfo(name, paths) {
110+
const patternBits = name.split('-');
108111

109-
var i = 1;
110-
var c = patternBits.length;
112+
let i = 1;
113+
const c = patternBits.length;
111114

112-
var patternType = patternBits[0];
115+
let patternType = patternBits[0];
113116
while (paths[patternType] === undefined && i < c) {
114117
patternType += '-' + patternBits[i];
115118
i++;
116119
}
117120

118-
var pattern = name.slice(patternType.length + 1, name.length);
121+
const pattern = name.slice(patternType.length + 1, name.length);
119122

120123
return [patternType, pattern];
121124
},
@@ -125,12 +128,12 @@ export const urlHandler = {
125128
*
126129
* @return {Object} a search of the window.location.search vars
127130
*/
128-
getRequestVars: function() {
131+
getRequestVars() {
129132
// the following is taken from https://developer.mozilla.org/en-US/docs/Web/API/window.location
130-
var oGetVars = new function(sSearch) {
133+
const oGetVars = new function(sSearch) {
131134
if (sSearch.length > 1) {
132135
for (
133-
var aItKey, nKeyId = 0, aCouples = sSearch.substr(1).split('&');
136+
let aItKey, nKeyId = 0, aCouples = sSearch.substr(1).split('&');
134137
nKeyId < aCouples.length;
135138
nKeyId++
136139
) {
@@ -149,21 +152,21 @@ export const urlHandler = {
149152
* @param {String} the shorthand partials syntax for a given pattern
150153
* @param {String} the path given by the loaded iframe
151154
*/
152-
pushPattern: function(pattern, givenPath) {
153-
var data = {
154-
pattern: pattern,
155+
pushPattern(pattern, givenPath) {
156+
const data = {
157+
pattern,
155158
};
156-
var fileName = urlHandler.getFileName(pattern);
157-
var path = window.location.pathname;
159+
const fileName = urlHandler.getFileName(pattern);
160+
let path = window.location.pathname;
158161
path =
159162
window.location.protocol === 'file'
160163
? path.replace('/public/index.html', 'public/')
161164
: path.replace(/\/index\.html/, '/');
162-
var expectedPath =
165+
const expectedPath =
163166
window.location.protocol + '//' + window.location.host + path + fileName;
164-
if (givenPath != expectedPath) {
167+
if (givenPath !== expectedPath) {
165168
// make sure to update the iframe because there was a click
166-
var obj = JSON.stringify({
169+
const obj = JSON.stringify({
167170
event: 'patternLab.updatePath',
168171
path: fileName,
169172
});
@@ -172,17 +175,17 @@ export const urlHandler = {
172175
.contentWindow.postMessage(obj, urlHandler.targetOrigin);
173176
} else {
174177
// add to the history
175-
var addressReplacement =
176-
window.location.protocol == 'file:'
178+
const addressReplacement =
179+
window.location.protocol === 'file:'
177180
? null
178181
: window.location.protocol +
179182
'//' +
180183
window.location.host +
181184
window.location.pathname.replace('index.html', '') +
182185
'?p=' +
183186
pattern;
184-
if (history.pushState !== undefined) {
185-
history.pushState(data, null, addressReplacement);
187+
if (window.history.pushState !== undefined) {
188+
window.history.pushState(data, null, addressReplacement);
186189
}
187190
document.getElementById('title').innerHTML = 'Pattern Lab - ' + pattern;
188191

@@ -200,9 +203,9 @@ export const urlHandler = {
200203
* based on a click forward or backward modify the url and iframe source
201204
* @param {Object} event info like state and properties set in pushState()
202205
*/
203-
popPattern: function(e) {
204-
var patternName;
205-
var state = e.state;
206+
popPattern(e) {
207+
let patternName;
208+
const state = e.state;
206209

207210
if (state === null) {
208211
this.skipBack = false;
@@ -211,13 +214,13 @@ export const urlHandler = {
211214
patternName = state.pattern;
212215
}
213216

214-
var iFramePath = '';
217+
let iFramePath = '';
215218
iFramePath = this.getFileName(patternName);
216219
if (iFramePath === '') {
217220
iFramePath = 'styleguide/html/styleguide.html';
218221
}
219222

220-
var obj = JSON.stringify({
223+
const obj = JSON.stringify({
221224
event: 'patternLab.updatePath',
222225
path: iFramePath,
223226
});

0 commit comments

Comments
 (0)