dataTables.searchBuilder.js 165 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707
  1. /*! SearchBuilder 1.2.1
  2. * ©SpryMedia Ltd - datatables.net/license/mit
  3. */
  4. (function () {
  5. 'use strict';
  6. var $$2;
  7. var dataTable$2;
  8. // eslint-disable-next-line no-extra-parens
  9. var moment = window.moment;
  10. // eslint-disable-next-line no-extra-parens
  11. var luxon = window.luxon;
  12. /**
  13. * Sets the value of jQuery for use in the file
  14. *
  15. * @param jq the instance of jQuery to be set
  16. */
  17. function setJQuery$2(jq) {
  18. $$2 = jq;
  19. dataTable$2 = jq.fn.dataTable;
  20. }
  21. /**
  22. * The Criteria class is used within SearchBuilder to represent a search criteria
  23. */
  24. var Criteria = /** @class */ (function () {
  25. function Criteria(table, opts, topGroup, index, depth) {
  26. var _this = this;
  27. if (index === void 0) { index = 0; }
  28. if (depth === void 0) { depth = 1; }
  29. // Check that the required version of DataTables is included
  30. if (!dataTable$2 || !dataTable$2.versionCheck || !dataTable$2.versionCheck('1.10.0')) {
  31. throw new Error('SearchPane requires DataTables 1.10 or newer');
  32. }
  33. this.classes = $$2.extend(true, {}, Criteria.classes);
  34. // Get options from user and any extra conditions/column types defined by plug-ins
  35. this.c = $$2.extend(true, {}, Criteria.defaults, $$2.fn.dataTable.ext.searchBuilder, opts);
  36. var i18n = this.c.i18n;
  37. this.s = {
  38. condition: undefined,
  39. conditions: {},
  40. data: undefined,
  41. dataIdx: -1,
  42. dataPoints: [],
  43. dateFormat: false,
  44. depth: depth,
  45. dt: table,
  46. filled: false,
  47. index: index,
  48. origData: undefined,
  49. topGroup: topGroup,
  50. type: '',
  51. value: []
  52. };
  53. this.dom = {
  54. buttons: $$2('<div/>')
  55. .addClass(this.classes.buttonContainer),
  56. condition: $$2('<select disabled/>')
  57. .addClass(this.classes.condition)
  58. .addClass(this.classes.dropDown)
  59. .addClass(this.classes.italic)
  60. .attr('autocomplete', 'hacking'),
  61. conditionTitle: $$2('<option value="" disabled selected hidden/>')
  62. .text(this.s.dt.i18n('searchBuilder.condition', i18n.condition)),
  63. container: $$2('<div/>')
  64. .addClass(this.classes.container),
  65. data: $$2('<select/>')
  66. .addClass(this.classes.data)
  67. .addClass(this.classes.dropDown)
  68. .addClass(this.classes.italic),
  69. dataTitle: $$2('<option value="" disabled selected hidden/>')
  70. .text(this.s.dt.i18n('searchBuilder.data', i18n.data)),
  71. defaultValue: $$2('<select disabled/>')
  72. .addClass(this.classes.value)
  73. .addClass(this.classes.dropDown)
  74. .addClass(this.classes.select),
  75. "delete": $$2('<button>&times</button>')
  76. .addClass(this.classes["delete"])
  77. .addClass(this.classes.button)
  78. .attr('title', this.s.dt.i18n('searchBuilder.deleteTitle', i18n.deleteTitle))
  79. .attr('type', 'button'),
  80. // eslint-disable-next-line no-useless-escape
  81. left: $$2('<button>\<</button>')
  82. .addClass(this.classes.left)
  83. .addClass(this.classes.button)
  84. .attr('title', this.s.dt.i18n('searchBuilder.leftTitle', i18n.leftTitle))
  85. .attr('type', 'button'),
  86. // eslint-disable-next-line no-useless-escape
  87. right: $$2('<button>\></button>')
  88. .addClass(this.classes.right)
  89. .addClass(this.classes.button)
  90. .attr('title', this.s.dt.i18n('searchBuilder.rightTitle', i18n.rightTitle))
  91. .attr('type', 'button'),
  92. value: [
  93. $$2('<select disabled/>')
  94. .addClass(this.classes.value)
  95. .addClass(this.classes.dropDown)
  96. .addClass(this.classes.italic)
  97. .addClass(this.classes.select)
  98. ],
  99. valueTitle: $$2('<option value="--valueTitle--" selected/>')
  100. .text(this.s.dt.i18n('searchBuilder.value', i18n.value))
  101. };
  102. // If the greyscale option is selected then add the class to add the grey colour to SearchBuilder
  103. if (this.c.greyscale) {
  104. this.dom.data.addClass(this.classes.greyscale);
  105. this.dom.condition.addClass(this.classes.greyscale);
  106. this.dom.defaultValue.addClass(this.classes.greyscale);
  107. for (var _i = 0, _a = this.dom.value; _i < _a.length; _i++) {
  108. var val = _a[_i];
  109. val.addClass(this.classes.greyscale);
  110. }
  111. }
  112. // For responsive design, adjust the criterias properties on the following events
  113. this.s.dt.on('draw.dtsp', function () {
  114. _this._adjustCriteria();
  115. });
  116. this.s.dt.on('buttons-action', function () {
  117. _this._adjustCriteria();
  118. });
  119. $$2(window).on('resize.dtsp', dataTable$2.util.throttle(function () {
  120. _this._adjustCriteria();
  121. }));
  122. this._buildCriteria();
  123. return this;
  124. }
  125. /**
  126. * Adds the left button to the criteria
  127. */
  128. Criteria.prototype.updateArrows = function (hasSiblings, redraw) {
  129. if (hasSiblings === void 0) { hasSiblings = false; }
  130. if (redraw === void 0) { redraw = true; }
  131. // Empty the container and append all of the elements in the correct order
  132. this.dom.container.children().detach();
  133. this.dom.container
  134. .append(this.dom.data)
  135. .append(this.dom.condition)
  136. .append(this.dom.value[0]);
  137. this.setListeners();
  138. // Trigger the inserted events for the value elements as they are inserted
  139. if (this.dom.value[0] !== undefined) {
  140. this.dom.value[0].trigger('dtsb-inserted');
  141. }
  142. for (var i = 1; i < this.dom.value.length; i++) {
  143. this.dom.container.append(this.dom.value[i]);
  144. this.dom.value[i].trigger('dtsb-inserted');
  145. }
  146. // If this is a top level criteria then don't let it move left
  147. if (this.s.depth > 1) {
  148. this.dom.buttons.append(this.dom.left);
  149. }
  150. // If the depthLimit of the query has been hit then don't add the right button
  151. if ((this.c.depthLimit === false || this.s.depth < this.c.depthLimit) && hasSiblings) {
  152. this.dom.buttons.append(this.dom.right);
  153. }
  154. else {
  155. this.dom.right.remove();
  156. }
  157. this.dom.buttons.append(this.dom["delete"]);
  158. this.dom.container.append(this.dom.buttons);
  159. if (redraw) {
  160. // A different combination of arrows and selectors may lead to a need for responsive to be triggered
  161. this._adjustCriteria();
  162. }
  163. };
  164. /**
  165. * Destroys the criteria, removing listeners and container from the dom
  166. */
  167. Criteria.prototype.destroy = function () {
  168. // Turn off listeners
  169. this.dom.data.off('.dtsb');
  170. this.dom.condition.off('.dtsb');
  171. this.dom["delete"].off('.dtsb');
  172. for (var _i = 0, _a = this.dom.value; _i < _a.length; _i++) {
  173. var val = _a[_i];
  174. val.off('.dtsb');
  175. }
  176. // Remove container from the dom
  177. this.dom.container.remove();
  178. };
  179. /**
  180. * Passes in the data for the row and compares it against this single criteria
  181. *
  182. * @param rowData The data for the row to be compared
  183. * @returns boolean Whether the criteria has passed
  184. */
  185. Criteria.prototype.search = function (rowData, rowIdx) {
  186. var condition = this.s.conditions[this.s.condition];
  187. if (this.s.condition !== undefined && condition !== undefined) {
  188. var filter = rowData[this.s.dataIdx];
  189. // This check is in place for if a custom decimal character is in place
  190. if (this.s.type.includes('num') &&
  191. (this.s.dt.settings()[0].oLanguage.sDecimal !== '' ||
  192. this.s.dt.settings()[0].oLanguage.sThousands !== '')) {
  193. var splitRD = [rowData[this.s.dataIdx]];
  194. if (this.s.dt.settings()[0].oLanguage.sDecimal !== '') {
  195. splitRD = rowData[this.s.dataIdx].split(this.s.dt.settings()[0].oLanguage.sDecimal);
  196. }
  197. if (this.s.dt.settings()[0].oLanguage.sThousands !== '') {
  198. for (var i = 0; i < splitRD.length; i++) {
  199. splitRD[i] = splitRD[i].replace(this.s.dt.settings()[0].oLanguage.sThousands, ',');
  200. }
  201. }
  202. filter = splitRD.join('.');
  203. }
  204. // If orthogonal data is in place we need to get it's values for searching
  205. if (this.c.orthogonal.search !== 'filter') {
  206. var settings = this.s.dt.settings()[0];
  207. filter = settings.oApi._fnGetCellData(settings, rowIdx, this.s.dataIdx, typeof this.c.orthogonal === 'string' ?
  208. this.c.orthogonal :
  209. this.c.orthogonal.search);
  210. }
  211. if (this.s.type === 'array') {
  212. // Make sure we are working with an array
  213. if (!Array.isArray(filter)) {
  214. filter = [filter];
  215. }
  216. filter.sort();
  217. for (var _i = 0, filter_1 = filter; _i < filter_1.length; _i++) {
  218. var filt = filter_1[_i];
  219. if (filt) {
  220. filt = filt.replace(/[\r\n\u2028]/g, ' ');
  221. }
  222. }
  223. }
  224. else if (filter !== null) {
  225. filter = filter.replace(/[\r\n\u2028]/g, ' ');
  226. }
  227. if (this.s.type.includes('html')) {
  228. filter = filter.replace(/(<([^>]+)>)/ig, '');
  229. }
  230. // Not ideal, but jqueries .val() returns an empty string even
  231. // when the value set is null, so we shall assume the two are equal
  232. if (filter === null) {
  233. filter = '';
  234. }
  235. return condition.search(filter, this.s.value, this);
  236. }
  237. };
  238. /**
  239. * Gets the details required to rebuild the criteria
  240. */
  241. Criteria.prototype.getDetails = function (deFormatDates) {
  242. if (deFormatDates === void 0) { deFormatDates = false; }
  243. // This check is in place for if a custom decimal character is in place
  244. if (this.s.type !== null &&
  245. this.s.type.includes('num') &&
  246. (this.s.dt.settings()[0].oLanguage.sDecimal !== '' || this.s.dt.settings()[0].oLanguage.sThousands !== '')) {
  247. for (var i = 0; i < this.s.value.length; i++) {
  248. var splitRD = [this.s.value[i].toString()];
  249. if (this.s.dt.settings()[0].oLanguage.sDecimal !== '') {
  250. splitRD = this.s.value[i].split(this.s.dt.settings()[0].oLanguage.sDecimal);
  251. }
  252. if (this.s.dt.settings()[0].oLanguage.sThousands !== '') {
  253. for (var j = 0; j < splitRD.length; j++) {
  254. splitRD[j] = splitRD[j].replace(this.s.dt.settings()[0].oLanguage.sThousands, ',');
  255. }
  256. }
  257. this.s.value[i] = splitRD.join('.');
  258. }
  259. }
  260. else if (this.s.type !== null && deFormatDates) {
  261. if (this.s.type.includes('date') ||
  262. this.s.type.includes('time')) {
  263. for (var i = 0; i < this.s.value.length; i++) {
  264. if (this.s.value[i].match(/^\d{4}-([0]\d|1[0-2])-([0-2]\d|3[01])$/g) === null) {
  265. this.s.value[i] = '';
  266. }
  267. }
  268. }
  269. else if (this.s.type.includes('moment')) {
  270. for (var i = 0; i < this.s.value.length; i++) {
  271. this.s.value[i] = moment(this.s.value[i], this.s.dateFormat).toISOString();
  272. }
  273. }
  274. else if (this.s.type.includes('luxon')) {
  275. for (var i = 0; i < this.s.value.length; i++) {
  276. this.s.value[i] = luxon.DateTime.fromFormat(this.s.value[i], this.s.dateFormat).toISO();
  277. }
  278. }
  279. }
  280. if (this.s.type.includes('num') && this.s.dt.page.info().serverSide) {
  281. for (var i = 0; i < this.s.value.length; i++) {
  282. this.s.value[i] = this.s.value[i].replace(/[^0-9.]/g, '');
  283. }
  284. }
  285. return {
  286. condition: this.s.condition,
  287. data: this.s.data,
  288. origData: this.s.origData,
  289. type: this.s.type,
  290. value: this.s.value.map(function (a) { return a.toString(); })
  291. };
  292. };
  293. /**
  294. * Getter for the node for the container of the criteria
  295. *
  296. * @returns JQuery<HTMLElement> the node for the container
  297. */
  298. Criteria.prototype.getNode = function () {
  299. return this.dom.container;
  300. };
  301. /**
  302. * Populates the criteria data, condition and value(s) as far as has been selected
  303. */
  304. Criteria.prototype.populate = function () {
  305. this._populateData();
  306. // If the column index has been found attempt to select a condition
  307. if (this.s.dataIdx !== -1) {
  308. this._populateCondition();
  309. // If the condittion has been found attempt to select the values
  310. if (this.s.condition !== undefined) {
  311. this._populateValue();
  312. }
  313. }
  314. };
  315. /**
  316. * Rebuilds the criteria based upon the details passed in
  317. *
  318. * @param loadedCriteria the details required to rebuild the criteria
  319. */
  320. Criteria.prototype.rebuild = function (loadedCriteria) {
  321. // Check to see if the previously selected data exists, if so select it
  322. var foundData = false;
  323. var dataIdx;
  324. this._populateData();
  325. // If a data selection has previously been made attempt to find and select it
  326. if (loadedCriteria.data !== undefined) {
  327. var italic_1 = this.classes.italic;
  328. var data_1 = this.dom.data;
  329. this.dom.data.children('option').each(function () {
  330. if ($$2(this).text() === loadedCriteria.data) {
  331. $$2(this).prop('selected', true);
  332. data_1.removeClass(italic_1);
  333. foundData = true;
  334. dataIdx = $$2(this).val();
  335. }
  336. else {
  337. $$2(this).removeProp('selected');
  338. }
  339. });
  340. }
  341. // If the data has been found and selected then the condition can be populated and searched
  342. if (foundData) {
  343. this.s.data = loadedCriteria.data;
  344. this.s.origData = loadedCriteria.origData;
  345. this.s.dataIdx = dataIdx;
  346. this.c.orthogonal = this._getOptions().orthogonal;
  347. this.dom.dataTitle.remove();
  348. this._populateCondition();
  349. this.dom.conditionTitle.remove();
  350. var condition = void 0;
  351. // Check to see if the previously selected condition exists, if so select it
  352. var options = this.dom.condition.children('option');
  353. // eslint-disable-next-line @typescript-eslint/prefer-for-of
  354. for (var i = 0; i < options.length; i++) {
  355. var option = $$2(options[i]);
  356. if (loadedCriteria.condition !== undefined &&
  357. option.val() === loadedCriteria.condition &&
  358. typeof loadedCriteria.condition === 'string') {
  359. option.prop('selected', true);
  360. condition = option.val();
  361. }
  362. else {
  363. option.removeProp('selected');
  364. }
  365. }
  366. this.s.condition = condition;
  367. // If the condition has been found and selected then the value can be populated and searched
  368. if (this.s.condition !== undefined) {
  369. this.dom.conditionTitle.removeProp('selected');
  370. this.dom.conditionTitle.remove();
  371. this.dom.condition.removeClass(this.classes.italic);
  372. // eslint-disable-next-line @typescript-eslint/prefer-for-of
  373. for (var i = 0; i < options.length; i++) {
  374. var option = $$2(options[i]);
  375. if (option.val() !== this.s.condition) {
  376. option.removeProp('selected');
  377. }
  378. }
  379. this._populateValue(loadedCriteria);
  380. }
  381. else {
  382. this.dom.conditionTitle.prependTo(this.dom.condition).prop('selected', true);
  383. }
  384. }
  385. };
  386. /**
  387. * Sets the listeners for the criteria
  388. */
  389. Criteria.prototype.setListeners = function () {
  390. var _this = this;
  391. this.dom.data
  392. .unbind('change')
  393. .on('change', function () {
  394. _this.dom.dataTitle.removeProp('selected');
  395. // Need to go over every option to identify the correct selection
  396. var options = _this.dom.data.children('option.' + _this.classes.option);
  397. // eslint-disable-next-line @typescript-eslint/prefer-for-of
  398. for (var i = 0; i < options.length; i++) {
  399. var option = $$2(options[i]);
  400. if (option.val() === _this.dom.data.val()) {
  401. _this.dom.data.removeClass(_this.classes.italic);
  402. option.prop('selected', true);
  403. _this.s.dataIdx = +option.val();
  404. _this.s.data = option.text();
  405. _this.s.origData = option.prop('origData');
  406. _this.c.orthogonal = _this._getOptions().orthogonal;
  407. // When the data is changed, the values in condition and
  408. // value may also change so need to renew them
  409. _this._clearCondition();
  410. _this._clearValue();
  411. _this._populateCondition();
  412. // If this criteria was previously active in the search then
  413. // remove it from the search and trigger a new search
  414. if (_this.s.filled) {
  415. _this.s.filled = false;
  416. _this.s.dt.draw();
  417. _this.setListeners();
  418. }
  419. _this.s.dt.state.save();
  420. }
  421. else {
  422. option.removeProp('selected');
  423. }
  424. }
  425. });
  426. this.dom.condition
  427. .unbind('change')
  428. .on('change', function () {
  429. _this.dom.conditionTitle.removeProp('selected');
  430. // Need to go over every option to identify the correct selection
  431. var options = _this.dom.condition.children('option.' + _this.classes.option);
  432. // eslint-disable-next-line @typescript-eslint/prefer-for-of
  433. for (var i = 0; i < options.length; i++) {
  434. var option = $$2(options[i]);
  435. if (option.val() === _this.dom.condition.val()) {
  436. _this.dom.condition.removeClass(_this.classes.italic);
  437. option.prop('selected', true);
  438. var condDisp = option.val();
  439. // Find the condition that has been selected and store it internally
  440. for (var _i = 0, _a = Object.keys(_this.s.conditions); _i < _a.length; _i++) {
  441. var cond = _a[_i];
  442. if (cond === condDisp) {
  443. _this.s.condition = condDisp;
  444. break;
  445. }
  446. }
  447. // When the condition is changed, the value selector may switch between
  448. // a select element and an input element
  449. _this._clearValue();
  450. _this._populateValue();
  451. for (var _b = 0, _c = _this.dom.value; _b < _c.length; _b++) {
  452. var val = _c[_b];
  453. // If this criteria was previously active in the search then remove
  454. // it from the search and trigger a new search
  455. if (_this.s.filled && val !== undefined && _this.dom.container.has(val[0]).length !== 0) {
  456. _this.s.filled = false;
  457. _this.s.dt.draw();
  458. _this.setListeners();
  459. }
  460. }
  461. if (_this.dom.value.length === 0 ||
  462. _this.dom.value.length === 1 && _this.dom.value[0] === undefined) {
  463. _this.s.dt.draw();
  464. }
  465. }
  466. else {
  467. option.removeProp('selected');
  468. }
  469. }
  470. });
  471. };
  472. /**
  473. * Adjusts the criteria to make SearchBuilder responsive
  474. */
  475. Criteria.prototype._adjustCriteria = function () {
  476. // If this criteria is not present then don't bother adjusting it
  477. if ($$2(document).has(this.dom.container).length === 0) {
  478. return;
  479. }
  480. var valRight;
  481. var valWidth;
  482. var outmostval = this.dom.value[this.dom.value.length - 1];
  483. // Calculate the width and right value of the outmost value element
  484. if (outmostval !== undefined && this.dom.container.has(outmostval[0]).length !== 0) {
  485. valWidth = outmostval.outerWidth(true);
  486. valRight = outmostval.offset().left + valWidth;
  487. }
  488. else {
  489. return;
  490. }
  491. var leftOffset = this.dom.left.offset();
  492. var rightOffset = this.dom.right.offset();
  493. var clearOffset = this.dom["delete"].offset();
  494. var hasLeft = this.dom.container.has(this.dom.left[0]).length !== 0;
  495. var hasRight = this.dom.container.has(this.dom.right[0]).length !== 0;
  496. var buttonsLeft = hasLeft ?
  497. leftOffset.left :
  498. hasRight ?
  499. rightOffset.left :
  500. clearOffset.left;
  501. // Perform the responsive calculations and redraw where necessary
  502. if ((buttonsLeft - valRight < 15 ||
  503. hasLeft && leftOffset.top !== clearOffset.top ||
  504. hasRight && rightOffset.top !== clearOffset.top) &&
  505. !this.dom.container.parent().hasClass(this.classes.vertical)) {
  506. this.dom.container.parent().addClass(this.classes.vertical);
  507. this.s.topGroup.trigger('dtsb-redrawContents');
  508. }
  509. else if (buttonsLeft -
  510. (this.dom.data.offset().left +
  511. this.dom.data.outerWidth(true) +
  512. this.dom.condition.outerWidth(true) +
  513. valWidth) > 15
  514. && this.dom.container.parent().hasClass(this.classes.vertical)) {
  515. this.dom.container.parent().removeClass(this.classes.vertical);
  516. this.s.topGroup.trigger('dtsb-redrawContents');
  517. }
  518. };
  519. /**
  520. * Builds the elements of the dom together
  521. */
  522. Criteria.prototype._buildCriteria = function () {
  523. // Append Titles for select elements
  524. this.dom.data.append(this.dom.dataTitle);
  525. this.dom.condition.append(this.dom.conditionTitle);
  526. // Add elements to container
  527. this.dom.container
  528. .append(this.dom.data)
  529. .append(this.dom.condition);
  530. for (var _i = 0, _a = this.dom.value; _i < _a.length; _i++) {
  531. var val = _a[_i];
  532. val.append(this.dom.valueTitle);
  533. this.dom.container.append(val);
  534. }
  535. // Add buttons to container
  536. this.dom.container
  537. .append(this.dom["delete"])
  538. .append(this.dom.right);
  539. this.setListeners();
  540. };
  541. /**
  542. * Clears the condition select element
  543. */
  544. Criteria.prototype._clearCondition = function () {
  545. this.dom.condition.empty();
  546. this.dom.conditionTitle.prop('selected', true).attr('disabled', 'true');
  547. this.dom.condition.prepend(this.dom.conditionTitle).prop('selectedIndex', 0);
  548. this.s.conditions = {};
  549. this.s.condition = undefined;
  550. };
  551. /**
  552. * Clears the value elements
  553. */
  554. Criteria.prototype._clearValue = function () {
  555. if (this.s.condition !== undefined) {
  556. if (this.dom.value.length > 0 && this.dom.value[0] !== undefined) {
  557. var _loop_1 = function (val) {
  558. if (val !== undefined) {
  559. // Timeout is annoying but because of IOS
  560. setTimeout(function () {
  561. val.remove();
  562. }, 50);
  563. }
  564. };
  565. // Remove all of the value elements
  566. for (var _i = 0, _a = this.dom.value; _i < _a.length; _i++) {
  567. var val = _a[_i];
  568. _loop_1(val);
  569. }
  570. }
  571. // Call the init function to get the value elements for this condition
  572. this.dom.value = [].concat(this.s.conditions[this.s.condition].init(this, Criteria.updateListener));
  573. if (this.dom.value.length > 0 && this.dom.value[0] !== undefined) {
  574. this.dom.value[0].insertAfter(this.dom.condition).trigger('dtsb-inserted');
  575. // Insert all of the value elements
  576. for (var i = 1; i < this.dom.value.length; i++) {
  577. this.dom.value[i].insertAfter(this.dom.value[i - 1]).trigger('dtsb-inserted');
  578. }
  579. }
  580. }
  581. else {
  582. var _loop_2 = function (val) {
  583. if (val !== undefined) {
  584. // Timeout is annoying but because of IOS
  585. setTimeout(function () {
  586. val.remove();
  587. }, 50);
  588. }
  589. };
  590. // Remove all of the value elements
  591. for (var _b = 0, _c = this.dom.value; _b < _c.length; _b++) {
  592. var val = _c[_b];
  593. _loop_2(val);
  594. }
  595. // Append the default valueTitle to the default select element
  596. this.dom.valueTitle
  597. .prop('selected', true);
  598. this.dom.defaultValue
  599. .append(this.dom.valueTitle)
  600. .insertAfter(this.dom.condition);
  601. }
  602. this.s.value = [];
  603. this.dom.value = [
  604. $$2('<select disabled/>')
  605. .addClass(this.classes.value)
  606. .addClass(this.classes.dropDown)
  607. .addClass(this.classes.italic)
  608. .addClass(this.classes.select)
  609. .append(this.dom.valueTitle.clone())
  610. ];
  611. };
  612. /**
  613. * Gets the options for the column
  614. *
  615. * @returns {object} The options for the column
  616. */
  617. Criteria.prototype._getOptions = function () {
  618. var table = this.s.dt;
  619. return $$2.extend(true, {}, Criteria.defaults, table.settings()[0].aoColumns[this.s.dataIdx].searchBuilder);
  620. };
  621. /**
  622. * Populates the condition dropdown
  623. */
  624. Criteria.prototype._populateCondition = function () {
  625. var conditionOpts = [];
  626. var conditionsLength = Object.keys(this.s.conditions).length;
  627. // If there are no conditions stored then we need to get them from the appropriate type
  628. if (conditionsLength === 0) {
  629. var column = +this.dom.data.children('option:selected').val();
  630. this.s.type = this.s.dt.columns().type().toArray()[column];
  631. var colInits = this.s.dt.settings()[0].aoColumns;
  632. if (colInits !== undefined) {
  633. var colInit = colInits[column];
  634. if (colInit.searchBuilderType !== undefined && colInit.searchBuilderType !== null) {
  635. this.s.type = colInit.searchBuilderType;
  636. }
  637. else if (this.s.type === undefined || this.s.type === null) {
  638. this.s.type = colInit.sType;
  639. }
  640. }
  641. // If the column type is still unknown, call a draw to try reading it again
  642. if (this.s.type === null || this.s.type === undefined) {
  643. $$2.fn.dataTable.ext.oApi._fnColumnTypes(this.s.dt.settings()[0]);
  644. this.s.type = this.s.dt.columns().type().toArray()[column];
  645. }
  646. // Enable the condition element
  647. this.dom.condition
  648. .removeAttr('disabled')
  649. .empty()
  650. .append(this.dom.conditionTitle)
  651. .addClass(this.classes.italic);
  652. this.dom.conditionTitle
  653. .prop('selected', true);
  654. var decimal = this.s.dt.settings()[0].oLanguage.sDecimal;
  655. // This check is in place for if a custom decimal character is in place
  656. if (decimal !== '' && this.s.type.indexOf(decimal) === this.s.type.length - decimal.length) {
  657. if (this.s.type.includes('num-fmt')) {
  658. this.s.type = this.s.type.replace(decimal, '');
  659. }
  660. else if (this.s.type.includes('num')) {
  661. this.s.type = this.s.type.replace(decimal, '');
  662. }
  663. }
  664. // Select which conditions are going to be used based on the column type
  665. var conditionObj = this.c.conditions[this.s.type] !== undefined ?
  666. this.c.conditions[this.s.type] :
  667. this.s.type.includes('moment') ?
  668. this.c.conditions.moment :
  669. this.s.type.includes('luxon') ?
  670. this.c.conditions.luxon :
  671. this.c.conditions.string;
  672. // If it is a moment format then extract the date format
  673. if (this.s.type.includes('moment')) {
  674. this.s.dateFormat = this.s.type.replace(/moment-/g, '');
  675. }
  676. else if (this.s.type.includes('luxon')) {
  677. this.s.dateFormat = this.s.type.replace(/luxon-/g, '');
  678. }
  679. // Add all of the conditions to the select element
  680. for (var _i = 0, _a = Object.keys(conditionObj); _i < _a.length; _i++) {
  681. var condition = _a[_i];
  682. if (conditionObj[condition] !== null) {
  683. // Serverside processing does not supply the options for the select elements
  684. // Instead input elements need to be used for these instead
  685. if (this.s.dt.page.info().serverSide && conditionObj[condition].init === Criteria.initSelect) {
  686. conditionObj[condition].init = Criteria.initInput;
  687. conditionObj[condition].inputValue = Criteria.inputValueInput;
  688. conditionObj[condition].isInputValid = Criteria.isInputValidInput;
  689. }
  690. this.s.conditions[condition] = conditionObj[condition];
  691. var condName = conditionObj[condition].conditionName;
  692. if (typeof condName === 'function') {
  693. condName = condName(this.s.dt, this.c.i18n);
  694. }
  695. conditionOpts.push($$2('<option>', {
  696. text: condName,
  697. value: condition
  698. })
  699. .addClass(this.classes.option)
  700. .addClass(this.classes.notItalic));
  701. }
  702. }
  703. }
  704. // Otherwise we can just load them in
  705. else if (conditionsLength > 0) {
  706. this.dom.condition.empty().removeAttr('disabled').addClass(this.classes.italic);
  707. for (var _b = 0, _c = Object.keys(this.s.conditions); _b < _c.length; _b++) {
  708. var condition = _c[_b];
  709. var condName = this.s.conditions[condition].conditionName;
  710. if (typeof condName === 'function') {
  711. condName = condName(this.s.dt, this.c.i18n);
  712. }
  713. var newOpt = $$2('<option>', {
  714. text: condName,
  715. value: condition
  716. })
  717. .addClass(this.classes.option)
  718. .addClass(this.classes.notItalic);
  719. if (this.s.condition !== undefined && this.s.condition === condName) {
  720. newOpt.prop('selected', true);
  721. this.dom.condition.removeClass(this.classes.italic);
  722. }
  723. conditionOpts.push(newOpt);
  724. }
  725. }
  726. else {
  727. this.dom.condition
  728. .attr('disabled', 'true')
  729. .addClass(this.classes.italic);
  730. return;
  731. }
  732. for (var _d = 0, conditionOpts_1 = conditionOpts; _d < conditionOpts_1.length; _d++) {
  733. var opt = conditionOpts_1[_d];
  734. this.dom.condition.append(opt);
  735. }
  736. this.dom.condition.prop('selectedIndex', 0);
  737. };
  738. /**
  739. * Populates the data select element
  740. */
  741. Criteria.prototype._populateData = function () {
  742. var _this = this;
  743. this.dom.data.empty().append(this.dom.dataTitle);
  744. // If there are no datas stored then we need to get them from the table
  745. if (this.s.dataPoints.length === 0) {
  746. this.s.dt.columns().every(function (index) {
  747. // Need to check that the column can be filtered on before adding it
  748. if (_this.c.columns === true ||
  749. _this.s.dt.columns(_this.c.columns).indexes().toArray().includes(index)) {
  750. var found = false;
  751. for (var _i = 0, _a = _this.s.dataPoints; _i < _a.length; _i++) {
  752. var val = _a[_i];
  753. if (val.index === index) {
  754. found = true;
  755. break;
  756. }
  757. }
  758. if (!found) {
  759. var col = _this.s.dt.settings()[0].aoColumns[index];
  760. var opt = {
  761. index: index,
  762. origData: col.data,
  763. text: (col.searchBuilderTitle === undefined ?
  764. col.sTitle :
  765. col.searchBuilderTitle).replace(/(<([^>]+)>)/ig, '')
  766. };
  767. _this.s.dataPoints.push(opt);
  768. _this.dom.data.append($$2('<option>', {
  769. text: opt.text,
  770. value: opt.index
  771. })
  772. .addClass(_this.classes.option)
  773. .addClass(_this.classes.notItalic)
  774. .prop('origData', col.data)
  775. .prop('selected', _this.s.dataIdx === opt.index ? true : false));
  776. if (_this.s.dataIdx === opt.index) {
  777. _this.dom.dataTitle.removeProp('selected');
  778. }
  779. }
  780. }
  781. });
  782. }
  783. // Otherwise we can just load them in
  784. else {
  785. var _loop_3 = function (data) {
  786. this_1.s.dt.columns().every(function (index) {
  787. var col = _this.s.dt.settings()[0].aoColumns[index];
  788. if ((col.searchBuilderTitle === undefined ?
  789. col.sTitle :
  790. col.searchBuilderTitle).replace(/(<([^>]+)>)/ig, '') === data.text) {
  791. data.index = index;
  792. data.origData = col.data;
  793. }
  794. });
  795. var newOpt = $$2('<option>', {
  796. text: data.text.replace(/(<([^>]+)>)/ig, ''),
  797. value: data.index
  798. })
  799. .addClass(this_1.classes.option)
  800. .addClass(this_1.classes.notItalic)
  801. .prop('origData', data.origData);
  802. if (this_1.s.data === data.text) {
  803. this_1.s.dataIdx = data.index;
  804. this_1.dom.dataTitle.removeProp('selected');
  805. newOpt.prop('selected', true);
  806. this_1.dom.data.removeClass(this_1.classes.italic);
  807. }
  808. this_1.dom.data.append(newOpt);
  809. };
  810. var this_1 = this;
  811. for (var _i = 0, _a = this.s.dataPoints; _i < _a.length; _i++) {
  812. var data = _a[_i];
  813. _loop_3(data);
  814. }
  815. }
  816. };
  817. /**
  818. * Populates the Value select element
  819. *
  820. * @param loadedCriteria optional, used to reload criteria from predefined filters
  821. */
  822. Criteria.prototype._populateValue = function (loadedCriteria) {
  823. var _this = this;
  824. var prevFilled = this.s.filled;
  825. this.s.filled = false;
  826. // Remove any previous value elements
  827. // Timeout is annoying but because of IOS
  828. setTimeout(function () {
  829. _this.dom.defaultValue.remove();
  830. }, 50);
  831. var _loop_4 = function (val) {
  832. // Timeout is annoying but because of IOS
  833. setTimeout(function () {
  834. if (val !== undefined) {
  835. val.remove();
  836. }
  837. }, 50);
  838. };
  839. for (var _i = 0, _a = this.dom.value; _i < _a.length; _i++) {
  840. var val = _a[_i];
  841. _loop_4(val);
  842. }
  843. var children = this.dom.container.children();
  844. if (children.length > 3) {
  845. for (var i = 2; i < children.length - 1; i++) {
  846. $$2(children[i]).remove();
  847. }
  848. }
  849. // Find the column with the title matching the data for the criteria and take note of the index
  850. if (loadedCriteria !== undefined) {
  851. this.s.dt.columns().every(function (index) {
  852. if (_this.s.dt.settings()[0].aoColumns[index].sTitle === loadedCriteria.data) {
  853. _this.s.dataIdx = index;
  854. }
  855. });
  856. }
  857. // Initialise the value elements based on the condition
  858. this.dom.value = [].concat(this.s.conditions[this.s.condition].init(this, Criteria.updateListener, loadedCriteria !== undefined ? loadedCriteria.value : undefined));
  859. if (loadedCriteria !== undefined && loadedCriteria.value !== undefined) {
  860. this.s.value = loadedCriteria.value;
  861. }
  862. // Insert value elements and trigger the inserted event
  863. if (this.dom.value[0] !== undefined) {
  864. this.dom.value[0]
  865. .insertAfter(this.dom.condition)
  866. .trigger('dtsb-inserted');
  867. }
  868. for (var i = 1; i < this.dom.value.length; i++) {
  869. this.dom.value[i]
  870. .insertAfter(this.dom.value[i - 1])
  871. .trigger('dtsb-inserted');
  872. }
  873. // Check if the criteria can be used in a search
  874. this.s.filled = this.s.conditions[this.s.condition].isInputValid(this.dom.value, this);
  875. this.setListeners();
  876. // If it can and this is different to before then trigger a draw
  877. if (prevFilled !== this.s.filled) {
  878. this.s.dt.draw();
  879. this.setListeners();
  880. }
  881. };
  882. /**
  883. * Provides throttling capabilities to SearchBuilder without having to use dt's _fnThrottle function
  884. * This is because that function is not quite suitable for our needs as it runs initially rather than waiting
  885. *
  886. * @param args arguments supplied to the throttle function
  887. * @returns Function that is to be run that implements the throttling
  888. */
  889. Criteria.prototype._throttle = function (fn, frequency) {
  890. if (frequency === void 0) { frequency = 200; }
  891. var last = null;
  892. var timer = null;
  893. var that = this;
  894. if (frequency === null) {
  895. frequency = 200;
  896. }
  897. return function () {
  898. var args = [];
  899. for (var _i = 0; _i < arguments.length; _i++) {
  900. args[_i] = arguments[_i];
  901. }
  902. var now = +new Date();
  903. if (last !== null && now < last + frequency) {
  904. clearTimeout(timer);
  905. }
  906. else {
  907. last = now;
  908. }
  909. timer = setTimeout(function () {
  910. last = null;
  911. fn.apply(that, args);
  912. }, frequency);
  913. };
  914. };
  915. Criteria.version = '1.1.0';
  916. Criteria.classes = {
  917. button: 'dtsb-button',
  918. buttonContainer: 'dtsb-buttonContainer',
  919. condition: 'dtsb-condition',
  920. container: 'dtsb-criteria',
  921. data: 'dtsb-data',
  922. "delete": 'dtsb-delete',
  923. dropDown: 'dtsb-dropDown',
  924. greyscale: 'dtsb-greyscale',
  925. input: 'dtsb-input',
  926. italic: 'dtsb-italic',
  927. joiner: 'dtsp-joiner',
  928. left: 'dtsb-left',
  929. notItalic: 'dtsb-notItalic',
  930. option: 'dtsb-option',
  931. right: 'dtsb-right',
  932. select: 'dtsb-select',
  933. value: 'dtsb-value',
  934. vertical: 'dtsb-vertical'
  935. };
  936. /**
  937. * Default initialisation function for select conditions
  938. */
  939. Criteria.initSelect = function (that, fn, preDefined, array) {
  940. if (preDefined === void 0) { preDefined = null; }
  941. if (array === void 0) { array = false; }
  942. var column = that.dom.data.children('option:selected').val();
  943. var indexArray = that.s.dt.rows().indexes().toArray();
  944. var settings = that.s.dt.settings()[0];
  945. // Declare select element to be used with all of the default classes and listeners.
  946. var el = $$2('<select/>')
  947. .addClass(Criteria.classes.value)
  948. .addClass(Criteria.classes.dropDown)
  949. .addClass(Criteria.classes.italic)
  950. .addClass(Criteria.classes.select)
  951. .append(that.dom.valueTitle)
  952. .on('change', function () {
  953. $$2(this).removeClass(Criteria.classes.italic);
  954. fn(that, this);
  955. });
  956. if (that.c.greyscale) {
  957. el.addClass(Criteria.classes.greyscale);
  958. }
  959. var added = [];
  960. var options = [];
  961. // Add all of the options from the table to the select element.
  962. // Only add one option for each possible value
  963. for (var _i = 0, indexArray_1 = indexArray; _i < indexArray_1.length; _i++) {
  964. var index = indexArray_1[_i];
  965. var filter = settings.oApi._fnGetCellData(settings, index, column, typeof that.c.orthogonal === 'string' ?
  966. that.c.orthogonal :
  967. that.c.orthogonal.search);
  968. var value = {
  969. filter: typeof filter === 'string' ?
  970. filter.replace(/[\r\n\u2028]/g, ' ') : // Need to replace certain characters to match search values
  971. filter,
  972. index: index,
  973. text: settings.oApi._fnGetCellData(settings, index, column, typeof that.c.orthogonal === 'string' ?
  974. that.c.orthogonal :
  975. that.c.orthogonal.display)
  976. };
  977. // If we are dealing with an array type, either make sure we are working with arrays, or sort them
  978. if (that.s.type === 'array') {
  979. value.filter = !Array.isArray(value.filter) ?
  980. [value.filter] :
  981. value.filter = value.filter.sort();
  982. value.text = !Array.isArray(value.text) ?
  983. [value.text] :
  984. value.text = value.text.sort();
  985. }
  986. // Function to add an option to the select element
  987. var addOption = function (filt, text) {
  988. // Add text and value, stripping out any html if that is the column type
  989. var opt = $$2('<option>', {
  990. type: Array.isArray(filt) ? 'Array' : 'String',
  991. value: that.s.type.includes('html') && filt !== null && typeof filt === 'string' ?
  992. filt.replace(/(<([^>]+)>)/ig, '') :
  993. filt
  994. })
  995. .addClass(that.classes.option)
  996. .addClass(that.classes.notItalic)
  997. // Have to add the text this way so that special html characters are not escaped - &amp; etc.
  998. .html(typeof text === 'string' ?
  999. text.replace(/(<([^>]+)>)/ig, '') :
  1000. text);
  1001. var val = opt.val();
  1002. // Check that this value has not already been added
  1003. if (added.indexOf(val) === -1) {
  1004. added.push(val);
  1005. options.push(opt);
  1006. if (preDefined !== null && Array.isArray(preDefined[0])) {
  1007. preDefined[0] = preDefined[0].sort().join(',');
  1008. }
  1009. // If this value was previously selected as indicated by preDefined, then select it again
  1010. if (preDefined !== null && opt.val() === preDefined[0]) {
  1011. opt.prop('selected', true);
  1012. el.removeClass(Criteria.classes.italic);
  1013. }
  1014. }
  1015. };
  1016. // If this is to add the individual values within the array we need to loop over the array
  1017. if (array) {
  1018. for (var i = 0; i < value.filter.length; i++) {
  1019. addOption(value.filter[i], value.text[i]);
  1020. }
  1021. }
  1022. // Otherwise the value that is in the cell is to be added
  1023. else {
  1024. addOption(value.filter, value.text);
  1025. }
  1026. }
  1027. options.sort(function (a, b) {
  1028. if (that.s.type === 'array' ||
  1029. that.s.type === 'string' ||
  1030. that.s.type === 'html') {
  1031. if (a.val() < b.val()) {
  1032. return -1;
  1033. }
  1034. else if (a.val() > b.val()) {
  1035. return 1;
  1036. }
  1037. else {
  1038. return 0;
  1039. }
  1040. }
  1041. else if (that.s.type === 'num' ||
  1042. that.s.type === 'html-num') {
  1043. if (+a.val().replace(/(<([^>]+)>)/ig, '') < +b.val().replace(/(<([^>]+)>)/ig, '')) {
  1044. return -1;
  1045. }
  1046. else if (+a.val().replace(/(<([^>]+)>)/ig, '') > +b.val().replace(/(<([^>]+)>)/ig, '')) {
  1047. return 1;
  1048. }
  1049. else {
  1050. return 0;
  1051. }
  1052. }
  1053. else if (that.s.type === 'num-fmt' || that.s.type === 'html-num-fmt') {
  1054. if (+a.val().replace(/[^0-9.]/g, '') < +b.val().replace(/[^0-9.]/g, '')) {
  1055. return -1;
  1056. }
  1057. else if (+a.val().replace(/[^0-9.]/g, '') > +b.val().replace(/[^0-9.]/g, '')) {
  1058. return 1;
  1059. }
  1060. else {
  1061. return 0;
  1062. }
  1063. }
  1064. });
  1065. for (var _a = 0, options_1 = options; _a < options_1.length; _a++) {
  1066. var opt = options_1[_a];
  1067. el.append(opt);
  1068. }
  1069. return el;
  1070. };
  1071. /**
  1072. * Default initialisation function for select array conditions
  1073. *
  1074. * This exists because there needs to be different select functionality for contains/without and equals/not
  1075. */
  1076. Criteria.initSelectArray = function (that, fn, preDefined) {
  1077. if (preDefined === void 0) { preDefined = null; }
  1078. return Criteria.initSelect(that, fn, preDefined, true);
  1079. };
  1080. /**
  1081. * Default initialisation function for input conditions
  1082. */
  1083. Criteria.initInput = function (that, fn, preDefined) {
  1084. if (preDefined === void 0) { preDefined = null; }
  1085. // Declare the input element
  1086. var searchDelay = that.s.dt.settings()[0].searchDelay;
  1087. var el = $$2('<input/>')
  1088. .addClass(Criteria.classes.value)
  1089. .addClass(Criteria.classes.input)
  1090. .on('input keypress', that._throttle(function (e) {
  1091. var code = e.keyCode || e.which;
  1092. if (!that.c.enterSearch &&
  1093. !(that.s.dt.settings()[0].oInit.search !== undefined &&
  1094. that.s.dt.settings()[0].oInit.search["return"]) ||
  1095. code === 13) {
  1096. return fn(that, this);
  1097. }
  1098. }, searchDelay === null ? 100 : searchDelay));
  1099. if (that.c.greyscale) {
  1100. el.addClass(Criteria.classes.greyscale);
  1101. }
  1102. // If there is a preDefined value then add it
  1103. if (preDefined !== null) {
  1104. el.val(preDefined[0]);
  1105. }
  1106. // This is add responsive functionality to the logic button without redrawing everything else
  1107. that.s.dt.one('draw', function () {
  1108. that.s.topGroup.trigger('dtsb-redrawLogic');
  1109. });
  1110. return el;
  1111. };
  1112. /**
  1113. * Default initialisation function for conditions requiring 2 inputs
  1114. */
  1115. Criteria.init2Input = function (that, fn, preDefined) {
  1116. if (preDefined === void 0) { preDefined = null; }
  1117. // Declare all of the necessary jQuery elements
  1118. var searchDelay = that.s.dt.settings()[0].searchDelay;
  1119. var els = [
  1120. $$2('<input/>')
  1121. .addClass(Criteria.classes.value)
  1122. .addClass(Criteria.classes.input)
  1123. .on('input keypress', that._throttle(function (e) {
  1124. var code = e.keyCode || e.which;
  1125. if (!that.c.enterSearch &&
  1126. !(that.s.dt.settings()[0].oInit.search !== undefined &&
  1127. that.s.dt.settings()[0].oInit.search["return"]) ||
  1128. code === 13) {
  1129. return fn(that, this);
  1130. }
  1131. }, searchDelay === null ? 100 : searchDelay)),
  1132. $$2('<span>')
  1133. .addClass(that.classes.joiner)
  1134. .text(that.s.dt.i18n('searchBuilder.valueJoiner', that.c.i18n.valueJoiner)),
  1135. $$2('<input/>')
  1136. .addClass(Criteria.classes.value)
  1137. .addClass(Criteria.classes.input)
  1138. .on('input keypress', that._throttle(function (e) {
  1139. var code = e.keyCode || e.which;
  1140. if (!that.c.enterSearch &&
  1141. !(that.s.dt.settings()[0].oInit.search !== undefined &&
  1142. that.s.dt.settings()[0].oInit.search["return"]) ||
  1143. code === 13) {
  1144. return fn(that, this);
  1145. }
  1146. }, searchDelay === null ? 100 : searchDelay))
  1147. ];
  1148. if (that.c.greyscale) {
  1149. els[0].addClass(Criteria.classes.greyscale);
  1150. els[2].addClass(Criteria.classes.greyscale);
  1151. }
  1152. // If there is a preDefined value then add it
  1153. if (preDefined !== null) {
  1154. els[0].val(preDefined[0]);
  1155. els[2].val(preDefined[1]);
  1156. }
  1157. // This is add responsive functionality to the logic button without redrawing everything else
  1158. that.s.dt.one('draw', function () {
  1159. that.s.topGroup.trigger('dtsb-redrawLogic');
  1160. });
  1161. return els;
  1162. };
  1163. /**
  1164. * Default initialisation function for date conditions
  1165. */
  1166. Criteria.initDate = function (that, fn, preDefined) {
  1167. if (preDefined === void 0) { preDefined = null; }
  1168. var searchDelay = that.s.dt.settings()[0].searchDelay;
  1169. // Declare date element using DataTables dateTime plugin
  1170. var el = $$2('<input/>')
  1171. .addClass(Criteria.classes.value)
  1172. .addClass(Criteria.classes.input)
  1173. .dtDateTime({
  1174. attachTo: 'input',
  1175. format: that.s.dateFormat ? that.s.dateFormat : undefined
  1176. })
  1177. .on('change', that._throttle(function () {
  1178. return fn(that, this);
  1179. }, searchDelay === null ? 100 : searchDelay))
  1180. .on('input keypress', that.c.enterSearch ||
  1181. that.s.dt.settings()[0].oInit.search !== undefined &&
  1182. that.s.dt.settings()[0].oInit.search["return"] ?
  1183. function (e) {
  1184. that._throttle(function () {
  1185. var code = e.keyCode || e.which;
  1186. if (code === 13) {
  1187. return fn(that, this);
  1188. }
  1189. }, searchDelay === null ? 100 : searchDelay);
  1190. } :
  1191. that._throttle(function () {
  1192. return fn(that, this);
  1193. }, searchDelay === null ? 100 : searchDelay));
  1194. if (that.c.greyscale) {
  1195. el.addClass(Criteria.classes.greyscale);
  1196. }
  1197. // If there is a preDefined value then add it
  1198. if (preDefined !== null) {
  1199. el.val(preDefined[0]);
  1200. }
  1201. // This is add responsive functionality to the logic button without redrawing everything else
  1202. that.s.dt.one('draw', function () {
  1203. that.s.topGroup.trigger('dtsb-redrawLogic');
  1204. });
  1205. return el;
  1206. };
  1207. Criteria.initNoValue = function (that) {
  1208. // This is add responsive functionality to the logic button without redrawing everything else
  1209. that.s.dt.one('draw', function () {
  1210. that.s.topGroup.trigger('dtsb-redrawLogic');
  1211. });
  1212. };
  1213. Criteria.init2Date = function (that, fn, preDefined) {
  1214. var _this = this;
  1215. if (preDefined === void 0) { preDefined = null; }
  1216. var searchDelay = that.s.dt.settings()[0].searchDelay;
  1217. // Declare all of the date elements that are required using DataTables dateTime plugin
  1218. var els = [
  1219. $$2('<input/>')
  1220. .addClass(Criteria.classes.value)
  1221. .addClass(Criteria.classes.input)
  1222. .dtDateTime({
  1223. attachTo: 'input',
  1224. format: that.s.dateFormat ? that.s.dateFormat : undefined
  1225. })
  1226. .on('change', searchDelay !== null ?
  1227. that.s.dt.settings()[0].oApi._fnThrottle(function () {
  1228. return fn(that, this);
  1229. }, searchDelay) :
  1230. function () {
  1231. fn(that, _this);
  1232. })
  1233. .on('input keypress', !that.c.enterSearch &&
  1234. !(that.s.dt.settings()[0].oInit.search !== undefined &&
  1235. that.s.dt.settings()[0].oInit.search["return"]) &&
  1236. searchDelay !== null ?
  1237. that.s.dt.settings()[0].oApi._fnThrottle(function () {
  1238. return fn(that, this);
  1239. }, searchDelay) :
  1240. that.c.enterSearch ||
  1241. that.s.dt.settings()[0].oInit.search !== undefined &&
  1242. that.s.dt.settings()[0].oInit.search["return"] ?
  1243. function (e) {
  1244. var code = e.keyCode || e.which;
  1245. if (code === 13) {
  1246. fn(that, _this);
  1247. }
  1248. } :
  1249. function () {
  1250. fn(that, _this);
  1251. }),
  1252. $$2('<span>')
  1253. .addClass(that.classes.joiner)
  1254. .text(that.s.dt.i18n('searchBuilder.valueJoiner', that.c.i18n.valueJoiner)),
  1255. $$2('<input/>')
  1256. .addClass(Criteria.classes.value)
  1257. .addClass(Criteria.classes.input)
  1258. .dtDateTime({
  1259. attachTo: 'input',
  1260. format: that.s.dateFormat ? that.s.dateFormat : undefined
  1261. })
  1262. .on('change', searchDelay !== null ?
  1263. that.s.dt.settings()[0].oApi._fnThrottle(function () {
  1264. return fn(that, this);
  1265. }, searchDelay) :
  1266. function () {
  1267. fn(that, _this);
  1268. })
  1269. .on('input keypress', !that.c.enterSearch &&
  1270. !(that.s.dt.settings()[0].oInit.search !== undefined &&
  1271. that.s.dt.settings()[0].oInit.search["return"]) &&
  1272. searchDelay !== null ?
  1273. that.s.dt.settings()[0].oApi._fnThrottle(function () {
  1274. return fn(that, this);
  1275. }, searchDelay) :
  1276. that.c.enterSearch ||
  1277. that.s.dt.settings()[0].oInit.search !== undefined &&
  1278. that.s.dt.settings()[0].oInit.search["return"] ?
  1279. function (e) {
  1280. var code = e.keyCode || e.which;
  1281. if (code === 13) {
  1282. fn(that, _this);
  1283. }
  1284. } :
  1285. function () {
  1286. fn(that, _this);
  1287. })
  1288. ];
  1289. if (that.c.greyscale) {
  1290. els[0].addClass(Criteria.classes.greyscale);
  1291. els[2].addClass(Criteria.classes.greyscale);
  1292. }
  1293. // If there are and preDefined values then add them
  1294. if (preDefined !== null && preDefined.length > 0) {
  1295. els[0].val(preDefined[0]);
  1296. els[2].val(preDefined[1]);
  1297. }
  1298. // This is add responsive functionality to the logic button without redrawing everything else
  1299. that.s.dt.one('draw', function () {
  1300. that.s.topGroup.trigger('dtsb-redrawLogic');
  1301. });
  1302. return els;
  1303. };
  1304. /**
  1305. * Default function for select elements to validate condition
  1306. */
  1307. Criteria.isInputValidSelect = function (el) {
  1308. var allFilled = true;
  1309. // Check each element to make sure that the selections are valid
  1310. for (var _i = 0, el_1 = el; _i < el_1.length; _i++) {
  1311. var element = el_1[_i];
  1312. if (element.children('option:selected').length ===
  1313. element.children('option').length -
  1314. element.children('option.' + Criteria.classes.notItalic).length &&
  1315. element.children('option:selected').length === 1 &&
  1316. element.children('option:selected')[0] === element.children('option:hidden')[0]) {
  1317. allFilled = false;
  1318. }
  1319. }
  1320. return allFilled;
  1321. };
  1322. /**
  1323. * Default function for input and date elements to validate condition
  1324. */
  1325. Criteria.isInputValidInput = function (el) {
  1326. var allFilled = true;
  1327. // Check each element to make sure that the inputs are valid
  1328. for (var _i = 0, el_2 = el; _i < el_2.length; _i++) {
  1329. var element = el_2[_i];
  1330. if (element.is('input') && element.val().length === 0) {
  1331. allFilled = false;
  1332. }
  1333. }
  1334. return allFilled;
  1335. };
  1336. /**
  1337. * Default function for getting select conditions
  1338. */
  1339. Criteria.inputValueSelect = function (el) {
  1340. var values = [];
  1341. // Go through the select elements and push each selected option to the return array
  1342. for (var _i = 0, el_3 = el; _i < el_3.length; _i++) {
  1343. var element = el_3[_i];
  1344. if (element.is('select')) {
  1345. var val = element.children('option:selected').val();
  1346. // If the type of the option is an array we need to split it up and sort it
  1347. values.push(element.children('option:selected').attr('type') === 'Array' ?
  1348. val.split(',').sort() :
  1349. val);
  1350. }
  1351. }
  1352. return values;
  1353. };
  1354. /**
  1355. * Default function for getting input conditions
  1356. */
  1357. Criteria.inputValueInput = function (el) {
  1358. var values = [];
  1359. // Go through the input elements and push each value to the return array
  1360. for (var _i = 0, el_4 = el; _i < el_4.length; _i++) {
  1361. var element = el_4[_i];
  1362. if (element.is('input')) {
  1363. values.push(element.val());
  1364. }
  1365. }
  1366. return values;
  1367. };
  1368. /**
  1369. * Function that is run on each element as a call back when a search should be triggered
  1370. */
  1371. Criteria.updateListener = function (that, el) {
  1372. // When the value is changed the criteria is now complete so can be included in searches
  1373. // Get the condition from the map based on the key that has been selected for the condition
  1374. var condition = that.s.conditions[that.s.condition];
  1375. that.s.filled = condition.isInputValid(that.dom.value, that);
  1376. that.s.value = condition.inputValue(that.dom.value, that);
  1377. if (!that.s.filled) {
  1378. that.s.dt.draw();
  1379. return;
  1380. }
  1381. if (!Array.isArray(that.s.value)) {
  1382. that.s.value = [that.s.value];
  1383. }
  1384. for (var i = 0; i < that.s.value.length; i++) {
  1385. // If the value is an array we need to sort it
  1386. if (Array.isArray(that.s.value[i])) {
  1387. that.s.value[i].sort();
  1388. }
  1389. // Otherwise replace the decimal place character for i18n
  1390. else if (that.s.type.includes('num') &&
  1391. (that.s.dt.settings()[0].oLanguage.sDecimal !== '' ||
  1392. that.s.dt.settings()[0].oLanguage.sThousands !== '')) {
  1393. var splitRD = [that.s.value[i].toString()];
  1394. if (that.s.dt.settings()[0].oLanguage.sDecimal !== '') {
  1395. splitRD = that.s.value[i].split(that.s.dt.settings()[0].oLanguage.sDecimal);
  1396. }
  1397. if (that.s.dt.settings()[0].oLanguage.sThousands !== '') {
  1398. for (var j = 0; j < splitRD.length; j++) {
  1399. splitRD[j] = splitRD[j].replace(that.s.dt.settings()[0].oLanguage.sThousands, ',');
  1400. }
  1401. }
  1402. that.s.value[i] = splitRD.join('.');
  1403. }
  1404. }
  1405. // Take note of the cursor position so that we can refocus there later
  1406. var idx = null;
  1407. var cursorPos = null;
  1408. for (var i = 0; i < that.dom.value.length; i++) {
  1409. if (el === that.dom.value[i][0]) {
  1410. idx = i;
  1411. if (el.selectionStart !== undefined) {
  1412. cursorPos = el.selectionStart;
  1413. }
  1414. }
  1415. }
  1416. // Trigger a search
  1417. that.s.dt.draw();
  1418. // Refocus the element and set the correct cursor position
  1419. if (idx !== null) {
  1420. that.dom.value[idx].removeClass(that.classes.italic);
  1421. that.dom.value[idx].focus();
  1422. if (cursorPos !== null) {
  1423. that.dom.value[idx][0].setSelectionRange(cursorPos, cursorPos);
  1424. }
  1425. }
  1426. };
  1427. // The order of the conditions will make eslint sad :(
  1428. // Has to be in this order so that they are displayed correctly in select elements
  1429. // Also have to disable member ordering for this as the private methods used are not yet declared otherwise
  1430. // eslint-disable-next-line @typescript-eslint/member-ordering
  1431. Criteria.dateConditions = {
  1432. '=': {
  1433. conditionName: function (dt, i18n) {
  1434. return dt.i18n('searchBuilder.conditions.date.equals', i18n.conditions.date.equals);
  1435. },
  1436. init: Criteria.initDate,
  1437. inputValue: Criteria.inputValueInput,
  1438. isInputValid: Criteria.isInputValidInput,
  1439. search: function (value, comparison) {
  1440. value = value.replace(/(\/|-|,)/g, '-');
  1441. return value === comparison[0];
  1442. }
  1443. },
  1444. // eslint-disable-next-line sort-keys
  1445. '!=': {
  1446. conditionName: function (dt, i18n) {
  1447. return dt.i18n('searchBuilder.conditions.date.not', i18n.conditions.date.not);
  1448. },
  1449. init: Criteria.initDate,
  1450. inputValue: Criteria.inputValueInput,
  1451. isInputValid: Criteria.isInputValidInput,
  1452. search: function (value, comparison) {
  1453. value = value.replace(/(\/|-|,)/g, '-');
  1454. return value !== comparison[0];
  1455. }
  1456. },
  1457. '<': {
  1458. conditionName: function (dt, i18n) {
  1459. return dt.i18n('searchBuilder.conditions.date.before', i18n.conditions.date.before);
  1460. },
  1461. init: Criteria.initDate,
  1462. inputValue: Criteria.inputValueInput,
  1463. isInputValid: Criteria.isInputValidInput,
  1464. search: function (value, comparison) {
  1465. value = value.replace(/(\/|-|,)/g, '-');
  1466. return value < comparison[0];
  1467. }
  1468. },
  1469. '>': {
  1470. conditionName: function (dt, i18n) {
  1471. return dt.i18n('searchBuilder.conditions.date.after', i18n.conditions.date.after);
  1472. },
  1473. init: Criteria.initDate,
  1474. inputValue: Criteria.inputValueInput,
  1475. isInputValid: Criteria.isInputValidInput,
  1476. search: function (value, comparison) {
  1477. value = value.replace(/(\/|-|,)/g, '-');
  1478. return value > comparison[0];
  1479. }
  1480. },
  1481. 'between': {
  1482. conditionName: function (dt, i18n) {
  1483. return dt.i18n('searchBuilder.conditions.date.between', i18n.conditions.date.between);
  1484. },
  1485. init: Criteria.init2Date,
  1486. inputValue: Criteria.inputValueInput,
  1487. isInputValid: Criteria.isInputValidInput,
  1488. search: function (value, comparison) {
  1489. value = value.replace(/(\/|-|,)/g, '-');
  1490. if (comparison[0] < comparison[1]) {
  1491. return comparison[0] <= value && value <= comparison[1];
  1492. }
  1493. else {
  1494. return comparison[1] <= value && value <= comparison[0];
  1495. }
  1496. }
  1497. },
  1498. // eslint-disable-next-line sort-keys
  1499. '!between': {
  1500. conditionName: function (dt, i18n) {
  1501. return dt.i18n('searchBuilder.conditions.date.notBetween', i18n.conditions.date.notBetween);
  1502. },
  1503. init: Criteria.init2Date,
  1504. inputValue: Criteria.inputValueInput,
  1505. isInputValid: Criteria.isInputValidInput,
  1506. search: function (value, comparison) {
  1507. value = value.replace(/(\/|-|,)/g, '-');
  1508. if (comparison[0] < comparison[1]) {
  1509. return !(comparison[0] <= value && value <= comparison[1]);
  1510. }
  1511. else {
  1512. return !(comparison[1] <= value && value <= comparison[0]);
  1513. }
  1514. }
  1515. },
  1516. 'null': {
  1517. conditionName: function (dt, i18n) {
  1518. return dt.i18n('searchBuilder.conditions.date.empty', i18n.conditions.date.empty);
  1519. },
  1520. init: Criteria.initNoValue,
  1521. inputValue: function () {
  1522. return;
  1523. },
  1524. isInputValid: function () {
  1525. return true;
  1526. },
  1527. search: function (value) {
  1528. return value === null || value === undefined || value.length === 0;
  1529. }
  1530. },
  1531. // eslint-disable-next-line sort-keys
  1532. '!null': {
  1533. conditionName: function (dt, i18n) {
  1534. return dt.i18n('searchBuilder.conditions.date.notEmpty', i18n.conditions.date.notEmpty);
  1535. },
  1536. init: Criteria.initNoValue,
  1537. inputValue: function () {
  1538. return;
  1539. },
  1540. isInputValid: function () {
  1541. return true;
  1542. },
  1543. search: function (value) {
  1544. return !(value === null || value === undefined || value.length === 0);
  1545. }
  1546. }
  1547. };
  1548. // The order of the conditions will make eslint sad :(
  1549. // Has to be in this order so that they are displayed correctly in select elements
  1550. // Also have to disable member ordering for this as the private methods used are not yet declared otherwise
  1551. // eslint-disable-next-line @typescript-eslint/member-ordering
  1552. Criteria.momentDateConditions = {
  1553. '=': {
  1554. conditionName: function (dt, i18n) {
  1555. return dt.i18n('searchBuilder.conditions.date.equals', i18n.conditions.date.equals);
  1556. },
  1557. init: Criteria.initDate,
  1558. inputValue: Criteria.inputValueInput,
  1559. isInputValid: Criteria.isInputValidInput,
  1560. search: function (value, comparison, that) {
  1561. return moment(value, that.s.dateFormat).valueOf() ===
  1562. moment(comparison[0], that.s.dateFormat).valueOf();
  1563. }
  1564. },
  1565. // eslint-disable-next-line sort-keys
  1566. '!=': {
  1567. conditionName: function (dt, i18n) {
  1568. return dt.i18n('searchBuilder.conditions.date.not', i18n.conditions.date.not);
  1569. },
  1570. init: Criteria.initDate,
  1571. inputValue: Criteria.inputValueInput,
  1572. isInputValid: Criteria.isInputValidInput,
  1573. search: function (value, comparison, that) {
  1574. return moment(value, that.s.dateFormat).valueOf() !==
  1575. moment(comparison[0], that.s.dateFormat).valueOf();
  1576. }
  1577. },
  1578. '<': {
  1579. conditionName: function (dt, i18n) {
  1580. return dt.i18n('searchBuilder.conditions.date.before', i18n.conditions.date.before);
  1581. },
  1582. init: Criteria.initDate,
  1583. inputValue: Criteria.inputValueInput,
  1584. isInputValid: Criteria.isInputValidInput,
  1585. search: function (value, comparison, that) {
  1586. return moment(value, that.s.dateFormat).valueOf() < moment(comparison[0], that.s.dateFormat).valueOf();
  1587. }
  1588. },
  1589. '>': {
  1590. conditionName: function (dt, i18n) {
  1591. return dt.i18n('searchBuilder.conditions.date.after', i18n.conditions.date.after);
  1592. },
  1593. init: Criteria.initDate,
  1594. inputValue: Criteria.inputValueInput,
  1595. isInputValid: Criteria.isInputValidInput,
  1596. search: function (value, comparison, that) {
  1597. return moment(value, that.s.dateFormat).valueOf() > moment(comparison[0], that.s.dateFormat).valueOf();
  1598. }
  1599. },
  1600. 'between': {
  1601. conditionName: function (dt, i18n) {
  1602. return dt.i18n('searchBuilder.conditions.date.between', i18n.conditions.date.between);
  1603. },
  1604. init: Criteria.init2Date,
  1605. inputValue: Criteria.inputValueInput,
  1606. isInputValid: Criteria.isInputValidInput,
  1607. search: function (value, comparison, that) {
  1608. var val = moment(value, that.s.dateFormat).valueOf();
  1609. var comp0 = moment(comparison[0], that.s.dateFormat).valueOf();
  1610. var comp1 = moment(comparison[1], that.s.dateFormat).valueOf();
  1611. if (comp0 < comp1) {
  1612. return comp0 <= val && val <= comp1;
  1613. }
  1614. else {
  1615. return comp1 <= val && val <= comp0;
  1616. }
  1617. }
  1618. },
  1619. // eslint-disable-next-line sort-keys
  1620. '!between': {
  1621. conditionName: function (dt, i18n) {
  1622. return dt.i18n('searchBuilder.conditions.date.notBetween', i18n.conditions.date.notBetween);
  1623. },
  1624. init: Criteria.init2Date,
  1625. inputValue: Criteria.inputValueInput,
  1626. isInputValid: Criteria.isInputValidInput,
  1627. search: function (value, comparison, that) {
  1628. var val = moment(value, that.s.dateFormat).valueOf();
  1629. var comp0 = moment(comparison[0], that.s.dateFormat).valueOf();
  1630. var comp1 = moment(comparison[1], that.s.dateFormat).valueOf();
  1631. if (comp0 < comp1) {
  1632. return !(+comp0 <= +val && +val <= +comp1);
  1633. }
  1634. else {
  1635. return !(+comp1 <= +val && +val <= +comp0);
  1636. }
  1637. }
  1638. },
  1639. 'null': {
  1640. conditionName: function (dt, i18n) {
  1641. return dt.i18n('searchBuilder.conditions.date.empty', i18n.conditions.date.empty);
  1642. },
  1643. init: Criteria.initNoValue,
  1644. inputValue: function () {
  1645. return;
  1646. },
  1647. isInputValid: function () {
  1648. return true;
  1649. },
  1650. search: function (value) {
  1651. return value === null || value === undefined || value.length === 0;
  1652. }
  1653. },
  1654. // eslint-disable-next-line sort-keys
  1655. '!null': {
  1656. conditionName: function (dt, i18n) {
  1657. return dt.i18n('searchBuilder.conditions.date.notEmpty', i18n.conditions.date.notEmpty);
  1658. },
  1659. init: Criteria.initNoValue,
  1660. inputValue: function () {
  1661. return;
  1662. },
  1663. isInputValid: function () {
  1664. return true;
  1665. },
  1666. search: function (value) {
  1667. return !(value === null || value === undefined || value.length === 0);
  1668. }
  1669. }
  1670. };
  1671. // The order of the conditions will make eslint sad :(
  1672. // Has to be in this order so that they are displayed correctly in select elements
  1673. // Also have to disable member ordering for this as the private methods used are not yet declared otherwise
  1674. // eslint-disable-next-line @typescript-eslint/member-ordering
  1675. Criteria.luxonDateConditions = {
  1676. '=': {
  1677. conditionName: function (dt, i18n) {
  1678. return dt.i18n('searchBuilder.conditions.date.equals', i18n.conditions.date.equals);
  1679. },
  1680. init: Criteria.initDate,
  1681. inputValue: Criteria.inputValueInput,
  1682. isInputValid: Criteria.isInputValidInput,
  1683. search: function (value, comparison, that) {
  1684. return luxon.DateTime.fromFormat(value, that.s.dateFormat).ts
  1685. === luxon.DateTime.fromFormat(comparison[0], that.s.dateFormat).ts;
  1686. }
  1687. },
  1688. // eslint-disable-next-line sort-keys
  1689. '!=': {
  1690. conditionName: function (dt, i18n) {
  1691. return dt.i18n('searchBuilder.conditions.date.not', i18n.conditions.date.not);
  1692. },
  1693. init: Criteria.initDate,
  1694. inputValue: Criteria.inputValueInput,
  1695. isInputValid: Criteria.isInputValidInput,
  1696. search: function (value, comparison, that) {
  1697. return luxon.DateTime.fromFormat(value, that.s.dateFormat).ts
  1698. !== luxon.DateTime.fromFormat(comparison[0], that.s.dateFormat).ts;
  1699. }
  1700. },
  1701. '<': {
  1702. conditionName: function (dt, i18n) {
  1703. return dt.i18n('searchBuilder.conditions.date.before', i18n.conditions.date.before);
  1704. },
  1705. init: Criteria.initDate,
  1706. inputValue: Criteria.inputValueInput,
  1707. isInputValid: Criteria.isInputValidInput,
  1708. search: function (value, comparison, that) {
  1709. return luxon.DateTime.fromFormat(value, that.s.dateFormat).ts
  1710. < luxon.DateTime.fromFormat(comparison[0], that.s.dateFormat).ts;
  1711. }
  1712. },
  1713. '>': {
  1714. conditionName: function (dt, i18n) {
  1715. return dt.i18n('searchBuilder.conditions.date.after', i18n.conditions.date.after);
  1716. },
  1717. init: Criteria.initDate,
  1718. inputValue: Criteria.inputValueInput,
  1719. isInputValid: Criteria.isInputValidInput,
  1720. search: function (value, comparison, that) {
  1721. return luxon.DateTime.fromFormat(value, that.s.dateFormat).ts
  1722. > luxon.DateTime.fromFormat(comparison[0], that.s.dateFormat).ts;
  1723. }
  1724. },
  1725. 'between': {
  1726. conditionName: function (dt, i18n) {
  1727. return dt.i18n('searchBuilder.conditions.date.between', i18n.conditions.date.between);
  1728. },
  1729. init: Criteria.init2Date,
  1730. inputValue: Criteria.inputValueInput,
  1731. isInputValid: Criteria.isInputValidInput,
  1732. search: function (value, comparison, that) {
  1733. var val = luxon.DateTime.fromFormat(value, that.s.dateFormat).ts;
  1734. var comp0 = luxon.DateTime.fromFormat(comparison[0], that.s.dateFormat).ts;
  1735. var comp1 = luxon.DateTime.fromFormat(comparison[1], that.s.dateFormat).ts;
  1736. if (comp0 < comp1) {
  1737. return comp0 <= val && val <= comp1;
  1738. }
  1739. else {
  1740. return comp1 <= val && val <= comp0;
  1741. }
  1742. }
  1743. },
  1744. // eslint-disable-next-line sort-keys
  1745. '!between': {
  1746. conditionName: function (dt, i18n) {
  1747. return dt.i18n('searchBuilder.conditions.date.notBetween', i18n.conditions.date.notBetween);
  1748. },
  1749. init: Criteria.init2Date,
  1750. inputValue: Criteria.inputValueInput,
  1751. isInputValid: Criteria.isInputValidInput,
  1752. search: function (value, comparison, that) {
  1753. var val = luxon.DateTime.fromFormat(value, that.s.dateFormat).ts;
  1754. var comp0 = luxon.DateTime.fromFormat(comparison[0], that.s.dateFormat).ts;
  1755. var comp1 = luxon.DateTime.fromFormat(comparison[1], that.s.dateFormat).ts;
  1756. if (comp0 < comp1) {
  1757. return !(+comp0 <= +val && +val <= +comp1);
  1758. }
  1759. else {
  1760. return !(+comp1 <= +val && +val <= +comp0);
  1761. }
  1762. }
  1763. },
  1764. 'null': {
  1765. conditionName: function (dt, i18n) {
  1766. return dt.i18n('searchBuilder.conditions.date.empty', i18n.conditions.date.empty);
  1767. },
  1768. init: Criteria.initNoValue,
  1769. inputValue: function () {
  1770. return;
  1771. },
  1772. isInputValid: function () {
  1773. return true;
  1774. },
  1775. search: function (value) {
  1776. return value === null || value === undefined || value.length === 0;
  1777. }
  1778. },
  1779. // eslint-disable-next-line sort-keys
  1780. '!null': {
  1781. conditionName: function (dt, i18n) {
  1782. return dt.i18n('searchBuilder.conditions.date.notEmpty', i18n.conditions.date.notEmpty);
  1783. },
  1784. init: Criteria.initNoValue,
  1785. inputValue: function () {
  1786. return;
  1787. },
  1788. isInputValid: function () {
  1789. return true;
  1790. },
  1791. search: function (value) {
  1792. return !(value === null || value === undefined || value.length === 0);
  1793. }
  1794. }
  1795. };
  1796. // The order of the conditions will make eslint sad :(
  1797. // Has to be in this order so that they are displayed correctly in select elements
  1798. // Also have to disable member ordering for this as the private methods used are not yet declared otherwise
  1799. // eslint-disable-next-line @typescript-eslint/member-ordering
  1800. Criteria.numConditions = {
  1801. '=': {
  1802. conditionName: function (dt, i18n) {
  1803. return dt.i18n('searchBuilder.conditions.number.equals', i18n.conditions.number.equals);
  1804. },
  1805. init: Criteria.initSelect,
  1806. inputValue: Criteria.inputValueSelect,
  1807. isInputValid: Criteria.isInputValidSelect,
  1808. search: function (value, comparison) {
  1809. return +value === +comparison[0];
  1810. }
  1811. },
  1812. // eslint-disable-next-line sort-keys
  1813. '!=': {
  1814. conditionName: function (dt, i18n) {
  1815. return dt.i18n('searchBuilder.conditions.number.not', i18n.conditions.number.not);
  1816. },
  1817. init: Criteria.initSelect,
  1818. inputValue: Criteria.inputValueSelect,
  1819. isInputValid: Criteria.isInputValidSelect,
  1820. search: function (value, comparison) {
  1821. return +value !== +comparison[0];
  1822. }
  1823. },
  1824. '<': {
  1825. conditionName: function (dt, i18n) {
  1826. return dt.i18n('searchBuilder.conditions.number.lt', i18n.conditions.number.lt);
  1827. },
  1828. init: Criteria.initInput,
  1829. inputValue: Criteria.inputValueInput,
  1830. isInputValid: Criteria.isInputValidInput,
  1831. search: function (value, comparison) {
  1832. return +value < +comparison[0];
  1833. }
  1834. },
  1835. '<=': {
  1836. conditionName: function (dt, i18n) {
  1837. return dt.i18n('searchBuilder.conditions.number.lte', i18n.conditions.number.lte);
  1838. },
  1839. init: Criteria.initInput,
  1840. inputValue: Criteria.inputValueInput,
  1841. isInputValid: Criteria.isInputValidInput,
  1842. search: function (value, comparison) {
  1843. return +value <= +comparison[0];
  1844. }
  1845. },
  1846. '>=': {
  1847. conditionName: function (dt, i18n) {
  1848. return dt.i18n('searchBuilder.conditions.number.gte', i18n.conditions.number.gte);
  1849. },
  1850. init: Criteria.initInput,
  1851. inputValue: Criteria.inputValueInput,
  1852. isInputValid: Criteria.isInputValidInput,
  1853. search: function (value, comparison) {
  1854. return +value >= +comparison[0];
  1855. }
  1856. },
  1857. // eslint-disable-next-line sort-keys
  1858. '>': {
  1859. conditionName: function (dt, i18n) {
  1860. return dt.i18n('searchBuilder.conditions.number.gt', i18n.conditions.number.gt);
  1861. },
  1862. init: Criteria.initInput,
  1863. inputValue: Criteria.inputValueInput,
  1864. isInputValid: Criteria.isInputValidInput,
  1865. search: function (value, comparison) {
  1866. return +value > +comparison[0];
  1867. }
  1868. },
  1869. 'between': {
  1870. conditionName: function (dt, i18n) {
  1871. return dt.i18n('searchBuilder.conditions.number.between', i18n.conditions.number.between);
  1872. },
  1873. init: Criteria.init2Input,
  1874. inputValue: Criteria.inputValueInput,
  1875. isInputValid: Criteria.isInputValidInput,
  1876. search: function (value, comparison) {
  1877. if (+comparison[0] < +comparison[1]) {
  1878. return +comparison[0] <= +value && +value <= +comparison[1];
  1879. }
  1880. else {
  1881. return +comparison[1] <= +value && +value <= +comparison[0];
  1882. }
  1883. }
  1884. },
  1885. // eslint-disable-next-line sort-keys
  1886. '!between': {
  1887. conditionName: function (dt, i18n) {
  1888. return dt.i18n('searchBuilder.conditions.number.notBetween', i18n.conditions.number.notBetween);
  1889. },
  1890. init: Criteria.init2Input,
  1891. inputValue: Criteria.inputValueInput,
  1892. isInputValid: Criteria.isInputValidInput,
  1893. search: function (value, comparison) {
  1894. if (+comparison[0] < +comparison[1]) {
  1895. return !(+comparison[0] <= +value && +value <= +comparison[1]);
  1896. }
  1897. else {
  1898. return !(+comparison[1] <= +value && +value <= +comparison[0]);
  1899. }
  1900. }
  1901. },
  1902. 'null': {
  1903. conditionName: function (dt, i18n) {
  1904. return dt.i18n('searchBuilder.conditions.number.empty', i18n.conditions.number.empty);
  1905. },
  1906. init: Criteria.initNoValue,
  1907. inputValue: function () {
  1908. return;
  1909. },
  1910. isInputValid: function () {
  1911. return true;
  1912. },
  1913. search: function (value) {
  1914. return value === null || value === undefined || value.length === 0;
  1915. }
  1916. },
  1917. // eslint-disable-next-line sort-keys
  1918. '!null': {
  1919. conditionName: function (dt, i18n) {
  1920. return dt.i18n('searchBuilder.conditions.number.notEmpty', i18n.conditions.number.notEmpty);
  1921. },
  1922. init: Criteria.initNoValue,
  1923. inputValue: function () {
  1924. return;
  1925. },
  1926. isInputValid: function () {
  1927. return true;
  1928. },
  1929. search: function (value) {
  1930. return !(value === null || value === undefined || value.length === 0);
  1931. }
  1932. }
  1933. };
  1934. // The order of the conditions will make eslint sad :(
  1935. // Has to be in this order so that they are displayed correctly in select elements
  1936. // Also have to disable member ordering for this as the private methods used are not yet declared otherwise
  1937. // eslint-disable-next-line @typescript-eslint/member-ordering
  1938. Criteria.numFmtConditions = {
  1939. '=': {
  1940. conditionName: function (dt, i18n) {
  1941. return dt.i18n('searchBuilder.conditions.number.equals', i18n.conditions.number.equals);
  1942. },
  1943. init: Criteria.initSelect,
  1944. inputValue: Criteria.inputValueSelect,
  1945. isInputValid: Criteria.isInputValidSelect,
  1946. search: function (value, comparison) {
  1947. var val = value.indexOf('-') === 0 ?
  1948. '-' + value.replace(/[^0-9.]/g, '') :
  1949. value.replace(/[^0-9.]/g, '');
  1950. var comp = comparison[0].indexOf('-') === 0 ?
  1951. '-' + comparison[0].replace(/[^0-9.]/g, '') :
  1952. comparison[0].replace(/[^0-9.]/g, '');
  1953. return +val === +comp;
  1954. }
  1955. },
  1956. // eslint-disable-next-line sort-keys
  1957. '!=': {
  1958. conditionName: function (dt, i18n) {
  1959. return dt.i18n('searchBuilder.conditions.number.not', i18n.conditions.number.not);
  1960. },
  1961. init: Criteria.initSelect,
  1962. inputValue: Criteria.inputValueSelect,
  1963. isInputValid: Criteria.isInputValidSelect,
  1964. search: function (value, comparison) {
  1965. var val = value.indexOf('-') === 0 ?
  1966. '-' + value.replace(/[^0-9.]/g, '') :
  1967. value.replace(/[^0-9.]/g, '');
  1968. var comp = comparison[0].indexOf('-') === 0 ?
  1969. '-' + comparison[0].replace(/[^0-9.]/g, '') :
  1970. comparison[0].replace(/[^0-9.]/g, '');
  1971. return +val !== +comp;
  1972. }
  1973. },
  1974. '<': {
  1975. conditionName: function (dt, i18n) {
  1976. return dt.i18n('searchBuilder.conditions.number.lt', i18n.conditions.number.lt);
  1977. },
  1978. init: Criteria.initInput,
  1979. inputValue: Criteria.inputValueInput,
  1980. isInputValid: Criteria.isInputValidInput,
  1981. search: function (value, comparison) {
  1982. var val = value.indexOf('-') === 0 ?
  1983. '-' + value.replace(/[^0-9.]/g, '') :
  1984. value.replace(/[^0-9.]/g, '');
  1985. var comp = comparison[0].indexOf('-') === 0 ?
  1986. '-' + comparison[0].replace(/[^0-9.]/g, '') :
  1987. comparison[0].replace(/[^0-9.]/g, '');
  1988. return +val < +comp;
  1989. }
  1990. },
  1991. '<=': {
  1992. conditionName: function (dt, i18n) {
  1993. return dt.i18n('searchBuilder.conditions.number.lte', i18n.conditions.number.lte);
  1994. },
  1995. init: Criteria.initInput,
  1996. inputValue: Criteria.inputValueInput,
  1997. isInputValid: Criteria.isInputValidInput,
  1998. search: function (value, comparison) {
  1999. var val = value.indexOf('-') === 0 ?
  2000. '-' + value.replace(/[^0-9.]/g, '') :
  2001. value.replace(/[^0-9.]/g, '');
  2002. var comp = comparison[0].indexOf('-') === 0 ?
  2003. '-' + comparison[0].replace(/[^0-9.]/g, '') :
  2004. comparison[0].replace(/[^0-9.]/g, '');
  2005. return +val <= +comp;
  2006. }
  2007. },
  2008. '>=': {
  2009. conditionName: function (dt, i18n) {
  2010. return dt.i18n('searchBuilder.conditions.number.gte', i18n.conditions.number.gte);
  2011. },
  2012. init: Criteria.initInput,
  2013. inputValue: Criteria.inputValueInput,
  2014. isInputValid: Criteria.isInputValidInput,
  2015. search: function (value, comparison) {
  2016. var val = value.indexOf('-') === 0 ?
  2017. '-' + value.replace(/[^0-9.]/g, '') :
  2018. value.replace(/[^0-9.]/g, '');
  2019. var comp = comparison[0].indexOf('-') === 0 ?
  2020. '-' + comparison[0].replace(/[^0-9.]/g, '') :
  2021. comparison[0].replace(/[^0-9.]/g, '');
  2022. return +val >= +comp;
  2023. }
  2024. },
  2025. // eslint-disable-next-line sort-keys
  2026. '>': {
  2027. conditionName: function (dt, i18n) {
  2028. return dt.i18n('searchBuilder.conditions.number.gt', i18n.conditions.number.gt);
  2029. },
  2030. init: Criteria.initInput,
  2031. inputValue: Criteria.inputValueInput,
  2032. isInputValid: Criteria.isInputValidInput,
  2033. search: function (value, comparison) {
  2034. var val = value.indexOf('-') === 0 ?
  2035. '-' + value.replace(/[^0-9.]/g, '') :
  2036. value.replace(/[^0-9.]/g, '');
  2037. var comp = comparison[0].indexOf('-') === 0 ?
  2038. '-' + comparison[0].replace(/[^0-9.]/g, '') :
  2039. comparison[0].replace(/[^0-9.]/g, '');
  2040. return +val > +comp;
  2041. }
  2042. },
  2043. 'between': {
  2044. conditionName: function (dt, i18n) {
  2045. return dt.i18n('searchBuilder.conditions.number.between', i18n.conditions.number.between);
  2046. },
  2047. init: Criteria.init2Input,
  2048. inputValue: Criteria.inputValueInput,
  2049. isInputValid: Criteria.isInputValidInput,
  2050. search: function (value, comparison) {
  2051. var val = value.indexOf('-') === 0 ?
  2052. '-' + value.replace(/[^0-9.]/g, '') :
  2053. value.replace(/[^0-9.]/g, '');
  2054. var comp0 = comparison[0].indexOf('-') === 0 ?
  2055. '-' + comparison[0].replace(/[^0-9.]/g, '') :
  2056. comparison[0].replace(/[^0-9.]/g, '');
  2057. var comp1 = comparison[1].indexOf('-') === 0 ?
  2058. '-' + comparison[1].replace(/[^0-9.]/g, '') :
  2059. comparison[1].replace(/[^0-9.]/g, '');
  2060. if (+comp0 < +comp1) {
  2061. return +comp0 <= +val && +val <= +comp1;
  2062. }
  2063. else {
  2064. return +comp1 <= +val && +val <= +comp0;
  2065. }
  2066. }
  2067. },
  2068. // eslint-disable-next-line sort-keys
  2069. '!between': {
  2070. conditionName: function (dt, i18n) {
  2071. return dt.i18n('searchBuilder.conditions.number.notBetween', i18n.conditions.number.notBetween);
  2072. },
  2073. init: Criteria.init2Input,
  2074. inputValue: Criteria.inputValueInput,
  2075. isInputValid: Criteria.isInputValidInput,
  2076. search: function (value, comparison) {
  2077. var val = value.indexOf('-') === 0 ?
  2078. '-' + value.replace(/[^0-9.]/g, '') :
  2079. value.replace(/[^0-9.]/g, '');
  2080. var comp0 = comparison[0].indexOf('-') === 0 ?
  2081. '-' + comparison[0].replace(/[^0-9.]/g, '') :
  2082. comparison[0].replace(/[^0-9.]/g, '');
  2083. var comp1 = comparison[1].indexOf('-') === 0 ?
  2084. '-' + comparison[1].replace(/[^0-9.]/g, '') :
  2085. comparison[1].replace(/[^0-9.]/g, '');
  2086. if (+comp0 < +comp1) {
  2087. return !(+comp0 <= +val && +val <= +comp1);
  2088. }
  2089. else {
  2090. return !(+comp1 <= +val && +val <= +comp0);
  2091. }
  2092. }
  2093. },
  2094. 'null': {
  2095. conditionName: function (dt, i18n) {
  2096. return dt.i18n('searchBuilder.conditions.number.empty', i18n.conditions.number.empty);
  2097. },
  2098. init: Criteria.initNoValue,
  2099. inputValue: function () {
  2100. return;
  2101. },
  2102. isInputValid: function () {
  2103. return true;
  2104. },
  2105. search: function (value) {
  2106. return value === null || value === undefined || value.length === 0;
  2107. }
  2108. },
  2109. // eslint-disable-next-line sort-keys
  2110. '!null': {
  2111. conditionName: function (dt, i18n) {
  2112. return dt.i18n('searchBuilder.conditions.number.notEmpty', i18n.conditions.number.notEmpty);
  2113. },
  2114. init: Criteria.initNoValue,
  2115. inputValue: function () {
  2116. return;
  2117. },
  2118. isInputValid: function () {
  2119. return true;
  2120. },
  2121. search: function (value) {
  2122. return !(value === null || value === undefined || value.length === 0);
  2123. }
  2124. }
  2125. };
  2126. // The order of the conditions will make eslint sad :(
  2127. // Has to be in this order so that they are displayed correctly in select elements
  2128. // Also have to disable member ordering for this as the private methods used are not yet declared otherwise
  2129. // eslint-disable-next-line @typescript-eslint/member-ordering
  2130. Criteria.stringConditions = {
  2131. '=': {
  2132. conditionName: function (dt, i18n) {
  2133. return dt.i18n('searchBuilder.conditions.string.equals', i18n.conditions.string.equals);
  2134. },
  2135. init: Criteria.initSelect,
  2136. inputValue: Criteria.inputValueSelect,
  2137. isInputValid: Criteria.isInputValidSelect,
  2138. search: function (value, comparison) {
  2139. return value === comparison[0];
  2140. }
  2141. },
  2142. // eslint-disable-next-line sort-keys
  2143. '!=': {
  2144. conditionName: function (dt, i18n) {
  2145. return dt.i18n('searchBuilder.conditions.string.not', i18n.conditions.string.not);
  2146. },
  2147. init: Criteria.initSelect,
  2148. inputValue: Criteria.inputValueSelect,
  2149. isInputValid: Criteria.isInputValidInput,
  2150. search: function (value, comparison) {
  2151. return value !== comparison[0];
  2152. }
  2153. },
  2154. 'starts': {
  2155. conditionName: function (dt, i18n) {
  2156. return dt.i18n('searchBuilder.conditions.string.startsWith', i18n.conditions.string.startsWith);
  2157. },
  2158. init: Criteria.initInput,
  2159. inputValue: Criteria.inputValueInput,
  2160. isInputValid: Criteria.isInputValidInput,
  2161. search: function (value, comparison) {
  2162. return value.toLowerCase().indexOf(comparison[0].toLowerCase()) === 0;
  2163. }
  2164. },
  2165. // eslint-disable-next-line sort-keys
  2166. 'contains': {
  2167. conditionName: function (dt, i18n) {
  2168. return dt.i18n('searchBuilder.conditions.string.contains', i18n.conditions.string.contains);
  2169. },
  2170. init: Criteria.initInput,
  2171. inputValue: Criteria.inputValueInput,
  2172. isInputValid: Criteria.isInputValidInput,
  2173. search: function (value, comparison) {
  2174. return value.toLowerCase().includes(comparison[0].toLowerCase());
  2175. }
  2176. },
  2177. 'ends': {
  2178. conditionName: function (dt, i18n) {
  2179. return dt.i18n('searchBuilder.conditions.string.endsWith', i18n.conditions.string.endsWith);
  2180. },
  2181. init: Criteria.initInput,
  2182. inputValue: Criteria.inputValueInput,
  2183. isInputValid: Criteria.isInputValidInput,
  2184. search: function (value, comparison) {
  2185. return value.toLowerCase().endsWith(comparison[0].toLowerCase());
  2186. }
  2187. },
  2188. 'null': {
  2189. conditionName: function (dt, i18n) {
  2190. return dt.i18n('searchBuilder.conditions.string.empty', i18n.conditions.string.empty);
  2191. },
  2192. init: Criteria.initNoValue,
  2193. inputValue: function () {
  2194. return;
  2195. },
  2196. isInputValid: function () {
  2197. return true;
  2198. },
  2199. search: function (value) {
  2200. return value === null || value === undefined || value.length === 0;
  2201. }
  2202. },
  2203. // eslint-disable-next-line sort-keys
  2204. '!null': {
  2205. conditionName: function (dt, i18n) {
  2206. return dt.i18n('searchBuilder.conditions.string.notEmpty', i18n.conditions.string.notEmpty);
  2207. },
  2208. init: Criteria.initNoValue,
  2209. inputValue: function () {
  2210. return;
  2211. },
  2212. isInputValid: function () {
  2213. return true;
  2214. },
  2215. search: function (value) {
  2216. return !(value === null || value === undefined || value.length === 0);
  2217. }
  2218. }
  2219. };
  2220. // The order of the conditions will make eslint sad :(
  2221. // Also have to disable member ordering for this as the private methods used are not yet declared otherwise
  2222. // eslint-disable-next-line @typescript-eslint/member-ordering
  2223. Criteria.arrayConditions = {
  2224. 'contains': {
  2225. conditionName: function (dt, i18n) {
  2226. return dt.i18n('searchBuilder.conditions.array.contains', i18n.conditions.array.contains);
  2227. },
  2228. init: Criteria.initSelectArray,
  2229. inputValue: Criteria.inputValueSelect,
  2230. isInputValid: Criteria.isInputValidSelect,
  2231. search: function (value, comparison) {
  2232. return value.includes(comparison[0]);
  2233. }
  2234. },
  2235. 'without': {
  2236. conditionName: function (dt, i18n) {
  2237. return dt.i18n('searchBuilder.conditions.array.without', i18n.conditions.array.without);
  2238. },
  2239. init: Criteria.initSelectArray,
  2240. inputValue: Criteria.inputValueSelect,
  2241. isInputValid: Criteria.isInputValidSelect,
  2242. search: function (value, comparison) {
  2243. return value.indexOf(comparison[0]) === -1;
  2244. }
  2245. },
  2246. // eslint-disable-next-line sort-keys
  2247. '=': {
  2248. conditionName: function (dt, i18n) {
  2249. return dt.i18n('searchBuilder.conditions.array.equals', i18n.conditions.array.equals);
  2250. },
  2251. init: Criteria.initSelect,
  2252. inputValue: Criteria.inputValueSelect,
  2253. isInputValid: Criteria.isInputValidSelect,
  2254. search: function (value, comparison) {
  2255. if (value.length === comparison[0].length) {
  2256. for (var i = 0; i < value.length; i++) {
  2257. if (value[i] !== comparison[0][i]) {
  2258. return false;
  2259. }
  2260. }
  2261. return true;
  2262. }
  2263. return false;
  2264. }
  2265. },
  2266. // eslint-disable-next-line sort-keys
  2267. '!=': {
  2268. conditionName: function (dt, i18n) {
  2269. return dt.i18n('searchBuilder.conditions.array.not', i18n.conditions.array.not);
  2270. },
  2271. init: Criteria.initSelect,
  2272. inputValue: Criteria.inputValueSelect,
  2273. isInputValid: Criteria.isInputValidSelect,
  2274. search: function (value, comparison) {
  2275. if (value.length === comparison[0].length) {
  2276. for (var i = 0; i < value.length; i++) {
  2277. if (value[i] !== comparison[0][i]) {
  2278. return true;
  2279. }
  2280. }
  2281. return false;
  2282. }
  2283. return true;
  2284. }
  2285. },
  2286. 'null': {
  2287. conditionName: function (dt, i18n) {
  2288. return dt.i18n('searchBuilder.conditions.array.empty', i18n.conditions.array.empty);
  2289. },
  2290. init: Criteria.initNoValue,
  2291. inputValue: function () {
  2292. return;
  2293. },
  2294. isInputValid: function () {
  2295. return true;
  2296. },
  2297. search: function (value) {
  2298. return value === null || value === undefined || value.length === 0;
  2299. }
  2300. },
  2301. // eslint-disable-next-line sort-keys
  2302. '!null': {
  2303. conditionName: function (dt, i18n) {
  2304. return dt.i18n('searchBuilder.conditions.array.notEmpty', i18n.conditions.array.notEmpty);
  2305. },
  2306. init: Criteria.initNoValue,
  2307. inputValue: function () {
  2308. return;
  2309. },
  2310. isInputValid: function () {
  2311. return true;
  2312. },
  2313. search: function (value) {
  2314. return value !== null && value !== undefined && value.length !== 0;
  2315. }
  2316. }
  2317. };
  2318. // eslint will be sad because we have to disable member ordering for this as the
  2319. // private static properties used are not yet declared otherwise
  2320. // eslint-disable-next-line @typescript-eslint/member-ordering
  2321. Criteria.defaults = {
  2322. columns: true,
  2323. conditions: {
  2324. 'array': Criteria.arrayConditions,
  2325. 'date': Criteria.dateConditions,
  2326. 'html': Criteria.stringConditions,
  2327. 'html-num': Criteria.numConditions,
  2328. 'html-num-fmt': Criteria.numFmtConditions,
  2329. 'luxon': Criteria.luxonDateConditions,
  2330. 'moment': Criteria.momentDateConditions,
  2331. 'num': Criteria.numConditions,
  2332. 'num-fmt': Criteria.numFmtConditions,
  2333. 'string': Criteria.stringConditions
  2334. },
  2335. depthLimit: false,
  2336. enterSearch: false,
  2337. filterChanged: undefined,
  2338. greyscale: false,
  2339. i18n: {
  2340. add: 'Add Condition',
  2341. button: {
  2342. 0: 'Search Builder',
  2343. _: 'Search Builder (%d)'
  2344. },
  2345. clearAll: 'Clear All',
  2346. condition: 'Condition',
  2347. data: 'Data',
  2348. deleteTitle: 'Delete filtering rule',
  2349. leftTitle: 'Outdent criteria',
  2350. logicAnd: 'And',
  2351. logicOr: 'Or',
  2352. rightTitle: 'Indent criteria',
  2353. title: {
  2354. 0: 'Custom Search Builder',
  2355. _: 'Custom Search Builder (%d)'
  2356. },
  2357. value: 'Value',
  2358. valueJoiner: 'and'
  2359. },
  2360. logic: 'AND',
  2361. orthogonal: {
  2362. display: 'display',
  2363. search: 'filter'
  2364. },
  2365. preDefined: false
  2366. };
  2367. return Criteria;
  2368. }());
  2369. var $$1;
  2370. var dataTable$1;
  2371. /**
  2372. * Sets the value of jQuery for use in the file
  2373. *
  2374. * @param jq the instance of jQuery to be set
  2375. */
  2376. function setJQuery$1(jq) {
  2377. $$1 = jq;
  2378. dataTable$1 = jq.fn.dataTable;
  2379. }
  2380. /**
  2381. * The Group class is used within SearchBuilder to represent a group of criteria
  2382. */
  2383. var Group = /** @class */ (function () {
  2384. function Group(table, opts, topGroup, index, isChild, depth) {
  2385. if (index === void 0) { index = 0; }
  2386. if (isChild === void 0) { isChild = false; }
  2387. if (depth === void 0) { depth = 1; }
  2388. // Check that the required version of DataTables is included
  2389. if (!dataTable$1 || !dataTable$1.versionCheck || !dataTable$1.versionCheck('1.10.0')) {
  2390. throw new Error('SearchBuilder requires DataTables 1.10 or newer');
  2391. }
  2392. this.classes = $$1.extend(true, {}, Group.classes);
  2393. // Get options from user
  2394. this.c = $$1.extend(true, {}, Group.defaults, opts);
  2395. this.s = {
  2396. criteria: [],
  2397. depth: depth,
  2398. dt: table,
  2399. index: index,
  2400. isChild: isChild,
  2401. logic: undefined,
  2402. opts: opts,
  2403. preventRedraw: false,
  2404. toDrop: undefined,
  2405. topGroup: topGroup
  2406. };
  2407. this.dom = {
  2408. add: $$1('<button/>')
  2409. .addClass(this.classes.add)
  2410. .addClass(this.classes.button)
  2411. .attr('type', 'button'),
  2412. clear: $$1('<button>&times</button>')
  2413. .addClass(this.classes.button)
  2414. .addClass(this.classes.clearGroup)
  2415. .attr('type', 'button'),
  2416. container: $$1('<div/>')
  2417. .addClass(this.classes.group),
  2418. logic: $$1('<button><div/></button>')
  2419. .addClass(this.classes.logic)
  2420. .addClass(this.classes.button)
  2421. .attr('type', 'button'),
  2422. logicContainer: $$1('<div/>')
  2423. .addClass(this.classes.logicContainer)
  2424. };
  2425. // A reference to the top level group is maintained throughout any subgroups and criteria that may be created
  2426. if (this.s.topGroup === undefined) {
  2427. this.s.topGroup = this.dom.container;
  2428. }
  2429. this._setup();
  2430. return this;
  2431. }
  2432. /**
  2433. * Destroys the groups buttons, clears the internal criteria and removes it from the dom
  2434. */
  2435. Group.prototype.destroy = function () {
  2436. // Turn off listeners
  2437. this.dom.add.off('.dtsb');
  2438. this.dom.logic.off('.dtsb');
  2439. // Trigger event for groups at a higher level to pick up on
  2440. this.dom.container
  2441. .trigger('dtsb-destroy')
  2442. .remove();
  2443. this.s.criteria = [];
  2444. };
  2445. /**
  2446. * Gets the details required to rebuild the group
  2447. */
  2448. // Eslint upset at empty object but needs to be done
  2449. // eslint-disable-next-line @typescript-eslint/ban-types
  2450. Group.prototype.getDetails = function (deFormatDates) {
  2451. if (deFormatDates === void 0) { deFormatDates = false; }
  2452. if (this.s.criteria.length === 0) {
  2453. return {};
  2454. }
  2455. var details = {
  2456. criteria: [],
  2457. logic: this.s.logic
  2458. };
  2459. // NOTE here crit could be either a subgroup or a criteria
  2460. for (var _i = 0, _a = this.s.criteria; _i < _a.length; _i++) {
  2461. var crit = _a[_i];
  2462. details.criteria.push(crit.criteria.getDetails(deFormatDates));
  2463. }
  2464. return details;
  2465. };
  2466. /**
  2467. * Getter for the node for the container of the group
  2468. *
  2469. * @returns Node for the container of the group
  2470. */
  2471. Group.prototype.getNode = function () {
  2472. return this.dom.container;
  2473. };
  2474. /**
  2475. * Rebuilds the group based upon the details passed in
  2476. *
  2477. * @param loadedDetails the details required to rebuild the group
  2478. */
  2479. Group.prototype.rebuild = function (loadedDetails) {
  2480. // If no criteria are stored then just return
  2481. if (loadedDetails.criteria === undefined ||
  2482. loadedDetails.criteria === null ||
  2483. Array.isArray(loadedDetails.criteria) && loadedDetails.criteria.length === 0) {
  2484. return;
  2485. }
  2486. this.s.logic = loadedDetails.logic;
  2487. this.dom.logic.children().first().text(this.s.logic === 'OR'
  2488. ? this.s.dt.i18n('searchBuilder.logicOr', this.c.i18n.logicOr)
  2489. : this.s.dt.i18n('searchBuilder.logicAnd', this.c.i18n.logicAnd));
  2490. // Add all of the criteria, be it a sub group or a criteria
  2491. if (Array.isArray(loadedDetails.criteria)) {
  2492. for (var _i = 0, _a = loadedDetails.criteria; _i < _a.length; _i++) {
  2493. var crit = _a[_i];
  2494. if (crit.logic !== undefined) {
  2495. this._addPrevGroup(crit);
  2496. }
  2497. else if (crit.logic === undefined) {
  2498. this._addPrevCriteria(crit);
  2499. }
  2500. }
  2501. }
  2502. // For all of the criteria children, update the arrows incase they require changing and set the listeners
  2503. for (var _b = 0, _c = this.s.criteria; _b < _c.length; _b++) {
  2504. var crit = _c[_b];
  2505. if (crit.criteria instanceof Criteria) {
  2506. crit.criteria.updateArrows(this.s.criteria.length > 1, false);
  2507. this._setCriteriaListeners(crit.criteria);
  2508. }
  2509. }
  2510. };
  2511. /**
  2512. * Redraws the Contents of the searchBuilder Groups and Criteria
  2513. */
  2514. Group.prototype.redrawContents = function () {
  2515. if (this.s.preventRedraw) {
  2516. return;
  2517. }
  2518. // Clear the container out and add the basic elements
  2519. this.dom.container.children().detach();
  2520. this.dom.container
  2521. .append(this.dom.logicContainer)
  2522. .append(this.dom.add);
  2523. // Sort the criteria by index so that they appear in the correct order
  2524. this.s.criteria.sort(function (a, b) {
  2525. if (a.criteria.s.index < b.criteria.s.index) {
  2526. return -1;
  2527. }
  2528. else if (a.criteria.s.index > b.criteria.s.index) {
  2529. return 1;
  2530. }
  2531. return 0;
  2532. });
  2533. this.setListeners();
  2534. for (var i = 0; i < this.s.criteria.length; i++) {
  2535. var crit = this.s.criteria[i].criteria;
  2536. if (crit instanceof Criteria) {
  2537. // Reset the index to the new value
  2538. this.s.criteria[i].index = i;
  2539. this.s.criteria[i].criteria.s.index = i;
  2540. // Add to the group
  2541. this.s.criteria[i].criteria.dom.container.insertBefore(this.dom.add);
  2542. // Set listeners for various points
  2543. this._setCriteriaListeners(crit);
  2544. this.s.criteria[i].criteria.rebuild(this.s.criteria[i].criteria.getDetails());
  2545. }
  2546. else if (crit instanceof Group && crit.s.criteria.length > 0) {
  2547. // Reset the index to the new value
  2548. this.s.criteria[i].index = i;
  2549. this.s.criteria[i].criteria.s.index = i;
  2550. // Add the sub group to the group
  2551. this.s.criteria[i].criteria.dom.container.insertBefore(this.dom.add);
  2552. // Redraw the contents of the group
  2553. crit.redrawContents();
  2554. this._setGroupListeners(crit);
  2555. }
  2556. else {
  2557. // The group is empty so remove it
  2558. this.s.criteria.splice(i, 1);
  2559. i--;
  2560. }
  2561. }
  2562. this.setupLogic();
  2563. };
  2564. /**
  2565. * Resizes the logic button only rather than the entire dom.
  2566. */
  2567. Group.prototype.redrawLogic = function () {
  2568. for (var _i = 0, _a = this.s.criteria; _i < _a.length; _i++) {
  2569. var crit = _a[_i];
  2570. if (crit instanceof Group) {
  2571. crit.redrawLogic();
  2572. }
  2573. }
  2574. this.setupLogic();
  2575. };
  2576. /**
  2577. * Search method, checking the row data against the criteria in the group
  2578. *
  2579. * @param rowData The row data to be compared
  2580. * @returns boolean The result of the search
  2581. */
  2582. Group.prototype.search = function (rowData, rowIdx) {
  2583. if (this.s.logic === 'AND') {
  2584. return this._andSearch(rowData, rowIdx);
  2585. }
  2586. else if (this.s.logic === 'OR') {
  2587. return this._orSearch(rowData, rowIdx);
  2588. }
  2589. return true;
  2590. };
  2591. /**
  2592. * Locates the groups logic button to the correct location on the page
  2593. */
  2594. Group.prototype.setupLogic = function () {
  2595. // Remove logic button
  2596. this.dom.logicContainer.remove();
  2597. this.dom.clear.remove();
  2598. // If there are no criteria in the group then keep the logic removed and return
  2599. if (this.s.criteria.length < 1) {
  2600. if (!this.s.isChild) {
  2601. this.dom.container.trigger('dtsb-destroy');
  2602. // Set criteria left margin
  2603. this.dom.container.css('margin-left', 0);
  2604. }
  2605. return;
  2606. }
  2607. // Set width, take 2 for the border
  2608. var height = this.dom.container.height() - 1;
  2609. this.dom.clear.height('0px');
  2610. this.dom.logicContainer.append(this.dom.clear).width(height);
  2611. // Prepend logic button
  2612. this.dom.container.prepend(this.dom.logicContainer);
  2613. this._setLogicListener();
  2614. // Set criteria left margin
  2615. this.dom.container.css('margin-left', this.dom.logicContainer.outerHeight(true));
  2616. var logicOffset = this.dom.logicContainer.offset();
  2617. // Set horizontal alignment
  2618. var currentLeft = logicOffset.left;
  2619. var groupLeft = this.dom.container.offset().left;
  2620. var shuffleLeft = currentLeft - groupLeft;
  2621. var newPos = currentLeft - shuffleLeft - this.dom.logicContainer.outerHeight(true);
  2622. this.dom.logicContainer.offset({ left: newPos });
  2623. // Set vertical alignment
  2624. var firstCrit = this.dom.logicContainer.next();
  2625. var currentTop = logicOffset.top;
  2626. var firstTop = $$1(firstCrit).offset().top;
  2627. var shuffleTop = currentTop - firstTop;
  2628. var newTop = currentTop - shuffleTop;
  2629. this.dom.logicContainer.offset({ top: newTop });
  2630. this.dom.clear.outerHeight(this.dom.logicContainer.height());
  2631. this._setClearListener();
  2632. };
  2633. /**
  2634. * Sets listeners on the groups elements
  2635. */
  2636. Group.prototype.setListeners = function () {
  2637. var _this = this;
  2638. this.dom.add.unbind('click');
  2639. this.dom.add.on('click', function () {
  2640. // If this is the parent group then the logic button has not been added yet
  2641. if (!_this.s.isChild) {
  2642. _this.dom.container.prepend(_this.dom.logicContainer);
  2643. }
  2644. _this.addCriteria();
  2645. _this.dom.container.trigger('dtsb-add');
  2646. _this.s.dt.state.save();
  2647. return false;
  2648. });
  2649. for (var _i = 0, _a = this.s.criteria; _i < _a.length; _i++) {
  2650. var crit = _a[_i];
  2651. crit.criteria.setListeners();
  2652. }
  2653. this._setClearListener();
  2654. this._setLogicListener();
  2655. };
  2656. /**
  2657. * Adds a criteria to the group
  2658. *
  2659. * @param crit Instance of Criteria to be added to the group
  2660. */
  2661. Group.prototype.addCriteria = function (crit, redraw) {
  2662. if (crit === void 0) { crit = null; }
  2663. if (redraw === void 0) { redraw = true; }
  2664. var index = crit === null ? this.s.criteria.length : crit.s.index;
  2665. var criteria = new Criteria(this.s.dt, this.s.opts, this.s.topGroup, index, this.s.depth);
  2666. // If a Criteria has been passed in then set the values to continue that
  2667. if (crit !== null) {
  2668. criteria.c = crit.c;
  2669. criteria.s = crit.s;
  2670. criteria.s.depth = this.s.depth;
  2671. criteria.classes = crit.classes;
  2672. }
  2673. criteria.populate();
  2674. var inserted = false;
  2675. for (var i = 0; i < this.s.criteria.length; i++) {
  2676. if (i === 0 && this.s.criteria[i].criteria.s.index > criteria.s.index) {
  2677. // Add the node for the criteria at the start of the group
  2678. criteria.getNode().insertBefore(this.s.criteria[i].criteria.dom.container);
  2679. inserted = true;
  2680. }
  2681. else if (i < this.s.criteria.length - 1 &&
  2682. this.s.criteria[i].criteria.s.index < criteria.s.index &&
  2683. this.s.criteria[i + 1].criteria.s.index > criteria.s.index) {
  2684. // Add the node for the criteria in the correct location
  2685. criteria.getNode().insertAfter(this.s.criteria[i].criteria.dom.container);
  2686. inserted = true;
  2687. }
  2688. }
  2689. if (!inserted) {
  2690. criteria.getNode().insertBefore(this.dom.add);
  2691. }
  2692. // Add the details for this criteria to the array
  2693. this.s.criteria.push({
  2694. criteria: criteria,
  2695. index: index
  2696. });
  2697. this.s.criteria = this.s.criteria.sort(function (a, b) { return a.criteria.s.index - b.criteria.s.index; });
  2698. for (var _i = 0, _a = this.s.criteria; _i < _a.length; _i++) {
  2699. var opt = _a[_i];
  2700. if (opt.criteria instanceof Criteria) {
  2701. opt.criteria.updateArrows(this.s.criteria.length > 1, redraw);
  2702. }
  2703. }
  2704. this._setCriteriaListeners(criteria);
  2705. criteria.setListeners();
  2706. this.setupLogic();
  2707. };
  2708. /**
  2709. * Checks the group to see if it has any filled criteria
  2710. */
  2711. Group.prototype.checkFilled = function () {
  2712. for (var _i = 0, _a = this.s.criteria; _i < _a.length; _i++) {
  2713. var crit = _a[_i];
  2714. if (crit.criteria instanceof Criteria && crit.criteria.s.filled ||
  2715. crit.criteria instanceof Group && crit.criteria.checkFilled()) {
  2716. return true;
  2717. }
  2718. }
  2719. return false;
  2720. };
  2721. /**
  2722. * Gets the count for the number of criteria in this group and any sub groups
  2723. */
  2724. Group.prototype.count = function () {
  2725. var count = 0;
  2726. for (var _i = 0, _a = this.s.criteria; _i < _a.length; _i++) {
  2727. var crit = _a[_i];
  2728. if (crit.criteria instanceof Group) {
  2729. count += crit.criteria.count();
  2730. }
  2731. else {
  2732. count++;
  2733. }
  2734. }
  2735. return count;
  2736. };
  2737. /**
  2738. * Rebuilds a sub group that previously existed
  2739. *
  2740. * @param loadedGroup The details of a group within this group
  2741. */
  2742. Group.prototype._addPrevGroup = function (loadedGroup) {
  2743. var idx = this.s.criteria.length;
  2744. var group = new Group(this.s.dt, this.c, this.s.topGroup, idx, true, this.s.depth + 1);
  2745. // Add the new group to the criteria array
  2746. this.s.criteria.push({
  2747. criteria: group,
  2748. index: idx,
  2749. logic: group.s.logic
  2750. });
  2751. // Rebuild it with the previous conditions for that group
  2752. group.rebuild(loadedGroup);
  2753. this.s.criteria[idx].criteria = group;
  2754. this.s.topGroup.trigger('dtsb-redrawContents');
  2755. this._setGroupListeners(group);
  2756. };
  2757. /**
  2758. * Rebuilds a criteria of this group that previously existed
  2759. *
  2760. * @param loadedCriteria The details of a criteria within the group
  2761. */
  2762. Group.prototype._addPrevCriteria = function (loadedCriteria) {
  2763. var idx = this.s.criteria.length;
  2764. var criteria = new Criteria(this.s.dt, this.s.opts, this.s.topGroup, idx, this.s.depth);
  2765. criteria.populate();
  2766. // Add the new criteria to the criteria array
  2767. this.s.criteria.push({
  2768. criteria: criteria,
  2769. index: idx
  2770. });
  2771. // Rebuild it with the previous conditions for that criteria
  2772. criteria.rebuild(loadedCriteria);
  2773. this.s.criteria[idx].criteria = criteria;
  2774. this.s.topGroup.trigger('dtsb-redrawContents');
  2775. };
  2776. /**
  2777. * Checks And the criteria using AND logic
  2778. *
  2779. * @param rowData The row data to be checked against the search criteria
  2780. * @returns boolean The result of the AND search
  2781. */
  2782. Group.prototype._andSearch = function (rowData, rowIdx) {
  2783. // If there are no criteria then return true for this group
  2784. if (this.s.criteria.length === 0) {
  2785. return true;
  2786. }
  2787. for (var _i = 0, _a = this.s.criteria; _i < _a.length; _i++) {
  2788. var crit = _a[_i];
  2789. // If the criteria is not complete then skip it
  2790. if (crit.criteria instanceof Criteria && !crit.criteria.s.filled) {
  2791. continue;
  2792. }
  2793. // Otherwise if a single one fails return false
  2794. else if (!crit.criteria.search(rowData, rowIdx)) {
  2795. return false;
  2796. }
  2797. }
  2798. // If we get to here then everything has passed, so return true for the group
  2799. return true;
  2800. };
  2801. /**
  2802. * Checks And the criteria using OR logic
  2803. *
  2804. * @param rowData The row data to be checked against the search criteria
  2805. * @returns boolean The result of the OR search
  2806. */
  2807. Group.prototype._orSearch = function (rowData, rowIdx) {
  2808. // If there are no criteria in the group then return true
  2809. if (this.s.criteria.length === 0) {
  2810. return true;
  2811. }
  2812. // This will check to make sure that at least one criteria in the group is complete
  2813. var filledfound = false;
  2814. for (var _i = 0, _a = this.s.criteria; _i < _a.length; _i++) {
  2815. var crit = _a[_i];
  2816. if (crit.criteria instanceof Criteria && crit.criteria.s.filled) {
  2817. // A completed criteria has been found so set the flag
  2818. filledfound = true;
  2819. // If the search passes then return true
  2820. if (crit.criteria.search(rowData, rowIdx)) {
  2821. return true;
  2822. }
  2823. }
  2824. else if (crit.criteria instanceof Group && crit.criteria.checkFilled()) {
  2825. filledfound = true;
  2826. if (crit.criteria.search(rowData, rowIdx)) {
  2827. return true;
  2828. }
  2829. }
  2830. }
  2831. // If we get here we need to return the inverse of filledfound,
  2832. // as if any have been found and we are here then none have passed
  2833. return !filledfound;
  2834. };
  2835. /**
  2836. * Removes a criteria from the group
  2837. *
  2838. * @param criteria The criteria instance to be removed
  2839. */
  2840. Group.prototype._removeCriteria = function (criteria, group) {
  2841. if (group === void 0) { group = false; }
  2842. // If removing a criteria and there is only then then just destroy the group
  2843. if (this.s.criteria.length <= 1 && this.s.isChild) {
  2844. this.destroy();
  2845. }
  2846. else {
  2847. // Otherwise splice the given criteria out and redo the indexes
  2848. var last = void 0;
  2849. for (var i = 0; i < this.s.criteria.length; i++) {
  2850. if (this.s.criteria[i].index === criteria.s.index &&
  2851. (!group || this.s.criteria[i].criteria instanceof Group)) {
  2852. last = i;
  2853. }
  2854. }
  2855. // We want to remove the last element with the desired index, as its replacement will be inserted before it
  2856. if (last !== undefined) {
  2857. this.s.criteria.splice(last, 1);
  2858. }
  2859. for (var i = 0; i < this.s.criteria.length; i++) {
  2860. this.s.criteria[i].index = i;
  2861. this.s.criteria[i].criteria.s.index = i;
  2862. }
  2863. }
  2864. };
  2865. /**
  2866. * Sets the listeners in group for a criteria
  2867. *
  2868. * @param criteria The criteria for the listeners to be set on
  2869. */
  2870. Group.prototype._setCriteriaListeners = function (criteria) {
  2871. var _this = this;
  2872. criteria.dom["delete"]
  2873. .unbind('click')
  2874. .on('click', function () {
  2875. _this._removeCriteria(criteria);
  2876. criteria.dom.container.remove();
  2877. for (var _i = 0, _a = _this.s.criteria; _i < _a.length; _i++) {
  2878. var crit = _a[_i];
  2879. if (crit.criteria instanceof Criteria) {
  2880. crit.criteria.updateArrows(_this.s.criteria.length > 1);
  2881. }
  2882. }
  2883. criteria.destroy();
  2884. _this.s.dt.draw();
  2885. _this.s.topGroup.trigger('dtsb-redrawContents');
  2886. _this.s.topGroup.trigger('dtsb-updateTitle');
  2887. return false;
  2888. });
  2889. criteria.dom.right
  2890. .unbind('click')
  2891. .on('click', function () {
  2892. var idx = criteria.s.index;
  2893. var group = new Group(_this.s.dt, _this.s.opts, _this.s.topGroup, criteria.s.index, true, _this.s.depth + 1);
  2894. // Add the criteria that is to be moved to the new group
  2895. group.addCriteria(criteria);
  2896. // Update the details in the current groups criteria array
  2897. _this.s.criteria[idx].criteria = group;
  2898. _this.s.criteria[idx].logic = 'AND';
  2899. _this.s.topGroup.trigger('dtsb-redrawContents');
  2900. _this._setGroupListeners(group);
  2901. return false;
  2902. });
  2903. criteria.dom.left
  2904. .unbind('click')
  2905. .on('click', function () {
  2906. _this.s.toDrop = new Criteria(_this.s.dt, _this.s.opts, _this.s.topGroup, criteria.s.index);
  2907. _this.s.toDrop.s = criteria.s;
  2908. _this.s.toDrop.c = criteria.c;
  2909. _this.s.toDrop.classes = criteria.classes;
  2910. _this.s.toDrop.populate();
  2911. // The dropCriteria event mutates the reference to the index so need to store it
  2912. var index = _this.s.toDrop.s.index;
  2913. _this.dom.container.trigger('dtsb-dropCriteria');
  2914. criteria.s.index = index;
  2915. _this._removeCriteria(criteria);
  2916. // By tracking the top level group we can directly trigger a redraw on it,
  2917. // bubbling is also possible, but that is slow with deep levelled groups
  2918. _this.s.topGroup.trigger('dtsb-redrawContents');
  2919. _this.s.dt.draw();
  2920. return false;
  2921. });
  2922. };
  2923. /**
  2924. * Set's the listeners for the group clear button
  2925. */
  2926. Group.prototype._setClearListener = function () {
  2927. var _this = this;
  2928. this.dom.clear
  2929. .unbind('click')
  2930. .on('click', function () {
  2931. if (!_this.s.isChild) {
  2932. _this.dom.container.trigger('dtsb-clearContents');
  2933. return false;
  2934. }
  2935. _this.destroy();
  2936. _this.s.topGroup.trigger('dtsb-updateTitle');
  2937. _this.s.topGroup.trigger('dtsb-redrawContents');
  2938. return false;
  2939. });
  2940. };
  2941. /**
  2942. * Sets listeners for sub groups of this group
  2943. *
  2944. * @param group The sub group that the listeners are to be set on
  2945. */
  2946. Group.prototype._setGroupListeners = function (group) {
  2947. var _this = this;
  2948. // Set listeners for the new group
  2949. group.dom.add
  2950. .unbind('click')
  2951. .on('click', function () {
  2952. _this.setupLogic();
  2953. _this.dom.container.trigger('dtsb-add');
  2954. return false;
  2955. });
  2956. group.dom.container
  2957. .unbind('dtsb-add')
  2958. .on('dtsb-add', function () {
  2959. _this.setupLogic();
  2960. _this.dom.container.trigger('dtsb-add');
  2961. return false;
  2962. });
  2963. group.dom.container
  2964. .unbind('dtsb-destroy')
  2965. .on('dtsb-destroy', function () {
  2966. _this._removeCriteria(group, true);
  2967. group.dom.container.remove();
  2968. _this.setupLogic();
  2969. return false;
  2970. });
  2971. group.dom.container
  2972. .unbind('dtsb-dropCriteria')
  2973. .on('dtsb-dropCriteria', function () {
  2974. var toDrop = group.s.toDrop;
  2975. toDrop.s.index = group.s.index;
  2976. toDrop.updateArrows(_this.s.criteria.length > 1, false);
  2977. _this.addCriteria(toDrop, false);
  2978. return false;
  2979. });
  2980. group.setListeners();
  2981. };
  2982. /**
  2983. * Sets up the Group instance, setting listeners and appending elements
  2984. */
  2985. Group.prototype._setup = function () {
  2986. this.setListeners();
  2987. this.dom.add.text(this.s.dt.i18n('searchBuilder.add', this.c.i18n.add));
  2988. this.dom.logic.children().first().text(this.c.logic === 'OR'
  2989. ? this.s.dt.i18n('searchBuilder.logicOr', this.c.i18n.logicOr)
  2990. : this.s.dt.i18n('searchBuilder.logicAnd', this.c.i18n.logicAnd));
  2991. this.s.logic = this.c.logic === 'OR' ? 'OR' : 'AND';
  2992. if (this.c.greyscale) {
  2993. this.dom.logic.addClass(this.classes.greyscale);
  2994. }
  2995. this.dom.logicContainer.append(this.dom.logic).append(this.dom.clear);
  2996. // Only append the logic button immediately if this is a sub group,
  2997. // otherwise it will be prepended later when adding a criteria
  2998. if (this.s.isChild) {
  2999. this.dom.container.append(this.dom.logicContainer);
  3000. }
  3001. this.dom.container.append(this.dom.add);
  3002. };
  3003. /**
  3004. * Sets the listener for the logic button
  3005. */
  3006. Group.prototype._setLogicListener = function () {
  3007. var _this = this;
  3008. this.dom.logic
  3009. .unbind('click')
  3010. .on('click', function () {
  3011. _this._toggleLogic();
  3012. _this.s.dt.draw();
  3013. for (var _i = 0, _a = _this.s.criteria; _i < _a.length; _i++) {
  3014. var crit = _a[_i];
  3015. crit.criteria.setListeners();
  3016. }
  3017. });
  3018. };
  3019. /**
  3020. * Toggles the logic for the group
  3021. */
  3022. Group.prototype._toggleLogic = function () {
  3023. if (this.s.logic === 'OR') {
  3024. this.s.logic = 'AND';
  3025. this.dom.logic.children().first().text(this.s.dt.i18n('searchBuilder.logicAnd', this.c.i18n.logicAnd));
  3026. }
  3027. else if (this.s.logic === 'AND') {
  3028. this.s.logic = 'OR';
  3029. this.dom.logic.children().first().text(this.s.dt.i18n('searchBuilder.logicOr', this.c.i18n.logicOr));
  3030. }
  3031. };
  3032. Group.version = '1.1.0';
  3033. Group.classes = {
  3034. add: 'dtsb-add',
  3035. button: 'dtsb-button',
  3036. clearGroup: 'dtsb-clearGroup',
  3037. greyscale: 'dtsb-greyscale',
  3038. group: 'dtsb-group',
  3039. inputButton: 'dtsb-iptbtn',
  3040. logic: 'dtsb-logic',
  3041. logicContainer: 'dtsb-logicContainer'
  3042. };
  3043. Group.defaults = {
  3044. columns: true,
  3045. conditions: {
  3046. 'date': Criteria.dateConditions,
  3047. 'html': Criteria.stringConditions,
  3048. 'html-num': Criteria.numConditions,
  3049. 'html-num-fmt': Criteria.numFmtConditions,
  3050. 'luxon': Criteria.luxonDateConditions,
  3051. 'moment': Criteria.momentDateConditions,
  3052. 'num': Criteria.numConditions,
  3053. 'num-fmt': Criteria.numFmtConditions,
  3054. 'string': Criteria.stringConditions
  3055. },
  3056. depthLimit: false,
  3057. enterSearch: false,
  3058. filterChanged: undefined,
  3059. greyscale: false,
  3060. i18n: {
  3061. add: 'Add Condition',
  3062. button: {
  3063. 0: 'Search Builder',
  3064. _: 'Search Builder (%d)'
  3065. },
  3066. clearAll: 'Clear All',
  3067. condition: 'Condition',
  3068. data: 'Data',
  3069. deleteTitle: 'Delete filtering rule',
  3070. leftTitle: 'Outdent criteria',
  3071. logicAnd: 'And',
  3072. logicOr: 'Or',
  3073. rightTitle: 'Indent criteria',
  3074. title: {
  3075. 0: 'Custom Search Builder',
  3076. _: 'Custom Search Builder (%d)'
  3077. },
  3078. value: 'Value',
  3079. valueJoiner: 'and'
  3080. },
  3081. logic: 'AND',
  3082. orthogonal: {
  3083. display: 'display',
  3084. search: 'filter'
  3085. },
  3086. preDefined: false
  3087. };
  3088. return Group;
  3089. }());
  3090. var $;
  3091. var dataTable;
  3092. /**
  3093. * Sets the value of jQuery for use in the file
  3094. *
  3095. * @param jq the instance of jQuery to be set
  3096. */
  3097. function setJQuery(jq) {
  3098. $ = jq;
  3099. dataTable = jq.fn.DataTable;
  3100. }
  3101. /**
  3102. * SearchBuilder class for DataTables.
  3103. * Allows for complex search queries to be constructed and implemented on a DataTable
  3104. */
  3105. var SearchBuilder = /** @class */ (function () {
  3106. function SearchBuilder(builderSettings, opts) {
  3107. var _this = this;
  3108. // Check that the required version of DataTables is included
  3109. if (!dataTable || !dataTable.versionCheck || !dataTable.versionCheck('1.10.0')) {
  3110. throw new Error('SearchBuilder requires DataTables 1.10 or newer');
  3111. }
  3112. var table = new dataTable.Api(builderSettings);
  3113. this.classes = $.extend(true, {}, SearchBuilder.classes);
  3114. // Get options from user
  3115. this.c = $.extend(true, {}, SearchBuilder.defaults, opts);
  3116. this.dom = {
  3117. clearAll: $('<button type="button">' + table.i18n('searchBuilder.clearAll', this.c.i18n.clearAll) + '</button>')
  3118. .addClass(this.classes.clearAll)
  3119. .addClass(this.classes.button)
  3120. .attr('type', 'button'),
  3121. container: $('<div/>')
  3122. .addClass(this.classes.container),
  3123. title: $('<div/>')
  3124. .addClass(this.classes.title),
  3125. titleRow: $('<div/>')
  3126. .addClass(this.classes.titleRow),
  3127. topGroup: undefined
  3128. };
  3129. this.s = {
  3130. dt: table,
  3131. opts: opts,
  3132. search: undefined,
  3133. topGroup: undefined
  3134. };
  3135. // If searchbuilder is already defined for this table then return
  3136. if (table.settings()[0]._searchBuilder !== undefined) {
  3137. return;
  3138. }
  3139. table.settings()[0]._searchBuilder = this;
  3140. // Run the remaining setup when the table is initialised
  3141. if (this.s.dt.settings()[0]._bInitComplete) {
  3142. this._setUp();
  3143. }
  3144. else {
  3145. table.one('init.dt', function () {
  3146. _this._setUp();
  3147. });
  3148. }
  3149. return this;
  3150. }
  3151. /**
  3152. * Gets the details required to rebuild the SearchBuilder as it currently is
  3153. */
  3154. // eslint upset at empty object but that is what it is
  3155. // eslint-disable-next-line @typescript-eslint/ban-types
  3156. SearchBuilder.prototype.getDetails = function (deFormatDates) {
  3157. if (deFormatDates === void 0) { deFormatDates = false; }
  3158. return this.s.topGroup.getDetails(deFormatDates);
  3159. };
  3160. /**
  3161. * Getter for the node of the container for the searchBuilder
  3162. *
  3163. * @returns JQuery<HTMLElement> the node of the container
  3164. */
  3165. SearchBuilder.prototype.getNode = function () {
  3166. return this.dom.container;
  3167. };
  3168. /**
  3169. * Rebuilds the SearchBuilder to a state that is provided
  3170. *
  3171. * @param details The details required to perform a rebuild
  3172. */
  3173. SearchBuilder.prototype.rebuild = function (details) {
  3174. this.dom.clearAll.click();
  3175. // If there are no details to rebuild then return
  3176. if (details === undefined || details === null) {
  3177. return this;
  3178. }
  3179. this.s.topGroup.s.preventRedraw = true;
  3180. this.s.topGroup.rebuild(details);
  3181. this.s.topGroup.s.preventRedraw = false;
  3182. this.s.topGroup.redrawContents();
  3183. this.s.dt.draw(false);
  3184. this.s.topGroup.setListeners();
  3185. return this;
  3186. };
  3187. /**
  3188. * Applies the defaults to preDefined criteria
  3189. *
  3190. * @param preDef the array of criteria to be processed.
  3191. */
  3192. SearchBuilder.prototype._applyPreDefDefaults = function (preDef) {
  3193. var _this = this;
  3194. if (preDef.criteria !== undefined && preDef.logic === undefined) {
  3195. preDef.logic = 'AND';
  3196. }
  3197. var _loop_1 = function (crit) {
  3198. // Apply the defaults to any further criteria
  3199. if (crit.criteria !== undefined) {
  3200. crit = this_1._applyPreDefDefaults(crit);
  3201. }
  3202. else {
  3203. this_1.s.dt.columns().every(function (index) {
  3204. if (_this.s.dt.settings()[0].aoColumns[index].sTitle === crit.data) {
  3205. crit.dataIdx = index;
  3206. }
  3207. });
  3208. }
  3209. };
  3210. var this_1 = this;
  3211. for (var _i = 0, _a = preDef.criteria; _i < _a.length; _i++) {
  3212. var crit = _a[_i];
  3213. _loop_1(crit);
  3214. }
  3215. return preDef;
  3216. };
  3217. /**
  3218. * Set's up the SearchBuilder
  3219. */
  3220. SearchBuilder.prototype._setUp = function (loadState) {
  3221. var _this = this;
  3222. if (loadState === void 0) { loadState = true; }
  3223. // Register an Api method for getting the column type
  3224. $.fn.DataTable.Api.registerPlural('columns().type()', 'column().type()', function () {
  3225. return this.iterator('column', function (settings, column) {
  3226. return settings.aoColumns[column].sType;
  3227. }, 1);
  3228. });
  3229. // Check that DateTime is included, If not need to check if it could be used
  3230. // eslint-disable-next-line no-extra-parens
  3231. if (!dataTable.DateTime) {
  3232. var types = this.s.dt.columns().type().toArray();
  3233. if (types === undefined || types.includes(undefined) || types.includes(null)) {
  3234. types = [];
  3235. for (var _i = 0, _a = this.s.dt.settings()[0].aoColumns; _i < _a.length; _i++) {
  3236. var colInit = _a[_i];
  3237. types.push(colInit.searchBuilderType !== undefined ? colInit.searchBuilderType : colInit.sType);
  3238. }
  3239. }
  3240. var columnIdxs = this.s.dt.columns().toArray();
  3241. // If the types are not yet set then draw to see if they can be retrieved then
  3242. if (types === undefined || types.includes(undefined) || types.includes(null)) {
  3243. $.fn.dataTable.ext.oApi._fnColumnTypes(this.s.dt.settings()[0]);
  3244. types = this.s.dt.columns().type().toArray();
  3245. }
  3246. for (var i = 0; i < columnIdxs[0].length; i++) {
  3247. var column = columnIdxs[0][i];
  3248. var type = types[column];
  3249. if (
  3250. // Check if this column can be filtered
  3251. (this.c.columns === true ||
  3252. Array.isArray(this.c.columns) &&
  3253. this.c.columns.includes(i)) &&
  3254. // Check if the type is one of the restricted types
  3255. (type.includes('date') ||
  3256. type.includes('moment') ||
  3257. type.includes('luxon'))) {
  3258. alert('SearchBuilder Requires DateTime when used with dates.');
  3259. throw new Error('SearchBuilder requires DateTime');
  3260. }
  3261. }
  3262. }
  3263. this.s.topGroup = new Group(this.s.dt, this.c, undefined);
  3264. this._setClearListener();
  3265. this.s.dt.on('stateSaveParams', function (e, settings, data) {
  3266. data.searchBuilder = _this.getDetails();
  3267. data.page = _this.s.dt.page();
  3268. });
  3269. this._build();
  3270. this.s.dt.on('preXhr', function (e, settings, data) {
  3271. if (_this.s.dt.page.info().serverSide) {
  3272. data.searchBuilder = _this._collapseArray(_this.getDetails(true));
  3273. }
  3274. });
  3275. if (loadState) {
  3276. var loadedState = this.s.dt.state.loaded();
  3277. // If the loaded State is not null rebuild based on it for statesave
  3278. if (loadedState !== null && loadedState.searchBuilder !== undefined) {
  3279. this.s.topGroup.rebuild(loadedState.searchBuilder);
  3280. this.s.topGroup.dom.container.trigger('dtsb-redrawContents');
  3281. this.s.dt.page(loadedState.page).draw('page');
  3282. this.s.topGroup.setListeners();
  3283. }
  3284. // Otherwise load any predefined options
  3285. else if (this.c.preDefined !== false) {
  3286. this.c.preDefined = this._applyPreDefDefaults(this.c.preDefined);
  3287. this.rebuild(this.c.preDefined);
  3288. }
  3289. }
  3290. this._setEmptyListener();
  3291. this.s.dt.state.save();
  3292. };
  3293. SearchBuilder.prototype._collapseArray = function (criteria) {
  3294. if (criteria.logic === undefined) {
  3295. if (criteria.value !== undefined) {
  3296. criteria.value.sort(function (a, b) {
  3297. if (!isNaN(+a)) {
  3298. a = +a;
  3299. b = +b;
  3300. }
  3301. if (a < b) {
  3302. return -1;
  3303. }
  3304. else if (b < a) {
  3305. return 1;
  3306. }
  3307. else {
  3308. return 0;
  3309. }
  3310. });
  3311. criteria.value1 = criteria.value[0];
  3312. criteria.value2 = criteria.value[1];
  3313. }
  3314. }
  3315. else {
  3316. for (var i = 0; i < criteria.criteria.length; i++) {
  3317. criteria.criteria[i] = this._collapseArray(criteria.criteria[i]);
  3318. }
  3319. }
  3320. return criteria;
  3321. };
  3322. /**
  3323. * Updates the title of the SearchBuilder
  3324. *
  3325. * @param count the number of filters in the SearchBuilder
  3326. */
  3327. SearchBuilder.prototype._updateTitle = function (count) {
  3328. this.dom.title.html(this.s.dt.i18n('searchBuilder.title', this.c.i18n.title, count));
  3329. };
  3330. /**
  3331. * Builds all of the dom elements together
  3332. */
  3333. SearchBuilder.prototype._build = function () {
  3334. var _this = this;
  3335. // Empty and setup the container
  3336. this.dom.clearAll.remove();
  3337. this.dom.container.empty();
  3338. var count = this.s.topGroup.count();
  3339. this._updateTitle(count);
  3340. this.dom.titleRow.append(this.dom.title);
  3341. this.dom.container.append(this.dom.titleRow);
  3342. this.dom.topGroup = this.s.topGroup.getNode();
  3343. this.dom.container.append(this.dom.topGroup);
  3344. this._setRedrawListener();
  3345. var tableNode = this.s.dt.table(0).node();
  3346. if (!$.fn.dataTable.ext.search.includes(this.s.search)) {
  3347. // Custom search function for SearchBuilder
  3348. this.s.search = function (settings, searchData, dataIndex) {
  3349. if (settings.nTable !== tableNode) {
  3350. return true;
  3351. }
  3352. return _this.s.topGroup.search(searchData, dataIndex);
  3353. };
  3354. // Add SearchBuilder search function to the dataTables search array
  3355. $.fn.dataTable.ext.search.push(this.s.search);
  3356. }
  3357. this.s.dt.on('destroy.dt', function () {
  3358. _this.dom.container.remove();
  3359. _this.dom.clearAll.remove();
  3360. var searchIdx = $.fn.dataTable.ext.search.indexOf(_this.s.search);
  3361. while (searchIdx !== -1) {
  3362. $.fn.dataTable.ext.search.splice(searchIdx, 1);
  3363. searchIdx = $.fn.dataTable.ext.search.indexOf(_this.s.search);
  3364. }
  3365. });
  3366. };
  3367. /**
  3368. * Checks if the clearAll button should be added or not
  3369. */
  3370. SearchBuilder.prototype._checkClear = function () {
  3371. if (this.s.topGroup.s.criteria.length > 0) {
  3372. this.dom.clearAll.insertAfter(this.dom.title);
  3373. this._setClearListener();
  3374. }
  3375. else {
  3376. this.dom.clearAll.remove();
  3377. }
  3378. };
  3379. /**
  3380. * Update the count in the title/button
  3381. *
  3382. * @param count Number of filters applied
  3383. */
  3384. SearchBuilder.prototype._filterChanged = function (count) {
  3385. var fn = this.c.filterChanged;
  3386. if (typeof fn === 'function') {
  3387. fn(count, this.s.dt.i18n('searchBuilder.button', this.c.i18n.button, count));
  3388. }
  3389. };
  3390. /**
  3391. * Set the listener for the clear button
  3392. */
  3393. SearchBuilder.prototype._setClearListener = function () {
  3394. var _this = this;
  3395. this.dom.clearAll.unbind('click');
  3396. this.dom.clearAll.on('click', function () {
  3397. _this.s.topGroup = new Group(_this.s.dt, _this.c, undefined);
  3398. _this._build();
  3399. _this.s.dt.draw();
  3400. _this.s.topGroup.setListeners();
  3401. _this.dom.clearAll.remove();
  3402. _this._setEmptyListener();
  3403. _this._filterChanged(0);
  3404. return false;
  3405. });
  3406. };
  3407. /**
  3408. * Set the listener for the Redraw event
  3409. */
  3410. SearchBuilder.prototype._setRedrawListener = function () {
  3411. var _this = this;
  3412. this.s.topGroup.dom.container.unbind('dtsb-redrawContents');
  3413. this.s.topGroup.dom.container.on('dtsb-redrawContents', function () {
  3414. _this._checkClear();
  3415. _this.s.topGroup.redrawContents();
  3416. _this.s.topGroup.setupLogic();
  3417. _this._setEmptyListener();
  3418. var count = _this.s.topGroup.count();
  3419. _this._updateTitle(count);
  3420. _this._filterChanged(count);
  3421. _this.s.dt.draw();
  3422. _this.s.dt.state.save();
  3423. });
  3424. this.s.topGroup.dom.container.unbind('dtsb-redrawLogic');
  3425. this.s.topGroup.dom.container.on('dtsb-redrawLogic', function () {
  3426. _this.s.topGroup.redrawLogic();
  3427. var count = _this.s.topGroup.count();
  3428. _this._updateTitle(count);
  3429. _this._filterChanged(count);
  3430. });
  3431. this.s.topGroup.dom.container.unbind('dtsb-add');
  3432. this.s.topGroup.dom.container.on('dtsb-add', function () {
  3433. var count = _this.s.topGroup.count();
  3434. _this._updateTitle(count);
  3435. _this._filterChanged(count);
  3436. });
  3437. this.s.dt.on('postEdit postCreate postRemove', function () {
  3438. _this.s.topGroup.redrawContents();
  3439. });
  3440. this.s.topGroup.dom.container.unbind('dtsb-clearContents');
  3441. this.s.topGroup.dom.container.on('dtsb-clearContents', function () {
  3442. _this._setUp(false);
  3443. _this._filterChanged(0);
  3444. _this.s.dt.draw();
  3445. });
  3446. this.s.topGroup.dom.container.on('dtsb-updateTitle', function () {
  3447. var count = _this.s.topGroup.count();
  3448. _this._updateTitle(count);
  3449. _this._filterChanged(count);
  3450. });
  3451. };
  3452. /**
  3453. * Sets listeners to check whether clearAll should be added or removed
  3454. */
  3455. SearchBuilder.prototype._setEmptyListener = function () {
  3456. var _this = this;
  3457. this.s.topGroup.dom.add.on('click', function () {
  3458. _this._checkClear();
  3459. });
  3460. this.s.topGroup.dom.container.on('dtsb-destroy', function () {
  3461. _this.dom.clearAll.remove();
  3462. });
  3463. };
  3464. SearchBuilder.version = '1.2.1';
  3465. SearchBuilder.classes = {
  3466. button: 'dtsb-button',
  3467. clearAll: 'dtsb-clearAll',
  3468. container: 'dtsb-searchBuilder',
  3469. inputButton: 'dtsb-iptbtn',
  3470. title: 'dtsb-title',
  3471. titleRow: 'dtsb-titleRow'
  3472. };
  3473. SearchBuilder.defaults = {
  3474. columns: true,
  3475. conditions: {
  3476. 'date': Criteria.dateConditions,
  3477. 'html': Criteria.stringConditions,
  3478. 'html-num': Criteria.numConditions,
  3479. 'html-num-fmt': Criteria.numFmtConditions,
  3480. 'luxon': Criteria.luxonDateConditions,
  3481. 'moment': Criteria.momentDateConditions,
  3482. 'num': Criteria.numConditions,
  3483. 'num-fmt': Criteria.numFmtConditions,
  3484. 'string': Criteria.stringConditions
  3485. },
  3486. depthLimit: false,
  3487. enterSearch: false,
  3488. filterChanged: undefined,
  3489. greyscale: false,
  3490. i18n: {
  3491. add: 'Add Condition',
  3492. button: {
  3493. 0: 'Search Builder',
  3494. _: 'Search Builder (%d)'
  3495. },
  3496. clearAll: 'Clear All',
  3497. condition: 'Condition',
  3498. conditions: {
  3499. array: {
  3500. contains: 'Contains',
  3501. empty: 'Empty',
  3502. equals: 'Equals',
  3503. not: 'Not',
  3504. notEmpty: 'Not Empty',
  3505. without: 'Without'
  3506. },
  3507. date: {
  3508. after: 'After',
  3509. before: 'Before',
  3510. between: 'Between',
  3511. empty: 'Empty',
  3512. equals: 'Equals',
  3513. not: 'Not',
  3514. notBetween: 'Not Between',
  3515. notEmpty: 'Not Empty'
  3516. },
  3517. // eslint-disable-next-line id-blacklist
  3518. number: {
  3519. between: 'Between',
  3520. empty: 'Empty',
  3521. equals: 'Equals',
  3522. gt: 'Greater Than',
  3523. gte: 'Greater Than Equal To',
  3524. lt: 'Less Than',
  3525. lte: 'Less Than Equal To',
  3526. not: 'Not',
  3527. notBetween: 'Not Between',
  3528. notEmpty: 'Not Empty'
  3529. },
  3530. // eslint-disable-next-line id-blacklist
  3531. string: {
  3532. contains: 'Contains',
  3533. empty: 'Empty',
  3534. endsWith: 'Ends With',
  3535. equals: 'Equals',
  3536. not: 'Not',
  3537. notEmpty: 'Not Empty',
  3538. startsWith: 'Starts With'
  3539. }
  3540. },
  3541. data: 'Data',
  3542. deleteTitle: 'Delete filtering rule',
  3543. leftTitle: 'Outdent criteria',
  3544. logicAnd: 'And',
  3545. logicOr: 'Or',
  3546. rightTitle: 'Indent criteria',
  3547. title: {
  3548. 0: 'Custom Search Builder',
  3549. _: 'Custom Search Builder (%d)'
  3550. },
  3551. value: 'Value',
  3552. valueJoiner: 'and'
  3553. },
  3554. logic: 'AND',
  3555. orthogonal: {
  3556. display: 'display',
  3557. search: 'filter'
  3558. },
  3559. preDefined: false
  3560. };
  3561. return SearchBuilder;
  3562. }());
  3563. /*! SearchBuilder 1.2.1
  3564. * ©SpryMedia Ltd - datatables.net/license/mit
  3565. */
  3566. // DataTables extensions common UMD. Note that this allows for AMD, CommonJS
  3567. // (with window and jQuery being allowed as parameters to the returned
  3568. // function) or just default browser loading.
  3569. (function (factory) {
  3570. if (typeof define === 'function' && define.amd) {
  3571. // AMD
  3572. define(['jquery', 'datatables.net'], function ($) {
  3573. return factory($, window, document);
  3574. });
  3575. }
  3576. else if (typeof exports === 'object') {
  3577. // CommonJS
  3578. module.exports = function (root, $) {
  3579. if (!root) {
  3580. root = window;
  3581. }
  3582. if (!$ || !$.fn.dataTable) {
  3583. // eslint-disable-next-line @typescript-eslint/no-var-requires
  3584. $ = require('datatables.net')(root, $).$;
  3585. }
  3586. return factory($, root, root.document);
  3587. };
  3588. }
  3589. else {
  3590. // Browser - assume jQuery has already been loaded
  3591. // eslint-disable-next-line no-extra-parens
  3592. factory(window.jQuery, window, document);
  3593. }
  3594. }(function ($, window, document) {
  3595. setJQuery($);
  3596. setJQuery$1($);
  3597. setJQuery$2($);
  3598. var dataTable = $.fn.dataTable;
  3599. // eslint-disable-next-line no-extra-parens
  3600. $.fn.dataTable.SearchBuilder = SearchBuilder;
  3601. // eslint-disable-next-line no-extra-parens
  3602. $.fn.DataTable.SearchBuilder = SearchBuilder;
  3603. // eslint-disable-next-line no-extra-parens
  3604. $.fn.dataTable.Group = Group;
  3605. // eslint-disable-next-line no-extra-parens
  3606. $.fn.DataTable.Group = Group;
  3607. // eslint-disable-next-line no-extra-parens
  3608. $.fn.dataTable.Criteria = Criteria;
  3609. // eslint-disable-next-line no-extra-parens
  3610. $.fn.DataTable.Criteria = Criteria;
  3611. // eslint-disable-next-line no-extra-parens
  3612. var apiRegister = $.fn.dataTable.Api.register;
  3613. // Set up object for plugins
  3614. $.fn.dataTable.ext.searchBuilder = {
  3615. conditions: {}
  3616. };
  3617. $.fn.dataTable.ext.buttons.searchBuilder = {
  3618. action: function (e, dt, node, config) {
  3619. this.popover(config._searchBuilder.getNode(), {
  3620. align: 'dt-container'
  3621. });
  3622. // Need to redraw the contents to calculate the correct positions for the elements
  3623. if (config._searchBuilder.s.topGroup !== undefined) {
  3624. config._searchBuilder.s.topGroup.dom.container.trigger('dtsb-redrawContents');
  3625. }
  3626. },
  3627. config: {},
  3628. init: function (dt, node, config) {
  3629. var sb = new $.fn.dataTable.SearchBuilder(dt, $.extend({
  3630. filterChanged: function (count, text) {
  3631. dt.button(node).text(text);
  3632. }
  3633. }, config.config));
  3634. dt.button(node).text(config.text || dt.i18n('searchBuilder.button', sb.c.i18n.button, 0));
  3635. config._searchBuilder = sb;
  3636. },
  3637. text: null
  3638. };
  3639. apiRegister('searchBuilder.getDetails()', function (deFormatDates) {
  3640. if (deFormatDates === void 0) { deFormatDates = false; }
  3641. var ctx = this.context[0];
  3642. // If SearchBuilder has not been initialised on this instance then return
  3643. return ctx._searchBuilder ?
  3644. ctx._searchBuilder.getDetails(deFormatDates) :
  3645. null;
  3646. });
  3647. apiRegister('searchBuilder.rebuild()', function (details) {
  3648. var ctx = this.context[0];
  3649. // If SearchBuilder has not been initialised on this instance then return
  3650. if (ctx._searchBuilder === undefined) {
  3651. return null;
  3652. }
  3653. ctx._searchBuilder.rebuild(details);
  3654. return this;
  3655. });
  3656. apiRegister('searchBuilder.container()', function () {
  3657. var ctx = this.context[0];
  3658. // If SearchBuilder has not been initialised on this instance then return
  3659. return ctx._searchBuilder ?
  3660. ctx._searchBuilder.getNode() :
  3661. null;
  3662. });
  3663. /**
  3664. * Init function for SearchBuilder
  3665. *
  3666. * @param settings the settings to be applied
  3667. * @param options the options for SearchBuilder
  3668. * @returns JQUERY<HTMLElement> Returns the node of the SearchBuilder
  3669. */
  3670. function _init(settings, options) {
  3671. var api = new dataTable.Api(settings);
  3672. var opts = options
  3673. ? options
  3674. : api.init().searchBuilder || dataTable.defaults.searchBuilder;
  3675. var searchBuilder = new SearchBuilder(api, opts);
  3676. var node = searchBuilder.getNode();
  3677. return node;
  3678. }
  3679. // Attach a listener to the document which listens for DataTables initialisation
  3680. // events so we can automatically initialise
  3681. $(document).on('preInit.dt.dtsp', function (e, settings) {
  3682. if (e.namespace !== 'dt') {
  3683. return;
  3684. }
  3685. if (settings.oInit.searchBuilder ||
  3686. dataTable.defaults.searchBuilder) {
  3687. if (!settings._searchBuilder) {
  3688. _init(settings);
  3689. }
  3690. }
  3691. });
  3692. // DataTables `dom` feature option
  3693. dataTable.ext.feature.push({
  3694. cFeature: 'Q',
  3695. fnInit: _init
  3696. });
  3697. // DataTables 2 layout feature
  3698. if (dataTable.ext.features) {
  3699. dataTable.ext.features.register('searchBuilder', _init);
  3700. }
  3701. }));
  3702. }());