You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

202 lines
5.0 KiB

  1. // Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. //
  3. // MIT License (MIT)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a
  6. // copy of this software and associated documentation files (the "Software"),
  7. // to deal in the Software without restriction, including without limitation
  8. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. // and/or sell copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22. // opts { width, height, title, fields (like docfields) }
  23. wn.widgets.FieldGroup = function() {
  24. this.first_button = false;
  25. this.make_fields = function(body, fl) {
  26. $y(this.body, {padding:'11px'});
  27. this.fields_dict = {}; // reset
  28. for(var i=0; i<fl.length; i++) {
  29. var df = fl[i];
  30. var div = $a(body, 'div', '', {margin:'6px 0px'})
  31. f = make_field(df, null, div, null);
  32. f.not_in_form = 1;
  33. this.fields_dict[df.fieldname] = f
  34. f.refresh();
  35. // first button primary ?
  36. if(df.fieldtype=='Button' && !this.first_button) {
  37. $(f.input).addClass('btn-info');
  38. this.first_button = true;
  39. }
  40. }
  41. }
  42. /* get values */
  43. this.get_values = function() {
  44. var ret = {};
  45. var errors = [];
  46. for(var key in this.fields_dict) {
  47. var f = this.fields_dict[key];
  48. var v = f.get_value ? f.get_value() : null;
  49. if(f.df.reqd && !v)
  50. errors.push(f.df.label + ' is mandatory');
  51. if(v) ret[f.df.fieldname] = v;
  52. }
  53. if(errors.length) {
  54. msgprint('<b>Please check the following Errors</b>\n' + errors.join('\n'));
  55. return null;
  56. }
  57. return ret;
  58. }
  59. /* set field value */
  60. this.set_value = function(key, val){
  61. var f = this.fields_dict[key];
  62. if(f) {
  63. f.set_input(val);
  64. f.refresh_mandatory();
  65. }
  66. }
  67. /* set values from a dict */
  68. this.set_values = function(dict) {
  69. for(var key in dict) {
  70. if(this.fields_dict[key]) {
  71. this.set_value(key, dict[key]);
  72. }
  73. }
  74. }
  75. this.clear = function() {
  76. for(key in this.fields_dict) {
  77. var f = this.fields_dict[key];
  78. if(f) {
  79. f.set_input(f.df['default'] || '');
  80. }
  81. }
  82. }
  83. }
  84. wn.widgets.Dialog = function(opts) {
  85. this.opts = opts;
  86. this.display = false;
  87. this.make = function(opts) {
  88. if(opts)
  89. this.opts = opts;
  90. if(!this.opts.width) this.opts.width = 480;
  91. this.wrapper = $a(popup_cont, 'div', 'dialog_wrapper');
  92. if(this.opts.width)
  93. this.wrapper.style.width = this.opts.width + 'px';
  94. this.make_head();
  95. this.body = $a(this.wrapper, 'div', 'dialog_body');
  96. if(this.opts.fields)
  97. this.make_fields(this.body, this.opts.fields);
  98. }
  99. this.make_head = function() {
  100. var me = this;
  101. this.head = $a(this.wrapper, 'div', 'dialog_head');
  102. var t = make_table(this.head,1,2,'100%',['100%','16px'],{padding:'2px'});
  103. $y($td(t,0,0),{paddingLeft:'16px',fontWeight:'bold',fontSize:'14px',textAlign:'center'});
  104. $y($td(t,0,1),{textAlign:'right'});
  105. var img = $a($td(t,0,01),'img','',{cursor:'pointer'});
  106. img.src='lib/images/icons/close.gif';
  107. this.title_text = $td(t,0,0);
  108. this.set_title(this.opts.title);
  109. img.onclick = function() { if(me.oncancel)me.oncancel(); me.hide(); }
  110. this.cancel_img = img;
  111. }
  112. this.set_title = function(t) {
  113. this.title_text.innerHTML = t ? t : '';
  114. }
  115. this.set_postion = function() {
  116. // place it at the center
  117. var d = get_screen_dims();
  118. this.wrapper.style.left = ((d.w - cint(this.wrapper.style.width))/2) + 'px';
  119. this.wrapper.style.top = (get_scroll_top() + 60) + 'px';
  120. // place it on top
  121. top_index++;
  122. $y(this.wrapper,{zIndex:top_index});
  123. }
  124. /** show the dialog */
  125. this.show = function() {
  126. // already live, do nothing
  127. if(this.display) return;
  128. // set position
  129. this.set_postion()
  130. // show it
  131. $ds(this.wrapper);
  132. // hide background
  133. freeze();
  134. this.display = true;
  135. cur_dialog = this;
  136. // call onshow
  137. if(this.onshow)this.onshow();
  138. }
  139. this.hide = function() {
  140. // call onhide
  141. if(this.onhide) this.onhide();
  142. // hide
  143. unfreeze();
  144. $dh(this.wrapper);
  145. // clear open autosuggests
  146. if(cur_autosug) cur_autosug.clearSuggestions();
  147. // flags
  148. this.display = false;
  149. cur_dialog = null;
  150. }
  151. this.no_cancel = function() {
  152. $dh(this.cancel_img);
  153. }
  154. if(opts) this.make();
  155. }
  156. wn.widgets.Dialog.prototype = new wn.widgets.FieldGroup();
  157. // close open dialogs on ESC
  158. $(document).bind('keydown', function(e) {
  159. if(cur_dialog && !cur_dialog.no_cancel_flag && e.which==27) {
  160. cur_dialog.hide();
  161. }
  162. });