No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

189 líneas
4.7 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.appframe = new wn.views.AppFrame(this.wrapper);
  102. this.appframe.$titlebar.find('.close').unbind('click').click(function() {
  103. if(me.oncancel)me.oncancel(); me.hide();
  104. });
  105. this.set_title(this.opts.title);
  106. }
  107. this.set_title = function(t) {
  108. this.appframe.$titlebar.find('.appframe-title').html(t || '');
  109. }
  110. this.set_postion = function() {
  111. // place it at the center
  112. var d = get_screen_dims();
  113. this.wrapper.style.left = ((d.w - cint(this.wrapper.style.width))/2) + 'px';
  114. this.wrapper.style.top = (get_scroll_top() + 60) + 'px';
  115. // place it on top
  116. top_index++;
  117. $y(this.wrapper,{zIndex:top_index});
  118. }
  119. /** show the dialog */
  120. this.show = function() {
  121. // already live, do nothing
  122. if(this.display) return;
  123. // set position
  124. this.set_postion()
  125. // show it
  126. $ds(this.wrapper);
  127. // hide background
  128. freeze();
  129. this.display = true;
  130. cur_dialog = this;
  131. // call onshow
  132. if(this.onshow)this.onshow();
  133. }
  134. this.hide = function() {
  135. // call onhide
  136. if(this.onhide) this.onhide();
  137. // hide
  138. unfreeze();
  139. $dh(this.wrapper);
  140. // flags
  141. this.display = false;
  142. cur_dialog = null;
  143. }
  144. this.no_cancel = function() {
  145. this.appframe.$titlebar.find('.close').toggle(false);
  146. }
  147. if(opts) this.make();
  148. }
  149. wn.widgets.Dialog.prototype = new wn.widgets.FieldGroup();
  150. // close open dialogs on ESC
  151. $(document).bind('keydown', function(e) {
  152. if(cur_dialog && !cur_dialog.no_cancel_flag && e.which==27) {
  153. cur_dialog.hide();
  154. }
  155. });