text: define(["jquery","underscore","backbone","config","utils","text!templates/setup.html","views/modal-view"],function(e,t,n,r,i,s,o){var u=o.extend({tagName:"div",className:"setup-view modal fade",events:{"click #deleteMapButton":"deleteMapClicked","click #saveCloseButton":"saveClicked","click #cancelButton":"cancelClicked","click #savePositionButton":"savePositionClicked","click #saveViewOptionsButton":"saveViewClicked","click .sharing-toggle":"sharingToggleClicked"},initialize:function(e){this.template=t.template(s),u.__super__.initialize.apply(this,arguments),this.mapInfoChanged=!1,this.initialAreaChanged=this.viewOptionsChanged=!0,this.listenTo(this.model,"sync",this.populateFromModel)},populateFromModel:function(){var t=this,n=this.model.attributes;this.$(".map-name").html(n.title+" Setup"),this.modelFieldInputs.each(function(){e(this).removeClass("error");var t=this.name.split(".");t.length==2?n[t[0]]&&e(this).val(n[t[0]][t[1]]):e(this).val(n[this.name])}),this.updateSharingToggles(this.model.attributes.sharing),this.$(".public-url").val(this.model.publicUrl({secret:!1})),this.$(".secret-url").val(this.model.publicUrl({secret:!0})),t.setMapInfoChanged(!1)},updateSharingToggles:function(e){switch(e){case SharingType.PRIVATE:this.$(".is-private").show(),this.$(".is-public").hide();break;case SharingType.WORLD:this.$(".is-public").show(),this.$(".is-private").hide()}this.$(".model-input[name=sharing]").val(e)},sharingToggleClicked:function(t){return this.updateSharingToggles(e(t.currentTarget).attr("data-value")),this.setMapInfoChanged(!0),!1},focusTab:function(t){self.$(t).trigger("click");var n=self.$(".tab-pane.active").find("input, textarea");for(var r=0;r<n.length;r++)if(!e(n[r]).val()){e(n[r]).focus();return}},render:function(){var t=this;return u.__super__.render.call(this),this.$(".urls input").click(function(){e(this).select()}),this.$(".enter-email").click(function(){return t.focusEmail(),!1}),this.modelFieldInputs=this.$("form.map-setup .model-input"),this.modelFieldInputs.each(function(){e(this).on("change keydown",function(){t.setMapInfoChanged(!0)})}),this.$("#map-sharing button").on("click",function(){t.setMapInfoChanged(!0)}),e(this.el).on("hidden.bs.modal",function(){app.navigate(app.currentMapUri(null))}),this.populateFromModel(),this},setMapInfoChanged:function(e){var t=this;t.mapInfoChanged=e,e?t.$("#cancelButton").text(__("Cancel")):t.$("#cancelButton").text(__("Close")),t.$("#saveCloseButton").attr("disabled",!e),this.$("#saveCloseButton").attr("disabled",!e)},saveClicked:function(n){if(!this.mapInfoChanged)return this.close(),!1;var r={};this.modelFieldInputs.each(function(){r[this.name]=e(this).val()});var i=this;return this.$("#saveCloseButton").attr("disabled",!0),i.modelFieldInputs.removeClass("has-error"),this.model.save(r,{patch:!0,success:function(e,t,n){i.close(),i.trigger("map:saved"),i.setMapInfoChanged(!1)},error:function(n,r,s){var o=e.parseJSON(r.responseText);console.error("failed to update map: "+i.model.id),o&&o.errors&&t.each(o.errors,function(e){var t='input[name="'+e.path+'"]',n=i.$(t);n.addClass("has-error")}),i.$("#saveCloseButton").attr("disabled",!1)}}),!1},savePositionClicked:function(t){if(!this.initialAreaChanged)return;var n={initialArea:this.getInitialMapArea()},r=this;return this.$("#savePositionButton").attr("disabled",!0),this.model.save(n,{patch:!0,success:function(e,t,n){},error:function(t,n,i){var s=e.parseJSON(n.responseText);console.error("failed to update map: "+r.model.id),s&&s.errors&&console.error("errors:",s.errors),r.$("#savePositionButton").attr("disabled",!1)}}),!1},saveViewClicked:function(t){if(!this.viewOptionsChanged)return;var n={viewOptions:app.getCurrentViewOptions()},r=this;return this.$("#saveViewOptionsButton").attr("disabled",!0),this.model.save(n,{patch:!0,success:function(e,t,n){},error:function(t,n,i){var s=e.parseJSON(n.responseText);console.error("failed to update map: "+r.model.id),s&&s.errors&&console.error("errors:",s.errors),r.$("#saveViewOptionsButton").attr("disabled",!1)}}),!1},cancelClicked:function(e){return this.close(),!1},deleteMapClicked:function(e){var t=this,n=this.model.id;return window.confirm(__("Are you sure you want to delete this map? This action cannot be reversed!"))&&this.model.destroy({success:function(e,t,r){console.log("deleted map: "+n),window.location=window.BASE_URL},error:function(e,t,r){console.error("failed to delete map: "+n,t)}}),!1},getInitialMapArea:function(){var e=app.mapView.getVisibleMapArea();return delete e.bounds,e},show:function(){this.populateFromModel(),this.initialAreaChanged=app.mapView&&!t.isEqual(this.model.attributes.initialArea,this.getInitialMapArea()),this.viewOptionsChanged=app.mapView&&!t.isEqual(this.model.attributes.viewOptions,app.getCurrentViewOptions()),this.$("#savePositionButton").attr("disabled",!this.initialAreaChanged),this.$("#saveViewOptionsButton").attr("disabled",!this.viewOptionsChanged),u.__super__.show.call(this)}});return u});