@magic-spells/cart-progress-bar 0.2.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +89 -32
- package/dist/cart-progress-bar.css +15 -36
- package/dist/cart-progress-bar.esm.js +197 -273
- package/dist/cart-progress-bar.min.css +2 -1
- package/dist/cart-progress-bar.min.js +1 -1
- package/package.json +19 -26
- package/dist/cart-progress-bar.cjs.js +0 -333
- package/dist/cart-progress-bar.cjs.js.map +0 -1
- package/dist/cart-progress-bar.esm.js.map +0 -1
- package/dist/cart-progress-bar.js +0 -339
- package/dist/cart-progress-bar.js.map +0 -1
- package/dist/cart-progress-bar.scss +0 -124
- package/src/cart-progress-bar.js +0 -332
- package/src/cart-progress-bar.scss +0 -124
|
@@ -1,330 +1,254 @@
|
|
|
1
|
+
//#region src/cart-progress-bar.js
|
|
1
2
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
* ProgressBar helper class for the visual progress bar element
|
|
4
|
+
*/
|
|
5
|
+
var ProgressBar = class extends HTMLElement {
|
|
5
6
|
constructor() {
|
|
6
7
|
super();
|
|
7
|
-
this
|
|
8
|
+
const _ = this;
|
|
9
|
+
_.setAttribute("role", "progressbar");
|
|
10
|
+
_.setAttribute("aria-valuemin", "0");
|
|
11
|
+
_.setAttribute("aria-valuemax", "100");
|
|
12
|
+
_.setAttribute("aria-valuenow", "0");
|
|
13
|
+
_.innerHTML = "<div class=\"progress-bar-fill\"></div>";
|
|
8
14
|
}
|
|
9
|
-
|
|
10
|
-
#setupProgressBar() {
|
|
11
|
-
this.setAttribute('role', 'progressbar');
|
|
12
|
-
this.setAttribute('aria-valuemin', '0');
|
|
13
|
-
this.setAttribute('aria-valuemax', '100');
|
|
14
|
-
this.setAttribute('aria-valuenow', '0');
|
|
15
|
-
|
|
16
|
-
// Create the visual progress bar
|
|
17
|
-
this.innerHTML = '<div class="progress-bar-fill"></div>';
|
|
18
|
-
}
|
|
19
|
-
|
|
20
15
|
setPercent(percent) {
|
|
21
|
-
const
|
|
22
|
-
this.style.setProperty(
|
|
23
|
-
this.setAttribute(
|
|
16
|
+
const p = Math.max(0, Math.min(100, percent));
|
|
17
|
+
this.style.setProperty("--cart-progress-percent", `${p}%`);
|
|
18
|
+
this.setAttribute("aria-valuenow", p);
|
|
24
19
|
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// Define ProgressBar custom element immediately so it's available for CartProgressBar
|
|
28
|
-
customElements.define('progress-bar', ProgressBar);
|
|
29
|
-
|
|
20
|
+
};
|
|
21
|
+
if (!customElements.get("progress-bar")) customElements.define("progress-bar", ProgressBar);
|
|
30
22
|
/**
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
#
|
|
36
|
-
#
|
|
37
|
-
#
|
|
38
|
-
#
|
|
39
|
-
#
|
|
40
|
-
#
|
|
41
|
-
#
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
23
|
+
* CartProgressBar main component
|
|
24
|
+
*/
|
|
25
|
+
var CartProgressBar = class extends HTMLElement {
|
|
26
|
+
#threshold = 0;
|
|
27
|
+
#current = 0;
|
|
28
|
+
#percent = 0;
|
|
29
|
+
#msgAbove = "";
|
|
30
|
+
#msgBelow = "";
|
|
31
|
+
#bar = null;
|
|
32
|
+
#msgEl = null;
|
|
33
|
+
#moneyFmt = null;
|
|
34
|
+
#debounce = null;
|
|
35
|
+
#listenSelector = "cart-panel";
|
|
36
|
+
#listenEvent = "cart-panel:data-changed";
|
|
37
|
+
#listenTarget = null;
|
|
38
|
+
#listenHandler = null;
|
|
46
39
|
static get observedAttributes() {
|
|
47
|
-
return [
|
|
40
|
+
return [
|
|
41
|
+
"threshold",
|
|
42
|
+
"current",
|
|
43
|
+
"message-above",
|
|
44
|
+
"message-below",
|
|
45
|
+
"money-format",
|
|
46
|
+
"listen-selector",
|
|
47
|
+
"listen-event"
|
|
48
|
+
];
|
|
48
49
|
}
|
|
49
|
-
|
|
50
50
|
constructor() {
|
|
51
51
|
super();
|
|
52
|
-
this
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
// Store original message templates
|
|
61
|
-
this.#originalAboveMessage = this.getAttribute('message-above') || '';
|
|
62
|
-
this.#originalBelowMessage = this.getAttribute('message-below') || '';
|
|
52
|
+
const _ = this;
|
|
53
|
+
_.#threshold = parseFloat(_.getAttribute("threshold")) || 0;
|
|
54
|
+
_.#current = parseFloat(_.getAttribute("current")) || 0;
|
|
55
|
+
_.#msgAbove = _.getAttribute("message-above") || "";
|
|
56
|
+
_.#msgBelow = _.getAttribute("message-below") || "";
|
|
57
|
+
_.#moneyFmt = _.getAttribute("money-format");
|
|
58
|
+
_.#listenSelector = _.getAttribute("listen-selector") || "cart-panel";
|
|
59
|
+
_.#listenEvent = _.getAttribute("listen-event") || "cart-panel:data-changed";
|
|
63
60
|
}
|
|
64
|
-
|
|
65
61
|
async connectedCallback() {
|
|
66
|
-
|
|
67
|
-
await customElements.whenDefined('progress-bar');
|
|
68
|
-
|
|
69
|
-
if (!customElements.get('progress-bar')) {
|
|
70
|
-
throw new Error('<progress-bar> must be registered before <cart-progress-bar> runs');
|
|
71
|
-
}
|
|
72
|
-
|
|
62
|
+
await customElements.whenDefined("progress-bar");
|
|
73
63
|
this.#render();
|
|
74
64
|
this.#updateProgress();
|
|
75
65
|
this.#attachListeners();
|
|
76
66
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
67
|
+
disconnectedCallback() {
|
|
68
|
+
if (this.#debounce) clearTimeout(this.#debounce);
|
|
69
|
+
this.#detachListeners();
|
|
70
|
+
}
|
|
71
|
+
attributeChangedCallback(name, oldVal, newVal) {
|
|
72
|
+
if (oldVal === newVal) return;
|
|
73
|
+
const _ = this;
|
|
81
74
|
switch (name) {
|
|
82
|
-
case
|
|
83
|
-
|
|
84
|
-
|
|
75
|
+
case "threshold":
|
|
76
|
+
_.#threshold = parseFloat(newVal) || 0;
|
|
77
|
+
_.#updateProgress();
|
|
78
|
+
break;
|
|
79
|
+
case "current":
|
|
80
|
+
_.#current = parseFloat(newVal) || 0;
|
|
81
|
+
_.#updateProgress();
|
|
85
82
|
break;
|
|
86
|
-
case
|
|
87
|
-
|
|
88
|
-
|
|
83
|
+
case "message-above":
|
|
84
|
+
_.#msgAbove = newVal || "";
|
|
85
|
+
_.#updateMessages();
|
|
89
86
|
break;
|
|
90
|
-
case
|
|
91
|
-
|
|
92
|
-
|
|
87
|
+
case "message-below":
|
|
88
|
+
_.#msgBelow = newVal || "";
|
|
89
|
+
_.#updateMessages();
|
|
93
90
|
break;
|
|
94
|
-
case
|
|
95
|
-
|
|
96
|
-
|
|
91
|
+
case "money-format":
|
|
92
|
+
_.#moneyFmt = newVal;
|
|
93
|
+
_.#updateMessages();
|
|
97
94
|
break;
|
|
95
|
+
case "listen-selector":
|
|
96
|
+
_.#listenSelector = newVal || "cart-panel";
|
|
97
|
+
_.#detachListeners();
|
|
98
|
+
_.#attachListeners();
|
|
99
|
+
break;
|
|
100
|
+
case "listen-event":
|
|
101
|
+
_.#detachListeners();
|
|
102
|
+
_.#listenEvent = newVal || "cart-panel:data-changed";
|
|
103
|
+
_.#attachListeners();
|
|
98
104
|
}
|
|
99
105
|
}
|
|
100
|
-
|
|
101
106
|
#render() {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
// Create message element if it doesn't exist but we have message templates
|
|
111
|
-
if (!this.#messageElement && (this.#originalAboveMessage || this.#originalBelowMessage)) {
|
|
112
|
-
this.#messageElement = document.createElement('p');
|
|
113
|
-
this.#messageElement.setAttribute('data-content-cart-progress-message', '');
|
|
114
|
-
this.appendChild(this.#messageElement);
|
|
107
|
+
const _ = this;
|
|
108
|
+
_.#msgEl = _.querySelector("[data-content-cart-progress-message]");
|
|
109
|
+
_.#bar = _.querySelector("progress-bar");
|
|
110
|
+
if (!_.#msgEl && (_.#msgAbove || _.#msgBelow)) {
|
|
111
|
+
_.#msgEl = document.createElement("p");
|
|
112
|
+
_.#msgEl.setAttribute("data-content-cart-progress-message", "");
|
|
113
|
+
_.appendChild(_.#msgEl);
|
|
115
114
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
// Ensure progress-bar is defined before creating
|
|
120
|
-
if (customElements.get('progress-bar')) {
|
|
121
|
-
this.#progressBar = document.createElement('progress-bar');
|
|
122
|
-
this.appendChild(this.#progressBar);
|
|
123
|
-
} else {
|
|
124
|
-
// Fallback: wait for definition
|
|
125
|
-
customElements.whenDefined('progress-bar').then(() => {
|
|
126
|
-
if (!this.#progressBar) {
|
|
127
|
-
this.#progressBar = document.createElement('progress-bar');
|
|
128
|
-
this.appendChild(this.#progressBar);
|
|
129
|
-
this.#updateProgress(); // Update progress after creating
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
}
|
|
115
|
+
if (!_.#bar) {
|
|
116
|
+
_.#bar = document.createElement("progress-bar");
|
|
117
|
+
_.appendChild(_.#bar);
|
|
133
118
|
}
|
|
134
119
|
}
|
|
135
|
-
|
|
136
120
|
#updateProgress() {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
// Update progress bar
|
|
144
|
-
if (this.#progressBar) {
|
|
145
|
-
this.#progressBar.setPercent(this.#progressPercent);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// Update messages
|
|
149
|
-
this.#updateMessages();
|
|
150
|
-
|
|
151
|
-
// Update component state
|
|
152
|
-
this.#updateComponentState();
|
|
121
|
+
const _ = this;
|
|
122
|
+
const converted = _.#converted();
|
|
123
|
+
_.#percent = converted === 0 ? 100 : Math.min(100, _.#current / converted * 100);
|
|
124
|
+
if (_.#bar) _.#bar.setPercent(_.#percent);
|
|
125
|
+
_.#updateState(converted);
|
|
153
126
|
}
|
|
154
|
-
|
|
155
127
|
#attachListeners() {
|
|
156
|
-
|
|
157
|
-
const
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
this.#handleCartDataChange(event);
|
|
163
|
-
});
|
|
128
|
+
const _ = this;
|
|
129
|
+
const target = _.closest(_.#listenSelector);
|
|
130
|
+
if (target) {
|
|
131
|
+
_.#listenHandler = (e) => _.#onCartChange(e);
|
|
132
|
+
_.#listenTarget = target;
|
|
133
|
+
target.addEventListener(_.#listenEvent, _.#listenHandler);
|
|
164
134
|
}
|
|
165
135
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
// Otherwise fall back to total_price for backwards compatibility
|
|
173
|
-
let currentAmount = 0;
|
|
174
|
-
|
|
175
|
-
if (typeof updatedCart.calculated_subtotal !== 'undefined') {
|
|
176
|
-
// calculated_subtotal is already in dollars, no conversion needed
|
|
177
|
-
currentAmount = updatedCart.calculated_subtotal / 100;
|
|
178
|
-
} else if (typeof updatedCart.total_price !== 'undefined') {
|
|
179
|
-
// Convert from cents to dollars if needed (Shopify typically returns cents)
|
|
180
|
-
currentAmount = updatedCart.total_price / 100;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
this.setCurrentAmount(currentAmount);
|
|
136
|
+
#detachListeners() {
|
|
137
|
+
const _ = this;
|
|
138
|
+
if (_.#listenTarget && _.#listenHandler) {
|
|
139
|
+
_.#listenTarget.removeEventListener(_.#listenEvent, _.#listenHandler);
|
|
140
|
+
_.#listenTarget = null;
|
|
141
|
+
_.#listenHandler = null;
|
|
184
142
|
}
|
|
185
143
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
const
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
const message = messageTemplate
|
|
209
|
-
.replace(/\{\s*amount\s*\}/g, formattedAmount)
|
|
210
|
-
.replace(/\[\s*amount\s*\]/g, formattedAmount);
|
|
211
|
-
this.#messageElement.textContent = message;
|
|
212
|
-
this.#messageElement.style.display = 'block';
|
|
213
|
-
} else {
|
|
214
|
-
// Hide message if no template available for current state
|
|
215
|
-
this.#messageElement.style.display = 'none';
|
|
216
|
-
}
|
|
144
|
+
#onCartChange(event) {
|
|
145
|
+
const _ = this;
|
|
146
|
+
const cart = event.detail;
|
|
147
|
+
if (!cart) return;
|
|
148
|
+
if (_.#debounce) clearTimeout(_.#debounce);
|
|
149
|
+
_.#debounce = setTimeout(() => {
|
|
150
|
+
_.#debounce = null;
|
|
151
|
+
const amt = (cart.calculated_subtotal ?? cart.total_price ?? 0) / 100;
|
|
152
|
+
_.setCurrentAmount(amt);
|
|
153
|
+
}, 100);
|
|
154
|
+
}
|
|
155
|
+
#updateState(converted) {
|
|
156
|
+
const _ = this;
|
|
157
|
+
const complete = _.#current >= converted;
|
|
158
|
+
const remaining = Math.max(0, converted - _.#current);
|
|
159
|
+
const formatted = _.#fmtMoney(remaining);
|
|
160
|
+
if (_.#msgEl) {
|
|
161
|
+
let tpl = complete ? _.#msgAbove : _.#msgBelow || _.#msgAbove;
|
|
162
|
+
if (tpl) {
|
|
163
|
+
_.#msgEl.textContent = tpl.replace(/\[\s*amount\s*\]/g, formatted);
|
|
164
|
+
_.#msgEl.style.display = "block";
|
|
165
|
+
} else _.#msgEl.style.display = "none";
|
|
217
166
|
}
|
|
167
|
+
_.setAttribute("complete", complete.toString());
|
|
218
168
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
169
|
+
#fmtMoney(amt) {
|
|
170
|
+
const fmt = this.#moneyFmt;
|
|
171
|
+
if (!fmt) return amt.toFixed(2).replace(/\.00$/, "");
|
|
172
|
+
const fixed = amt.toFixed(2);
|
|
173
|
+
const noDecimals = Math.round(amt).toString();
|
|
174
|
+
const withComma = fixed.replace(".", ",");
|
|
175
|
+
const noDecWithComma = noDecimals.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
176
|
+
return fmt.replace(/\{\{\s*amount_no_decimals_with_comma_separator\s*\}\}/g, noDecWithComma).replace(/\{\{\s*amount_with_comma_separator\s*\}\}/g, withComma).replace(/\{\{\s*amount_no_decimals\s*\}\}/g, noDecimals).replace(/\{\{\s*amount\s*\}\}/g, fixed);
|
|
223
177
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
* Public API: Set the progress percentage directly
|
|
228
|
-
* @param {number} percent - Progress percentage (0-100)
|
|
229
|
-
*/
|
|
230
|
-
setPercent(percent) {
|
|
231
|
-
const clampedPercent = Math.max(0, Math.min(100, percent));
|
|
232
|
-
this.#progressPercent = clampedPercent;
|
|
233
|
-
|
|
234
|
-
if (this.#progressBar) {
|
|
235
|
-
this.#progressBar.setPercent(clampedPercent);
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
// Calculate current amount based on percentage
|
|
239
|
-
this.#currentAmount = (clampedPercent / 100) * this.#minAmount;
|
|
240
|
-
this.setAttribute('current', this.#currentAmount.toString());
|
|
241
|
-
|
|
242
|
-
this.#updateMessages();
|
|
243
|
-
this.#updateComponentState();
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* Public API: Set the current cart amount
|
|
248
|
-
* @param {number} amount - Current cart amount
|
|
249
|
-
*/
|
|
250
|
-
setCurrentAmount(amount) {
|
|
251
|
-
this.#currentAmount = parseFloat(amount) || 0;
|
|
252
|
-
this.setAttribute('current', this.#currentAmount.toString());
|
|
253
|
-
this.#updateProgress();
|
|
178
|
+
#converted() {
|
|
179
|
+
const rate = parseFloat(window.Shopify?.currency?.rate) || 1;
|
|
180
|
+
return this.#threshold * rate;
|
|
254
181
|
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
182
|
+
#updateMessages() {
|
|
183
|
+
this.#updateState(this.#converted());
|
|
184
|
+
}
|
|
185
|
+
setPercent(pct) {
|
|
186
|
+
const _ = this;
|
|
187
|
+
const p = Math.max(0, Math.min(100, pct));
|
|
188
|
+
_.#percent = p;
|
|
189
|
+
if (_.#bar) _.#bar.setPercent(p);
|
|
190
|
+
_.#current = p / 100 * _.#converted();
|
|
191
|
+
_.setAttribute("current", _.#current.toString());
|
|
192
|
+
_.#updateState(_.#converted());
|
|
193
|
+
}
|
|
194
|
+
setCurrentAmount(amt) {
|
|
195
|
+
const _ = this;
|
|
196
|
+
_.#current = parseFloat(amt) || 0;
|
|
197
|
+
_.setAttribute("current", _.#current.toString());
|
|
198
|
+
_.#updateProgress();
|
|
199
|
+
}
|
|
200
|
+
setThresholdAmount(amt) {
|
|
201
|
+
const _ = this;
|
|
202
|
+
_.#threshold = parseFloat(amt) || 0;
|
|
203
|
+
_.setAttribute("threshold", _.#threshold.toString());
|
|
204
|
+
_.#updateProgress();
|
|
205
|
+
}
|
|
206
|
+
/** @deprecated Use setThresholdAmount */
|
|
207
|
+
setMinAmount(amt) {
|
|
208
|
+
this.setThresholdAmount(amt);
|
|
273
209
|
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Public API: Get current progress information
|
|
277
|
-
*/
|
|
278
210
|
getProgress() {
|
|
211
|
+
const _ = this;
|
|
212
|
+
const converted = _.#converted();
|
|
279
213
|
return {
|
|
280
|
-
currentAmount:
|
|
281
|
-
thresholdAmount:
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
214
|
+
currentAmount: _.#current,
|
|
215
|
+
thresholdAmount: _.#threshold,
|
|
216
|
+
convertedThreshold: converted,
|
|
217
|
+
minAmount: _.#threshold,
|
|
218
|
+
remainingAmount: Math.max(0, converted - _.#current),
|
|
219
|
+
percent: _.#percent,
|
|
220
|
+
isComplete: _.#current >= converted,
|
|
221
|
+
currencyRate: parseFloat(window.Shopify?.currency?.rate) || 1
|
|
286
222
|
};
|
|
287
223
|
}
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
*/
|
|
294
|
-
setMessages(aboveMessage = null, belowMessage = null) {
|
|
295
|
-
if (aboveMessage !== null) {
|
|
296
|
-
this.#originalAboveMessage = aboveMessage;
|
|
297
|
-
this.setAttribute('message-above', aboveMessage);
|
|
224
|
+
setMessages(above = null, below = null) {
|
|
225
|
+
const _ = this;
|
|
226
|
+
if (above !== null) {
|
|
227
|
+
_.#msgAbove = above;
|
|
228
|
+
_.setAttribute("message-above", above);
|
|
298
229
|
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
this.setAttribute('message-below', belowMessage);
|
|
230
|
+
if (below !== null) {
|
|
231
|
+
_.#msgBelow = below;
|
|
232
|
+
_.setAttribute("message-below", below);
|
|
303
233
|
}
|
|
304
|
-
|
|
305
|
-
this.#updateMessages();
|
|
234
|
+
_.#updateMessages();
|
|
306
235
|
}
|
|
307
|
-
|
|
308
|
-
// Getters
|
|
309
236
|
get currentAmount() {
|
|
310
|
-
return this.#
|
|
237
|
+
return this.#current;
|
|
311
238
|
}
|
|
312
239
|
get thresholdAmount() {
|
|
313
|
-
return this.#
|
|
240
|
+
return this.#threshold;
|
|
314
241
|
}
|
|
315
242
|
get minAmount() {
|
|
316
|
-
return this.#
|
|
317
|
-
}
|
|
243
|
+
return this.#threshold;
|
|
244
|
+
}
|
|
318
245
|
get percent() {
|
|
319
|
-
return this.#
|
|
246
|
+
return this.#percent;
|
|
320
247
|
}
|
|
321
248
|
get isComplete() {
|
|
322
|
-
return this.#
|
|
249
|
+
return this.#current >= this.#converted();
|
|
323
250
|
}
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
customElements.define('cart-progress-bar', CartProgressBar);
|
|
328
|
-
|
|
251
|
+
};
|
|
252
|
+
if (!customElements.get("cart-progress-bar")) customElements.define("cart-progress-bar", CartProgressBar);
|
|
253
|
+
//#endregion
|
|
329
254
|
export { CartProgressBar, ProgressBar };
|
|
330
|
-
//# sourceMappingURL=cart-progress-bar.esm.js.map
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
cart-progress-bar{--cart-progress-bar-height:12px;--cart-progress-bar-border-radius:6px;--cart-progress-bar-
|
|
1
|
+
cart-progress-bar{--cart-progress-bar-height:12px;--cart-progress-bar-border-radius:6px;--cart-progress-bar-transition-duration:.3s;--cart-progress-bar-bg:#e9ecef;--cart-progress-bar-fill-before:#28a745;--cart-progress-bar-fill-after:#007bff;--cart-progress-percent:0%;--cart-progress-bar-fill-current:var(--cart-progress-bar-fill-before);width:100%;display:block}cart-progress-bar p[data-content-cart-progress-message]{transition:opacity var(--cart-progress-bar-transition-duration) ease}cart-progress-bar[complete=true] p[data-content-cart-progress-message]{font-weight:600}cart-progress-bar[complete=false]{--cart-progress-bar-fill-current:var(--cart-progress-bar-fill-before)}cart-progress-bar[complete=true]{--cart-progress-bar-fill-current:var(--cart-progress-bar-fill-after)}progress-bar{width:100%;height:var(--cart-progress-bar-height);background-color:var(--cart-progress-bar-bg);border-radius:var(--cart-progress-bar-border-radius);display:block;position:relative;overflow:hidden}progress-bar .progress-bar-fill{height:100%;width:var(--cart-progress-percent);background-color:var(--cart-progress-bar-fill-current);border-radius:var(--cart-progress-bar-border-radius);transition:width var(--cart-progress-bar-transition-duration) ease, background-color var(--cart-progress-bar-transition-duration) ease}
|
|
2
|
+
/*$vite$:1*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).CartProgressBar={})}(this,function(e){"
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).CartProgressBar={})}(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:"Module"});var ProgressBar=class extends HTMLElement{constructor(){super();const e=this;e.setAttribute("role","progressbar"),e.setAttribute("aria-valuemin","0"),e.setAttribute("aria-valuemax","100"),e.setAttribute("aria-valuenow","0"),e.innerHTML='<div class="progress-bar-fill"></div>'}setPercent(e){const t=Math.max(0,Math.min(100,e));this.style.setProperty("--cart-progress-percent",`${t}%`),this.setAttribute("aria-valuenow",t)}};customElements.get("progress-bar")||customElements.define("progress-bar",ProgressBar);var CartProgressBar=class extends HTMLElement{#e=0;#t=0;#s=0;#r="";#n="";#a=null;#o=null;#l=null;#c=null;#i="cart-panel";#u="cart-panel:data-changed";#d=null;#m=null;static get observedAttributes(){return["threshold","current","message-above","message-below","money-format","listen-selector","listen-event"]}constructor(){super();const e=this;e.#e=parseFloat(e.getAttribute("threshold"))||0,e.#t=parseFloat(e.getAttribute("current"))||0,e.#r=e.getAttribute("message-above")||"",e.#n=e.getAttribute("message-below")||"",e.#l=e.getAttribute("money-format"),e.#i=e.getAttribute("listen-selector")||"cart-panel",e.#u=e.getAttribute("listen-event")||"cart-panel:data-changed"}async connectedCallback(){await customElements.whenDefined("progress-bar"),this.#g(),this.#h(),this.#p()}disconnectedCallback(){this.#c&&clearTimeout(this.#c),this.#b()}attributeChangedCallback(e,t,s){if(t===s)return;const r=this;switch(e){case"threshold":r.#e=parseFloat(s)||0,r.#h();break;case"current":r.#t=parseFloat(s)||0,r.#h();break;case"message-above":r.#r=s||"",r.#v();break;case"message-below":r.#n=s||"",r.#v();break;case"money-format":r.#l=s,r.#v();break;case"listen-selector":r.#i=s||"cart-panel",r.#b(),r.#p();break;case"listen-event":r.#b(),r.#u=s||"cart-panel:data-changed",r.#p()}}#g(){const e=this;e.#o=e.querySelector("[data-content-cart-progress-message]"),e.#a=e.querySelector("progress-bar"),e.#o||!e.#r&&!e.#n||(e.#o=document.createElement("p"),e.#o.setAttribute("data-content-cart-progress-message",""),e.appendChild(e.#o)),e.#a||(e.#a=document.createElement("progress-bar"),e.appendChild(e.#a))}#h(){const e=this,t=e.#A();e.#s=0===t?100:Math.min(100,e.#t/t*100),e.#a&&e.#a.setPercent(e.#s),e.#y(t)}#p(){const e=this,t=e.closest(e.#i);t&&(e.#m=t=>e.#f(t),e.#d=t,t.addEventListener(e.#u,e.#m))}#b(){const e=this;e.#d&&e.#m&&(e.#d.removeEventListener(e.#u,e.#m),e.#d=null,e.#m=null)}#f(e){const t=this,s=e.detail;s&&(t.#c&&clearTimeout(t.#c),t.#c=setTimeout(()=>{t.#c=null;const e=(s.calculated_subtotal??s.total_price??0)/100;t.setCurrentAmount(e)},100))}#y(e){const t=this,s=t.#t>=e,r=Math.max(0,e-t.#t),n=t.#E(r);if(t.#o){let e=s?t.#r:t.#n||t.#r;e?(t.#o.textContent=e.replace(/\[\s*amount\s*\]/g,n),t.#o.style.display="block"):t.#o.style.display="none"}t.setAttribute("complete",s.toString())}#E(e){const t=this.#l;if(!t)return e.toFixed(2).replace(/\.00$/,"");const s=e.toFixed(2),r=Math.round(e).toString(),n=s.replace(".",","),a=r.replace(/\B(?=(\d{3})+(?!\d))/g,",");return t.replace(/\{\{\s*amount_no_decimals_with_comma_separator\s*\}\}/g,a).replace(/\{\{\s*amount_with_comma_separator\s*\}\}/g,n).replace(/\{\{\s*amount_no_decimals\s*\}\}/g,r).replace(/\{\{\s*amount\s*\}\}/g,s)}#A(){const e=parseFloat(window.Shopify?.currency?.rate)||1;return this.#e*e}#v(){this.#y(this.#A())}setPercent(e){const t=this,s=Math.max(0,Math.min(100,e));t.#s=s,t.#a&&t.#a.setPercent(s),t.#t=s/100*t.#A(),t.setAttribute("current",t.#t.toString()),t.#y(t.#A())}setCurrentAmount(e){const t=this;t.#t=parseFloat(e)||0,t.setAttribute("current",t.#t.toString()),t.#h()}setThresholdAmount(e){const t=this;t.#e=parseFloat(e)||0,t.setAttribute("threshold",t.#e.toString()),t.#h()}setMinAmount(e){this.setThresholdAmount(e)}getProgress(){const e=this,t=e.#A();return{currentAmount:e.#t,thresholdAmount:e.#e,convertedThreshold:t,minAmount:e.#e,remainingAmount:Math.max(0,t-e.#t),percent:e.#s,isComplete:e.#t>=t,currencyRate:parseFloat(window.Shopify?.currency?.rate)||1}}setMessages(e=null,t=null){const s=this;null!==e&&(s.#r=e,s.setAttribute("message-above",e)),null!==t&&(s.#n=t,s.setAttribute("message-below",t)),s.#v()}get currentAmount(){return this.#t}get thresholdAmount(){return this.#e}get minAmount(){return this.#e}get percent(){return this.#s}get isComplete(){return this.#t>=this.#A()}};customElements.get("cart-progress-bar")||customElements.define("cart-progress-bar",CartProgressBar),e.CartProgressBar=CartProgressBar,e.ProgressBar=ProgressBar});
|