UNPKG

ag-grid-autocomplete-editor

Version:

Quick implementation of autocompletion into ag-Grid cell using autocompleter package.

2 lines 9.64 kB
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("@ag-grid-community/core"));else if("function"==typeof define&&define.amd)define(["@ag-grid-community/core"],t);else{var n="object"==typeof exports?t(require("@ag-grid-community/core")):t(e["@ag-grid-community/core"]);for(var o in n)("object"==typeof exports?exports:e)[o]=n[o]}}(self,(function(e){return(()=>{"use strict";var t={638:(e,t,n)=>{n.r(t)},801:t=>{t.exports=e}},n={};function o(e){var r=n[e];if(void 0!==r)return r.exports;var i=n[e]={exports:{}};return t[e](i,i.exports,o),i.exports}o.d=(e,t)=>{for(var n in t)o.o(t,n)&&!o.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},o.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),o.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var r={};return(()=>{o.r(r),o.d(r,{AutocompleteSelectCellEditor:()=>v,default:()=>m});var e=o(801);function t(e){var t=document.createElement("div");return t.textContent=e,t.className="group",t}function n(e){var t=document.createElement("div");return t.textContent=e.label||"",t}function i(e){var t=e;return[function(){return t},function(e){return t=e}]}var u="input",c=new Set([38,13,27,39,37,16,17,18,20,91,9]);function a(e){var o=i(e.input.ownerDocument||window.document),r=o[0],a=o[1],s=i([]),l=s[0],p=s[1],f=i(""),d=f[0],v=f[1],m=i(void 0),g=m[0],h=m[1],y=i(0),b=y[0],w=y[1],E=i(void 0),x=E[0],S=E[1],L=e.strict,O=e.autoselectfirst,T=e.onFreeTextSelect,C=e.minLength,I=void 0===C?2:C,P=e.showOnFocus,D=e.input,A=e.className,j=e.customize,N=e.emptyMsg,M=e.render,k=e.renderGroup,_=e.debounceWaitMs||0,F=I,H=document.createElement("div"),q=H.style;H.className="autocomplete "+(A||""),q.position="fixed";var W=function(){w(b()+1)};function K(){var e=x();void 0!==e&&(window.clearTimeout(e),S(void 0))}function z(){return!!H.parentNode}function G(){W(),p([]),v(""),h(void 0),H&&H.remove()}function V(t,n){n.preventDefault(),n.stopPropagation(),e.onSelect(t,D,n),G()}function R(){!function(e){for(;e.firstChild;)e.firstChild.remove()}(H);var e=l(),o=function(e,o,r,i,u,c){void 0===u&&(u=n),void 0===c&&(c=t);for(var a=document.createDocumentFragment(),s=[],l="#9?$",p=function(e){if(e.group&&e.group!==l){l=e.group;var t=c(e.group,r);s.push(t)}var n=u(e,r);n.addEventListener("click",(function(t){return i(e,t)})),e===o&&n.classList.add("selected"),s.push(n)},f=0,d=e;f<d.length;f++)p(d[f]);return a.append.apply(a,s),a}(e,g(),d(),V,M,k);if(H.append(o),0===e.length&&L){if(!N)return void G();var i=document.createElement("div");i.className="empty",i.textContent=N,H.append(i),h(void 0)}a(D.ownerDocument||window.document),H.parentNode||r().body.append(H),function(){if(z()){var e=r();q.height="auto",q.width=D.offsetWidth+"px";var t=D.getBoundingClientRect(),n=t.top+D.offsetHeight,o=e.defaultView.innerHeight-n;o<0&&(o=0),q.top=n+"px",q.bottom="",q.left=t.left+"px",q.maxHeight=o+"px",j&&j(D,t,H,o)}}(),function(){var e=H.querySelector(".selected");if(e){var t=e.previousElementSibling;if(t&&null===t.previousElementSibling&&t.className.includes("group")&&(e=t),e.offsetTop<H.scrollTop)H.scrollTop=e.offsetTop;else{var n=e.offsetTop+e.offsetHeight,o=H.scrollTop+H.offsetHeight;n>o&&(H.scrollTop+=n-o)}}}()}function $(){z()&&R()}function B(){$()}function J(e){e.target!==H?$():e.preventDefault()}function Q(t){W(),D.value.length>=F||1===t?(K(),S(function(t){var n=b(),o=0===t?_:0,r=D.value,i=function(e){b()===n&&e&&(p(e),v(r),h(e.length>0&&O?e[0]:void 0),R())};return window.setTimeout((function(){e.fetch(r,i,t)}),o)}(t))):G()}function U(e){var t=e.which||e.keyCode||0;c.has(t)||40===t&&z()||Q(0)}function X(t){var n=t.which||t.keyCode||0;if(38===n||40===n||27===n||13===n||9===n){if(t.preventDefault(),z()&&t.stopPropagation(),38===n||40===n||27===n)return void(27===n?(e.onSelect(void 0,D,t),G()):function(e){if(z()&&l().length>0){switch(e){case 38:!function(){var e=l(),t=g();if(0!==e.length)if(void 0!==t&&t!==e[0]){for(var n=e.length-1;n>0;n-=1)if(t===e[n]||1===n)return void h(e[n-1])}else h(e[e.length-1]);else h(void 0)}();break;case 40:!function(){var e=l(),t=g();if(0!==e.length)if(void 0!==t&&t!==e[e.length-1]){for(var n=0;n<e.length-1;n+=1)if(t===e[n])return void h(e[n+1])}else h(e[0]);else h(void 0)}()}R()}}(n));13!==n&&9!==n||function(t){var n=g();if(L)e.onSelect(n,D,t);else{var o={label:D.value};if(n)return void e.onSelect(n,D,t);T&&T(o,D),e.onSelect(o,D,t)}G()}(t)}}function Y(){P&&Q(1)}function Z(e){e.preventDefault(),e.stopPropagation(),setTimeout((function(){r().activeElement!==D&&G()}),200)}return function(){var e=r();D.addEventListener("keydown",X),D.addEventListener(u,U),D.addEventListener("focusout",Z),D.addEventListener("focus",Y),e.addEventListener("resize",B),e.addEventListener("scroll",J,!0)}(),{destroy:function(){!function(){var e=r();D.removeEventListener("focus",Y),D.removeEventListener("keydown",X),D.removeEventListener(u,U),D.removeEventListener("focusout",Z),e.removeEventListener("resize",B),e.removeEventListener("scroll",J,!0)}(),K(),G(),W()}}}var s,l=(s=function(e,t){return(s=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])})(e,t)},function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=e}s(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}),p=function(){return(p=Object.assign||function(e){for(var t,n=1,o=arguments.length;n<o;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e}).apply(this,arguments)};o(638);var f=new Set([8,46,13,9,38,40]),d=function(e){function t(){var t=e.call(this,'<div class="ag-wrapper ag-input-wrapper ag-text-field-input-wrapper ag-cell-editor-autocomplete-wrapper" style="padding: 0 !important;"><input class="ag-input-field-input ag-text-field-input ag-cell-editor-autocomplete-input" type="text"/></div>')||this;return t.focusAfterAttached=!1,t.required=!1,t.eInput=t.getGui().querySelector("input"),t.currentItem&&(t.eInput.value=t.currentItem.label||t.currentItem.value),t}return l(t,e),t.getSelectData=function(e){return"function"==typeof e.selectData?e.selectData(e):Array.isArray(e.selectData)?e.selectData:[]},t.getDefaultAutocompleteSettings=function(e){return{showOnFocus:!1,render:function(e,t,n){var o=document.createElement("div"),r=(null!=n?n:"").replace(/[$()*+.?[\\\]^{|}]/g,"\\$&"),i=new RegExp(r,"gi"),u=document.createElement("span");return u.innerHTML=t.label.replace(i,(function(e){return"<strong>"+e+"</strong>"})),o.append(u),e.addManagedListener(o,"mousedown",(function(n){e.currentItem=t,n.stopPropagation()})),o},renderGroup:function(e,t){var n=document.createElement("div");return n.textContent=t,n.className="group",n},className:"ag-cell-editor-autocomplete",minLength:1,emptyMsg:"None",strict:!0,autoselectfirst:!0,onFreeTextSelect:function(){},onSelect:function(e,t){e.currentItem=t},fetch:function(n,o,r){var i=t.getSelectData(e),u=o.toLowerCase()||n.eInput.value.toLowerCase();r(i.filter((function(e){return e.label.toLowerCase().includes(u)})))},debounceWaitMs:200,customize:function(e,t,n,o,r){r<100&&(o.style.top="10px",o.style.bottom=window.innerHeight-n.bottom+t.offsetHeight+"px",o.style.maxHeight="140px")}}},t.suppressKeyboardEvent=function(e){var t=e.event.keyCode;return e.editing&&f.has(t)},t.getStartValue=function(e){return 8===e.keyPress||46===e.keyPress?"":e.charPress?e.charPress:e.formatValue(e.value)},t.prototype.init=function(e){var n=this;this.stopEditing=e.stopEditing;var o=t.getDefaultAutocompleteSettings(e);this.focusAfterAttached=e.cellStartedEdit,this.eInput.placeholder=e.placeholder||"",this.eInput.value=t.getStartValue(e);var r=p(p({},o),e.autocomplete);this.autocompleter=a({input:this.eInput,render:function(e,t){return r.render(n,e,t)},renderGroup:function(e,t){return r.renderGroup(n,e,t)},className:r.className,minLength:r.minLength,emptyMsg:r.emptyMsg,strict:r.strict,autoselectfirst:r.autoselectfirst,showOnFocus:r.showOnFocus,onFreeTextSelect:function(e,t){return r.onFreeTextSelect(n,e,t)},onSelect:function(e,t,o){var i=r.onSelect(n,e,t);return o instanceof KeyboardEvent||o instanceof o.view.document.defaultView.KeyboardEvent?n.handleTabEvent(o):n.destroy(),i},fetch:function(e,t,o){return r.fetch(n,e,t,o)},debounceWaitMs:r.debounceWaitMs,customize:function(e,t,o,i){return r.customize(n,e,t,o,i)}}),e.required&&(this.required=!0),e.colDef.suppressKeyboardEvent||(e.colDef.suppressKeyboardEvent=t.suppressKeyboardEvent)},t.prototype.handleTabEvent=function(e){9===(e.which||e.keyCode||0)&&this.gridOptionsWrapper?e.shiftKey?this.gridOptionsWrapper.getApi().tabToPreviousCell():this.gridOptionsWrapper.getApi().tabToNextCell():this.destroy()},t.prototype.afterGuiAttached=function(){if(this.focusAfterAttached){var e=this.eInput;e.focus(),e.select();var t=e.value?e.value.length:0;t>0&&e.setSelectionRange(t,t)}},t.prototype.focusIn=function(){this.eInput.focus(),this.eInput.select()},t.prototype.focusOut=function(){this.eInput.blur(),this.autocompleter.destroy()},t.prototype.destroy=function(){this.focusOut(),this.stopEditing&&this.stopEditing()},t.prototype.getValue=function(){return this.currentItem},t.prototype.isCancelAfterEnd=function(){return!!this.required&&!this.currentItem},t.prototype.isCancelBeforeStart=function(){return!1},t.prototype.isPopup=function(){return!1},t}(e.PopupComponent);const v=d,m=d})(),r})()})); //# sourceMappingURL=index.js.map