PluginProbe
ElasticPress / 4.2.2
ElasticPress v4.2.2
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / assets / js / utils / helpers.js

helpers.js in ElasticPress 4.2.2, at assets/js/utils/helpers.js

158 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Simple throttling function for waiting a set amount of time after the last keypress
3 * So we don't overload the server with too many requests at once
4 *
5 * @param {Function} fn - function to be debounced
6 * @param {number} delay - integer
7 * @returns {Function} - new function, with the provided function wrapped in a timeout
8 */
9 export const debounce = (fn, delay) => {
10 let timer = null;
11
12 // don't use a fat arrow in order to preserve the proper context
13 return function debouncedFunction(...args) {
14 const context = this;
15 window.clearTimeout(timer);
16
17 timer = window.setTimeout(() => {
18 fn.apply(context, args);
19 }, delay);
20 };
21 };
22
23 /**
24 * Helper function to escape input to be treated as a literal string with a RegEx
25 *
26 * @param {string} string - string to be escaped
27 * @returns {string} escaped string
28 */
29 export const escapeRegExp = (string) => string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
30
31 /**
32 * Helper function to escape input to be treated as a literal string with a RegEx
33 *
34 * @param {string} string - string to replace
35 * @param {string} term - tearm to search for
36 * @param {string} replacement replace value to use
37 * @returns {string} replaced string
38 */
39 export const replaceGlobally = (string, term, replacement) => {
40 return string.replace(new RegExp(escapeRegExp(term), 'g'), replacement);
41 };
42
43 /**
44 * Escapes double quotes for specific data-attr
45 *
46 * @param {string} str The provided string containing double quotes
47 * @returns {string} The escaped string
48 */
49 export const escapeDoubleQuotes = (str) => str.replace(/\\([\s\S])|(")/g, '"');
50
51 /**
52 * Finds parent node with the provided class param
53 *
54 * @param {*} el - node to search for its ancestor
55 * @param {*} className - class attribute to search for
56 * @returns {Element} - ancestor element of provided el
57 */
58 export const findAncestorByClass = (el, className) => {
59 // eslint-disable-next-line
60 while ( ( el = el.parentElement ) && !el.classList.contains( className ) );
61 return el;
62 };
63
64 /**
65 * Array pluck
66 *
67 * @param {Array} array - array to search
68 * @param {string} key - array to search
69 * @returns {Array} - new array
70 */
71 export const pluck = (array, key) => {
72 return array.map((o) => o[key]);
73 };
74
75 /**
76 * Formats object like a url query string, which is how ajax methods
77 * in PHP expect to receive the data, e.g. action_wp_ajax_ep_save_feature
78 * from dashboard.php
79 *
80 * @param {object} obj - js object
81 * @returns {string} urlencoded string for POST ajax request
82 */
83 export const formatPostBody = (obj) => {
84 return Object.keys(obj)
85 .map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`)
86 .join('&')
87 .replace(/%20/g, '+');
88 };
89
90 /**
91 * Helper method to wrap show/hide elements. Not exported.
92 *
93 * @param {Array} els - could possibly be a single node, or an array of nodes
94 * @param {string} display - css display property to set
95 */
96 const showOrHideNodes = (els, display) => {
97 let nodes = [];
98
99 // convert nodelist to array
100 // eslint-disable-next-line no-prototype-builtins
101 if (NodeList.prototype.isPrototypeOf(els)) {
102 nodes = Array.from(els);
103 }
104
105 // if not converted, then it was a single node,
106 // so create an array
107 if (!nodes.length) {
108 if (Array.isArray(els)) {
109 nodes = [...els, ...nodes];
110 } else {
111 nodes.push(els);
112 }
113 }
114
115 nodes.forEach((el) => {
116 el.style.display = display; // eslint-disable-line no-param-reassign
117 });
118 };
119
120 /**
121 * Decorated helper function to show node/NodeList/array of nodes
122 *
123 * @param {Array} els - Nodelist/array of Nodes to show
124 * @returns {Function} - showOrHideNodes
125 */
126 export const showElements = (els) => showOrHideNodes(els, 'inline-block');
127
128 /**
129 * Decorated helper function to hide node/NodeList/array of nodes
130 *
131 * @param {Array} els - Nodelist/array of Nodes to show
132 * @returns {Function} - showOrHideNodes
133 */
134 export const hideElements = (els) => showOrHideNodes(els, 'none');
135
136 /**
137 * Specify a function to execute when the DOM is fully loaded.
138 *
139 * @param {Function} callback A function to execute after the DOM is ready.
140 * @returns {void}
141 */
142 export const domReady = (callback) => {
143 if (typeof document === 'undefined') {
144 return;
145 }
146
147 if (
148 document.readyState === 'complete' || // DOMContentLoaded + Images/Styles/etc loaded, so we call directly.
149 document.readyState === 'interactive' // DOMContentLoaded fires at this point, so we call directly.
150 ) {
151 callback();
152 return;
153 }
154
155 // DOMContentLoaded has not fired yet, delay callback until then.
156 document.addEventListener('DOMContentLoaded', callback);
157 };
158