signalk-ais-target-prioritizer
Version:
A SignalK plugin that priorizes AIS targets according to guard and CPA criteria.
1 lines • 4.58 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("rbush")):"function"==typeof define&&define.amd?define(["rbush"],t):"object"==typeof exports?exports.labelgun=t(require("rbush")):e.labelgun=t(e.rbush)}(this,function(e){return function(e){function t(l){if(a[l])return a[l].exports;var i=a[l]={i:l,l:!1,exports:{}};return e[l].call(i.exports,i,i.exports,t),i.l=!0,i.exports}var a={};return t.m=e,t.c=a,t.d=function(e,a,l){t.o(e,a)||Object.defineProperty(e,a,{configurable:!1,enumerable:!0,get:l})},t.n=function(e){var a=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(a,"a",a),a},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,a){"use strict";function l(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var i=function(){function e(e,t){for(var a=0;a<t.length;a++){var l=t[a];l.enumerable=l.enumerable||!1,l.configurable=!0,"value"in l&&(l.writable=!0),Object.defineProperty(e,l.key,l)}}return function(t,a,l){return a&&e(t.prototype,a),l&&e(t,l),t}}(),n=a(1),s=function(e){return e&&e.__esModule?e:{default:e}}(n),r=function(){function e(t,a,i){l(this,e);var n=i||10;this.tree=(0,s.default)(n),this.allLabels={},this.hasChanged=[],this.allChanged=!1,this.hideLabel=t,this.showLabel=a}return i(e,[{key:"_total",value:function(e){var t=0;for(var a in this.allLabels)this.allLabels[a].state==e&&(t+=1);return t}},{key:"totalShown",value:function(){return this._total("show")}},{key:"totalHidden",value:function(){return this._total("hide")}},{key:"_getLabelsByState",value:function(e){var t=[];for(var a in this.allLabels)this.allLabels[a].state==e&&t.push(this.allLabels[a]);return t}},{key:"getHidden",value:function(){return this._getLabelsByState("hide")}},{key:"getShown",value:function(){return this._getLabelsByState("show")}},{key:"getCollisions",value:function(e){var t=this.allLabels[e];if(void 0===t)throw Error("Label doesn't exist :"+JSON.stringify(e));var a=this.tree.search(t),l=a.indexOf(t);return void 0!==l&&a.splice(l,1),a}},{key:"getLabel",value:function(e){return this.allLabels[e]}},{key:"reset",value:function(){this.tree.clear(),this.allLabels={},this.hasChanged=[],this.allChanged=!1}},{key:"_callLabelCallbacks",value:function(e){for(var t in this.allLabels)this._callLabelStateCallback(this.allLabels[t],e)}},{key:"_callLabelStateCallback",value:function(e,t){var a=t||e.state;"show"===a&&this.showLabel(e),"hide"===a&&this.hideLabel(e)}},{key:"_compareLabels",value:function(){var e=this;this.orderedLabels=[];for(var t in this.allLabels)this.orderedLabels.push(this.allLabels[t]);this.orderedLabels.sort(this._compare),this.orderedLabels.forEach(function(t){var a=e.tree.search(t);(0===a.length||t.isDragged||e._allLower(a,t))&&(e.allLabels[t.id].state="show")})}},{key:"_allLower",value:function(e,t){for(var a=void 0,l=0;l<e.length;l++)if(a=e[l],"show"===a.state||a.weight>t.weight||a.isDragged)return!1;return!0}},{key:"_compare",value:function(e,t){return t.weight-e.weight}},{key:"_setupLabels",value:function(){var e=this;if(this.allChanged){this.allChanged=!1,this.hasChanged=[],this.tree.clear();var t=[];for(var a in this.allLabels)this._handleLabelIngestion(a),t.push(this.allLabels[a]);this.tree.load(t)}else this.hasChanged.length>0&&(this.hasChanged.forEach(function(t){e._handleLabelIngestion(t),e.tree.insert(e.allLabels[t])}),this.hasChanged=[])}},{key:"_handleLabelIngestion",value:function(e){var t=this.allLabels[e];this.ingestLabel({bottomLeft:[t.minX,t.minY],topRight:[t.maxX,t.maxY]},t.id,t.weight,t.labelObject,t.name,t.isDragged)}},{key:"update",value:function(e){this.allChanged=!e,this._setupLabels(),this._compareLabels(),this._callLabelCallbacks()}},{key:"removeLabel",value:function(e,t){this.tree.remove(t||this.allLabels[e]),delete this.allLabels[e]}},{key:"ingestLabel",value:function(e,t,a,l,i,n){if(void 0!==a&&null!==a||(a=0),!e||!e.bottomLeft||!e.topRight)throw Error("Bounding box must be defined with bottomLeft and topRight properties");if("string"!=typeof t&&"number"!=typeof t)throw Error("Label IDs must be a string or a number");var s=this.allLabels[t];s&&this.removeLabel(s.id,s);var r={minX:e.bottomLeft[0],minY:e.bottomLeft[1],maxX:e.topRight[0],maxY:e.topRight[1],state:"hide",id:t,weight:a,labelObject:l,name:i,isDragged:n};this.allLabels[r.id]=r}},{key:"labelHasChanged",value:function(e){-1===this.hasChanged.indexOf(e)&&this.hasChanged.push(e)}}]),e}();t.default=r},function(t,a){t.exports=e}])});