text: /**
   * Copyright 2018 The Pennsylvania State University
   * @license Apache-2.0, see License.md for full text.
   */
  import{html as e,PolymerElement as t}from"../../@polymer/polymer/polymer-element.js";import{afterNextRender as i}from"../../@polymer/polymer/lib/utils/render-status.js";import*as s from"../../@polymer/polymer/lib/utils/async.js";import"../simple-modal/simple-modal.js";class LrnsysOutline extends t{constructor(){super(),import("../../@polymer/paper-input/paper-input.js"),import("../../@polymer/paper-icon-button/paper-icon-button.js"),import("../../@polymer/paper-button/paper-button.js"),import("./lib/lrnsys-outline-item.js")}static get template(){return e`
        <style>
          :host {
            display: block;
          }
          :host kbd {
            display: inline-block;
            background: #333;
            color: white;
            border-radius: 4px;
            margin: 4px 4px 4px 0;
            padding: 8px;
            font-family: Verdana, Geneva, Tahoma, sans-serif;
            font-size: 85%;
          }
        </style>
        <div id="itemslist">
          <template is="dom-repeat" items="{{items}}" as="item">
            <lrnsys-outline-item
              disable-down="[[item.disableDown]]"
              disable-left="[[item.disableLeft]]"
              disable-right="[[item.disableRight]]"
              disable-up="[[item.disableUp]]"
              id$="[[item.id]]"
              index$="[[item.index]]"
              indent-level="{{item.indent}}"
              parent="{{item.parent}}"
              title="{{item.title}}"
            >
            </lrnsys-outline-item>
          </template>
        </div>
      `}static get tag(){return"lrnsys-outline"}static get properties(){return{data:{type:Array,value:null},items:{type:Array,value:null,notify:!0},activeItem:{type:Object,notify:!0}}}connectedCallback(){super.connectedCallback(),window.SimpleModal.requestAvailability(),i(this,(function(){this.addEventListener("delete-item",this._handleRemoveItem.bind(this)),this.addEventListener("indent-item",this._handleIndentItem.bind(this)),this.addEventListener("add-item",this._handleAddItem.bind(this)),this.addEventListener("move-item",this._handleMoveItem.bind(this)),this.addEventListener("change-item",this._handleChangeItem.bind(this)),this.addEventListener("focus-item",this._handleFocusItem.bind(this)),this.addEventListener("blur-item",this._handleBlurItem.bind(this))}))}disconnectedCallback(){this.removeEventListener("delete-item",this._handleRemoveItem.bind(this)),this.removeEventListener("indent-item",this._handleIndentItem.bind(this)),this.removeEventListener("add-item",this._handleAddItem.bind(this)),this.removeEventListener("move-item",this._handleMoveItem.bind(this)),this.removeEventListener("change-item",this._handleChangeItem.bind(this)),this.removeEventListener("focus-item",this._handleFocusItem.bind(this)),this.removeEventListener("blur-item",this._handleBlurItem.bind(this)),super.disconnectedCallback()}ready(){super.ready(),(null===this.data||this.data.length<1)&&(this.__tempid=void 0===this.__tempid?0:this.__tempid+1,this.data=[{id:"outline-item-"+this.__tempid,title:"",order:0,parent:null}]),this.setData(this.data)}setData(e){if(void 0!==e&&e.length>0){let i=-1;for(var t in e){let s=parseInt(this._getIndent(e,t));this.__tempid=void 0===this.__tempid?0:this.__tempid+1,e[t].index=parseInt(t),e[t].indent=s,e[t].prevSibling=this._getSibling(parseInt(t),s,!0),e[t].nextSibling=this._getSibling(parseInt(t),s,!1),e[t].disableUp=null===e[t].prevSibling,e[t].disableDown=null===e[t].nextSibling,e[t].disableLeft=0===s,e[t].disableRight=s>i,e[t].id=void 0===e[t].id?"outline-item-"+this.__tempid:e[t].id,i=s}}this.set("items",[]),this.set("items",e)}getData(){for(var e in this.items)this.items[e].order=this._getOrder(this.items[e]),this.notifyPath(`items.${e}.order`);return this.items}addItem(e){let t=e.item,i=e.new,n=this.items.findIndex(e=>e.id===t.id)+1;this.__tempid=this.__tempid+1,this.splice("items",n,0,{id:"outline-item-"+this.__tempid,title:i,indent:t.indent,parent:t.parent}),this.items[n].indentLevel=t.indent,this.notifyPath(`items.${n}.indentLevel`),this.setData(this.items),void 0!==this.__focusedItem&&null!==this.__focusedItem&&s.microTask.run(()=>{setTimeout(()=>{this.__focusedItem=t.nextElementSibling,this.__focusedItem.focus()},50)})}removeItem(e){let t=this.items.findIndex(t=>t.id===e.id),i=document.createElement("paper-button");i.raised=!0,i.addEventListener("click",this._deleteItemConfirm.bind(this)),i.appendChild(document.createTextNode("Yes, delete"));const s=new CustomEvent("simple-modal-show",{bubbles:!0,cancelable:!0,detail:{title:`Do you really want to delete ${this.items[t].title}?`,elements:{buttons:i},invokedBy:e.shadowRoot.querySelector("#delete"),clone:!1}});this.dispatchEvent(s)}_deleteItemConfirm(e){let t=this.items.findIndex(e=>e.id===this.activeItem.id);this.activeItem.classList.add("collapse-to-remove");const i=new CustomEvent("simple-modal-hide",{bubbles:!0,cancelable:!0,detail:{}});this.dispatchEvent(i),setTimeout(()=>{for(var e in this.__focusedItem=this.activeItem.previousElementSibling,this.items)this.items[e].parent==this.items[t].id&&(this.items[e].parent=this.items[t].parent);this.activeItem.classList.remove("collapse-to-remove"),this.splice("items",t,1),void 0!==this.__focusedItem&&null!==this.__focusedItem&&s.microTask.run(()=>{setTimeout(()=>{this.__focusedItem.focus()},50)})},300)}moveItem(e,t){let i=e.index,n=this._getLastChild(e),l=n-i+1,d=t?this.items[i].prevSibling:this._getLastChild(this.items[n+1])-l+1;if(d>-1&&d<this.items.length&&(t&&!e.disableUp||!t&&!e.disableDown)){let e=this.splice("items",i,l);this.splice("items",d,0,e),this.__focusedItem=this.shadowRoot.querySelector("#itemslist").querySelectorAll("lrnsys-outline-item")[d],this.setData(this.items),void 0!==this.__focusedItem&&null!==this.__focusedItem&&s.microTask.run(()=>{setTimeout(()=>{this.__focusedItem.focus()},50)})}}_adjustIndent(e,t){if(t>0&&!e.disableRight||t<0&&!e.disableLeft){let i=parseInt(e.index),s=e.indent,n=e.indent+t,l=i+1,d=null!==e.prevSibling&&void 0!==e.prevSibling?e.prevSibling.id:null,m=this._getItemById(e.parent)&&this._getItemById(e.parent).parent?this._getItemById(e.parent).parent.id:null;for(e.indent=n,e.parent=t>0?d:m,e.prevSibling=this._getSibling(i,n,!0),e.nextSibling=this._getSibling(i,n,!1),e.disableUp=null===e.prevSibling,e.disableDown=null===e.nextSibling,e.disableLeft=0===n,e.disableRight=null===this.items[i-1]||void 0===this.items[i-1]||n>this.items[i-1].indentLevel,this.set(`items.${i}`,e),this.notifyPath(`items.${i}.*`);null!==this.items[l]&&void 0!==this.items[l]&&s<this.items[l].indentLevel;)this.items[l].indentLevel=this.items[l].indentLevel+t,this.notifyPath(`items.${l}.indentLevel`),l++,next=this.items[l]}}_getLastChild(e){let t=null!=e?this._getSibling(e.index,e.indent,!1):null;return null!=t?t-1:void 0!==e&&null!==e.parent&&null!==e.parent&&null!==this._getItemById(e.parent)?this._getLastChild(this._getItemById(e.parent)):this.items.length-1}_getIndent(e,t){if(void 0!==e[t].parent){let i=e.findIndex(i=>i.id===e[t].parent);if(-1!==i&&void 0!==e[i]&&void 0!==e[i].indent)return e[i].indent+1}return 0}_getOrder(e){let t=0,i=0;for(var s in this.items)this.items[s].parent==e.parent&&this.items[s].id==e.id?i=t:this.items[s].parent==e.parent&&t++;return i}_getSibling(e,t,i){let s=i?-1:1,n=e+s,l=null;if(null!==this.items)for(;n<this.items.length&&n>-1;)null===l&&void 0!==this.items[n]&&void 0!==this.items[e]&&this.items[n].parent===this.items[e].parent&&(l=n),n+=s;return l}_getItemById(e,t){let i=this.items.findIndex(t=>t.id===e);return t=void 0===t?0:t,void 0!==this.items[i+t]?this.items[i+t]:null}_handleAddItem(e){this.addItem(e.detail)}_handleRemoveItem(e){this.activeItem=e.detail.item,this.removeItem(e.detail.item)}_handleMoveItem(e){this.activeItem=e.detail.item,this.moveItem(e.detail.item,e.detail.moveUp,e.detail.byGroup)}_handleFocusItem(e){(e.detail.moveUp?e.detail.item.previousElementSibling:e.detail.item.nextElementSibling).setSelection()}_handleIndentItem(e){let t=e.detail.increase?1:-1;this._adjustIndent(this._getItemById(e.detail.item.id),t),this.setData(this.items)}_handleChangeItem(e){if(null!=this._getItemById(e.detail.item.id)){let t=this.items.findIndex(t=>t.id===e.detail.item.id);void 0!==this.items[t]&&(this.items[t].title=e.detail.value,this.notifyPath(`items.${t}.title`))}}_handleFocusItem(e){this.__focusedItem=e.srcElement}_handleBlurItem(e){}}window.customElements.define(LrnsysOutline.tag,LrnsysOutline);export{LrnsysOutline};