show_level.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. $.cs_showLevel = function (setting) {
  10. const sortTitle = ['第一层', '第二层', '第三层','第四层', '第五层'];
  11. if (!setting.selector) throw '未指定模块';
  12. if (!setting.levels) {
  13. setting.levels = [
  14. { type: 'sort', count: 5, },
  15. { type: 'last', title: '最底层' },
  16. // { type: 'leafXmj', title: '只显示项目节' },
  17. // { type: 'curMeasure', title: '只显示本期计量' },
  18. ];
  19. }
  20. const initShowLevel = function () {
  21. const obj = $(setting.selector);
  22. const html = [];
  23. html.push('<button class="btn btn-sm btn-light dropdown-toggle text-primary" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' +
  24. '<i class="fa fa-list-ol"></i> 显示层级</button>');
  25. html.push('<div class="dropdown-menu" aria-labelledby="dropdownMenuButton" id="showLevelList">');
  26. for (const l of setting.levels) {
  27. if (l.type === 'sort') {
  28. let count = typeof l.count === 'function' ? l.count() : l.count;
  29. count = Math.min(count, 5);
  30. for (let i = 1; i <= l.count; ++i) {
  31. html.push(`<a class="dropdown-item" name="showLevel" tag="${i}" href="javascript: void(0);">${sortTitle[i-1]}</a>`);
  32. }
  33. } else {
  34. html.push(`<a class="dropdown-item" name="showLevel" tag="${l.type}" href="javascript: void(0);">${l.title}</a>`);
  35. }
  36. }
  37. html.push('</div>');
  38. obj.html(html.join(''));
  39. $('a[name=showLevel]').click(function () {
  40. setTimeout(() => {
  41. const tag = $(this).attr('tag');
  42. showWaitingView();
  43. setting.showLevel(tag);
  44. closeWaitingView();
  45. });
  46. });
  47. refreshMenuVisible();
  48. };
  49. const refreshMenuVisible = function () {
  50. const showMenu = function (tag, visible) {
  51. if (visible) $(`[tag=${tag}]`).show();
  52. if (!visible) $(`[tag=${tag}]`).hide();
  53. };
  54. for (const l of setting.levels) {
  55. if (l.type === 'sort') {
  56. let visibleCount = typeof l.visible_count === 'function' ? l.visible_count() : l.visible_count;
  57. for (let i = 1; i <= l.count; ++i) {
  58. showMenu(i, i <= visibleCount);
  59. }
  60. } else {
  61. const visible = typeof l.visible === 'function' ? l.visible() : l.visible;
  62. showMenu(l.type, visible);
  63. }
  64. }
  65. };
  66. return { initShowLevel, refreshMenuVisible } ;
  67. };