dialog.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. function _createButton(arg) {
  2. arg = arg || {};
  3. var name = arg.name || '',
  4. span = K('<span class="ke-button-common ke-button-outer" title="' + name + '"></span>'),
  5. btn = K('<input class="ke-button-common ke-button" type="button" value="' + name + '" />');
  6. if (arg.click) {
  7. btn.click(arg.click);
  8. }
  9. span.append(btn);
  10. return span;
  11. }
  12. // create KToolbar class
  13. function KDialog(options) {
  14. this.init(options);
  15. }
  16. _extend(KDialog, KWidget, {
  17. init : function(options) {
  18. var self = this;
  19. var shadowMode = _undef(options.shadowMode, true);
  20. options.z = options.z || 811213;
  21. options.shadowMode = false;
  22. options.autoScroll = _undef(options.autoScroll, true);
  23. KDialog.parent.init.call(self, options);
  24. var title = options.title,
  25. body = K(options.body, self.doc),
  26. previewBtn = options.previewBtn,
  27. yesBtn = options.yesBtn,
  28. noBtn = options.noBtn,
  29. closeBtn = options.closeBtn,
  30. showMask = _undef(options.showMask, true);
  31. self.div.addClass('ke-dialog').bind('click,mousedown', function(e){
  32. e.stopPropagation();
  33. });
  34. var contentDiv = K('<div class="ke-dialog-content"></div>').appendTo(self.div);
  35. if (_IE && _V < 7) {
  36. self.iframeMask = K('<iframe src="about:blank" class="ke-dialog-shadow"></iframe>').appendTo(self.div);
  37. } else if (shadowMode) {
  38. K('<div class="ke-dialog-shadow"></div>').appendTo(self.div);
  39. }
  40. var headerDiv = K('<div class="ke-dialog-header"></div>');
  41. contentDiv.append(headerDiv);
  42. headerDiv.html(title);
  43. self.closeIcon = K('<span class="ke-dialog-icon-close" title="' + closeBtn.name + '"></span>').click(closeBtn.click);
  44. headerDiv.append(self.closeIcon);
  45. self.draggable({
  46. clickEl : headerDiv,
  47. beforeDrag : options.beforeDrag
  48. });
  49. var bodyDiv = K('<div class="ke-dialog-body"></div>');
  50. contentDiv.append(bodyDiv);
  51. bodyDiv.append(body);
  52. var footerDiv = K('<div class="ke-dialog-footer"></div>');
  53. if (previewBtn || yesBtn || noBtn) {
  54. contentDiv.append(footerDiv);
  55. }
  56. _each([
  57. { btn : previewBtn, name : 'preview' },
  58. { btn : yesBtn, name : 'yes' },
  59. { btn : noBtn, name : 'no' }
  60. ], function() {
  61. if (this.btn) {
  62. var button = _createButton(this.btn);
  63. button.addClass('ke-dialog-' + this.name);
  64. footerDiv.append(button);
  65. }
  66. });
  67. if (self.height) {
  68. bodyDiv.height(_removeUnit(self.height) - headerDiv.height() - footerDiv.height());
  69. }
  70. self.div.width(self.div.width());
  71. self.div.height(self.div.height());
  72. self.mask = null;
  73. if (showMask) {
  74. var docEl = _docElement(self.doc),
  75. docWidth = Math.max(docEl.scrollWidth, docEl.clientWidth),
  76. docHeight = Math.max(docEl.scrollHeight, docEl.clientHeight);
  77. self.mask = _widget({
  78. x : 0,
  79. y : 0,
  80. z : self.z - 1,
  81. cls : 'ke-dialog-mask',
  82. width : docWidth,
  83. height : docHeight
  84. });
  85. }
  86. self.autoPos(self.div.width(), self.div.height());
  87. self.footerDiv = footerDiv;
  88. self.bodyDiv = bodyDiv;
  89. self.headerDiv = headerDiv;
  90. self.isLoading = false;
  91. },
  92. setMaskIndex : function(z) {
  93. var self = this;
  94. self.mask.div.css('z-index', z);
  95. },
  96. showLoading : function(msg) {
  97. msg = _undef(msg, '');
  98. var self = this, body = self.bodyDiv;
  99. self.loading = K('<div class="ke-dialog-loading"><div class="ke-inline-block ke-dialog-loading-content" style="margin-top:' + Math.round(body.height() / 3) + 'px;">' + msg + '</div></div>')
  100. .width(body.width()).height(body.height())
  101. .css('top', self.headerDiv.height() + 'px');
  102. body.css('visibility', 'hidden').after(self.loading);
  103. self.isLoading = true;
  104. return self;
  105. },
  106. hideLoading : function() {
  107. this.loading && this.loading.remove();
  108. this.bodyDiv.css('visibility', 'visible');
  109. this.isLoading = false;
  110. return this;
  111. },
  112. remove : function() {
  113. var self = this;
  114. if (self.options.beforeRemove) {
  115. self.options.beforeRemove.call(self);
  116. }
  117. self.mask && self.mask.remove();
  118. self.iframeMask && self.iframeMask.remove();
  119. self.closeIcon.unbind();
  120. K('input', self.div).unbind();
  121. K('button', self.div).unbind();
  122. self.footerDiv.unbind();
  123. self.bodyDiv.unbind();
  124. self.headerDiv.unbind();
  125. K('iframe', self.div).each(function() {
  126. //this.src = 'javascript:false';
  127. K(this).remove();
  128. });
  129. KDialog.parent.remove.call(self);
  130. return self;
  131. }
  132. });
  133. function _dialog(options) {
  134. return new KDialog(options);
  135. }
  136. K.DialogClass = KDialog;
  137. K.dialog = _dialog;