| 1 |
"use strict"; |
| 2 |
|
| 3 |
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } |
| 4 |
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } |
| 5 |
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } |
| 6 |
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } |
| 7 |
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } |
| 8 |
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } |
| 9 |
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } |
| 10 |
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } |
| 11 |
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } |
| 12 |
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } |
| 13 |
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } |
| 14 |
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } |
| 15 |
(function ($) { |
| 16 |
'use strict'; |
| 17 |
|
| 18 |
var shapesDefaultStyles = { |
| 19 |
text: { |
| 20 |
'text-shape-1': { |
| 21 |
width: 100, |
| 22 |
height: 32, |
| 23 |
borderRadius: 5, |
| 24 |
marginX: 10, |
| 25 |
marginY: 10 |
| 26 |
}, |
| 27 |
'text-shape-2': { |
| 28 |
width: 100, |
| 29 |
height: 32, |
| 30 |
borderRadius: 5, |
| 31 |
marginX: 10, |
| 32 |
marginY: 10 |
| 33 |
}, |
| 34 |
'text-shape-3': { |
| 35 |
width: 125, |
| 36 |
height: 32, |
| 37 |
borderRadius: 5, |
| 38 |
marginX: 10, |
| 39 |
marginY: 10 |
| 40 |
}, |
| 41 |
'text-shape-4': { |
| 42 |
width: 125, |
| 43 |
height: 32, |
| 44 |
borderRadius: 5, |
| 45 |
marginX: 10, |
| 46 |
marginY: 10 |
| 47 |
}, |
| 48 |
'text-shape-5': { |
| 49 |
width: 140, |
| 50 |
height: 32, |
| 51 |
borderRadius: 0, |
| 52 |
marginX: 25, |
| 53 |
marginY: 10, |
| 54 |
disableInShortCode: true |
| 55 |
}, |
| 56 |
'text-shape-6': { |
| 57 |
width: 140, |
| 58 |
height: 32, |
| 59 |
borderRadius: 0, |
| 60 |
marginX: 25, |
| 61 |
marginY: 10, |
| 62 |
disableInShortCode: true |
| 63 |
}, |
| 64 |
'text-shape-7': { |
| 65 |
width: 90, |
| 66 |
height: 90, |
| 67 |
borderRadius: 5, |
| 68 |
marginX: 10, |
| 69 |
marginY: 10 |
| 70 |
}, |
| 71 |
'text-shape-8': { |
| 72 |
width: 100, |
| 73 |
height: 100, |
| 74 |
borderRadius: 50, |
| 75 |
marginX: 10, |
| 76 |
marginY: 10 |
| 77 |
} |
| 78 |
}, |
| 79 |
image: { |
| 80 |
'image-shape-1': { |
| 81 |
width: 45, |
| 82 |
height: 45 |
| 83 |
}, |
| 84 |
'image-shape-2': { |
| 85 |
width: 45, |
| 86 |
height: 25 |
| 87 |
}, |
| 88 |
'image-shape-3': { |
| 89 |
width: 45, |
| 90 |
height: 15 |
| 91 |
}, |
| 92 |
'image-shape-4': { |
| 93 |
width: 45, |
| 94 |
height: 25 |
| 95 |
}, |
| 96 |
'image-shape-5': { |
| 97 |
width: 45, |
| 98 |
height: 15 |
| 99 |
}, |
| 100 |
'image-shape-6': { |
| 101 |
width: 38, |
| 102 |
height: 45 |
| 103 |
}, |
| 104 |
'image-shape-7': { |
| 105 |
width: 45, |
| 106 |
height: 30 |
| 107 |
}, |
| 108 |
'image-shape-8': { |
| 109 |
width: 45, |
| 110 |
height: 15 |
| 111 |
}, |
| 112 |
'image-shape-9': { |
| 113 |
width: 45, |
| 114 |
height: 27 |
| 115 |
}, |
| 116 |
'image-shape-10': { |
| 117 |
width: 38, |
| 118 |
height: 45 |
| 119 |
}, |
| 120 |
'image-shape-11': { |
| 121 |
width: 45, |
| 122 |
height: 12 |
| 123 |
}, |
| 124 |
'image-shape-12': { |
| 125 |
width: 45, |
| 126 |
height: 15 |
| 127 |
}, |
| 128 |
'image-shape-13': { |
| 129 |
width: 45, |
| 130 |
height: 32 |
| 131 |
}, |
| 132 |
'image-shape-14': { |
| 133 |
width: 42, |
| 134 |
height: 45 |
| 135 |
}, |
| 136 |
'image-shape-15': { |
| 137 |
width: 45, |
| 138 |
height: 10 |
| 139 |
}, |
| 140 |
'image-shape-16': { |
| 141 |
width: 45, |
| 142 |
height: 32 |
| 143 |
}, |
| 144 |
'image-shape-17': { |
| 145 |
width: 45, |
| 146 |
height: 14 |
| 147 |
}, |
| 148 |
'image-shape-18': { |
| 149 |
width: 38, |
| 150 |
height: 45 |
| 151 |
}, |
| 152 |
'image-shape-19': { |
| 153 |
width: 45, |
| 154 |
height: 30 |
| 155 |
}, |
| 156 |
'image-shape-20': { |
| 157 |
width: 45, |
| 158 |
height: 28 |
| 159 |
}, |
| 160 |
'image-shape-21': { |
| 161 |
width: 38, |
| 162 |
height: 45 |
| 163 |
}, |
| 164 |
'image-shape-22': { |
| 165 |
width: 45, |
| 166 |
height: 45 |
| 167 |
}, |
| 168 |
'image-shape-23': { |
| 169 |
width: 45, |
| 170 |
height: 35 |
| 171 |
}, |
| 172 |
'image-shape-24': { |
| 173 |
width: 45, |
| 174 |
height: 45 |
| 175 |
} |
| 176 |
} |
| 177 |
}; |
| 178 |
$(document).on('click', '.merchant-flexible-content-control.product-labels-style .layout', function () { |
| 179 |
var $this = $(this), |
| 180 |
$parent = $this.closest('.merchant-flexible-content-control.product-labels-style'); |
| 181 |
$parent.find('.layout').removeClass('active'); |
| 182 |
$this.addClass('active'); |
| 183 |
initPreview(); |
| 184 |
}); |
| 185 |
$(document).on('merchant-flexible-content-added', function (e, $layout) { |
| 186 |
$('.merchant-flexible-content-control.product-labels-style').find('.layout').removeClass('active'); |
| 187 |
$layout.addClass('active'); |
| 188 |
initPreview(); |
| 189 |
}); |
| 190 |
$(document).on('merchant-product-labels-reload-product-preview', function (e) { |
| 191 |
initPreview(); |
| 192 |
}); |
| 193 |
$(document).on('change.merchant keyup change', function () { |
| 194 |
initPreview(); |
| 195 |
}); |
| 196 |
function initPreview() { |
| 197 |
var layout = $('.merchant-flexible-content-control.product-labels-style').find('.layout.active'), |
| 198 |
labelContent = layout.find('.merchant-field-label input').val(), |
| 199 |
labelType = layout.find('.merchant-field-label_type input:checked').val(), |
| 200 |
textShapeEl = layout.find('.merchant-choices-label_text_shape input:checked'), |
| 201 |
textShape = textShapeEl.val(), |
| 202 |
imageShapeEl = layout.find('.merchant-choices-label_image_shape input:checked'), |
| 203 |
imageShape = imageShapeEl.val(), |
| 204 |
customImageEl = layout.find('.merchant-field-label_image_shape_custom input[type="hidden"]'), |
| 205 |
customImage = customImageEl.val(), |
| 206 |
positionX = layout.find('.merchant-field-position_x input').val() || 10, |
| 207 |
positionY = layout.find('.merchant-field-position_y input').val() || 10, |
| 208 |
positionAnchor = layout.find('.merchant-field-position_anchor select').val() || 'top-left', |
| 209 |
labelWidth = layout.find('.merchant-field-label_width input').val(), |
| 210 |
labelHeight = layout.find('.merchant-field-label_height input').val(), |
| 211 |
backgroundColor = layout.find('.merchant-field-background_color input').val(), |
| 212 |
textColor = layout.find('.merchant-field-text_color input').val(), |
| 213 |
borderRadius = layout.find('.merchant-field-shape_radius input').val(), |
| 214 |
fontSize = layout.find('.merchant-field-font_size input').val(), |
| 215 |
fontStyle = layout.find('.merchant-field-font_style select').val(); |
| 216 |
var labelPreview = $('.merchant-product-labels-preview').find('.merchant-product-labels'); |
| 217 |
var classes = "merchant-product-labels__regular position-absolute merchant-product-labels__".concat(labelType); |
| 218 |
classes += labelType === 'text' ? " merchant-product-labels__".concat(textShape) : ''; |
| 219 |
var labelClassPattern = /\bmerchant-product-labels__\S+/g; |
| 220 |
|
| 221 |
// Build CSS based on anchor point and position values |
| 222 |
var css = { |
| 223 |
'top': positionY + 'px' |
| 224 |
}; |
| 225 |
|
| 226 |
// Handle horizontal positioning based on anchor point |
| 227 |
if (positionAnchor === 'top-right') { |
| 228 |
css['right'] = positionX + 'px'; |
| 229 |
css['left'] = ''; |
| 230 |
} else { |
| 231 |
// top-left (default) |
| 232 |
css['left'] = positionX + 'px'; |
| 233 |
css['right'] = ''; |
| 234 |
} |
| 235 |
if (labelType === 'text') { |
| 236 |
css['width'] = labelWidth + 'px'; |
| 237 |
css['height'] = labelHeight + 'px'; |
| 238 |
css['background-color'] = backgroundColor; |
| 239 |
css['color'] = textColor; |
| 240 |
css['font-size'] = fontSize + 'px'; |
| 241 |
css['border-radius'] = borderRadius + 'px'; |
| 242 |
var fontStyles = { |
| 243 |
'normal': { |
| 244 |
'font-style': '', |
| 245 |
'font-weight': '' |
| 246 |
}, |
| 247 |
'italic': { |
| 248 |
'font-style': 'italic', |
| 249 |
'font-weight': '' |
| 250 |
}, |
| 251 |
'bold': { |
| 252 |
'font-style': '', |
| 253 |
'font-weight': 'bold' |
| 254 |
}, |
| 255 |
'bold_italic': { |
| 256 |
'font-style': 'italic', |
| 257 |
'font-weight': 'bold' |
| 258 |
} |
| 259 |
}; |
| 260 |
css = _objectSpread(_objectSpread({}, css), fontStyles[fontStyle]); |
| 261 |
|
| 262 |
// Apply rotation for shapes 5 and 6 based on anchor point |
| 263 |
if (textShape === 'text-shape-5' || textShape === 'text-shape-6') { |
| 264 |
if (positionAnchor === 'top-right') { |
| 265 |
// Invert rotation for top-right anchor (use -45deg instead of 45deg) |
| 266 |
if (textShape === 'text-shape-5') { |
| 267 |
css['transform'] = 'rotate(45deg) translate(50%, 50%)'; |
| 268 |
css['transform-origin'] = 'right'; |
| 269 |
} else if (textShape === 'text-shape-6') { |
| 270 |
css['transform'] = 'rotate(45deg) translate(50%, 25%)'; |
| 271 |
css['transform-origin'] = 'right'; |
| 272 |
} |
| 273 |
} else { |
| 274 |
// top-left (default) |
| 275 |
if (textShape === 'text-shape-5') { |
| 276 |
css['transform'] = 'rotate(-45deg) translate(-50%, 50%)'; |
| 277 |
css['transform-origin'] = 'left'; |
| 278 |
} else if (textShape === 'text-shape-6') { |
| 279 |
css['transform'] = 'rotate(-45deg) translate(-50%, 25%)'; |
| 280 |
css['transform-origin'] = 'left'; |
| 281 |
} |
| 282 |
} |
| 283 |
} else { |
| 284 |
css['transform'] = 'none'; |
| 285 |
css['transform-origin'] = 'none'; |
| 286 |
} |
| 287 |
var currency = labelPreview.attr('data-currency'); |
| 288 |
|
| 289 |
// Update shortcode to content |
| 290 |
labelContent = labelContent.replace(/{sale}/g, '50%').replace(/{sale_amount}/g, "".concat(currency, "50")).replace(/{inventory}/g, 'In stock').replace(/{inventory_quantity}/g, 19); |
| 291 |
|
| 292 |
// Update label content & styles |
| 293 |
labelPreview.css(css).removeClass(function (index, className) { |
| 294 |
return (className.match(labelClassPattern) || []).join(' '); |
| 295 |
}).removeClass('position-top-right position-top-left position-absolute').addClass(classes).find('span').css({ |
| 296 |
width: '', |
| 297 |
height: '' |
| 298 |
}).text(labelContent.trim()); |
| 299 |
} else { |
| 300 |
css['width'] = ''; |
| 301 |
css['height'] = ''; |
| 302 |
css['background-color'] = ''; |
| 303 |
css['color'] = ''; |
| 304 |
css['font-size'] = ''; |
| 305 |
css['border-radius'] = ''; |
| 306 |
css['transform'] = 'none'; |
| 307 |
css['transform-origin'] = 'none'; |
| 308 |
var img = customImage ? customImageEl.closest('.merchant-field-label_image_shape_custom').find('.merchant-upload-image img').clone() : imageShapeEl.closest('label').find('img').clone(); |
| 309 |
|
| 310 |
// Update label content & styles |
| 311 |
labelPreview.css(css).removeClass(function (index, className) { |
| 312 |
return (className.match(labelClassPattern) || []).join(' '); |
| 313 |
}).removeClass('position-top-right position-top-left position-absolute').addClass(classes).find('span').css({ |
| 314 |
width: labelWidth + 'px', |
| 315 |
height: labelHeight + 'px' |
| 316 |
}).html(img); |
| 317 |
|
| 318 |
// Toggle custom image upload element |
| 319 |
layout.find('.merchant-upload-button-drag-drop').toggle(!customImage); |
| 320 |
} |
| 321 |
} |
| 322 |
$(document).on('change input change.merchant', '.merchant-module-page-setting-box', function (e) { |
| 323 |
$(document).trigger('merchant-product-labels-reload-product-preview'); |
| 324 |
}); |
| 325 |
$(document).on('click', '.merchant-flexible-content-control.product-labels-style .layout', function (e) { |
| 326 |
$('.merchant-flexible-content-control.product-labels-style .layout').removeClass('active'); |
| 327 |
$(this).addClass('active'); |
| 328 |
$(document).trigger('merchant-product-labels-reload-product-preview'); |
| 329 |
}); |
| 330 |
$(document).on('merchant-flexible-content-deleted', function (e, deletedItem) { |
| 331 |
if (deletedItem.hasClass('active')) { |
| 332 |
// if active item was deleted, set previous item to active |
| 333 |
$('.merchant-flexible-content-control.product-labels-style .merchant-flexible-content .layout:first-child').addClass('active'); |
| 334 |
$(document).trigger('merchant-product-labels-reload-product-preview'); |
| 335 |
} |
| 336 |
}); |
| 337 |
$('.merchant-flexible-content-control.product-labels-style .merchant-flexible-content .layout:first-child').addClass('active'); |
| 338 |
$(document).trigger('merchant-product-labels-reload-product-preview'); |
| 339 |
|
| 340 |
// Shapes |
| 341 |
$(document).on('change', '.merchant-field-label_type input', function () { |
| 342 |
var labelType = $(this).val(); |
| 343 |
var $layout = $(this).closest('.layout'); |
| 344 |
var shape = $layout.find(labelType === 'text' ? '.merchant-choices-label_text_shape input:checked' : '.merchant-choices-label_image_shape input:checked').val(); |
| 345 |
updateStyles(labelType, shape, $(this)); |
| 346 |
}); |
| 347 |
$(document).on('change', '.merchant-choices-label_text_shape input', function () { |
| 348 |
var $layout = $(this).closest('.layout'); |
| 349 |
var shape = $layout.find('.merchant-choices-label_text_shape input:checked').val(); |
| 350 |
updateStyles('text', shape, $(this)); |
| 351 |
}); |
| 352 |
$(document).on('change', '.merchant-choices-label_image_shape input', function () { |
| 353 |
var $layout = $(this).closest('.layout'); |
| 354 |
var shape = $layout.find('.merchant-choices-label_image_shape input:checked').val(); |
| 355 |
updateStyles('image', shape, $(this)); |
| 356 |
}); |
| 357 |
$(document).on('save.merchant', function (e, module) { |
| 358 |
if (module === 'product-labels') { |
| 359 |
$('.merchant-choices-label_text_shape input').each(function () { |
| 360 |
$(this).removeAttr('data-previous'); |
| 361 |
}); |
| 362 |
} |
| 363 |
}); |
| 364 |
|
| 365 |
// Page load |
| 366 |
handleShortcodeChange($('.merchant-field-use_shortcode input').is(':checked')); |
| 367 |
|
| 368 |
// Enable/Disable Shortcode |
| 369 |
$(document).on('change', '.merchant-field-use_shortcode input', function () { |
| 370 |
handleShortcodeChange($(this).is(':checked')); |
| 371 |
}); |
| 372 |
|
| 373 |
// Enable/Disable shapes based on shortcode enabled/disabled |
| 374 |
function handleShortcodeChange(isEnabled) { |
| 375 |
var dataPrev = 'data-previous'; |
| 376 |
$('.merchant-flexible-content-control.product-labels-style .layout .merchant-choices-label_text_shape').each(function () { |
| 377 |
var $inputs = $(this).find('input'); |
| 378 |
$inputs.each(function () { |
| 379 |
var _shapesDefaultStyles$; |
| 380 |
var $input = $(this); |
| 381 |
var value = $input.val(); |
| 382 |
var isDisabledInShortCode = (_shapesDefaultStyles$ = shapesDefaultStyles.text[value]) === null || _shapesDefaultStyles$ === void 0 ? void 0 : _shapesDefaultStyles$.disableInShortCode; |
| 383 |
if (isEnabled) { |
| 384 |
if (isDisabledInShortCode) { |
| 385 |
if ($input.is(':checked')) { |
| 386 |
// Store previous value and change the selection to default shape |
| 387 |
$input.attr(dataPrev, value).prop('checked', false); |
| 388 |
$inputs.filter('input[value="text-shape-1"]').prop('checked', true); |
| 389 |
updateStyles('text', 'text-shape-1', $inputs.filter('input[value="text-shape-1"]')); |
| 390 |
} |
| 391 |
|
| 392 |
// Disable |
| 393 |
$input.attr('disabled', true); |
| 394 |
} |
| 395 |
} else { |
| 396 |
if ($input.attr(dataPrev)) { |
| 397 |
// Revert to previously selected shape if shortcode disabled without saving. |
| 398 |
$inputs.filter('input[value="text-shape-1"]').prop('checked', false); |
| 399 |
$inputs.filter("input[value=\"".concat(value, "\"]")).prop('checked', true); |
| 400 |
$input.removeAttr(dataPrev); |
| 401 |
updateStyles('text', value, $inputs.filter("input[value=\"".concat(value, "\"]"))); |
| 402 |
} |
| 403 |
|
| 404 |
// Enable |
| 405 |
$input.attr('disabled', false); |
| 406 |
} |
| 407 |
}); |
| 408 |
}); |
| 409 |
} |
| 410 |
var updateStyles = function updateStyles(shapeType, selectedShape, $input) { |
| 411 |
var $layout = $input.closest('.layout'); |
| 412 |
var properties = { |
| 413 |
'merchant-field-label_width': 'width', |
| 414 |
'merchant-field-label_height': 'height', |
| 415 |
'merchant-field-shape_radius': 'borderRadius', |
| 416 |
'merchant-field-margin_x': 'marginX', |
| 417 |
'merchant-field-margin_y': 'marginY' |
| 418 |
}; |
| 419 |
var labelType = $layout.find('.merchant-field-label_type input:checked').val(); |
| 420 |
|
| 421 |
// If custom image uploaded, don't change style when trying to select predefined images |
| 422 |
if (labelType === 'image') { |
| 423 |
var customImage = $layout.find('.merchant-field-label_image_shape_custom input[type="hidden"]').val(); |
| 424 |
if (customImage && $input.closest('.merchant-choices-label_image_shape').length) { |
| 425 |
return; |
| 426 |
} |
| 427 |
} |
| 428 |
for (var _i = 0, _Object$entries = Object.entries(properties); _i < _Object$entries.length; _i++) { |
| 429 |
var _shapesDefaultStyles$2; |
| 430 |
var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2), |
| 431 |
inputWrapper = _Object$entries$_i[0], |
| 432 |
propertyName = _Object$entries$_i[1]; |
| 433 |
var value = (_shapesDefaultStyles$2 = shapesDefaultStyles[shapeType][selectedShape]) === null || _shapesDefaultStyles$2 === void 0 ? void 0 : _shapesDefaultStyles$2[propertyName]; |
| 434 |
if (value || value === 0) { |
| 435 |
$layout.find(".".concat(inputWrapper, " input")).val(value).attr('value', value).trigger('input').trigger('change'); |
| 436 |
} |
| 437 |
} |
| 438 |
}; |
| 439 |
})(jQuery); |