TableAjax.php.tableTools.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // @require variables:
  2. if ('undefined' === typeof JS_FUNCTION_NAME) throw "Missing JS_FUNCTION_NAME";
  3. if ('undefined' === typeof URL_GET_TABLE_TOOLS) throw "Missing URL_GET_TABLE_TOOLS";
  4. function toggleTableTools(n) {
  5. var $n = jQuery(n);
  6. var $ul = $n.next();
  7. if ($n.data('fetched')) return false;
  8. function renderTableToolsData(tableTools) {
  9. var procesInitMapHtml = '';
  10. if (!tableTools) {
  11. return '<li><a href="#">' + "Brak danych" + '</a></li>';
  12. } else if (!tableTools.length) {
  13. return '<li><a href="#">' + "Brak narzędzi dla tej tabeli" + '</a></li>';
  14. } else {
  15. return tableTools.map(function (tool) {
  16. return '<li><a href="' + tool.url + '">' + tool.label + '</a></li>';
  17. })
  18. }
  19. return procesInitMapHtml;
  20. }
  21. window.fetch(URL_GET_TABLE_TOOLS, {
  22. method: 'GET',
  23. credentials: 'same-origin',
  24. }).then(function (response) {
  25. return response.json()
  26. }).then(function (data) {
  27. var type = data.type || null
  28. var msg = data.msg || "Error"
  29. var tableTools = (data.body && data.body.tableTools) ? data.body.tableTools : null
  30. if (type) {
  31. if ('success' == type) {
  32. $ul.html(renderTableToolsData(tableTools));
  33. } else if ('error' == type) {
  34. $ul.html('<li><a href="#">' + msg + '</a></li>');
  35. } else {
  36. $ul.html('<li><a href="#">Wystapił błąd podczas pobierania listy narzędzi</a></li>');
  37. }
  38. } else {
  39. $ul.html('<li><a href="#">Wystapił błąd podczas pobierania listy narzędzi</a></li>');
  40. }
  41. $n.data('fetched', true);
  42. }).catch(function (e) {
  43. console.warn(e)
  44. })
  45. return false;
  46. }
  47. global[JS_FUNCTION_NAME] = toggleTableTools