learndash-admin-binary-selector.min.js
12.5 KB
function learndash_binary_selector(e){var r=this;r.selector_div=e,r.selector_id=jQuery(r.selector_div).prop("id"),r.nonce=jQuery(r.selector_div).data("nonce"),r.selected_items={},r.selector_data_loaded=!1,r.selector_data=[],this.init=function(){r.get_selector_data(!0),r.get_selector_form_element(),r.update_legend(),r.init_actions(),r.init_pager(),r.init_search()},this.init_actions=function(){jQuery(".learndash-binary-selector-section-middle",r.selector_div).on("click","a.learndash-binary-selector-button-add",r.add_selected_items),jQuery(".learndash-binary-selector-section-middle",r.selector_div).on("click","a.learndash-binary-selector-button-remove",r.remove_selected_items)},this.init_pager=function(){jQuery("ul.learndash-binary-selector-pager",r.selector_div).length&&(jQuery("ul.learndash-binary-selector-pager",r.selector_div).each((function(e){var a=this;if(jQuery(a).hasClass("learndash-binary-selector-pager-left"))var t="left";else if(jQuery(a).hasClass("learndash-binary-selector-pager-right"))t="right";void 0!==t&&void 0!==r.selector_data[t]&&void 0!==r.selector_data[t].pager&&r.update_pager(a,r.selector_data[t].pager)})),jQuery(".learndash-binary-selector-section ul.learndash-binary-selector-pager a",r.selector_div).on("click",(function(e){e.preventDefault(),r.handle_pager(this)})))},this.init_search=function(){if(jQuery(".learndash-binary-selector-search",r.selector_div).length){var e,a={};jQuery(".learndash-binary-selector-search",r.selector_div).on("focus",(function(){var t=this,s=r.get_section_el(t);if(void 0!==s){var l=jQuery(r.selector_div).attr("id");if(void 0!==l){if(jQuery(t).hasClass("learndash-binary-selector-search-left"))var n="left";else{if(!jQuery(t).hasClass("learndash-binary-selector-search-right"))return;n="right"}a[n]={},a[n].query_data={},void 0!==r.selector_data[n]&&void 0!==r.selector_data.selector_class&&void 0!==r.selector_data.selector_nonce&&void 0!==r.selector_data.query_vars&&(a[n].query_data.query_vars=r.selector_data.query_vars,a[n].query_data.position=n,a[n].query_data.selector_class=r.selector_data.selector_class,a[n].query_data.selector_nonce=r.selector_data.selector_nonce,a[n].selector_html_id=l,a[n].query_data.selected_ids=r.get_selector_form_element(!0),a[n].current_value=jQuery(t).val(),""!=e&&clearInterval(e),e=setInterval((function(){if(a[n].current_value=jQuery(t).val(),""==a[n].current_value){if(a[n].query_data.query_vars.search!=a[n].current_value){a[n].query_data.query_vars.search=a[n].current_value,a[n].query_data.query_vars=r.selector_data.query_vars,a[n].query_data.query_vars.search="";var l={action:"learndash_binary_selector_pager",nonce:r.nonce,query_data:a[n].query_data};jQuery.ajax({type:"POST",url:ajaxurl,dataType:"json",cache:!1,data:l,error:function(e,r,a){},success:function(e){void 0!==e.html_options&&jQuery(".learndash-binary-selector-items",s).empty().append(e.html_options);var a=jQuery("ul.learndash-binary-selector-pager",s);void 0!==a&&(jQuery(a).show(),void 0!==e.pager&&(r.selector_data[n].pager=e.pager,r.update_pager(a,r.selector_data[n].pager),r.update_legend()))}})}}else{if(a[n].current_value.length>=3&&a[n].query_data.query_vars.search!=a[n].current_value){a[n].query_data.query_vars.search=a[n].current_value,a[n].query_data.query_vars.paged=1;l={action:"learndash_binary_selector_pager",nonce:r.nonce,query_data:a[n].query_data};jQuery.ajax({type:"POST",url:ajaxurl,dataType:"json",cache:!1,data:l,error:function(e,r,a){},success:function(e){if(void 0!==e.html_options?jQuery(".learndash-binary-selector-items",s).empty().append(e.html_options):jQuery(".learndash-binary-selector-items",s).empty().append(""),void 0!==e.pager){void 0!==(a=jQuery(".learndash-binary-selector-pager",s))&&(jQuery(a).show(),r.selector_data[n].pager=e.pager,r.update_pager(a,e.pager),r.update_legend())}else{var a=jQuery(".learndash-binary-selector-pager",s);jQuery(a).hide()}}})}jQuery(".learndash-binary-selector-search",r.section_el).is(":focus")||clearInterval(e)}}),200))}}}))}},this.update_legend=function(){var e=jQuery("select.learndash-binary-selector-items-left option",r.selector_div).length;jQuery(".learndash-binary-selector-legend-left span.items-total-count",r.selector_div).html(e);e=jQuery("select.learndash-binary-selector-items-right option",r.selector_div).length;jQuery(".learndash-binary-selector-legend-right span.items-total-count",r.selector_div).html(e)},this.handle_pager=function(e){var a=r.get_section_el(e);if(void 0!==a){var t=jQuery(r.selector_div).attr("id");if(void 0!==t){var s=jQuery(e).parents("ul.learndash-binary-selector-pager");if(void 0!==s){var l={};if(void 0!==r.selector_data.query_vars&&(l.query_vars=r.selector_data.query_vars,void 0!==r.selector_data.selector_class)){if(l.selector_class=r.selector_data.selector_class,l.selector_nonce=r.selector_data.selector_nonce,l.selector_html_id=t,jQuery(s).hasClass("learndash-binary-selector-pager-left"))var n="left";else if(jQuery(s).hasClass("learndash-binary-selector-pager-right"))n="right";if(void 0!==n&&(l.position=n,void 0!==r.selector_data[n]&&void 0!==r.selector_data[n].pager)){var i=r.selector_data[n].pager;if(jQuery(e).hasClass("learndash-binary-selector-pager-prev")){if(1==i.current_page)return;l.query_vars.paged=parseInt(i.current_page)-1}else if(jQuery(e).hasClass("learndash-binary-selector-pager-next")){if(parseInt(i.current_page)==parseInt(i.total_pages))return;l.query_vars.paged=parseInt(i.current_page)+1}l.selected_ids=r.get_selector_form_element(!0);var o={action:"learndash_binary_selector_pager",nonce:r.nonce,query_data:l};jQuery.ajax({type:"POST",url:ajaxurl,dataType:"json",cache:!1,data:o,error:function(e,r,a){},success:function(e){void 0!==e.html_options&&jQuery(".learndash-binary-selector-items",a).empty().append(e.html_options),void 0!==e["query-vars"]&&(r.selector_data[n]["query-vars"]=e["query-vars"]),void 0!==e.pager&&(r.selector_data[n].pager=e.pager,r.update_pager(s,r.selector_data[n].pager),r.update_legend())}})}}}}}},this.update_pager=function(e,r){if(void 0!==e){if(void 0!==r.current_page)var a=parseInt(r.current_page);else a=0;if(void 0!==r.total_pages)var t=parseInt(r.total_pages);else t=0;if(void 0!==r.total_items)var s=parseInt(r.total_items);else s=0;a>=1&&t>=1?(jQuery(".learndash-binary-selector-pager-info span.current_page",e).html(a),jQuery(".learndash-binary-selector-pager-info span.current_page",e).show(),jQuery(".learndash-binary-selector-pager-info span.total_pages",e).html(t),jQuery(".learndash-binary-selector-pager-info span.total_pages",e).show(),jQuery(".learndash-binary-selector-pager-info span.total_items span.total_items_count",e).html(s),jQuery(".learndash-binary-selector-pager-info span.total_items",e).show(),jQuery(".learndash-binary-selector-pager-info",e).show(),1==a?jQuery(".learndash-binary-selector-pager-prev a",e).hide():jQuery(".learndash-binary-selector-pager-prev a",e).show(),a==t?jQuery(".learndash-binary-selector-pager-next a",e).hide():jQuery(".learndash-binary-selector-pager-next a",e).show()):(jQuery(".learndash-binary-selector-pager-info span.current_page",e).hide(),jQuery(".learndash-binary-selector-pager-info span.total_pages",e).hide(),jQuery(".learndash-binary-selector-pager-prev a",e).hide(),jQuery(".learndash-binary-selector-pager-next a",e).hide())}},this.init_lazy_load=function(){},this.add_selected_items=function(e){e.preventDefault();var a=[];jQuery(".learndash-binary-selector-items-left",r.selector_div).length&&jQuery(".learndash-binary-selector-items-right",r.selector_div).length&&jQuery(".learndash-binary-selector-items-left option:selected",r.selector_div).each((function(){var e=jQuery(this);if(!e.hasClass("learndash-binary-selector-item-disabled")){jQuery(e,r.selector_div).removeClass("learndash-binary-selector-item-selected"),jQuery(e,r.selector_div).clone().appendTo(jQuery(".learndash-binary-selector-items-right",r.selector_div)),jQuery(e,r.selector_div).addClass("learndash-binary-selector-item-disabled"),jQuery(e,r.selector_div).prop("selected",!1),jQuery(e,r.selector_div).prop("disabled",!0);var t=jQuery(e).attr("data-value");""!=t&&a.push(t)}})),r.update_selected_form_element("add",a)},this.remove_selected_items=function(e){e.preventDefault();var a=[];jQuery(".learndash-binary-selector-items-left",r.selector_div).length&&jQuery(".learndash-binary-selector-items-right",r.selector_div).length&&jQuery(".learndash-binary-selector-items-right option:selected",r.selector_div).each((function(){var e=jQuery(this),t=jQuery(e).attr("data-value");""!=t&&a.push(t),jQuery('.learndash-binary-selector-items-left option[data-value="'+t+'"]',r.selector_div).length&&(jQuery('.learndash-binary-selector-items-left option[data-value="'+t+'"]',r.selector_div).removeClass("learndash-binary-selector-item-disabled"),jQuery('.learndash-binary-selector-items-left option[data-value="'+t+'"]',r.selector_div).removeClass("learndash-binary-selector-item-selected"),jQuery('.learndash-binary-selector-items-left option[data-value="'+t+'"]',r.selector_div).prop("disabled",!1)),jQuery(e).remove()})),r.update_selected_form_element("remove",a)},this.get_section_el=function(e){if(void 0!==e)return jQuery(e).parents(".learndash-binary-selector-section")},this.update_selected_form_element=function(e,a){a.length&&("add"==e?(jQuery.each(a,(function(e,a){var t=parseInt(a);-1==jQuery.inArray(t,r.selected_items)&&r.selected_items.push(t)})),r.save_selector_form_element()):"remove"==e&&(jQuery.each(a,(function(e,a){var t=parseInt(a),s=jQuery.inArray(t,r.selected_items);-1!==s&&r.selected_items.splice(s,1)})),r.save_selector_form_element()))},this.get_selector_form_element=function(e){void 0===e&&(e=!1);var a=jQuery("input.learndash-binary-selector-form-element",r.selector_div).val();return 0==e?(r.selected_items=JSON.parse(a),r.selected_items=jQuery.map(r.selected_items,(function(e){return parseInt(e)})),r.selected_items):a},this.save_selector_form_element=function(){jQuery("input.learndash-binary-selector-form-element",r.selector_div).val(JSON.stringify(r.selected_items)),jQuery("input.learndash-binary-selector-form-changed",r.selector_div).val("1")},this.sort_right_options=function(){},this.get_selector_data=function(e){if(void 0===e&&(e=!1),1!=r.selector_data_loaded||!0===e){if(r.selector_data_loaded=!0,element_data=jQuery(r.selector_div).attr("data"),"undefined"==typeof element_data)return;r.selector_data=JSON.parse(element_data)}return r.selector_data},this.get_selector_data_element=function(e){var a=r.get_selector_data();if(void 0!==a&&void 0!==e&&void 0!==a[e])return a[e]},this.set_selector_data_element=function(e,a){r.get_selector_data(),this.selector_data[e]=a},this.update_selector_data=function(e){jQuery(r.selector_div).attr("data",JSON.stringify(e)),this.selector_data=e},this.lazy_load_items=function(e){void 0===e&&(e=r.get_selector_data()),void 0===e.query_vars.paged&&(e.query_vars.paged=0),e.query_vars.paged=parseInt(e.query_vars.paged)+1;var a={action:"learndash_binary_selector_lazy_loader",nonce:r.nonce,"query-data":e};jQuery.ajax({type:"POST",url:ajaxurl,dataType:"json",cache:!1,data:a,error:function(e,r,a){},success:function(e){void 0!==e&&void 0!==e.html_options&&(""!=e.html_options?(jQuery("select.learndash-binary-selector-items-left",r.selector_div).append(e.html_options),delete e.html_options,r.update_selector_data(e),r.update_legend(),r.lazy_load_items(e)):(r.update_selector_data(e),r.set_selected_items()))}})},this.set_selected_items=function(){r.get_selector_form_element(),jQuery("select.learndash-binary-selector-items-right",r.selector_div).empty(),jQuery("select.learndash-binary-selector-items-left > option.learndash-binary-selector-item ",r.selector_div).removeClass("learndash-binary-selector-item-disabled"),jQuery("select.learndash-binary-selector-items-left > option.learndash-binary-selector-item ",r.selector_div).prop("selected",!1),jQuery("select.learndash-binary-selector-items-left > option.learndash-binary-selector-item ",r.selector_div).prop("disabled",!1),jQuery.each(r.selected_items,(function(e,a){jQuery('.learndash-binary-selector-items-left option.learndash-binary-selector-item[value="'+a+'"]',r.selector_div).each((function(){var e=jQuery(this);jQuery(e,r.selector_div).clone().appendTo(jQuery(".learndash-binary-selector-items-right",r.selector_div)),jQuery(e,r.selector_div).addClass("learndash-binary-selector-item-disabled"),jQuery(e,r.selector_div).prop("selected",!1),jQuery(e,r.selector_div).prop("disabled",!0)}))})),r.update_legend()}}jQuery((function(){var e=[];jQuery(".learndash-binary-selector").each((function(){var r=jQuery(this).prop("id");void 0!==r&&(e[r]=new learndash_binary_selector(this),e[r].init())}))})),jQuery.expr[":"].Contains=jQuery.expr.createPseudo((function(e){return function(r){return jQuery(r).text().toUpperCase().indexOf(e.toUpperCase())>=0}}));