dataTables.searchPanes.js 126 KB

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