!function(e){formintorjs.define(["admin/builder/builder/fields-panel","admin/builder/builder/shadows-panel","text!admin/templates/builder.html"],function(i,t,a){return Backbone.View.extend({mainTpl:Forminator.Utils.template(e(a).find("#builder-main-tpl").html()),buttonsTpl:Forminator.Utils.template(e(a).find("#builder-buttons-tpl").html()),noFieldsTpl:Forminator.Utils.template(e(a).find("#builder-main-no-fields-tpl").html()),events:{"click .forminator-save-changes":"save_changes","click .forminator-save-layout":"save_layout","click .wpmudev-button-cancel":"cancel_builder","click .forminator-add-new-field":"show_fields","change .wpmudev-input":"set_dirty"},$main:!1,$shadows_container:!1,fields:{},settings:{},dirty:!1,is_edit:!1,initialize:function(e){return this.wrappers=this.model.get("wrappers"),this.settings=this.model.get("settings"),this.listenTo(Forminator.Events,"dnd:reload:fields",this.render),this.listenTo(Forminator.Events,"dnd:models:updated",this.render),this.sidebar||(this.sidebar=new Forminator.Views.Builder.Sidebar({model:this.model,el:"#wpmudev-sidebar .wpmudev-sidebar--wrap"})),this.render()},render:function(){var i=Forminator.Data.currentForm.formID||-1;this.$el.html(this.mainTpl()),this.$el.find(".wpmudev-builder").append(this.buttonsTpl()),-1===i?e("#wpmudev-header h1").html(Forminator.l10n.builder.builder_new_title):(this.is_edit=!0,e("#wpmudev-header h1").html(Forminator.l10n.builder.builder_edit_title)),this.$main=this.$el.find(".wpmudev-builder--form"),this.$shadows_container=this.$el.find(".wpmudev-builder--shadow"),Forminator.Events.off("forminator:add:field:click");var t=this;e(window).off("beforeunload.forminator-leave-wizard-confirm"),e(window).on("beforeunload.forminator-leave-wizard-confirm",function(e){if(t.dirty)return Forminator.l10n.popup.save_alert}),this.render_fields(),this.render_shadows(),this.render_form_name()},render_fields:function(){if(0===this.wrappers.length)return this.show_placeholder(),void this.disable_save();this.enable_save(),this.fields_panel&&(this.fields_panel.off(),this.fields_panel.remove()),this.fields_panel=new i({model:this.model}),this.$main.append(this.fields_panel.$el)},disable_save:function(){this.$el.find(".forminator-save").attr("disabled",!0)},enable_save:function(){this.$el.find(".forminator-save").attr("disabled",!1)},render_shadows:function(){this.shadows_panel&&(this.$shadows_container.html(""),this.shadows_panel.off(),this.shadows_panel.remove()),this.shadows_panel=new t({el:this.$shadows_container,model:this.model}),this.$shadows_container.append(this.shadows_panel.$el)},render_form_name:function(){var e=new Forminator.Settings.Text({model:this.model,id:"builder-form-name",name:"formName",placeholder:Forminator.l10n.builder.form_name});this.$el.find(".wpmudev-builder--form-name").append(e.el)},show_placeholder:function(){this.$main.append(this.noFieldsTpl())},save_changes:function(i){i.preventDefault(),this.validate()?this.save(!1,!0):e("html, body").animate({scrollTop:0},500)},save_layout:function(i){i.preventDefault(),this.validate()?this.save(!0,!0):e("html, body").animate({scrollTop:0},500)},save:function(i,t){var a=this,n=Forminator.Utils.model_to_json(this.model),r=Forminator.Data.currentForm.formName||this.model.get("formName")||"",o=Forminator.Data.currentForm.formID||-1;t&&this.$el.find(".forminator-save").addClass("wpmudev-button-onload"),e.post({url:Forminator.Data.ajaxUrl,data:{action:"forminator_save_builder_fields",formName:r,formID:o,data:n}}).success(function(e){if(-1===o&&(Forminator.Data.currentForm.formID=e.data),a.is_edit&&(a.dirty=!1),t&&(setTimeout(function(){a.$el.find(".forminator-save").removeClass("wpmudev-button-onload")},500),!i)){var n=_.template("<strong>{{ formName }}</strong> {{ Forminator.l10n.options.been_saved }}");Forminator.Notification.open("success",n({formName:r}),4e3)}i&&(Forminator.Events.trigger("forminator:sidebar:close:settings"),Forminator.navigate("appearance",{trigger:!0}))}).error(function(){Forminator.Notification.open("error",Forminator.l10n.options.error_saving,5e3)})},validate:function(){var e=!1;return _.isEmpty(this.model.get("formName"))?(this.$el.find("#forminator-validate-name").show(),e=!0):this.$el.find("#forminator-validate-name").hide(),Forminator.Utils.ensure_max_of_type_field(this.model.get("wrappers"),"captcha",1)||(e=!0),!e},cancel_builder:function(e){e.preventDefault(),window.location.href=Forminator.Data.modules.custom_form.form_list_url},show_fields:function(i){i.preventDefault(),e("body.wp-admin").addClass("wpmudev-disabled--mobile"),e("#wpmudev-sidebar").addClass("wpmudev-is_active wpmudev-show")},set_dirty:function(){this.dirty=!0}})})}(jQuery);