!function(){var e,t;e=this,t=function(e){"use strict";e.Forms={}},"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Djblets=e.Djblets||{}),Djblets.Forms.ConditionChoice=Backbone.Model.extend({defaults:{name:null,valueField:null},initialize(e){this.operators=new Backbone.Collection(e.operators,{model:Djblets.Forms.ConditionOperator,parse:!0})},createValueField(e){var t=this.get("valueField");return new t.viewClass(_.defaults({model:new t.modelClass(_.defaults({fieldName:e},t.modelData))},t.viewData))},parse(e){return{id:e.id,name:e.name,valueField:Djblets.Forms.ConditionChoice.parseValueFieldData(e.valueField)}}},{parseValueFieldData(e){let t=null;var i;return void 0!==e&&(i=e.model,e=e.view,t={modelClass:Djblets.getObjectByName(i.className),modelData:i.data,viewClass:Djblets.getObjectByName(e.className),viewData:e.data}),t}}),Djblets.Forms.Condition=Backbone.Model.extend({defaults:{choice:null,error:null,operator:null,valid:!0,value:null},initialize(){this.on("change",()=>{var e=this.get("choice");e!==this.previous("choice")&&(this.set("operator",e.operators.first()),this.unset("value"))})},destroy(e){this.stopListening(),this.trigger("destroy",this,this.collection,e)}}),Djblets.Forms.ConditionOperator=Backbone.Model.extend({defaults:{name:null,useValue:!1,valueField:null},createValueField(e){var t=this.get("valueField");return console.assert(t,"This operator does not have a custom valueField."),new t.viewClass(_.defaults({model:new t.modelClass(_.defaults({fieldName:e},t.modelData))},t.viewData))},parse(e){return{id:e.id,name:e.name,useValue:e.useValue,valueField:Djblets.Forms.ConditionChoice.parseValueFieldData(e.valueField)}}}),Djblets.Forms.ConditionSet=Backbone.Model.extend({defaults:{fieldName:null,lastID:null},initialize(e){this.choices=new Backbone.Collection(e.choicesData,{model:Djblets.Forms.ConditionChoice,parse:!0});const s=this;this.conditions=new Backbone.Collection(e.conditionsData,{model:function(e,t){var i=e.choice||s.choices.get(e.choiceID),l=e.operator||(i?i.operators.get(e.operatorID):null),o=s.get("lastID"),o=null===o?0:o+1;return s.set("lastID",o),new Djblets.Forms.Condition({choice:i,error:e.error,id:o,operator:l,valid:e.valid,value:e.value},t)}})},addNewCondition(){var e=this.choices.first();this.conditions.add({choice:e,operator:e.operators.first()})},parse(e){return{fieldName:e.fieldName}}}),Djblets.Forms.ConditionValueField=Backbone.Model.extend({defaults:{fieldHTML:null}}),Djblets.Forms.BaseConditionValueFieldView=Backbone.View.extend({tagName:"span",render(){return this.$el.html(this.model.get("fieldHTML")),this},setValue(e){console.assert(!1,"setValue() must be implemented by this subclass.")},getValue(){console.assert(!1,"getValue() must be implemented by this subclass.")}});{const i=Backbone.View.extend({className:"conditions-field-row",tagName:"li",events:{"click .conditions-field-row-delete":"_onDeleteClicked"},initialize(e){this.listenTo(this.model,"destroy",()=>{this.$el.slideUp(e.rowAnimationSpeedMS,this.remove.bind(this))})},_onDeleteClicked(e){e.stopPropagation(),e.preventDefault(),this.model.destroy()}}),l=i.extend({template:_.template(`<a href="#"
   class="conditions-field-action conditions-field-row-delete">
 <span class="ink-i-delete-item"></span>
</a>
<span class="conditions-field-choice"></span>
<span class="conditions-field-operator"></span>
<span class="conditions-field-value"></span>
<% if (error) { %>
 <span class="conditions-field-error"><%- error %></span>
<% } %>`),events:_.extend({"change .conditions-field-choice select":"_onSelectChoiceChanged","change .conditions-field-operator select":"_onSelectOperatorChanged"},i.prototype.events),initialize(e){i.prototype.initialize.call(this,e),this.conditionSet=e.conditionSet,this._$choice=null,this._$operator=null,this._$valueWrapper=null,this._$newValue=null,this._defaultValueField=null,this._valueField=null},render(){this.$el.html(this.template(this.model.attributes));var e=this.conditionSet.get("fieldName"),t=this.model.get("id");return this._$choice=$("<select>").attr("name",e+`_choice[${t}]`),this.conditionSet.choices.each(e=>{this._$choice.append($("<option>").val(e.id).text(e.get("name")))}),this._$choice.appendTo(this.$el.children(".conditions-field-choice")),this._$operator=$("<select>").attr("name",e+`_operator[${t}]`).appendTo(this.$el.children(".conditions-field-operator")),this._$valueWrapper=this.$el.children(".conditions-field-value"),this.listenTo(this.model,"change:choice",this._onChoiceChanged),this.listenTo(this.model,"change:operator",this._onOperatorChanged),this.listenTo(this.model,"change:value",this._onValueChanged),this._onChoiceChanged(),this._onOperatorChanged(),this._onValueChanged(),this},_onChoiceChanged(){var e=this.model.get("choice"),t=this.conditionSet.get("fieldName"),i=this.model.get("id");this._$choice.val(e.id),this._defaultValueField=e.createValueField(t+`_value[${i}]`),this._$operator.empty(),e.operators.each(e=>{this._$operator.append($("<option>").val(e.id).text(e.get("name")))}),this._$operator.val(this.model.get("operator").id)},_onOperatorChanged(){var e,t,i=this.model.get("operator");let l;this._$operator.val(i.id),this._$valueWrapper.toggle(i.get("useValue")),(l=null!==i.get("valueField")?(e=this.conditionSet.get("fieldName"),t=this.model.get("id"),i.createValueField(e+`_value[${t}]`)):this._defaultValueField)!==this._valueField&&(null!==this._$newValue&&l!==this._valueField&&(this._$newValue.remove(),this._$newValue=null),this._valueField=l,this._$newValue=this._valueField.render().$el.appendTo(this._$valueWrapper))},_onValueChanged(){this._valueField.setValue(this.model.get("value"))},_onSelectChoiceChanged(){this.model.set("choice",this.conditionSet.choices.get(this._$choice.val()))},_onSelectOperatorChanged(){var e=this.model.get("choice");this.model.set("operator",e.operators.get(this._$operator.val()))}}),o=i.extend({render(){var e=this.model.get("value");return null!==e&&this.$(".conditions-field-value").text(e),this}});Djblets.Forms.ConditionSetView=Backbone.View.extend({DEFAULT_ROW_ANIMATION_SPEED_MS:300,events:{"change #conditions_mode input":"_onConditionModeChanged","click .conditions-field-add-condition a":"_onAddRowClicked"},initialize(e){this._rowAnimationSpeedMS=e.rowAnimationSpeedMS||this.DEFAULT_ROW_ANIMATION_SPEED_MS,this._$lastID=null,this._$rows=null},render(){var e=this.model.get("fieldName"),t=this.model.conditions;this._$lastID=this.$el.children(`input[name=${e}_last_id]`).bindProperty("value",this.model,"lastID"),this._$mode=this.$("#conditions_mode input"),this._$rowsContainer=this.$(".conditions-field-rows-container"),this._$rows=this._$rowsContainer.children(".conditions-field-rows");const i=this._$rows.children();return t.each((e,t)=>{this._addConditionRow(e,i[t])}),this.listenTo(t,"add",e=>this._addConditionRow(e)),this._onConditionModeChanged(),this},_addConditionRow(e,t){e=new(e.get("valid")?l:o)({conditionSet:this.model,el:t,model:e,rowAnimationSpeedMS:this._rowAnimationSpeedMS});e.render(),void 0===t&&e.$el.hide().appendTo(this._$rows).slideDown(this._rowAnimationSpeedMS)},_onAddRowClicked(e){e.stopPropagation(),e.preventDefault(),this.model.addNewCondition()},_onConditionModeChanged(){var e=this._$mode.filter(":checked").val();this._$rowsContainer.toggle("always"!==e)}})}{const s=Djblets.Forms.BaseConditionValueFieldView;Djblets.Forms.ConditionValueFormFieldView=s.extend({render(){return s.prototype.render.call(this),this.$input=this.$el.children().attr("name",this.model.get("fieldName")),this},setValue(e){this.$input.val(e)},getValue(e){return this.$input.val()}})}{const n=_.template(`<li class="djblets-c-list-edit-widget__entry"
    data-list-index="<%- index %>">
 <%= renderedDefaultRow %>
 <a href="#" class="djblets-c-list-edit-widget__remove-item"
    role="button" title="<%- removeText %>">
   <span class="fa fa-times"></span>
 </a>
</li>`);Djblets.Forms.ListEditView=Backbone.View.extend({events:{"click .djblets-c-list-edit-widget__add-item":"_addItem","click .djblets-c-list-edit-widget__remove-item":"_removeItem"},initialize(e){this._removeText=e.removeText,this._fieldName=e.fieldName,this._renderedDefaultRow=e.renderedDefaultRow,this._numItems=0,this._$numRowsEl=null},render(){return this.$el.data("djblets-list-edit-view",this),this._$list=this.$el.children(".djblets-c-list-edit-widget__entries"),this._numItems=this._$list.find(".djblets-c-list-edit-widget__entry").length,this._$addBtn=this.$el.children(".djblets-c-list-edit-widget__add-item"),this._$numRowsEl=this.$(`input[name="${this._fieldName}_num_rows"]`).val(this._numItems),this},_createDefaultEntry(e){var t=_.uniqueId("djblets-list-edit-row"),t=this._renderedDefaultRow.replaceAll("__EDIT_LIST_ROW_ID__",t).replaceAll("__EDIT_LIST_ROW_INDEX__",e),t=$(n({index:e,removeText:this._removeText,renderedDefaultRow:t}));return this._updateEntryInputName(t,e),this._$numRowsEl.val(e+1),t},_updateEntryInputName(e,i){e=e.find(".djblets-c-list-edit-widget__input");const l=this._fieldName;1<e.length?e.each((e,t)=>$(t).attr("name",l+`_value[${i}]_`+e)):e.attr("name",l+`_value[${i}]`)},_addItem(e){e.preventDefault(),e.stopPropagation(),this._$list.append(this._createDefaultEntry(this._numItems)),this._numItems+=1},_removeItem(e){e.preventDefault(),e.stopPropagation();var t,e=$(e.target).closest(".djblets-c-list-edit-widget__entry");1<this._numItems?(e.remove(),--this._numItems,this._$list.find(".djblets-c-list-edit-widget__entry").each((e,t)=>{t=$(t);t.attr("data-list-index",e),this._updateEntryInputName(t,e)}),this._$numRowsEl.val(this._numItems)):(t=this._createDefaultEntry(0),e.replaceWith(t))}})}Djblets.Forms.PrivacyConsentFieldView=Backbone.View.extend({events:{"change input":"_onConsentChanged"},initialize(e){this.options=e;var e=this.$(".privacy-consent-field-choices"),t=e.find(".privacy-consent-field-choice-allow"),e=e.find(".privacy-consent-field-choice-block");this._$allowInput=t.children("input"),this._$allowLabel=t.children("label"),this._$blockInput=e.children("input"),this._$blockLabel=e.children("label")},render(){return this._onConsentChanged(),this},_onConsentChanged(){var e=this._$allowInput.is(":checked"),t=this._$blockInput.is(":checked");this.$el.toggleClass("privacy-consent-field-allow",e),this.$el.toggleClass("privacy-consent-field-block",t),this._$allowLabel.text(e?this.options.allowedText:this.options.allowText),this._$blockLabel.text(t?this.options.blockedText:this.options.blockText)}})}.call(this);
