1
|
|
|
!function(t,e){var i;void 0===e.Amarkal?(i={},e.Amarkal=i):i=e.Amarkal,i.settings={haveBeenModified:!1,init:function(){t(".amarkal-ui-component").on("amarkal.change",function(t,e){i.settings.notifier.notice('Settings have changed, click "Save" to apply them.'),i.settings.sections.flag("notice",e.props.section),i.settings.fields.flag("notice",e.props.name),i.settings.haveBeenModified=!0}),t(window).on("beforeunload",function(){if(i.settings.haveBeenModified)return"Are you sure you want to leave this page?"}),i.settings.fields.init(),i.settings.search.init(),i.settings.sections.init()}},t(document).ready(function(){i.settings.init()}),i.settings.notifier={classes:["error","success","notice","info"],prefix:"amarkal-",timeout:null,$el:null,notify:function(t,e,s){clearTimeout(i.settings.notifier.timeout),i.settings.notifier.$el.removeAttr("class").addClass(i.settings.notifier.prefix+t).html("<p>"+e+"</p>"),void 0!==s&&(i.settings.notifier.timeout=setTimeout(function(){i.settings.notifier.$el.removeAttr("class").html("")},s))},error:function(t,e){i.settings.notifier.notify("error",t,e)},success:function(t,e){i.settings.notifier.notify("success",t,e)},notice:function(t,e){i.settings.notifier.notify("notice",t,e)},info:function(t,e){i.settings.notifier.notify("info",t,e)},init:function(){this.$el=t("#amarkal-settings-notices")}},t(document).ready(function(){i.settings.notifier.init()}),i.settings.search={$input:null,init:function(){this.$input=t("#settings-search"),this.$input.on("keyup",this.onKeyup.bind(this))},onKeyup:function(e){var s=this.$input.val().toLowerCase().replace(/[^\w\d\s]/g,"");if(s.length>0){var n=i.settings.fields.search(s);i.settings.sections.deactivate(),i.settings.header.setSectionTitle("Field Search Results"),i.settings.header.setSectionSubtitle(""),i.settings.fields.hideAll(),i.settings.fields.show(n),t("#settings-search-results").addClass("visible").text(n.length?n.length+" settings found":"Nothing found")}else t("#settings-search-results").removeClass("visible").text(""),i.settings.sections.activate(i.settings.sections.prevSection)}},i.settings.sections={$links:null,$loader:null,data:null,activeSection:null,prevSection:null,init:function(){this.data=JSON.parse(t("#sections-config").text()),this.$links=t(".amarkal-settings-sections li"),this.$loader=t("#amarkal-settings-loader"),this.initSections()},initSections:function(){if("object"==typeof this.data&&Object.keys(this.data).length>0){var e=this;this.$links.on("click",function(){e.activate(t(this).attr("data-slug"))}),this.activateInitialSection(),this.$loader.hide()}else i.settings.fields.showAll()},activateInitialSection:function(){""!==window.location.hash?this.activate(window.location.hash.substring(1)):this.activate(t(this.$links[0]).attr("data-slug"))},activate:function(t){this.activeSection!==t&&(null!==this.activeSection&&(this.prevSection=this.activeSection),this.activeSection=t,this.$links.removeClass("active").filter('[data-slug="'+t+'"]').addClass("active"),i.settings.header.setSectionTitle(this.getTitle(t)),i.settings.header.setSectionSubtitle(this.getSubtitle(t)),i.settings.fields.showBySection(t),window.location="#"+t)},deactivate:function(){this.activeSection;this.$links.removeClass("active"),i.settings.fields.hideAll(),this.activeSection=null},flag:function(t,e){this.$links.filter('[data-slug="'+e+'"]').addClass("flag-"+t)},unflagAll:function(){this.$links.removeClass("flag-error flag-notice")},getTitle:function(t){return this.data[t].title},getSubtitle:function(t){return this.data[t].subtitle}},i.settings.fields={$fields:null,init:function(){this.$fields=t(".amarkal-settings-field")},hideAll:function(){this.hide(this.$fields)},showAll:function(){this.show(this.$fields)},showBySection:function(t){this.hideAll(),this.show(this.$fields.filter('[data-section="'+t+'"]'))},search:function(e){var i=[];return this.$fields.each(function(){var s=t(this),n=s.find("h3").text().toLowerCase(),a=s.find(".help-content").text();description=s.find(".description").text(),(n.match(e)||description.match(e)||a.match(e))&&i.push(s)}),t(i).map(function(){return this.toArray()})},show:function(t){t.addClass("visible").find(".amarkal-ui-component").amarkalUIComponent("refresh")},hide:function(t){t.removeClass("visible")},flag:function(t,e){this.$fields.filter('[data-name="'+e+'"]').addClass("flag-"+t)}},i.settings.header={setSectionTitle:function(e){t("#amarkal-settings-sec-title").text(e)},setSectionSubtitle:function(e){t("#amarkal-settings-sec-subtitle").text(e)}},t(document).ready(function(){t(".amarkal-settings-button").on("click",function(e){e.preventDefault();var s=t(this);s.data("disabled")||(t(".amarkal-settings-button").data("disabled",!0),s.addClass("processing"),i.settings[s.val()](function(){s.removeClass("processing"),t(".amarkal-settings-button").data("disabled",!1)}))})}),i.settings.save=function(e){i.settings._postData("save",function(s){if(i.settings._clearNotices(),t.isEmptyObject(s.errors))i.settings.notifier.success("Settings saved",3e3);else{for(var n in s.errors){var a=t('[amarkal-component-name="'+n+'"]'),o=a.amarkalUIComponent("instance");a.amarkalUIComponent("makeInvalid"),a.parent().children(".amarkal-settings-error").addClass("amarkal-visible").html(s.errors[n])}i.settings.notifier.error("Some errors have occured, see below for more information."),i.settings.sections.flag("error",o.props.section),i.settings.fields.flag("error",o.props.name)}t("#amarkal-settings-form").amarkalUIForm("setData",s.values,s.errors),e()})},i.settings.resetAll=function(e){i.settings._postData("reset_all",function(s){i.settings._clearNotices(),i.settings.notifier.success("Default settings applied",3e3),t("#amarkal-settings-form").amarkalUIForm("setData",s.values,s.errors),e()})},i.settings.resetSection=function(e){i.settings._postData("reset_section",function(s){i.settings._clearNotices(),i.settings.notifier.success("Default settings applied for the section <strong>"+i.settings.sections.getTitle(i.settings.sections.activeSection)+"</strong>",3e3),t("#amarkal-settings-form").amarkalUIForm("setData",s.values,s.errors),e()})},i.settings._clearNotices=function(){t(".amarkal-ui-component").amarkalUIComponent("reset"),t(".amarkal-settings-error").removeClass("amarkal-visible").html(""),i.settings.sections.unflagAll()},i.settings._postData=function(e,s){var n=t("#amarkal-settings-form").amarkalUIForm("getData");t("#amarkal-settings-form").find('input[name^="_amarkal"]').each(function(){n[t(this).attr("name")]=t(this).val()}),n._amarkal_settings_section=i.settings.sections.activeSection,t.post(ajaxurl,{action:"amarkal_settings_"+e,data:n},s)}}(jQuery,window); |