Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

dialog.js 2.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  2. // MIT License. See license.txt
  3. wn.provide('wn.ui');
  4. var cur_dialog;
  5. wn.ui.open_dialogs = [];
  6. wn.ui.Dialog = wn.ui.FieldGroup.extend({
  7. _intro:' usage:\n\
  8. \n\
  9. var dialog = new wn.ui.Dialog({\n\
  10. title: "Dialog Title",\n\
  11. fields: [\n\
  12. {fieldname:"field1", fieldtype:"Data", reqd:1, label: "Test 1"},\n\
  13. {fieldname:"field2", fieldtype:"Link", reqd:1, label: "Test 1", options:"Some DocType"},\n\
  14. {fieldname:"mybutton", fieldtype:"Button", reqd:1, label: "Submit"},\n\
  15. ]\n\
  16. })\n\
  17. dialog.get_input("mybutton").click(function() { /* do something; */ dialog.hide(); });\n\
  18. dialog.show()',
  19. init: function(opts) {
  20. this.display = false;
  21. if(!opts.width) opts.width = 480;
  22. $.extend(this, opts);
  23. this._super();
  24. this.make();
  25. },
  26. make: function() {
  27. // ui-front class is used as appendTo by jquery.autocomplete
  28. this.$wrapper = $('<div class="modal" style="overflow: auto;">\
  29. <div class="modal-dialog">\
  30. <div class="modal-content">\
  31. <div class="modal-header">\
  32. <a type="button" class="close" \
  33. data-dismiss="modal" aria-hidden="true">&times;</a>\
  34. <h4 class="modal-title"></h4>\
  35. </div>\
  36. <div class="modal-body ui-front">\
  37. </div>\
  38. </div>\
  39. </div>\
  40. </div>')
  41. .appendTo(document.body);
  42. this.wrapper = this.$wrapper.find('.modal-dialog').get(0);
  43. this.make_head();
  44. this.body = this.$wrapper.find(".modal-body").get(0);
  45. // make fields (if any)
  46. this._super();
  47. var me = this;
  48. this.$wrapper
  49. .on("hide.bs.modal", function() {
  50. me.display = false;
  51. if(wn.ui.open_dialogs[wn.ui.open_dialogs.length-1]===me) {
  52. wn.ui.open_dialogs.pop();
  53. if(wn.ui.open_dialogs.length)
  54. cur_dialog = wn.ui.open_dialogs[wn.ui.open_dialogs.length-1];
  55. else
  56. cur_dialog = null;
  57. }
  58. me.onhide && me.onhide();
  59. })
  60. .on("shown.bs.modal", function() {
  61. // focus on first input
  62. me.display = true;
  63. cur_dialog = me;
  64. wn.ui.open_dialogs.push(me);
  65. var first = me.$wrapper.find(':input:first');
  66. if(first.length && first.attr("data-fieldtype")!="Date") {
  67. try {
  68. first.get(0).focus();
  69. } catch(e) {
  70. console.log("Dialog: unable to focus on first input: " + e);
  71. }
  72. }
  73. me.onshow && me.onshow();
  74. })
  75. },
  76. make_head: function() {
  77. var me = this;
  78. //this.appframe = new wn.ui.AppFrame(this.wrapper);
  79. //this.appframe.set_document_title = false;
  80. this.set_title(this.title);
  81. },
  82. set_title: function(t) {
  83. this.$wrapper.find(".modal-title").html(t);
  84. },
  85. show: function() {
  86. // show it
  87. this.$wrapper.modal("show");
  88. },
  89. hide: function(from_event) {
  90. this.$wrapper.modal("hide");
  91. },
  92. no_cancel: function() {
  93. this.$wrapper.find('.close').toggle(false);
  94. }
  95. });
  96. // close open dialogs on ESC
  97. $(document).bind('keydown', function(e) {
  98. if(cur_dialog && !cur_dialog.no_cancel_flag && e.which==27) {
  99. cur_dialog.hide();
  100. }
  101. });