yvanui.toolbar.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * toolbar - jQuery EasyUI
  3. */
  4. (function ($) {
  5. $.parser.plugins.push("toolbar");
  6. $(function () {
  7. //构造方法
  8. });
  9. function createMenu(item) {
  10. var $dom = $('<ul class="dropdown-menu user-menu hide"></ul>');
  11. $.yvan.fillCommonProperties($dom, item);
  12. var items = $.yvan.getItems(item);
  13. for (var i = 0; i < items.length; i++) {
  14. var $item = $('<li></li>');
  15. var $a = $('<a></a>');
  16. var $icon = $('<i></i>');
  17. $icon.addClass(items[i].iconCls);
  18. $a.append($icon);
  19. $a.append('&nbsp;' + items[i].text);
  20. $a.click(items[i].onClick);
  21. $item.append($a);
  22. $dom.append($item);
  23. }
  24. return $dom;
  25. }
  26. function render(target, context) {
  27. var opts = $.data(target, 'toolbar').options;
  28. var $target = $(target);
  29. $target.addClass('yvan-form-toolbar');
  30. $target.html('');
  31. if ($.trim(opts.title)) {
  32. $target.append($('<span class="yvan-form-toolbar-title">' + opts.title + '</span><span class="yvan-form-toolbar-separate yvan-separate"></span>'));
  33. }
  34. var items = $.yvan.getItems(opts);
  35. // 工具栏的按钮,与下级的表格进行关联
  36. // 如果按钮有 hideOnUnselected 属性,代表如果表格没有选中任何行,是不需要显示的
  37. var relateToGridArray = [];
  38. for (var i = 0; i < items.length; i++) {
  39. var value = opts.items[i];
  40. if ($.type(value) === 'object') {
  41. if (items[i].hasOwnProperty('xtype')) {
  42. var css = 'easyui-' + items[i].xtype;
  43. var $dom = $('<input class="' + css + ' ctl_' + items[i].name + '" name="' + items[i].name + '" />');
  44. $.yvan.fillCommonProperties($dom, items[i]);
  45. $target.append($dom);
  46. //初始化元素
  47. $dom[items[i].xtype](items[i]);
  48. if ($.type(items[i].onRender) === 'function') {
  49. items[i].onRender.call($dom[0], $target, items[i], context);
  50. }
  51. } else {
  52. if ($.type(value.onBeforeRender) === 'function') {
  53. if (value.onBeforeRender.call(this, $target, value, context) === false) {
  54. return;
  55. }
  56. }
  57. var clazz = "easyui-linkbutton";
  58. if (value.hasOwnProperty('menu')) {
  59. value.iconCls = "fa fa-angle-down";
  60. clazz = "easyui-menubutton";
  61. }
  62. var $dom = $('<a class="' + clazz + ' button-wathet" tabIndex="-1"></a>');
  63. $.yvan.fillCommonProperties($dom, value);
  64. $target.append($dom);
  65. if (window.hasOwnProperty('shortcut')) {
  66. shortcut.processButton($dom, value, context);
  67. }
  68. if (value.hasOwnProperty('menu')) {
  69. var $menu = createMenu(value.menu);
  70. var menuId = $.yvan.createId('menu');
  71. $menu.attr('id', menuId);
  72. $target.append($menu);
  73. $menu.menu({});
  74. value.menu = '#' + menuId;
  75. $dom.menubutton(value);
  76. } else {
  77. $dom.linkbutton(value);
  78. }
  79. if (value.hideOnUnselected) {
  80. //toolbar 与下面的表格进行关联
  81. relateToGridArray.push($dom[0]);
  82. }
  83. if ($.type(value.onRender) === 'function') {
  84. value.onRender.call($dom[0], $target, value, context);
  85. }
  86. }
  87. } else if (value === '-') {
  88. $target.append('<span class="yvan-form-toolbar-title"></span>');
  89. } else if (value === '|') {
  90. $target.append('<span class="yvan-form-toolbar-separate"></span>');
  91. }
  92. } // for
  93. //目前 hideOnUnselected 属性,只能与 grid 表格做关联
  94. if (relateToGridArray.length > 0) {
  95. setTimeout(function () {
  96. var $grid = $target.closest('.ui-jqgrid').down('grid');
  97. if ($grid && $grid.length > 0) {
  98. //找到表格, 并绑定表格的事件
  99. setInterval(function () {
  100. if ($grid.find('.ui-state-highlight').length > 0) {
  101. relateToGridArray.forEach(function (item) {
  102. $(item).show();
  103. });
  104. } else {
  105. relateToGridArray.forEach(function (item) {
  106. $(item).hide();
  107. });
  108. }
  109. }, 500);
  110. } else {
  111. console.error('can\'t find grid', $target);
  112. }
  113. });
  114. }
  115. }
  116. $.fn.toolbar = function (options, param) {
  117. if (typeof options === 'string') {
  118. var method = $.fn.toolbar.methods[options];
  119. return method(this, param);
  120. }
  121. options = options || {};
  122. return this.each(function () {
  123. var state = $.data(this, 'toolbar');
  124. if (state) {
  125. $.extend(state.options, options);
  126. } else {
  127. $.data(this, 'toolbar', {
  128. options: $.extend({}, $.fn.toolbar.defaults, options)
  129. });
  130. }
  131. render(this, param);
  132. });
  133. };
  134. $.fn.toolbar.methods = {};
  135. $.fn.toolbar.defaults = {};
  136. })(jQuery);