yvanui.dialog2_layer.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * dialog2
  3. * @author luoyifan
  4. * 2018-11-22 17:32:00
  5. */
  6. "use strict";
  7. $.parser.auto = false;
  8. (function ($) {
  9. $.parser.plugins.push("dialog2");
  10. $(function () {
  11. //构造方法
  12. });
  13. var resize = function (layero) {
  14. var me = layero.find('.easyui-layout');
  15. me.layout('resize', {
  16. height: layero[0].clientHeight - 43
  17. });
  18. };
  19. $.fn.dialog2 = function (options, param) {
  20. if (typeof options === 'string') {
  21. var method = $.fn.dialog2.methods[options];
  22. if (!method) {
  23. method = $.fn.power.methods[options];
  24. }
  25. return method(this, param);
  26. }
  27. return this.each(function () {
  28. var state = $.data(this, 'dialog2');
  29. if (state) {
  30. $.extend(state.options, options);
  31. } else {
  32. $.data(this, 'dialog2', {
  33. options: options, //对话框内容(可能是函数)
  34. param: param, //调用参数
  35. runtime: { //运行时参数
  36. domId: 'd_' + (new Date()).getTime()
  37. },
  38. context: null
  39. });
  40. }
  41. });
  42. };
  43. $.fn.dialog2.methods = {
  44. show: function (jq, context) {
  45. var target = jq[0];
  46. var opts = $.data(target, 'dialog2').options;
  47. //var param = $.extend({}, $.data(target, 'dialog2').param, context || {});
  48. var param = $.extend({}, context);
  49. var runtime = $.data(target, 'dialog2').runtime;
  50. if (runtime.layerindex) {
  51. console.error('对话框已经打开过了!');
  52. return;
  53. }
  54. if ($.type(opts) === 'function') {
  55. //dialog 内容是函数,则调用函数,并获取结果
  56. opts = opts(param);
  57. }
  58. //记录旧的 focus 控件
  59. var oldFocus = document.activeElement;
  60. //填充缺省参数
  61. opts = $.extend({}, $.fn.dialog2.defaults, opts);
  62. //执行 dom 的构建过程,并记录下 context 的数据
  63. var $target = $(target);
  64. var $dom = $('<div id="' + runtime.domId + '" tabindex="99"></div>');
  65. $.yvan.fillCommonProperties($dom, opts);
  66. $dom.attr('xtype', 'dialog');
  67. $target.append($dom);
  68. //dialog 是快捷键容器
  69. shortcut.createContainer($dom, param);
  70. $dom.power($.yvan.props(opts, ['north', 'center', 'west', 'east', 'south', 'xtype', 'attr', 'itemId', 'id', 'html', 'class', 'css', 'title']), param);
  71. var $shade;
  72. //打开对话框
  73. var layerOpts = {
  74. area: [
  75. opts.width + ($.type(opts.width) === 'number' ? 'px' : ''),
  76. opts.height + ($.type(opts.height) === 'number' ? 'px' : '')
  77. ],
  78. title: opts.title,
  79. type: 1,
  80. maxmin: 1,
  81. shade: false,
  82. shadeClose: false,
  83. resize: true,
  84. anim: 0,
  85. skin: 'layer-class',
  86. zIndex: layer.zIndex,
  87. content: $dom,
  88. success: function (layero, index) {
  89. $.data(layero.down('dialog')[0], 'context', context);
  90. shortcut.disableLayroTabIndex(layero);
  91. shortcut.registe($dom, 'esc', function () {
  92. layer.close(index);
  93. });
  94. layer.setTop(layero);
  95. $.parser.parse($dom);
  96. runtime.layerindex = index;
  97. var zIndex = parseInt(layero.css('z-index')) - 1;
  98. $shade = $('<div class="layui-layer-shade" id="layui-layer-shade2" times="2" style="z-index: ' + zIndex + '; background-color: rgb(0, 0, 0); opacity: 0.3;"></div>');
  99. $target.append($shade);
  100. resize(layero);
  101. if (opts.onLoad) {
  102. opts.onLoad(layero, context);
  103. }
  104. //将当前焦点放在 $dom 上
  105. setTimeout(function () {
  106. $dom[0].focus();
  107. }, 10);
  108. },
  109. end: function () {
  110. if (opts.onUnload) {
  111. opts.onUnload(context);
  112. }
  113. $shade.remove();
  114. $dom.remove();
  115. delete runtime.layerindex;
  116. //还原旧焦点
  117. if (oldFocus) {
  118. setTimeout(function () {
  119. oldFocus.focus();
  120. }, 10);
  121. }
  122. },
  123. resizing: resize,
  124. full: resize,
  125. restore: resize
  126. };
  127. //构造 layer 的按钮
  128. if (opts.btn && $.type(opts.btn) === 'array') {
  129. layerOpts.btn = [];
  130. for (var i = 0; i < opts.btn.length; i++) {
  131. layerOpts.btn.push(
  132. (opts.btn[i].iconCls ? '<i class="' + opts.btn[i].iconCls + '"></i>&nbsp;' : '') +
  133. (opts.btn[i].text ? opts.btn[i].text : '')
  134. );
  135. if (i === 0) {
  136. layerOpts.yes = opts.btn[i].onClick;
  137. } else {
  138. layerOpts['btn' + (i + 1)] = opts.btn[i].onClick;
  139. }
  140. }
  141. }
  142. layer.open(layerOpts);
  143. return $dom;
  144. },
  145. power: function (jq) {
  146. var target = jq[0];
  147. var opts = $.data(target, 'dialog2').options;
  148. return $('#' + opts.domId).power;
  149. },
  150. close: function (jq) {
  151. var target = jq[0];
  152. var runtime = $.data(target, 'dialog2').runtime;
  153. if (runtime.layerindex) {
  154. layer.close(runtime.layerindex);
  155. }
  156. }
  157. };
  158. $.fn.dialog2.defaults = {
  159. title: '未命名对话框',
  160. width: 300,
  161. height: 200
  162. };
  163. })(jQuery);