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

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