Skip to content

Commit 76a2ebb

Browse files
authored
Merge pull request #1051 from Patternslib/maintenance
Maintenance
2 parents d08aafb + e36c58c commit 76a2ebb

File tree

82 files changed

+369
-558
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+369
-558
lines changed

index.html

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
54
<title>Patterns demo pages</title>
6-
<link rel="stylesheet" href="/style/common.css" type="text/css" />
7-
<script
8-
src="/bundle.min.js"
9-
type="text/javascript"
10-
charset="utf-8"
11-
></script>
5+
<meta charset="utf-8">
6+
<link rel="stylesheet" href="/style/common.css" />
7+
<script src="/bundle.min.js"></script>
128
</head>
139
<body class="component-index">
1410
<h1>Demos and documentation for the individual patterns</h1>

src/core/base.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ const initBasePattern = function ($el, options, trigger) {
4646
};
4747

4848
const Base = async function ($el, options, trigger) {
49+
if (!$el) {
50+
log.warn("No element given to pattern.");
51+
return;
52+
}
4953
if (!$el.jquery) {
5054
$el = $($el);
5155
}

src/core/base.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ describe("pat-base: The Base class for patterns", function () {
5858
expect(tmp.el).toBe(node);
5959
});
6060

61+
it("Does nothing when initialized with no DOM node", function () {
62+
const Tmp = Base.extend({
63+
name: "example",
64+
init: () => {},
65+
});
66+
const tmp = new Tmp(null);
67+
console.log(tmp);
68+
expect(tmp instanceof Tmp).toBeTruthy();
69+
expect(tmp.$el).toBeFalsy();
70+
expect(tmp.el).toBeFalsy();
71+
});
72+
6173
it("will automatically register a pattern in the registry when extended", function () {
6274
jest.spyOn(registry, "register");
6375
var NewPattern = Base.extend({

src/core/dom.js

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@ import events from "./events";
44
const DATA_PREFIX = "__patternslib__data_prefix__";
55
const DATA_STYLE_DISPLAY = "__patternslib__style__display";
66

7+
/**
8+
* Return an array of DOM nodes.
9+
*
10+
* @param {Node|NodeList|jQuery} nodes - The DOM node to start the search from.
11+
*
12+
* @returns {Array} - An array of DOM nodes.
13+
*/
714
const toNodeArray = (nodes) => {
8-
// Return an array of DOM nodes
915
if (nodes.jquery || nodes instanceof NodeList) {
1016
// jQuery or document.querySelectorAll
1117
nodes = [...nodes];
@@ -15,10 +21,15 @@ const toNodeArray = (nodes) => {
1521
return nodes;
1622
};
1723

24+
/**
25+
* Like querySelectorAll but including the element where it starts from.
26+
* Returns an Array, not a NodeList
27+
*
28+
* @param {Node} el - The DOM node to start the search from.
29+
*
30+
* @returns {Array} - The DOM nodes found.
31+
*/
1832
const querySelectorAllAndMe = (el, selector) => {
19-
// Like querySelectorAll but including the element where it starts from.
20-
// Returns an Array, not a NodeList
21-
2233
if (!el) {
2334
return [];
2435
}
@@ -30,16 +41,23 @@ const querySelectorAllAndMe = (el, selector) => {
3041
return all;
3142
};
3243

44+
/**
45+
* Wrap a element with a wrapper element.
46+
*
47+
* @param {Node} el - The DOM node to wrap.
48+
*/
3349
const wrap = (el, wrapper) => {
34-
// Wrap a element with a wrapper element.
3550
// See: https://stackoverflow.com/a/13169465/1337474
36-
3751
el.parentNode.insertBefore(wrapper, el);
3852
wrapper.appendChild(el);
3953
};
4054

55+
/**
56+
* Hides the element with ``display: none`` and stores the current display value.
57+
*
58+
* @param {Node} el - The DOM node to hide.
59+
*/
4160
const hide = (el) => {
42-
// Hides the element with ``display: none``
4361
if (el.style.display === "none") {
4462
// Nothing to do.
4563
return;
@@ -51,38 +69,65 @@ const hide = (el) => {
5169
el.setAttribute("hidden", "");
5270
};
5371

72+
/**
73+
* Shows element by removing ``display: none`` and restoring the display value
74+
* to whatever it was before.
75+
*
76+
* @param {Node} el - The DOM node to show.
77+
*/
5478
const show = (el) => {
55-
// Shows element by removing ``display: none`` and restoring the display
56-
// value to whatever it was before.
5779
const val = el[DATA_STYLE_DISPLAY] || null;
5880
el.style.display = val;
5981
delete el[DATA_STYLE_DISPLAY];
6082
el.removeAttribute("hidden", "");
6183
};
6284

85+
/**
86+
* Return all direct parents of ``el`` matching ``selector``.
87+
* This matches against all parents but not the element itself.
88+
* The order of elements is from the search starting point up to higher
89+
* DOM levels.
90+
*
91+
* @param {Node} el - The DOM node to start the search from.
92+
* @param {String} selector - CSS selector to match against.
93+
* @returns {Array} - List of matching DOM nodes.
94+
*/
6395
const find_parents = (el, selector) => {
64-
// Return all direct parents of ``el`` matching ``selector``.
65-
// This matches against all parents but not the element itself.
66-
// The order of elements is from the search starting point up to higher
67-
// DOM levels.
6896
const ret = [];
69-
let parent = el?.parentNode?.closest?.(selector);
97+
let parent = el;
7098
while (parent) {
71-
ret.push(parent);
7299
parent = parent.parentNode?.closest?.(selector);
100+
if (parent) ret.push(parent);
73101
}
74102
return ret;
75103
};
76104

105+
/**
106+
* Find an element in the whole DOM tree if the selector is an ID selector,
107+
* otherwise use the given element as the starting point.
108+
*
109+
* @param {Node} el - The DOM node to start the search from.
110+
* @param {String} selector - The CSS selector to search for.
111+
*
112+
* @returns {NodeList} - The DOM nodes found.
113+
*
114+
*/
77115
const find_scoped = (el, selector) => {
78116
// If the selector starts with an object id do a global search,
79117
// otherwise do a local search.
80118
return (selector.indexOf("#") === 0 ? document : el).querySelectorAll(selector);
81119
};
82120

121+
/**
122+
* Return all HTMLElement parents of el, starting from the direct parent of el.
123+
* The document itself is excluded because it's not a real DOM node.
124+
*
125+
* @param {Node} el - The DOM node to start the search from.
126+
*
127+
* @returns {Array} - The DOM nodes found.
128+
*/
83129
const get_parents = (el) => {
84130
// Return all HTMLElement parents of el, starting from the direct parent of el.
85-
// The document itself is excluded because it's not a real DOM node.
86131
const parents = [];
87132
let parent = el?.parentNode;
88133
while (parent) {
@@ -211,7 +256,7 @@ const find_scroll_container = (el, direction, fallback = document.body) => {
211256
* @param name {String} - The name of the variable. Note - this is stored on
212257
* the DOM node prefixed with the DATA_PREFIX.
213258
* @param default_value {Any} - Optional default value.
214-
* @return {Any} - The value which is stored on the DOM node.
259+
* @returns {Any} - The value which is stored on the DOM node.
215260
*/
216261
const get_data = (el, name, default_value) => {
217262
return el[`${DATA_PREFIX}${name}`] || default_value;

src/core/dom.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,14 @@ describe("core.dom tests", () => {
177177

178178
done();
179179
});
180+
180181
it("don't break with no element.", (done) => {
181182
const res = dom.find_parents(null, ".findme");
182183
expect(res.length).toEqual(0);
183184

184185
done();
185186
});
187+
186188
it("don't break with DocumentFragment without a parent.", (done) => {
187189
const el = new DocumentFragment();
188190
el.innerHTML = `<div class="starthere"></div>`;

src/core/utils.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,6 @@ function extend(obj) {
156156
}
157157
// END: Taken from Underscore.js until here.
158158

159-
function rebaseURL(base, url) {
160-
base = new URL(base, window.location).href; // If base is relative make it absolute.
161-
if (url.indexOf("://") !== -1 || url[0] === "/" || url.indexOf("data:") === 0) {
162-
return url;
163-
}
164-
return base.slice(0, base.lastIndexOf("/") + 1) + url;
165-
}
166-
167159
function findLabel(input) {
168160
var $label;
169161
for (
@@ -683,7 +675,6 @@ var utils = {
683675
escapeRegExp: escapeRegExp,
684676
isObject: isObject,
685677
extend: extend,
686-
rebaseURL: rebaseURL,
687678
findLabel: findLabel,
688679
elementInViewport: elementInViewport,
689680
removeWildcardClass: removeWildcardClass,

src/core/utils.test.js

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,6 @@ import $ from "jquery";
33
import { jest } from "@jest/globals";
44

55
describe("basic tests", function () {
6-
describe("rebaseURL", function () {
7-
it("Keep URL with scheme", function () {
8-
expect(
9-
utils.rebaseURL("http://example.com/foo/", "http://other.com/me")
10-
).toBe("http://other.com/me");
11-
});
12-
13-
it("Keep URL with absolute path", function () {
14-
expect(utils.rebaseURL("http://example.com/foo/", "/me")).toBe("/me");
15-
});
16-
17-
it("Rebase to base with filename", function () {
18-
expect(
19-
utils.rebaseURL("http://example.com/foo/index.html", "me/page.html")
20-
).toBe("http://example.com/foo/me/page.html");
21-
});
22-
23-
it("Rebase to base with directory path", function () {
24-
expect(utils.rebaseURL("http://example.com/foo/", "me/page.html")).toBe(
25-
"http://example.com/foo/me/page.html"
26-
);
27-
});
28-
29-
it("Rebase with absolute base url", function () {
30-
expect(
31-
utils.rebaseURL("/foo/", "me/page.html").indexOf("/foo/me/page.html") > 0
32-
).toBe(true);
33-
});
34-
35-
it("Rebase with relative base url", function () {
36-
expect(
37-
utils
38-
.rebaseURL("example.com/foo/", "me/page.html")
39-
.indexOf("example.com/foo/me/page.html") > 0
40-
).toBe(true);
41-
});
42-
43-
it("Doesn't rebase data: urls", function () {
44-
expect(
45-
utils.rebaseURL("http://example.com/foo/", "data:image-base64gibberish")
46-
).toBe("data:image-base64gibberish");
47-
});
48-
});
49-
506
describe("removeDuplicateObjects", function () {
517
it("removes removes duplicates inside an array of objects", function () {
528
var objs = [];

src/pat/auto-scale/index.html

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
54
<title>Auto scale</title>
6-
<link rel="stylesheet" href="/style/common.css" type="text/css" />
7-
<script
8-
src="/bundle.min.js"
9-
type="text/javascript"
10-
charset="utf-8"
11-
></script>
5+
<meta charset="utf-8">
6+
<link rel="stylesheet" href="/style/common.css" />
7+
<script src="/bundle.min.js"></script>
128
</head>
139

1410
<body>
@@ -82,8 +78,7 @@
8278
>
8379
open in fullscreen
8480
</button>
85-
86-
<style type="text/css">
81+
<style>
8782
.pat-button {
8883
float: left;
8984
}

src/pat/auto-submit/index.html

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
54
<title>Auto submit</title>
6-
<link rel="stylesheet" href="/style/common.css" type="text/css" />
7-
<script
8-
src="/bundle.min.js"
9-
type="text/javascript"
10-
charset="utf-8"
11-
></script>
12-
<style type="text/css" media="screen">
5+
<meta charset="utf-8">
6+
<link rel="stylesheet" href="/style/common.css" />
7+
<script src="/bundle.min.js"></script>
8+
<style>
139
label {
1410
display: block;
1511
}
@@ -35,7 +31,7 @@
3531
<body class="home">
3632
<h1>pat-autosubmit demo</h1>
3733
<div id="demo">
38-
<style type="text/css">
34+
<style>
3935
.pat-message {
4036
animation: fadeIn 0.5s;
4137
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
54
<title>Feedback</title>
5+
<meta charset="utf-8">
66
</head>
77
<body>
8-
<p id="message"
8+
<p id="message"
99
class="pat-message success">Succeeded!!</p>
1010
</body>
1111
</html>

0 commit comments

Comments
 (0)