PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 1.6
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v1.6
2.3.2 2.3.1 2.3.0 2.2.8 2.2.7 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.11.0 1.11.1 1.11.2 1.6 1.7 1.8 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 1.9.10 1.9.11 All 60 releases
merchant / assets / js / admin / admin.js

admin.js in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 1.6, at assets/js/admin/admin.js

817 lines 32.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 "use strict";
2
3 ;
4 (function ($, window, document, undefined) {
5 'use strict';
6
7 var merchant = merchant || {};
8 $(document).ready(function () {
9 $('.merchant-module-page-setting-field').each(function () {
10 var $field = $(this);
11 if ($field.data('condition') && $field.data('condition').length) {
12 var condition = $field.data('condition');
13 var $target = $('input[name="merchant[' + condition[0] + ']"],select[name="merchant[' + condition[0] + ']"]');
14 if ($target.length) {
15 $target.on('change merchant.change', function (e) {
16 var $element = $(this);
17 var passed = false;
18 switch (condition[1]) {
19 case '==':
20 if ($element.attr('type') === 'radio' || $element.attr('type') === 'checkbox') {
21 if ($element.is(':checked') && $element.val() == condition[2]) {
22 passed = true;
23 }
24 }
25 if ($element.is('select') && $element.val() == condition[2]) {
26 passed = true;
27 }
28 break;
29 case 'any':
30 if ($element.attr('type') === 'radio' || $element.attr('type') === 'checkbox') {
31 if ($element.is(':checked') && condition[2].split('|').includes($element.val())) {
32 passed = true;
33 }
34 }
35 if ($element.is('select') && condition[2].split('|').includes($element.val())) {
36 passed = true;
37 }
38 break;
39 }
40 if (passed) {
41 $field.removeClass('merchant-hide').addClass('merchant-show');
42 } else {
43 $field.removeClass('merchant-show').addClass('merchant-hide');
44 }
45 }).trigger('merchant.change');
46 }
47 }
48 });
49
50 // AjaxSave
51 var $ajaxForm = $('.merchant-module-page-ajax-form');
52 var $ajaxHeader = $('.merchant-module-page-ajax-header');
53 var $ajaxSaveBtn = $('.merchant-module-save-button');
54 $('.merchant-module-page-content').on('change keypress change.merchant', ':input:not(.merchant-module-question-answer-textarea):not(.merchant-license-code-input)', function () {
55 if (!merchant.show_save) {
56 $ajaxHeader.addClass('merchant-show');
57 $ajaxHeader.removeClass('merchant-saving');
58 merchant.show_save = true;
59 }
60 });
61 $ajaxForm.ajaxForm({
62 beforeSubmit: function beforeSubmit() {
63 $ajaxHeader.addClass('merchant-saving');
64 },
65 success: function success() {
66 $ajaxHeader.removeClass('merchant-show');
67 merchant.show_save = false;
68
69 // Module Alert after Ajax Save
70 if (!$('.merchant-module-action').hasClass('merchant-enabled')) {
71 var $moduleAlert = $('.merchant-module-alert');
72 $moduleAlert.addClass('merchant-show');
73 $(document).off('click.merchant-alert-close');
74 $(document).on('click.merchant-alert-close', function (e) {
75 if (!$(e.target).closest('.merchant-module-alert-wrapper').length) {
76 $moduleAlert.removeClass('merchant-show');
77 $(document).off('click.merchant-alert-close');
78 }
79 });
80 }
81 }
82 });
83 $('.merchant-module-question-answer-button').on('click', function (e) {
84 e.preventDefault();
85 var $button = $(this);
86 var $textarea = $('.merchant-module-question-answer-textarea');
87 $('.merchant-module-question-answer-dropdown').removeClass('merchant-show');
88 $('.merchant-module-question-thank-you-dropdown').addClass('merchant-show');
89 window.wp.ajax.post('merchant_module_feedback', {
90 subject: $textarea.data('subject'),
91 message: $textarea.val(),
92 module: $button.closest('.merchant-module-action').find('.merchant-module-page-button-action-activate').data('module'),
93 nonce: window.merchant.nonce
94 });
95 });
96 $('.merchant-module-page-button-action-activate').on('click', function (e) {
97 e.preventDefault();
98 $('.merchant-module-question-list-dropdown').removeClass('merchant-show');
99 $('.merchant-module-question-answer-dropdown').removeClass('merchant-show');
100 $('.merchant-module-question-answer-form').removeClass('merchant-show');
101 $('.merchant-module-question-answer-title').removeClass('merchant-show');
102 $('.merchant-module-question-thank-you-dropdown').removeClass('merchant-show');
103 $('.merchant-module-question-answer-textarea').val('');
104 window.wp.ajax.post('merchant_module_activate', {
105 module: $(this).data('module'),
106 nonce: window.merchant.nonce
107 }).done(function () {
108 $('body').removeClass('merchant-module-disabled').addClass('merchant-module-enabled');
109 $('.merchant-module-action').addClass('merchant-enabled');
110 });
111 });
112 $('.merchant-module-page-button-action-deactivate').on('click', function (e) {
113 e.preventDefault();
114 window.wp.ajax.post('merchant_module_deactivate', {
115 module: $(this).data('module'),
116 nonce: window.merchant.nonce
117 }).done(function () {
118 $('body').removeClass('merchant-module-enabled').addClass('merchant-module-disabled');
119 $('.merchant-module-action').removeClass('merchant-enabled');
120 $('.merchant-module-question-list-dropdown').addClass('merchant-show');
121 });
122 });
123 $('.merchant-module-question-list-dropdown li').on('click', function (e) {
124 var $question = $(this);
125 var target = $question.data('answer-target');
126 var $answer = $('[data-answer-title="' + target + '"]');
127 if ($answer.length) {
128 $answer.addClass('merchant-show').siblings().removeClass('merchant-show');
129 $('.merchant-module-question-answer-dropdown').addClass('merchant-show');
130 $('.merchant-module-question-answer-textarea').attr('data-subject', $question.text().trim());
131 } else {
132 $('.merchant-module-question-thank-you-dropdown').addClass('merchant-show');
133 $('.merchant-module-question-answer-dropdown').removeClass('merchant-show');
134 }
135 $('.merchant-module-question-answer-textarea').val('');
136 $('.merchant-module-question-list-dropdown').removeClass('merchant-show');
137 });
138 $('.merchant-module-dropdown-close').on('click', function (e) {
139 e.preventDefault();
140 $(this).closest('.merchant-module-dropdown').removeClass('merchant-show');
141 });
142 $('.merchant-module-page-button-deactivate').on('click', function (e) {
143 e.preventDefault();
144 var $button = $(this);
145 var $dropdown = $('.merchant-module-deactivate-dropdown');
146 $dropdown.toggleClass('merchant-show');
147 $(document).off('click.merchant-close');
148 $(document).on('click.merchant-close', function (e) {
149 if (!$(e.target).closest('.merchant-module-deactivate').length) {
150 $dropdown.removeClass('merchant-show');
151 $(document).off('click.merchant-close');
152 }
153 });
154 });
155 $('.merchant-range').each(function () {
156 var $range = $(this);
157 var $rangeInput = $range.find('.merchant-range-input');
158 var $numberInput = $range.find('.merchant-range-number-input');
159 $rangeInput.on('change input merchant.range merchant-init.range', function (e) {
160 var $range = $(this);
161 var value = (e.type === 'merchant' ? $numberInput.val() : $range.val()) || 0;
162 var min = $range.attr('min') || 0;
163 var max = $range.attr('max') || 1;
164 var percentage = (value - min) / (max - min) * 100;
165 $range.css({
166 'background': 'linear-gradient(to right, #3858E9 0%, #3858E9 ' + percentage + '%, #ddd ' + percentage + '%, #ddd 100%)'
167 });
168 $rangeInput.val(value);
169 $numberInput.val(value);
170 }).trigger('merchant-init.range');
171 $numberInput.on('change input blur', function () {
172 if ($rangeInput.hasClass('merchant-range-input')) {
173 $rangeInput.val($(this).val()).trigger('merchant.range');
174 }
175 });
176 });
177 $('.merchant-color').each(function () {
178 var $color = $(this);
179 var $picker = $color.find('.merchant-color-picker');
180 var $input = $color.find('.merchant-color-input');
181 var inited;
182 var pickr;
183 $picker.on('click', function () {
184 var $bodyHTML = $('body,html');
185 $bodyHTML.addClass('merchant-height-auto');
186 if (!inited) {
187 pickr = new Pickr({
188 el: $picker.get(0),
189 container: 'body',
190 theme: 'merchant',
191 appClass: 'merchant-pcr-app',
192 default: $input.val() || '#212121',
193 swatches: ['#000000', '#F44336', '#E91E63', '#673AB7', '#03A9F4', '#8BC34A', '#FFEB3B', '#FFC107', '#FFFFFF'],
194 sliders: 'h',
195 useAsButton: true,
196 components: {
197 hue: true,
198 preview: true,
199 opacity: true,
200 interaction: {
201 input: true,
202 clear: true
203 }
204 },
205 i18n: {
206 'btn:clear': 'Default'
207 }
208 });
209 pickr.on('change', function (color) {
210 var colorCode;
211 if (color.a === 1) {
212 pickr.setColorRepresentation('HEX');
213 colorCode = color.toHEXA().toString(0);
214 } else {
215 pickr.setColorRepresentation('RGBA');
216 colorCode = color.toRGBA().toString(0);
217 }
218 $picker.css({
219 'background-color': colorCode
220 });
221 if ($input.val() !== colorCode) {
222 $input.val(colorCode).trigger('change.merchant');
223 }
224 });
225 pickr.on('clear', function () {
226 var defaultColor = $picker.data('default-color');
227 if (defaultColor) {
228 pickr.setColor(defaultColor);
229 } else {
230 $picker.css({
231 'background-color': 'white'
232 });
233 $input.val('');
234 }
235 });
236 pickr.on('hide', function () {
237 $bodyHTML.removeClass('merchant-height-auto');
238 });
239 $picker.data('pickr', pickr);
240 setTimeout(function () {
241 pickr.show();
242 });
243 inited = true;
244 } else {
245 pickr.setColor($input.val());
246 }
247 });
248 $input.on('change keyup', function () {
249 var colorCode = $(this).val();
250 $picker.css({
251 'background-color': colorCode
252 });
253 });
254 });
255
256 // Sortable.
257 var SortableField = {
258 init: function init(field) {
259 this.events();
260 },
261 events: function events() {
262 var self = this;
263 $('.merchant-sortable').each(function () {
264 var field = $(this),
265 input = field.find('.merchant-sortable-input');
266
267 // Init sortable.
268 $(field.find('ul.merchant-sortable-list').first()).sortable({
269 // Update value when we stop sorting.
270 update: function update() {
271 input.val(self.sortableGetNewVal(field)).trigger('change.merchant');
272 }
273 }).disableSelection().find('li').each(function () {
274 // Enable/disable options when we click on the eye of Thundera.
275 $(this).find('i.visibility').click(function () {
276 $(this).toggleClass('dashicons-visibility-faint').parents('li:eq(0)').toggleClass('invisible');
277 });
278 }).click(function () {
279 // Update value when click in the eye.
280 if ($(event.target).hasClass('dashicons-visibility')) {
281 input.val(self.sortableGetNewVal(field)).trigger('change.merchant');
282 }
283 });
284 });
285 },
286 sortableGetNewVal: function sortableGetNewVal(field) {
287 var items = $(field.find('li'));
288 var newVal = [];
289 _.each(items, function (item) {
290 if (!$(item).hasClass('invisible')) {
291 newVal.push($(item).data('value'));
292 }
293 });
294 return JSON.stringify(newVal);
295 }
296 };
297
298 // Intialize Sortable.
299 SortableField.init();
300
301 // Sortable Repeater.
302 var SortableRepeaterField = {
303 init: function init(field) {
304 var self = this;
305
306 // Update the values for all our input fields and initialise the sortable repeater.
307 $('.merchant-sortable-repeater-control').each(function () {
308 // If there is an existing customizer value, populate our rows
309 var defaultValuesArray = JSON.parse($(this).find('.merchant-sortable-repeater-input').val());
310 var numRepeaterItems = defaultValuesArray.length;
311 if (numRepeaterItems > 0) {
312 // Add the first item to our existing input field
313 $(this).find('.repeater-input').val(defaultValuesArray[0]);
314
315 // Create a new row for each new value
316 if (numRepeaterItems > 1) {
317 var i;
318 for (i = 1; i < numRepeaterItems; ++i) {
319 self.appendRow($(this), defaultValuesArray[i]);
320 }
321 }
322 }
323
324 // Make our Repeater fields sortable.
325 if (!$(this).hasClass('disable-sorting')) {
326 $(this).find('.merchant-sortable-repeater.sortable').sortable({
327 update: function update(event, ui) {
328 self.getAllInputs($(this).parent());
329 }
330 });
331 }
332 });
333
334 // Events.
335 this.events();
336 },
337 events: function events() {
338 var self = this;
339
340 // Remove item starting from it's parent element
341 $('.merchant-sortable-repeater.sortable').on('click', '.customize-control-sortable-repeater-delete', function (event) {
342 event.preventDefault();
343 var numItems = $(this).parent().parent().find('.repeater').length;
344 if (numItems > 1) {
345 $(this).parent().slideUp('fast', function () {
346 var parentContainer = $(this).parent().parent();
347 $(this).remove();
348 self.getAllInputs(parentContainer);
349 });
350 } else {
351 // $(this).parent().find('.repeater-input').val('');
352 self.getAllInputs($(this).parent().parent().parent());
353 }
354 });
355
356 // Add new item
357 $('.customize-control-sortable-repeater-add').click(function (event) {
358 event.preventDefault();
359 self.appendRow($(this).parent());
360 self.getAllInputs($(this).parent());
361 });
362
363 // Refresh our hidden field if any fields change
364 $('.merchant-sortable-repeater.sortable').change(function () {
365 self.getAllInputs($(this).parent());
366 });
367 $('.merchant-sortable-repeater.sortable').on('focusout', '.repeater-input', function () {
368 self.getAllInputs($(this).parent());
369 });
370 },
371 /**
372 * Append a new row to our list of elements.
373 *
374 */
375 appendRow: function appendRow($element) {
376 var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
377 var newRow = '<div class="repeater" style="display:none"><input type="text" value="' + defaultValue + '" class="repeater-input" /><span class="dashicons dashicons-menu"></span><a class="customize-control-sortable-repeater-delete" href="#"><span class="dashicons dashicons-no-alt"></span></a></div>';
378 $element.find('.sortable').append(newRow);
379 $element.find('.sortable').find('.repeater:last').slideDown('slow', function () {
380 $(this).find('input').focus();
381 });
382 },
383 /**
384 * Get the values from the repeater input fields and add to our hidden field.
385 *
386 */
387 getAllInputs: function getAllInputs($element) {
388 var inputValues = $element.find('.repeater-input').map(function () {
389 return $(this).val();
390 }).toArray();
391
392 // Add all the values from our repeater fields to the hidden field (which is the one that actually gets saved)
393 $element.find('.merchant-sortable-repeater-input').val(JSON.stringify(inputValues));
394 // Important! Make sure to trigger change event so Customizer knows it has to save the field
395 $element.find('.merchant-sortable-repeater-input').trigger('change');
396 }
397 };
398
399 // Initialize Sortable Repeater.
400 SortableRepeaterField.init();
401
402 // Flexible Content.
403 var FlexibleContentField = {
404 init: function init(field) {
405 var self = this;
406
407 // Update the values for all our input fields and initialise the sortable repeater.
408 $('.merchant-flexible-content-control').each(function () {
409 var $content = $(this).find('.merchant-flexible-content');
410 $content.sortable({
411 axis: 'y',
412 cursor: 'move',
413 helper: 'original',
414 handle: '.customize-control-flexible-content-move',
415 stop: function stop(event, ui) {
416 $content.trigger('merchant.sorted');
417 self.refreshNumbers($content);
418 }
419 });
420 });
421
422 // Events.
423 this.events();
424 },
425 events: function events() {
426 var self = this;
427 $('.customize-control-flexible-content-add-button').click(function (event) {
428 event.preventDefault();
429 event.stopImmediatePropagation();
430 $(this).parent().find('.customize-control-flexible-content-add-list').toggleClass('active');
431 });
432 $('.customize-control-flexible-content-add').click(function (event) {
433 event.preventDefault();
434 event.stopImmediatePropagation();
435 var $field = $('.merchant-flexible-content-control[data-id=' + $(this).data('id') + ']');
436 var $layouts = $field.find('.layouts');
437 var $selected = $(this).data('layout');
438 var $layout = $layouts.find('.layout[data-type=' + $selected + ']').clone(true);
439 var $content = $field.find('.merchant-flexible-content');
440 var $items = $content.find('.layout');
441 $layout.find('input, select').each(function () {
442 if ($(this).data('name')) {
443 $(this).attr('name', $(this).data('name').replace('0', $items.length));
444 }
445 });
446 $layout.find('.layout-count').text($items.length + 1);
447 $content.append($layout);
448 $content.removeClass('empty');
449 $(this).parent().removeClass('active');
450 if ($layout.find('.merchant-module-page-setting-field-upload').length) {
451 initUploadField($layout.find('.merchant-module-page-setting-field-upload'));
452 }
453 if ($layout.find('.merchant-module-page-setting-field-select_ajax').length) {
454 initSelectAjax($layout.find('.merchant-module-page-setting-field-select_ajax'));
455 }
456 });
457 $('.customize-control-flexible-content-delete').click(function (event) {
458 event.preventDefault();
459 var $item = $(this).closest('.layout');
460 var $content = $item.parent();
461 $item.remove();
462 if ($content.find('.layout').length === 0) {
463 $content.addClass('empty');
464 }
465 self.refreshNumbers($content);
466 });
467 },
468 refreshNumbers: function refreshNumbers($content) {
469 $content.find('.layout').each(function (index) {
470 var $count = $(this).find('.layout-count').text();
471 var $inputIndex = parseInt($count) - 1;
472 $(this).find('.layout-count').text(index + 1);
473 $(this).find('input, select').each(function () {
474 if ($(this).attr('name')) {
475 $(this).attr('name', $(this).attr('name').replace('[' + $inputIndex + ']', '[*refreshed*' + index + ']'));
476 }
477 });
478 });
479 $content.find('.layout').each(function (index) {
480 // We've added *refreshed* tp the attribute name in the prior loop as refreshing the numbers in the attribute can cause
481 // checked boxes to be unchecked due to similar attribute names during the change while sorting, within this loop we remove them
482 $(this).find('input, select').each(function () {
483 $(this).attr('name', $(this).attr('name').replace('*refreshed*', ''));
484 });
485 });
486 $content.parent().find('input').trigger('change.merchant');
487 }
488 };
489
490 // Initialize Flexible Content.
491 FlexibleContentField.init();
492
493 // Create Page Control.
494 var CreatePageControl = {
495 init: function init() {
496 this.events();
497 },
498 events: function events() {
499 var self = this;
500 $(document).on('click', '.merchant-create-page-control-button', function (e) {
501 e.preventDefault();
502 var $this = $(this),
503 $create_message = $this.parent().find('.merchant-create-page-control-create-message'),
504 $success_message = $this.parent().find('.merchant-create-page-control-success-message'),
505 initial_text = $this.text(),
506 creating_text = $this.data('creating-text'),
507 created_text = $this.data('created-text'),
508 page_title = $this.data('page-title'),
509 page_meta_key = $this.data('page-meta-key'),
510 page_meta_value = $this.data('page-meta-value'),
511 option_name = $this.data('option-name'),
512 nonce = $this.data('nonce');
513 if (!page_title) {
514 return false;
515 }
516 $(this).text(creating_text);
517 $(this).attr('disabled', true);
518 $.ajax({
519 type: 'post',
520 url: ajaxurl,
521 data: {
522 action: 'merchant_create_page_control',
523 page_title: page_title,
524 page_meta_key: page_meta_key,
525 page_meta_value: page_meta_value,
526 option_name: option_name,
527 nonce: nonce
528 },
529 success: function success(response) {
530 self.ajaxResponseHandler(response, $this, $success_message, $create_message);
531 }
532 });
533 });
534 },
535 ajaxResponseHandler: function ajaxResponseHandler(response, $this, $success_message, $create_message) {
536 if ('success' === response.status) {
537 var href = $success_message.find('a').attr('href'),
538 newhref = href.replace('?post=&', '?post=' + response.page_id + '&');
539 $success_message.find('a').attr('href', newhref);
540 $success_message.css('display', 'block');
541 $create_message.remove();
542 $this.remove();
543 }
544 }
545 };
546
547 // Initialize Create Page Control.
548 CreatePageControl.init();
549 $('.merchant-module-page-setting-field-gallery').each(function () {
550 var $this = $(this);
551 var $button = $this.find('.merchant-gallery-button');
552 var $input = $this.find('.merchant-gallery-input');
553 var $images = $this.find('.merchant-gallery-images');
554 var $remove = $this.find('.merchant-gallery-remove');
555 var wpMediaFrame;
556 var sortable = $images.sortable({
557 helper: 'original',
558 update: function update(event, ui) {
559 var selectedIds = [];
560 $images.find('.merchant-gallery-image').each(function () {
561 selectedIds.push($(this).data('item-id'));
562 });
563 $input.val(selectedIds.join(',')).trigger('change');
564 }
565 });
566 $remove.on('click', function (e) {
567 e.preventDefault();
568 $(this).parent().remove();
569 var selectedIds = [];
570 $images.find('.merchant-gallery-image').each(function () {
571 selectedIds.push($(this).data('item-id'));
572 });
573 $input.val(selectedIds.join(',')).trigger('change');
574 });
575 $button.on('click', function (e) {
576 var $btn = $(this);
577 var ids = $input.val();
578 var mode = ids ? 'edit' : 'add';
579 e.preventDefault();
580 if (typeof window.wp === 'undefined' || !window.wp.media || !window.wp.media.gallery) {
581 return;
582 }
583 if (mode === 'add') {
584 wpMediaFrame = window.wp.media({
585 library: {
586 type: 'image'
587 },
588 frame: 'post',
589 state: 'gallery',
590 multiple: true
591 });
592 wpMediaFrame.open();
593 } else {
594 wpMediaFrame = window.wp.media.gallery.edit('[gallery ids="' + ids + '"]');
595 }
596 wpMediaFrame.on('update', function (selection) {
597 $images.empty();
598 var selectedIds = selection.models.map(function (attachment) {
599 var item = attachment.toJSON();
600 var thumb = item.sizes && item.sizes.thumbnail && item.sizes.thumbnail.url ? item.sizes.thumbnail.url : item.url;
601 $images.append('<div class="merchant-gallery-image" data-item-id="' + item.id + '"><i class="merchant-gallery-remove dashicons dashicons-no-alt"></i><img src="' + thumb + '" /></div>');
602 return item.id;
603 });
604 $input.val(selectedIds.join(',')).trigger('change');
605 $this.find('.merchant-gallery-remove').on('click', function (e) {
606 e.preventDefault();
607 $(this).parent().remove();
608 var selectedIds = [];
609 $images.find('.merchant-gallery-image').each(function () {
610 selectedIds.push($(this).data('item-id'));
611 });
612 $input.val(selectedIds.join(',')).trigger('change');
613 });
614 });
615 });
616 });
617 var initUploadField = function initUploadField(element) {
618 var $this = element;
619 var $button = $this.find('.merchant-upload-button');
620 var $input = $this.find('.merchant-upload-input');
621 var $wrapper = $this.find('.merchant-upload-wrapper');
622 var $remove = $this.find('.merchant-upload-remove');
623 var wpMediaFrame;
624 $remove.on('click', function (e) {
625 e.preventDefault();
626 $(this).parent().remove();
627 $input.val('').trigger('change');
628 });
629 $button.on('click', function (e) {
630 e.preventDefault();
631 if (typeof window.wp === 'undefined' || !window.wp.media) {
632 return;
633 }
634 if (!wpMediaFrame) {
635 wpMediaFrame = window.wp.media({
636 library: {
637 type: 'image'
638 }
639 });
640 }
641 wpMediaFrame.open();
642 wpMediaFrame.on('select', function () {
643 $wrapper.empty();
644 var item = wpMediaFrame.state().get('selection').first().attributes;
645 var thumb = item.sizes && item.sizes.thumbnail && item.sizes.thumbnail.url ? item.sizes.thumbnail.url : item.url;
646 $wrapper.append('<div class="merchant-upload-image"><i class="merchant-upload-remove dashicons dashicons-no-alt"></i><img src="' + thumb + '" /></div>');
647 $input.val(item.id).trigger('change');
648 $this.find('.merchant-upload-remove').on('click', function (e) {
649 e.preventDefault();
650 $(this).parent().remove();
651 $input.val('').trigger('change');
652 });
653 });
654 });
655 };
656 $('.merchant-module-page-setting-field-upload:not(.template)').each(function () {
657 initUploadField($(this));
658 });
659 var initSelectAjax = function initSelectAjax($selectAjax) {
660 var $select = $selectAjax.find('select');
661 var $source = $select.data('source');
662 var $config = window.merchant_admin_options;
663 var $object = {
664 width: '100%'
665 };
666 if ($source === 'post' || $source === 'product') {
667 $object.minimumInputLength = 1;
668 $object.ajax = {
669 url: $config.ajaxurl,
670 dataType: 'json',
671 delay: 250,
672 cache: true,
673 data: function data(params) {
674 return {
675 action: 'merchant_admin_options_select_ajax',
676 nonce: $config.ajaxnonce,
677 term: params.term,
678 source: $source
679 };
680 },
681 processResults: function processResults(response, params) {
682 if (response.success) {
683 return {
684 results: response.data
685 };
686 }
687 return {};
688 }
689 };
690 }
691 $select.select2($object);
692 $selectAjax.find('.select2-selection--multiple').append('<span class="merchant-select2-clear"></span>');
693 };
694 $('.merchant-module-page-setting-field-select_ajax:not(.template)').each(function () {
695 initSelectAjax($(this));
696 });
697 $('.merchant-module-page-settings-responsive').each(function () {
698 var $this = $(this);
699 var $button = $this.find('.merchant-module-page-settings-devices button');
700 var $container = $this.find('.merchant-module-page-settings-device-container');
701 $button.on('click', function (e) {
702 e.preventDefault();
703 var $device = $(this).data('device');
704 $button.removeClass('active');
705 $container.removeClass('active');
706 $(this).addClass('active');
707 $container.each(function () {
708 if ($(this).data('device') === $device) {
709 $(this).addClass('active');
710 }
711 });
712 });
713 });
714 $('.merchant-animated-buttons').each(function () {
715 var $button = $(this).find('label');
716 var $demo = $('.merchant-animation-demo');
717 var animation;
718 var animationHover;
719 $button.on('click', function () {
720 $demo.removeClass('merchant-animation-' + animation);
721 $demo.removeClass('merchant-animation-' + animationHover);
722 animation = $(this).find('input').attr('value');
723 setTimeout(function () {
724 $demo.addClass('merchant-animation-' + animation);
725 }, 100);
726 setTimeout(function () {
727 $demo.removeClass('merchant-animation-' + animation);
728 }, 1000);
729 });
730 $button.mouseover(function () {
731 $demo.removeClass('merchant-animation-' + animation);
732 animationHover = $(this).find('input').attr('value');
733 $demo.addClass('merchant-animation-' + animationHover);
734 }).mouseout(function () {
735 $demo.removeClass('merchant-animation-' + animationHover);
736 });
737 });
738
739 // Notifications Sidebar
740 var $notificationsSidebar = $('.merchant-notifications-sidebar');
741 if ($notificationsSidebar.length) {
742 var $notifications = $('.merchant-notifications');
743 $notifications.on('click', function (e) {
744 e.preventDefault();
745 var $notification = $(this);
746 var latestNotificationDate = $notificationsSidebar.find('.merchant-notification:first-child .merchant-notification-date').data('raw-date');
747 $notificationsSidebar.toggleClass('opened');
748 if (!$notification.hasClass('read')) {
749 $.post(window.merchant.ajax_url, {
750 action: 'merchant_notifications_read',
751 nonce: window.merchant.nonce,
752 latest_notification_date: latestNotificationDate
753 }, function (response) {
754 if (response.success) {
755 setTimeout(function () {
756 $notification.addClass('read');
757 }, 2000);
758 }
759 });
760 }
761 });
762 $(window).on('scroll', function () {
763 if (window.pageYOffset > 60) {
764 $notificationsSidebar.addClass('closing');
765 setTimeout(function () {
766 $notificationsSidebar.removeClass('opened');
767 $notificationsSidebar.removeClass('closing');
768 }, 300);
769 }
770 });
771
772 // Close Sidebar
773 $('.merchant-notifications-sidebar-close').on('click', function (e) {
774 e.preventDefault();
775 $notificationsSidebar.addClass('closing');
776 setTimeout(function () {
777 $notificationsSidebar.removeClass('opened');
778 $notificationsSidebar.removeClass('closing');
779 }, 300);
780 });
781 }
782
783 // Tabs Navigation.
784 var tabs = $('.merchant-tabs-nav');
785 if (tabs.length) {
786 tabs.each(function () {
787 var tabWrapperId = $(this).data('tab-wrapper-id');
788 $(this).find('.merchant-tabs-nav-link').on('click', function (e) {
789 e.preventDefault();
790 var tabsNavLink = $(this).closest('.merchant-tabs-nav').find('.merchant-tabs-nav-link'),
791 to = $(this).data('tab-to');
792
793 // Tab Nav Item
794 tabsNavLink.each(function () {
795 $(this).closest('.merchant-tabs-nav-item').removeClass('active');
796 });
797 $(this).closest('.merchant-tabs-nav-item').addClass('active');
798
799 // Tab Content
800 var tabContentWrapper = $('.merchant-tab-content-wrapper[data-tab-wrapper-id="' + tabWrapperId + '"]');
801 tabContentWrapper.find('> .merchant-tab-content').removeClass('active');
802 tabContentWrapper.find('> .merchant-tab-content[data-tab-content-id="' + to + '"]').addClass('active');
803 });
804 });
805 }
806
807 // Module Alert
808 var $moduleAlert = $('.merchant-module-alert');
809 if ($moduleAlert.length) {
810 $moduleAlert.find('.merchant-module-alert-close').on('click', function (e) {
811 e.preventDefault();
812 $moduleAlert.removeClass('merchant-show');
813 $(document).off('click.merchant-alert-close');
814 });
815 }
816 });
817 })(jQuery, window, document);