notices.js (5511B)
1 /* global redux */ 2 3 (function( $ ) { 4 'use strict'; 5 6 $.redux = $.redux || {}; 7 8 $.redux.sanitize = function() { 9 if ( redux.optName.sanitize && redux.optName.sanitize.sanitize ) { 10 $.each( 11 redux.optName.sanitize.sanitize, 12 function( sectionID, sectionArray ) { 13 sectionID = null; 14 $.each( 15 sectionArray.sanitize, 16 function( key, value ) { 17 $.redux.fixInput( key, value ); 18 } 19 ); 20 } 21 ); 22 } 23 }; 24 25 $.redux.fixInput = function( key, value ) { 26 var val; 27 var input; 28 var inputVal; 29 var ul; 30 var li; 31 32 if ( 'multi_text' === value.type ) { 33 ul = $( '#' + value.id + '-ul' ); 34 li = $( ul.find( 'li' ) ); 35 36 li.each( 37 function() { 38 input = $( this ).find( 'input' ); 39 inputVal = input.val(); 40 41 if ( inputVal === value.old ) { 42 input.val( value.current ); 43 } 44 } 45 ); 46 47 return; 48 } 49 50 input = $( 'input#' + value.id + '-' + key ); 51 52 if ( 0 === input.length ) { 53 input = $( 'input#' + value.id ); 54 } 55 56 if ( 0 === input.length ) { 57 input = $( 'textarea#' + value.id + '-textarea' ); 58 } 59 60 if ( input.length > 0 ) { 61 val = '' === value.current ? value.default : value.current; 62 63 $( input ).val( val ); 64 } 65 }; 66 67 $.redux.notices = function() { 68 if ( redux.optName.errors && redux.optName.errors.errors ) { 69 $.each( 70 redux.optName.errors.errors, 71 function( sectionID, sectionArray ) { 72 sectionID = null; 73 $.each( 74 sectionArray.errors, 75 function( key, value ) { 76 $( '#' + redux.optName.args.opt_name + '-' + value.id ).addClass( 'redux-field-error' ); 77 if ( 0 === $( '#' + redux.optName.args.opt_name + '-' + value.id ).parent().find( '.redux-th-error' ).length ) { 78 $( '#' + redux.optName.args.opt_name + '-' + value.id ).append( '<div class="redux-th-error">' + value.msg + '</div>' ); 79 } else { 80 $( '#' + redux.optName.args.opt_name + '-' + value.id ).parent().find( '.redux-th-error' ).html( value.msg ).css( 'display', 'block' ); 81 } 82 83 $.redux.fixInput( key, value ); 84 } 85 ); 86 } 87 ); 88 89 $( '.redux-container' ).each( 90 function() { 91 var totalErrors; 92 93 var container = $( this ); 94 95 // Ajax cleanup. 96 container.find( '.redux-menu-error' ).remove(); 97 98 totalErrors = container.find( '.redux-field-error' ).length; 99 100 if ( totalErrors > 0 ) { 101 container.find( '.redux-field-errors span' ).text( totalErrors ); 102 container.find( '.redux-field-errors' ).slideDown(); 103 container.find( '.redux-group-tab' ).each( 104 function() { 105 var sectionID; 106 var subParent; 107 108 var total = $( this ).find( '.redux-field-error' ).length; 109 if ( total > 0 ) { 110 sectionID = $( this ).attr( 'id' ).split( '_' ); 111 112 sectionID = sectionID[0]; 113 container.find( '.redux-group-tab-link-a[data-key="' + sectionID + '"]' ).prepend( '<span class="redux-menu-error">' + total + '</span>' ); 114 container.find( '.redux-group-tab-link-a[data-key="' + sectionID + '"]' ).addClass( 'hasError' ); 115 116 subParent = container.find( '.redux-group-tab-link-a[data-key="' + sectionID + '"]' ).parents( '.hasSubSections:first' ); 117 118 if ( subParent ) { 119 subParent.find( '.redux-group-tab-link-a:first' ).addClass( 'hasError' ); 120 } 121 } 122 } 123 ); 124 } 125 } 126 ); 127 } 128 129 if ( redux.optName.warnings && redux.optName.warnings.warnings ) { 130 $.each( 131 redux.optName.warnings.warnings, 132 function( sectionID, sectionArray ) { 133 sectionID = null; 134 $.each( 135 sectionArray.warnings, 136 function( key, value ) { 137 $( '#' + redux.optName.args.opt_name + '-' + value.id ).addClass( 'redux-field-warning' ); 138 139 if ( 0 === $( '#' + redux.optName.args.opt_name + '-' + value.id ).parent().find( '.redux-th-warning' ).length ) { 140 $( '#' + redux.optName.args.opt_name + '-' + value.id ).append( '<div class="redux-th-warning">' + value.msg + '</div>' ); 141 } else { 142 $( '#' + redux.optName.args.opt_name + '-' + value.id ).parent().find( '.redux-th-warning' ).html( value.msg ).css( 'display', 'block' ); 143 } 144 145 $.redux.fixInput( key, value ); 146 } 147 ); 148 } 149 ); 150 151 $( '.redux-container' ).each( 152 function() { 153 var sectionID; 154 var subParent; 155 var total; 156 var totalWarnings; 157 158 var container = $( this ); 159 160 // Ajax cleanup. 161 container.find( '.redux-menu-warning' ).remove(); 162 163 totalWarnings = container.find( '.redux-field-warning' ).length; 164 165 if ( totalWarnings > 0 ) { 166 container.find( '.redux-field-warnings span' ).text( totalWarnings ); 167 container.find( '.redux-field-warnings' ).slideDown(); 168 container.find( '.redux-group-tab' ).each( 169 function() { 170 total = $( this ).find( '.redux-field-warning' ).length; 171 172 if ( total > 0 ) { 173 sectionID = $( this ).attr( 'id' ).split( '_' ); 174 175 sectionID = sectionID[0]; 176 container.find( '.redux-group-tab-link-a[data-key="' + sectionID + '"]' ).prepend( '<span class="redux-menu-warning">' + total + '</span>' ); 177 container.find( '.redux-group-tab-link-a[data-key="' + sectionID + '"]' ).addClass( 'hasWarning' ); 178 179 subParent = container.find( '.redux-group-tab-link-a[data-key="' + sectionID + '"]' ).parents( '.hasSubSections:first' ); 180 181 if ( subParent ) { 182 subParent.find( '.redux-group-tab-link-a:first' ).addClass( 'hasWarning' ); 183 } 184 } 185 } 186 ); 187 } 188 } 189 ); 190 } 191 }; 192 })( jQuery );