您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

181 行
3.9 KiB

  1. // opts { width, height, title, fields (like docfields) }
  2. wn.widgets.FieldGroup = function() {
  3. this.first_button = false;
  4. this.make_fields = function(body, fl) {
  5. $y(this.body, {padding:'11px'});
  6. this.fields_dict = {}; // reset
  7. for(var i=0; i<fl.length; i++) {
  8. var df = fl[i];
  9. var div = $a(body, 'div', '', {margin:'6px 0px'})
  10. f = make_field(df, null, div, null);
  11. f.not_in_form = 1;
  12. this.fields_dict[df.fieldname] = f
  13. f.refresh();
  14. // first button primary ?
  15. if(df.fieldtype=='Button' && !this.first_button) {
  16. $(f.input).addClass('primary');
  17. this.first_button = true;
  18. }
  19. }
  20. }
  21. /* get values */
  22. this.get_values = function() {
  23. var ret = {};
  24. var errors = [];
  25. for(var key in this.fields_dict) {
  26. var f = this.fields_dict[key];
  27. var v = f.get_value ? f.get_value() : null;
  28. if(f.df.reqd && !v)
  29. errors.push(f.df.label + ' is mandatory');
  30. if(v) ret[f.df.fieldname] = v;
  31. }
  32. if(errors.length) {
  33. msgprint('<b>Please check the following Errors</b>\n' + errors.join('\n'));
  34. return null;
  35. }
  36. return ret;
  37. }
  38. /* set field value */
  39. this.set_value = function(key, val){
  40. var f = this.fields_dict[key];
  41. if(f) {
  42. f.set_input(val);
  43. f.refresh_mandatory();
  44. }
  45. }
  46. /* set values from a dict */
  47. this.set_values = function(dict) {
  48. for(var key in dict) {
  49. if(this.fields_dict[key]) {
  50. this.set_value(key, dict[key]);
  51. }
  52. }
  53. }
  54. this.clear = function() {
  55. for(key in this.fields_dict) {
  56. var f = this.fields_dict[key];
  57. if(f) {
  58. f.set_input(f.df['default'] || '');
  59. }
  60. }
  61. }
  62. }
  63. wn.widgets.Dialog = function(opts) {
  64. this.opts = opts;
  65. this.display = false;
  66. this.make = function(opts) {
  67. if(opts)
  68. this.opts = opts;
  69. if(!this.opts.width) this.opts.width = 480;
  70. this.wrapper = $a(popup_cont, 'div', 'dialog_wrapper');
  71. if(this.opts.width)
  72. this.wrapper.style.width = this.opts.width + 'px';
  73. this.make_head();
  74. this.body = $a(this.wrapper, 'div', 'dialog_body');
  75. if(this.opts.fields)
  76. this.make_fields(this.body, this.opts.fields);
  77. }
  78. this.make_head = function() {
  79. var me = this;
  80. this.head = $a(this.wrapper, 'div', 'dialog_head');
  81. var t = make_table(this.head,1,2,'100%',['100%','16px'],{padding:'2px'});
  82. $y($td(t,0,0),{paddingLeft:'16px',fontWeight:'bold',fontSize:'14px',textAlign:'center'});
  83. $y($td(t,0,1),{textAlign:'right'});
  84. var img = $a($td(t,0,01),'img','',{cursor:'pointer'});
  85. img.src='lib/images/icons/close.gif';
  86. this.title_text = $td(t,0,0);
  87. this.set_title(this.opts.title);
  88. img.onclick = function() { if(me.oncancel)me.oncancel(); me.hide(); }
  89. this.cancel_img = img;
  90. }
  91. this.set_title = function(t) {
  92. this.title_text.innerHTML = t ? t : '';
  93. }
  94. this.set_postion = function() {
  95. // place it at the center
  96. var d = get_screen_dims();
  97. this.wrapper.style.left = ((d.w - cint(this.wrapper.style.width))/2) + 'px';
  98. this.wrapper.style.top = (get_scroll_top() + 60) + 'px';
  99. // place it on top
  100. top_index++;
  101. $y(this.wrapper,{zIndex:top_index});
  102. }
  103. /** show the dialog */
  104. this.show = function() {
  105. // already live, do nothing
  106. if(this.display) return;
  107. // set position
  108. this.set_postion()
  109. // show it
  110. $ds(this.wrapper);
  111. // hide background
  112. freeze();
  113. this.display = true;
  114. cur_dialog = this;
  115. // call onshow
  116. if(this.onshow)this.onshow();
  117. }
  118. this.hide = function() {
  119. // call onhide
  120. if(this.onhide) this.onhide();
  121. // hide
  122. unfreeze();
  123. $dh(this.wrapper);
  124. // clear open autosuggests
  125. if(cur_autosug) cur_autosug.clearSuggestions();
  126. // flags
  127. this.display = false;
  128. cur_dialog = null;
  129. }
  130. this.no_cancel = function() {
  131. $dh(this.cancel_img);
  132. }
  133. if(opts) this.make();
  134. }
  135. wn.widgets.Dialog.prototype = new wn.widgets.FieldGroup();
  136. // Close dialog on Escape
  137. keypress_observers.push(new function() {
  138. this.notify_keypress = function(e, kc) {
  139. if(cur_dialog && kc==27 && !cur_dialog.no_cancel_flag)
  140. cur_dialog.hide();
  141. }
  142. });