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