{"version":3,"sources":["reorderable-group-for.js"],"names":[],"mappings":"AAUA;IAKE,mEAMC;IARD,eAAiB;IAGf,aAAsB;IACtB,QAAY;IACZ,gBAA4B;IAC5B,cAAwB;IAsC1B,uBAEC;IApCD,aA0BC;IApBC,mBAmBC;IAfK,cAAkD;IAClD,mBAA0D;IAiBlE,eAGC;IAOD,iBAEC;IAED,iBAEC;IAED,gCAEC;IAED,iBAqBC;IAED,gBAEC;CACF","file":"reorderable-group-for.d.ts","sourcesContent":["import {inject} from 'aurelia-dependency-injection';\nimport {customAttribute} from 'aurelia-templating';\nimport {DndService} from 'bcx-aurelia-dnd';\nimport {EventAggregator} from 'aurelia-event-aggregator';\n// import {TaskQueue} from 'aurelia-task-queue';\nimport {ReorderableGroupMap} from './reorderable-group-map';\nimport repeaterDndType from './repeater-dnd-type';\n\nconst example = 'reorderable-group-for.bind=\"arrayModel\"';\n\n@customAttribute('reorderable-group-for')\n@inject(Element, EventAggregator, DndService, ReorderableGroupMap)\nexport class ReorderableGroupFor {\n  intention = null;\n\n  constructor(element, ea, dndService, groupMap) {\n    this.element = element;\n    this.ea = ea;\n    this.dndService = dndService;\n    this.groupMap = groupMap;\n    this.resetIntention = this.resetIntention.bind(this);\n  }\n\n  bind() {\n    const {value} = this;\n    if (!Array.isArray(value)) {\n      throw new Error('reorderable-group-for needs items to be an array. e.g. ' + example);\n    }\n\n    this._subsribers = [\n      this.ea.subscribe('dnd:willStart', () => {\n        const repeaterInfo = this.groupMap.get(value);\n        if (repeaterInfo) {\n          this.group = repeaterDndType(repeaterInfo.group());\n          this.repeaterId = repeaterDndType(repeaterInfo.repeaterId);\n        } else {\n          this.group = null;\n          this.repeaterId = null;\n        }\n\n        this.resetIntention();\n      }),\n      this.ea.subscribe('dnd:didEnd', this.resetIntention),\n      this.ea.subscribe('reorderable-group:intention-changed', intention => {\n        if (intention.type !== this.group) return;\n        // sync intention from other repeater\n        this.intention = intention;\n      })\n    ];\n  }\n\n  unbind() {\n    this._subsribers.forEach(s => s.dispose());\n    this._subsribers = [];\n  }\n\n\n  resetIntention() {\n    this.intention = null;\n  }\n\n  attached() {\n    this.dndService.addTarget(this, {element: this.element});\n  }\n\n  detached() {\n    this.dndService.removeTarget(this);\n  }\n\n  dndCanDrop(model) {\n    return model.type === this.group;\n  }\n\n  dndHover() {\n    if (!this.repeaterId) return;\n\n    const {isHoveringShallowly, model} = this.dnd;\n    if (!isHoveringShallowly) return;\n\n    const {type, index, item, repeaterId} = model;\n    const length = this.value ? this.value.length : 0;\n    const inSameGroup = model.repeaterId === this.repeaterId;\n    const defaultTargetIndex = inSameGroup ? length - 1 : length;\n\n    if (!this.intention || this.intention.toRepeaterId !== this.repeaterId) {\n      this.ea.publish('reorderable-group:intention-changed', {\n        type,\n        item,\n        fromIndex: index,\n        fromRepeaterId: repeaterId,\n        toIndex: defaultTargetIndex, // move to last position\n        toRepeaterId: this.repeaterId\n      });\n    }\n  }\n\n  dndDrop() {\n    /* no-op */\n  }\n}\n"],"sourceRoot":"../src/"}