PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 4.0.4
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v4.0.4
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 2.3.5 2.3.6 2.4.0 2.4.1 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 2.4.15 2.4.16 2.4.17 2.4.18 2.4.19 2.4.2 2.4.20 2.4.21 All 88 releases
post-carousel / admin / views / sp-framework / assets / js / spf.js

spf.js in Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News 4.0.4, at admin/views/sp-framework/assets/js/spf.js

2,234 lines 56.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 *
3 * -----------------------------------------------------------
4 *
5 * SPF Framework
6 *
7 * -----------------------------------------------------------
8 *
9 */
10 ; (function ($, window, document, undefined) {
11 'use strict'
12
13 //
14 // Constants
15 //
16 var SP_PC = SP_PC || {}
17
18 SP_PC.funcs = {}
19
20 SP_PC.vars = {
21 onloaded: false,
22 $body: $('body'),
23 $window: $(window),
24 $document: $(document),
25 is_rtl: $('body').hasClass('rtl'),
26 code_themes: []
27 }
28
29 //
30 // Helper Functions
31 //
32 SP_PC.helper = {
33 //
34 // Generate UID
35 //
36 uid: function (prefix) {
37 return (
38 (prefix || '') +
39 Math.random()
40 .toString(36)
41 .substr(2, 9)
42 )
43 },
44
45 // Quote regular expression characters
46 //
47 preg_quote: function (str) {
48 return (str + '').replace(/(\[|\-|\])/g, '\\$1')
49 },
50
51 //
52 // Rename input names
53 //
54 name_nested_replace: function ($selector, field_id) {
55 var checks = []
56 var regex = new RegExp(
57 '(' + SP_PC.helper.preg_quote(field_id) + ')\\[(\\d+)\\]',
58 'g'
59 )
60
61 $selector.find(':radio').each(function () {
62 if (this.checked || this.orginal_checked) {
63 this.orginal_checked = true
64 }
65 })
66
67 $selector.each(function (index) {
68 $(this)
69 .find(':input')
70 .each(function () {
71 this.name = this.name.replace(regex, field_id + '[' + index + ']')
72 if (this.orginal_checked) {
73 this.checked = true
74 }
75 })
76 })
77 },
78
79 //
80 // Debounce
81 //
82 debounce: function (callback, threshold, immediate) {
83 var timeout
84 return function () {
85 var context = this,
86 args = arguments
87 var later = function () {
88 timeout = null
89 if (!immediate) {
90 callback.apply(context, args)
91 }
92 }
93 var callNow = immediate && !timeout
94 clearTimeout(timeout)
95 timeout = setTimeout(later, threshold)
96 if (callNow) {
97 callback.apply(context, args)
98 }
99 }
100 },
101
102 //
103 // Get a cookie
104 //
105 get_cookie: function (name) {
106 var e,
107 b,
108 cookie = document.cookie,
109 p = name + '='
110
111 if (!cookie) {
112 return
113 }
114
115 b = cookie.indexOf('; ' + p)
116
117 if (b === -1) {
118 b = cookie.indexOf(p)
119
120 if (b !== 0) {
121 return null
122 }
123 } else {
124 b += 2
125 }
126
127 e = cookie.indexOf(';', b)
128
129 if (e === -1) {
130 e = cookie.length
131 }
132
133 return decodeURIComponent(cookie.substring(b + p.length, e))
134 },
135
136 //
137 // Set a cookie
138 //
139 set_cookie: function (name, value, expires, path, domain, secure) {
140 var d = new Date()
141
142 if (typeof expires === 'object' && expires.toGMTString) {
143 expires = expires.toGMTString()
144 } else if (parseInt(expires, 10)) {
145 d.setTime(d.getTime() + parseInt(expires, 10) * 1000)
146 expires = d.toGMTString()
147 } else {
148 expires = ''
149 }
150
151 document.cookie =
152 name +
153 '=' +
154 encodeURIComponent(value) +
155 (expires ? '; expires=' + expires : '') +
156 (path ? '; path=' + path : '') +
157 (domain ? '; domain=' + domain : '') +
158 (secure ? '; secure' : '')
159 },
160
161 //
162 // Remove a cookie
163 //
164 remove_cookie: function (name, path, domain, secure) {
165 SP_PC.helper.set_cookie(name, '', -1000, path, domain, secure)
166 }
167 }
168
169 //
170 // Custom clone for textarea and select clone() bug
171 //
172 $.fn.spf_clone = function () {
173 var base = $.fn.clone.apply(this, arguments),
174 clone = this.find('select').add(this.filter('select')),
175 cloned = base.find('select').add(base.filter('select'))
176
177 for (var i = 0; i < clone.length; ++i) {
178 for (var j = 0; j < clone[i].options.length; ++j) {
179 if (clone[i].options[j].selected === true) {
180 cloned[i].options[j].selected = true
181 }
182 }
183 }
184
185 this.find(':radio').each(function () {
186 this.orginal_checked = this.checked
187 })
188
189 return base
190 }
191
192 //
193 // Expand All Options
194 //
195 $.fn.spf_expand_all = function () {
196 return this.each(function () {
197 $(this).on('click', function (e) {
198 e.preventDefault()
199 $('.spf-wrapper').toggleClass('spf-show-all')
200 $('.spf-section').spf_reload_script()
201 $(this)
202 .find('.fa')
203 .toggleClass('fa-indent')
204 .toggleClass('fa-outdent')
205 })
206 })
207 }
208
209 //
210 // Options Navigation
211 //
212 $.fn.spf_nav_options = function () {
213 return this.each(function () {
214 var $nav = $(this),
215 $links = $nav.find('a'),
216 $hidden = $nav.closest('.spf').find('.spf-section-id'),
217 $last_section
218
219 $(window)
220 .on('hashchange', function () {
221 var hash = window.location.hash.match(new RegExp('tab=([^&]*)'))
222 var slug = hash
223 ? hash[1]
224 : $links
225 .first()
226 .attr('href')
227 .replace('#tab=', '')
228 var $link = $('#spf-tab-link-' + slug)
229
230 if ($link.length > 0) {
231 $link
232 .closest('.spf-tab-depth-0')
233 .addClass('spf-tab-active')
234 .siblings()
235 .removeClass('spf-tab-active')
236 $links.removeClass('spf-section-active')
237 $link.addClass('spf-section-active')
238
239 if ($last_section !== undefined) {
240 $last_section.hide()
241 }
242
243 var $section = $('#spf-section-' + slug)
244 $section.css({ display: 'block' })
245 $section.spf_reload_script()
246
247 $hidden.val(slug)
248
249 $last_section = $section
250 }
251 })
252 .trigger('hashchange')
253 })
254 }
255
256 //
257 // Metabox Tabs
258 //
259 $.fn.spf_nav_metabox = function () {
260 return this.each(function () {
261 var $nav = $(this),
262 $links = $nav.find('a'),
263 unique_id = $nav.data('unique'),
264 post_id = $('#post_ID').val() || 'global',
265 $last_section,
266 $last_link
267
268 $links.on('click', function (e) {
269 e.preventDefault()
270
271 var $link = $(this),
272 section_id = $link.data('section')
273
274 if ($last_link !== undefined) {
275 $last_link.removeClass('spf-section-active')
276 }
277
278 if ($last_section !== undefined) {
279 $last_section.hide()
280 }
281
282 $link.addClass('spf-section-active')
283
284 var $section = $('#spf-section-' + section_id)
285 $section.css({ display: 'block' })
286 $section.spf_reload_script()
287
288 SP_PC.helper.set_cookie(
289 'spf-last-metabox-tab-' + post_id + '-' + unique_id,
290 section_id
291 )
292
293 $last_section = $section
294 $last_link = $link
295 })
296
297 var get_cookie = SP_PC.helper.get_cookie(
298 'spf-last-metabox-tab-' + post_id + '-' + unique_id
299 )
300
301 if (get_cookie) {
302 $nav.find('a[data-section="' + get_cookie + '"]').trigger('click')
303 } else {
304 $links.first('a').trigger('click')
305 }
306 })
307 }
308
309 //
310 // Metabox Page Templates Listener
311 //
312 $.fn.spf_page_templates = function () {
313 if (this.length) {
314 $(document).on(
315 'change',
316 '.editor-page-attributes__template select, #page_template',
317 function () {
318 var maybe_value = $(this).val() || 'default'
319
320 $('.spf-page-templates')
321 .removeClass('spf-show')
322 .addClass('spf-hide')
323 $(
324 '.spf-page-' +
325 maybe_value.toLowerCase().replace(/[^a-zA-Z0-9]+/g, '-')
326 )
327 .removeClass('spf-hide')
328 .addClass('spf-show')
329 }
330 )
331 }
332 }
333
334 //
335 // Metabox Post Formats Listener
336 //
337 $.fn.spf_post_formats = function () {
338 if (this.length) {
339 $(document).on(
340 'change',
341 '.editor-post-format select, #formatdiv input[name="post_format"]',
342 function () {
343 var maybe_value = $(this).val() || 'default'
344
345 // Fallback for classic editor version
346 maybe_value = maybe_value === '0' ? 'default' : maybe_value
347
348 $('.spf-post-formats')
349 .removeClass('spf-show')
350 .addClass('spf-hide')
351 $('.spf-post-format-' + maybe_value)
352 .removeClass('spf-hide')
353 .addClass('spf-show')
354 }
355 )
356 }
357 }
358
359 //
360 // Search
361 //
362 $.fn.spf_search = function () {
363 return this.each(function () {
364 var $this = $(this),
365 $input = $this.find('input')
366
367 $input.on('change keyup', function () {
368 var value = $(this).val(),
369 $wrapper = $('.spf-wrapper'),
370 $section = $wrapper.find('.spf-section'),
371 $fields = $section.find('> .spf-field:not(.hidden)'),
372 $titles = $fields.find('> .spf-title, .spf-search-tags')
373
374 if (value.length > 3) {
375 $fields.addClass('spf-hidden')
376 $wrapper.addClass('spf-search-all')
377
378 $titles.each(function () {
379 var $title = $(this)
380
381 if ($title.text().match(new RegExp('.*?' + value + '.*?', 'i'))) {
382 var $field = $title.closest('.spf-field')
383
384 $field.removeClass('spf-hidden')
385 $field.parent().spf_reload_script()
386 }
387 })
388 } else {
389 $fields.removeClass('spf-hidden')
390 $wrapper.removeClass('spf-search-all')
391 }
392 })
393 })
394 }
395
396 //
397 // Sticky Header
398 //
399 $.fn.spf_sticky = function () {
400 return this.each(function () {
401 var $this = $(this),
402 $window = $(window),
403 $inner = $this.find('.spf-header-inner'),
404 padding =
405 parseInt($inner.css('padding-left')) +
406 parseInt($inner.css('padding-right')),
407 offset = 32,
408 scrollTop = 0,
409 lastTop = 0,
410 ticking = false,
411 stickyUpdate = function () {
412 var offsetTop = $this.offset().top,
413 stickyTop = Math.max(offset, offsetTop - scrollTop),
414 winWidth = Math.max(
415 document.documentElement.clientWidth,
416 window.innerWidth || 0
417 )
418
419 if (stickyTop <= offset && winWidth > 782) {
420 $inner.css({ width: $this.outerWidth() - padding })
421 $this.css({ height: $this.outerHeight() }).addClass('spf-sticky')
422 } else {
423 $inner.removeAttr('style')
424 $this.removeAttr('style').removeClass('spf-sticky')
425 }
426 },
427 requestTick = function () {
428 if (!ticking) {
429 requestAnimationFrame(function () {
430 stickyUpdate()
431 ticking = false
432 })
433 }
434
435 ticking = true
436 },
437 onSticky = function () {
438 scrollTop = $window.scrollTop()
439 requestTick()
440 }
441
442 $window.on('scroll resize', onSticky)
443
444 onSticky()
445 })
446 }
447
448 //
449 // Dependency System
450 //
451 $.fn.spf_dependency = function () {
452 return this.each(function () {
453 var $this = $(this),
454 ruleset = $.spf_deps.createRuleset(),
455 depends = [],
456 is_global = false
457
458 $this.children('[data-controller]').each(function () {
459 var $field = $(this),
460 controllers = $field.data('controller').split('|'),
461 conditions = $field.data('condition').split('|'),
462 values = $field
463 .data('value')
464 .toString()
465 .split('|'),
466 rules = ruleset
467
468 if ($field.data('depend-global')) {
469 is_global = true
470 }
471
472 $.each(controllers, function (index, depend_id) {
473 var value = values[index] || '',
474 condition = conditions[index] || conditions[0]
475
476 rules = rules.createRule(
477 '[data-depend-id="' + depend_id + '"]',
478 condition,
479 value
480 )
481
482 rules.include($field)
483
484 depends.push(depend_id)
485 })
486 })
487
488 if (depends.length) {
489 if (is_global) {
490 $.spf_deps.enable(SP_PC.vars.$body, ruleset, depends)
491 } else {
492 $.spf_deps.enable($this, ruleset, depends)
493 }
494 }
495 })
496 }
497
498 //
499 // Field: accordion
500 //
501 $.fn.spf_field_accordion = function () {
502 return this.each(function () {
503 // Keep the accordion open by default.
504
505 var $titles = $(this).find('.spf-accordion-title')
506
507 //$titles.on('load', function() {
508 //var $title_area = $(this),
509 // var $content_area = $titles.next();
510
511 //$content_area.addClass('spf-accordion-open');
512 // if (!$content_area.data('opened')) {
513 // $content_area.spf_reload_script();
514 // $content_area.data('opened', true);
515 // }
516 // $content_area.addClass('spf-accordion-open');
517 //});
518
519 $titles.on('click', function () {
520 var $title = $(this),
521 $icon = $title.find('.spf-accordion-icon'),
522 $content = $title.next()
523
524 if ($icon.hasClass('fa-angle-right')) {
525 $icon.removeClass('fa-angle-right').addClass('fa-angle-down');
526 $title.toggleClass('expanded')
527 } else {
528 $icon.removeClass('fa-angle-down').addClass('fa-angle-right');
529 $title.toggleClass('expanded')
530 }
531
532 if (!$content.data('opened')) {
533 $content.spf_reload_script()
534 $content.data('opened', true)
535 }
536
537 $content.toggleClass('spf-accordion-open')
538 })
539 /* var $sortable = $(this).find('.spf-accordion-items');
540 $sortable.sortable({
541 axis: 'y',
542 helper: 'original',
543 cursor: 'move',
544 placeholder: 'widget-placeholder',
545 update: function( event, ui ) {
546 $sortable.spf_customizer_refresh();
547 }
548 });
549 $sortable.find('.spf-accordion-content').spf_reload_script();*/
550 })
551 }
552
553 //
554 // Field: sortable
555 //
556 $.fn.spf_field_sortable = function () {
557 return this.each(function () {
558 var $sortable = $(this).find('.spf--sortable')
559 // Post content sort only with handle
560 if ($(this).hasClass('post_content_sorter')) {
561 $sortable.sortable({
562 axis: 'y',
563 helper: 'original',
564 handle: '.spf--sortable-helper',
565 cursor: 'move',
566 placeholder: 'widget-placeholder',
567 items: '.spf--sortable-item:not(:nth-child(5),:nth-child(6))'
568 // cancel: '.spf--sortable-item:nth-child(5),.spf--sortable-item:nth-child(5)' // ⛔ disable last item
569
570 // update: function( event, ui ) {
571 // $sortable.spf_customizer_refresh();
572 // }
573 })
574 } else {
575 $sortable.sortable({
576 axis: 'y',
577 helper: 'original',
578 cursor: 'move',
579 placeholder: 'widget-placeholder',
580 items: '.spf--sortable-item:not(:nth-child(5),:nth-child(6))'
581 // update: function( event, ui ) {
582 // $sortable.spf_customizer_refresh();
583 // }
584 })
585 }
586
587 $sortable.find('.spf--sortable-content').spf_reload_script()
588 })
589 }
590
591 //
592 // Field: code_editor - Using WP Code Editor.
593 //
594 $.fn.spf_field_code_editor = function () {
595 return this.each(function () {
596 if (typeof wp === 'undefined' || typeof wp.codeEditor === 'undefined') {
597 return;
598 }
599
600 var $this = $(this),
601 $textarea = $this.find('textarea'),
602 settings = $textarea.data('editor') || {};
603
604 // Merge with WP defaults
605 var editorSettings = wp.codeEditor.defaultSettings ? _.clone(wp.codeEditor.defaultSettings) : {};
606 editorSettings.codemirror = _.extend(
607 {},
608 editorSettings.codemirror,
609 settings
610 );
611
612 // Initialize editor
613 var editor = wp.codeEditor.initialize($textarea[0], editorSettings);
614 //editor.codemirror.setOption('theme', 'monokai');
615 // Sync changes back to textarea
616 editor.codemirror.on('change', function () {
617 $textarea.val(editor.codemirror.getValue()).trigger('change');
618 });
619 });
620 };
621
622
623 //
624 // Field: tabbed
625 //
626 $.fn.spf_field_tabbed = function () {
627 return this.each(function () {
628 var $this = $(this),
629 $links = $this.find('.spf-tabbed-nav a'),
630 $sections = $this.find('.spf-tabbed-section');
631
632 $links.on('click', function (e) {
633 e.preventDefault();
634
635 var $link = $(this),
636 index = $link.index(),
637 $section = $sections.eq(index);
638
639 // Store the active tab index in a cookie
640 SP_PC.helper.set_cookie('activeTabIndex', index);
641
642 $link.addClass('spf-tabbed-active').siblings().removeClass('spf-tabbed-active');
643 $section.spf_reload_script();
644 $section.removeClass('hidden').siblings().addClass('hidden');
645 });
646 // Check if there's a stored active tab index in the cookie
647 var activeTabIndex = SP_PC.helper.get_cookie('activeTabIndex');
648 // Check if the cookie exists
649 if (activeTabIndex !== null) {
650 $links.eq(activeTabIndex).trigger('click');
651 } else {
652 $links.first().trigger('click');
653 }
654
655 });
656 };
657
658 //
659 // Field: fieldset
660 //
661 $.fn.spf_field_fieldset = function () {
662 return this.each(function () {
663 $(this)
664 .find('.spf-fieldset-content')
665 .spf_reload_script()
666 })
667 }
668
669 //
670 // Field: group
671 //
672 $.fn.spf_field_group = function () {
673 return this.each(function () {
674 var $this = $(this),
675 $fieldset = $this.children('.spf-fieldset'),
676 $group = $fieldset.length ? $fieldset : $this,
677 $wrapper = $group.children('.spf-cloneable-wrapper'),
678 $hidden = $group.children('.spf-cloneable-hidden'),
679 $max = $group.children('.spf-cloneable-max'),
680 $min = $group.children('.spf-cloneable-min'),
681 field_id = $wrapper.data('field-id'),
682 unique_id = $wrapper.data('unique-id'),
683 is_number = Boolean(Number($wrapper.data('title-number'))),
684 max = parseInt($wrapper.data('max')),
685 min = parseInt($wrapper.data('min'))
686
687 // clear accordion arrows if multi-instance
688 if ($wrapper.hasClass('ui-accordion')) {
689 $wrapper.find('.ui-accordion-header-icon').remove()
690 }
691
692 var update_title_numbers = function ($selector) {
693 $selector.find('.spf-cloneable-title-number').each(function (index) {
694 $(this).html(
695 $(this)
696 .closest('.spf-cloneable-item')
697 .index() +
698 1 +
699 '.'
700 )
701 })
702 }
703
704 // Hide the taxonomy relation field. -- Shamim
705 var $pcp_group_parent = $this.parent()
706 var pcp_hide_the_taxonomy_relation = function (count_tax) {
707 if (count_tax < 2) {
708 $($pcp_group_parent)
709 .find('.pcp_relate_among_taxonomies')
710 .hide()
711 } else {
712 $($pcp_group_parent)
713 .find('.pcp_relate_among_taxonomies')
714 .show()
715 }
716 }
717
718 var count = $wrapper.children('.spf-cloneable-item').length
719 pcp_hide_the_taxonomy_relation(count)
720
721 $wrapper.accordion({
722 header: '> .spf-cloneable-item > .spf-cloneable-title',
723 collapsible: true,
724 active: false,
725 animate: false,
726 heightStyle: 'content',
727 icons: {
728 header: 'spf-cloneable-header-icon',
729 activeHeader: 'spf-cloneable-header-icon'
730 },
731 activate: function (event, ui) {
732 var $panel = ui.newPanel
733 var $header = ui.newHeader
734
735 if ($panel.length && !$panel.data('opened')) {
736 var $fields = $panel.children()
737 var $first = $fields.first().find('select').first()
738 var $title = $header.find('.spf-cloneable-value')
739 var $title_value = $first.children("option").filter(":selected").text();
740 if ($title_value == 'Select Taxonomy') {
741 $title_value = 'Taxonomy';
742 }
743 $title.text($title_value);
744 $first.on('change', function (event) {
745 $title.text($first.val());
746 if ($title.text($first.val())) {
747 $title.text($first.children("option").filter(":selected").text());
748 }
749 });
750
751 $panel.spf_reload_script()
752 $panel.data('opened', true)
753 $panel.data('retry', false)
754 } else if ($panel.data('retry')) {
755 $panel.spf_reload_script_retry()
756 $panel.data('retry', false)
757 }
758 }
759 })
760
761 $wrapper.sortable({
762 axis: 'y',
763 handle: '.spf-cloneable-title,.spf-cloneable-sort',
764 helper: 'original',
765 cursor: 'move',
766 placeholder: 'widget-placeholder',
767 start: function (event, ui) {
768 $wrapper.accordion({ active: false })
769 $wrapper.sortable('refreshPositions')
770 ui.item.children('.spf-cloneable-content').data('retry', true)
771 },
772 update: function (event, ui) {
773 SP_PC.helper.name_nested_replace(
774 $wrapper.children('.spf-cloneable-item'),
775 field_id
776 )
777
778 //$wrapper.spf_customizer_refresh();
779
780 if (is_number) {
781 update_title_numbers($wrapper)
782 }
783 }
784 })
785
786 $group.children('.spf-cloneable-add').on('click', function (e) {
787 e.preventDefault()
788
789 var count = $wrapper.children('.spf-cloneable-item').length
790
791 $min.hide()
792
793 if (max && count + 1 > max) {
794 $max.show()
795 return
796 }
797
798 var new_field_id = unique_id + field_id + '[' + count + ']'
799
800 var $cloned_item = $hidden.spf_clone(true)
801
802 $cloned_item.removeClass('spf-cloneable-hidden')
803 // $cloned_item.find(':input').each( function() {
804 // this.name = new_field_id + this.name.replace( ( this.name.startsWith('_nonce') ? '_nonce' : unique_id ), '');
805 // });
806 $cloned_item.find(':input[name!="_pseudo"]').each(function () {
807 this.name =
808 new_field_id +
809 this.name.replace(
810 this.name.startsWith('_nonce') ? '_nonce' : unique_id,
811 ''
812 )
813 })
814 $cloned_item.find('.spf-data-wrapper').each(function () {
815 $(this).attr('data-unique-id', new_field_id)
816 })
817 $wrapper.append($cloned_item)
818 $wrapper.accordion('refresh')
819 $wrapper.accordion({ active: count })
820 // $wrapper.spf_customizer_refresh();
821 // $wrapper.spf_customizer_listen({closest: true});
822
823 if (is_number) {
824 update_title_numbers($wrapper)
825 }
826 pcp_hide_the_taxonomy_relation(count + 1)
827 })
828
829 var event_clone = function (e) {
830 e.preventDefault()
831
832 var count = $wrapper.children('.spf-cloneable-item').length
833
834 $min.hide()
835
836 if (max && count + 1 > max) {
837 $max.show()
838 return
839 }
840
841 var $this = $(this),
842 $parent = $this.parent().parent(),
843 $cloned_helper = $parent
844 .children('.spf-cloneable-helper')
845 .spf_clone(true),
846 $cloned_title = $parent.children('.spf-cloneable-title').spf_clone(),
847 $cloned_content = $parent
848 .children('.spf-cloneable-content')
849 .spf_clone(),
850 cloned_regex = new RegExp(
851 '(' + SP_PC.helper.preg_quote(field_id) + ')\\[(\\d+)\\]',
852 'g'
853 )
854
855 $cloned_content.find('.spf-data-wrapper').each(function () {
856 var $this = $(this)
857 $this.attr(
858 'data-unique-id',
859 $this
860 .attr('data-unique-id')
861 .replace(
862 cloned_regex,
863 field_id + '[' + ($parent.index() + 1) + ']'
864 )
865 )
866 })
867
868 var $cloned = $('<div class="spf-cloneable-item" />')
869 $cloned.append($cloned_helper)
870 $cloned.append($cloned_title)
871 $cloned.append($cloned_content)
872
873 $wrapper
874 .children()
875 .eq($parent.index())
876 .after($cloned)
877
878 SP_PC.helper.name_nested_replace(
879 $wrapper.children('.spf-cloneable-item'),
880 field_id
881 )
882
883 $wrapper.accordion('refresh')
884 // $wrapper.spf_customizer_refresh();
885 // $wrapper.spf_customizer_listen({closest: true});
886
887 if (is_number) {
888 update_title_numbers($wrapper)
889 }
890 pcp_hide_the_taxonomy_relation(count + 1)
891 }
892
893 $wrapper
894 .children('.spf-cloneable-item')
895 .children('.spf-cloneable-helper')
896 .on('click', '.spf-cloneable-clone', event_clone)
897 $group
898 .children('.spf-cloneable-hidden')
899 .children('.spf-cloneable-helper')
900 .on('click', '.spf-cloneable-clone', event_clone)
901 var event_remove = function (e) {
902 e.preventDefault()
903
904 var count = $wrapper.children('.spf-cloneable-item').length
905
906 $max.hide()
907 $min.hide()
908
909 if (min && count - 1 < min) {
910 $min.show()
911 return
912 }
913
914 $(this)
915 .closest('.spf-cloneable-item')
916 .remove()
917
918 SP_PC.helper.name_nested_replace(
919 $wrapper.children('.spf-cloneable-item'),
920 field_id
921 )
922
923 //$wrapper.spf_customizer_refresh();
924
925 if (is_number) {
926 update_title_numbers($wrapper)
927 }
928 pcp_hide_the_taxonomy_relation(count - 1)
929 }
930 $wrapper
931 .children('.spf-cloneable-item')
932 .children('.spf-cloneable-helper')
933 .on('click', '.spf-cloneable-remove', event_remove)
934 $group
935 .children('.spf-cloneable-hidden')
936 .children('.spf-cloneable-helper')
937 .on('click', '.spf-cloneable-remove', event_remove)
938 })
939 }
940
941 //
942 // Field: icon
943 //
944 $.fn.spf_field_icon = function () {
945 return this.each(function () {
946 var $this = $(this)
947
948 $this.on('click', '.spf-icon-add', function (e) {
949 e.preventDefault()
950
951 var $button = $(this)
952 var $modal = $('#spf-modal-icon')
953
954 $modal.show()
955
956 SP_PC.vars.$icon_target = $this
957
958 if (!SP_PC.vars.icon_modal_loaded) {
959 $modal.find('.spf-modal-loading').show()
960
961 // window.wp.ajax.post( 'spf-get-icons', { nonce: $button.data('nonce') } ).done( function( response ) {
962 window.wp.ajax
963 .post('spf-get-icons', {
964 nonce: $button.data('nonce')
965 })
966 .done(function (response) {
967 $modal.find('.spf-modal-loading').hide()
968
969 SP_PC.vars.icon_modal_loaded = true
970
971 var $load = $modal.find('.spf-modal-load').html(response.content)
972
973 $load.on('click', 'a', function (e) {
974 e.preventDefault()
975
976 var icon = $(this).data('spf-icon')
977
978 SP_PC.vars.$icon_target
979 .find('i')
980 .removeAttr('class')
981 .addClass(icon)
982 SP_PC.vars.$icon_target
983 .find('input')
984 .val(icon)
985 .trigger('change')
986 SP_PC.vars.$icon_target
987 .find('.spf-icon-preview')
988 .removeClass('hidden')
989 SP_PC.vars.$icon_target
990 .find('.spf-icon-remove')
991 .removeClass('hidden')
992
993 $modal.hide()
994 })
995
996 $modal.on('change keyup', '.spf-icon-search', function () {
997 var value = $(this).val(),
998 $icons = $load.find('a')
999
1000 $icons.each(function () {
1001 var $elem = $(this)
1002
1003 if (
1004 $elem.data('spf-icon').search(new RegExp(value, 'i')) < 0
1005 ) {
1006 $elem.hide()
1007 } else {
1008 $elem.show()
1009 }
1010 })
1011 })
1012
1013 $modal.on(
1014 'click',
1015 '.spf-modal-close, .spf-modal-overlay',
1016 function () {
1017 $modal.hide()
1018 }
1019 )
1020 })
1021 .fail(function (response) {
1022 $modal.find('.spf-modal-loading').hide()
1023 $modal.find('.spf-modal-load').html(response.error)
1024 $modal.on('click', function () {
1025 $modal.hide()
1026 })
1027 })
1028 }
1029 })
1030
1031 $this.on('click', '.spf-icon-remove', function (e) {
1032 e.preventDefault()
1033 $this.find('.spf-icon-preview').addClass('hidden')
1034 $this
1035 .find('input')
1036 .val('')
1037 .trigger('change')
1038 $(this).addClass('hidden')
1039 })
1040 })
1041 }
1042
1043 //
1044 // Field: slider
1045 //
1046 $.fn.spf_field_slider = function () {
1047 return this.each(function () {
1048 var $this = $(this),
1049 $input = $this.find('input'),
1050 $slider = $this.find('.spf-slider-ui'),
1051 data = $input.data(),
1052 value = $input.val() || 0
1053
1054 if ($slider.hasClass('ui-slider')) {
1055 $slider.empty()
1056 }
1057
1058 $slider.slider({
1059 range: 'min',
1060 value: value,
1061 min: data.min,
1062 max: data.max,
1063 step: data.step,
1064 slide: function (e, o) {
1065 $input.val(o.value).trigger('change')
1066 }
1067 })
1068
1069 $input.keyup(function () {
1070 $slider.slider('value', $input.val())
1071 })
1072 })
1073 }
1074
1075 //
1076 // Field: c
1077 //
1078 $.fn.spf_field_sorter = function () {
1079 return this.each(function () {
1080 var $this = $(this),
1081 $enabled = $this.find('.spf-enabled'),
1082 $has_disabled = $this.find('.spf-disabled'),
1083 $disabled = $has_disabled.length ? $has_disabled : false
1084
1085 $enabled.sortable({
1086 connectWith: $disabled,
1087
1088 placeholder: 'ui-sortable-placeholder',
1089
1090 update: function (event, ui) {
1091 var $el = ui.item.find('input')
1092
1093 if (ui.item.parent().hasClass('spf-enabled')) {
1094 $el.attr('name', $el.attr('name').replace('disabled', 'enabled'))
1095 } else {
1096 $el.attr('name', $el.attr('name').replace('enabled', 'disabled'))
1097 }
1098
1099 // $this.spf_customizer_refresh();
1100 }
1101 })
1102
1103 if ($disabled) {
1104 $disabled.sortable({
1105 connectWith: $enabled,
1106 placeholder: 'ui-sortable-placeholder'
1107 // update: function( event, ui ) {
1108 // $this.spf_customizer_refresh();
1109 // }
1110 })
1111 }
1112 })
1113 }
1114
1115 //
1116 // Field: spinner
1117 //
1118 $.fn.spf_field_spinner = function () {
1119 return this.each(function () {
1120 var $this = $(this),
1121 $input = $this.find('input'),
1122 $inited = $this.find('.ui-spinner-button'),
1123 $unit = $input.data('unit')
1124
1125 if ($inited.length) {
1126 $inited.remove()
1127 }
1128
1129 $input.spinner({
1130 max: $input.data('max') || 100,
1131 min: $input.data('min') || 0,
1132 step: $input.data('step') || 1,
1133 create: function (event, ui) {
1134 if ($unit.length) {
1135 $this
1136 .find('.ui-spinner-up')
1137 .after(
1138 '<span class="ui-button-text-only spf--unit">' +
1139 $unit +
1140 '</span>'
1141 )
1142 }
1143 },
1144 spin: function (event, ui) {
1145 $input.val(ui.value).trigger('change')
1146 }
1147 })
1148 })
1149 }
1150
1151 //
1152 // Field: switcher
1153 //
1154 $.fn.spf_field_switcher = function () {
1155 return this.each(function () {
1156 var $switcher = $(this).find('.spf--switcher')
1157
1158 $switcher.on('click', function () {
1159 var value = 0
1160 var $input = $switcher.find('input')
1161
1162 if ($switcher.hasClass('spf--active')) {
1163 $switcher.removeClass('spf--active')
1164 } else {
1165 value = 1
1166 $switcher.addClass('spf--active')
1167 }
1168
1169 $input.val(value).trigger('change')
1170 })
1171 })
1172 }
1173
1174 //
1175 // Confirm
1176 //
1177 $.fn.spf_confirm = function () {
1178 return this.each(function () {
1179 $(this).on('click', function (e) {
1180 var confirm_text =
1181 $(this).data('confirm') || window.spf_vars.i18n.confirm
1182 var confirm_answer = confirm(confirm_text)
1183 SP_PC.vars.is_confirm = true
1184
1185 if (!confirm_answer) {
1186 e.preventDefault()
1187 SP_PC.vars.is_confirm = false
1188
1189 return false
1190 }
1191 })
1192 })
1193 }
1194
1195 $.fn.serializeObject = function () {
1196 var obj = {}
1197
1198 $.each(this.serializeArray(), function (i, o) {
1199 var n = o.name,
1200 v = o.value
1201
1202 obj[n] =
1203 obj[n] === undefined
1204 ? v
1205 : $.isArray(obj[n])
1206 ? obj[n].concat(v)
1207 : [obj[n], v]
1208 })
1209
1210 return obj
1211 }
1212
1213 //
1214 // Options Save
1215 //
1216 $.fn.spf_save = function () {
1217 return this.each(function () {
1218 var $this = $(this),
1219 $buttons = $('.spf-save'),
1220 $panel = $('.spf-options'),
1221 flooding = false,
1222 timeout
1223
1224 $this.on('click', function (e) {
1225 if (!flooding) {
1226 var $text = $this.data('save'),
1227 $value = $this.val()
1228
1229 $buttons.attr('value', $text)
1230
1231 if ($this.hasClass('spf-save-ajax')) {
1232 e.preventDefault()
1233
1234 $panel.addClass('spf-saving')
1235 $buttons.prop('disabled', true)
1236
1237 window.wp.ajax
1238 .post('spf_' + $panel.data('unique') + '_ajax_save', {
1239 data: $('#spf-form').serializeJSONSP_PC(),
1240 nonce: $('#spf_options_nonce' + $panel.data('unique')).val(),
1241 })
1242 .done(function (response) {
1243 clearTimeout(timeout)
1244
1245 var $result_success = $('.spf-form-success')
1246
1247 $result_success
1248 .empty()
1249 .append(response.notice)
1250 .slideDown('fast', function () {
1251 timeout = setTimeout(function () {
1252 $result_success.slideUp('fast')
1253 }, 2000)
1254 })
1255
1256 // clear errors
1257 $('.spf-error').remove()
1258
1259 var $append_errors = $('.spf-form-error')
1260
1261 $append_errors.empty().hide()
1262
1263 if (Object.keys(response.errors).length) {
1264 var error_icon = '<i class="spf-label-error spf-error">!</i>'
1265
1266 $.each(response.errors, function (key, error_message) {
1267 var $field = $('[data-depend-id="' + key + '"]'),
1268 $link = $(
1269 '#spf-tab-link-' +
1270 ($field.closest('.spf-section').index() + 1)
1271 ),
1272 $tab = $link.closest('.spf-tab-depth-0')
1273
1274 $field
1275 .closest('.spf-fieldset')
1276 .append(
1277 '<p class="spf-text-error spf-error">' +
1278 error_message +
1279 '</p>'
1280 )
1281
1282 if (!$link.find('.spf-error').length) {
1283 $link.append(error_icon)
1284 }
1285
1286 if (!$tab.find('.spf-arrow .spf-error').length) {
1287 $tab.find('.spf-arrow').append(error_icon)
1288 }
1289
1290 console.log(error_message)
1291
1292 $append_errors.append(
1293 '<div>' + error_icon + ' ' + error_message + '</div>'
1294 )
1295 })
1296
1297 $append_errors.show()
1298 }
1299
1300 $panel.removeClass('spf-saving')
1301 $buttons.prop('disabled', false).attr('value', $value)
1302 flooding = false
1303 })
1304 .fail(function (response) {
1305 alert(response.error)
1306 })
1307 }
1308 }
1309
1310 flooding = true
1311 })
1312 })
1313 }
1314
1315 //
1316 // WP Color Picker
1317 //
1318 if (typeof Color === 'function') {
1319 Color.fn.toString = function () {
1320 if (this._alpha < 1) {
1321 return this.toCSS('rgba', this._alpha).replace(/\s+/g, '')
1322 }
1323
1324 var hex = parseInt(this._color, 10).toString(16)
1325
1326 if (this.error) {
1327 return ''
1328 }
1329
1330 if (hex.length < 6) {
1331 for (var i = 6 - hex.length - 1; i >= 0; i--) {
1332 hex = '0' + hex
1333 }
1334 }
1335
1336 return '#' + hex
1337 }
1338 }
1339
1340 SP_PC.funcs.parse_color = function (color) {
1341 var value = color.replace(/\s+/g, ''),
1342 trans =
1343 value.indexOf('rgba') !== -1
1344 ? parseFloat(value.replace(/^.*,(.+)\)/, '$1') * 100)
1345 : 100,
1346 rgba = trans < 100 ? true : false
1347
1348 return { value: value, transparent: trans, rgba: rgba }
1349 }
1350
1351 $.fn.spf_color = function () {
1352 return this.each(function () {
1353 var $input = $(this),
1354 picker_color = SP_PC.funcs.parse_color($input.val()),
1355 palette_color = window.spf_vars.color_palette.length
1356 ? window.spf_vars.color_palette
1357 : true,
1358 $container
1359
1360 // Destroy and Re-init.
1361 if ($input.hasClass('wp-color-picker')) {
1362 $input
1363 .closest('.wp-picker-container')
1364 .after($input)
1365 .remove()
1366 }
1367
1368 $input.wpColorPicker({
1369 palettes: palette_color,
1370 change: function (event, ui) {
1371 var ui_color_value = ui.color.toString()
1372
1373 $container.removeClass('spf--transparent-active')
1374 $container
1375 .find('.spf--transparent-offset')
1376 .css('background-color', ui_color_value)
1377 $input.val(ui_color_value).trigger('change')
1378 },
1379 create: function () {
1380 $container = $input.closest('.wp-picker-container')
1381
1382 var a8cIris = $input.data('a8cIris'),
1383 $transparent_wrap = $(
1384 '<div class="spf--transparent-wrap">' +
1385 '<div class="spf--transparent-slider"></div>' +
1386 '<div class="spf--transparent-offset"></div>' +
1387 '<div class="spf--transparent-text"></div>' +
1388 '<div class="spf--transparent-button">transparent <i class="fa fa-toggle-off"></i></div>' +
1389 '</div>'
1390 ).appendTo($container.find('.wp-picker-holder')),
1391 $transparent_slider = $transparent_wrap.find(
1392 '.spf--transparent-slider'
1393 ),
1394 $transparent_text = $transparent_wrap.find(
1395 '.spf--transparent-text'
1396 ),
1397 $transparent_offset = $transparent_wrap.find(
1398 '.spf--transparent-offset'
1399 ),
1400 $transparent_button = $transparent_wrap.find(
1401 '.spf--transparent-button'
1402 )
1403
1404 if ($input.val() === 'transparent') {
1405 $container.addClass('spf--transparent-active')
1406 }
1407
1408 $transparent_button.on('click', function () {
1409 if ($input.val() !== 'transparent') {
1410 $input
1411 .val('transparent')
1412 .trigger('change')
1413 .removeClass('iris-error')
1414 $container.addClass('spf--transparent-active')
1415 } else {
1416 $input.val(a8cIris._color.toString()).trigger('change')
1417 $container.removeClass('spf--transparent-active')
1418 }
1419 })
1420
1421 $transparent_slider.slider({
1422 value: picker_color.transparent,
1423 step: 1,
1424 min: 0,
1425 max: 100,
1426 slide: function (event, ui) {
1427 var slide_value = parseFloat(ui.value / 100)
1428 a8cIris._color._alpha = slide_value
1429 $input.wpColorPicker('color', a8cIris._color.toString())
1430 $transparent_text.text(
1431 slide_value === 1 || slide_value === 0 ? '' : slide_value
1432 )
1433 },
1434 create: function () {
1435 var slide_value = parseFloat(picker_color.transparent / 100),
1436 text_value = slide_value < 1 ? slide_value : ''
1437
1438 $transparent_text.text(text_value)
1439 $transparent_offset.css('background-color', picker_color.value)
1440
1441 $container.on('click', '.wp-picker-clear', function () {
1442 a8cIris._color._alpha = 1
1443 $transparent_text.text('')
1444 $transparent_slider.slider('option', 'value', 100)
1445 $container.removeClass('spf--transparent-active')
1446 $input.trigger('change')
1447 })
1448
1449 $container.on('click', '.wp-picker-default', function () {
1450 var default_color = SP_PC.funcs.parse_color(
1451 $input.data('default-color')
1452 ),
1453 default_value = parseFloat(default_color.transparent / 100),
1454 default_text = default_value < 1 ? default_value : ''
1455
1456 a8cIris._color._alpha = default_value
1457 $transparent_text.text(default_text)
1458 $transparent_slider.slider(
1459 'option',
1460 'value',
1461 default_color.transparent
1462 )
1463 })
1464 }
1465 })
1466 }
1467 })
1468 })
1469 }
1470
1471 //
1472 // Number (only allow numeric inputs)
1473 //
1474 $.fn.spf_number = function () {
1475 return this.each(function () {
1476 $(this).on('keypress', function (e) {
1477 if (
1478 e.keyCode !== 0 &&
1479 e.keyCode !== 8 &&
1480 e.keyCode !== 45 &&
1481 e.keyCode !== 46 &&
1482 (e.keyCode < 48 || e.keyCode > 57)
1483 ) {
1484 return false
1485 }
1486 })
1487 })
1488 }
1489
1490 //
1491 // ChosenJS
1492 //
1493 $.fn.spf_chosen = function () {
1494 return this.each(function () {
1495 var $this = $(this),
1496 $inited = $this.parent().find('.chosen-container'),
1497 is_sortable = $this.hasClass('spf-chosen-sortable') || false,
1498 is_ajax = $this.hasClass('spf-chosen-ajax') || false,
1499 is_multiple = $this.attr('multiple') || false,
1500 set_width = is_multiple ? 'auto' : 'auto',
1501 set_options = $.extend(
1502 {
1503 allow_single_deselect: true,
1504 disable_search_threshold: 10,
1505 width: set_width,
1506 no_results_text: window.spf_vars.i18n.no_results_text
1507 },
1508 $this.data('chosen-settings')
1509 )
1510
1511 if ($inited.length) {
1512 $inited.remove()
1513 }
1514
1515 // Chosen ajax
1516 if (is_ajax) {
1517 var set_ajax_options = $.extend(
1518 {
1519 data: {
1520 type: 'post',
1521 nonce: ''
1522 },
1523 allow_single_deselect: true,
1524 disable_search_threshold: -1,
1525 width: '100%',
1526 min_length: 3,
1527 type_delay: 500,
1528 typing_text: window.spf_vars.i18n.typing_text,
1529 searching_text: window.spf_vars.i18n.searching_text,
1530 no_results_text: window.spf_vars.i18n.no_results_text
1531 },
1532 $this.data('chosen-settings')
1533 )
1534
1535 $this.SP_PCAjaxChosen(set_ajax_options)
1536 } else {
1537 $this.chosen(set_options)
1538 }
1539
1540 // Chosen keep options order
1541 if (is_multiple) {
1542 var $hidden_select = $this.parent().find('.spf-hidden-select')
1543 var $hidden_value = $hidden_select.val() || []
1544
1545 $this.on('change', function (obj, result) {
1546 if (result && result.selected) {
1547 $hidden_select.append(
1548 '<option value="' +
1549 result.selected +
1550 '" selected="selected">' +
1551 result.selected +
1552 '</option>'
1553 )
1554 } else if (result && result.deselected) {
1555 $hidden_select
1556 .find('option[value="' + result.deselected + '"]')
1557 .remove()
1558 }
1559
1560 // Force customize refresh
1561 if (
1562 $hidden_select.children().length === 0 &&
1563 window.wp.customize !== undefined
1564 ) {
1565 window.wp.customize
1566 .control($hidden_select.data('customize-setting-link'))
1567 .setting.set('')
1568 }
1569
1570 $hidden_select.trigger('change')
1571 })
1572
1573 // Chosen order abstract
1574 $this.SP_PCChosenOrder($hidden_value, true)
1575 }
1576
1577 // Chosen sortable
1578 if (is_sortable) {
1579 var $chosen_container = $this.parent().find('.chosen-container')
1580 var $chosen_choices = $chosen_container.find('.chosen-choices')
1581
1582 $chosen_choices.bind('mousedown', function (event) {
1583 if ($(event.target).is('span')) {
1584 event.stopPropagation()
1585 }
1586 })
1587
1588 $chosen_choices.sortable({
1589 items: 'li:not(.search-field)',
1590 helper: 'orginal',
1591 cursor: 'move',
1592 placeholder: 'search-choice-placeholder',
1593 start: function (e, ui) {
1594 ui.placeholder.width(ui.item.innerWidth())
1595 ui.placeholder.height(ui.item.innerHeight())
1596 },
1597 update: function (e, ui) {
1598 var select_options = ''
1599 var chosen_object = $this.data('chosen')
1600 var $prev_select = $this.parent().find('.spf-hidden-select')
1601
1602 $chosen_choices.find('.search-choice-close').each(function () {
1603 var option_array_index = $(this).data('option-array-index')
1604 $.each(chosen_object.results_data, function (index, data) {
1605 if (data.array_index === option_array_index) {
1606 select_options +=
1607 '<option value="' +
1608 data.value +
1609 '" selected>' +
1610 data.value +
1611 '</option>'
1612 }
1613 })
1614 })
1615
1616 $prev_select.children().remove()
1617 $prev_select.append(select_options)
1618 $prev_select.trigger('change')
1619 }
1620 })
1621 }
1622 })
1623 }
1624
1625 //
1626 // Helper Checkbox Checker
1627 //
1628 $.fn.spf_checkbox = function () {
1629 return this.each(function () {
1630 var $this = $(this),
1631 $input = $this.find('.spf--input'),
1632 $checkbox = $this.find('.spf--checkbox')
1633
1634 $checkbox.on('click', function () {
1635 $input.val(Number($checkbox.prop('checked'))).trigger('change')
1636 })
1637 })
1638 }
1639
1640 //
1641 // Siblings
1642 //
1643 $.fn.spf_siblings = function () {
1644 return this.each(function () {
1645 var $this = $(this),
1646 $siblings = $this.find('.spf--sibling:not(.pcp-pro-only)'),
1647 multiple = $this.data('multiple') || false
1648
1649 $siblings.on('click', function () {
1650 var $sibling = $(this)
1651
1652 if (multiple) {
1653 if ($sibling.hasClass('spf--active')) {
1654 $sibling.removeClass('spf--active')
1655 $sibling
1656 .find('input')
1657 .prop('checked', false)
1658 .trigger('change')
1659 } else {
1660 $sibling.addClass('spf--active')
1661 $sibling
1662 .find('input')
1663 .prop('checked', true)
1664 .trigger('change')
1665 }
1666 } else {
1667 $this.find('input').prop('checked', false)
1668 $sibling
1669 .find('input')
1670 .prop('checked', true)
1671 .trigger('change')
1672 $sibling
1673 .addClass('spf--active')
1674 .siblings()
1675 .removeClass('spf--active')
1676 }
1677 })
1678 })
1679 }
1680
1681 //
1682 // Help Tooltip
1683 //
1684 // $.fn.spf_help = function () {
1685 // return this.each(function () {
1686 // var $this = $(this),
1687 // $tooltip,
1688 // offset_left
1689
1690 // $this.on({
1691 // mouseenter: function () {
1692 // $tooltip = $('<div class="spf-tooltip"></div>')
1693 // .html($this.find('.spf-help-text').html())
1694 // .appendTo('body')
1695 // offset_left = SP_PC.vars.is_rtl
1696 // ? $this.offset().left - $tooltip.outerWidth()
1697 // : $this.offset().left + 24
1698
1699 // $tooltip.css({
1700 // top: $this.offset().top - ($tooltip.outerHeight() / 2 - 14),
1701 // left: offset_left
1702 // })
1703 // },
1704 // mouseleave: function () {
1705 // if ($tooltip !== undefined) {
1706 // $tooltip.remove()
1707 // }
1708 // }
1709 // })
1710 // })
1711 // }
1712 //
1713 // Help Tooltip
1714 //
1715 $.fn.spf_help = function () {
1716 return this.each(function () {
1717 var $this = $(this);
1718 var $tooltip;
1719 var $class = '';
1720 $this.on({
1721 mouseenter: function () {
1722 // this class add with the support tooltip.
1723 if ($this.find('.spf-support').length > 0) {
1724 $class = 'support-tooltip';
1725 }
1726 $tooltip = $('<div class="spf-tooltip ' + $class + '"></div>')
1727 .html($this.find('.spf-help-text').html())
1728 .appendTo('body');
1729
1730 var offset_left = SP_PC.vars.is_rtl
1731 ? $this.offset().left - $tooltip.outerWidth()
1732 : $this.offset().left + 24;
1733 var $top = $this.offset().top - ($tooltip.outerHeight() / 2 - 14);
1734 // this block used for support tooltip.
1735 if ($this.find('.spf-support').length > 0) {
1736 $top = $this.offset().top + 48;
1737 offset_left = $this.offset().left - 231;
1738 }
1739 $tooltip.css({
1740 top: $top,
1741 left: offset_left,
1742 });
1743 },
1744 mouseleave: function () {
1745 if ($tooltip !== undefined) {
1746 // Check if the cursor is still over the tooltip
1747 if (!$tooltip.is(':hover')) {
1748 $tooltip.remove();
1749 }
1750 }
1751 },
1752 });
1753 // Event delegation to handle tooltip removal when the cursor leaves the tooltip itself.
1754 $('body').on('mouseleave', '.spf-tooltip', function () {
1755 if ($tooltip !== undefined) {
1756 $tooltip.remove();
1757 }
1758 });
1759 });
1760 }
1761
1762 //
1763 // Window on resize
1764 //
1765 SP_PC.vars.$window
1766 .on(
1767 'resize spf.resize',
1768 SP_PC.helper.debounce(function (event) {
1769 var window_width =
1770 navigator.userAgent.indexOf('AppleWebKit/') > -1
1771 ? SP_PC.vars.$window.width()
1772 : window.innerWidth
1773
1774 if (window_width <= 782 && !SP_PC.vars.onloaded) {
1775 $('.spf-section').spf_reload_script()
1776 SP_PC.vars.onloaded = true
1777 }
1778 }, 200)
1779 )
1780 .trigger('spf.resize')
1781
1782 //
1783 // Widgets Framework
1784 //
1785 $.fn.spf_widgets = function () {
1786 if (this.length) {
1787 $(document).on('widget-added widget-updated', function (event, $widget) {
1788 $widget.find('.spf-fields').spf_reload_script()
1789 })
1790
1791 $('.widgets-sortables, .control-section-sidebar').on(
1792 'sortstop',
1793 function (event, ui) {
1794 ui.item.find('.spf-fields').spf_reload_script_retry()
1795 }
1796 )
1797
1798 $(document).on('click', '.widget-top', function (event) {
1799 $(this)
1800 .parent()
1801 .find('.spf-fields')
1802 .spf_reload_script()
1803 })
1804 }
1805 }
1806
1807 //
1808 // Retry Plugins
1809 //
1810 $.fn.spf_reload_script_retry = function () {
1811 return this.each(function () {
1812 var $this = $(this)
1813 })
1814 }
1815
1816 //
1817 // Reload Plugins
1818 //
1819 $.fn.spf_reload_script = function (options) {
1820 var settings = $.extend(
1821 {
1822 dependency: true
1823 },
1824 options
1825 )
1826
1827 return this.each(function () {
1828 var $this = $(this)
1829 // Avoid for conflicts
1830 if (!$this.data('inited')) {
1831 // Field plugins
1832 $this.children('.spf-field-accordion').spf_field_accordion()
1833 $this.children('.spf-field-code_editor').spf_field_code_editor()
1834 $this.children('.spf-field-fieldset').spf_field_fieldset()
1835 $this.children('.spf-field-group').spf_field_group()
1836 $this.children('.spf-field-icon').spf_field_icon()
1837 $this.children('.spf-field-slider').spf_field_slider()
1838 $this.children('.spf-field-sortable').spf_field_sortable()
1839 $this.children('.spf-field-sorter').spf_field_sorter()
1840 $this.children('.spf-field-spinner').spf_field_spinner()
1841 $this.children('.spf-field-switcher').spf_field_switcher()
1842 // $this.children('.spf-field-typography').spf_field_typography()
1843 $this.children('.spf-field-tabbed').spf_field_tabbed();
1844
1845
1846 // Field colors
1847 $this
1848 .children('.spf-field-border')
1849 .find('.spf-color')
1850 .spf_color()
1851 $this
1852 .children('.spf-field-background')
1853 .find('.spf-color')
1854 .spf_color()
1855 $this
1856 .children('.spf-field-color')
1857 .find('.spf-color')
1858 .spf_color()
1859 $this
1860 .children('.spf-field-color_group')
1861 .find('.spf-color')
1862 .spf_color()
1863 $this
1864 .children('.spf-field-link_color')
1865 .find('.spf-color')
1866 .spf_color()
1867 $this
1868 .children('.spf-field-typography')
1869 .find('.spf-color')
1870 .spf_color()
1871
1872 // Field chosenjs
1873 $this
1874 .children('.spf-field-select')
1875 .find('.spf-chosen')
1876 .spf_chosen()
1877
1878 // Field Checkbox
1879 $this
1880 .children('.spf-field-checkbox')
1881 .find('.spf-checkbox')
1882 .spf_checkbox()
1883
1884 // Field Siblings
1885 $this
1886 .children('.spf-field-button_set')
1887 .find('.spf-siblings')
1888 .spf_siblings()
1889 $this
1890 .children('.spf-field-layout_preset')
1891 .find('.spf-siblings')
1892 .spf_siblings()
1893 $this
1894 .children('.spf-field-palette')
1895 .find('.spf-siblings')
1896 .spf_siblings()
1897
1898 // Help Tooltip
1899 $this
1900 .children('.spf-field')
1901 .find('.spf-help')
1902 .spf_help()
1903 $('.pcp-admin-header').find('.spf-support-area')
1904 .spf_help()
1905
1906 if (settings.dependency) {
1907 $this.spf_dependency()
1908 }
1909
1910 $this.data('inited', true)
1911
1912 $(document).trigger('spf-reload-script', $this)
1913 }
1914 })
1915 }
1916
1917 //
1918 // Document ready and run scripts.
1919 //
1920 $(document).ready(function () {
1921 $('.spf-save').spf_save()
1922 $('.spf-confirm').spf_confirm()
1923 $('.spf-nav-options').spf_nav_options()
1924 $('.spf-nav-metabox').spf_nav_metabox()
1925 $('.spf-expand-all').spf_expand_all()
1926 $('.spf-search').spf_search()
1927 $('.spf-sticky-header').spf_sticky()
1928 $('.spf-page-templates').spf_page_templates()
1929 $('.spf-post-formats').spf_post_formats()
1930 $('.spf-onload').spf_reload_script()
1931 $('.widget').spf_widgets()
1932 })
1933
1934 // ======================================================
1935 // Post
1936 // ------------------------------------------------------
1937 // Trigger taxonomy list when post type is selected.
1938 $('.sp_pcp_post_type select').on('change', function (event) {
1939 event.preventDefault()
1940 var data = {
1941 action: 'sps_get_taxonomies',
1942 pcp_post_types: $(this).val(),
1943 nonce: $('#spf_pcp_metabox_noncesp_pcp_view_options').val()
1944 }
1945 $.post(ajaxurl, data, function (response) {
1946 $('.sp_pcp_post_taxonomy select').html(response)
1947 $('.sp_pcp_post_taxonomy select').trigger('chosen:updated')
1948 })
1949 })
1950 // Update terms list on the change of post taxonomy.
1951 $(document).on('change', '.sp_pcp_post_taxonomy select', function (e) {
1952 e.preventDefault();
1953 var taxSelector = $(this)
1954 var data = {
1955 action: 'sps_get_terms', // Callback function.
1956 pcp_post_taxonomy: $(this).val(),
1957 nonce: $('#spf_pcp_metabox_noncesp_pcp_view_options').val()
1958 }
1959 $.ajax({
1960 type: "POST",
1961 url: ajaxurl,
1962 data: data,
1963 error: function (response) {
1964 console.log(response)
1965 },
1966 success: function (response) {
1967 $(taxSelector)
1968 .parent()
1969 .parent('.sp_pcp_post_taxonomy')
1970 .next('.sp_pcp_taxonomy_terms')
1971 .find('select')
1972 .html(response)
1973 $(taxSelector)
1974 .parent()
1975 .parent('.sp_pcp_post_taxonomy')
1976 .next('.sp_pcp_taxonomy_terms')
1977 .find('select')
1978 .trigger('chosen:updated')
1979 }
1980 })
1981 })
1982
1983 // Populate specific post list in the drop-down.
1984 var pt_saved_value = $('.sp_pcp_post_type select').val()
1985 // Change Include only post_type on change of Post Type field.
1986 var data_include_only = $('.sp_pcp_include_only_posts .spf-chosen-ajax').data(
1987 'chosen-settings'
1988 )
1989 // Change Exclude post_type on change of Post Type field.
1990 var data_exclude = $('.sp_pcp_exclude_posts .spf-chosen-ajax').data(
1991 'chosen-settings'
1992 )
1993 if ($('.spf-nav').hasClass('spf-nav-metabox')) {
1994 data_exclude.data.query_args.post_type = pt_saved_value
1995 data_include_only.data.query_args.post_type = pt_saved_value
1996 }
1997 $('.sp_pcp_post_type select').on('change', function (event) {
1998 event.preventDefault()
1999 var pt_value = $(this).val()
2000 // Change Include only post_type on change of Post Type field.
2001 data_include_only.data.query_args.post_type = pt_value
2002 // Change Exclude post_type on change of Post Type field.
2003 data_exclude.data.query_args.post_type = pt_value
2004 })
2005
2006 /* Keep the accordion field's first item opened */
2007 $(window).on('load', function () {
2008 $('.pcp-opened-accordion').each(function () {
2009 if (!$(this).hasClass('hidden')) {
2010 $(this).addClass('pcp_saved_filter')
2011 }
2012 })
2013 })
2014 $('.spf-field-checkbox.pcp_advanced_filter').on('change', function (event) {
2015 $('.pcp-opened-accordion').each(function () {
2016 if ($(this).hasClass('hidden')) {
2017 $(this).removeClass('pcp_saved_filter')
2018 } else {
2019 $(this).addClass('pcp_saved_filter')
2020 }
2021 if (!$(this).hasClass('pcp_saved_filter')) {
2022 if (
2023 $(this)
2024 .find('.spf-accordion-title')
2025 .siblings('.spf-accordion-content')
2026 .hasClass('spf-accordion-open')
2027 ) {
2028 $(this).find('.spf-accordion-title')
2029 } else {
2030 $(this)
2031 .find('.spf-accordion-title')
2032 .trigger('click')
2033 $(this)
2034 .find('.spf-accordion-content')
2035 .find('.spf-cloneable-add')
2036 .trigger('click')
2037 }
2038 }
2039 })
2040 })
2041
2042 // Pro only tag.
2043 $('.pcp-pagination-type li:nth-child(n+2) input').attr('disabled', true)
2044 $("label:contains((Pro))").css({ 'pointer-events': 'none', 'opacity': '0.5', 'user-select': 'none' })
2045 $("label:contains((Pro)) input").attr('disabled', true).css('opacity', '1')
2046 $("select option:contains((Pro))").attr('disabled', true).css('opacity', '1')
2047 // Image custom size.
2048 // Post Meta fields 5th to last.
2049 // Post content type with limit.
2050
2051 // $('.post_content_sorter .spf--sortable-item:has(div.pcp-pro-only)').css({ "pointer-events": "none", "borderColor": "#bebebe" });
2052 // $('.post_content_sorter .spf--sortable-item:has(div.pcp-pro-only)').find('h4.spf-accordion-title, .spf--sortable-helper').css({ "backgroundColor": "#f0f0f0", "color": "#777", "marginLeft": "0", "paddingRight": "0" });
2053 // $('.post_content_sorter .spf--sortable-item:has(div.pcp-pro-only)').find('.spf--sortable-helper .fa').css("color", "#777");
2054
2055
2056
2057 var preview_box = $('#spsp-preview-box');
2058 var pcp_display = $('#sp_pcp_display').hide();
2059
2060 // $('#spsp-show-preview:contains(Hide)').
2061 $(document).on('click', '#spsp-show-preview:contains(Hide)', function (e) {
2062 e.preventDefault();
2063 var _this = $(this);
2064 _this.html('<i class="fa fa-eye" aria-hidden="true"></i> Show Preview');
2065 preview_box.html('');
2066 pcp_display.hide();
2067 });
2068 $(document).on('click', '#spsp-show-preview:not(:contains(Hide))', function (e) {
2069 e.preventDefault();
2070 var previewJS = window.spf_vars.previewJS;
2071 var _data = $('form#post').serialize();
2072 var _this = $(this);
2073 var $sp_id = $(this).data('id');
2074 var data = {
2075 action: 'spf_preview_meta_box',
2076 id: $sp_id,
2077 data: _data,
2078 nonce: $('#spf_pcp_metabox_noncesp_pcp_view_options').val()
2079 };
2080 $.ajax({
2081 type: "POST",
2082 url: ajaxurl,
2083 data: data,
2084 error: function (response) {
2085 console.log(response)
2086 },
2087 success: function (response) {
2088 pcp_display.show();
2089 preview_box.html(response);
2090 $.getScript(previewJS, function () {
2091 _this.html('<i class="fa fa-eye-slash" aria-hidden="true"></i> Hide Preview');
2092 $(document).on('change', '#sp_pcp_view_options', function (e) {
2093 e.preventDefault();
2094 _this.html('<i class="fa fa-refresh" aria-hidden="true"></i> Update Preview');
2095 });
2096 $(document).on('change', '#spf-section-sp_pcp_layouts_1', function (e) {
2097 e.preventDefault();
2098 _this.html('<i class="fa fa-refresh" aria-hidden="true"></i> Update Preview');
2099 });
2100 $("html, body").animate({ scrollTop: pcp_display.offset().top - 50 }, "slow");
2101 })
2102 }
2103 })
2104 });
2105
2106 function isValidJSONString(str) {
2107 try {
2108 JSON.parse(str);
2109 } catch (e) {
2110 return false;
2111 }
2112 return true;
2113 }
2114 // Smart-post-show export.
2115 var $export_type = $('.pcp_what_export').find('input:checked').val();
2116 $('.pcp_what_export').on('change', function () {
2117 $export_type = $(this).find('input:checked').val();
2118 });
2119
2120 $('.pcp_export .spf--button').on('click', function (event) {
2121 event.preventDefault();
2122
2123 var $shortcode_ids = $('.pcp_post_ids select').val();
2124 var $ex_nonce = $('#spf_options_noncesp_post_carousel_tools').val();
2125 var selected_shortcode = $export_type === 'selected_shortcodes' ? $shortcode_ids : 'all_shortcodes';
2126 if ($export_type === 'all_shortcodes' || $export_type === 'selected_shortcodes') {
2127 var data = {
2128 action: 'pcp_export_shortcodes',
2129 pcp_ids: selected_shortcode,
2130 nonce: $ex_nonce,
2131 }
2132 } else {
2133 $('.spf-form-result.spf-form-success').text('No carousel selected.').show();
2134 setTimeout(function () {
2135 $('.spf-form-result.spf-form-success').hide().text('');
2136 }, 3000);
2137 }
2138 $.post(ajaxurl, data, function (resp) {
2139 if (resp) {
2140 // Convert JSON Array to string.
2141 if (isValidJSONString(resp)) {
2142 var json = JSON.stringify(JSON.parse(resp));
2143 } else {
2144 var json = JSON.stringify(resp);
2145 }
2146 // Convert JSON string to BLOB.
2147 var blob = new Blob([json], { type: 'application/json' });
2148 var link = document.createElement('a');
2149 var pcp_time = $.now();
2150 link.href = window.URL.createObjectURL(blob);
2151 link.download = "smart-post-show-" + pcp_time + ".json";
2152 link.click();
2153 $('.spf-form-result.spf-form-success').text('Exported successfully!').show();
2154 setTimeout(function () {
2155 $('.spf-form-result.spf-form-success').hide().text('');
2156 $('.pcp_post_ids select').val('').trigger('chosen:updated');
2157 }, 3000);
2158 }
2159 });
2160 });
2161 // smart-post-show import.
2162 $('.pcp_import button.import').on('click', function (event) {
2163 event.preventDefault();
2164 var $this = $(this),
2165 this_text = $this.text(),
2166 pcp_shortcodes = $('#import').prop('files')[0];
2167 if ($('#import').val() != '') {
2168 $this.prop('disabled', true).css('opacity', '0.7');
2169 var $im_nonce = $('#spf_options_noncesp_post_carousel_tools').val();
2170 var reader = new FileReader();
2171 reader.readAsText(pcp_shortcodes);
2172 reader.onload = function (event) {
2173 var jsonObj = JSON.stringify(event.target.result);
2174 $.ajax({
2175 url: ajaxurl,
2176 type: 'POST',
2177 data: {
2178 shortcode: jsonObj,
2179 action: 'pcp_import_shortcodes',
2180 nonce: $im_nonce,
2181 },
2182 success: function (resp) {
2183 $('.spf-form-result.spf-form-success').text('Imported successfully!').show();
2184 setTimeout(function () {
2185 $('.spf-form-result.spf-form-success').hide().text('');
2186 $('#import').val('');
2187 $this.prop('disabled', false).css('opacity', '1');
2188 window.location.replace($('#pcp_shortcode_link_redirect').attr('href'));
2189 }, 2000);
2190 },
2191 error: function (error) {
2192 $('#import').val('');
2193 $this.prop('disabled', false).css('opacity', '1');
2194 }
2195 });
2196 }
2197 } else {
2198 $('.spf-form-result.spf-form-success').text('No exported json file chosen.').show();
2199 setTimeout(function () {
2200 $('.spf-form-result.spf-form-success').hide().text('');
2201 }, 3000);
2202 }
2203 });
2204
2205 $(document).on('keyup change', '.sp_post_carousel_page_pcp_settings #spf-form', function (e) {
2206 e.preventDefault();
2207 var $button = $(this).find('.spf-save');
2208 $button.css({ "background-color": "#00C263", "pointer-events": "initial" }).val('Save Settings');
2209 });
2210 $('.sp_post_carousel_page_pcp_settings .spf-save').on('click', function (e) {
2211 e.preventDefault();
2212 $(this).css({ "background-color": "#C5C5C6", "pointer-events": "none" }).val('Changes Saved');
2213 })
2214 // Function to update icon type
2215 function updateNavPosition(selector, regex, type) {
2216 var str = "";
2217 $(selector + ' option:selected').each(function () {
2218 str = $(this).val();
2219 });
2220 var src = $(selector + ' .spf-fieldset img').attr('src');
2221 var result = src.match(regex);
2222 if (result && result[1]) {
2223 src = src.replace(result[1], str);
2224 $(selector + ' .spf-fieldset img').attr('src', src);
2225 }
2226 }
2227 $('.pcp_carousel_nav_position').on('change', function () {
2228 updateNavPosition(".pcp_carousel_nav_position", /nav-position\/(.+)\.svg/, 'top_right');
2229 });
2230 if ($('.pcp_carousel_nav_position').length > 0) {
2231 updateNavPosition(".pcp_carousel_nav_position", /nav-position\/(.+)\.svg/, 'top_right');
2232 }
2233
2234 })(jQuery, window, document)