Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

112 řádky
3.3 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. wn.provide('wn.ui');
  23. wn.ui.FieldGroup = Class.extend({
  24. init: function(opts) {
  25. $.extend(this, opts);
  26. this.make_fields();
  27. if(!this.no_submit_on_enter)
  28. this.catch_enter_as_submit();
  29. },
  30. first_button: false,
  31. make_fields: function() {
  32. $(this.parent).css({padding:'25px'});
  33. this.fields_dict = {}; // reset
  34. for(var i=0; i< this.fields.length; i++) {
  35. var df = this.fields[i];
  36. if(!df.fieldname && df.label) {
  37. df.fieldname = df.label.replace(/ /g, '_').toLowerCase();
  38. }
  39. if(!df.fieldtype) df.fieldtype="Data";
  40. var div = $a(this.parent, 'div', '', {margin:'6px 0px'})
  41. f = make_field(df, null, div, null);
  42. f.not_in_form = 1;
  43. f.dialog_wrapper = this.dialog_wrapper || null;
  44. this.fields_dict[df.fieldname] = f
  45. f.refresh();
  46. // first button primary ?
  47. if(df.fieldtype=='Button' && !this.first_button) {
  48. $(f.input).addClass('btn-info');
  49. this.first_button = true;
  50. }
  51. }
  52. },
  53. catch_enter_as_submit: function() {
  54. var me = this;
  55. $(this.parent).find(':input[type="text"], :input[type="password"]').keypress(function(e) {
  56. if(e.which==13) {
  57. $(me.parent).find('.btn-info:first').click();
  58. }
  59. })
  60. },
  61. get_input: function(fieldname) {
  62. var field = this.fields_dict[fieldname];
  63. return $(field.txt ? field.txt : field.input);
  64. },
  65. get_values: function() {
  66. var ret = {};
  67. var errors = [];
  68. for(var key in this.fields_dict) {
  69. var f = this.fields_dict[key];
  70. var v = f.get_value ? f.get_value() : null;
  71. if(f.df.reqd && !v)
  72. errors.push(f.df.label + ' is mandatory');
  73. if(v) ret[f.df.fieldname] = v;
  74. }
  75. if(errors.length) {
  76. msgprint('<b>Please check the following Errors</b>\n' + errors.join('\n'));
  77. return null;
  78. }
  79. return ret;
  80. },
  81. get_value: function(key) {
  82. var f = this.fields_dict[key];
  83. return f && (f.get_value ? f.get_value() : null);
  84. },
  85. set_value: function(key, val){
  86. var f = this.fields_dict[key];
  87. if(f) {
  88. f.set_input(val);
  89. }
  90. },
  91. set_values: function(dict) {
  92. for(var key in dict) {
  93. if(this.fields_dict[key]) {
  94. this.set_value(key, dict[key]);
  95. }
  96. }
  97. },
  98. clear: function() {
  99. for(key in this.fields_dict) {
  100. var f = this.fields_dict[key];
  101. if(f) {
  102. f.set_input(f.df['default'] || '');
  103. }
  104. }
  105. },
  106. });