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.
 
 
 
 
 
 

184 line
5.0 KiB

  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  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. me.remove_attachment($(remove_btn).data("fileid"))
  74. }
  75. );
  76. return false;
  77. });
  78. if(!wn.model.can_write(this.frm.doctype, this.frm.name)) {
  79. $close.remove();
  80. }
  81. },
  82. remove_attachment_by_filename: function(filename, callback) {
  83. this.remove_attachment(this.get_attachments()[filename], callback);
  84. },
  85. remove_attachment: function(fileid, callback) {
  86. var me = this;
  87. return wn.call({
  88. method: 'webnotes.widgets.form.utils.remove_attach',
  89. args: {
  90. fid: fileid,
  91. dt: me.frm.doctype,
  92. dn: me.frm.docname
  93. },
  94. callback: function(r,rt) {
  95. if(r.exc) {
  96. if(!r._server_messages)
  97. msgprint("There were errors.");
  98. return;
  99. }
  100. me.remove_fileid(fileid);
  101. me.frm.toolbar.show_infobar();
  102. if(callback) callback();
  103. }
  104. });
  105. },
  106. new_attachment: function(fieldname) {
  107. var me = this;
  108. if(!this.dialog) {
  109. this.dialog = new wn.ui.Dialog({
  110. title: wn._('Upload Attachment'),
  111. });
  112. }
  113. this.dialog.show();
  114. $(this.dialog.body).empty();
  115. wn.upload.make({
  116. parent: this.dialog.body,
  117. args: {
  118. from_form: 1,
  119. doctype: this.frm.doctype,
  120. docname: this.frm.docname,
  121. },
  122. callback: function(fileid, filename, r) {
  123. me.dialog.hide();
  124. me.update_attachment(fileid, filename, fieldname, r);
  125. },
  126. onerror: function() {
  127. me.dialog.hide();
  128. },
  129. max_width: this.frm.cscript ? this.frm.cscript.attachment_max_width : null,
  130. max_height: this.frm.cscript ? this.frm.cscript.attachment_max_height : null,
  131. });
  132. },
  133. update_attachment: function(fileid, filename, fieldname, r) {
  134. if(fileid) {
  135. this.add_to_attachments(fileid, filename);
  136. this.refresh();
  137. if(fieldname) {
  138. this.frm.set_value(fieldname, wn.utils.get_file_link(filename));
  139. this.frm.cscript[fieldname] && this.frm.cscript[fieldname](this.frm.doc);
  140. this.frm.toolbar.show_infobar();
  141. }
  142. }
  143. },
  144. add_to_attachments: function(fileid, filename) {
  145. this.get_attachments()[filename] = fileid;
  146. },
  147. remove_fileid: function(fileid) {
  148. var attachments = this.get_attachments();
  149. var new_attachments = {};
  150. $.each(attachments, function(key, value) {
  151. if(value!=fileid)
  152. new_attachments[key] = value;
  153. });
  154. this.frm.get_docinfo().attachments = new_attachments;
  155. this.refresh();
  156. },
  157. refresh_attachment_select_fields: function() {
  158. for(var i=0; i<this.frm.fields.length; i++) {
  159. if(this.frm.fields[i].df.options=="attach_files:" && this.frm.fields[i].$input) {
  160. var fieldname = this.frm.fields[i].df.fieldname;
  161. var selected_option = this.frm.fields[i].$input.find("option:selected").val();
  162. if(this.frm.doc[fieldname]!=null && selected_option!==this.frm.doc[fieldname]) {
  163. this.frm.script_manager.trigger(fieldname);
  164. this.frm.set_value(fieldname, "");
  165. }
  166. this.frm.fields[i].refresh();
  167. }
  168. }
  169. }
  170. });