(function(){if(!window.Djblets){window.Djblets={}}"use strict";Djblets.AddIntegrationPopupView=Backbone.View.extend({className:"djblets-c-integrations-popup",integrationTemplateSource:`<li class="djblets-c-integration">
 <a href="<%- addURL %>">
  <% if (iconSrc) { %>
   <img class="djblets-c-integration__icon"
        src="<%- iconSrc %>"
        srcset="<%- iconSrcSet %>"
        width="48" height="48" alt="">
  <% } %>
  <div class="djblets-c-integration__details">
   <div class="djblets-c-integration__name"><%- name %></div>
   <div class="djblets-c-integration__description">
    <%- description %>
   </div>
  </div>
 </a>
</li>`,emptyIntegrationsTemplateSource:`<p class="djblets-c-integrations-popup__empty">
 ${_.escape(gettext("There are no integrations currently installed."))}
</p>`,initialize(options){this.integrations=options.integrations},render(){const integrations=this.integrations;if(integrations.length>0){const itemTemplate=_.template(this.integrationTemplateSource);const $list=$("<ul>");for(let i=0;i<integrations.length;i++){$list.append(itemTemplate(integrations[i]))}this.$el.append($list)}else{this.$el.addClass("-is-empty").append(_.template(this.emptyIntegrationsTemplateSource)())}return this},remove(){this.hide();Backbone.View.prototype.remove.call(this)},show($button){const $window=$(window);const $el=this.$el;const el=this.el;const buttonPos=$button.position();$el.move(buttonPos.left,buttonPos.top+$button.outerHeight(),"absolute").width(100).height(1).show();const popupBorderWidths=$el.getExtents("b","lr");const scrollbarWidth=el.offsetWidth-el.clientWidth-popupBorderWidths;const popupOffset=$el.offset();const popupHeight=Math.floor($window.height()-popupOffset.top-$el.getExtents("bm","tb"));$el.css({height:"auto","max-height":popupHeight});if($el.hasClass("-is-empty")){$el.css("width","auto")}else{const tileWidth=$el.find(".djblets-c-integration").filter(":first").outerWidth(true);const winWidth=$window.width();const availWidth=winWidth-popupOffset.left-scrollbarWidth-$el.getExtents("m","r");let popupWidth=Math.max(Math.floor(availWidth/tileWidth),1)*tileWidth+scrollbarWidth+popupBorderWidths;$el.outerWidth(popupWidth);const newScrollbarWidth=el.offsetWidth-el.clientWidth-popupBorderWidths;if(newScrollbarWidth===0){popupWidth-=scrollbarWidth;$el.outerWidth(popupWidth)}if(popupOffset.left+popupWidth>winWidth){$el.css("left",winWidth-popupWidth)}}$(document).one("click.djblets-integrations-popup",()=>this.hide());$(window).one("resize.djblets-integrations-popup",()=>this.hide())},hide(){this.$el.hide();$(document).off("click.djblets-integrations-popup");$(window).off("resize.djblets-integrations-popup")}});"use strict";(function(){const IntegrationConfigItem=Djblets.Config.ListItem.extend({defaults:_.defaults({removeLabel:gettext("Delete"),showRemove:true},Djblets.Config.ListItem.prototype.defaults),url(){return this.get("editURL")},parse(data){return{editURL:data.editURL,id:data.id,integrationID:data.integrationID,itemState:data.enabled?"enabled":"disabled",name:data.name}}});const IntegrationConfigItemView=Djblets.Config.TableItemView.extend({className:"djblets-c-integration-config djblets-c-config-forms-list__item",actionHandlers:{delete:"_onDeleteClicked"},template:_.template(`<td class="djblets-c-integration-config__name">
 <img src="<%- iconSrc %>"
      srcset="<%- iconSrcSet %>"
      width="32" height="32" alt="">
 <a href="<%- editURL %>"><%- name %></a>
</td>
<td class="djblets-c-integration-config__integration-name">
 <%- integrationName %>
</td>
<td class="djblets-c-config-forms-list__item-state"></td>
<td></td>`),getRenderContext(){const integrationID=this.model.get("integrationID");const integration=this.model.collection.options.integrationsMap[integrationID];return{iconSrc:integration.iconSrc,iconSrcSet:integration.iconSrcSet,integrationName:integration.name}},_onDeleteClicked(){$("<p>").text(gettext("This integration will be permanently removed. This cannot be undone.")).modalBox({title:gettext("Are you sure you want to delete this integration?"),buttons:[$("<button>").text(gettext("Cancel")),$('<button class="danger">').text(gettext("Delete Integration")).click(()=>this.model.destroy({beforeSend:xhr=>{xhr.setRequestHeader("X-CSRFToken",this.model.collection.options.csrfToken)}}))]})}});Djblets.IntegrationConfigListView=Backbone.View.extend({events:{"click .djblets-c-integration-configs__add":"_onAddIntegrationClicked"},addIntegrationPopupViewType:Djblets.AddIntegrationPopupView,listItemsCollectionType:Djblets.Config.ListItems,listItemType:IntegrationConfigItem,listViewType:Djblets.Config.TableView,listItemViewType:IntegrationConfigItemView,initialize(options){this._integrationIDs=options.integrationIDs;this._integrationsMap=options.integrationsMap;this.list=new Djblets.Config.List({},{collection:new this.listItemsCollectionType(options.configs,{csrfToken:options.csrfToken,integrationsMap:options.integrationsMap,model:this.listItemType,parse:true})});this._popup=null},render(){this.listView=new this.listViewType({el:this.$(".djblets-c-config-forms-list"),model:this.list,ItemView:this.listItemViewType});this.listView.render().$el.removeAttr("aria-busy");this._$listContainer=this.listView.$el.parent();this.listenTo(this.list.collection,"add remove",this._showOrHideConfigsList);this._showOrHideConfigsList();return this},_showOrHideConfigsList(){if(this.list.collection.length>0){this._$listContainer.show()}else{this._$listContainer.hide()}},_onAddIntegrationClicked(e){e.preventDefault();e.stopPropagation();if(!this._popup){const integrationIDs=this._integrationIDs;const integrationsMap=this._integrationsMap;const integrations=[];for(let i=0;i<integrationIDs.length;i++){integrations.push(integrationsMap[integrationIDs[i]])}this._popup=new this.addIntegrationPopupViewType({integrations:integrations});this._popup.render().$el.appendTo(this.$el)}this._popup.show($(e.target))}})})()}).call(this);
