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.

attachments.js 4.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  2. // MIT License. See license.txt
  3. wn.provide("wn.ui.form");
  4. wn.ui.form.Attachments = Class.extend({
  5. init: function(opts) {
  6. $.extend(this, opts);
  7. this.make();
  8. },
  9. make: function() {
  10. var me = this;
  11. this.wrapper = $('<div>\
  12. <div class="attachment-list"></div>\
  13. </div>').appendTo(this.parent);
  14. this.$list = this.wrapper.find(".attachment-list");
  15. this.parent.find(".btn").click(function() {
  16. me.new_attachment();
  17. });
  18. },
  19. max_reached: function() {
  20. // no of attachments
  21. var n = keys(this.get_attachments()).length;
  22. // button if the number of attachments is less than max
  23. if(n < this.frm.meta.max_attachments || !this.frm.meta.max_attachments) {
  24. return false;
  25. }
  26. return true;
  27. },
  28. refresh: function() {
  29. var doc = this.frm.doc;
  30. if(doc.__islocal) {
  31. this.parent.toggle(false);
  32. return;
  33. }
  34. this.parent.toggle(true);
  35. this.parent.find(".btn").toggle(!this.max_reached());
  36. this.$list.empty();
  37. var attachments = this.get_attachments();
  38. var file_names = keys(attachments).sort();
  39. // add attachment objects
  40. if(file_names.length) {
  41. for(var i=0; i<file_names.length; i++) {
  42. this.add_attachment(file_names[i], attachments);
  43. }
  44. } else {
  45. $('<p class="text-muted">' + wn._("None") + '</p>').appendTo(this.$list);
  46. }
  47. // refresh select fields with options attach_files:
  48. this.refresh_attachment_select_fields();
  49. },
  50. get_attachments: function() {
  51. return this.frm.get_docinfo().attachments;
  52. },
  53. add_attachment: function(filename, attachments) {
  54. var fileid = attachments[filename];
  55. var me = this;
  56. var $attach = $(repl('<div class="alert alert-info" style="margin-bottom: 7px">\
  57. <span style="display: inline-block; width: 90%; \
  58. text-overflow: ellipsis; white-space: nowrap; overflow: hidden;">\
  59. <i class="icon-file"></i> <a href="%(href)s"\
  60. target="_blank" title="%(filename)s">%(filename)s</a></span><a href="#" class="close">&times;</a>\
  61. </div>', {
  62. filename: filename,
  63. href: wn.utils.get_file_link(filename)
  64. }))
  65. .appendTo(this.$list)
  66. var $close =
  67. $attach.find(".close")
  68. .data("fileid", fileid)
  69. .click(function() {
  70. var remove_btn = this;
  71. wn.confirm(wn._("Are you sure you want to delete the attachment?"),
  72. function() {
  73. var data = $(remove_btn).data("fileid");
  74. return wn.call({
  75. method: 'webnotes.widgets.form.utils.remove_attach',
  76. args: {
  77. fid: data,
  78. dt: me.frm.doctype,
  79. dn: me.frm.docname
  80. },
  81. callback: function(r,rt) {
  82. if(r.exc) {
  83. if(!r._server_messages)
  84. msgprint("There were errors.");
  85. return;
  86. }
  87. me.remove_fileid(data);
  88. me.frm.toolbar.show_infobar();
  89. }
  90. });
  91. });
  92. return false;
  93. });
  94. if(!wn.model.can_write(this.frm.doctype, this.frm.name)) {
  95. $close.remove();
  96. }
  97. },
  98. new_attachment: function(fieldname) {
  99. var me = this;
  100. if(!this.dialog) {
  101. this.dialog = new wn.ui.Dialog({
  102. title: wn._('Upload Attachment'),
  103. });
  104. }
  105. this.dialog.show();
  106. $(this.dialog.body).empty();
  107. wn.upload.make({
  108. parent: this.dialog.body,
  109. args: {
  110. from_form: 1,
  111. doctype: this.frm.doctype,
  112. docname: this.frm.docname,
  113. },
  114. callback: function(fileid, filename, r) {
  115. me.dialog.hide();
  116. me.update_attachment(fileid, filename, fieldname, r);
  117. },
  118. onerror: function() {
  119. me.dialog.hide();
  120. },
  121. max_width: this.frm.cscript ? this.frm.cscript.attachment_max_width : null,
  122. max_height: this.frm.cscript ? this.frm.cscript.attachment_max_height : null,
  123. });
  124. },
  125. update_attachment: function(fileid, filename, fieldname, r) {
  126. if(fileid) {
  127. this.add_to_attachments(fileid, filename);
  128. this.refresh();
  129. if(fieldname) {
  130. this.frm.set_value(fieldname, wn.utils.get_file_link(filename));
  131. this.frm.cscript[fieldname] && this.frm.cscript[fieldname](this.frm.doc);
  132. this.frm.toolbar.show_infobar();
  133. }
  134. }
  135. },
  136. add_to_attachments: function(fileid, filename) {
  137. this.get_attachments()[filename] = fileid;
  138. },
  139. remove_fileid: function(fileid) {
  140. var attachments = this.get_attachments();
  141. var new_attachments = {};
  142. $.each(attachments, function(key, value) {
  143. if(value!=fileid)
  144. new_attachments[key] = value;
  145. });
  146. this.frm.get_docinfo().attachments = new_attachments;
  147. this.refresh();
  148. },
  149. refresh_attachment_select_fields: function() {
  150. for(var i=0; i<this.frm.fields.length; i++) {
  151. if(this.frm.fields[i].df.options=="attach_files:" && this.frm.fields[i].$input) {
  152. var fieldname = this.frm.fields[i].df.fieldname;
  153. var selected_option = this.frm.fields[i].$input.find("option:selected").val();
  154. if(this.frm.doc[fieldname]!=null && selected_option!==this.frm.doc[fieldname]) {
  155. this.frm.script_manager.trigger(fieldname);
  156. this.frm.set_value(fieldname, "");
  157. }
  158. this.frm.fields[i].refresh();
  159. }
  160. }
  161. }
  162. });