dataTables.searchPanes.js 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624
  1. /*! SearchPanes 1.2.1
  2. * 2019-2020 SpryMedia Ltd - datatables.net/license
  3. */
  4. (function () {
  5. 'use strict';
  6. var $;
  7. var DataTable;
  8. function setJQuery(jq) {
  9. $ = jq;
  10. DataTable = jq.fn.dataTable;
  11. }
  12. var SearchPane = /** @class */ (function () {
  13. /**
  14. * Creates the panes, sets up the search function
  15. * @param paneSettings The settings for the searchPanes
  16. * @param opts The options for the default features
  17. * @param idx the index of the column for this pane
  18. * @returns {object} the pane that has been created, including the table and the index of the pane
  19. */
  20. function SearchPane(paneSettings, opts, idx, layout, panesContainer, panes) {
  21. var _this = this;
  22. if (panes === void 0) { panes = null; }
  23. // Check that the required version of DataTables is included
  24. if (!DataTable || !DataTable.versionCheck || !DataTable.versionCheck('1.10.0')) {
  25. throw new Error('SearchPane requires DataTables 1.10 or newer');
  26. }
  27. // Check that Select is included
  28. if (!DataTable.select) {
  29. throw new Error('SearchPane requires Select');
  30. }
  31. var table = new DataTable.Api(paneSettings);
  32. this.classes = $.extend(true, {}, SearchPane.classes);
  33. // Get options from user
  34. this.c = $.extend(true, {}, SearchPane.defaults, opts);
  35. this.customPaneSettings = panes;
  36. this.s = {
  37. cascadeRegen: false,
  38. clearing: false,
  39. colOpts: [],
  40. deselect: false,
  41. displayed: false,
  42. dt: table,
  43. dtPane: undefined,
  44. filteringActive: false,
  45. index: idx,
  46. indexes: [],
  47. lastCascade: false,
  48. lastSelect: false,
  49. listSet: false,
  50. name: undefined,
  51. redraw: false,
  52. rowData: {
  53. arrayFilter: [],
  54. arrayOriginal: [],
  55. arrayTotals: [],
  56. bins: {},
  57. binsOriginal: {},
  58. binsTotal: {},
  59. filterMap: new Map(),
  60. totalOptions: 0
  61. },
  62. scrollTop: 0,
  63. searchFunction: undefined,
  64. selectPresent: false,
  65. serverSelect: [],
  66. serverSelecting: false,
  67. showFiltered: false,
  68. tableLength: null,
  69. updating: false
  70. };
  71. var rowLength = table.columns().eq(0).toArray().length;
  72. this.colExists = this.s.index < rowLength;
  73. // Add extra elements to DOM object including clear and hide buttons
  74. this.c.layout = layout;
  75. var layVal = parseInt(layout.split('-')[1], 10);
  76. this.dom = {
  77. buttonGroup: $('<div/>').addClass(this.classes.buttonGroup),
  78. clear: $('<button type="button">&#215;</button>')
  79. .addClass(this.classes.dull)
  80. .addClass(this.classes.paneButton)
  81. .addClass(this.classes.clearButton),
  82. container: $('<div/>').addClass(this.classes.container).addClass(this.classes.layout +
  83. (layVal < 10 ? layout : layout.split('-')[0] + '-9')),
  84. countButton: $('<button type="button"></button>')
  85. .addClass(this.classes.paneButton)
  86. .addClass(this.classes.countButton),
  87. dtP: $('<table><thead><tr><th>' +
  88. (this.colExists
  89. ? $(table.column(this.colExists ? this.s.index : 0).header()).text()
  90. : this.customPaneSettings.header || 'Custom Pane') + '</th><th/></tr></thead></table>'),
  91. lower: $('<div/>').addClass(this.classes.subRow2).addClass(this.classes.narrowButton),
  92. nameButton: $('<button type="button"></button>').addClass(this.classes.paneButton).addClass(this.classes.nameButton),
  93. panesContainer: panesContainer,
  94. searchBox: $('<input/>').addClass(this.classes.paneInputButton).addClass(this.classes.search),
  95. searchButton: $('<button type = "button" class="' + this.classes.searchIcon + '"></button>')
  96. .addClass(this.classes.paneButton),
  97. searchCont: $('<div/>').addClass(this.classes.searchCont),
  98. searchLabelCont: $('<div/>').addClass(this.classes.searchLabelCont),
  99. topRow: $('<div/>').addClass(this.classes.topRow),
  100. upper: $('<div/>').addClass(this.classes.subRow1).addClass(this.classes.narrowSearch)
  101. };
  102. this.s.displayed = false;
  103. table = this.s.dt;
  104. this.selections = [];
  105. this.s.colOpts = this.colExists ? this._getOptions() : this._getBonusOptions();
  106. var colOpts = this.s.colOpts;
  107. var clear = $('<button type="button">X</button>').addClass(this.classes.paneButton);
  108. $(clear).text(table.i18n('searchPanes.clearPane', 'X'));
  109. this.dom.container.addClass(colOpts.className);
  110. this.dom.container.addClass((this.customPaneSettings !== null && this.customPaneSettings.className !== undefined)
  111. ? this.customPaneSettings.className
  112. : '');
  113. // Set the value of name incase ordering is desired
  114. if (this.s.colOpts.name !== undefined) {
  115. this.s.name = this.s.colOpts.name;
  116. }
  117. else if (this.customPaneSettings !== null && this.customPaneSettings.name !== undefined) {
  118. this.s.name = this.customPaneSettings.name;
  119. }
  120. else {
  121. this.s.name = this.colExists ?
  122. $(table.column(this.s.index).header()).text() :
  123. this.customPaneSettings.header || 'Custom Pane';
  124. }
  125. $(panesContainer).append(this.dom.container);
  126. var tableNode = table.table(0).node();
  127. // Custom search function for table
  128. this.s.searchFunction = function (settings, searchData, dataIndex, origData) {
  129. // If no data has been selected then show all
  130. if (_this.selections.length === 0) {
  131. return true;
  132. }
  133. if (settings.nTable !== tableNode) {
  134. return true;
  135. }
  136. var filter = null;
  137. if (_this.colExists) {
  138. // Get the current filtered data
  139. filter = searchData[_this.s.index];
  140. if (colOpts.orthogonal.filter !== 'filter') {
  141. // get the filter value from the map
  142. filter = _this.s.rowData.filterMap.get(dataIndex);
  143. if (filter instanceof $.fn.dataTable.Api) {
  144. filter = filter.toArray();
  145. }
  146. }
  147. }
  148. return _this._search(filter, dataIndex);
  149. };
  150. $.fn.dataTable.ext.search.push(this.s.searchFunction);
  151. // If the clear button for this pane is clicked clear the selections
  152. if (this.c.clear) {
  153. $(clear).on('click', function () {
  154. var searches = _this.dom.container.find(_this.classes.search);
  155. searches.each(function () {
  156. $(this).val('');
  157. $(this).trigger('input');
  158. });
  159. _this.clearPane();
  160. });
  161. }
  162. // Sometimes the top row of the panes containing the search box and ordering buttons appears
  163. // weird if the width of the panes is lower than expected, this fixes the design.
  164. // Equally this may occur when the table is resized.
  165. table.on('draw.dtsp', function () {
  166. _this._adjustTopRow();
  167. });
  168. table.on('buttons-action', function () {
  169. _this._adjustTopRow();
  170. });
  171. $(window).on('resize.dtsp', DataTable.util.throttle(function () {
  172. _this._adjustTopRow();
  173. }));
  174. // When column-reorder is present and the columns are moved, it is necessary to
  175. // reassign all of the panes indexes to the new index of the column.
  176. table.on('column-reorder.dtsp', function (e, settings, details) {
  177. _this.s.index = details.mapping[_this.s.index];
  178. });
  179. return this;
  180. }
  181. /**
  182. * In the case of a rebuild there is potential for new data to have been included or removed
  183. * so all of the rowData must be reset as a precaution.
  184. */
  185. SearchPane.prototype.clearData = function () {
  186. this.s.rowData = {
  187. arrayFilter: [],
  188. arrayOriginal: [],
  189. arrayTotals: [],
  190. bins: {},
  191. binsOriginal: {},
  192. binsTotal: {},
  193. filterMap: new Map(),
  194. totalOptions: 0
  195. };
  196. };
  197. /**
  198. * Clear the selections in the pane
  199. */
  200. SearchPane.prototype.clearPane = function () {
  201. // Deselect all rows which are selected and update the table and filter count.
  202. this.s.dtPane.rows({ selected: true }).deselect();
  203. this.updateTable();
  204. return this;
  205. };
  206. /**
  207. * Strips all of the SearchPanes elements from the document and turns all of the listeners for the buttons off
  208. */
  209. SearchPane.prototype.destroy = function () {
  210. $(this.s.dtPane).off('.dtsp');
  211. $(this.s.dt).off('.dtsp');
  212. $(this.dom.nameButton).off('.dtsp');
  213. $(this.dom.countButton).off('.dtsp');
  214. $(this.dom.clear).off('.dtsp');
  215. $(this.dom.searchButton).off('.dtsp');
  216. $(this.dom.container).remove();
  217. var searchIdx = $.fn.dataTable.ext.search.indexOf(this.s.searchFunction);
  218. while (searchIdx !== -1) {
  219. $.fn.dataTable.ext.search.splice(searchIdx, 1);
  220. searchIdx = $.fn.dataTable.ext.search.indexOf(this.s.searchFunction);
  221. }
  222. // If the datatables have been defined for the panes then also destroy these
  223. if (this.s.dtPane !== undefined) {
  224. this.s.dtPane.destroy();
  225. }
  226. this.s.listSet = false;
  227. };
  228. /**
  229. * Updates the number of filters that have been applied in the title
  230. */
  231. SearchPane.prototype.getPaneCount = function () {
  232. return this.s.dtPane !== undefined ?
  233. this.s.dtPane.rows({ selected: true }).data().toArray().length :
  234. 0;
  235. };
  236. /**
  237. * Rebuilds the panes from the start having deleted the old ones
  238. * @param? last boolean to indicate if this is the last pane a selection was made in
  239. * @param? dataIn data to be used in buildPane
  240. * @param? init Whether this is the initial draw or not
  241. * @param? maintainSelection Whether the current selections are to be maintained over rebuild
  242. */
  243. SearchPane.prototype.rebuildPane = function (last, dataIn, init, maintainSelection) {
  244. if (last === void 0) { last = false; }
  245. if (dataIn === void 0) { dataIn = null; }
  246. if (init === void 0) { init = null; }
  247. if (maintainSelection === void 0) { maintainSelection = false; }
  248. this.clearData();
  249. var selectedRows = [];
  250. this.s.serverSelect = [];
  251. var prevEl = null;
  252. // When rebuilding strip all of the HTML Elements out of the container and start from scratch
  253. if (this.s.dtPane !== undefined) {
  254. if (maintainSelection) {
  255. if (!this.s.dt.page.info().serverSide) {
  256. selectedRows = this.s.dtPane.rows({ selected: true }).data().toArray();
  257. }
  258. else {
  259. this.s.serverSelect = this.s.dtPane.rows({ selected: true }).data().toArray();
  260. }
  261. }
  262. this.s.dtPane.clear().destroy();
  263. prevEl = $(this.dom.container).prev();
  264. this.destroy();
  265. this.s.dtPane = undefined;
  266. $.fn.dataTable.ext.search.push(this.s.searchFunction);
  267. }
  268. this.dom.container.removeClass(this.classes.hidden);
  269. this.s.displayed = false;
  270. this._buildPane(!this.s.dt.page.info().serverSide ?
  271. selectedRows :
  272. this.s.serverSelect, last, dataIn, init, prevEl);
  273. return this;
  274. };
  275. /**
  276. * removes the pane from the page and sets the displayed property to false.
  277. */
  278. SearchPane.prototype.removePane = function () {
  279. this.s.displayed = false;
  280. $(this.dom.container).hide();
  281. };
  282. /**
  283. * Sets the cascadeRegen property of the pane. Accessible from above because as SearchPanes.ts deals with the rebuilds.
  284. * @param val the boolean value that the cascadeRegen property is to be set to
  285. */
  286. SearchPane.prototype.setCascadeRegen = function (val) {
  287. this.s.cascadeRegen = val;
  288. };
  289. /**
  290. * This function allows the clearing property to be assigned. This is used when implementing cascadePane.
  291. * In setting this to true for the clearing of the panes selection on the deselects it forces the pane to
  292. * repopulate from the entire dataset not just the displayed values.
  293. * @param val the boolean value which the clearing property is to be assigned
  294. */
  295. SearchPane.prototype.setClear = function (val) {
  296. this.s.clearing = val;
  297. };
  298. /**
  299. * Updates the values of all of the panes
  300. * @param draw whether this has been triggered by a draw event or not
  301. */
  302. SearchPane.prototype.updatePane = function (draw) {
  303. if (draw === void 0) { draw = false; }
  304. this.s.updating = true;
  305. this._updateCommon(draw);
  306. this.s.updating = false;
  307. };
  308. /**
  309. * Updates the panes if one of the options to do so has been set to true
  310. * rather than the filtered message when using viewTotal.
  311. */
  312. SearchPane.prototype.updateTable = function () {
  313. var selectedRows = this.s.dtPane.rows({ selected: true }).data().toArray();
  314. this.selections = selectedRows;
  315. this._searchExtras();
  316. // If either of the options that effect how the panes are displayed are selected then update the Panes
  317. if (this.c.cascadePanes || this.c.viewTotal) {
  318. this.updatePane();
  319. }
  320. };
  321. /**
  322. * Sets the listeners for the pane.
  323. *
  324. * Having it in it's own function makes it easier to only set them once
  325. */
  326. SearchPane.prototype._setListeners = function () {
  327. var _this = this;
  328. var rowData = this.s.rowData;
  329. var t0;
  330. // When an item is selected on the pane, add these to the array which holds selected items.
  331. // Custom search will perform.
  332. this.s.dtPane.on('select.dtsp', function () {
  333. clearTimeout(t0);
  334. if (_this.s.dt.page.info().serverSide && !_this.s.updating) {
  335. if (!_this.s.serverSelecting) {
  336. _this.s.serverSelect = _this.s.dtPane.rows({ selected: true }).data().toArray();
  337. _this.s.scrollTop = $(_this.s.dtPane.table().node()).parent()[0].scrollTop;
  338. _this.s.selectPresent = true;
  339. _this.s.dt.draw(false);
  340. }
  341. }
  342. else {
  343. $(_this.dom.clear).removeClass(_this.classes.dull);
  344. _this.s.selectPresent = true;
  345. if (!_this.s.updating) {
  346. _this._makeSelection();
  347. }
  348. _this.s.selectPresent = false;
  349. }
  350. });
  351. // When an item is deselected on the pane, re add the currently selected items to the array
  352. // which holds selected items. Custom search will be performed.
  353. this.s.dtPane.on('deselect.dtsp', function () {
  354. t0 = setTimeout(function () {
  355. if (_this.s.dt.page.info().serverSide && !_this.s.updating) {
  356. if (!_this.s.serverSelecting) {
  357. _this.s.serverSelect = _this.s.dtPane.rows({ selected: true }).data().toArray();
  358. _this.s.deselect = true;
  359. _this.s.dt.draw(false);
  360. }
  361. }
  362. else {
  363. _this.s.deselect = true;
  364. if (_this.s.dtPane.rows({ selected: true }).data().toArray().length === 0) {
  365. $(_this.dom.clear).addClass(_this.classes.dull);
  366. }
  367. _this._makeSelection();
  368. _this.s.deselect = false;
  369. _this.s.dt.state.save();
  370. }
  371. }, 50);
  372. });
  373. // When saving the state store all of the selected rows for preselection next time around
  374. this.s.dt.on('stateSaveParams.dtsp', function (e, settings, data) {
  375. // If the data being passed in is empty then a state clear must have occured so clear the panes state as well
  376. if ($.isEmptyObject(data)) {
  377. _this.s.dtPane.state.clear();
  378. return;
  379. }
  380. var selected = [];
  381. var searchTerm;
  382. var order;
  383. var bins;
  384. var arrayFilter;
  385. // Get all of the data needed for the state save from the pane
  386. if (_this.s.dtPane !== undefined) {
  387. selected = _this.s.dtPane.rows({ selected: true }).data().map(function (item) { return item.filter.toString(); }).toArray();
  388. searchTerm = $(_this.dom.searchBox).val();
  389. order = _this.s.dtPane.order();
  390. bins = rowData.binsOriginal;
  391. arrayFilter = rowData.arrayOriginal;
  392. }
  393. if (data.searchPanes === undefined) {
  394. data.searchPanes = {};
  395. }
  396. if (data.searchPanes.panes === undefined) {
  397. data.searchPanes.panes = [];
  398. }
  399. for (var i = 0; i < data.searchPanes.panes.length; i++) {
  400. if (data.searchPanes.panes[i].id === _this.s.index) {
  401. data.searchPanes.panes.splice(i, 1);
  402. i--;
  403. }
  404. }
  405. // Add the panes data to the state object
  406. data.searchPanes.panes.push({
  407. arrayFilter: arrayFilter,
  408. bins: bins,
  409. id: _this.s.index,
  410. order: order,
  411. searchTerm: searchTerm,
  412. selected: selected
  413. });
  414. });
  415. this.s.dtPane.on('user-select.dtsp', function (e, _dt, type, cell, originalEvent) {
  416. originalEvent.stopPropagation();
  417. });
  418. this.s.dtPane.on('draw.dtsp', function () {
  419. _this._adjustTopRow();
  420. });
  421. // When the button to order by the name of the options is clicked then
  422. // change the ordering to whatever it isn't currently
  423. $(this.dom.nameButton).on('click.dtsp', function () {
  424. var currentOrder = _this.s.dtPane.order()[0][1];
  425. _this.s.dtPane.order([0, currentOrder === 'asc' ? 'desc' : 'asc']).draw();
  426. _this.s.dt.state.save();
  427. });
  428. // When the button to order by the number of entries in the column is clicked then
  429. // change the ordering to whatever it isn't currently
  430. $(this.dom.countButton).on('click.dtsp', function () {
  431. var currentOrder = _this.s.dtPane.order()[0][1];
  432. _this.s.dtPane.order([1, currentOrder === 'asc' ? 'desc' : 'asc']).draw();
  433. _this.s.dt.state.save();
  434. });
  435. // When the clear button is clicked reset the pane
  436. $(this.dom.clear).on('click.dtsp', function () {
  437. var searches = _this.dom.container.find('.' + _this.classes.search);
  438. searches.each(function () {
  439. // set the value of the search box to be an empty string and then search on that, effectively reseting
  440. $(this).val('');
  441. $(this).trigger('input');
  442. });
  443. _this.clearPane();
  444. });
  445. // When the search button is clicked then draw focus to the search box
  446. $(this.dom.searchButton).on('click.dtsp', function () {
  447. $(_this.dom.searchBox).focus();
  448. });
  449. // When a character is inputted into the searchbox search the pane for matching values.
  450. // Doing it this way means that no button has to be clicked to trigger a search, it is done asynchronously
  451. $(this.dom.searchBox).on('input.dtsp', function () {
  452. _this.s.dtPane.search($(_this.dom.searchBox).val()).draw();
  453. _this.s.dt.state.save();
  454. });
  455. // Make sure to save the state once the pane has been built
  456. this.s.dt.state.save();
  457. return true;
  458. };
  459. /**
  460. * Takes in potentially undetected rows and adds them to the array if they are not yet featured
  461. * @param filter the filter value of the potential row
  462. * @param display the display value of the potential row
  463. * @param sort the sort value of the potential row
  464. * @param type the type value of the potential row
  465. * @param arrayFilter the array to be populated
  466. * @param bins the bins to be populated
  467. */
  468. SearchPane.prototype._addOption = function (filter, display, sort, type, arrayFilter, bins) {
  469. // If the filter is an array then take a note of this, and add the elements to the arrayFilter array
  470. if (Array.isArray(filter) || filter instanceof DataTable.Api) {
  471. // Convert to an array so that we can work with it
  472. if (filter instanceof DataTable.Api) {
  473. filter = filter.toArray();
  474. display = display.toArray();
  475. }
  476. if (filter.length === display.length) {
  477. for (var i = 0; i < filter.length; i++) {
  478. // If we haven't seen this row before add it
  479. if (!bins[filter[i]]) {
  480. bins[filter[i]] = 1;
  481. arrayFilter.push({
  482. display: display[i],
  483. filter: filter[i],
  484. sort: sort[i],
  485. type: type[i]
  486. });
  487. }
  488. // Otherwise just increment the count
  489. else {
  490. bins[filter[i]]++;
  491. }
  492. this.s.rowData.totalOptions++;
  493. }
  494. return;
  495. }
  496. else {
  497. throw new Error('display and filter not the same length');
  498. }
  499. }
  500. // If the values were affected by othogonal data and are not an array then check if it is already present
  501. else if (typeof this.s.colOpts.orthogonal === 'string') {
  502. if (!bins[filter]) {
  503. bins[filter] = 1;
  504. arrayFilter.push({
  505. display: display,
  506. filter: filter,
  507. sort: sort,
  508. type: type
  509. });
  510. this.s.rowData.totalOptions++;
  511. }
  512. else {
  513. bins[filter]++;
  514. this.s.rowData.totalOptions++;
  515. return;
  516. }
  517. }
  518. // Otherwise we must just be adding an option
  519. else {
  520. arrayFilter.push({
  521. display: display,
  522. filter: filter,
  523. sort: sort,
  524. type: type
  525. });
  526. }
  527. };
  528. /**
  529. * Adds a row to the panes table
  530. * @param display the value to be displayed to the user
  531. * @param filter the value to be filtered on when searchpanes is implemented
  532. * @param shown the number of rows in the table that are currently visible matching this criteria
  533. * @param total the total number of rows in the table that match this criteria
  534. * @param sort the value to be sorted in the pane table
  535. * @param type the value of which the type is to be derived from
  536. */
  537. SearchPane.prototype._addRow = function (display, filter, shown, total, sort, type, className) {
  538. var index;
  539. for (var _i = 0, _a = this.s.indexes; _i < _a.length; _i++) {
  540. var entry = _a[_i];
  541. if (entry.filter === filter) {
  542. index = entry.index;
  543. }
  544. }
  545. if (index === undefined) {
  546. index = this.s.indexes.length;
  547. this.s.indexes.push({ filter: filter, index: index });
  548. }
  549. return this.s.dtPane.row.add({
  550. className: className,
  551. display: display !== '' ?
  552. display :
  553. this.s.colOpts.emptyMessage !== false ?
  554. this.s.colOpts.emptyMessage :
  555. this.c.emptyMessage,
  556. filter: filter,
  557. index: index,
  558. shown: shown,
  559. sort: sort !== '' ?
  560. sort :
  561. this.s.colOpts.emptyMessage !== false ?
  562. this.s.colOpts.emptyMessage :
  563. this.c.emptyMessage,
  564. total: total,
  565. type: type
  566. });
  567. };
  568. /**
  569. * Adjusts the layout of the top row when the screen is resized
  570. */
  571. SearchPane.prototype._adjustTopRow = function () {
  572. var subContainers = this.dom.container.find('.' + this.classes.subRowsContainer);
  573. var subRow1 = this.dom.container.find('.dtsp-subRow1');
  574. var subRow2 = this.dom.container.find('.dtsp-subRow2');
  575. var topRow = this.dom.container.find('.' + this.classes.topRow);
  576. // If the width is 0 then it is safe to assume that the pane has not yet been displayed.
  577. // Even if it has, if the width is 0 it won't make a difference if it has the narrow class or not
  578. if (($(subContainers[0]).width() < 252 || $(topRow[0]).width() < 252) && $(subContainers[0]).width() !== 0) {
  579. $(subContainers[0]).addClass(this.classes.narrow);
  580. $(subRow1[0]).addClass(this.classes.narrowSub).removeClass(this.classes.narrowSearch);
  581. $(subRow2[0]).addClass(this.classes.narrowSub).removeClass(this.classes.narrowButton);
  582. }
  583. else {
  584. $(subContainers[0]).removeClass(this.classes.narrow);
  585. $(subRow1[0]).removeClass(this.classes.narrowSub).addClass(this.classes.narrowSearch);
  586. $(subRow2[0]).removeClass(this.classes.narrowSub).addClass(this.classes.narrowButton);
  587. }
  588. };
  589. /**
  590. * Method to construct the actual pane.
  591. * @param selectedRows previously selected Rows to be reselected
  592. * @last boolean to indicate whether this pane was the last one to have a selection made
  593. */
  594. SearchPane.prototype._buildPane = function (selectedRows, last, dataIn, init, prevEl) {
  595. var _this = this;
  596. if (selectedRows === void 0) { selectedRows = []; }
  597. if (last === void 0) { last = false; }
  598. if (dataIn === void 0) { dataIn = null; }
  599. if (init === void 0) { init = null; }
  600. if (prevEl === void 0) { prevEl = null; }
  601. // Aliases
  602. this.selections = [];
  603. var table = this.s.dt;
  604. var column = table.column(this.colExists ? this.s.index : 0);
  605. var colOpts = this.s.colOpts;
  606. var rowData = this.s.rowData;
  607. // Other Variables
  608. var countMessage = table.i18n('searchPanes.count', '{total}');
  609. var filteredMessage = table.i18n('searchPanes.countFiltered', '{shown} ({total})');
  610. var loadedFilter = table.state.loaded();
  611. // If the listeners have not been set yet then using the latest state may result in funny errors
  612. if (this.s.listSet) {
  613. loadedFilter = table.state();
  614. }
  615. // If it is not a custom pane in place
  616. if (this.colExists) {
  617. var idx = -1;
  618. if (loadedFilter && loadedFilter.searchPanes && loadedFilter.searchPanes.panes) {
  619. for (var i = 0; i < loadedFilter.searchPanes.panes.length; i++) {
  620. if (loadedFilter.searchPanes.panes[i].id === this.s.index) {
  621. idx = i;
  622. break;
  623. }
  624. }
  625. }
  626. // Perform checks that do not require populate pane to run
  627. if ((colOpts.show === false
  628. || (colOpts.show !== undefined && colOpts.show !== true)) &&
  629. idx === -1) {
  630. this.dom.container.addClass(this.classes.hidden);
  631. this.s.displayed = false;
  632. return false;
  633. }
  634. else if (colOpts.show === true || idx !== -1) {
  635. this.s.displayed = true;
  636. }
  637. if (!this.s.dt.page.info().serverSide &&
  638. (dataIn === null ||
  639. dataIn.searchPanes === null ||
  640. dataIn.searchPanes.options === null)) {
  641. // Only run populatePane if the data has not been collected yet
  642. if (rowData.arrayFilter.length === 0) {
  643. this._populatePane(last);
  644. this.s.rowData.totalOptions = 0;
  645. this._detailsPane();
  646. // If the index is not found then no data has been added to the state for this pane,
  647. // which will only occur if it has previously failed to meet the criteria to be
  648. // displayed, therefore we can just hide it again here
  649. if (loadedFilter && loadedFilter.searchPanes && loadedFilter.searchPanes.panes && idx === -1) {
  650. this.dom.container.addClass(this.classes.hidden);
  651. this.s.displayed = false;
  652. return;
  653. }
  654. rowData.arrayOriginal = rowData.arrayTotals;
  655. rowData.binsOriginal = rowData.binsTotal;
  656. }
  657. var binLength = Object.keys(rowData.binsOriginal).length;
  658. var uniqueRatio = this._uniqueRatio(binLength, table.rows()[0].length);
  659. // Don't show the pane if there isn't enough variance in the data, or there is only 1 entry for that pane
  660. if (this.s.displayed === false && ((colOpts.show === undefined && colOpts.threshold === null ?
  661. uniqueRatio > this.c.threshold :
  662. uniqueRatio > colOpts.threshold)
  663. || (colOpts.show !== true && binLength <= 1))) {
  664. this.dom.container.addClass(this.classes.hidden);
  665. this.s.displayed = false;
  666. return;
  667. }
  668. // If the option viewTotal is true then find
  669. // the total count for the whole table to display alongside the displayed count
  670. if (this.c.viewTotal && rowData.arrayTotals.length === 0) {
  671. this.s.rowData.totalOptions = 0;
  672. this._detailsPane();
  673. }
  674. else {
  675. rowData.binsTotal = rowData.bins;
  676. }
  677. this.dom.container.addClass(this.classes.show);
  678. this.s.displayed = true;
  679. }
  680. else if (dataIn !== null && dataIn.searchPanes !== null && dataIn.searchPanes.options !== null) {
  681. if (dataIn.tableLength !== undefined) {
  682. this.s.tableLength = dataIn.tableLength;
  683. this.s.rowData.totalOptions = this.s.tableLength;
  684. }
  685. else if (this.s.tableLength === null || table.rows()[0].length > this.s.tableLength) {
  686. this.s.tableLength = table.rows()[0].length;
  687. this.s.rowData.totalOptions = this.s.tableLength;
  688. }
  689. var colTitle = table.column(this.s.index).dataSrc();
  690. if (dataIn.searchPanes.options[colTitle] !== undefined) {
  691. for (var _i = 0, _a = dataIn.searchPanes.options[colTitle]; _i < _a.length; _i++) {
  692. var dataPoint = _a[_i];
  693. this.s.rowData.arrayFilter.push({
  694. display: dataPoint.label,
  695. filter: dataPoint.value,
  696. sort: dataPoint.label,
  697. type: dataPoint.label
  698. });
  699. this.s.rowData.bins[dataPoint.value] = this.c.viewTotal || this.c.cascadePanes ?
  700. dataPoint.count :
  701. dataPoint.total;
  702. this.s.rowData.binsTotal[dataPoint.value] = dataPoint.total;
  703. }
  704. }
  705. var binLength = Object.keys(rowData.binsTotal).length;
  706. var uniqueRatio = this._uniqueRatio(binLength, this.s.tableLength);
  707. // Don't show the pane if there isn't enough variance in the data, or there is only 1 entry for that pane
  708. if (this.s.displayed === false && ((colOpts.show === undefined && colOpts.threshold === null ?
  709. uniqueRatio > this.c.threshold :
  710. uniqueRatio > colOpts.threshold)
  711. || (colOpts.show !== true && binLength <= 1))) {
  712. this.dom.container.addClass(this.classes.hidden);
  713. this.s.displayed = false;
  714. return;
  715. }
  716. this.s.rowData.arrayOriginal = this.s.rowData.arrayFilter;
  717. this.s.rowData.binsOriginal = this.s.rowData.bins;
  718. this.s.displayed = true;
  719. }
  720. }
  721. else {
  722. this.s.displayed = true;
  723. }
  724. // If the variance is accceptable then display the search pane
  725. this._displayPane();
  726. if (!this.s.listSet) {
  727. // Here, when the state is loaded if the data object on the original table is empty,
  728. // then a state.clear() must have occurred, so delete all of the panes tables state objects too.
  729. this.dom.dtP.on('stateLoadParams.dt', function (e, settings, data) {
  730. if ($.isEmptyObject(table.state.loaded())) {
  731. $.each(data, function (index, value) {
  732. delete data[index];
  733. });
  734. }
  735. });
  736. }
  737. // Add the container to the document in its original location
  738. if (prevEl !== null && $(this.dom.panesContainer).has(prevEl).length > 0) {
  739. $(this.dom.container).insertAfter(prevEl);
  740. }
  741. else {
  742. $(this.dom.panesContainer).prepend(this.dom.container);
  743. }
  744. // Declare the datatable for the pane
  745. var errMode = $.fn.dataTable.ext.errMode;
  746. $.fn.dataTable.ext.errMode = 'none';
  747. var haveScroller = DataTable.Scroller;
  748. this.s.dtPane = $(this.dom.dtP).DataTable($.extend(true, {
  749. columnDefs: [
  750. {
  751. className: 'dtsp-nameColumn',
  752. data: 'display',
  753. render: function (data, type, row) {
  754. if (type === 'sort') {
  755. return row.sort;
  756. }
  757. else if (type === 'type') {
  758. return row.type;
  759. }
  760. var message;
  761. (_this.s.filteringActive || _this.s.showFiltered) && _this.c.viewTotal
  762. ? message = filteredMessage.replace(/{total}/, row.total)
  763. : message = countMessage.replace(/{total}/, row.total);
  764. message = message.replace(/{shown}/, row.shown);
  765. while (message.indexOf('{total}') !== -1) {
  766. message = message.replace(/{total}/, row.total);
  767. }
  768. while (message.indexOf('{shown}') !== -1) {
  769. message = message.replace(/{shown}/, row.shown);
  770. }
  771. // We are displaying the count in the same columne as the name of the search option.
  772. // This is so that there is not need to call columns.adjust(), which in turn speeds up the code
  773. var pill = '<span class="' + _this.classes.pill + '">' + message + '</span>';
  774. if (_this.c.hideCount || colOpts.hideCount) {
  775. pill = '';
  776. }
  777. return '<div class="' + _this.classes.nameCont + '"><span title="' +
  778. (typeof data === 'string' && data.match(/<[^>]*>/) !== null ? data.replace(/<[^>]*>/g, '') : data) +
  779. '" class="' + _this.classes.name + '">' +
  780. data + '</span>' +
  781. pill + '</div>';
  782. },
  783. targets: 0,
  784. // Accessing the private datatables property to set type based on the original table.
  785. // This is null if not defined by the user, meaning that automatic type detection would take place
  786. type: table.settings()[0].aoColumns[this.s.index] !== undefined ?
  787. table.settings()[0].aoColumns[this.s.index]._sManualType :
  788. null
  789. },
  790. {
  791. className: 'dtsp-countColumn ' + this.classes.badgePill,
  792. data: 'shown',
  793. orderData: [1, 2],
  794. targets: 1,
  795. visible: false
  796. },
  797. {
  798. data: 'total',
  799. targets: 2,
  800. visible: false
  801. }
  802. ],
  803. deferRender: true,
  804. dom: 't',
  805. info: false,
  806. language: this.s.dt.settings()[0].oLanguage,
  807. paging: haveScroller ? true : false,
  808. scrollX: false,
  809. scrollY: '200px',
  810. scroller: haveScroller ? true : false,
  811. select: true,
  812. stateSave: table.settings()[0].oFeatures.bStateSave ? true : false
  813. }, this.c.dtOpts, colOpts !== undefined ? colOpts.dtOpts : {}, (this.s.colOpts.options !== undefined || !this.colExists)
  814. ? {
  815. createdRow: function (row, data, dataIndex) {
  816. $(row).addClass(data.className);
  817. }
  818. }
  819. : undefined, (this.customPaneSettings !== null && this.customPaneSettings.dtOpts !== undefined)
  820. ? this.customPaneSettings.dtOpts
  821. : {}));
  822. $(this.dom.dtP).addClass(this.classes.table);
  823. // This is hacky but necessary for when datatables is generating the column titles automatically
  824. $(this.dom.searchBox).attr('placeholder', colOpts.header !== undefined
  825. ? colOpts.header
  826. : this.colExists
  827. ? table.settings()[0].aoColumns[this.s.index].sTitle
  828. : this.customPaneSettings.header || 'Custom Pane');
  829. // As the pane table is not in the document yet we must initialise select ourselves
  830. $.fn.dataTable.select.init(this.s.dtPane);
  831. $.fn.dataTable.ext.errMode = errMode;
  832. // If it is not a custom pane
  833. if (this.colExists) {
  834. // On initialisation, do we need to set a filtering value from a
  835. // saved state or init option?
  836. var search = column.search();
  837. search = search ? search.substr(1, search.length - 2).split('|') : [];
  838. // Count the number of empty cells
  839. var count_1 = 0;
  840. rowData.arrayFilter.forEach(function (element) {
  841. if (element.filter === '') {
  842. count_1++;
  843. }
  844. });
  845. // Add all of the search options to the pane
  846. for (var i = 0, ien = rowData.arrayFilter.length; i < ien; i++) {
  847. var selected = false;
  848. for (var _b = 0, _c = this.s.serverSelect; _b < _c.length; _b++) {
  849. var option = _c[_b];
  850. if (option.filter === rowData.arrayFilter[i].filter) {
  851. selected = true;
  852. }
  853. }
  854. if (this.s.dt.page.info().serverSide &&
  855. (!this.c.cascadePanes ||
  856. (this.c.cascadePanes && rowData.bins[rowData.arrayFilter[i].filter] !== 0) ||
  857. (this.c.cascadePanes && init !== null) ||
  858. selected)) {
  859. var row = this._addRow(rowData.arrayFilter[i].display, rowData.arrayFilter[i].filter, init ?
  860. rowData.binsTotal[rowData.arrayFilter[i].filter] :
  861. rowData.bins[rowData.arrayFilter[i].filter], this.c.viewTotal || init
  862. ? String(rowData.binsTotal[rowData.arrayFilter[i].filter])
  863. : rowData.bins[rowData.arrayFilter[i].filter], rowData.arrayFilter[i].sort, rowData.arrayFilter[i].type);
  864. for (var _d = 0, _e = this.s.serverSelect; _d < _e.length; _d++) {
  865. var option = _e[_d];
  866. if (option.filter === rowData.arrayFilter[i].filter) {
  867. this.s.serverSelecting = true;
  868. row.select();
  869. this.s.serverSelecting = false;
  870. }
  871. }
  872. }
  873. else if (!this.s.dt.page.info().serverSide &&
  874. rowData.arrayFilter[i] &&
  875. (rowData.bins[rowData.arrayFilter[i].filter] !== undefined || !this.c.cascadePanes)) {
  876. this._addRow(rowData.arrayFilter[i].display, rowData.arrayFilter[i].filter, rowData.bins[rowData.arrayFilter[i].filter], rowData.binsTotal[rowData.arrayFilter[i].filter], rowData.arrayFilter[i].sort, rowData.arrayFilter[i].type);
  877. }
  878. else if (!this.s.dt.page.info().serverSide) {
  879. // Just pass an empty string as the message will be calculated based on that in _addRow()
  880. this._addRow('', count_1, count_1, '', '', '');
  881. }
  882. }
  883. }
  884. DataTable.select.init(this.s.dtPane);
  885. // If there are custom options set or it is a custom pane then get them
  886. if (colOpts.options !== undefined ||
  887. (this.customPaneSettings !== null && this.customPaneSettings.options !== undefined)) {
  888. this._getComparisonRows();
  889. }
  890. // Display the pane
  891. this.s.dtPane.draw();
  892. this._adjustTopRow();
  893. if (!this.s.listSet) {
  894. this._setListeners();
  895. this.s.listSet = true;
  896. }
  897. for (var _f = 0, selectedRows_1 = selectedRows; _f < selectedRows_1.length; _f++) {
  898. var selection = selectedRows_1[_f];
  899. if (selection !== undefined) {
  900. for (var _g = 0, _h = this.s.dtPane.rows().indexes().toArray(); _g < _h.length; _g++) {
  901. var row = _h[_g];
  902. if (this.s.dtPane.row(row).data() !== undefined && selection.filter === this.s.dtPane.row(row).data().filter) {
  903. // If this is happening when serverSide processing is happening then different behaviour is needed
  904. if (this.s.dt.page.info().serverSide) {
  905. this.s.serverSelecting = true;
  906. this.s.dtPane.row(row).select();
  907. this.s.serverSelecting = false;
  908. }
  909. else {
  910. this.s.dtPane.row(row).select();
  911. }
  912. }
  913. }
  914. }
  915. }
  916. // If SSP and the table is ready, apply the search for the pane
  917. if (this.s.dt.page.info().serverSide) {
  918. this.s.dtPane.search($(this.dom.searchBox).val()).draw();
  919. }
  920. // Reload the selection, searchbox entry and ordering from the previous state
  921. // Need to check here if SSP that this is the first draw, otherwise it will infinite loop
  922. if (loadedFilter &&
  923. loadedFilter.searchPanes &&
  924. loadedFilter.searchPanes.panes &&
  925. (dataIn === null ||
  926. dataIn.draw === 1)) {
  927. if (!this.c.cascadePanes) {
  928. this._reloadSelect(loadedFilter);
  929. }
  930. for (var _j = 0, _k = loadedFilter.searchPanes.panes; _j < _k.length; _j++) {
  931. var pane = _k[_j];
  932. if (pane.id === this.s.index) {
  933. $(this.dom.searchBox).val(pane.searchTerm);
  934. $(this.dom.searchBox).trigger('input');
  935. this.s.dtPane.order(pane.order).draw();
  936. }
  937. }
  938. }
  939. // Make sure to save the state once the pane has been built
  940. this.s.dt.state.save();
  941. return true;
  942. };
  943. /**
  944. * Update the array which holds the display and filter values for the table
  945. */
  946. SearchPane.prototype._detailsPane = function () {
  947. var table = this.s.dt;
  948. this.s.rowData.arrayTotals = [];
  949. this.s.rowData.binsTotal = {};
  950. var settings = this.s.dt.settings()[0];
  951. var indexArray = table.rows().indexes();
  952. if (!this.s.dt.page.info().serverSide) {
  953. for (var _i = 0, indexArray_1 = indexArray; _i < indexArray_1.length; _i++) {
  954. var rowIdx = indexArray_1[_i];
  955. this._populatePaneArray(rowIdx, this.s.rowData.arrayTotals, settings, this.s.rowData.binsTotal);
  956. }
  957. }
  958. };
  959. /**
  960. * Appends all of the HTML elements to their relevant parent Elements
  961. */
  962. SearchPane.prototype._displayPane = function () {
  963. var container = this.dom.container;
  964. var colOpts = this.s.colOpts;
  965. var layVal = parseInt(this.c.layout.split('-')[1], 10);
  966. // Empty everything to start again
  967. $(this.dom.topRow).empty();
  968. $(this.dom.dtP).empty();
  969. $(this.dom.topRow).addClass(this.classes.topRow);
  970. // If there are more than 3 columns defined then make there be a smaller gap between the panes
  971. if (layVal > 3) {
  972. $(this.dom.container).addClass(this.classes.smallGap);
  973. }
  974. $(this.dom.topRow).addClass(this.classes.subRowsContainer);
  975. $(this.dom.upper).appendTo(this.dom.topRow);
  976. $(this.dom.lower).appendTo(this.dom.topRow);
  977. $(this.dom.searchCont).appendTo(this.dom.upper);
  978. $(this.dom.buttonGroup).appendTo(this.dom.lower);
  979. // If no selections have been made in the pane then disable the clear button
  980. if (this.c.dtOpts.searching === false ||
  981. (colOpts.dtOpts !== undefined &&
  982. colOpts.dtOpts.searching === false) ||
  983. (!this.c.controls || !colOpts.controls) ||
  984. (this.customPaneSettings !== null &&
  985. this.customPaneSettings.dtOpts !== undefined &&
  986. this.customPaneSettings.dtOpts.searching !== undefined &&
  987. !this.customPaneSettings.dtOpts.searching)) {
  988. $(this.dom.searchBox).attr('disabled', 'disabled')
  989. .removeClass(this.classes.paneInputButton)
  990. .addClass(this.classes.disabledButton);
  991. }
  992. $(this.dom.searchBox).appendTo(this.dom.searchCont);
  993. // Create the contents of the searchCont div. Worth noting that this function will change when using semantic ui
  994. this._searchContSetup();
  995. // If the clear button is allowed to show then display it
  996. if (this.c.clear && this.c.controls && colOpts.controls) {
  997. $(this.dom.clear).appendTo(this.dom.buttonGroup);
  998. }
  999. if (this.c.orderable && colOpts.orderable && this.c.controls && colOpts.controls) {
  1000. $(this.dom.nameButton).appendTo(this.dom.buttonGroup);
  1001. }
  1002. // If the count column is hidden then don't display the ordering button for it
  1003. if (!this.c.hideCount &&
  1004. !colOpts.hideCount &&
  1005. this.c.orderable &&
  1006. colOpts.orderable &&
  1007. this.c.controls &&
  1008. colOpts.controls) {
  1009. $(this.dom.countButton).appendTo(this.dom.buttonGroup);
  1010. }
  1011. $(this.dom.topRow).prependTo(this.dom.container);
  1012. $(container).append(this.dom.dtP);
  1013. $(container).show();
  1014. };
  1015. /**
  1016. * Gets the options for the row for the customPanes
  1017. * @returns {object} The options for the row extended to include the options from the user.
  1018. */
  1019. SearchPane.prototype._getBonusOptions = function () {
  1020. // We need to reset the thresholds as if they have a value in colOpts then that value will be used
  1021. var defaultMutator = {
  1022. orthogonal: {
  1023. threshold: null
  1024. },
  1025. threshold: null
  1026. };
  1027. return $.extend(true, {}, SearchPane.defaults, defaultMutator, this.c !== undefined ? this.c : {});
  1028. };
  1029. /**
  1030. * Adds the custom options to the pane
  1031. * @returns {Array} Returns the array of rows which have been added to the pane
  1032. */
  1033. SearchPane.prototype._getComparisonRows = function () {
  1034. var colOpts = this.s.colOpts;
  1035. // Find the appropriate options depending on whether this is a pane for a specific column or a custom pane
  1036. var options = colOpts.options !== undefined
  1037. ? colOpts.options
  1038. : this.customPaneSettings !== null && this.customPaneSettings.options !== undefined
  1039. ? this.customPaneSettings.options
  1040. : undefined;
  1041. if (options === undefined) {
  1042. return;
  1043. }
  1044. var tableVals = this.s.dt.rows({ search: 'applied' }).data().toArray();
  1045. var appRows = this.s.dt.rows({ search: 'applied' });
  1046. var tableValsTotal = this.s.dt.rows().data().toArray();
  1047. var allRows = this.s.dt.rows();
  1048. var rows = [];
  1049. // Clear all of the other rows from the pane, only custom options are to be displayed when they are defined
  1050. this.s.dtPane.clear();
  1051. for (var _i = 0, options_1 = options; _i < options_1.length; _i++) {
  1052. var comp = options_1[_i];
  1053. // Initialise the object which is to be placed in the row
  1054. var insert = comp.label !== '' ? comp.label : this.c.emptyMessage;
  1055. var comparisonObj = {
  1056. className: comp.className,
  1057. display: insert,
  1058. filter: typeof comp.value === 'function' ? comp.value : [],
  1059. shown: 0,
  1060. sort: insert,
  1061. total: 0,
  1062. type: insert
  1063. };
  1064. // If a custom function is in place
  1065. if (typeof comp.value === 'function') {
  1066. // Count the number of times the function evaluates to true for the data currently being displayed
  1067. for (var tVal = 0; tVal < tableVals.length; tVal++) {
  1068. if (comp.value.call(this.s.dt, tableVals[tVal], appRows[0][tVal])) {
  1069. comparisonObj.shown++;
  1070. }
  1071. }
  1072. // Count the number of times the function evaluates to true for the original data in the Table
  1073. for (var i = 0; i < tableValsTotal.length; i++) {
  1074. if (comp.value.call(this.s.dt, tableValsTotal[i], allRows[0][i])) {
  1075. comparisonObj.total++;
  1076. }
  1077. }
  1078. // Update the comparisonObj
  1079. if (typeof comparisonObj.filter !== 'function') {
  1080. comparisonObj.filter.push(comp.filter);
  1081. }
  1082. }
  1083. // If cascadePanes is not active or if it is and the comparisonObj should be shown then add it to the pane
  1084. if (!this.c.cascadePanes || (this.c.cascadePanes && comparisonObj.shown !== 0)) {
  1085. rows.push(this._addRow(comparisonObj.display, comparisonObj.filter, comparisonObj.shown, comparisonObj.total, comparisonObj.sort, comparisonObj.type, comparisonObj.className));
  1086. }
  1087. }
  1088. return rows;
  1089. };
  1090. /**
  1091. * Gets the options for the row for the customPanes
  1092. * @returns {object} The options for the row extended to include the options from the user.
  1093. */
  1094. SearchPane.prototype._getOptions = function () {
  1095. var table = this.s.dt;
  1096. // We need to reset the thresholds as if they have a value in colOpts then that value will be used
  1097. var defaultMutator = {
  1098. emptyMessage: false,
  1099. orthogonal: {
  1100. threshold: null
  1101. },
  1102. threshold: null
  1103. };
  1104. return $.extend(true, {}, SearchPane.defaults, defaultMutator, table.settings()[0].aoColumns[this.s.index].searchPanes);
  1105. };
  1106. /**
  1107. * This method allows for changes to the panes and table to be made when a selection or a deselection occurs
  1108. * @param select Denotes whether a selection has been made or not
  1109. */
  1110. SearchPane.prototype._makeSelection = function () {
  1111. this.updateTable();
  1112. this.s.updating = true;
  1113. this.s.dt.draw();
  1114. this.s.updating = false;
  1115. };
  1116. /**
  1117. * Fill the array with the values that are currently being displayed in the table
  1118. * @param last boolean to indicate whether this was the last pane a selection was made in
  1119. */
  1120. SearchPane.prototype._populatePane = function (last) {
  1121. if (last === void 0) { last = false; }
  1122. var table = this.s.dt;
  1123. this.s.rowData.arrayFilter = [];
  1124. this.s.rowData.bins = {};
  1125. var settings = this.s.dt.settings()[0];
  1126. // If cascadePanes or viewTotal are active it is necessary to get the data which is currently
  1127. // being displayed for their functionality. Also make sure that this was not the last pane to have a selection made
  1128. if (!this.s.dt.page.info().serverSide) {
  1129. var indexArray = (this.c.cascadePanes || this.c.viewTotal) && (!this.s.clearing && !last) ?
  1130. table.rows({ search: 'applied' }).indexes() :
  1131. table.rows().indexes();
  1132. for (var _i = 0, _a = indexArray.toArray(); _i < _a.length; _i++) {
  1133. var index = _a[_i];
  1134. this._populatePaneArray(index, this.s.rowData.arrayFilter, settings);
  1135. }
  1136. }
  1137. };
  1138. /**
  1139. * Populates an array with all of the data for the table
  1140. * @param rowIdx The current row index to be compared
  1141. * @param arrayFilter The array that is to be populated with row Details
  1142. * @param bins The bins object that is to be populated with the row counts
  1143. */
  1144. SearchPane.prototype._populatePaneArray = function (rowIdx, arrayFilter, settings, bins) {
  1145. if (bins === void 0) { bins = this.s.rowData.bins; }
  1146. var colOpts = this.s.colOpts;
  1147. // Retrieve the rendered data from the cell using the fnGetCellData function
  1148. // rather than the cell().render API method for optimisation
  1149. if (typeof colOpts.orthogonal === 'string') {
  1150. var rendered = settings.oApi._fnGetCellData(settings, rowIdx, this.s.index, colOpts.orthogonal);
  1151. this.s.rowData.filterMap.set(rowIdx, rendered);
  1152. this._addOption(rendered, rendered, rendered, rendered, arrayFilter, bins);
  1153. }
  1154. else {
  1155. var filter = settings.oApi._fnGetCellData(settings, rowIdx, this.s.index, colOpts.orthogonal.search);
  1156. // Null and empty string are to be considered the same value
  1157. if (filter === null) {
  1158. filter = '';
  1159. }
  1160. if (typeof filter === 'string') {
  1161. filter = filter.replace(/<[^>]*>/g, '');
  1162. }
  1163. this.s.rowData.filterMap.set(rowIdx, filter);
  1164. if (!bins[filter]) {
  1165. bins[filter] = 1;
  1166. this._addOption(filter, settings.oApi._fnGetCellData(settings, rowIdx, this.s.index, colOpts.orthogonal.display), settings.oApi._fnGetCellData(settings, rowIdx, this.s.index, colOpts.orthogonal.sort), settings.oApi._fnGetCellData(settings, rowIdx, this.s.index, colOpts.orthogonal.type), arrayFilter, bins);
  1167. this.s.rowData.totalOptions++;
  1168. }
  1169. else {
  1170. bins[filter]++;
  1171. this.s.rowData.totalOptions++;
  1172. return;
  1173. }
  1174. }
  1175. };
  1176. /**
  1177. * Reloads all of the previous selects into the panes
  1178. * @param loadedFilter The loaded filters from a previous state
  1179. */
  1180. SearchPane.prototype._reloadSelect = function (loadedFilter) {
  1181. // If the state was not saved don't selected any
  1182. if (loadedFilter === undefined) {
  1183. return;
  1184. }
  1185. var idx;
  1186. // For each pane, check that the loadedFilter list exists and is not null,
  1187. // find the id of each search item and set it to be selected.
  1188. for (var i = 0; i < loadedFilter.searchPanes.panes.length; i++) {
  1189. if (loadedFilter.searchPanes.panes[i].id === this.s.index) {
  1190. idx = i;
  1191. break;
  1192. }
  1193. }
  1194. if (idx !== undefined) {
  1195. var table = this.s.dtPane;
  1196. var rows = table.rows({ order: 'index' }).data().map(function (item) { return item.filter !== null ?
  1197. item.filter.toString() :
  1198. null; }).toArray();
  1199. for (var _i = 0, _a = loadedFilter.searchPanes.panes[idx].selected; _i < _a.length; _i++) {
  1200. var filter = _a[_i];
  1201. var id = -1;
  1202. if (filter !== null) {
  1203. id = rows.indexOf(filter.toString());
  1204. }
  1205. if (id > -1) {
  1206. this.s.serverSelecting = true;
  1207. table.row(id).select();
  1208. this.s.serverSelecting = false;
  1209. }
  1210. }
  1211. }
  1212. };
  1213. /**
  1214. * This method decides whether a row should contribute to the pane or not
  1215. * @param filter the value that the row is to be filtered on
  1216. * @param dataIndex the row index
  1217. */
  1218. SearchPane.prototype._search = function (filter, dataIndex) {
  1219. var colOpts = this.s.colOpts;
  1220. var table = this.s.dt;
  1221. // For each item selected in the pane, check if it is available in the cell
  1222. for (var _i = 0, _a = this.selections; _i < _a.length; _i++) {
  1223. var colSelect = _a[_i];
  1224. if (typeof colSelect.filter === 'string') {
  1225. // The filter value will not have the &amp; in place but a &,
  1226. // so we need to do a replace to make sure that they will match
  1227. colSelect.filter = colSelect.filter.replaceAll('&amp;', '&');
  1228. }
  1229. // if the filter is an array then is the column present in it
  1230. if (Array.isArray(filter)) {
  1231. if (filter.indexOf(colSelect.filter) !== -1) {
  1232. return true;
  1233. }
  1234. }
  1235. // if the filter is a function then does it meet the criteria of that function or not
  1236. else if (typeof colSelect.filter === 'function') {
  1237. if (colSelect.filter.call(table, table.row(dataIndex).data(), dataIndex)) {
  1238. if (colOpts.combiner === 'or') {
  1239. return true;
  1240. }
  1241. }
  1242. // If the combiner is an "and" then we need to check against all possible selections
  1243. // so if it fails here then the and is not met and return false
  1244. else if (colOpts.combiner === 'and') {
  1245. return false;
  1246. }
  1247. }
  1248. // otherwise if the two filter values are equal then return true
  1249. // Loose type checking incase number type in column comparing to a string
  1250. else if ((filter === colSelect.filter) ||
  1251. (!(typeof filter === 'string' && filter.length === 0) && filter == colSelect.filter) ||
  1252. (colSelect.filter === null && typeof filter === 'string' && filter === '')) {
  1253. return true;
  1254. }
  1255. }
  1256. // If the combiner is an and then we need to check against all possible selections
  1257. // so return true here if so because it would have returned false earlier if it had failed
  1258. if (colOpts.combiner === 'and') {
  1259. return true;
  1260. }
  1261. // Otherwise it hasn't matched with anything by this point so it must be false
  1262. else {
  1263. return false;
  1264. }
  1265. };
  1266. /**
  1267. * Creates the contents of the searchCont div
  1268. *
  1269. * NOTE This is overridden when semantic ui styling in order to integrate the search button into the text box.
  1270. */
  1271. SearchPane.prototype._searchContSetup = function () {
  1272. if (this.c.controls && this.s.colOpts.controls) {
  1273. $(this.dom.searchButton).appendTo(this.dom.searchLabelCont);
  1274. }
  1275. if (!(this.c.dtOpts.searching === false ||
  1276. this.s.colOpts.dtOpts.searching === false ||
  1277. (this.customPaneSettings !== null &&
  1278. this.customPaneSettings.dtOpts !== undefined &&
  1279. this.customPaneSettings.dtOpts.searching !== undefined &&
  1280. !this.customPaneSettings.dtOpts.searching))) {
  1281. $(this.dom.searchLabelCont).appendTo(this.dom.searchCont);
  1282. }
  1283. };
  1284. /**
  1285. * Adds outline to the pane when a selection has been made
  1286. */
  1287. SearchPane.prototype._searchExtras = function () {
  1288. var updating = this.s.updating;
  1289. this.s.updating = true;
  1290. var filters = this.s.dtPane.rows({ selected: true }).data().pluck('filter').toArray();
  1291. var nullIndex = filters.indexOf(this.s.colOpts.emptyMessage !== false ?
  1292. this.s.colOpts.emptyMessage :
  1293. this.c.emptyMessage);
  1294. var container = $(this.s.dtPane.table().container());
  1295. // If null index is found then search for empty cells as a filter.
  1296. if (nullIndex > -1) {
  1297. filters[nullIndex] = '';
  1298. }
  1299. // If a filter has been applied then outline the respective pane, remove it when it no longer is.
  1300. if (filters.length > 0) {
  1301. container.addClass(this.classes.selected);
  1302. }
  1303. else if (filters.length === 0) {
  1304. container.removeClass(this.classes.selected);
  1305. }
  1306. this.s.updating = updating;
  1307. };
  1308. /**
  1309. * Finds the ratio of the number of different options in the table to the number of rows
  1310. * @param bins the number of different options in the table
  1311. * @param rowCount the total number of rows in the table
  1312. * @returns {number} returns the ratio
  1313. */
  1314. SearchPane.prototype._uniqueRatio = function (bins, rowCount) {
  1315. if (rowCount > 0 &&
  1316. ((this.s.rowData.totalOptions > 0 && !this.s.dt.page.info().serverSide) ||
  1317. (this.s.dt.page.info().serverSide && this.s.tableLength > 0))) {
  1318. return bins / this.s.rowData.totalOptions;
  1319. }
  1320. else {
  1321. return 1;
  1322. }
  1323. };
  1324. /**
  1325. * updates the options within the pane
  1326. * @param draw a flag to define whether this has been called due to a draw event or not
  1327. */
  1328. SearchPane.prototype._updateCommon = function (draw) {
  1329. if (draw === void 0) { draw = false; }
  1330. // Update the panes if doing a deselect. if doing a select then
  1331. // update all of the panes except for the one causing the change
  1332. if (!this.s.dt.page.info().serverSide &&
  1333. this.s.dtPane !== undefined &&
  1334. (!this.s.filteringActive || this.c.cascadePanes || draw === true) &&
  1335. (this.c.cascadePanes !== true || this.s.selectPresent !== true) && (!this.s.lastSelect || !this.s.lastCascade)) {
  1336. var colOpts = this.s.colOpts;
  1337. var selected = this.s.dtPane.rows({ selected: true }).data().toArray();
  1338. var scrollTop = $(this.s.dtPane.table().node()).parent()[0].scrollTop;
  1339. var rowData = this.s.rowData;
  1340. // Clear the pane in preparation for adding the updated search options
  1341. this.s.dtPane.clear();
  1342. // If it is not a custom pane
  1343. if (this.colExists) {
  1344. // Only run populatePane if the data has not been collected yet
  1345. if (rowData.arrayFilter.length === 0) {
  1346. this._populatePane();
  1347. }
  1348. // If cascadePanes is active and the table has returned to its default state then
  1349. // there is a need to update certain parts ofthe rowData.
  1350. else if (this.c.cascadePanes
  1351. && this.s.dt.rows().data().toArray().length === this.s.dt.rows({ search: 'applied' }).data().toArray().length) {
  1352. rowData.arrayFilter = rowData.arrayOriginal;
  1353. rowData.bins = rowData.binsOriginal;
  1354. }
  1355. // Otherwise if viewTotal or cascadePanes is active then the data from the table must be read.
  1356. else if (this.c.viewTotal || this.c.cascadePanes) {
  1357. this._populatePane();
  1358. }
  1359. // If the viewTotal option is selected then find the totals for the table
  1360. if (this.c.viewTotal) {
  1361. this._detailsPane();
  1362. }
  1363. else {
  1364. rowData.binsTotal = rowData.bins;
  1365. }
  1366. if (this.c.viewTotal && !this.c.cascadePanes) {
  1367. rowData.arrayFilter = rowData.arrayTotals;
  1368. }
  1369. var _loop_1 = function (dataP) {
  1370. // If both view Total and cascadePanes have been selected and the count of the row is not 0 then add it to pane
  1371. // Do this also if the viewTotal option has been selected and cascadePanes has not
  1372. if (dataP && ((rowData.bins[dataP.filter] !== undefined && rowData.bins[dataP.filter] !== 0 && this_1.c.cascadePanes)
  1373. || !this_1.c.cascadePanes
  1374. || this_1.s.clearing)) {
  1375. var row = this_1._addRow(dataP.display, dataP.filter, !this_1.c.viewTotal
  1376. ? rowData.bins[dataP.filter]
  1377. : rowData.bins[dataP.filter] !== undefined
  1378. ? rowData.bins[dataP.filter]
  1379. : 0, this_1.c.viewTotal
  1380. ? String(rowData.binsTotal[dataP.filter])
  1381. : rowData.bins[dataP.filter], dataP.sort, dataP.type);
  1382. // Find out if the filter was selected in the previous search, if so select it and remove from array.
  1383. var selectIndex = selected.findIndex(function (element) {
  1384. return element.filter === dataP.filter;
  1385. });
  1386. if (selectIndex !== -1) {
  1387. row.select();
  1388. selected.splice(selectIndex, 1);
  1389. }
  1390. }
  1391. };
  1392. var this_1 = this;
  1393. for (var _i = 0, _a = rowData.arrayFilter; _i < _a.length; _i++) {
  1394. var dataP = _a[_i];
  1395. _loop_1(dataP);
  1396. }
  1397. }
  1398. if ((colOpts.searchPanes !== undefined && colOpts.searchPanes.options !== undefined) ||
  1399. colOpts.options !== undefined ||
  1400. (this.customPaneSettings !== null && this.customPaneSettings.options !== undefined)) {
  1401. var rows = this._getComparisonRows();
  1402. var _loop_2 = function (row) {
  1403. var selectIndex = selected.findIndex(function (element) {
  1404. if (element.display === row.data().display) {
  1405. return true;
  1406. }
  1407. });
  1408. if (selectIndex !== -1) {
  1409. row.select();
  1410. selected.splice(selectIndex, 1);
  1411. }
  1412. };
  1413. for (var _b = 0, rows_1 = rows; _b < rows_1.length; _b++) {
  1414. var row = rows_1[_b];
  1415. _loop_2(row);
  1416. }
  1417. }
  1418. // Add search options which were previously selected but whos results are no
  1419. // longer present in the resulting data set.
  1420. for (var _c = 0, selected_1 = selected; _c < selected_1.length; _c++) {
  1421. var selectedEl = selected_1[_c];
  1422. var row = this._addRow(selectedEl.display, selectedEl.filter, 0, this.c.viewTotal
  1423. ? selectedEl.total
  1424. : 0, selectedEl.display, selectedEl.display);
  1425. this.s.updating = true;
  1426. row.select();
  1427. this.s.updating = false;
  1428. }
  1429. this.s.dtPane.draw();
  1430. this.s.dtPane.table().node().parentNode.scrollTop = scrollTop;
  1431. }
  1432. };
  1433. SearchPane.version = '1.1.0';
  1434. SearchPane.classes = {
  1435. buttonGroup: 'dtsp-buttonGroup',
  1436. buttonSub: 'dtsp-buttonSub',
  1437. clear: 'dtsp-clear',
  1438. clearAll: 'dtsp-clearAll',
  1439. clearButton: 'clearButton',
  1440. container: 'dtsp-searchPane',
  1441. countButton: 'dtsp-countButton',
  1442. disabledButton: 'dtsp-disabledButton',
  1443. dull: 'dtsp-dull',
  1444. hidden: 'dtsp-hidden',
  1445. hide: 'dtsp-hide',
  1446. layout: 'dtsp-',
  1447. name: 'dtsp-name',
  1448. nameButton: 'dtsp-nameButton',
  1449. nameCont: 'dtsp-nameCont',
  1450. narrow: 'dtsp-narrow',
  1451. paneButton: 'dtsp-paneButton',
  1452. paneInputButton: 'dtsp-paneInputButton',
  1453. pill: 'dtsp-pill',
  1454. search: 'dtsp-search',
  1455. searchCont: 'dtsp-searchCont',
  1456. searchIcon: 'dtsp-searchIcon',
  1457. searchLabelCont: 'dtsp-searchButtonCont',
  1458. selected: 'dtsp-selected',
  1459. smallGap: 'dtsp-smallGap',
  1460. subRow1: 'dtsp-subRow1',
  1461. subRow2: 'dtsp-subRow2',
  1462. subRowsContainer: 'dtsp-subRowsContainer',
  1463. title: 'dtsp-title',
  1464. topRow: 'dtsp-topRow'
  1465. };
  1466. // Define SearchPanes default options
  1467. SearchPane.defaults = {
  1468. cascadePanes: false,
  1469. clear: true,
  1470. combiner: 'or',
  1471. controls: true,
  1472. container: function (dt) {
  1473. return dt.table().container();
  1474. },
  1475. dtOpts: {},
  1476. emptyMessage: '<i>No Data</i>',
  1477. hideCount: false,
  1478. layout: 'columns-3',
  1479. name: undefined,
  1480. orderable: true,
  1481. orthogonal: {
  1482. display: 'display',
  1483. filter: 'filter',
  1484. hideCount: false,
  1485. search: 'filter',
  1486. show: undefined,
  1487. sort: 'sort',
  1488. threshold: 0.6,
  1489. type: 'type'
  1490. },
  1491. preSelect: [],
  1492. threshold: 0.6,
  1493. viewTotal: false
  1494. };
  1495. return SearchPane;
  1496. }());
  1497. var $$1;
  1498. var DataTable$1;
  1499. function setJQuery$1(jq) {
  1500. $$1 = jq;
  1501. DataTable$1 = jq.fn.dataTable;
  1502. }
  1503. var SearchPanes = /** @class */ (function () {
  1504. function SearchPanes(paneSettings, opts, fromInit) {
  1505. var _this = this;
  1506. if (fromInit === void 0) { fromInit = false; }
  1507. this.regenerating = false;
  1508. // Check that the required version of DataTables is included
  1509. if (!DataTable$1 || !DataTable$1.versionCheck || !DataTable$1.versionCheck('1.10.0')) {
  1510. throw new Error('SearchPane requires DataTables 1.10 or newer');
  1511. }
  1512. // Check that Select is included
  1513. if (!DataTable$1.select) {
  1514. throw new Error('SearchPane requires Select');
  1515. }
  1516. var table = new DataTable$1.Api(paneSettings);
  1517. this.classes = $$1.extend(true, {}, SearchPanes.classes);
  1518. // Get options from user
  1519. this.c = $$1.extend(true, {}, SearchPanes.defaults, opts);
  1520. // Add extra elements to DOM object including clear
  1521. this.dom = {
  1522. clearAll: $$1('<button type="button">Clear All</button>').addClass(this.classes.clearAll),
  1523. container: $$1('<div/>').addClass(this.classes.panes).text(table.i18n('searchPanes.loadMessage', 'Loading Search Panes...')),
  1524. emptyMessage: $$1('<div/>').addClass(this.classes.emptyMessage),
  1525. options: $$1('<div/>').addClass(this.classes.container),
  1526. panes: $$1('<div/>').addClass(this.classes.container),
  1527. title: $$1('<div/>').addClass(this.classes.title),
  1528. titleRow: $$1('<div/>').addClass(this.classes.titleRow),
  1529. wrapper: $$1('<div/>')
  1530. };
  1531. this.s = {
  1532. colOpts: [],
  1533. dt: table,
  1534. filterCount: 0,
  1535. filterPane: -1,
  1536. page: 0,
  1537. panes: [],
  1538. selectionList: [],
  1539. serverData: {},
  1540. stateRead: false,
  1541. updating: false
  1542. };
  1543. if (table.settings()[0]._searchPanes !== undefined) {
  1544. return;
  1545. }
  1546. this._getState();
  1547. if (this.s.dt.page.info().serverSide) {
  1548. table.on('preXhr.dt', function (e, settings, data) {
  1549. if (data.searchPanes === undefined) {
  1550. data.searchPanes = {};
  1551. }
  1552. for (var _i = 0, _a = _this.s.selectionList; _i < _a.length; _i++) {
  1553. var selection = _a[_i];
  1554. var src = _this.s.dt.column(selection.index).dataSrc();
  1555. if (data.searchPanes[src] === undefined) {
  1556. data.searchPanes[src] = {};
  1557. }
  1558. for (var i = 0; i < selection.rows.length; i++) {
  1559. data.searchPanes[src][i] = selection.rows[i].filter;
  1560. }
  1561. }
  1562. });
  1563. }
  1564. // We are using the xhr event to rebuild the panes if required due to viewTotal being enabled
  1565. // If viewTotal is not enabled then we simply update the data from the server
  1566. table.on('xhr', function (e, settings, json, xhr) {
  1567. if (json && json.searchPanes && json.searchPanes.options) {
  1568. _this.s.serverData = json;
  1569. _this.s.serverData.tableLength = json.recordsTotal;
  1570. _this._serverTotals();
  1571. }
  1572. });
  1573. table.settings()[0]._searchPanes = this;
  1574. this.dom.clearAll.text(table.i18n('searchPanes.clearMessage', 'Clear All'));
  1575. if (this.s.dt.settings()[0]._bInitComplete || fromInit) {
  1576. this._paneDeclare(table, paneSettings, opts);
  1577. }
  1578. else {
  1579. table.one('preInit.dt', function (settings) {
  1580. _this._paneDeclare(table, paneSettings, opts);
  1581. });
  1582. }
  1583. return this;
  1584. }
  1585. /**
  1586. * Clear the selections of all of the panes
  1587. */
  1588. SearchPanes.prototype.clearSelections = function () {
  1589. // Load in all of the searchBoxes in the documents
  1590. var searches = this.dom.container.find(this.classes.search);
  1591. // For each searchBox set the input text to be empty and then trigger
  1592. // an input on them so that they no longer filter the panes
  1593. searches.each(function () {
  1594. $$1(this).val('');
  1595. $$1(this).trigger('input');
  1596. });
  1597. var returnArray = [];
  1598. // For every pane, clear the selections in the pane
  1599. for (var _i = 0, _a = this.s.panes; _i < _a.length; _i++) {
  1600. var pane = _a[_i];
  1601. if (pane.s.dtPane !== undefined) {
  1602. returnArray.push(pane.clearPane());
  1603. }
  1604. }
  1605. this.s.dt.draw();
  1606. return returnArray;
  1607. };
  1608. /**
  1609. * returns the container node for the searchPanes
  1610. */
  1611. SearchPanes.prototype.getNode = function () {
  1612. return this.dom.container;
  1613. };
  1614. /**
  1615. * rebuilds all of the panes
  1616. */
  1617. SearchPanes.prototype.rebuild = function (targetIdx, maintainSelection) {
  1618. if (targetIdx === void 0) { targetIdx = false; }
  1619. if (maintainSelection === void 0) { maintainSelection = false; }
  1620. $$1(this.dom.emptyMessage).remove();
  1621. // As a rebuild from scratch is required, empty the searchpanes container.
  1622. var returnArray = [];
  1623. // Rebuild each pane individually, if a specific pane has been selected then only rebuild that one
  1624. if (targetIdx === false) {
  1625. $$1(this.dom.panes).empty();
  1626. }
  1627. for (var _i = 0, _a = this.s.panes; _i < _a.length; _i++) {
  1628. var pane = _a[_i];
  1629. if (targetIdx !== false && pane.s.index !== targetIdx) {
  1630. continue;
  1631. }
  1632. pane.clearData();
  1633. returnArray.push(
  1634. // Pass a boolean to say whether this is the last choice made for maintaining selections when rebuilding
  1635. pane.rebuildPane(this.s.selectionList[this.s.selectionList.length - 1] !== undefined ?
  1636. pane.s.index === this.s.selectionList[this.s.selectionList.length - 1].index :
  1637. false, this.s.dt.page.info().serverSide ?
  1638. this.s.serverData :
  1639. undefined, null, maintainSelection));
  1640. $$1(this.dom.panes).append(pane.dom.container);
  1641. }
  1642. // Only need to trigger a search if it is not server side processing
  1643. if (!this.s.dt.page.info().serverSide) {
  1644. this.s.dt.draw();
  1645. }
  1646. if (this.c.cascadePanes || this.c.viewTotal) {
  1647. this.redrawPanes(true);
  1648. }
  1649. else {
  1650. this._updateSelection();
  1651. }
  1652. // Attach panes, clear buttons, and title bar to the document
  1653. this._updateFilterCount();
  1654. this._attachPaneContainer();
  1655. this.s.dt.draw();
  1656. // If a single pane has been rebuilt then return only that pane
  1657. if (returnArray.length === 1) {
  1658. return returnArray[0];
  1659. }
  1660. // Otherwise return all of the panes that have been rebuilt
  1661. else {
  1662. return returnArray;
  1663. }
  1664. };
  1665. /**
  1666. * Redraws all of the panes
  1667. */
  1668. SearchPanes.prototype.redrawPanes = function (rebuild) {
  1669. if (rebuild === void 0) { rebuild = false; }
  1670. var table = this.s.dt;
  1671. // Only do this if the redraw isn't being triggered by the panes updating themselves
  1672. if (!this.s.updating && !this.s.dt.page.info().serverSide) {
  1673. var filterActive = true;
  1674. var filterPane = this.s.filterPane;
  1675. // If the number of rows currently visible is equal to the number of rows in the table
  1676. // then there can't be any filtering taking place
  1677. if (table.rows({ search: 'applied' }).data().toArray().length === table.rows().data().toArray().length) {
  1678. filterActive = false;
  1679. }
  1680. // Otherwise if viewTotal is active then it is necessary to determine which panes a select is present in.
  1681. // If there is only one pane with a selection present then it should not show the filtered message as
  1682. // more selections may be made in that pane.
  1683. else if (this.c.viewTotal) {
  1684. for (var _i = 0, _a = this.s.panes; _i < _a.length; _i++) {
  1685. var pane = _a[_i];
  1686. if (pane.s.dtPane !== undefined) {
  1687. var selectLength = pane.s.dtPane.rows({ selected: true }).data().toArray().length;
  1688. if (selectLength === 0) {
  1689. for (var _b = 0, _c = this.s.selectionList; _b < _c.length; _b++) {
  1690. var selection = _c[_b];
  1691. if (selection.index === pane.s.index && selection.rows.length !== 0) {
  1692. selectLength = selection.rows.length;
  1693. }
  1694. }
  1695. }
  1696. // If filterPane === -1 then a pane with a selection has not been found yet, so set filterPane to that panes index
  1697. if (selectLength > 0 && filterPane === -1) {
  1698. filterPane = pane.s.index;
  1699. }
  1700. // Then if another pane is found with a selection then set filterPane to null to
  1701. // show that multiple panes have selections present
  1702. else if (selectLength > 0) {
  1703. filterPane = null;
  1704. }
  1705. }
  1706. }
  1707. }
  1708. var deselectIdx = void 0;
  1709. var newSelectionList = [];
  1710. // Don't run this if it is due to the panes regenerating
  1711. if (!this.regenerating) {
  1712. for (var _d = 0, _e = this.s.panes; _d < _e.length; _d++) {
  1713. var pane = _e[_d];
  1714. // Identify the pane where a selection or deselection has been made and add it to the list.
  1715. if (pane.s.selectPresent) {
  1716. this.s.selectionList.push({ index: pane.s.index, rows: pane.s.dtPane.rows({ selected: true }).data().toArray(), protect: false });
  1717. table.state.save();
  1718. break;
  1719. }
  1720. else if (pane.s.deselect) {
  1721. deselectIdx = pane.s.index;
  1722. var selectedData = pane.s.dtPane.rows({ selected: true }).data().toArray();
  1723. if (selectedData.length > 0) {
  1724. this.s.selectionList.push({ index: pane.s.index, rows: selectedData, protect: true });
  1725. }
  1726. }
  1727. }
  1728. if (this.s.selectionList.length > 0) {
  1729. var last = this.s.selectionList[this.s.selectionList.length - 1].index;
  1730. for (var _f = 0, _g = this.s.panes; _f < _g.length; _f++) {
  1731. var pane = _g[_f];
  1732. pane.s.lastSelect = (pane.s.index === last);
  1733. }
  1734. }
  1735. // Remove selections from the list from the pane where a deselect has taken place
  1736. for (var i = 0; i < this.s.selectionList.length; i++) {
  1737. if (this.s.selectionList[i].index !== deselectIdx || this.s.selectionList[i].protect === true) {
  1738. var further = false;
  1739. // Find out if this selection is the last one in the list for that pane
  1740. for (var j = i + 1; j < this.s.selectionList.length; j++) {
  1741. if (this.s.selectionList[j].index === this.s.selectionList[i].index) {
  1742. further = true;
  1743. }
  1744. }
  1745. // If there are no selections for this pane in the list then just push this one
  1746. if (!further) {
  1747. newSelectionList.push(this.s.selectionList[i]);
  1748. this.s.selectionList[i].protect = false;
  1749. }
  1750. }
  1751. }
  1752. var solePane = -1;
  1753. if (newSelectionList.length === 1) {
  1754. solePane = newSelectionList[0].index;
  1755. }
  1756. // Update all of the panes to reflect the current state of the filters
  1757. for (var _h = 0, _j = this.s.panes; _h < _j.length; _h++) {
  1758. var pane = _j[_h];
  1759. if (pane.s.dtPane !== undefined) {
  1760. var tempFilter = true;
  1761. pane.s.filteringActive = true;
  1762. if ((filterPane !== -1 && filterPane !== null && filterPane === pane.s.index) ||
  1763. filterActive === false ||
  1764. pane.s.index === solePane) {
  1765. tempFilter = false;
  1766. pane.s.filteringActive = false;
  1767. }
  1768. pane.updatePane(!tempFilter ? false : filterActive);
  1769. }
  1770. }
  1771. // Update the label that shows how many filters are in place
  1772. this._updateFilterCount();
  1773. // If the length of the selections are different then some of them have been removed and a deselect has occured
  1774. if (newSelectionList.length > 0 && (newSelectionList.length < this.s.selectionList.length || rebuild)) {
  1775. this._cascadeRegen(newSelectionList);
  1776. var last = newSelectionList[newSelectionList.length - 1].index;
  1777. for (var _k = 0, _l = this.s.panes; _k < _l.length; _k++) {
  1778. var pane = _l[_k];
  1779. pane.s.lastSelect = (pane.s.index === last);
  1780. }
  1781. }
  1782. else if (newSelectionList.length > 0) {
  1783. // Update all of the other panes as you would just making a normal selection
  1784. for (var _m = 0, _o = this.s.panes; _m < _o.length; _m++) {
  1785. var paneUpdate = _o[_m];
  1786. if (paneUpdate.s.dtPane !== undefined) {
  1787. var tempFilter = true;
  1788. paneUpdate.s.filteringActive = true;
  1789. if ((filterPane !== -1 && filterPane !== null && filterPane === paneUpdate.s.index) || filterActive === false) {
  1790. tempFilter = false;
  1791. paneUpdate.s.filteringActive = false;
  1792. }
  1793. paneUpdate.updatePane(!tempFilter ? tempFilter : filterActive);
  1794. }
  1795. }
  1796. }
  1797. }
  1798. else {
  1799. var solePane = -1;
  1800. if (newSelectionList.length === 1) {
  1801. solePane = newSelectionList[0].index;
  1802. }
  1803. for (var _p = 0, _q = this.s.panes; _p < _q.length; _p++) {
  1804. var pane = _q[_p];
  1805. if (pane.s.dtPane !== undefined) {
  1806. var tempFilter = true;
  1807. pane.s.filteringActive = true;
  1808. if ((filterPane !== -1 && filterPane !== null && filterPane === pane.s.index) ||
  1809. filterActive === false ||
  1810. pane.s.index === solePane) {
  1811. tempFilter = false;
  1812. pane.s.filteringActive = false;
  1813. }
  1814. pane.updatePane(!tempFilter ? tempFilter : filterActive);
  1815. }
  1816. }
  1817. // Update the label that shows how many filters are in place
  1818. this._updateFilterCount();
  1819. }
  1820. if (!filterActive) {
  1821. this.s.selectionList = [];
  1822. }
  1823. }
  1824. };
  1825. /**
  1826. * Attach the panes, buttons and title to the document
  1827. */
  1828. SearchPanes.prototype._attach = function () {
  1829. var _this = this;
  1830. $$1(this.dom.container).removeClass(this.classes.hide);
  1831. $$1(this.dom.titleRow).removeClass(this.classes.hide);
  1832. $$1(this.dom.titleRow).remove();
  1833. $$1(this.dom.title).appendTo(this.dom.titleRow);
  1834. // If the clear button is permitted attach it
  1835. if (this.c.clear) {
  1836. $$1(this.dom.clearAll).appendTo(this.dom.titleRow);
  1837. $$1(this.dom.clearAll).on('click.dtsps', function () {
  1838. _this.clearSelections();
  1839. });
  1840. }
  1841. $$1(this.dom.titleRow).appendTo(this.dom.container);
  1842. // Attach the container for each individual pane to the overall container
  1843. for (var _i = 0, _a = this.s.panes; _i < _a.length; _i++) {
  1844. var pane = _a[_i];
  1845. $$1(pane.dom.container).appendTo(this.dom.panes);
  1846. }
  1847. // Attach everything to the document
  1848. $$1(this.dom.panes).appendTo(this.dom.container);
  1849. if ($$1('div.' + this.classes.container).length === 0) {
  1850. $$1(this.dom.container).prependTo(this.s.dt);
  1851. }
  1852. return this.dom.container;
  1853. };
  1854. /**
  1855. * Attach the top row containing the filter count and clear all button
  1856. */
  1857. SearchPanes.prototype._attachExtras = function () {
  1858. $$1(this.dom.container).removeClass(this.classes.hide);
  1859. $$1(this.dom.titleRow).removeClass(this.classes.hide);
  1860. $$1(this.dom.titleRow).remove();
  1861. $$1(this.dom.title).appendTo(this.dom.titleRow);
  1862. // If the clear button is permitted attach it
  1863. if (this.c.clear) {
  1864. $$1(this.dom.clearAll).appendTo(this.dom.titleRow);
  1865. }
  1866. $$1(this.dom.titleRow).appendTo(this.dom.container);
  1867. return this.dom.container;
  1868. };
  1869. /**
  1870. * If there are no panes to display then this method is called to either
  1871. * display a message in their place or hide them completely.
  1872. */
  1873. SearchPanes.prototype._attachMessage = function () {
  1874. // Create a message to display on the screen
  1875. var message;
  1876. try {
  1877. message = this.s.dt.i18n('searchPanes.emptyPanes', 'No SearchPanes');
  1878. }
  1879. catch (error) {
  1880. message = null;
  1881. }
  1882. // If the message is an empty string then searchPanes.emptyPanes is undefined,
  1883. // therefore the pane container should be removed from the display
  1884. if (message === null) {
  1885. $$1(this.dom.container).addClass(this.classes.hide);
  1886. $$1(this.dom.titleRow).removeClass(this.classes.hide);
  1887. return;
  1888. }
  1889. else {
  1890. $$1(this.dom.container).removeClass(this.classes.hide);
  1891. $$1(this.dom.titleRow).addClass(this.classes.hide);
  1892. }
  1893. // Otherwise display the message
  1894. $$1(this.dom.emptyMessage).text(message);
  1895. this.dom.emptyMessage.appendTo(this.dom.container);
  1896. return this.dom.container;
  1897. };
  1898. /**
  1899. * Attaches the panes to the document and displays a message or hides if there are none
  1900. */
  1901. SearchPanes.prototype._attachPaneContainer = function () {
  1902. // If a pane is to be displayed then attach the normal pane output
  1903. for (var _i = 0, _a = this.s.panes; _i < _a.length; _i++) {
  1904. var pane = _a[_i];
  1905. if (pane.s.displayed === true) {
  1906. return this._attach();
  1907. }
  1908. }
  1909. // Otherwise attach the custom message or remove the container from the display
  1910. return this._attachMessage();
  1911. };
  1912. /**
  1913. * Prepares the panes for selections to be made when cascade is active and a deselect has occured
  1914. * @param newSelectionList the list of selections which are to be made
  1915. */
  1916. SearchPanes.prototype._cascadeRegen = function (newSelectionList) {
  1917. // Set this to true so that the actions taken do not cause this to run until it is finished
  1918. this.regenerating = true;
  1919. // If only one pane has been selected then take note of its index
  1920. var solePane = -1;
  1921. if (newSelectionList.length === 1) {
  1922. solePane = newSelectionList[0].index;
  1923. }
  1924. // Let the pane know that a cascadeRegen is taking place to avoid unexpected behaviour
  1925. // and clear all of the previous selections in the pane
  1926. for (var _i = 0, _a = this.s.panes; _i < _a.length; _i++) {
  1927. var pane = _a[_i];
  1928. pane.setCascadeRegen(true);
  1929. pane.setClear(true);
  1930. // If this is the same as the pane with the only selection then pass it as a parameter into clearPane
  1931. if ((pane.s.dtPane !== undefined && pane.s.index === solePane) || pane.s.dtPane !== undefined) {
  1932. pane.clearPane();
  1933. }
  1934. pane.setClear(false);
  1935. }
  1936. // Remake Selections
  1937. this._makeCascadeSelections(newSelectionList);
  1938. // Set the selection list property to be the list without the selections from the deselect pane
  1939. this.s.selectionList = newSelectionList;
  1940. // The regeneration of selections is over so set it back to false
  1941. for (var _b = 0, _c = this.s.panes; _b < _c.length; _b++) {
  1942. var pane = _c[_b];
  1943. pane.setCascadeRegen(false);
  1944. }
  1945. this.regenerating = false;
  1946. };
  1947. /**
  1948. * Attaches the message to the document but does not add any panes
  1949. */
  1950. SearchPanes.prototype._checkMessage = function () {
  1951. // If a pane is to be displayed then attach the normal pane output
  1952. for (var _i = 0, _a = this.s.panes; _i < _a.length; _i++) {
  1953. var pane = _a[_i];
  1954. if (pane.s.displayed === true) {
  1955. return;
  1956. }
  1957. }
  1958. // Otherwise attach the custom message or remove the container from the display
  1959. return this._attachMessage();
  1960. };
  1961. /**
  1962. * Gets the selection list from the previous state and stores it in the selectionList Property
  1963. */
  1964. SearchPanes.prototype._getState = function () {
  1965. var loadedFilter = this.s.dt.state.loaded();
  1966. if (loadedFilter && loadedFilter.searchPanes && loadedFilter.searchPanes.selectionList !== undefined) {
  1967. this.s.selectionList = loadedFilter.searchPanes.selectionList;
  1968. }
  1969. };
  1970. /**
  1971. * Makes all of the selections when cascade is active
  1972. * @param newSelectionList the list of selections to be made, in the order they were originally selected
  1973. */
  1974. SearchPanes.prototype._makeCascadeSelections = function (newSelectionList) {
  1975. // make selections in the order they were made previously, excluding those from the pane where a deselect was made
  1976. for (var i = 0; i < newSelectionList.length; i++) {
  1977. var _loop_1 = function (pane) {
  1978. if (pane.s.index === newSelectionList[i].index && pane.s.dtPane !== undefined) {
  1979. // When regenerating the cascade selections we need this flag so that the panes are only ignored if it
  1980. // is the last selection and the pane for that selection
  1981. if (i === newSelectionList.length - 1) {
  1982. pane.s.lastCascade = true;
  1983. }
  1984. // if there are any selections currently in the pane then deselect them as we are about to make our new selections
  1985. if (pane.s.dtPane.rows({ selected: true }).data().toArray().length > 0 && pane.s.dtPane !== undefined) {
  1986. pane.setClear(true);
  1987. pane.clearPane();
  1988. pane.setClear(false);
  1989. }
  1990. var _loop_2 = function (row) {
  1991. pane.s.dtPane.rows().every(function (rowIdx) {
  1992. if (pane.s.dtPane.row(rowIdx).data() !== undefined &&
  1993. row !== undefined &&
  1994. pane.s.dtPane.row(rowIdx).data().filter === row.filter) {
  1995. pane.s.dtPane.row(rowIdx).select();
  1996. }
  1997. });
  1998. };
  1999. // select every row in the pane that was selected previously
  2000. for (var _i = 0, _a = newSelectionList[i].rows; _i < _a.length; _i++) {
  2001. var row = _a[_i];
  2002. _loop_2(row);
  2003. }
  2004. // Update the label that shows how many filters are in place
  2005. this_1._updateFilterCount();
  2006. pane.s.lastCascade = false;
  2007. }
  2008. };
  2009. var this_1 = this;
  2010. // As the selections may have been made across the panes in a different order to the pane index we must identify
  2011. // which pane has the index of the selection. This is also important for colreorder etc
  2012. for (var _i = 0, _a = this.s.panes; _i < _a.length; _i++) {
  2013. var pane = _a[_i];
  2014. _loop_1(pane);
  2015. }
  2016. }
  2017. // Make sure that the state is saved after all of these selections
  2018. this.s.dt.state.save();
  2019. };
  2020. /**
  2021. * Declares the instances of individual searchpanes dependant on the number of columns.
  2022. * It is necessary to run this once preInit has completed otherwise no panes will be
  2023. * created as the column count will be 0.
  2024. * @param table the DataTable api for the parent table
  2025. * @param paneSettings the settings passed into the constructor
  2026. * @param opts the options passed into the constructor
  2027. */
  2028. SearchPanes.prototype._paneDeclare = function (table, paneSettings, opts) {
  2029. var _this = this;
  2030. // Create Panes
  2031. table
  2032. .columns(this.c.columns.length > 0 ? this.c.columns : undefined)
  2033. .eq(0)
  2034. .each(function (idx) {
  2035. _this.s.panes.push(new SearchPane(paneSettings, opts, idx, _this.c.layout, _this.dom.panes));
  2036. });
  2037. // If there is any extra custom panes defined then create panes for them too
  2038. var rowLength = table.columns().eq(0).toArray().length;
  2039. var paneLength = this.c.panes.length;
  2040. for (var i = 0; i < paneLength; i++) {
  2041. var id = rowLength + i;
  2042. this.s.panes.push(new SearchPane(paneSettings, opts, id, this.c.layout, this.dom.panes, this.c.panes[i]));
  2043. }
  2044. // If a custom ordering is being used
  2045. if (this.c.order.length > 0) {
  2046. // Make a new Array of panes based upon the order
  2047. var newPanes = this.c.order.map(function (name, index, values) {
  2048. return _this._findPane(name);
  2049. });
  2050. // Remove the old panes from the dom
  2051. this.dom.panes.empty();
  2052. this.s.panes = newPanes;
  2053. // Append the panes in the correct order
  2054. for (var _i = 0, _a = this.s.panes; _i < _a.length; _i++) {
  2055. var pane = _a[_i];
  2056. this.dom.panes.append(pane.dom.container);
  2057. }
  2058. }
  2059. // If this internal property is true then the DataTable has been initialised already
  2060. if (this.s.dt.settings()[0]._bInitComplete) {
  2061. this._startup(table);
  2062. }
  2063. else {
  2064. // Otherwise add the paneStartup function to the list of functions that are to be run when the table is initialised
  2065. // This will garauntee that the panes are initialised before the init event and init Complete callback is fired
  2066. this.s.dt.settings()[0].aoInitComplete.push({ fn: function () {
  2067. _this._startup(table);
  2068. } });
  2069. }
  2070. };
  2071. /**
  2072. * Finds a pane based upon the name of that pane
  2073. * @param name string representing the name of the pane
  2074. * @returns SearchPane The pane which has that name
  2075. */
  2076. SearchPanes.prototype._findPane = function (name) {
  2077. for (var _i = 0, _a = this.s.panes; _i < _a.length; _i++) {
  2078. var pane = _a[_i];
  2079. if (name === pane.s.name) {
  2080. return pane;
  2081. }
  2082. }
  2083. };
  2084. /**
  2085. * Works out which panes to update when data is recieved from the server and viewTotal is active
  2086. */
  2087. SearchPanes.prototype._serverTotals = function () {
  2088. var selectPresent = false;
  2089. var deselectPresent = false;
  2090. var table = this.s.dt;
  2091. for (var _i = 0, _a = this.s.panes; _i < _a.length; _i++) {
  2092. var pane = _a[_i];
  2093. // Identify the pane where a selection or deselection has been made and add it to the list.
  2094. if (pane.s.selectPresent) {
  2095. this.s.selectionList.push({ index: pane.s.index, rows: pane.s.dtPane.rows({ selected: true }).data().toArray(), protect: false });
  2096. table.state.save();
  2097. pane.s.selectPresent = false;
  2098. selectPresent = true;
  2099. break;
  2100. }
  2101. else if (pane.s.deselect) {
  2102. var selectedData = pane.s.dtPane.rows({ selected: true }).data().toArray();
  2103. if (selectedData.length > 0) {
  2104. this.s.selectionList.push({ index: pane.s.index, rows: selectedData, protect: true });
  2105. }
  2106. selectPresent = true;
  2107. deselectPresent = true;
  2108. }
  2109. }
  2110. // Build an updated list based on any selections or deselections added
  2111. if (!selectPresent) {
  2112. this.s.selectionList = [];
  2113. }
  2114. else {
  2115. var newSelectionList = [];
  2116. for (var i = 0; i < this.s.selectionList.length; i++) {
  2117. var further = false;
  2118. // Find out if this selection is the last one in the list for that pane
  2119. for (var j = i + 1; j < this.s.selectionList.length; j++) {
  2120. if (this.s.selectionList[j].index === this.s.selectionList[i].index) {
  2121. further = true;
  2122. }
  2123. }
  2124. // If there are no selections for this pane in the list then just push this one
  2125. if (!further) {
  2126. var push = false;
  2127. for (var _b = 0, _c = this.s.panes; _b < _c.length; _b++) {
  2128. var pane = _c[_b];
  2129. if (pane.s.index === this.s.selectionList[i].index &&
  2130. pane.s.dtPane.rows({ selected: true }).data().toArray().length > 0) {
  2131. push = true;
  2132. }
  2133. }
  2134. if (push) {
  2135. newSelectionList.push(this.s.selectionList[i]);
  2136. }
  2137. }
  2138. }
  2139. this.s.selectionList = newSelectionList;
  2140. }
  2141. var initIdx = -1;
  2142. // If there has been a deselect and only one pane has a selection then update everything
  2143. if (deselectPresent && this.s.selectionList.length === 1) {
  2144. for (var _d = 0, _e = this.s.panes; _d < _e.length; _d++) {
  2145. var pane = _e[_d];
  2146. pane.s.lastSelect = false;
  2147. pane.s.deselect = false;
  2148. if (pane.s.dtPane !== undefined && pane.s.dtPane.rows({ selected: true }).data().toArray().length > 0) {
  2149. initIdx = pane.s.index;
  2150. }
  2151. }
  2152. }
  2153. // Otherwise if there are more 1 selections then find the last one and set it to not update that pane
  2154. else if (this.s.selectionList.length > 0) {
  2155. var last = this.s.selectionList[this.s.selectionList.length - 1].index;
  2156. for (var _f = 0, _g = this.s.panes; _f < _g.length; _f++) {
  2157. var pane = _g[_f];
  2158. pane.s.lastSelect = (pane.s.index === last);
  2159. pane.s.deselect = false;
  2160. }
  2161. }
  2162. // Otherwise if there are no selections then find where that took place and do not update to maintain scrolling
  2163. else if (this.s.selectionList.length === 0) {
  2164. for (var _h = 0, _j = this.s.panes; _h < _j.length; _h++) {
  2165. var pane = _j[_h];
  2166. // pane.s.lastSelect = (pane.s.deselect === true);
  2167. pane.s.lastSelect = false;
  2168. pane.s.deselect = false;
  2169. }
  2170. }
  2171. $$1(this.dom.panes).empty();
  2172. // Rebuild the desired panes
  2173. for (var _k = 0, _l = this.s.panes; _k < _l.length; _k++) {
  2174. var pane = _l[_k];
  2175. if (!pane.s.lastSelect) {
  2176. pane.rebuildPane(undefined, this.s.dt.page.info().serverSide ? this.s.serverData : undefined, pane.s.index === initIdx ? true : null, true);
  2177. }
  2178. else {
  2179. pane._setListeners();
  2180. }
  2181. // append all of the panes and enable select
  2182. $$1(this.dom.panes).append(pane.dom.container);
  2183. if (pane.s.dtPane !== undefined) {
  2184. $$1(pane.s.dtPane.table().node()).parent()[0].scrollTop = pane.s.scrollTop;
  2185. $$1.fn.dataTable.select.init(pane.s.dtPane);
  2186. }
  2187. }
  2188. // Only need to trigger a search if it is not server side processing
  2189. if (!this.s.dt.page.info().serverSide) {
  2190. this.s.dt.draw();
  2191. }
  2192. };
  2193. /**
  2194. * Initialises the tables previous/preset selections and initialises callbacks for events
  2195. * @param table the parent table for which the searchPanes are being created
  2196. */
  2197. SearchPanes.prototype._startup = function (table) {
  2198. var _this = this;
  2199. $$1(this.dom.container).text('');
  2200. // Attach clear button and title bar to the document
  2201. this._attachExtras();
  2202. $$1(this.dom.container).append(this.dom.panes);
  2203. $$1(this.dom.panes).empty();
  2204. var loadedFilter = this.s.dt.state.loaded();
  2205. if (this.c.viewTotal && !this.c.cascadePanes) {
  2206. if (loadedFilter !== null &&
  2207. loadedFilter !== undefined &&
  2208. loadedFilter.searchPanes !== undefined &&
  2209. loadedFilter.searchPanes.panes !== undefined) {
  2210. var filterActive = false;
  2211. for (var _i = 0, _a = loadedFilter.searchPanes.panes; _i < _a.length; _i++) {
  2212. var pane = _a[_i];
  2213. if (pane.selected.length > 0) {
  2214. filterActive = true;
  2215. break;
  2216. }
  2217. }
  2218. if (filterActive) {
  2219. for (var _b = 0, _c = this.s.panes; _b < _c.length; _b++) {
  2220. var pane = _c[_b];
  2221. pane.s.showFiltered = true;
  2222. }
  2223. }
  2224. }
  2225. }
  2226. for (var _d = 0, _e = this.s.panes; _d < _e.length; _d++) {
  2227. var pane = _e[_d];
  2228. pane.rebuildPane(undefined, Object.keys(this.s.serverData).length > 0 ? this.s.serverData : undefined);
  2229. $$1(this.dom.panes).append(pane.dom.container);
  2230. }
  2231. // Only need to trigger a search if it is not server side processing
  2232. if (!this.s.dt.page.info().serverSide) {
  2233. this.s.dt.draw();
  2234. }
  2235. // Reset the paging if that has been saved in the state
  2236. if (!this.s.stateRead && loadedFilter !== null && loadedFilter !== undefined) {
  2237. this.s.dt.page((loadedFilter.start / this.s.dt.page.len()));
  2238. this.s.dt.draw('page');
  2239. }
  2240. this.s.stateRead = true;
  2241. if (this.c.viewTotal && !this.c.cascadePanes) {
  2242. for (var _f = 0, _g = this.s.panes; _f < _g.length; _f++) {
  2243. var pane = _g[_f];
  2244. pane.updatePane();
  2245. }
  2246. }
  2247. this._updateFilterCount();
  2248. this._checkMessage();
  2249. // When a draw is called on the DataTable, update all of the panes incase the data in the DataTable has changed
  2250. table.on('preDraw.dtsps', function () {
  2251. _this._updateFilterCount();
  2252. if ((_this.c.cascadePanes || _this.c.viewTotal) && !_this.s.dt.page.info().serverSide) {
  2253. _this.redrawPanes();
  2254. }
  2255. else {
  2256. _this._updateSelection();
  2257. }
  2258. _this.s.filterPane = -1;
  2259. });
  2260. // Whenever a state save occurs store the selection list in the state object
  2261. this.s.dt.on('stateSaveParams.dtsp', function (e, settings, data) {
  2262. if (data.searchPanes === undefined) {
  2263. data.searchPanes = {};
  2264. }
  2265. data.searchPanes.selectionList = _this.s.selectionList;
  2266. });
  2267. if (this.s.dt.page.info().serverSide) {
  2268. table.off('page');
  2269. table.on('page', function () {
  2270. _this.s.page = _this.s.dt.page();
  2271. });
  2272. table.off('preXhr.dt');
  2273. table.on('preXhr.dt', function (e, settings, data) {
  2274. if (data.searchPanes === undefined) {
  2275. data.searchPanes = {};
  2276. }
  2277. // Count how many filters are being applied
  2278. var filterCount = 0;
  2279. for (var _i = 0, _a = _this.s.panes; _i < _a.length; _i++) {
  2280. var pane = _a[_i];
  2281. var src = _this.s.dt.column(pane.s.index).dataSrc();
  2282. if (data.searchPanes[src] === undefined) {
  2283. data.searchPanes[src] = {};
  2284. }
  2285. if (pane.s.dtPane !== undefined) {
  2286. var rowData = pane.s.dtPane.rows({ selected: true }).data().toArray();
  2287. for (var i = 0; i < rowData.length; i++) {
  2288. data.searchPanes[src][i] = rowData[i].filter;
  2289. filterCount++;
  2290. }
  2291. }
  2292. }
  2293. if (_this.c.viewTotal) {
  2294. _this._prepViewTotal();
  2295. }
  2296. // If there is a filter to be applied, then we need to read from the start of the result set
  2297. // and set the paging to 0. This matches the behaviour of client side processing
  2298. if (filterCount > 0) {
  2299. // If the number of filters has changed we need to read from the start of the result set and reset the paging
  2300. if (filterCount !== _this.s.filterCount) {
  2301. data.start = 0;
  2302. _this.s.page = 0;
  2303. }
  2304. // Otherwise it is a paging request and we need to read from whatever the paging has been set to
  2305. else {
  2306. data.start = _this.s.page * _this.s.dt.page.len();
  2307. }
  2308. _this.s.dt.page(_this.s.page);
  2309. _this.s.filterCount = filterCount;
  2310. }
  2311. });
  2312. }
  2313. else {
  2314. table.on('preXhr.dt', function (e, settings, data) {
  2315. for (var _i = 0, _a = _this.s.panes; _i < _a.length; _i++) {
  2316. var pane = _a[_i];
  2317. pane.clearData();
  2318. }
  2319. });
  2320. }
  2321. // If the data is reloaded from the server then it is possible that it has changed completely,
  2322. // so we need to rebuild the panes
  2323. this.s.dt.on('xhr', function (e, settings, json, xhr) {
  2324. var processing = false;
  2325. if (!_this.s.dt.page.info().serverSide) {
  2326. _this.s.dt.one('preDraw', function () {
  2327. if (processing) {
  2328. return;
  2329. }
  2330. var page = _this.s.dt.page();
  2331. processing = true;
  2332. $$1(_this.dom.panes).empty();
  2333. for (var _i = 0, _a = _this.s.panes; _i < _a.length; _i++) {
  2334. var pane = _a[_i];
  2335. pane.clearData(); // Clears all of the bins and will mean that the data has to be re-read
  2336. // Pass a boolean to say whether this is the last choice made for maintaining selections when rebuilding
  2337. pane.rebuildPane(_this.s.selectionList[_this.s.selectionList.length - 1] !== undefined ?
  2338. pane.s.index === _this.s.selectionList[_this.s.selectionList.length - 1].index :
  2339. false, undefined, undefined, true);
  2340. $$1(_this.dom.panes).append(pane.dom.container);
  2341. }
  2342. if (!_this.s.dt.page.info().serverSide) {
  2343. _this.s.dt.draw();
  2344. }
  2345. if (_this.c.cascadePanes || _this.c.viewTotal) {
  2346. _this.redrawPanes(_this.c.cascadePanes);
  2347. }
  2348. else {
  2349. _this._updateSelection();
  2350. }
  2351. _this._checkMessage();
  2352. _this.s.dt.one('draw', function () {
  2353. _this.s.dt.page(page).draw(false);
  2354. });
  2355. });
  2356. }
  2357. });
  2358. // PreSelect any selections which have been defined using the preSelect option
  2359. for (var _h = 0, _j = this.s.panes; _h < _j.length; _h++) {
  2360. var pane = _j[_h];
  2361. if (pane !== undefined &&
  2362. pane.s.dtPane !== undefined &&
  2363. ((pane.s.colOpts.preSelect !== undefined && pane.s.colOpts.preSelect.length > 0) ||
  2364. (pane.customPaneSettings !== null &&
  2365. pane.customPaneSettings.preSelect !== undefined &&
  2366. pane.customPaneSettings.preSelect.length > 0))) {
  2367. var tableLength = pane.s.dtPane.rows().data().toArray().length;
  2368. for (var i = 0; i < tableLength; i++) {
  2369. if (pane.s.colOpts.preSelect.indexOf(pane.s.dtPane.cell(i, 0).data()) !== -1 ||
  2370. (pane.customPaneSettings !== null &&
  2371. pane.customPaneSettings.preSelect !== undefined &&
  2372. pane.customPaneSettings.preSelect.indexOf(pane.s.dtPane.cell(i, 0).data()) !== -1)) {
  2373. pane.s.dtPane.row(i).select();
  2374. }
  2375. }
  2376. pane.updateTable();
  2377. }
  2378. }
  2379. if (this.s.selectionList !== undefined && this.s.selectionList.length > 0) {
  2380. var last = this.s.selectionList[this.s.selectionList.length - 1].index;
  2381. for (var _k = 0, _l = this.s.panes; _k < _l.length; _k++) {
  2382. var pane = _l[_k];
  2383. pane.s.lastSelect = (pane.s.index === last);
  2384. }
  2385. }
  2386. // If cascadePanes is active then make the previous selections in the order they were previously
  2387. if (this.s.selectionList.length > 0 && this.c.cascadePanes) {
  2388. this._cascadeRegen(this.s.selectionList);
  2389. }
  2390. // Update the title bar to show how many filters have been selected
  2391. this._updateFilterCount();
  2392. // If the table is destroyed and restarted then clear the selections so that they do not persist.
  2393. table.on('destroy.dtsps', function () {
  2394. for (var _i = 0, _a = _this.s.panes; _i < _a.length; _i++) {
  2395. var pane = _a[_i];
  2396. pane.destroy();
  2397. }
  2398. table.off('.dtsps');
  2399. $$1(_this.dom.clearAll).off('.dtsps');
  2400. $$1(_this.dom.container).remove();
  2401. _this.clearSelections();
  2402. });
  2403. // When the clear All button has been pressed clear all of the selections in the panes
  2404. if (this.c.clear) {
  2405. $$1(this.dom.clearAll).on('click.dtsps', function () {
  2406. _this.clearSelections();
  2407. });
  2408. }
  2409. table.settings()[0]._searchPanes = this;
  2410. };
  2411. SearchPanes.prototype._prepViewTotal = function () {
  2412. var filterPane = this.s.filterPane;
  2413. var filterActive = false;
  2414. for (var _i = 0, _a = this.s.panes; _i < _a.length; _i++) {
  2415. var pane = _a[_i];
  2416. if (pane.s.dtPane !== undefined) {
  2417. var selectLength = pane.s.dtPane.rows({ selected: true }).data().toArray().length;
  2418. // If filterPane === -1 then a pane with a selection has not been found yet, so set filterPane to that panes index
  2419. if (selectLength > 0 && filterPane === -1) {
  2420. filterPane = pane.s.index;
  2421. filterActive = true;
  2422. }
  2423. // Then if another pane is found with a selection then set filterPane to null to
  2424. // show that multiple panes have selections present
  2425. else if (selectLength > 0) {
  2426. filterPane = null;
  2427. }
  2428. }
  2429. }
  2430. // Update all of the panes to reflect the current state of the filters
  2431. for (var _b = 0, _c = this.s.panes; _b < _c.length; _b++) {
  2432. var pane = _c[_b];
  2433. if (pane.s.dtPane !== undefined) {
  2434. pane.s.filteringActive = true;
  2435. if ((filterPane !== -1 && filterPane !== null && filterPane === pane.s.index) || filterActive === false) {
  2436. pane.s.filteringActive = false;
  2437. }
  2438. }
  2439. }
  2440. };
  2441. /**
  2442. * Updates the number of filters that have been applied in the title
  2443. */
  2444. SearchPanes.prototype._updateFilterCount = function () {
  2445. var filterCount = 0;
  2446. // Add the number of all of the filters throughout the panes
  2447. for (var _i = 0, _a = this.s.panes; _i < _a.length; _i++) {
  2448. var pane = _a[_i];
  2449. if (pane.s.dtPane !== undefined) {
  2450. filterCount += pane.getPaneCount();
  2451. }
  2452. }
  2453. // Run the message through the internationalisation method to improve readability
  2454. var message = this.s.dt.i18n('searchPanes.title', 'Filters Active - %d', filterCount);
  2455. $$1(this.dom.title).text(message);
  2456. if (this.c.filterChanged !== undefined && typeof this.c.filterChanged === 'function') {
  2457. this.c.filterChanged.call(this.s.dt, filterCount);
  2458. }
  2459. };
  2460. /**
  2461. * Updates the selectionList when cascade is not in place
  2462. */
  2463. SearchPanes.prototype._updateSelection = function () {
  2464. this.s.selectionList = [];
  2465. for (var _i = 0, _a = this.s.panes; _i < _a.length; _i++) {
  2466. var pane = _a[_i];
  2467. if (pane.s.dtPane !== undefined) {
  2468. this.s.selectionList.push({ index: pane.s.index, rows: pane.s.dtPane.rows({ selected: true }).data().toArray(), protect: false });
  2469. }
  2470. }
  2471. this.s.dt.state.save();
  2472. };
  2473. SearchPanes.version = '1.2.1';
  2474. SearchPanes.classes = {
  2475. clear: 'dtsp-clear',
  2476. clearAll: 'dtsp-clearAll',
  2477. container: 'dtsp-searchPanes',
  2478. emptyMessage: 'dtsp-emptyMessage',
  2479. hide: 'dtsp-hidden',
  2480. panes: 'dtsp-panesContainer',
  2481. search: 'dtsp-search',
  2482. title: 'dtsp-title',
  2483. titleRow: 'dtsp-titleRow'
  2484. };
  2485. // Define SearchPanes default options
  2486. SearchPanes.defaults = {
  2487. cascadePanes: false,
  2488. clear: true,
  2489. container: function (dt) {
  2490. return dt.table().container();
  2491. },
  2492. columns: [],
  2493. filterChanged: undefined,
  2494. layout: 'columns-3',
  2495. order: [],
  2496. panes: [],
  2497. viewTotal: false
  2498. };
  2499. return SearchPanes;
  2500. }());
  2501. /*! SearchPanes 1.2.1
  2502. * 2019-2020 SpryMedia Ltd - datatables.net/license
  2503. */
  2504. // DataTables extensions common UMD. Note that this allows for AMD, CommonJS
  2505. // (with window and jQuery being allowed as parameters to the returned
  2506. // function) or just default browser loading.
  2507. (function (factory) {
  2508. if (typeof define === 'function' && define.amd) {
  2509. // AMD
  2510. define(['jquery', 'datatables.net'], function ($) {
  2511. return factory($, window, document);
  2512. });
  2513. }
  2514. else if (typeof exports === 'object') {
  2515. // CommonJS
  2516. module.exports = function (root, $) {
  2517. if (!root) {
  2518. root = window;
  2519. }
  2520. if (!$ || !$.fn.dataTable) {
  2521. $ = require('datatables.net')(root, $).$;
  2522. }
  2523. return factory($, root, root.document);
  2524. };
  2525. }
  2526. else {
  2527. // Browser - assume jQuery has already been loaded
  2528. factory(window.jQuery, window, document);
  2529. }
  2530. }(function ($, window, document) {
  2531. setJQuery($);
  2532. setJQuery$1($);
  2533. var DataTable = $.fn.dataTable;
  2534. $.fn.dataTable.SearchPanes = SearchPanes;
  2535. $.fn.DataTable.SearchPanes = SearchPanes;
  2536. $.fn.dataTable.SearchPane = SearchPane;
  2537. $.fn.DataTable.SearchPane = SearchPane;
  2538. var apiRegister = $.fn.dataTable.Api.register;
  2539. apiRegister('searchPanes()', function () {
  2540. return this;
  2541. });
  2542. apiRegister('searchPanes.clearSelections()', function () {
  2543. return this.iterator('table', function (ctx) {
  2544. if (ctx._searchPanes) {
  2545. ctx._searchPanes.clearSelections();
  2546. }
  2547. });
  2548. });
  2549. apiRegister('searchPanes.rebuildPane()', function (targetIdx, maintainSelections) {
  2550. return this.iterator('table', function (ctx) {
  2551. if (ctx._searchPanes) {
  2552. ctx._searchPanes.rebuild(targetIdx, maintainSelections);
  2553. }
  2554. });
  2555. });
  2556. apiRegister('searchPanes.container()', function () {
  2557. var ctx = this.context[0];
  2558. return ctx._searchPanes
  2559. ? ctx._searchPanes.getNode()
  2560. : null;
  2561. });
  2562. $.fn.dataTable.ext.buttons.searchPanesClear = {
  2563. text: 'Clear Panes',
  2564. action: function (e, dt, node, config) {
  2565. dt.searchPanes.clearSelections();
  2566. }
  2567. };
  2568. $.fn.dataTable.ext.buttons.searchPanes = {
  2569. action: function (e, dt, node, config) {
  2570. e.stopPropagation();
  2571. this.popover(config._panes.getNode(), {
  2572. align: 'dt-container'
  2573. });
  2574. config._panes.rebuild(undefined, true);
  2575. },
  2576. config: {},
  2577. init: function (dt, node, config) {
  2578. var panes = new $.fn.dataTable.SearchPanes(dt, $.extend({
  2579. filterChanged: function (count) {
  2580. dt.button(node).text(dt.i18n('searchPanes.collapse', { 0: 'SearchPanes', _: 'SearchPanes (%d)' }, count));
  2581. }
  2582. }, config.config));
  2583. var message = dt.i18n('searchPanes.collapse', 'SearchPanes', 0);
  2584. dt.button(node).text(message);
  2585. config._panes = panes;
  2586. },
  2587. text: 'Search Panes'
  2588. };
  2589. function _init(settings, fromPre) {
  2590. if (fromPre === void 0) { fromPre = false; }
  2591. var api = new DataTable.Api(settings);
  2592. var opts = api.init().searchPanes || DataTable.defaults.searchPanes;
  2593. var searchPanes = new SearchPanes(api, opts, fromPre);
  2594. var node = searchPanes.getNode();
  2595. return node;
  2596. }
  2597. // Attach a listener to the document which listens for DataTables initialisation
  2598. // events so we can automatically initialise
  2599. $(document).on('preInit.dt.dtsp', function (e, settings, json) {
  2600. if (e.namespace !== 'dt') {
  2601. return;
  2602. }
  2603. if (settings.oInit.searchPanes ||
  2604. DataTable.defaults.searchPanes) {
  2605. if (!settings._searchPanes) {
  2606. _init(settings, true);
  2607. }
  2608. }
  2609. });
  2610. // DataTables `dom` feature option
  2611. DataTable.ext.feature.push({
  2612. cFeature: 'P',
  2613. fnInit: _init
  2614. });
  2615. // DataTables 2 layout feature
  2616. if (DataTable.ext.features) {
  2617. DataTable.ext.features.register('searchPanes', _init);
  2618. }
  2619. }));
  2620. }());