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