You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dataTables.searchPanes.js 130KB

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