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.
 
 
 
 
 
 

114 lines
3.2 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.Dialog = wn.ui.FieldGroup.extend({
  24. init: function(opts) {
  25. this.display = false;
  26. if(!opts.width) opts.width = 480;
  27. $.extend(this, opts);
  28. this.make();
  29. // init fields
  30. if(this.fields) {
  31. this.parent = this.body
  32. this._super({});
  33. }
  34. },
  35. make: function() {
  36. if(!$('#dialog-container').length) {
  37. $('<div id="dialog-container">').appendTo('body');
  38. }
  39. this.wrapper = $('<div class="dialog_wrapper">')
  40. .appendTo('#dialog-container').get(0);
  41. if(this.width)
  42. this.wrapper.style.width = this.width + 'px';
  43. this.make_head();
  44. this.body = $a(this.wrapper, 'div', 'dialog_body');
  45. },
  46. make_head: function() {
  47. var me = this;
  48. this.appframe = new wn.ui.AppFrame(this.wrapper);
  49. this.appframe.set_document_title = false;
  50. this.appframe.$titlebar.find('.close').unbind('click').click(function() {
  51. if(me.oncancel)me.oncancel(); me.hide();
  52. });
  53. this.set_title(this.title);
  54. },
  55. set_title: function(t) {
  56. this.appframe.$titlebar.find('.appframe-title').html(t || '');
  57. },
  58. set_postion: function() {
  59. // place it at the center
  60. this.wrapper.style.left = (($(window).width() - cint(this.wrapper.style.width))/2) + 'px';
  61. this.wrapper.style.top = ($(window).scrollTop() + 60) + 'px';
  62. },
  63. show: function() {
  64. // already live, do nothing
  65. if(this.display) return;
  66. // set position
  67. this.set_postion()
  68. // show it
  69. $ds(this.wrapper);
  70. // hide background
  71. wn.dom.freeze();
  72. this.display = true;
  73. cur_dialog = this;
  74. // call onshow
  75. if(this.onshow)this.onshow();
  76. // focus on first input
  77. $(this.wrapper).find(':input:first').focus();
  78. },
  79. hide: function() {
  80. // call onhide
  81. if(this.onhide) this.onhide();
  82. // hide
  83. wn.dom.unfreeze();
  84. $dh(this.wrapper);
  85. // flags
  86. this.display = false;
  87. cur_dialog = null;
  88. },
  89. no_cancel: function() {
  90. this.appframe.$titlebar.find('.close').toggle(false);
  91. }
  92. });
  93. // close open dialogs on ESC
  94. $(document).bind('keydown', function(e) {
  95. if(cur_dialog && !cur_dialog.no_cancel_flag && e.which==27) {
  96. cur_dialog.hide();
  97. }
  98. });