選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

215 行
5.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. // 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. this.catch_enter_as_submit = function() {
  48. var me = this;
  49. $(this.body).find(':input[type="text"], :input[type="password"]').keypress(function(e) {
  50. if(e.which==13) {
  51. $(me.body).find('.btn-info:first').click();
  52. }
  53. })
  54. }
  55. /* get values */
  56. this.get_values = function() {
  57. var ret = {};
  58. var errors = [];
  59. for(var key in this.fields_dict) {
  60. var f = this.fields_dict[key];
  61. var v = f.get_value ? f.get_value() : null;
  62. if(f.df.reqd && !v)
  63. errors.push(f.df.label + ' is mandatory');
  64. if(v) ret[f.df.fieldname] = v;
  65. }
  66. if(errors.length) {
  67. msgprint('<b>Please check the following Errors</b>\n' + errors.join('\n'));
  68. return null;
  69. }
  70. return ret;
  71. }
  72. /* set field value */
  73. this.set_value = function(key, val){
  74. var f = this.fields_dict[key];
  75. if(f) {
  76. f.set_input(val);
  77. f.refresh_mandatory();
  78. }
  79. }
  80. /* set values from a dict */
  81. this.set_values = function(dict) {
  82. for(var key in dict) {
  83. if(this.fields_dict[key]) {
  84. this.set_value(key, dict[key]);
  85. }
  86. }
  87. }
  88. this.clear = function() {
  89. for(key in this.fields_dict) {
  90. var f = this.fields_dict[key];
  91. if(f) {
  92. f.set_input(f.df['default'] || '');
  93. }
  94. }
  95. }
  96. }
  97. wn.widgets.Dialog = function(opts) {
  98. this.display = false;
  99. this.make = function(opts) {
  100. if(opts) {
  101. this.opts = opts;
  102. $.extend(this, opts);
  103. }
  104. if(!this.opts.width) this.opts.width = 480;
  105. if(!$('#dialog-container').length) {
  106. $('<div id="dialog-container">').appendTo('body');
  107. }
  108. this.wrapper = $('<div class="dialog_wrapper">').appendTo('#dialog-container').get(0);
  109. if(this.opts.width)
  110. this.wrapper.style.width = this.opts.width + 'px';
  111. this.make_head();
  112. this.body = $a(this.wrapper, 'div', 'dialog_body');
  113. if(this.opts.fields) {
  114. this.make_fields(this.body, this.opts.fields);
  115. this.catch_enter_as_submit();
  116. }
  117. }
  118. this.make_head = function() {
  119. var me = this;
  120. this.appframe = new wn.ui.AppFrame(this.wrapper);
  121. this.appframe.$titlebar.find('.close').unbind('click').click(function() {
  122. if(me.oncancel)me.oncancel(); me.hide();
  123. });
  124. this.set_title(this.opts.title);
  125. }
  126. this.set_title = function(t) {
  127. this.appframe.$titlebar.find('.appframe-title').html(t || '');
  128. }
  129. this.set_postion = function() {
  130. // place it at the center
  131. this.wrapper.style.left = (($(window).width() - cint(this.wrapper.style.width))/2) + 'px';
  132. this.wrapper.style.top = ($(window).scrollTop() + 60) + 'px';
  133. // place it on top
  134. top_index++;
  135. $y(this.wrapper,{zIndex:top_index});
  136. }
  137. /** show the dialog */
  138. this.show = function() {
  139. // already live, do nothing
  140. if(this.display) return;
  141. // set position
  142. this.set_postion()
  143. // show it
  144. $ds(this.wrapper);
  145. // hide background
  146. freeze();
  147. this.display = true;
  148. cur_dialog = this;
  149. // call onshow
  150. if(this.onshow)this.onshow();
  151. // focus on first input
  152. $(this.wrapper).find(':input:first').focus();
  153. }
  154. this.hide = function() {
  155. // call onhide
  156. if(this.onhide) this.onhide();
  157. // hide
  158. unfreeze();
  159. $dh(this.wrapper);
  160. // flags
  161. this.display = false;
  162. cur_dialog = null;
  163. }
  164. this.no_cancel = function() {
  165. this.appframe.$titlebar.find('.close').toggle(false);
  166. }
  167. if(opts) this.make(opts);
  168. }
  169. wn.widgets.Dialog.prototype = new wn.widgets.FieldGroup();
  170. wn.provide('wn.ui');
  171. wn.ui.Dialog = wn.widgets.Dialog
  172. // close open dialogs on ESC
  173. $(document).bind('keydown', function(e) {
  174. if(cur_dialog && !cur_dialog.no_cancel_flag && e.which==27) {
  175. cur_dialog.hide();
  176. }
  177. });