text: 'use strict';angular.module('pTreeSelect',[]),'use strict',angular.module('pTreeSelect').directive('pTree',['$window','pTreeSelectSvc',function(a,b){return{restrict:'E',scope:{pTreeData:'=',pTreeCfg:'=',pTreeResult:'='},template:'<div id="ptree-main"><div id="ptree-x"><p-tree-list ng-repeat="list in lists | pTreeSkip:svc" class="ptree-cell"></p-tree-list></div><div id="ptree-y"><p-tree-item ng-repeat="item in context._items | pTreeGapLookup:context:svc | pTreeFilter:context.filter | pTreeItems:svc:context | filter:{__hidden:\'!true\'} | orderBy:context._name | pTreeUnique:context._name | pTreeSetFilteredList:context | pTreeLimitTo:context.itemsToShow:context.curY | pTreeLast:svc:context"></p-tree-item><a ng-click="context.showMore()" ng-hide="context.itemsToShow &gt;= context.filteredList.length - context.curY" class="pointer">show more</a></div></div>',link:function(d){angular.element(a).on('keydown',function(g){if(console.log(g.code),'ArrowUp'===g.code)d.$apply(function(){d.svc.scrollUp()});else if('ArrowDown'===g.code)d.$apply(function(){d.svc.scrollDown()});else if('ArrowLeft'===g.code&&d.controlKey)d.$apply(function(){d.context=d.svc.moveLeft()});else if('ArrowRight'===g.code&&d.controlKey)d.$apply(function(){d.context=d.svc.moveRight()});else if('ArrowLeft'===g.code)d.$apply(function(){d.context=d.svc.scrollLeft()});else if('ArrowRight'===g.code)d.$apply(function(){d.context=d.svc.scrollRight()});else if('Escape'===g.code)d.$apply(function(){d.context.reset(d.svc)});else if('Space'===g.code)d.$apply(function(){d.context.toggleResultItem(d.context.focus[d.context.itemKey],d.svc)});else if('Slash'===g.code)d.$apply(function(){d.context.focusFilterElement()});else return d.controlKey='ControlLeft'===g.code;d.controlKey='ControlLeft'===g.code,g.preventDefault()}),d.svc=b.create(d.pTreeCfg||{}),d.lists=d.svc.getLists(),d.context={},new Promise(function(g){g(d.pTreeData)}).then(function(g){d.$apply(function(){d.context=d.svc.init(g,d.pTreeResult)})}),d.$watch('context.name',function(g){g&&d.context.enter()}),d.$watch('context.moved',function(g){g&&d.context.enter()})}}}]).directive('pTreeList',[function(){return{restrict:'E',template:'<div id="ptree-list" ng-class="{ \'pointer\': list.movable }" p-tree-draggable="p-tree-draggable" draggable="{{list.movable}}"><div><div ng-show="$index &lt; context.offsetX" class="ptree-xlabel">{{ list.focus[list._name] }}</div><div ng-show="$index === svc.cfg.offsetX" class="arrow-box"><!--HELPME: is there a way around $parent?--><div ng-click="$parent.context = svc.scrollLeft()" class="arrow arrow-left"></div><div ng-click="svc.scrollUp()" class="arrow arrow-up"></div><div ng-click="$parent.context = svc.scrollRight()" class="arrow arrow-right"></div><div ng-click="svc.scrollDown()" class="arrow arrow-down"></div></div><div>{{ list.name }}</div></div><div ng-class="{ \'y-current\': $index === svc.cfg.offsetX }" class="filterdiv"><!--the list.cnt === context.cnt comparison is to show the filterbox only for the list that has the focus--><input type="text" placeholder="type / to focus" ng-model="list.filter" ng-class="{ focused: list.cnt === context.cnt &amp;&amp; list.filter &amp;&amp; list.filter.length }" p-tree-filter="p-tree-filter" class="filter"></div><div ng-show="$index &gt; context.offsetX &amp;&amp; list.hasSelected()" class="preview"><div>Selected items</div><ul><li ng-repeat="sitem in list.getSelected() | pTreeUnique:list._name | orderBy:list._name | limitTo:list.selectToShow"><div ng-click="list.toggleResultItem(sitem[context.itemKey], svc, true, $parent.$index)"></div>{{sitem[list._name]}}</li><a ng-click="list.selectShowMore()" ng-show="list.selectToShow &lt; list.__selectCnt" class="pointer">show more</a></ul></div></div>'}}]).directive('pTreeFilter',['$timeout',function(){return{restrict:'A',link:function(c,d){c.$watch('list.filter',function(f,g){void 0===f||f===g||c.list.filterChange()}),c.svc.setFilterElement(c.list.name,d),d.on('blur',function(){c.context.focusFilterElement(!1)})}}}]).directive('pTreeItem',[function(){return{restrict:'E',template:'<div ng-class="{ \'ptree-master\': $index === context.offsetY, pointer: context.selectable }" ng-init="resultItem = context.getResultItem(item[context.itemKey])" ng-click="context.selectable &amp;&amp; context.toggleResultItem(item[context.itemKey], svc)" class="ptree-cell"><!--span(ng-show="context.selectable && ! item.__ignore" ng-class="resultItem[context.selectKey] ? \'selectable-selected\' : \'selectable-unselected\'")\n| &nbsp;&nbsp;&nbsp;--><div ng-show="context.selectable &amp;&amp; ! item.__ignore" ng-class="resultItem[context.selectKey] ? \'selectable-selected\' : \'selectable-unselected\'" class="selectable"></div>{{ item[context._name] }}</div>'}}]).directive('pTreeDraggable',[function(){return{restrict:'A',link:function(b,c){function e(f){b.context.getDraggedGroup()===b.list.group&&f.preventDefault()}c.on('dragstart',function(f){f.dataTransfer.setData('application/json',JSON.stringify({index:b.list.cnt,group:b.list.group})),b.context.setDraggedGroup(b.list.group)}),c.on('dragover',e),c.on('dragenter',e),c.on('drop',function(f){b.$apply(function(){b.$parent.context=b.svc.moveByMouse(JSON.parse(f.dataTransfer.getData('application/json')),b.list.cnt)})})}}}]),'use strict',angular.module('pTreeSelect').filter('pTreeFilter',[function(){return function(a,b){var d,c=[];if(!b)return a;d=new RegExp(b,'i');for(var e=0,f=null;f=a[e];e++)(f.__filler||f.name.match(d))&&c.push(f);return c}}]).filter('pTreeLimitTo',[function(){return function(a,b,c,d){return a&&a.length?(d||(d=0),c||(c=0),0<b?a.slice(c+d,c+b+d):a.slice(c)):a}}]).filter('pTreeSkip',['pTreeSelectSvc',function(){return function(b,c){if(!b||!b.length)return b;var d=c.getCurX();return b.slice(d)}}]).filter('pTreeItems',['pTreeSelectSvc',function(){return function(b,c,d){if(!b||!b.length)return b;if(d.lookupCached&&d.lookupCache&&d.filterCache===d.filter)return d.lookupCache;var e=[];e=c.relationshipsFilter(b);var f=b[c.cfg.offsetY];return f.__empty||(f=b[c.cfg.offsetY+1]),f.__empty?f.__hidden=e.some((g)=>!g.__filler||g.__noMatch&&!g.__hidden):console.error('Panic, __empty item not found!'),d.lookupCache=e,e}}]).filter('pTreeUnique',[function(){return function(a,b){if(!a||!a.length)return a;var c={},d=a.reduce(function(e,f){return(f.__filler||!c[f[b]])&&e.push(f),c[f[b]]=1,e},[]);return d}}]).filter('pTreeSetFilteredList',['pTreeSelectSvc',function(){return function(b,c){return b&&b.length?(c.setFilteredList(b),b):b}}]).filter('pTreeGapLookup',['pTreeSelectSvc',function(){return function(b,c,d){if(console.log('gapLookup called with lefty '+c.name),!c.gapLookup||c.lookupCached)return b;var e=d.righty();console.log('gapLookup righty '+(e?e.name:'null'));var f=e&&!!d.relationshipsFilter(e.items,!0,e,1).length,g=b[d.cfg.offsetY];return g.__noMatch||(g=b[d.cfg.offsetY+1]),g.__noMatch?g.__hidden=!f:console.error('Panic, __noMatch item not found!'),b}}]).filter('pTreeLast',[function(){return function(a,b,c){return a&&a.length?(c.cachedFilter=c.filter,b.cfg.debug||(c.lookupCached=!0),a):a}}]),function(){'use strict';function a(c,d,e){angular.extend(this,d,c),this.curY=0,this.cnt=e,this.pages=1,this.selectPages=1,this.filter='',this.itemsToShow=this.pageSize,this.selectToShow=this.pageSize,this._name=this.displayField,this.fillItems(),this.filteredList=this._items,this.focus=this.items[0],this.keepFocus=!1,this.lookupCached=!1,this.lookupCache=null,this.prevFocus,this.moved=0,this.topLevelShared=!0,this.lastOther=null,this.__selectCnt=0}a.prototype={scrollUp:function(){return!!(this.curY+this.offsetY+1<this.filteredList.length)&&(this.curY++,this.focus=this.filteredList[this.curY+this.offsetY],!0)},scrollDown:function(){return!!(0<this.curY)&&(this.curY--,this.focus=this.filteredList[this.curY+this.offsetY],!0)},fillItems:function(){this._items=' '.repeat(0>this.offsetY-this.curY?0:this.offsetY-this.curY).split('').reduce((c)=>{return c.push({__id:c.length,name:'',__filler:!0,__ignore:!0}),c},[]).concat(this.items),this.unfiltered&&this._items.splice(this.offsetY,0,{__id:'unfiltered',__filler:!0,__unfiltered:!0,name:this.unfilteredName}),this._items.splice(this.offsetY,0,{__id:'empty',__filler:!0,__empty:!0,__hidden:!0,name:this.emptyName}),this.movable&&this._items.splice(this.offsetY,0,{[this.itemKey]:'__ptree-noMatch',__selectable:!0,[this.selectKey]:!1,__id:'none',__filler:!0,__noMatch:!0,__hidden:!0,[this._name]:this.noneName})},setFilteredList:function(c){this.filteredList=c},enter:function(){if(this.filteredList){this.prevFocus=this.focus;var c=this.offsetY;if(this.focus&&(this.keepFocus||this.curY+this.offsetY>=this.filteredList.length)){this.keepFocus=!1;for(var d=this.filteredList.length-1;0<=d&&this.focus[this._name]!==this.filteredList[d][this._name]&&(!this.filteredList[d].__filler||this.filteredList[d].__hidden);d--);this.curY=d-this.offsetY,0>this.curY&&(this.curY=0),this.curY+this.offsetY>=this.filteredList.length&&(c=0)}this.focus=this.filteredList[this.curY+c]}},leave:function(c){this.focus!==this.prevFocus&&c.invalidateLookupCache()},invalidateLookupCache:function(){this.lookupCached=!1},showMore:function(){this.itemsToShow=this.pageSize*++this.pages},selectShowMore:function(){this.selectToShow=this.pageSize*++this.selectPages},filterChange:function(){this.curY=0},reset:function(c,d){this.curY=0,this.filter.length?this.filter='':(this.pages=1,this.itemsToShow=this.pageSize,this.selectToShow=this.pageSize),this.focusFilterElement(!1),c.invalidateLookupCache(d?2:1),this.moved++},invalidateLookupCache:function(){this.lookupCached=!1},isLookupCached:function(){return this.lookupCached},hasFocusChanged:function(){return this.focus!==this.prevFocus},getResultItem:function(c){return this.resultMap?this.resultMap[c]:null},toggleResultItem:function(c,d,e,f){var g=this.resultMap[c];if(!g)return console.error('Panic, no resultMap entry for key '+c+' found');var h=!g[this.selectKey],j=this.focus,l=e?f-d.cfg.offsetX:0;for(let m of Object.keys(this.resultMap)){let n=this.resultMap[m];n[this._name]===g[this._name]&&(e||this.topLevelShared||n===g)&&(this.focus=n,this._toggleResultItem(m,d,h,l))}this.focus=j},_toggleResultItem:function(c,d,e,f){return this.selectable||void 0!==e?this.resultMap?c&&!this.resultMap[c]?console.error('Panic, no key '+c+' in resultMap on selectable context '+this.name):void(e=e===void 0?c?!this.resultMap[c][this.selectKey]:void 0:e,this.selectable&&c&&(this.resultMap[c][this.selectKey]=e),'inherit'===this.selectMode&&d.propagateSelection(this,c?this.resultMap[c][this._name]:null,e,f||0)):console.error('Panic, no resultMap on selectable context '+this.name):console.error('Panic, cannot toggle resultItem selection on not-selectable list '+this.name)},flushSelection:function(){if(this.selectable)for(let c of this.items)c.__filler||(c[this.selectKey]=!1)},hasSelected:function(){for(let c of Object.keys(this.resultMap))if(this.resultMap[c][this.selectKey]&&!this.resultMap[c].__filler)return!0;return!1},getSelected:function(){var c=[];for(let d of Object.keys(this.resultMap))this.resultMap[d][this.selectKey]&&!this.resultMap[d].__filler&&c.push(this.resultMap[d]);return this.__selectCnt=c.length,c},setDraggedGroup:function(c){this.draggedGroup=c},getDraggedGroup:function(){return this.draggedGroup},setFilterElement:function(c){this.filterElement=c},focusFilterElement:function(c){!1===c?(this.filterElement.removeClass('forcefocused'),this.filterElement[0].blur()):(this.filterElement.addClass('forcefocused'),this.filterElement[0].focus())}},angular.module('pTreeSelect').factory('pTreeSelectSvc',[function(){function c(e){this;for(var g in this.defaults={offsetX:2,offsetY:2,pageSize:8,group:void 0,unfiltered:!1,unfilteredName:'* UNFILTERED *',noneName:'* NONE *',gapLookup:!1,displayField:'name',foreignKey:'name',emptyName:'* EMPTY *',keyboardCtrl:!0,selectable:!1,itemKey:'id',selectKey:'__selected',isolated:!1,selectMode:'inherit'},this.cfg=e,this.defaults)void 0===this.cfg[g]&&(this.cfg[g]=this.defaults[g]);this.curX=0,this.contexts=[],this.pointers={},this.listChannel=[]}c.prototype={init:function(e,f){this.data=e,f||(f=this.contexts);for(let g=0;g<e.length;g++)this.contexts[g]=new a(e[g],this.cfg,g),this.pointers[e[g].name]&&console.error('Oops, the list names should be unique; '+e[g].name+' seems to appear more often than just once.'),this.pointers[e[g].name]=this.contexts[g];for(let g of this.contexts){g.resultMap={};let h;for(h=f.length;h--&&f[h].name!==g.name;);if(-1===h){let j=[];f.push({name:g.name,items:j});for(let l of g._items)if(!l.__filler||l.__selectable){let m=null;m=l.__filler?{[g.itemKey]:l[g.itemKey],[g._name]:l[g._name],get[g.selectKey](){let n=this.context.lastOther?this.context.lastOther[this.context.itemKey]:'noOther';return console.log('getting '+n),!!this.selectMap[n]},set[g.selectKey](n){let o=this.context.lastOther?this.context.lastOther[this.context.itemKey]:'noOther';console.log('setting '+o+' = '+n),this.selectMap[o]=n},__filler:!0,__noMatch:l.__noMatch,context:g,selectMap:{}}:{[g.itemKey]:l[g.itemKey],[g._name]:l[g._name],[g.selectKey]:!!l[g.selectKey]},l.__filler||j.push(m),g.resultMap[l[g.itemKey]]=m}}else for(let j of f[h]._items){let l=null;j.__filler&&j.__selectable?(l=j.__filler?{[g.itemKey]:j[g.itemKey],[g._name]:j[g._name],get[g.selectKey](){let m=this.context.lastOther?this.context.lastOther[this.context.itemKey]:'noOther';return console.log('getting '+m),!!this.selectMap[m]},set[g.selectKey](m){let n=this.context.lastOther?this.context.lastOther[this.context.itemKey]:'noOther';console.log('setting '+n+' = '+m),this.selectMap[n]=m},__filler:!0,__noMatch:j.__noMatch,context:g,selectMap:{}}:{[g.itemKey]:j[g.itemKey],[g._name]:j[g._name],[g.selectKey]:!!j[g.selectKey]},g.resultMap[j[g.itemKey]]=l):!j.__filler&&(g.resultMap[j[g.itemKey]]=j)}}this.resultMap={};for(let g of f)if(g.selectable){this.resultMap[g.name]={};for(let h of g._items)h[g.itemKey]&&(this.resultMap[g.name][h[g.itemKey]]=h)}return this.refreshLists(),this.getContext()},getCurX:function(){return this.curX},getLists:function(){return this.listChannel},getContext:function(){return this.listChannel[this.curX+this.cfg.offsetX]},righty:function(e){return e||(e=0),this.listChannel[this.curX+this.cfg.offsetX+e+1]},lefty:function(){return this.listChannel[this.curX+this.cfg.offsetX-1]},refreshLists:function(){var e=this;this.listChannel.splice(0,this.listChannel.length),this.listChannel.push.apply(this.listChannel,' '.repeat(this.cfg.offsetX).split('').reduce((f)=>{return f.push({__id:f.length}),f},[])),this.listChannel.push.apply(this.listChannel,this.contexts.reduce(function(f,g){return(!g.showIf||Object.keys(g.showIf).every(function(h){var j=e.pointers[h]._name;return e.pointers[h]&&e.pointers[h].focus[j]===g.showIf[h]}))&&f.push(g),f},[]))},invalidateLookupCache:function(e){e||(e=0);for(var f=this.listChannel.length-1;f>this.curX+this.cfg.offsetX-e;f--)this.listChannel[f].invalidateLookupCache()},scrollLeft:function(){return this.curX&&(this.getContext().leave(this),this.curX--),this.getContext()},scrollRight:function(){return this.curX+this.cfg.offsetX!==this.listChannel.length-1&&(this.getContext().leave(this),this.curX++),this.getContext()},scrollUp:function(){this.listChannel[this.curX+this.cfg.offsetX].scrollUp()&&this.refreshLists()},scrollDown:function(){this.listChannel[this.curX+this.cfg.offsetX].scrollDown()&&this.refreshLists()},moveByMouse:function(e,f){if(e.index!==f){var g=this.contexts.splice(e.index,1)[0];this.contexts.splice(f,0,g),this.refreshContextCounters(),this.refreshLists();var h=this.getContext();return h.reset(this),this.flushSelection(e.index>f?g:this.contexts[e.index]),h}},moveLeft:function(){var f=this.getContext(),g=this.contexts,h=f.cnt,j=this.listChannel[this.curX+this.cfg.offsetX-1];return f.movable?j.group===f.group?(g.splice(h,1),g.splice(j.cnt,0,f),this.refreshContextCounters(),this.curX--,f.reset(this),this.refreshLists(),this.flushSelection(f),f):f:f},moveRight:function(){var f=this.getContext(),g=this.contexts,h=f.cnt,j=this.listChannel[this.curX+this.cfg.offsetX+1];return f.movable?j&&j.group===f.group?(g.splice(h,1),g.splice(j.cnt,0,f),j.keepFocus=!0,this.refreshContextCounters(),this.curX++,f.reset(this,!0),this.refreshLists(),this.flushSelection(this.lefty()),f):f:f},refreshContextCounters:function(){for(var e=0,f=null;f=this.contexts[e];e++)f.cnt=e},flushSelection:function(e){var f=e.name,g=!1;for(let h of this.contexts)if(h===e&&(g=!0),g){if(f!==h.name&&h.isolated)return;h.flushSelection()}},propagateSelection:function(e,f,g,h){var j=this.righty(h);if(j&&!(e.group!==j.group&&j.isolate)){e.hasFocusChanged()&&this.invalidateLookupCache();var l=this.relationshipsFilter(j.items,!f,j,h+1);j.gapLookup&&l.push(j._items[this.cfg.offsetY]),j.isLookupCached()||(j.setFilteredList(l),j.enter()),j.invalidateLookupCache();for(let m of l)j.focus=m,j._toggleResultItem(m[j.itemKey],this,g,h+1)}},filterForeignMatch:function(e,f,g,h,j,l,m){return!e&&(m&&!m[h]||!f[h]||-1<f[h].indexOf(g.focus[m&&m[h]?m[h]:l]))||e&&(!m||m[h])&&f[h]&&!(-1<f[h].indexOf(g.focus[m[h]||l]))},filterSharedMatch:function(e,f,g,h,j,l,m,n){return n.topLevelShared=!1,n.lastOther=g.focus,!e&&(!m||!m[h]||g._items.some(function(o){return o[j]===g.focus[j]&&o[m[h]]===f[m[h]]}))||e&&m&&m[h]&&!g._items.some(function(o){return o[m[h]]===f[m[h]]})},relationshipsFilter:function(e,f,g,h){g||(g=this.getContext());var j=e,l=[],m=this;h||(h=0),g.topLevelShared=!0,g.lastOther=null;for(var n=this.cfg.offsetX,o=null;n<this.curX+this.cfg.offsetX+h;n++)if(o=this.listChannel[n],!g.foreignKeyRelationships||g.foreignKeyRelationships[o.name]||g.sharedKeyRelationships&&g.sharedKeyRelationships[o.name]){if(!o.focus)return console.error('Panic, other.focus not set!'),[];if(o.focus.__unfiltered||o.focus.__empty)continue;let p=f&&n===this.curX+this.cfg.offsetX+h-1||o.focus.__noMatch;j.every(function(q){return q.__filler&&!f?l.push(q):((g.sharedKeyRelationships&&g.sharedKeyRelationships[o[o._name]]?m.filterSharedMatch(p,q,o,o.name,o._name,g.foreignKey,g.sharedKeyRelationships,g):m.filterForeignMatch(p,q,o,o.name,o._name,g.foreignKey,g.foreignKeyRelationships))&&l.push(q),!(f&&p)||!l.length)}),j=l,l=[]}return j},setFilterElement:function(e,f){for(let g of this.contexts)if(g.name===e)return g.setFilterElement(f)}};var d;return{create:function(e,f){return d=new c(e,f),d}}}])}();