PluginProbe ʕ •ᴥ•ʔ
Custom Sidebars – Dynamic Sidebar Classic Widget Area Manager / 3.0.7.1
Custom Sidebars – Dynamic Sidebar Classic Widget Area Manager v3.0.7.1
trunk 2.1.2.0 3.0.0.0 3.0.0.1 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.7.1 3.0.8 3.0.8.1 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.3 3.31 3.32 3.35 3.36 3.37 3.38
custom-sidebars / js / cs-visibility.js
custom-sidebars / js Last commit date
cs-cloning.js 9 years ago cs-cloning.min.js 9 years ago cs-visibility.js 9 years ago cs-visibility.min.js 9 years ago cs.js 9 years ago cs.min.js 9 years ago
cs-visibility.js
223 lines
1 /*! Custom Sidebars - v3.0.7.1
2 * https://premium.wpmudev.org/project/custom-sidebars-pro/
3 * Copyright (c) 2017; * Licensed GPLv2+ */
4 /*global jQuery:false */
5 /*global window:false */
6 /*global document:false */
7 /*global wp:false */
8 /*global wpmUi:false */
9
10 jQuery(function init_visibility() {
11 var $doc = jQuery( document );
12
13 /**
14 * Moves the "Visibility" button next to the save button.
15 */
16 var init_widget = function init_widget( ev, el ) {
17 var $widget = jQuery( el ).closest( '.widget' ),
18 $btn = $widget.find( '.csb-visibility-button' ),
19 $target = $widget.find( '.widget-control-actions .widget-control-save' ),
20 $spinner = $widget.find( '.widget-control-actions .spinner' );
21
22 if ( $widget.data( '_csb_visibility' ) ) {
23 return;
24 }
25
26 $spinner.insertBefore( $target ).css({ 'float': 'left' });
27 $btn.insertBefore( $target ).click( toggle_section );
28 $widget.on( 'click', '.toggle-action b', toggle_action );
29 $widget.on( 'csb:update', update_display );
30 $widget.on( 'click', '.clear-filter', remove_filter );
31 $widget.on( 'click', '.choose-filters', show_filter_menu );
32 $widget.on( 'click', '.add-filter', add_filter );
33 $widget.on( 'change', 'input[data-lbl-all][data-lbl-single]', toggle_label );
34 $widget.on( 'change', 'select.posttype', update_posttypes );
35
36 $widget.data( '_csb_visibility', true );
37 };
38
39 /**
40 * Removes the current filter row from the conditions
41 */
42 var remove_filter = function remove_filter( ev ) {
43 var $me = jQuery( this ),
44 $row = $me.closest( '.csb-option-row' ),
45 $widget = $me.closest( '.widget' ),
46 sel = '.' + jQuery.trim( $row.attr( 'class' ).replace( 'csb-option-row', '') ),
47 $add_item = $widget.find( '[data-for="' + sel + '"]' ),
48 $input = $row.find( 'input, select, textarea' );
49
50 ev.preventDefault();
51 $add_item.show();
52 $row.fadeOut( 400, function clear_values() {
53 // After row is hidden clear the input values.
54 $input.val('').trigger('change.select2');
55 $widget.trigger('csb:update');
56 } );
57 return false;
58 };
59
60 /**
61 * User clicks on a new filter option in the "Add filter" dropdown.
62 * Show the filter row.
63 */
64 var add_filter = function add_filter( ev ) {
65 var $me = jQuery( this ),
66 sel = $me.data( 'for' ),
67 $widget = $me.closest( '.widget' ),
68 $always = $widget.find( '.csb-always' ),
69 $filter = $widget.find( sel );
70
71 ev.preventDefault();
72 $filter.show();
73 $me.hide();
74 $always.hide();
75 hide_filter_menu();
76 $widget.trigger('csb:update');
77 return false;
78 };
79
80 /**
81 * When a filter block is added or removed we need to show/hide some hints.
82 */
83 var update_display = function update_display() {
84 var $widget = jQuery( this ).closest( '.widget' ),
85 $always = $widget.find( '.csb-always' ),
86 $rows = $widget.find( '.csb-option-row:visible:not(.csb-action,.csb-always)' );
87
88 if ( $rows.length === 0 ) {
89 $always.show();
90 } else {
91 $always.hide();
92 $rows.find( '.csb-and' ).show();
93 $rows.first().find( '.csb-and' ).hide();
94 }
95 wpmUi.upgrade_multiselect( $widget );
96 };
97
98 /**
99 * Let user add a new filter.
100 */
101 var show_filter_menu = function show_filter_menu( ev ) {
102 var $me = jQuery( this ),
103 $row = $me.closest( '.csb-option-row' ),
104 $menu = $row.find( '.dropdown' );
105
106 ev.preventDefault();
107 $menu.show();
108 $doc.one( 'click', hide_filter_menu );
109 return false;
110 };
111
112 /**
113 * Close the filter menu again.
114 */
115 var hide_filter_menu = function hide_filter_menu( ev ) {
116 jQuery( '.csb-action .dropdown:visible' ).hide();
117 };
118
119 /**
120 * Shows or hides the visibility-options for the current widget.
121 */
122 var toggle_section = function toggle_section( ev ) {
123 var $me = jQuery( this ),
124 $widget = $me.closest( '.widget' ),
125 $section = $widget.find( '.csb-visibility-inner' ),
126 $flag = $section.find( '.csb-visible-flag' );
127
128 ev.preventDefault();
129 if ( $flag.val() === '0' ) {
130 $flag.val('1');
131 $section.show();
132 $widget.trigger('csb:update');
133 } else {
134 $flag.val('0');
135 $section.hide();
136 }
137
138 return false;
139 };
140
141 /**
142 * Toggles the widget state between "show if" / "hide if"
143 */
144 var toggle_action = function toggle_action( ev ) {
145 var $me = jQuery( this ).closest( 'label' ),
146 $widget = $me.closest( '.widget' ),
147 sel = '#' + $me.attr( 'for' ),
148 $action = $widget.find( sel ),
149 state = $action.val(),
150 $lbl_show = $widget.find( '.lbl-show-if' ),
151 $lbl_hide = $widget.find( '.lbl-hide-if' );
152
153 ev.preventDefault();
154 if ( 'show' !== state ) {
155 $lbl_show.show();
156 $lbl_hide.hide();
157 $action.val( 'show' );
158 } else {
159 $lbl_show.hide();
160 $lbl_hide.show();
161 $action.val( 'hide' );
162 }
163 return false;
164 };
165
166 /**
167 * Used for the posttype filter: When user changes the "All posts" checkbox
168 * the label will toggle between "All posts" and "Only these posts".
169 */
170 var toggle_label = function toggle_label( ev ) {
171 var $me = jQuery( this ).closest( 'label' ),
172 $row = $me.closest( '.csb-detail-row' ),
173 $inp = $me.find( 'input[type=checkbox]' ),
174 $lbl = $me.find( '.lbl' ),
175 $detail = $row.find( '.detail' ),
176 $detail_inp = $detail.find( 'input,select,textarea' );
177
178 if ( $inp.prop( 'checked' ) ) {
179 $lbl.text( $inp.data( 'lbl-single' ) );
180 $detail.show();
181 } else {
182 $lbl.text( $inp.data( 'lbl-all' ) );
183 $detail.hide();
184 $detail_inp.val('').trigger('change.select2');
185 }
186 };
187
188 /**
189 * When the user changes the posttype-filter show or hide the detail-rows
190 * for each posttype.
191 */
192 var update_posttypes = function update_posttypes( ev ) {
193 var $me = jQuery( this ),
194 $row = $me.closest( '.csb-option-row' ),
195 $types = $row.find( '.csb-detail-row' ),
196 types = $me.val(),
197 i;
198
199 $types.addClass( 'csb-hide' );
200 if ( types ) {
201 for ( i = 0; i < types.length; i += 1 ) {
202 $types.filter( '.csb-pt-' + types[i] ).removeClass( 'csb-hide ');
203 }
204 }
205
206 $types.each(function check_detail_row() {
207 var $detail = jQuery( this ),
208 $check = $detail.find( 'input[type=checkbox]' );
209
210 if ( $detail.hasClass( 'csb-hide' ) ) {
211 $detail.hide();
212 $check.prop( 'checked', false );
213 toggle_label.call( $check );
214 } else {
215 $detail.show();
216 }
217 });
218 };
219
220 jQuery( '#widgets-right .widget' ).each( init_widget );
221 $doc.on( 'widget-added', init_widget );
222 });
223