var GformShortcodeUI;!function(a,b){var c=window.GformShortcodeUI={models:{},collections:{},views:{},utils:{},strings:{}};c.models.ShortcodeAttribute=Backbone.Model.extend({defaults:{attr:"",label:"",type:"",section:"",description:"","default":"",value:""}}),c.models.ShortcodeAttributes=Backbone.Collection.extend({model:c.models.ShortcodeAttribute,clone:function(){return new this.constructor(_.map(this.models,function(a){return a.clone()}))}}),c.models.Shortcode=Backbone.Model.extend({defaults:{label:"",shortcode_tag:"",action_tag:"",attrs:c.models.ShortcodeAttributes},set:function(a,b){return void 0===a.attrs||a.attrs instanceof c.models.ShortcodeAttributes||(_.each(a.attrs,function(a){void 0!=a["default"]&&(a.value=a["default"])}),a.attrs=new c.models.ShortcodeAttributes(a.attrs)),Backbone.Model.prototype.set.call(this,a,b)},toJSON:function(a){return a=Backbone.Model.prototype.toJSON.call(this,a),void 0!==a.attrs&&a.attrs instanceof c.models.ShortcodeAttributes&&(a.attrs=a.attrs.toJSON()),a},clone:function(){var a=Backbone.Model.prototype.clone.call(this);return a.set("attrs",a.get("attrs").clone()),a},formatShortcode:function(){var a,b,c=[];return this.get("attrs").each(function(a){var d=a.get("value"),e=a.get("type"),f=a.get("default");(!d||d.length<1)&&"checkbox"!=e||"checkbox"==e&&"true"!=f&&!d||("content"===a.get("attr")?b=a.get("value"):c.push(a.get("attr")+'="'+d+'"'))}),a="[{{ shortcode }} {{ attributes }}]",b&&b.length>0&&(a+="{{ content }}[/{{ shortcode }}]"),a=a.replace(/{{ shortcode }}/g,this.get("shortcode_tag")),a=a.replace(/{{ attributes }}/g,c.join(" ")),a=a.replace(/{{ content }}/g,b)},validate:function(a){var b=[],d=a.attrs.findWhere({attr:"id"});return d.get("value")||b.push({id:c.strings.pleaseSelectAForm}),b.length?b:null}}),c.collections.Shortcodes=Backbone.Collection.extend({model:c.models.Shortcode}),c.views.editShortcodeForm=wp.Backbone.View.extend({el:"#gform-shortcode-ui-container",template:wp.template("gf-shortcode-default-edit-form"),hasAdvancedValue:!1,events:{"click #gform-update-shortcode":"insertShortcode","click #gform-insert-shortcode":"insertShortcode","click #gform-cancel-shortcode":"cancelShortcode"},initialize:function(){_.bindAll(this,"beforeRender","render","afterRender");var a=this;this.render=_.wrap(this.render,function(b){return a.beforeRender(),b(),a.afterRender(),a}),this.model.get("attrs").each(function(b){switch(b.get("section")){case"required":a.views.add(".gf-edit-shortcode-form-required-attrs",new c.views.editAttributeField({model:b,parent:a}));break;case"standard":a.views.add(".gf-edit-shortcode-form-standard-attrs",new c.views.editAttributeField({model:b,parent:a}));break;default:a.views.add(".gf-edit-shortcode-form-advanced-attrs",new c.views.editAttributeField({model:b,parent:a})),a.hasAdvancedVal||(a.hasAdvancedVal=""!==b.get("value"))}}),this.listenTo(this.model,"change",this.render)},beforeRender:function(){},afterRender:function(){gform_initialize_tooltips(),b("#gform-insert-shortcode").toggle("insert"==this.options.viewMode),b("#gform-update-shortcode").toggle("insert"!=this.options.viewMode),b("#gf-edit-shortcode-form-advanced-attrs").toggle(this.hasAdvancedVal)},insertShortcode:function(a){var b=this.model.isValid({validate:!0});b?(send_to_editor(this.model.formatShortcode()),tb_remove(),this.dispose()):_.each(this.model.validationError,function(a){_.each(a,function(a,b){alert(a)})})},cancelShortcode:function(a){tb_remove(),this.dispose()},dispose:function(){this.remove(),b("#gform-shortcode-ui-wrap").append('
')}}),c.views.editAttributeField=Backbone.View.extend({tagName:"div",initialize:function(a){this.parent=a.parent},events:{'keyup input[type="text"]':"updateValue","keyup textarea":"updateValue","change select":"updateValue","change #gf-shortcode-attr-action":"updateAction","change input[type=checkbox]":"updateCheckbox","change input[type=radio]":"updateValue","change input[type=email]":"updateValue","change input[type=number]":"updateValue","change input[type=date]":"updateValue","change input[type=url]":"updateValue"},render:function(){return this.template=wp.media.template("gf-shortcode-ui-field-"+this.model.get("type")),this.$el.html(this.template(this.model.toJSON()))},updateValue:function(a){var c=b(a.target);this.model.set("value",c.val())},updateCheckbox:function(a){var c=b(a.target),d=c.prop("checked");this.model.set("value",d)},updateAction:function(a){var d=b(a.target),e=d.val();this.model.set("value",e);var f=this.parent.model,g=c.shortcodes.findWhere({shortcode_tag:"gravityform",action_tag:e}),h=f.get("attrs");g.get("attrs").each(function(a){var b=a.get("attr"),c=h.findWhere({attr:b});if("undefined"!=typeof c){var d=c.get("attr");if(b==d){var e=c.get("value");a.set("value",String(e))}}}),b(this.parent.el).empty();var i=this.parent.options.viewMode;this.parent.dispose(),this.parent.model.set(g),GformShortcodeUI=new c.views.editShortcodeForm({model:g,viewMode:i}),GformShortcodeUI.render()}}),c.utils.shortcodeViewConstructor={initialize:function(a){this.shortcodeModel=this.getShortcodeModel(this.shortcode)},getShortcodeModel:function(a){var b="undefined"!=typeof a.attrs.named.action?a.attrs.named.action:"",d=c.shortcodes.findWhere({action_tag:b});if(d){var e=d.clone();return e.get("attrs").each(function(b){b.get("attr")in a.attrs.named&&b.set("value",a.attrs.named[b.get("attr")]),"content"===b.get("attr")&&"content"in a&&b.set("value",a.content)}),e}},getContent:function(){return this.content||this.fetch(),this.content},fetch:function(){var a=this;if(!this.fetching){this.fetching=!0;var c,d=this.shortcodeModel.get("attrs").findWhere({attr:"id"}),e=d.get("value");c={action:"gf_do_shortcode",post_id:b("#post_ID").val(),form_id:e,shortcode:this.shortcodeModel.formatShortcode(),nonce:gfShortcodeUIData.previewNonce},b.post(ajaxurl,c).done(function(b){a.content=b}).fail(function(){a.content=''+gfShortcodeUIData.strings.errorLoadingPreview+""}).always(function(){delete a.fetching,a.render()})}},setLoader:function(){this.setContent('