Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

201 строка
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. if(!window.make_field) {
  27. // called in website, load some libs
  28. wn.require('css/fields.css');
  29. wn.require('js/fields.js');
  30. }
  31. $y(this.body, {padding:'11px'});
  32. this.fields_dict = {}; // reset
  33. for(var i=0; i<fl.length; i++) {
  34. var df = fl[i];
  35. var div = $a(body, 'div', '', {margin:'6px 0px'})
  36. f = make_field(df, null, div, null);
  37. f.not_in_form = 1;
  38. this.fields_dict[df.fieldname] = f
  39. f.refresh();
  40. // first button primary ?
  41. if(df.fieldtype=='Button' && !this.first_button) {
  42. $(f.input).addClass('btn-info');
  43. this.first_button = true;
  44. }
  45. }
  46. }
  47. /* get values */
  48. this.get_values = function() {
  49. var ret = {};
  50. var errors = [];
  51. for(var key in this.fields_dict) {
  52. var f = this.fields_dict[key];
  53. var v = f.get_value ? f.get_value() : null;
  54. if(f.df.reqd && !v)
  55. errors.push(f.df.label + ' is mandatory');
  56. if(v) ret[f.df.fieldname] = v;
  57. }
  58. if(errors.length) {
  59. msgprint('<b>Please check the following Errors</b>\n' + errors.join('\n'));
  60. return null;
  61. }
  62. return ret;
  63. }
  64. /* set field value */
  65. this.set_value = function(key, val){
  66. var f = this.fields_dict[key];
  67. if(f) {
  68. f.set_input(val);
  69. f.refresh_mandatory();
  70. }
  71. }
  72. /* set values from a dict */
  73. this.set_values = function(dict) {
  74. for(var key in dict) {
  75. if(this.fields_dict[key]) {
  76. this.set_value(key, dict[key]);
  77. }
  78. }
  79. }
  80. this.clear = function() {
  81. for(key in this.fields_dict) {
  82. var f = this.fields_dict[key];
  83. if(f) {
  84. f.set_input(f.df['default'] || '');
  85. }
  86. }
  87. }
  88. }
  89. wn.widgets.Dialog = function(opts) {
  90. this.display = false;
  91. this.make = function(opts) {
  92. if(opts) {
  93. this.opts = opts;
  94. $.extend(this, opts);
  95. }
  96. if(!this.opts.width) this.opts.width = 480;
  97. if(!$('#dialog-container').length) {
  98. $('<div id="dialog-container">').appendTo('body');
  99. }
  100. this.wrapper = $('<div class="dialog_wrapper">').appendTo('#dialog-container').get(0);
  101. if(this.opts.width)
  102. this.wrapper.style.width = this.opts.width + 'px';
  103. this.make_head();
  104. this.body = $a(this.wrapper, 'div', 'dialog_body');
  105. if(this.opts.fields)
  106. this.make_fields(this.body, this.opts.fields);
  107. }
  108. this.make_head = function() {
  109. var me = this;
  110. this.appframe = new wn.ui.AppFrame(this.wrapper);
  111. this.appframe.$titlebar.find('.close').unbind('click').click(function() {
  112. if(me.oncancel)me.oncancel(); me.hide();
  113. });
  114. this.set_title(this.opts.title);
  115. }
  116. this.set_title = function(t) {
  117. this.appframe.$titlebar.find('.appframe-title').html(t || '');
  118. }
  119. this.set_postion = function() {
  120. // place it at the center
  121. this.wrapper.style.left = (($(window).width() - cint(this.wrapper.style.width))/2) + 'px';
  122. this.wrapper.style.top = ($(window).scrollTop() + 60) + 'px';
  123. // place it on top
  124. top_index++;
  125. $y(this.wrapper,{zIndex:top_index});
  126. }
  127. /** show the dialog */
  128. this.show = function() {
  129. // already live, do nothing
  130. if(this.display) return;
  131. // set position
  132. this.set_postion()
  133. // show it
  134. $ds(this.wrapper);
  135. // hide background
  136. freeze();
  137. this.display = true;
  138. cur_dialog = this;
  139. // call onshow
  140. if(this.onshow)this.onshow();
  141. }
  142. this.hide = function() {
  143. // call onhide
  144. if(this.onhide) this.onhide();
  145. // hide
  146. unfreeze();
  147. $dh(this.wrapper);
  148. // flags
  149. this.display = false;
  150. cur_dialog = null;
  151. }
  152. this.no_cancel = function() {
  153. this.appframe.$titlebar.find('.close').toggle(false);
  154. }
  155. if(opts) this.make(opts);
  156. }
  157. wn.widgets.Dialog.prototype = new wn.widgets.FieldGroup();
  158. wn.provide('wn.ui');
  159. wn.ui.Dialog = wn.widgets.Dialog
  160. // close open dialogs on ESC
  161. $(document).bind('keydown', function(e) {
  162. if(cur_dialog && !cur_dialog.no_cancel_flag && e.which==27) {
  163. cur_dialog.hide();
  164. }
  165. });