!function(e){formintorjs.define(["admin/settings/multi-setting","text!tpl/fields.html"],function(t,i){return t.extend({multiple:!1,events:{"click .wpmudev-add_answer":"add_answer","click .wpmudev-answer-toggle-container":"toggle_answer","click .wpmudev-remove-answer":"delete_answer","click .wpmudev-preview--image":"add_image","click .wpmudev-url--input":"add_image","click .wpmudev-url--clear":"clear_image","change .wpmudev-answer-value":"update_value","change .wpmudev-quiz-results":"update_result","change .wpmudev-answer-toggle":"update_toggle"},className:"wpmudev-option",init:function(e){this.app=Forminator.Data.application||!1,this.results=e.results,this.question_index=e.question_index,this.answers=this.model.get("answers"),this.listenTo(Forminator.Events,"forminator:quiz:results:updated",this.render)},render:function(){this.$el.html(""),this.$el.append(this.get_label_html()),this.$el.append(this.get_field_html()),this.answers.length>0&&this.$el.find(".wpmudev-multianswer").addClass("wpmudev-has_answers"),this.trigger("rendered",this.get_value()),this.on_render()},get_values_html:function(){var e=this;return this.answers.map(function(t,i){return e.get_value_html(t,i)}).join(" ")},get_value_html:function(t,s){var n=(this.get_saved_value(),!1),a=Forminator.Utils.template(e(i).find("#settings-field-question-multiple-answer-tpl").html());return"nowrong"===this.app&&(n=this.results.toJSON()),a({row:t.toJSON(),index:s,question_index:this.question_index,type:this.app,results:n})},get_field_html:function(){var t=this.get_values_html();return Forminator.Utils.template(e(i).find("#settings-field-question-answer-list-tpl").html())({childs:t})},add_answer:function(e){e.preventDefault(),new_answer=new Forminator.Models.Answer({}),this.answers.add(new_answer,{silent:!0}),this.render()},delete_answer:function(t){t.preventDefault();var i=e(t.target),s=this.get_model(i);this.answers.remove(s,{silent:!0}),this.render()},get_index:function(e){return e.closest(".wpmudev-answer").data("index")},get_model:function(e){var t=this.get_index(e);return this.answers.get_by_index(t)},move_option:function(e,t){var i=this.get_model(e),s=this.answers.model_index(i);this.answers.move_to(t,s),this.render()},update_value:function(t){var i=this.get_model(e(t.target));value=e(t.target).val(),i.set("title",value)},update_result:function(t){var i=this.get_model(e(t.target));value=e(t.target).val(),slug=Forminator.Utils.get_slug(value),i.set("result",slug)},update_toggle:function(t){t.preventDefault();var i=this.get_model(e(t.target));value=e(t.target).val(),this.answers.each(function(e,t){e.set("toggle",!1)}),this.$el.find(".wpmudev-answer-toggle").not(t.target).prop("checked",!1),e(t.target).is(":checked")?i.set("toggle",!0):i.set("toggle",!1)},toggle_answer:function(t){e(t.target).closest(".wpmudev-answer").toggleClass("wpmudev-is_open")},add_image:function(t){t.preventDefault();var i=this,s=wp.media({title:this.options.popup_label,button:{text:this.options.popup_button_label},multiple:!1}).on("select",function(){var n,a=i.get_model(e(t.target)),r=s.state().get("selection").first().toJSON(),l=e(t.target).closest(".wpmudev-answer"),u=l.find(".wpmudev-browse--preview"),d=l.find(".wpmudev-get_image"),o=l.find(".wpmudev-url--input");r&&r.url&&(u.addClass("wpmudev-has_image"),n=_.isUndefined(r.sizes.medium.url)?r.url:r.sizes.medium.url,d.css("background-image","url({url})".replace("{url}",r.sizes.thumbnail.url)),o.val("{url}".replace("{url}",n)),a.set("image",n))});s.open()},clear_image:function(t){var i=this.get_model(e(t.target)),s=e(t.target).closest(".wpmudev-answer"),n=s.find(".wpmudev-browse--preview"),a=s.find(".wpmudev-get_image"),r=s.find(".wpmudev-url--input");n.removeClass("wpmudev-has_image"),a.css("background-image",""),r.val(""),i.set("image","")},on_render:function(){var t=this;Forminator.Utils.init_select2(),setTimeout(function(){var i=e(".wpmudev-multianswer--list li:last-child").find(".wpmudev-input"),s=i.val();_.isEmpty(s)&&i.focus(),e(".wpmudev-multianswer--list").sortable({handle:".wpmudev-answer--move",update:function(e,i){t.move_option(i.item,i.item.index())}})},100)}})})}(jQuery);