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.

преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 14 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. });