PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 2.4.26
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v2.4.26
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 2.4.26, at admin/views/sp-framework/assets/js/spf.js

2,651 lines 66.0 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 } else {
527 $icon.removeClass('fa-angle-down').addClass('fa-angle-right')
528 }
529
530 if (!$content.data('opened')) {
531 $content.spf_reload_script()
532 $content.data('opened', true)
533 }
534
535 $content.toggleClass('spf-accordion-open')
536 })
537 /* var $sortable = $(this).find('.spf-accordion-items');
538 $sortable.sortable({
539 axis: 'y',
540 helper: 'original',
541 cursor: 'move',
542 placeholder: 'widget-placeholder',
543 update: function( event, ui ) {
544 $sortable.spf_customizer_refresh();
545 }
546 });
547 $sortable.find('.spf-accordion-content').spf_reload_script();*/
548 })
549 }
550
551 //
552 // Field: sortable
553 //
554 $.fn.spf_field_sortable = function () {
555 return this.each(function () {
556 var $sortable = $(this).find('.spf--sortable')
557 // Post content sort only with handle
558 if ($(this).hasClass('post_content_sorter')) {
559 $sortable.sortable({
560 axis: 'y',
561 helper: 'original',
562 handle: '.spf--sortable-helper',
563 cursor: 'move',
564 placeholder: 'widget-placeholder'
565 // update: function( event, ui ) {
566 // $sortable.spf_customizer_refresh();
567 // }
568 })
569 } else {
570 $sortable.sortable({
571 axis: 'y',
572 helper: 'original',
573 cursor: 'move',
574 placeholder: 'widget-placeholder'
575 // update: function( event, ui ) {
576 // $sortable.spf_customizer_refresh();
577 // }
578 })
579 }
580
581 $sortable.find('.spf--sortable-content').spf_reload_script()
582 })
583 }
584
585 //
586 // Field: code_editor
587 //
588 $.fn.spf_field_code_editor = function () {
589 return this.each(function () {
590 if (typeof CodeMirror !== 'function') {
591 return
592 }
593
594 var $this = $(this),
595 $textarea = $this.find('textarea'),
596 $inited = $this.find('.CodeMirror'),
597 data_editor = $textarea.data('editor')
598
599 if ($inited.length) {
600 $inited.remove()
601 }
602
603 var interval = setInterval(function () {
604 if ($this.is(':visible')) {
605 var code_editor = CodeMirror.fromTextArea($textarea[0], data_editor)
606
607 // load code-mirror theme css.
608 if (
609 data_editor.theme !== 'default' &&
610 SP_PC.vars.code_themes.indexOf(data_editor.theme) === -1
611 ) {
612 var $cssLink = $('<link>')
613
614 $('#spf-codemirror-css').after($cssLink)
615
616 $cssLink.attr({
617 rel: 'stylesheet',
618 id: 'spf-codemirror-' + data_editor.theme + '-css',
619 href:
620 data_editor.cdnURL + '/theme/' + data_editor.theme + '.min.css',
621 type: 'text/css',
622 media: 'all'
623 })
624
625 SP_PC.vars.code_themes.push(data_editor.theme)
626 }
627
628 CodeMirror.modeURL = data_editor.cdnURL + '/mode/%N/%N.min.js'
629 CodeMirror.autoLoadMode(code_editor, data_editor.mode)
630
631 code_editor.on('change', function (editor, event) {
632 $textarea.val(code_editor.getValue()).trigger('change')
633 })
634
635 clearInterval(interval)
636 }
637 })
638 })
639 }
640
641 //
642 // Field: fieldset
643 //
644 $.fn.spf_field_fieldset = function () {
645 return this.each(function () {
646 $(this)
647 .find('.spf-fieldset-content')
648 .spf_reload_script()
649 })
650 }
651
652 //
653 // Field: group
654 //
655 $.fn.spf_field_group = function () {
656 return this.each(function () {
657 var $this = $(this),
658 $fieldset = $this.children('.spf-fieldset'),
659 $group = $fieldset.length ? $fieldset : $this,
660 $wrapper = $group.children('.spf-cloneable-wrapper'),
661 $hidden = $group.children('.spf-cloneable-hidden'),
662 $max = $group.children('.spf-cloneable-max'),
663 $min = $group.children('.spf-cloneable-min'),
664 field_id = $wrapper.data('field-id'),
665 unique_id = $wrapper.data('unique-id'),
666 is_number = Boolean(Number($wrapper.data('title-number'))),
667 max = parseInt($wrapper.data('max')),
668 min = parseInt($wrapper.data('min'))
669
670 // clear accordion arrows if multi-instance
671 if ($wrapper.hasClass('ui-accordion')) {
672 $wrapper.find('.ui-accordion-header-icon').remove()
673 }
674
675 var update_title_numbers = function ($selector) {
676 $selector.find('.spf-cloneable-title-number').each(function (index) {
677 $(this).html(
678 $(this)
679 .closest('.spf-cloneable-item')
680 .index() +
681 1 +
682 '.'
683 )
684 })
685 }
686
687 // Hide the taxonomy relation field. -- Shamim
688 var $pcp_group_parent = $this.parent()
689 var pcp_hide_the_taxonomy_relation = function (count_tax) {
690 if (count_tax < 2) {
691 $($pcp_group_parent)
692 .find('.pcp_relate_among_taxonomies')
693 .hide()
694 } else {
695 $($pcp_group_parent)
696 .find('.pcp_relate_among_taxonomies')
697 .show()
698 }
699 }
700
701 var count = $wrapper.children('.spf-cloneable-item').length
702 pcp_hide_the_taxonomy_relation(count)
703
704 $wrapper.accordion({
705 header: '> .spf-cloneable-item > .spf-cloneable-title',
706 collapsible: true,
707 active: false,
708 animate: false,
709 heightStyle: 'content',
710 icons: {
711 header: 'spf-cloneable-header-icon fa fa-angle-right',
712 activeHeader: 'spf-cloneable-header-icon fa fa-angle-down'
713 },
714 activate: function (event, ui) {
715 var $panel = ui.newPanel
716 var $header = ui.newHeader
717
718 if ($panel.length && !$panel.data('opened')) {
719 var $fields = $panel.children()
720 var $first = $fields
721 .first()
722 .find('select')
723 .first()
724 var $title = $header.find('.spf-cloneable-value')
725
726 $first.on('change', function (event) {
727 //$title.text( $first.children("option").filter(":selected").text() );
728 $title.text($first.val())
729 })
730
731 $panel.spf_reload_script()
732 $panel.data('opened', true)
733 $panel.data('retry', false)
734 } else if ($panel.data('retry')) {
735 $panel.spf_reload_script_retry()
736 $panel.data('retry', false)
737 }
738 }
739 })
740
741 $wrapper.sortable({
742 axis: 'y',
743 handle: '.spf-cloneable-title,.spf-cloneable-sort',
744 helper: 'original',
745 cursor: 'move',
746 placeholder: 'widget-placeholder',
747 start: function (event, ui) {
748 $wrapper.accordion({ active: false })
749 $wrapper.sortable('refreshPositions')
750 ui.item.children('.spf-cloneable-content').data('retry', true)
751 },
752 update: function (event, ui) {
753 SP_PC.helper.name_nested_replace(
754 $wrapper.children('.spf-cloneable-item'),
755 field_id
756 )
757
758 //$wrapper.spf_customizer_refresh();
759
760 if (is_number) {
761 update_title_numbers($wrapper)
762 }
763 }
764 })
765
766 $group.children('.spf-cloneable-add').on('click', function (e) {
767 e.preventDefault()
768
769 var count = $wrapper.children('.spf-cloneable-item').length
770
771 $min.hide()
772
773 if (max && count + 1 > max) {
774 $max.show()
775 return
776 }
777
778 var new_field_id = unique_id + field_id + '[' + count + ']'
779
780 var $cloned_item = $hidden.spf_clone(true)
781
782 $cloned_item.removeClass('spf-cloneable-hidden')
783 // $cloned_item.find(':input').each( function() {
784 // this.name = new_field_id + this.name.replace( ( this.name.startsWith('_nonce') ? '_nonce' : unique_id ), '');
785 // });
786 $cloned_item.find(':input[name!="_pseudo"]').each(function () {
787 this.name =
788 new_field_id +
789 this.name.replace(
790 this.name.startsWith('_nonce') ? '_nonce' : unique_id,
791 ''
792 )
793 })
794 $cloned_item.find('.spf-data-wrapper').each(function () {
795 $(this).attr('data-unique-id', new_field_id)
796 })
797 $wrapper.append($cloned_item)
798 $wrapper.accordion('refresh')
799 $wrapper.accordion({ active: count })
800 // $wrapper.spf_customizer_refresh();
801 // $wrapper.spf_customizer_listen({closest: true});
802
803 if (is_number) {
804 update_title_numbers($wrapper)
805 }
806 pcp_hide_the_taxonomy_relation(count + 1)
807 })
808
809 var event_clone = function (e) {
810 e.preventDefault()
811
812 var count = $wrapper.children('.spf-cloneable-item').length
813
814 $min.hide()
815
816 if (max && count + 1 > max) {
817 $max.show()
818 return
819 }
820
821 var $this = $(this),
822 $parent = $this.parent().parent(),
823 $cloned_helper = $parent
824 .children('.spf-cloneable-helper')
825 .spf_clone(true),
826 $cloned_title = $parent.children('.spf-cloneable-title').spf_clone(),
827 $cloned_content = $parent
828 .children('.spf-cloneable-content')
829 .spf_clone(),
830 cloned_regex = new RegExp(
831 '(' + SP_PC.helper.preg_quote(field_id) + ')\\[(\\d+)\\]',
832 'g'
833 )
834
835 $cloned_content.find('.spf-data-wrapper').each(function () {
836 var $this = $(this)
837 $this.attr(
838 'data-unique-id',
839 $this
840 .attr('data-unique-id')
841 .replace(
842 cloned_regex,
843 field_id + '[' + ($parent.index() + 1) + ']'
844 )
845 )
846 })
847
848 var $cloned = $('<div class="spf-cloneable-item" />')
849 $cloned.append($cloned_helper)
850 $cloned.append($cloned_title)
851 $cloned.append($cloned_content)
852
853 $wrapper
854 .children()
855 .eq($parent.index())
856 .after($cloned)
857
858 SP_PC.helper.name_nested_replace(
859 $wrapper.children('.spf-cloneable-item'),
860 field_id
861 )
862
863 $wrapper.accordion('refresh')
864 // $wrapper.spf_customizer_refresh();
865 // $wrapper.spf_customizer_listen({closest: true});
866
867 if (is_number) {
868 update_title_numbers($wrapper)
869 }
870 pcp_hide_the_taxonomy_relation(count + 1)
871 }
872
873 $wrapper
874 .children('.spf-cloneable-item')
875 .children('.spf-cloneable-helper')
876 .on('click', '.spf-cloneable-clone', event_clone)
877 $group
878 .children('.spf-cloneable-hidden')
879 .children('.spf-cloneable-helper')
880 .on('click', '.spf-cloneable-clone', event_clone)
881 var event_remove = function (e) {
882 e.preventDefault()
883
884 var count = $wrapper.children('.spf-cloneable-item').length
885
886 $max.hide()
887 $min.hide()
888
889 if (min && count - 1 < min) {
890 $min.show()
891 return
892 }
893
894 $(this)
895 .closest('.spf-cloneable-item')
896 .remove()
897
898 SP_PC.helper.name_nested_replace(
899 $wrapper.children('.spf-cloneable-item'),
900 field_id
901 )
902
903 //$wrapper.spf_customizer_refresh();
904
905 if (is_number) {
906 update_title_numbers($wrapper)
907 }
908 pcp_hide_the_taxonomy_relation(count - 1)
909 }
910 $wrapper
911 .children('.spf-cloneable-item')
912 .children('.spf-cloneable-helper')
913 .on('click', '.spf-cloneable-remove', event_remove)
914 $group
915 .children('.spf-cloneable-hidden')
916 .children('.spf-cloneable-helper')
917 .on('click', '.spf-cloneable-remove', event_remove)
918 })
919 }
920
921 //
922 // Field: icon
923 //
924 $.fn.spf_field_icon = function () {
925 return this.each(function () {
926 var $this = $(this)
927
928 $this.on('click', '.spf-icon-add', function (e) {
929 e.preventDefault()
930
931 var $button = $(this)
932 var $modal = $('#spf-modal-icon')
933
934 $modal.show()
935
936 SP_PC.vars.$icon_target = $this
937
938 if (!SP_PC.vars.icon_modal_loaded) {
939 $modal.find('.spf-modal-loading').show()
940
941 // window.wp.ajax.post( 'spf-get-icons', { nonce: $button.data('nonce') } ).done( function( response ) {
942 window.wp.ajax
943 .post('spf-get-icons', {
944 nonce: $button.data('nonce')
945 })
946 .done(function (response) {
947 $modal.find('.spf-modal-loading').hide()
948
949 SP_PC.vars.icon_modal_loaded = true
950
951 var $load = $modal.find('.spf-modal-load').html(response.content)
952
953 $load.on('click', 'a', function (e) {
954 e.preventDefault()
955
956 var icon = $(this).data('spf-icon')
957
958 SP_PC.vars.$icon_target
959 .find('i')
960 .removeAttr('class')
961 .addClass(icon)
962 SP_PC.vars.$icon_target
963 .find('input')
964 .val(icon)
965 .trigger('change')
966 SP_PC.vars.$icon_target
967 .find('.spf-icon-preview')
968 .removeClass('hidden')
969 SP_PC.vars.$icon_target
970 .find('.spf-icon-remove')
971 .removeClass('hidden')
972
973 $modal.hide()
974 })
975
976 $modal.on('change keyup', '.spf-icon-search', function () {
977 var value = $(this).val(),
978 $icons = $load.find('a')
979
980 $icons.each(function () {
981 var $elem = $(this)
982
983 if (
984 $elem.data('spf-icon').search(new RegExp(value, 'i')) < 0
985 ) {
986 $elem.hide()
987 } else {
988 $elem.show()
989 }
990 })
991 })
992
993 $modal.on(
994 'click',
995 '.spf-modal-close, .spf-modal-overlay',
996 function () {
997 $modal.hide()
998 }
999 )
1000 })
1001 .fail(function (response) {
1002 $modal.find('.spf-modal-loading').hide()
1003 $modal.find('.spf-modal-load').html(response.error)
1004 $modal.on('click', function () {
1005 $modal.hide()
1006 })
1007 })
1008 }
1009 })
1010
1011 $this.on('click', '.spf-icon-remove', function (e) {
1012 e.preventDefault()
1013 $this.find('.spf-icon-preview').addClass('hidden')
1014 $this
1015 .find('input')
1016 .val('')
1017 .trigger('change')
1018 $(this).addClass('hidden')
1019 })
1020 })
1021 }
1022
1023 //
1024 // Field: slider
1025 //
1026 $.fn.spf_field_slider = function () {
1027 return this.each(function () {
1028 var $this = $(this),
1029 $input = $this.find('input'),
1030 $slider = $this.find('.spf-slider-ui'),
1031 data = $input.data(),
1032 value = $input.val() || 0
1033
1034 if ($slider.hasClass('ui-slider')) {
1035 $slider.empty()
1036 }
1037
1038 $slider.slider({
1039 range: 'min',
1040 value: value,
1041 min: data.min,
1042 max: data.max,
1043 step: data.step,
1044 slide: function (e, o) {
1045 $input.val(o.value).trigger('change')
1046 }
1047 })
1048
1049 $input.keyup(function () {
1050 $slider.slider('value', $input.val())
1051 })
1052 })
1053 }
1054
1055 //
1056 // Field: c
1057 //
1058 $.fn.spf_field_sorter = function () {
1059 return this.each(function () {
1060 var $this = $(this),
1061 $enabled = $this.find('.spf-enabled'),
1062 $has_disabled = $this.find('.spf-disabled'),
1063 $disabled = $has_disabled.length ? $has_disabled : false
1064
1065 $enabled.sortable({
1066 connectWith: $disabled,
1067
1068 placeholder: 'ui-sortable-placeholder',
1069
1070 update: function (event, ui) {
1071 var $el = ui.item.find('input')
1072
1073 if (ui.item.parent().hasClass('spf-enabled')) {
1074 $el.attr('name', $el.attr('name').replace('disabled', 'enabled'))
1075 } else {
1076 $el.attr('name', $el.attr('name').replace('enabled', 'disabled'))
1077 }
1078
1079 // $this.spf_customizer_refresh();
1080 }
1081 })
1082
1083 if ($disabled) {
1084 $disabled.sortable({
1085 connectWith: $enabled,
1086 placeholder: 'ui-sortable-placeholder'
1087 // update: function( event, ui ) {
1088 // $this.spf_customizer_refresh();
1089 // }
1090 })
1091 }
1092 })
1093 }
1094
1095 //
1096 // Field: spinner
1097 //
1098 $.fn.spf_field_spinner = function () {
1099 return this.each(function () {
1100 var $this = $(this),
1101 $input = $this.find('input'),
1102 $inited = $this.find('.ui-spinner-button'),
1103 $unit = $input.data('unit')
1104
1105 if ($inited.length) {
1106 $inited.remove()
1107 }
1108
1109 $input.spinner({
1110 max: $input.data('max') || 100,
1111 min: $input.data('min') || 0,
1112 step: $input.data('step') || 1,
1113 create: function (event, ui) {
1114 if ($unit.length) {
1115 $this
1116 .find('.ui-spinner-up')
1117 .after(
1118 '<span class="ui-button-text-only spf--unit">' +
1119 $unit +
1120 '</span>'
1121 )
1122 }
1123 },
1124 spin: function (event, ui) {
1125 $input.val(ui.value).trigger('change')
1126 }
1127 })
1128 })
1129 }
1130
1131 //
1132 // Field: switcher
1133 //
1134 $.fn.spf_field_switcher = function () {
1135 return this.each(function () {
1136 var $switcher = $(this).find('.spf--switcher')
1137
1138 $switcher.on('click', function () {
1139 var value = 0
1140 var $input = $switcher.find('input')
1141
1142 if ($switcher.hasClass('spf--active')) {
1143 $switcher.removeClass('spf--active')
1144 } else {
1145 value = 1
1146 $switcher.addClass('spf--active')
1147 }
1148
1149 $input.val(value).trigger('change')
1150 })
1151 })
1152 }
1153
1154 //
1155 // Field: typography
1156 //
1157 $.fn.spf_field_typography = function () {
1158 return this.each(function () {
1159 var base = this
1160 var $this = $(this)
1161 var loaded_fonts = []
1162 var webfonts = spf_typography_json.webfonts
1163 var googlestyles = spf_typography_json.googlestyles
1164 var defaultstyles = spf_typography_json.defaultstyles
1165
1166 //
1167 //
1168 // Sanitize google font subset
1169 base.sanitize_subset = function (subset) {
1170 subset = subset.replace('-ext', ' Extended')
1171 subset = subset.charAt(0).toUpperCase() + subset.slice(1)
1172 return subset
1173 }
1174
1175 //
1176 //
1177 // Sanitize google font styles (weight and style)
1178 base.sanitize_style = function (style) {
1179 return googlestyles[style] ? googlestyles[style] : style
1180 }
1181
1182 //
1183 //
1184 // Load google font
1185 base.load_google_font = function (font_family, weight, style) {
1186 if (font_family && typeof WebFont === 'object') {
1187 weight = weight ? weight.replace('normal', '') : ''
1188 style = style ? style.replace('normal', '') : ''
1189
1190 if (weight || style) {
1191 font_family = font_family + ':' + weight + style
1192 }
1193
1194 if (loaded_fonts.indexOf(font_family) === -1) {
1195 WebFont.load({ google: { families: [font_family] } })
1196 }
1197
1198 loaded_fonts.push(font_family)
1199 }
1200 }
1201
1202 //
1203 //
1204 // Append select options
1205 base.append_select_options = function (
1206 $select,
1207 options,
1208 condition,
1209 type,
1210 is_multi
1211 ) {
1212 $select
1213 .find('option')
1214 .not(':first')
1215 .remove()
1216
1217 var opts = ''
1218
1219 $.each(options, function (key, value) {
1220 var selected
1221 var name = value
1222
1223 // is_multi
1224 if (is_multi) {
1225 selected =
1226 condition && condition.indexOf(value) !== -1 ? ' selected' : ''
1227 } else {
1228 selected = condition && condition === value ? ' selected' : ''
1229 }
1230
1231 if (type === 'subset') {
1232 name = base.sanitize_subset(value)
1233 } else if (type === 'style') {
1234 name = base.sanitize_style(value)
1235 }
1236
1237 opts +=
1238 '<option value="' +
1239 value +
1240 '"' +
1241 selected +
1242 '>' +
1243 name +
1244 '</option>'
1245 })
1246
1247 $select
1248 .append(opts)
1249 .trigger('spf.change')
1250 .trigger('chosen:updated')
1251 }
1252
1253 base.init = function () {
1254 //
1255 //
1256 // Constants
1257 var selected_styles = []
1258 var $typography = $this.find('.spf--typography')
1259 var $type = $this.find('.spf--type')
1260 var $styles = $this.find('.spf--block-font-style')
1261 var unit = $typography.data('unit')
1262 var exclude_fonts = $typography.data('exclude')
1263 ? $typography.data('exclude').split(',')
1264 : []
1265
1266 //
1267 //
1268 // Chosen init
1269 if ($this.find('.spf--chosen').length) {
1270 var $chosen_selects = $this.find('select')
1271
1272 $chosen_selects.each(function () {
1273 var $chosen_select = $(this),
1274 $chosen_inited = $chosen_select.parent().find('.chosen-container')
1275
1276 if ($chosen_inited.length) {
1277 $chosen_inited.remove()
1278 }
1279
1280 $chosen_select.chosen({
1281 allow_single_deselect: true,
1282 disable_search_threshold: 15,
1283 width: '100%'
1284 })
1285 })
1286 }
1287
1288 //
1289 //
1290 // Font family select
1291 var $font_family_select = $this.find('.spf--font-family')
1292 var first_font_family = $font_family_select.val()
1293
1294 // Clear default font family select options
1295 $font_family_select
1296 .find('option')
1297 .not(':first-child')
1298 .remove()
1299
1300 var opts = ''
1301
1302 $.each(webfonts, function (type, group) {
1303 // Check for exclude fonts
1304 if (exclude_fonts && exclude_fonts.indexOf(type) !== -1) {
1305 return
1306 }
1307
1308 opts += '<optgroup label="' + group.label + '">'
1309
1310 $.each(group.fonts, function (key, value) {
1311 // use key if value is object
1312 value = typeof value === 'object' ? key : value
1313 var selected = value === first_font_family ? ' selected' : ''
1314 opts +=
1315 '<option value="' +
1316 value +
1317 '" data-type="' +
1318 type +
1319 '"' +
1320 selected +
1321 '>' +
1322 value +
1323 '</option>'
1324 })
1325
1326 opts += '</optgroup>'
1327 })
1328
1329 // Append google font select options
1330 $font_family_select.append(opts).trigger('chosen:updated')
1331
1332 //
1333 //
1334 // Font style select
1335 var $font_style_block = $this.find('.spf--block-font-style')
1336
1337 if ($font_style_block.length) {
1338 var $font_style_select = $this.find('.spf--font-style-select')
1339 var first_style_value = $font_style_select.val()
1340 ? $font_style_select.val().replace(/normal/g, '')
1341 : ''
1342
1343 //
1344 // Font Style on on change listener
1345 $font_style_select.on('change spf.change', function (event) {
1346 var style_value = $font_style_select.val()
1347
1348 // set a default value
1349 if (
1350 !style_value &&
1351 selected_styles &&
1352 selected_styles.indexOf('normal') === -1
1353 ) {
1354 style_value = selected_styles[0]
1355 }
1356
1357 // set font weight, for eg. replacing 800italic to 800
1358 var font_normal =
1359 style_value &&
1360 style_value !== 'italic' &&
1361 style_value === 'normal'
1362 ? 'normal'
1363 : ''
1364 var font_weight =
1365 style_value &&
1366 style_value !== 'italic' &&
1367 style_value !== 'normal'
1368 ? style_value.replace('italic', '')
1369 : font_normal
1370 var font_style =
1371 style_value && style_value.substr(-6) === 'italic' ? 'italic' : ''
1372
1373 $this.find('.spf--font-weight').val(font_weight)
1374 $this.find('.spf--font-style').val(font_style)
1375 })
1376
1377 //
1378 //
1379 // Extra font style select
1380 var $extra_font_style_block = $this.find('.spf--block-extra-styles')
1381
1382 if ($extra_font_style_block.length) {
1383 var $extra_font_style_select = $this.find('.spf--extra-styles')
1384 var first_extra_style_value = $extra_font_style_select.val()
1385 }
1386 }
1387
1388 //
1389 //
1390 // Subsets select
1391 var $subset_block = $this.find('.spf--block-subset')
1392 if ($subset_block.length) {
1393 var $subset_select = $this.find('.spf--subset')
1394 var first_subset_select_value = $subset_select.val()
1395 var subset_multi_select = $subset_select.data('multiple') || false
1396 }
1397
1398 //
1399 //
1400 // Backup font family
1401 var $backup_font_family_block = $this.find(
1402 '.spf--block-backup-font-family'
1403 )
1404
1405 //
1406 //
1407 // Font Family on Change Listener
1408 $font_family_select
1409 .on('change spf.change', function (event) {
1410 // Hide subsets on change
1411 if ($subset_block.length) {
1412 $subset_block.addClass('hidden')
1413 }
1414
1415 // Hide extra font style on change
1416 if ($extra_font_style_block.length) {
1417 $extra_font_style_block.addClass('hidden')
1418 }
1419
1420 // Hide backup font family on change
1421 if ($backup_font_family_block.length) {
1422 $backup_font_family_block.addClass('hidden')
1423 }
1424
1425 var $selected = $font_family_select.find(':selected')
1426 var value = $selected.val()
1427 var type = $selected.data('type')
1428
1429 if (type && value) {
1430 // Show backup fonts if font type google or custom
1431 if (
1432 (type === 'google' || type === 'custom') &&
1433 $backup_font_family_block.length
1434 ) {
1435 $backup_font_family_block.removeClass('hidden')
1436 }
1437
1438 // Appending font style select options
1439 if ($font_style_block.length) {
1440 // set styles for multi and normal style selectors
1441 var styles = defaultstyles
1442
1443 // Custom or gogle font styles
1444 if (type === 'google' && webfonts[type].fonts[value][0]) {
1445 styles = webfonts[type].fonts[value][0]
1446 } else if (type === 'custom' && webfonts[type].fonts[value]) {
1447 styles = webfonts[type].fonts[value]
1448 }
1449
1450 selected_styles = styles
1451
1452 // Set selected style value for avoid load errors
1453 var set_auto_style =
1454 styles.indexOf('normal') !== -1 ? 'normal' : styles[0]
1455 var set_style_value =
1456 first_style_value && styles.indexOf(first_style_value) !== -1
1457 ? first_style_value
1458 : set_auto_style
1459
1460 // Append style select options
1461 base.append_select_options(
1462 $font_style_select,
1463 styles,
1464 set_style_value,
1465 'style'
1466 )
1467
1468 // Clear first value
1469 first_style_value = false
1470
1471 // Show style select after appended
1472 $font_style_block.removeClass('hidden')
1473
1474 // Appending extra font style select options
1475 if (
1476 type === 'google' &&
1477 $extra_font_style_block.length &&
1478 styles.length > 1
1479 ) {
1480 // Append extra-style select options
1481 base.append_select_options(
1482 $extra_font_style_select,
1483 styles,
1484 first_extra_style_value,
1485 'style',
1486 true
1487 )
1488
1489 // Clear first value
1490 first_extra_style_value = false
1491
1492 // Show style select after appended
1493 $extra_font_style_block.removeClass('hidden')
1494 }
1495 }
1496
1497 // Appending google fonts subsets select options
1498 if (
1499 type === 'google' &&
1500 $subset_block.length &&
1501 webfonts[type].fonts[value][1]
1502 ) {
1503 var subsets = webfonts[type].fonts[value][1]
1504 var set_auto_subset =
1505 subsets.length < 2 && subsets[0] !== 'latin' ? subsets[0] : ''
1506 var set_subset_value =
1507 first_subset_select_value &&
1508 subsets.indexOf(first_subset_select_value) !== -1
1509 ? first_subset_select_value
1510 : set_auto_subset
1511
1512 // check for multiple subset select
1513 set_subset_value =
1514 subset_multi_select && first_subset_select_value
1515 ? first_subset_select_value
1516 : set_subset_value
1517
1518 base.append_select_options(
1519 $subset_select,
1520 subsets,
1521 set_subset_value,
1522 'subset',
1523 subset_multi_select
1524 )
1525
1526 first_subset_select_value = false
1527
1528 $subset_block.removeClass('hidden')
1529 }
1530 } else {
1531 // Clear Styles
1532 $styles.find(':input').val('')
1533
1534 // Clear subsets options if type and value empty
1535 if ($subset_block.length) {
1536 $subset_select
1537 .find('option')
1538 .not(':first-child')
1539 .remove()
1540 $subset_select.trigger('chosen:updated')
1541 }
1542
1543 // Clear font styles options if type and value empty
1544 if ($font_style_block.length) {
1545 $font_style_select
1546 .find('option')
1547 .not(':first-child')
1548 .remove()
1549 $font_style_select.trigger('chosen:updated')
1550 }
1551 }
1552
1553 // Update font type input value
1554 $type.val(type)
1555 })
1556 .trigger('spf.change')
1557
1558 //
1559 //
1560 // Preview
1561 var $preview_block = $this.find('.spf--block-preview')
1562
1563 if ($preview_block.length) {
1564 var $preview = $this.find('.spf--preview')
1565
1566 // Set preview styles on change
1567 $this.on(
1568 'change',
1569 SP_PC.helper.debounce(function (event) {
1570 $preview_block.removeClass('hidden')
1571
1572 var font_family = $font_family_select.val(),
1573 font_weight = $this.find('.spf--font-weight').val(),
1574 font_style = $this.find('.spf--font-style').val(),
1575 font_size = $this.find('.spf--font-size').val(),
1576 font_variant = $this.find('.spf--font-variant').val(),
1577 line_height = $this.find('.spf--line-height').val(),
1578 text_align = $this.find('.spf--text-align').val(),
1579 text_transform = $this.find('.spf--text-transform').val(),
1580 text_decoration = $this.find('.spf--text-decoration').val(),
1581 text_color = $this.find('.spf--color').val(),
1582 word_spacing = $this.find('.spf--word-spacing').val(),
1583 letter_spacing = $this.find('.spf--letter-spacing').val(),
1584 custom_style = $this.find('.spf--custom-style').val(),
1585 type = $this.find('.spf--type').val()
1586
1587 if (type === 'google') {
1588 base.load_google_font(font_family, font_weight, font_style)
1589 }
1590
1591 var properties = {}
1592
1593 if (font_family) {
1594 properties.fontFamily = font_family
1595 }
1596 if (font_weight) {
1597 properties.fontWeight = font_weight
1598 }
1599 if (font_style) {
1600 properties.fontStyle = font_style
1601 }
1602 if (font_variant) {
1603 properties.fontVariant = font_variant
1604 }
1605 if (font_size) {
1606 properties.fontSize = font_size + unit
1607 }
1608 if (line_height) {
1609 properties.lineHeight = line_height + unit
1610 }
1611 if (letter_spacing) {
1612 properties.letterSpacing = letter_spacing + unit
1613 }
1614 if (word_spacing) {
1615 properties.wordSpacing = word_spacing + unit
1616 }
1617 if (text_align) {
1618 properties.textAlign = text_align
1619 }
1620 if (text_transform) {
1621 properties.textTransform = text_transform
1622 }
1623 if (text_decoration) {
1624 properties.textDecoration = text_decoration
1625 }
1626 if (text_color) {
1627 properties.color = text_color
1628 }
1629
1630 $preview.removeAttr('style')
1631
1632 // Customs style attribute
1633 if (custom_style) {
1634 $preview.attr('style', custom_style)
1635 }
1636
1637 $preview.css(properties)
1638 }, 100)
1639 )
1640
1641 // Preview black and white backgrounds trigger
1642 $preview_block.on('click', function () {
1643 $preview.toggleClass('spf--black-background')
1644
1645 var $toggle = $preview_block.find('.spf--toggle')
1646
1647 if ($toggle.hasClass('fa-toggle-off')) {
1648 $toggle.removeClass('fa-toggle-off').addClass('fa-toggle-on')
1649 } else {
1650 $toggle.removeClass('fa-toggle-on').addClass('fa-toggle-off')
1651 }
1652 })
1653
1654 if (!$preview_block.hasClass('hidden')) {
1655 $this.trigger('change')
1656 }
1657 }
1658 }
1659
1660 base.init()
1661 })
1662 }
1663
1664 //
1665 // Confirm
1666 //
1667 $.fn.spf_confirm = function () {
1668 return this.each(function () {
1669 $(this).on('click', function (e) {
1670 var confirm_text =
1671 $(this).data('confirm') || window.spf_vars.i18n.confirm
1672 var confirm_answer = confirm(confirm_text)
1673 SP_PC.vars.is_confirm = true
1674
1675 if (!confirm_answer) {
1676 e.preventDefault()
1677 SP_PC.vars.is_confirm = false
1678
1679 return false
1680 }
1681 })
1682 })
1683 }
1684
1685 $.fn.serializeObject = function () {
1686 var obj = {}
1687
1688 $.each(this.serializeArray(), function (i, o) {
1689 var n = o.name,
1690 v = o.value
1691
1692 obj[n] =
1693 obj[n] === undefined
1694 ? v
1695 : $.isArray(obj[n])
1696 ? obj[n].concat(v)
1697 : [obj[n], v]
1698 })
1699
1700 return obj
1701 }
1702
1703 //
1704 // Options Save
1705 //
1706 $.fn.spf_save = function () {
1707 return this.each(function () {
1708 var $this = $(this),
1709 $buttons = $('.spf-save'),
1710 $panel = $('.spf-options'),
1711 flooding = false,
1712 timeout
1713
1714 $this.on('click', function (e) {
1715 if (!flooding) {
1716 var $text = $this.data('save'),
1717 $value = $this.val()
1718
1719 $buttons.attr('value', $text)
1720
1721 if ($this.hasClass('spf-save-ajax')) {
1722 e.preventDefault()
1723
1724 $panel.addClass('spf-saving')
1725 $buttons.prop('disabled', true)
1726
1727 window.wp.ajax
1728 .post('spf_' + $panel.data('unique') + '_ajax_save', {
1729 data: $('#spf-form').serializeJSONSP_PC()
1730 })
1731 .done(function (response) {
1732 clearTimeout(timeout)
1733
1734 var $result_success = $('.spf-form-success')
1735
1736 $result_success
1737 .empty()
1738 .append(response.notice)
1739 .slideDown('fast', function () {
1740 timeout = setTimeout(function () {
1741 $result_success.slideUp('fast')
1742 }, 2000)
1743 })
1744
1745 // clear errors
1746 $('.spf-error').remove()
1747
1748 var $append_errors = $('.spf-form-error')
1749
1750 $append_errors.empty().hide()
1751
1752 if (Object.keys(response.errors).length) {
1753 var error_icon = '<i class="spf-label-error spf-error">!</i>'
1754
1755 $.each(response.errors, function (key, error_message) {
1756 var $field = $('[data-depend-id="' + key + '"]'),
1757 $link = $(
1758 '#spf-tab-link-' +
1759 ($field.closest('.spf-section').index() + 1)
1760 ),
1761 $tab = $link.closest('.spf-tab-depth-0')
1762
1763 $field
1764 .closest('.spf-fieldset')
1765 .append(
1766 '<p class="spf-text-error spf-error">' +
1767 error_message +
1768 '</p>'
1769 )
1770
1771 if (!$link.find('.spf-error').length) {
1772 $link.append(error_icon)
1773 }
1774
1775 if (!$tab.find('.spf-arrow .spf-error').length) {
1776 $tab.find('.spf-arrow').append(error_icon)
1777 }
1778
1779 console.log(error_message)
1780
1781 $append_errors.append(
1782 '<div>' + error_icon + ' ' + error_message + '</div>'
1783 )
1784 })
1785
1786 $append_errors.show()
1787 }
1788
1789 $panel.removeClass('spf-saving')
1790 $buttons.prop('disabled', false).attr('value', $value)
1791 flooding = false
1792 })
1793 .fail(function (response) {
1794 alert(response.error)
1795 })
1796 }
1797 }
1798
1799 flooding = true
1800 })
1801 })
1802 }
1803
1804 //
1805 // WP Color Picker
1806 //
1807 if (typeof Color === 'function') {
1808 Color.fn.toString = function () {
1809 if (this._alpha < 1) {
1810 return this.toCSS('rgba', this._alpha).replace(/\s+/g, '')
1811 }
1812
1813 var hex = parseInt(this._color, 10).toString(16)
1814
1815 if (this.error) {
1816 return ''
1817 }
1818
1819 if (hex.length < 6) {
1820 for (var i = 6 - hex.length - 1; i >= 0; i--) {
1821 hex = '0' + hex
1822 }
1823 }
1824
1825 return '#' + hex
1826 }
1827 }
1828
1829 SP_PC.funcs.parse_color = function (color) {
1830 var value = color.replace(/\s+/g, ''),
1831 trans =
1832 value.indexOf('rgba') !== -1
1833 ? parseFloat(value.replace(/^.*,(.+)\)/, '$1') * 100)
1834 : 100,
1835 rgba = trans < 100 ? true : false
1836
1837 return { value: value, transparent: trans, rgba: rgba }
1838 }
1839
1840 $.fn.spf_color = function () {
1841 return this.each(function () {
1842 var $input = $(this),
1843 picker_color = SP_PC.funcs.parse_color($input.val()),
1844 palette_color = window.spf_vars.color_palette.length
1845 ? window.spf_vars.color_palette
1846 : true,
1847 $container
1848
1849 // Destroy and Re-init.
1850 if ($input.hasClass('wp-color-picker')) {
1851 $input
1852 .closest('.wp-picker-container')
1853 .after($input)
1854 .remove()
1855 }
1856
1857 $input.wpColorPicker({
1858 palettes: palette_color,
1859 change: function (event, ui) {
1860 var ui_color_value = ui.color.toString()
1861
1862 $container.removeClass('spf--transparent-active')
1863 $container
1864 .find('.spf--transparent-offset')
1865 .css('background-color', ui_color_value)
1866 $input.val(ui_color_value).trigger('change')
1867 },
1868 create: function () {
1869 $container = $input.closest('.wp-picker-container')
1870
1871 var a8cIris = $input.data('a8cIris'),
1872 $transparent_wrap = $(
1873 '<div class="spf--transparent-wrap">' +
1874 '<div class="spf--transparent-slider"></div>' +
1875 '<div class="spf--transparent-offset"></div>' +
1876 '<div class="spf--transparent-text"></div>' +
1877 '<div class="spf--transparent-button">transparent <i class="fa fa-toggle-off"></i></div>' +
1878 '</div>'
1879 ).appendTo($container.find('.wp-picker-holder')),
1880 $transparent_slider = $transparent_wrap.find(
1881 '.spf--transparent-slider'
1882 ),
1883 $transparent_text = $transparent_wrap.find(
1884 '.spf--transparent-text'
1885 ),
1886 $transparent_offset = $transparent_wrap.find(
1887 '.spf--transparent-offset'
1888 ),
1889 $transparent_button = $transparent_wrap.find(
1890 '.spf--transparent-button'
1891 )
1892
1893 if ($input.val() === 'transparent') {
1894 $container.addClass('spf--transparent-active')
1895 }
1896
1897 $transparent_button.on('click', function () {
1898 if ($input.val() !== 'transparent') {
1899 $input
1900 .val('transparent')
1901 .trigger('change')
1902 .removeClass('iris-error')
1903 $container.addClass('spf--transparent-active')
1904 } else {
1905 $input.val(a8cIris._color.toString()).trigger('change')
1906 $container.removeClass('spf--transparent-active')
1907 }
1908 })
1909
1910 $transparent_slider.slider({
1911 value: picker_color.transparent,
1912 step: 1,
1913 min: 0,
1914 max: 100,
1915 slide: function (event, ui) {
1916 var slide_value = parseFloat(ui.value / 100)
1917 a8cIris._color._alpha = slide_value
1918 $input.wpColorPicker('color', a8cIris._color.toString())
1919 $transparent_text.text(
1920 slide_value === 1 || slide_value === 0 ? '' : slide_value
1921 )
1922 },
1923 create: function () {
1924 var slide_value = parseFloat(picker_color.transparent / 100),
1925 text_value = slide_value < 1 ? slide_value : ''
1926
1927 $transparent_text.text(text_value)
1928 $transparent_offset.css('background-color', picker_color.value)
1929
1930 $container.on('click', '.wp-picker-clear', function () {
1931 a8cIris._color._alpha = 1
1932 $transparent_text.text('')
1933 $transparent_slider.slider('option', 'value', 100)
1934 $container.removeClass('spf--transparent-active')
1935 $input.trigger('change')
1936 })
1937
1938 $container.on('click', '.wp-picker-default', function () {
1939 var default_color = SP_PC.funcs.parse_color(
1940 $input.data('default-color')
1941 ),
1942 default_value = parseFloat(default_color.transparent / 100),
1943 default_text = default_value < 1 ? default_value : ''
1944
1945 a8cIris._color._alpha = default_value
1946 $transparent_text.text(default_text)
1947 $transparent_slider.slider(
1948 'option',
1949 'value',
1950 default_color.transparent
1951 )
1952 })
1953 }
1954 })
1955 }
1956 })
1957 })
1958 }
1959
1960 //
1961 // Number (only allow numeric inputs)
1962 //
1963 $.fn.spf_number = function () {
1964 return this.each(function () {
1965 $(this).on('keypress', function (e) {
1966 if (
1967 e.keyCode !== 0 &&
1968 e.keyCode !== 8 &&
1969 e.keyCode !== 45 &&
1970 e.keyCode !== 46 &&
1971 (e.keyCode < 48 || e.keyCode > 57)
1972 ) {
1973 return false
1974 }
1975 })
1976 })
1977 }
1978
1979 //
1980 // ChosenJS
1981 //
1982 $.fn.spf_chosen = function () {
1983 return this.each(function () {
1984 var $this = $(this),
1985 $inited = $this.parent().find('.chosen-container'),
1986 is_sortable = $this.hasClass('spf-chosen-sortable') || false,
1987 is_ajax = $this.hasClass('spf-chosen-ajax') || false,
1988 is_multiple = $this.attr('multiple') || false,
1989 set_width = is_multiple ? 'auto' : 'auto',
1990 set_options = $.extend(
1991 {
1992 allow_single_deselect: true,
1993 disable_search_threshold: 10,
1994 width: set_width,
1995 no_results_text: window.spf_vars.i18n.no_results_text
1996 },
1997 $this.data('chosen-settings')
1998 )
1999
2000 if ($inited.length) {
2001 $inited.remove()
2002 }
2003
2004 // Chosen ajax
2005 if (is_ajax) {
2006 var set_ajax_options = $.extend(
2007 {
2008 data: {
2009 type: 'post',
2010 nonce: ''
2011 },
2012 allow_single_deselect: true,
2013 disable_search_threshold: -1,
2014 width: '100%',
2015 min_length: 3,
2016 type_delay: 500,
2017 typing_text: window.spf_vars.i18n.typing_text,
2018 searching_text: window.spf_vars.i18n.searching_text,
2019 no_results_text: window.spf_vars.i18n.no_results_text
2020 },
2021 $this.data('chosen-settings')
2022 )
2023
2024 $this.SP_PCAjaxChosen(set_ajax_options)
2025 } else {
2026 $this.chosen(set_options)
2027 }
2028
2029 // Chosen keep options order
2030 if (is_multiple) {
2031 var $hidden_select = $this.parent().find('.spf-hidden-select')
2032 var $hidden_value = $hidden_select.val() || []
2033
2034 $this.on('change', function (obj, result) {
2035 if (result && result.selected) {
2036 $hidden_select.append(
2037 '<option value="' +
2038 result.selected +
2039 '" selected="selected">' +
2040 result.selected +
2041 '</option>'
2042 )
2043 } else if (result && result.deselected) {
2044 $hidden_select
2045 .find('option[value="' + result.deselected + '"]')
2046 .remove()
2047 }
2048
2049 // Force customize refresh
2050 if (
2051 $hidden_select.children().length === 0 &&
2052 window.wp.customize !== undefined
2053 ) {
2054 window.wp.customize
2055 .control($hidden_select.data('customize-setting-link'))
2056 .setting.set('')
2057 }
2058
2059 $hidden_select.trigger('change')
2060 })
2061
2062 // Chosen order abstract
2063 $this.SP_PCChosenOrder($hidden_value, true)
2064 }
2065
2066 // Chosen sortable
2067 if (is_sortable) {
2068 var $chosen_container = $this.parent().find('.chosen-container')
2069 var $chosen_choices = $chosen_container.find('.chosen-choices')
2070
2071 $chosen_choices.bind('mousedown', function (event) {
2072 if ($(event.target).is('span')) {
2073 event.stopPropagation()
2074 }
2075 })
2076
2077 $chosen_choices.sortable({
2078 items: 'li:not(.search-field)',
2079 helper: 'orginal',
2080 cursor: 'move',
2081 placeholder: 'search-choice-placeholder',
2082 start: function (e, ui) {
2083 ui.placeholder.width(ui.item.innerWidth())
2084 ui.placeholder.height(ui.item.innerHeight())
2085 },
2086 update: function (e, ui) {
2087 var select_options = ''
2088 var chosen_object = $this.data('chosen')
2089 var $prev_select = $this.parent().find('.spf-hidden-select')
2090
2091 $chosen_choices.find('.search-choice-close').each(function () {
2092 var option_array_index = $(this).data('option-array-index')
2093 $.each(chosen_object.results_data, function (index, data) {
2094 if (data.array_index === option_array_index) {
2095 select_options +=
2096 '<option value="' +
2097 data.value +
2098 '" selected>' +
2099 data.value +
2100 '</option>'
2101 }
2102 })
2103 })
2104
2105 $prev_select.children().remove()
2106 $prev_select.append(select_options)
2107 $prev_select.trigger('change')
2108 }
2109 })
2110 }
2111 })
2112 }
2113
2114 //
2115 // Helper Checkbox Checker
2116 //
2117 $.fn.spf_checkbox = function () {
2118 return this.each(function () {
2119 var $this = $(this),
2120 $input = $this.find('.spf--input'),
2121 $checkbox = $this.find('.spf--checkbox')
2122
2123 $checkbox.on('click', function () {
2124 $input.val(Number($checkbox.prop('checked'))).trigger('change')
2125 })
2126 })
2127 }
2128
2129 //
2130 // Siblings
2131 //
2132 $.fn.spf_siblings = function () {
2133 return this.each(function () {
2134 var $this = $(this),
2135 $siblings = $this.find('.spf--sibling'),
2136 multiple = $this.data('multiple') || false
2137
2138 $siblings.on('click', function () {
2139 var $sibling = $(this)
2140
2141 if (multiple) {
2142 if ($sibling.hasClass('spf--active')) {
2143 $sibling.removeClass('spf--active')
2144 $sibling
2145 .find('input')
2146 .prop('checked', false)
2147 .trigger('change')
2148 } else {
2149 $sibling.addClass('spf--active')
2150 $sibling
2151 .find('input')
2152 .prop('checked', true)
2153 .trigger('change')
2154 }
2155 } else {
2156 $this.find('input').prop('checked', false)
2157 $sibling
2158 .find('input')
2159 .prop('checked', true)
2160 .trigger('change')
2161 $sibling
2162 .addClass('spf--active')
2163 .siblings()
2164 .removeClass('spf--active')
2165 }
2166 })
2167 })
2168 }
2169
2170 //
2171 // Help Tooltip
2172 //
2173 $.fn.spf_help = function () {
2174 return this.each(function () {
2175 var $this = $(this),
2176 $tooltip,
2177 offset_left
2178
2179 $this.on({
2180 mouseenter: function () {
2181 $tooltip = $('<div class="spf-tooltip"></div>')
2182 .html($this.find('.spf-help-text').html())
2183 .appendTo('body')
2184 offset_left = SP_PC.vars.is_rtl
2185 ? $this.offset().left - $tooltip.outerWidth()
2186 : $this.offset().left + 24
2187
2188 $tooltip.css({
2189 top: $this.offset().top - ($tooltip.outerHeight() / 2 - 14),
2190 left: offset_left
2191 })
2192 },
2193 mouseleave: function () {
2194 if ($tooltip !== undefined) {
2195 $tooltip.remove()
2196 }
2197 }
2198 })
2199 })
2200 }
2201
2202 //
2203 // Window on resize
2204 //
2205 SP_PC.vars.$window
2206 .on(
2207 'resize spf.resize',
2208 SP_PC.helper.debounce(function (event) {
2209 var window_width =
2210 navigator.userAgent.indexOf('AppleWebKit/') > -1
2211 ? SP_PC.vars.$window.width()
2212 : window.innerWidth
2213
2214 if (window_width <= 782 && !SP_PC.vars.onloaded) {
2215 $('.spf-section').spf_reload_script()
2216 SP_PC.vars.onloaded = true
2217 }
2218 }, 200)
2219 )
2220 .trigger('spf.resize')
2221
2222 //
2223 // Widgets Framework
2224 //
2225 $.fn.spf_widgets = function () {
2226 if (this.length) {
2227 $(document).on('widget-added widget-updated', function (event, $widget) {
2228 $widget.find('.spf-fields').spf_reload_script()
2229 })
2230
2231 $('.widgets-sortables, .control-section-sidebar').on(
2232 'sortstop',
2233 function (event, ui) {
2234 ui.item.find('.spf-fields').spf_reload_script_retry()
2235 }
2236 )
2237
2238 $(document).on('click', '.widget-top', function (event) {
2239 $(this)
2240 .parent()
2241 .find('.spf-fields')
2242 .spf_reload_script()
2243 })
2244 }
2245 }
2246
2247 //
2248 // Retry Plugins
2249 //
2250 $.fn.spf_reload_script_retry = function () {
2251 return this.each(function () {
2252 var $this = $(this)
2253 })
2254 }
2255
2256 //
2257 // Reload Plugins
2258 //
2259 $.fn.spf_reload_script = function (options) {
2260 var settings = $.extend(
2261 {
2262 dependency: true
2263 },
2264 options
2265 )
2266
2267 return this.each(function () {
2268 var $this = $(this)
2269 // Avoid for conflicts
2270 if (!$this.data('inited')) {
2271 // Field plugins
2272 $this.children('.spf-field-accordion').spf_field_accordion()
2273 $this.children('.spf-field-code_editor').spf_field_code_editor()
2274 $this.children('.spf-field-fieldset').spf_field_fieldset()
2275 $this.children('.spf-field-group').spf_field_group()
2276 $this.children('.spf-field-icon').spf_field_icon()
2277 $this.children('.spf-field-slider').spf_field_slider()
2278 $this.children('.spf-field-sortable').spf_field_sortable()
2279 $this.children('.spf-field-sorter').spf_field_sorter()
2280 $this.children('.spf-field-spinner').spf_field_spinner()
2281 $this.children('.spf-field-switcher').spf_field_switcher()
2282 $this.children('.spf-field-typography').spf_field_typography()
2283
2284 // Field colors
2285 $this
2286 .children('.spf-field-border')
2287 .find('.spf-color')
2288 .spf_color()
2289 $this
2290 .children('.spf-field-background')
2291 .find('.spf-color')
2292 .spf_color()
2293 $this
2294 .children('.spf-field-color')
2295 .find('.spf-color')
2296 .spf_color()
2297 $this
2298 .children('.spf-field-color_group')
2299 .find('.spf-color')
2300 .spf_color()
2301 $this
2302 .children('.spf-field-link_color')
2303 .find('.spf-color')
2304 .spf_color()
2305 $this
2306 .children('.spf-field-typography')
2307 .find('.spf-color')
2308 .spf_color()
2309
2310 // Field chosenjs
2311 $this
2312 .children('.spf-field-select')
2313 .find('.spf-chosen')
2314 .spf_chosen()
2315
2316 // Field Checkbox
2317 $this
2318 .children('.spf-field-checkbox')
2319 .find('.spf-checkbox')
2320 .spf_checkbox()
2321
2322 // Field Siblings
2323 $this
2324 .children('.spf-field-button_set')
2325 .find('.spf-siblings')
2326 .spf_siblings()
2327 $this
2328 .children('.spf-field-layout_preset')
2329 .find('.spf-siblings')
2330 .spf_siblings()
2331 $this
2332 .children('.spf-field-palette')
2333 .find('.spf-siblings')
2334 .spf_siblings()
2335
2336 // Help Tooltip
2337 $this
2338 .children('.spf-field')
2339 .find('.spf-help')
2340 .spf_help()
2341
2342 if (settings.dependency) {
2343 $this.spf_dependency()
2344 }
2345
2346 $this.data('inited', true)
2347
2348 $(document).trigger('spf-reload-script', $this)
2349 }
2350 })
2351 }
2352
2353 //
2354 // Document ready and run scripts.
2355 //
2356 $(document).ready(function () {
2357 $('.spf-save').spf_save()
2358 $('.spf-confirm').spf_confirm()
2359 $('.spf-nav-options').spf_nav_options()
2360 $('.spf-nav-metabox').spf_nav_metabox()
2361 $('.spf-expand-all').spf_expand_all()
2362 $('.spf-search').spf_search()
2363 $('.spf-sticky-header').spf_sticky()
2364 $('.spf-page-templates').spf_page_templates()
2365 $('.spf-post-formats').spf_post_formats()
2366 $('.spf-onload').spf_reload_script()
2367 $('.widget').spf_widgets()
2368 })
2369
2370 // ======================================================
2371 // Post
2372 // ------------------------------------------------------
2373 // Trigger taxonomy list when post type is selected.
2374 $('.sp_pcp_post_type select').on('change', function (event) {
2375 event.preventDefault()
2376 var data = {
2377 action: 'sps_get_taxonomies',
2378 pcp_post_types: $(this).val(),
2379 nonce: $('#spf_pcp_metabox_noncesp_pcp_view_options').val()
2380 }
2381 $.post(ajaxurl, data, function (response) {
2382 $('.sp_pcp_post_taxonomy select').html(response)
2383 $('.sp_pcp_post_taxonomy select').trigger('chosen:updated')
2384 })
2385 })
2386 // Update terms list on the change of post taxonomy.
2387 $(document).on('change', '.sp_pcp_post_taxonomy select', function (e) {
2388 e.preventDefault();
2389 var taxSelector = $(this)
2390 var data = {
2391 action: 'sps_get_terms', // Callback function.
2392 pcp_post_taxonomy: $(this).val(),
2393 nonce: $('#spf_pcp_metabox_noncesp_pcp_view_options').val()
2394 }
2395 $.ajax({
2396 type: "POST",
2397 url: ajaxurl,
2398 data: data,
2399 error: function (response) {
2400 console.log(response)
2401 },
2402 success: function (response) {
2403 $(taxSelector)
2404 .parent()
2405 .parent('.sp_pcp_post_taxonomy')
2406 .next('.sp_pcp_taxonomy_terms')
2407 .find('select')
2408 .html(response)
2409 $(taxSelector)
2410 .parent()
2411 .parent('.sp_pcp_post_taxonomy')
2412 .next('.sp_pcp_taxonomy_terms')
2413 .find('select')
2414 .trigger('chosen:updated')
2415 }
2416 })
2417 })
2418
2419 // Populate specific post list in the drop-down.
2420 var pt_saved_value = $('.sp_pcp_post_type select').val()
2421 // Change Include only post_type on change of Post Type field.
2422 var data_include_only = $('.sp_pcp_include_only_posts .spf-chosen-ajax').data(
2423 'chosen-settings'
2424 )
2425 // Change Exclude post_type on change of Post Type field.
2426 var data_exclude = $('.sp_pcp_exclude_posts .spf-chosen-ajax').data(
2427 'chosen-settings'
2428 )
2429 if ($('.spf-nav').hasClass('spf-nav-metabox')) {
2430 data_exclude.data.query_args.post_type = pt_saved_value
2431 data_include_only.data.query_args.post_type = pt_saved_value
2432 }
2433 $('.sp_pcp_post_type select').on('change', function (event) {
2434 event.preventDefault()
2435 var pt_value = $(this).val()
2436 // Change Include only post_type on change of Post Type field.
2437 data_include_only.data.query_args.post_type = pt_value
2438 // Change Exclude post_type on change of Post Type field.
2439 data_exclude.data.query_args.post_type = pt_value
2440 })
2441
2442 /* Keep the accordion field's first item opened */
2443 $(window).on('load', function () {
2444 $('.pcp-opened-accordion').each(function () {
2445 if (!$(this).hasClass('hidden')) {
2446 $(this).addClass('pcp_saved_filter')
2447 }
2448 })
2449 })
2450 $('.spf-field-checkbox.pcp_advanced_filter').on('change', function (event) {
2451 $('.pcp-opened-accordion').each(function () {
2452 if ($(this).hasClass('hidden')) {
2453 $(this).removeClass('pcp_saved_filter')
2454 } else {
2455 $(this).addClass('pcp_saved_filter')
2456 }
2457 if (!$(this).hasClass('pcp_saved_filter')) {
2458 if (
2459 $(this)
2460 .find('.spf-accordion-title')
2461 .siblings('.spf-accordion-content')
2462 .hasClass('spf-accordion-open')
2463 ) {
2464 $(this).find('.spf-accordion-title')
2465 } else {
2466 $(this)
2467 .find('.spf-accordion-title')
2468 .trigger('click')
2469 $(this)
2470 .find('.spf-accordion-content')
2471 .find('.spf-cloneable-add')
2472 .trigger('click')
2473 }
2474 }
2475 })
2476 })
2477
2478 // Pro only tag.
2479 $('.pcp-pagination-type li:nth-child(n+2) input').attr('disabled', true)
2480 $("label:contains((Pro))").css({ 'pointer-events': 'none', 'opacity': '0.8', 'user-select': 'none' })
2481 $("label:contains((Pro)) input").attr('disabled', true).css('opacity', '1')
2482 $("select option:contains((Pro))").attr('disabled', true).css('opacity', '1')
2483 // Image custom size.
2484 // Post Meta fields 5th to last.
2485 // Post content type with limit.
2486
2487 $('.post_content_sorter .spf--sortable-item:has(div.pcp-pro-only)').css({ "pointer-events": "none", "borderColor": "#bebebe" });
2488 $('.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" });
2489 $('.post_content_sorter .spf--sortable-item:has(div.pcp-pro-only)').find('.spf--sortable-helper .fa').css("color", "#777");
2490
2491
2492
2493 var preview_box = $('#spsp-preview-box');
2494 var pcp_display = $('#sp_pcp_display').hide();
2495
2496 // $('#spsp-show-preview:contains(Hide)').
2497 $(document).on('click', '#spsp-show-preview:contains(Hide)', function (e) {
2498 e.preventDefault();
2499 var _this = $(this);
2500 _this.html('<i class="fa fa-eye" aria-hidden="true"></i> Show Preview');
2501 preview_box.html('');
2502 pcp_display.hide();
2503 });
2504 $(document).on('click', '#spsp-show-preview:not(:contains(Hide))', function (e) {
2505 e.preventDefault();
2506 var previewJS = window.spf_vars.previewJS;
2507 var _data = $('form#post').serialize();
2508 var _this = $(this);
2509 var $sp_id = $(this).data('id');
2510 var data = {
2511 action: 'spf_preview_meta_box',
2512 id: $sp_id,
2513 data: _data,
2514 nonce: $('#spf_pcp_metabox_noncesp_pcp_view_options').val()
2515 };
2516 $.ajax({
2517 type: "POST",
2518 url: ajaxurl,
2519 data: data,
2520 error: function (response) {
2521 console.log(response)
2522 },
2523 success: function (response) {
2524 pcp_display.show();
2525 preview_box.html(response);
2526 $.getScript(previewJS, function () {
2527 _this.html('<i class="fa fa-eye-slash" aria-hidden="true"></i> Hide Preview');
2528 $(document).on('change', '#sp_pcp_view_options', function (e) {
2529 e.preventDefault();
2530 _this.html('<i class="fa fa-refresh" aria-hidden="true"></i> Update Preview');
2531 });
2532 $(document).on('change', '#spf-section-sp_pcp_layouts_1', function (e) {
2533 e.preventDefault();
2534 _this.html('<i class="fa fa-refresh" aria-hidden="true"></i> Update Preview');
2535 });
2536 $("html, body").animate({ scrollTop: pcp_display.offset().top - 50 }, "slow");
2537 })
2538 }
2539 })
2540 });
2541
2542 function isValidJSONString(str) {
2543 try {
2544 JSON.parse(str);
2545 } catch (e) {
2546 return false;
2547 }
2548 return true;
2549 }
2550 // Smart-post-show export.
2551 var $export_type = $('.pcp_what_export').find('input:checked').val();
2552 $('.pcp_what_export').on('change', function () {
2553 $export_type = $(this).find('input:checked').val();
2554 });
2555
2556 $('.pcp_export .spf--button').on('click', function (event) {
2557 event.preventDefault();
2558
2559 var $shortcode_ids = $('.pcp_post_ids select').val();
2560 var $ex_nonce = $('#spf_options_noncesp_post_carousel_tools').val();
2561 var selected_shortcode = $export_type === 'selected_shortcodes' ? $shortcode_ids : 'all_shortcodes';
2562 if ($export_type === 'all_shortcodes' || $export_type === 'selected_shortcodes') {
2563 var data = {
2564 action: 'pcp_export_shortcodes',
2565 pcp_ids: selected_shortcode,
2566 nonce: $ex_nonce,
2567 }
2568 } else {
2569 $('.spf-form-result.spf-form-success').text('No carousel selected.').show();
2570 setTimeout(function () {
2571 $('.spf-form-result.spf-form-success').hide().text('');
2572 }, 3000);
2573 }
2574 $.post(ajaxurl, data, function (resp) {
2575 if (resp) {
2576 // Convert JSON Array to string.
2577 if (isValidJSONString(resp)) {
2578 var json = JSON.stringify(JSON.parse(resp));
2579 } else {
2580 var json = JSON.stringify(resp);
2581 }
2582 // Convert JSON string to BLOB.
2583 var blob = new Blob([json], { type: 'application/json' });
2584 var link = document.createElement('a');
2585 var pcp_time = $.now();
2586 link.href = window.URL.createObjectURL(blob);
2587 link.download = "smart-post-show-" + pcp_time + ".json";
2588 link.click();
2589 $('.spf-form-result.spf-form-success').text('Exported successfully!').show();
2590 setTimeout(function () {
2591 $('.spf-form-result.spf-form-success').hide().text('');
2592 $('.pcp_post_ids select').val('').trigger('chosen:updated');
2593 }, 3000);
2594 }
2595 });
2596 });
2597 // smart-post-show import.
2598 $('.pcp_import button.import').on('click',function (event) {
2599 event.preventDefault();
2600 var $this = $(this),
2601 this_text = $this.text(),
2602 pcp_shortcodes = $('#import').prop('files')[0];
2603 if ($('#import').val() != '') {
2604 $this.prop('disabled',true).css( 'opacity', '0.7' );
2605 var $im_nonce = $('#spf_options_noncesp_post_carousel_tools').val();
2606 var reader = new FileReader();
2607 reader.readAsText(pcp_shortcodes);
2608 reader.onload = function (event) {
2609 var jsonObj = JSON.stringify(event.target.result);
2610 $.ajax({
2611 url: ajaxurl,
2612 type: 'POST',
2613 data: {
2614 shortcode: jsonObj,
2615 action: 'pcp_import_shortcodes',
2616 nonce: $im_nonce,
2617 },
2618 success: function (resp) {
2619 $('.spf-form-result.spf-form-success').text('Imported successfully!').show();
2620 setTimeout(function () {
2621 $('.spf-form-result.spf-form-success').hide().text('');
2622 $('#import').val('');
2623 $this.prop('disabled',false).css( 'opacity', '1' );
2624 window.location.replace($('#pcp_shortcode_link_redirect').attr('href'));
2625 }, 2000);
2626 },
2627 error: function(error){
2628 $('#import').val('');
2629 $this.prop('disabled',false).css( 'opacity', '1' );
2630 }
2631 });
2632 }
2633 } else {
2634 $('.spf-form-result.spf-form-success').text('No exported json file chosen.').show();
2635 setTimeout(function () {
2636 $('.spf-form-result.spf-form-success').hide().text('');
2637 }, 3000);
2638 }
2639 });
2640
2641 $(document).on('keyup change', '.sp_post_carousel_page_pcp_settings #spf-form', function (e) {
2642 e.preventDefault();
2643 var $button = $(this).find('.spf-save');
2644 $button.css({ "background-color": "#00C263", "pointer-events": "initial" }).val('Save Settings');
2645 });
2646 $('.sp_post_carousel_page_pcp_settings .spf-save').on('click', function (e) {
2647 e.preventDefault();
2648 $(this).css({ "background-color": "#C5C5C6", "pointer-events": "none" }).val('Changes Saved');
2649 })
2650
2651 })(jQuery, window, document)