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