balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

tab-check.js (8530B)


      1 /* global redux */
      2 
      3 (function( $ ) {
      4 	'use strict';
      5 
      6 	$.redux = $.redux || {};
      7 
      8 	$.redux.tabCheck = function() {
      9 		var link;
     10 		var tab;
     11 		var sTab;
     12 		var cookieName;
     13 		var opt_name;
     14 
     15 		$( '.redux-group-tab-link-a' ).on(
     16 			'click',
     17 			function() {
     18 				var elements;
     19 				var index;
     20 				var el;
     21 				var relid;
     22 				var oldid;
     23 				var cookieName;
     24 				var boxIndex;
     25 				var parentID;
     26 				var newParent;
     27 
     28 				link = $( this );
     29 
     30 				if ( link.parent().hasClass( 'empty_section' ) && link.parent().hasClass( 'hasSubSections' ) ) {
     31 					elements = $( this ).closest( 'ul' ).find( '.redux-group-tab-link-a' );
     32 					index    = elements.index( this );
     33 
     34 					link = elements.slice( index + 1, index + 2 );
     35 				}
     36 
     37 				el    = link.parents( '.redux-container:first' );
     38 				relid = link.data( 'rel' ); // The group ID of interest.
     39 				oldid = el.find( '.redux-group-tab-link-li.active:first .redux-group-tab-link-a' ).data( 'rel' );
     40 				opt_name = $.redux.getOptName( el );
     41 
     42 				if ( oldid === relid ) {
     43 					return;
     44 				}
     45 
     46 				cookieName = '';
     47 
     48 				if ( ! link.parents( '.postbox-container:first' ).length ) {
     49 					$( '#currentSection' ).val( relid );
     50 
     51 					cookieName = 'redux_current_tab_' + redux.optName.args.opt_name;
     52 				} else {
     53 					el.prev( '#currentSection' ).val( relid );
     54 
     55 					boxIndex = el.data( 'index' );
     56 
     57 					if ( '' !== boxIndex ) {
     58 						cookieName = 'redux_metabox_' + boxIndex + '_current_tab_' + redux.optName.args.opt_name;
     59 					}
     60 				}
     61 
     62 				// Set the proper page cookie.
     63 				$.cookie(
     64 					cookieName,
     65 					relid,
     66 					{
     67 						expires: 7,
     68 						path: '/'
     69 					}
     70 				);
     71 
     72 				if ( el.find( '#' + relid + '_section_group_li' ).parents( '.redux-group-tab-link-li' ).length ) {
     73 					parentID = el.find( '#' + relid + '_section_group_li' ).parents( '.redux-group-tab-link-li' ).attr( 'id' ).split( '_' );
     74 					parentID = parentID[0];
     75 				}
     76 
     77 				el.find( '#toplevel_page_' + redux.optName.args.slug + ' .wp-submenu a.current' ).removeClass( 'current' );
     78 				el.find( '#toplevel_page_' + redux.optName.args.slug + ' .wp-submenu li.current' ).removeClass( 'current' );
     79 
     80 				el.find( '#toplevel_page_' + redux.optName.args.slug + ' .wp-submenu a' ).each(
     81 					function() {
     82 						var url = $( this ).attr( 'href' ).split( '&tab=' );
     83 
     84 						if ( url[1] === relid || url[1] === parentID ) {
     85 							$( this ).addClass( 'current' );
     86 							$( this ).parent().addClass( 'current' );
     87 						}
     88 					}
     89 				);
     90 
     91 				if ( el.find( '#' + oldid + '_section_group_li' ).find( '#' + oldid + '_section_group_li' ).length ) {
     92 					el.find( '#' + oldid + '_section_group_li' ).addClass( 'activeChild' );
     93 					el.find( '#' + relid + '_section_group_li' ).addClass( 'active' ).removeClass( 'activeChild' );
     94 				} else if ( el.find( '#' + relid + '_section_group_li' ).parents( '#' + oldid + '_section_group_li' ).length || el.find( '#' + oldid + '_section_group_li' ).parents( 'ul.subsection' ).find( '#' + relid + '_section_group_li' ).length ) {
     95 					if ( el.find( '#' + relid + '_section_group_li' ).parents( '#' + oldid + '_section_group_li' ).length ) {
     96 						el.find( '#' + oldid + '_section_group_li' ).addClass( 'activeChild' ).removeClass( 'active' );
     97 					} else {
     98 						el.find( '#' + relid + '_section_group_li' ).addClass( 'active' );
     99 						el.find( '#' + oldid + '_section_group_li' ).removeClass( 'active' );
    100 					}
    101 					el.find( '#' + relid + '_section_group_li' ).removeClass( 'activeChild' ).addClass( 'active' );
    102 				} else {
    103 					setTimeout(
    104 						function() {
    105 							el.find( '#' + relid + '_section_group_li' ).addClass( 'active' ).removeClass( 'activeChild' ).find( 'ul.subsection' ).slideDown();
    106 						},
    107 						1
    108 					);
    109 
    110 					if ( el.find( '#' + oldid + '_section_group_li' ).find( 'ul.subsection' ).length ) {
    111 						el.find( '#' + oldid + '_section_group_li' ).find( 'ul.subsection' ).slideUp(
    112 							'fast',
    113 							function() {
    114 								el.find( '#' + oldid + '_section_group_li' ).removeClass( 'active' ).removeClass( 'activeChild' );
    115 							}
    116 						);
    117 
    118 						newParent = el.find( '#' + relid + '_section_group_li' ).parents( '.hasSubSections:first' );
    119 
    120 						if ( newParent.length > 0 ) {
    121 							el.find( '#' + relid + '_section_group_li' ).removeClass( 'active' );
    122 							relid = newParent.find( '.redux-group-tab-link-a:first' ).data( 'rel' );
    123 
    124 							if ( newParent.hasClass( 'empty_section' ) ) {
    125 								newParent.find( '.subsection li:first' ).addClass( 'active' );
    126 								el.find( '#' + relid + '_section_group_li' ).removeClass( 'active' ).addClass( 'activeChild' ).find( 'ul.subsection' ).slideDown();
    127 								newParent = newParent.find( '.subsection li:first' );
    128 								relid     = newParent.find( '.redux-group-tab-link-a:first' ).data( 'rel' );
    129 							} else {
    130 								el.find( '#' + relid + '_section_group_li' ).addClass( 'active' ).removeClass( 'activeChild' ).find( 'ul.subsection' ).slideDown();
    131 							}
    132 						}
    133 					} else if ( el.find( '#' + oldid + '_section_group_li' ).parents( 'ul.subsection' ).length ) {
    134 						if ( ! el.find( '#' + oldid + '_section_group_li' ).parents( '#' + relid + '_section_group_li' ).length ) {
    135 							el.find( '#' + oldid + '_section_group_li' ).parents( 'ul.subsection' ).slideUp(
    136 								'fast',
    137 								function() {
    138 									el.find( '#' + oldid + '_section_group_li' ).removeClass( 'active' );
    139 									el.find( '#' + oldid + '_section_group_li' ).parents( '.redux-group-tab-link-li' ).removeClass( 'active' ).removeClass( 'activeChild' );
    140 									el.find( '#' + relid + '_section_group_li' ).parents( '.redux-group-tab-link-li' ).addClass( 'activeChild' ).find( 'ul.subsection' ).slideDown();
    141 									el.find( '#' + relid + '_section_group_li' ).addClass( 'active' );
    142 								}
    143 							);
    144 						} else {
    145 							el.find( '#' + oldid + '_section_group_li' ).removeClass( 'active' );
    146 						}
    147 					} else {
    148 						el.find( '#' + oldid + '_section_group_li' ).removeClass( 'active' );
    149 
    150 						if ( el.find( '#' + relid + '_section_group_li' ).parents( '.redux-group-tab-link-li' ).length ) {
    151 							setTimeout(
    152 								function() {
    153 									el.find( '#' + relid + '_section_group_li' ).parents( '.redux-group-tab-link-li' ).addClass( 'activeChild' ).find( 'ul.subsection' ).slideDown();
    154 								},
    155 								50
    156 							);
    157 
    158 							el.find( '#' + relid + '_section_group_li' ).addClass( 'active' );
    159 						}
    160 					}
    161 				}
    162 
    163 				// Show the group.
    164 				el.find( '#' + oldid + '_section_group' ).hide();
    165 
    166 				el.find( '#' + relid + '_section_group' ).fadeIn(
    167 					200,
    168 					function() {
    169 						if ( 0 !== el.find( '#redux-footer' ).length ) {
    170 							$.redux.stickyInfo(); // Race condition fix.
    171 						}
    172 
    173 						$.redux.initFields();
    174 					}
    175 				);
    176 
    177 				$( '#toplevel_page_' + redux.optName.args.slug ).find( '.current' ).removeClass( 'current' );
    178 			}
    179 		);
    180 
    181 		if ( undefined !== redux.optName.last_tab ) {
    182 			$( '#' + redux.optName.last_tab + '_section_group_li_a' ).trigger( 'click' );
    183 
    184 			return;
    185 		}
    186 
    187 		tab = decodeURI( ( new RegExp( 'tab=(.+?)(&|$)' ).exec( location.search ) || [''])[1] );
    188 
    189 		if ( '' !== tab ) {
    190 			if ( $.cookie( 'redux_current_tab_get' ) !== tab ) {
    191 				$.cookie(
    192 					'redux_current_tab',
    193 					tab,
    194 					{
    195 						expires: 7,
    196 						path: '/'
    197 					}
    198 				);
    199 
    200 				$.cookie(
    201 					'redux_current_tab_get',
    202 					tab,
    203 					{
    204 						expires: 7,
    205 						path: '/'
    206 					}
    207 				);
    208 
    209 				$.cookie(
    210 					'redux_current_tab_' + redux.optName.args.opt_name,
    211 					tab,
    212 					{
    213 						expires: 7,
    214 						path: '/'
    215 					}
    216 				);
    217 
    218 				$( '#' + tab + '_section_group_li' ).trigger( 'click' );
    219 			}
    220 		} else if ( '' !== $.cookie( 'redux_current_tab_get' ) ) {
    221 			$.removeCookie( 'redux_current_tab_get' );
    222 		}
    223 
    224 		$( '.redux-container' ).each(
    225 			function() {
    226 				var boxIndex;
    227 
    228 				if ( ! $( this ).parents( '.postbox-container:first' ).length ) {
    229 					opt_name = $( '.redux-ajax-security' ).data( 'opt-name' );
    230 
    231 					cookieName = 'redux_current_tab_' + opt_name;
    232 
    233 					sTab = $( this ).find( '#' + $.cookie( cookieName ) + '_section_group_li_a' );
    234 				} else {
    235 					opt_name = $.redux.getOptName( this );
    236 
    237 					boxIndex = $( this ).data( 'index' );
    238 
    239 					if ( '' === boxIndex ) {
    240 						boxIndex = 0;
    241 					}
    242 
    243 					cookieName = 'redux_metabox_' + boxIndex + '_current_tab_' + opt_name;
    244 
    245 					sTab = $( this ).find( '#' + $.cookie( cookieName ) + '_section_group_li_a' );
    246 				}
    247 
    248 				// Tab the first item or the saved one.
    249 				if ( null === $.cookie( cookieName ) || 'undefined' === typeof ( $.cookie( cookieName ) ) || 0 === sTab.length ) {
    250 					$( this ).find( '.redux-group-tab-link-a:first' ).trigger( 'click' );
    251 				} else {
    252 					sTab.trigger( 'click' );
    253 				}
    254 			}
    255 		);
    256 	};
    257 })( jQuery );