datatable.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // jQuery UI style "widget" for selecting and sorting "fields"
  2. $(function()
  3. {
  4. // the widget definition, where "itop" is the namespace,
  5. // "datatable" the widget name
  6. $.widget( "itop.datatable",
  7. {
  8. // default options
  9. options:
  10. {
  11. sPersistentId: '',
  12. sFilter: '',
  13. oColumns: {},
  14. sSelectMode: '',
  15. sViewLink: 'true',
  16. iNbObjects: 0,
  17. iDefaultPageSize: -1,
  18. iPageSize: -1,
  19. iPageIndex: 0,
  20. oClassAliases: {},
  21. sTableId : null,
  22. oExtraParams: {},
  23. sRenderUrl: 'index.php',
  24. oRenderParameters: {},
  25. oDefaultSettings: {},
  26. oLabels: { moveup: 'Move Up', movedown: 'Move Down' }
  27. },
  28. // the constructor
  29. _create: function()
  30. {
  31. this.aDlgStateParams = ['iDefaultPageSize', 'oColumns'];
  32. this.element
  33. .addClass('itop-datatable');
  34. var me = this;
  35. var sId = new String(this.element.attr('id'));
  36. var sListId = sId.replace('datatable_', '');
  37. var bViewLink = (this.options.sViewLink == 'true');
  38. $('#sfl_'+sListId).fieldsorter({hasKeyColumn: bViewLink, labels: this.options.oLabels, fields: this.options.oColumns, onChange: function() { me._onSpecificSettings(); } });
  39. $('#datatable_dlg_'+sListId).find('input[name=page_size]').click(function() { me._onSpecificSettings(); });
  40. $('#datatable_dlg_'+sListId).find('input[name=save_settings]').click(function() { me._updateSaveScope(); });
  41. this.element.find('.itop_popup > ul li').popupmenu();
  42. this._updateSaveScope();
  43. this._saveDlgState();
  44. },
  45. // called when created, and later when changing options
  46. _refresh: function()
  47. {
  48. oParams = this.options.oRenderParameters;
  49. oParams.operation = 'datatable';
  50. oParams.filter = this.options.sFilter;
  51. oParams.extra_param = this.options.oExtraParams;
  52. oParams.start = 0;
  53. oParams.end = this.options.iPageSize;
  54. oParams.select_mode = this.options.sSelectMode;
  55. oParams.display_key = this.options.sViewLink;
  56. oParams.class_aliases = this.options.oClassAliases;
  57. oParams.columns = this.options.oColumns;
  58. var iSortCol = 0;
  59. var aCurrentSort = [];
  60. for(var k1 in oParams.columns) //Aliases
  61. {
  62. for(var k2 in oParams.columns[k1]) //Attribute codes
  63. {
  64. if (oParams.columns[k1][k2].sort != 'none')
  65. {
  66. oParams.sort_col = iSortCol;
  67. oParams.sort_order = oParams.columns[k1][k2].sort;
  68. aCurrentSort.push([iSortCol, (oParams.columns[k1][k2].sort == 'asc') ? 0 : 1]);
  69. break; //TODO make this more generic, Sort on just one column for now
  70. }
  71. iSortCol++;
  72. }
  73. break; //TODO: DBObjectSet supports only sorting on the first alias of the set
  74. }
  75. var sId = new String(this.element.attr('id'));
  76. var sListId = sId.replace('datatable_', '');
  77. oParams.list_id = sListId;
  78. var me = this;
  79. this.element.block();
  80. $.post(this.options.sRenderUrl, oParams, function(data) {
  81. // Nasty workaround to clear the pager's state for paginated lists !!!
  82. // See jquery.tablesorter.pager.js / saveParams / restoreParams
  83. if (window.pager_params)
  84. {
  85. window.pager_params['pager'+sListId] = undefined;
  86. }
  87. // End of workaround
  88. me.element.find('.datacontents').html(data);
  89. // restore the sort order on columns
  90. me.element.find('table.listResults').trigger('fakesorton', [aCurrentSort]);
  91. me.element.unblock();
  92. }, 'html' );
  93. },
  94. _useDefaultSettings: function(bResetAll)
  95. {
  96. var oParams = this.options.oRenderParameters;
  97. oParams.operation = 'datatable_reset_settings';
  98. oParams.table_id = this.options.sTableId;
  99. oParams.defaults = bResetAll;
  100. oParams.class_aliases = this.options.oClassAliases;
  101. var me = this;
  102. $.post(this.options.sRenderUrl, oParams, function(data) {
  103. // Do nothing...
  104. }, 'html' );
  105. },
  106. _saveSettings: function(bSaveAsDefaults)
  107. {
  108. var oParams = this.options.oRenderParameters;
  109. oParams.operation = 'datatable_save_settings';
  110. oParams.page_size = this.options.iPageSize;
  111. oParams.table_id = this.options.sTableId;
  112. oParams.defaults = bSaveAsDefaults;
  113. oParams.class_aliases = this.options.oClassAliases;
  114. oParams.columns = this.options.oColumns;
  115. var iSortCol = 0;
  116. var sSortOrder = '';
  117. for(var i in this.options.oColumns)
  118. {
  119. if (this.options.oColumns[i].checked)
  120. {
  121. if (this.options.oColumns[i].sort != 'none')
  122. {
  123. sSortOrder = this.options.oColumns[i].sort;
  124. }
  125. else
  126. {
  127. iSortCol++;
  128. }
  129. }
  130. }
  131. if ((this.options.sSelectMode != '') && (this.options.sSelectMode != 'none'))
  132. {
  133. iSortCol++;
  134. }
  135. oParams.sort_col = iSortCol;
  136. oParams.sort_order = sSortOrder;
  137. var me = this;
  138. $.post(this.options.sRenderUrl, oParams, function(data) {
  139. // Do nothing...
  140. }, 'html' );
  141. },
  142. onDlgOk: function()
  143. {
  144. var oOptions = {};
  145. var sId = new String(this.element.attr('id'));
  146. var sListId = sId.replace('datatable_', '');
  147. oSettings = $('#datatable_dlg_'+sListId).find('input[name=settings]:checked');
  148. if (oSettings.val() == 'defaults')
  149. {
  150. oOptions = { iPageSize: this.options.oDefaultSettings.iDefaultPageSize,
  151. oColumns: this.options.oDefaultSettings.oColumns
  152. };
  153. }
  154. else
  155. {
  156. var oDisplayColumns = {};
  157. var iColIdx = 0;
  158. var iSortIdx = 0;
  159. var sSortDirection = 'asc';
  160. var oColumns = $('#datatable_dlg_'+sListId).find(':itop-fieldsorter').fieldsorter('get_params');
  161. var iPageSize = parseInt($('#datatable_dlg_'+sListId+' input[name=page_size]').val(), 10);
  162. oOptions = {oColumns: oColumns, iPageSize: iPageSize, iDefaultPageSize: iPageSize };
  163. }
  164. this._setOptions(oOptions);
  165. // Check if we need to save the settings or not...
  166. var oSaveCheck = $('#datatable_dlg_'+sListId).find('input[name=save_settings]');
  167. var oSaveScope = $('#datatable_dlg_'+sListId).find('input[name=scope]:checked');
  168. if (oSaveCheck.attr('checked'))
  169. {
  170. if (oSettings.val() == 'defaults')
  171. {
  172. this._useDefaultSettings((oSaveScope.val() == 'defaults'));
  173. }
  174. else
  175. {
  176. this._saveSettings((oSaveScope.val() == 'defaults'));
  177. }
  178. }
  179. this._saveDlgState();
  180. },
  181. onDlgCancel: function()
  182. {
  183. this._restoreDlgState();
  184. },
  185. _onSpecificSettings: function()
  186. {
  187. var sId = new String(this.element.attr('id'));
  188. var sListId = sId.replace('datatable_', '');
  189. $('#datatable_dlg_'+sListId).find('input.specific_settings').attr('checked', 'checked');
  190. },
  191. _updateSaveScope: function()
  192. {
  193. var sId = new String(this.element.attr('id'));
  194. var sListId = sId.replace('datatable_', '');
  195. var oSaveCheck = $('#datatable_dlg_'+sListId).find('input[name=save_settings]');
  196. if (oSaveCheck.attr('checked'))
  197. {
  198. $('#datatable_dlg_'+sListId).find('input[name=scope]').each(function() {
  199. if ($(this).attr('stay-disabled') != 'true')
  200. {
  201. $(this).removeAttr('disabled');
  202. }
  203. });
  204. }
  205. else
  206. {
  207. $('#datatable_dlg_'+sListId).find('input[name=scope]').attr('disabled', 'disabled');
  208. }
  209. },
  210. // events bound via _bind are removed automatically
  211. // revert other modifications here
  212. _destroy: function()
  213. {
  214. this.element
  215. .removeClass('itop-datatable');
  216. var sId = new String(this.element.attr('id'));
  217. var sListId = sId.replace('datatable_', '');
  218. $('#sfl_'+sListId).remove();
  219. $('#datatable_dlg_'+sListId).remove();
  220. },
  221. // _setOptions is called with a hash of all options that are changing
  222. _setOptions: function()
  223. {
  224. // in 1.9 would use _superApply
  225. this._superApply(arguments);
  226. this._refresh();
  227. },
  228. // _setOption is called for each individual option that is changing
  229. _setOption: function( key, value )
  230. {
  231. // in 1.9 would use _super
  232. this._superApply(arguments);
  233. },
  234. UpdateState: function( config )
  235. {
  236. var iPageSize = config.page_size;
  237. if (iPageSize == -1)
  238. {
  239. iPageSize = 0;
  240. }
  241. this.options.iPageSize = iPageSize;
  242. var iPos = 0;
  243. for (alias in this.options.oColumns)
  244. {
  245. for (attcode in this.options.oColumns[alias])
  246. {
  247. this.options.oColumns[alias][attcode]['sort'] = 'none';
  248. if (this.options.oColumns[alias][attcode]['checked'])
  249. {
  250. if (iPos == config.sort_index)
  251. {
  252. this.options.oColumns[alias][attcode]['sort'] = config.sort_order;
  253. }
  254. iPos++;
  255. }
  256. }
  257. }
  258. var sId = new String(this.element.attr('id'));
  259. var sListId = sId.replace('datatable_', '');
  260. var dlgElement = $('#datatable_dlg_'+sListId);
  261. dlgElement.find('input[name=page_size]').val(iPageSize);
  262. dlgElement.find(':itop-fieldsorter').fieldsorter('option', { fields: this.options.oColumns });
  263. },
  264. _saveDlgState: function()
  265. {
  266. this.originalState = {};
  267. for(k in this.aDlgStateParams)
  268. {
  269. this.originalState[this.aDlgStateParams[k]] = this.options[this.aDlgStateParams[k]];
  270. }
  271. var sId = new String(this.element.attr('id'));
  272. var sListId = sId.replace('datatable_', '');
  273. this.originalState.oFields = $('#datatable_dlg_'+sListId).find(':itop-fieldsorter').fieldsorter('get_params');
  274. },
  275. _restoreDlgState: function()
  276. {
  277. var sId = new String(this.element.attr('id'));
  278. var sListId = sId.replace('datatable_', '');
  279. var dlgElement = $('#datatable_dlg_'+sListId);
  280. for(k in this.aDlgStateParams)
  281. {
  282. this._setOption(this.aDlgStateParams[k], this.originalState[this.aDlgStateParams[k]]);
  283. }
  284. dlgElement.find('input[name=page_size]').val(this.originalState.iDefaultPageSize);
  285. dlgElement.find(':itop-fieldsorter').fieldsorter('option', { fields: this.originalState.oFields });
  286. },
  287. IsDialogOpen: function()
  288. {
  289. var sId = new String(this.element.attr('id'));
  290. var sListId = sId.replace('datatable_', '');
  291. var oDlgOpen = $('#datatable_dlg_'+sListId+' :visible');
  292. return (oDlgOpen.length > 0);
  293. },
  294. DoRefresh: function()
  295. {
  296. this._refresh();
  297. },
  298. GetMultipleSelectionParams: function()
  299. {
  300. var oRes = {};
  301. oRes.selectionMode = '';
  302. if (this.element.find(':input[name=selectionMode]').length > 0)
  303. {
  304. oRes.selectionMode = this.element.find(':input[name=selectionMode]').val();
  305. }
  306. oRes.selectObject = [];
  307. this.element.find(':input[name^=selectObject]:checked').each(function() {
  308. oRes.selectObject.push($(this).val());
  309. });
  310. oRes.storedSelection = [];
  311. this.element.find(':input[name^=storedSelection]').each(function() {
  312. oRes.storedSelection.push($(this).val());
  313. });
  314. return oRes;
  315. }
  316. });
  317. });