← All changes
|
libraries/cmb2-metatabs-options/code/cmb2multiopts.js
+132
-0
5.7.3
→
6.0.4
View file →
| @@ -1,0 +1,132 @@ | ||
| 1 | +/** | |
| 2 | + * @since 1.1.1 Code reformatted to WP standard | |
| 3 | + * @since 1.0.1 Removed postboxes toggle | |
| 4 | + * @since 1.0.0 | |
| 5 | + */ | |
| 6 | + | |
| 7 | +jQuery.noConflict(); | |
| 8 | +jQuery(document).ready(function ($) { | |
| 9 | + | |
| 10 | + /** | |
| 11 | + * Localized var from WP. | |
| 12 | + * | |
| 13 | + * @name cmb2OptTabs | |
| 14 | + * @property {string} key | |
| 15 | + * @property {string} posttype | |
| 16 | + * @property {string} defaulttab | |
| 17 | + */ | |
| 18 | + | |
| 19 | + var slug = cmb2OptTabs.key, | |
| 20 | + posttype = cmb2OptTabs.posttype, | |
| 21 | + cls = { | |
| 22 | + toggle: 'opt-content', | |
| 23 | + tab: 'opt-tab', | |
| 24 | + defaulttab: 'opt-tab-' + cmb2OptTabs.defaulttab, | |
| 25 | + hidden: 'opt-hidden', | |
| 26 | + wpactive: 'nav-tab-active' | |
| 27 | + }, | |
| 28 | + activetab = '', | |
| 29 | + page = slug, | |
| 30 | + $containers = $('.' + cls.toggle); | |
| 31 | + | |
| 32 | + // move metaboxes to correct tabs | |
| 33 | + moveMetaboxes(); | |
| 34 | + | |
| 35 | + // when page loads, make tab active | |
| 36 | + tabSwitch(); | |
| 37 | + | |
| 38 | + // show and hide tab content on tab clicks | |
| 39 | + $('.' + cls.tab).on('click', function (e) { | |
| 40 | + e.preventDefault(); | |
| 41 | + tabSwitch($(this)); | |
| 42 | + }); | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * TAB SWITCH | |
| 46 | + * Changes the tab content when tab is clicked. | |
| 47 | + * | |
| 48 | + * @param $tab | |
| 49 | + * | |
| 50 | + * @since 1.0.0 | |
| 51 | + */ | |
| 52 | + function tabSwitch($tab) { | |
| 53 | + | |
| 54 | + var id, content, tab; | |
| 55 | + | |
| 56 | + // if $tab was not set, get the tab from the query string | |
| 57 | + if (typeof($tab) === 'undefined') { | |
| 58 | + tab = getQ('tab').length ? getQ('tab') : cls.defaulttab; | |
| 59 | + $tab = $('#' + tab); | |
| 60 | + } | |
| 61 | + | |
| 62 | + // get id and content location from the chosen tab | |
| 63 | + id = $tab.attr('id'); | |
| 64 | + content = $tab.data('optcontent'); | |
| 65 | + | |
| 66 | + // hide all tabs and show current tab | |
| 67 | + $('.' + cls.toggle).hide(); | |
| 68 | + $(content).show(); | |
| 69 | + | |
| 70 | + // remove and set the Wordpress active tab class on the tab navigation | |
| 71 | + $('.' + cls.tab).removeClass(cls.wpactive); | |
| 72 | + $('#' + id).addClass(cls.wpactive).trigger('blur'); | |
| 73 | + | |
| 74 | + // set the current active tab | |
| 75 | + activetab = id; | |
| 76 | + | |
| 77 | + // change the browser URL to reflect the current tab; this allows Wordpress to change to this tab on save | |
| 78 | + if (history.pushState) { | |
| 79 | + | |
| 80 | + var stateObject = {dummy: true}, | |
| 81 | + postvar = posttype ? 'post_type=' + posttype + '&' : ''; | |
| 82 | + | |
| 83 | + var url = window.location.protocol | |
| 84 | + + "//" | |
| 85 | + + window.location.host | |
| 86 | + + window.location.pathname | |
| 87 | + + '?' | |
| 88 | + + postvar | |
| 89 | + + 'page=' + page | |
| 90 | + + '&tab=' + id; | |
| 91 | + | |
| 92 | + history.pushState(stateObject, $(document).find('title').text(), url); | |
| 93 | + } | |
| 94 | + } | |
| 95 | + | |
| 96 | + /** | |
| 97 | + * MOVE METABOXES | |
| 98 | + * Moves metaboxes to their proper tabs. Each tab container has a data attribute "boxes" with a comma-delimited | |
| 99 | + * string containing the metaboxes which belong within. | |
| 100 | + * | |
| 101 | + * @since 1.0.0 | |
| 102 | + */ | |
| 103 | + function moveMetaboxes() { | |
| 104 | + $containers.each(function () { | |
| 105 | + var $this = $(this), | |
| 106 | + $sortable = $this.find('.meta-box-sortables'), | |
| 107 | + boxes; | |
| 108 | + boxes = $this.data('boxes').split(','); | |
| 109 | + $.each(boxes, function (i, v) { | |
| 110 | + $('#' + v).appendTo($sortable).removeClass('hide-if-js ' + cls.hidden); | |
| 111 | + }); | |
| 112 | + }); | |
| 113 | + } | |
| 114 | + | |
| 115 | + /** | |
| 116 | + * GETQ | |
| 117 | + * Gets the query string. | |
| 118 | + * | |
| 119 | + * @param name | |
| 120 | + * @returns {string} | |
| 121 | + * | |
| 122 | + * @since 1.0.0 | |
| 123 | + */ | |
| 124 | + function getQ(name) { | |
| 125 | + var regex, results; | |
| 126 | + name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); | |
| 127 | + regex = new RegExp("[\\?&]" + name + "=([^&#]*)"); | |
| 128 | + results = regex.exec(location.search); | |
| 129 | + return results === null ? | |
| 130 | + "" : decodeURIComponent(results[1].replace(/\+/g, " ")); | |
| 131 | + } | |
| 132 | +}); | |