PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 1.7
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v1.7
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 1.9.12 All 59 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.7, at assets/js/admin/admin.js

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