Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

attachments.js 4.7 KiB

12 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.form");
  23. wn.ui.form.Attachments = Class.extend({
  24. init: function(opts) {
  25. $.extend(this, opts);
  26. this.make();
  27. },
  28. make: function() {
  29. var me = this;
  30. this.wrapper = $('<div>\
  31. <div class="alert-list"></div>\
  32. <button class="btn btn-small"><i class="icon-plus"></i></button>\
  33. </div>').appendTo(this.parent);
  34. this.$list = this.wrapper.find(".alert-list");
  35. this.parent.find(".btn").click(function() {
  36. me.new_attachment();
  37. });
  38. },
  39. max_reached: function() {
  40. // no of attachments
  41. var n = this.frm.doc.file_list ? this.frm.doc.file_list.split('\n').length : 0;
  42. // button if the number of attachments is less than max
  43. if(n < this.frm.meta.max_attachments || !this.frm.meta.max_attachments) {
  44. return false;
  45. }
  46. return true;
  47. },
  48. refresh: function() {
  49. if(this.frm.doc.__islocal || !this.frm.meta.allow_attach) {
  50. this.parent.toggle(false);
  51. return;
  52. }
  53. this.parent.toggle(true);
  54. this.parent.find(".btn").toggle(!this.max_reached());
  55. this.$list.empty();
  56. var fl = this.get_filelist();
  57. // add attachment objects
  58. for(var i=0; i<fl.length; i++) {
  59. this.add_attachment(fl[i]);
  60. }
  61. },
  62. get_filelist: function() {
  63. return this.frm.doc.file_list ? this.frm.doc.file_list.split('\n') : [];
  64. },
  65. add_attachment: function(fileinfo) {
  66. fileinfo = fileinfo.split(',');
  67. var filename = fileinfo[0];
  68. var fileid = fileinfo[1];
  69. var me = this;
  70. $(repl('<div class="alert alert-info"><span style="display: inline-block; width: 90%;\
  71. text-overflow: ellipsis; white-space: nowrap; overflow: hidden;">\
  72. <i class="icon-file"></i> <a href="%(href)s"\
  73. target="_blank" title="%(filename)s">%(filename)s</a></span><a href="#" class="close">&times;</a>\
  74. </div>', {
  75. filename: filename,
  76. href: wn.utils.get_file_link(filename)
  77. }))
  78. .appendTo(this.$list)
  79. .find(".close")
  80. .data("fileid", fileid)
  81. .click(function() {
  82. var remove_btn = this;
  83. wn.confirm(wn._("Are you sure you want to delete the attachment?"),
  84. function() {
  85. var data = $(remove_btn).data("fileid");
  86. wn.call({
  87. method: 'webnotes.widgets.form.utils.remove_attach',
  88. args: {
  89. fid: data,
  90. dt: me.frm.doctype,
  91. dn: me.frm.docname
  92. },
  93. callback: function(r,rt) {
  94. if(r.exc) {
  95. msgprint("There were errors.");
  96. return;
  97. }
  98. me.remove_fileid(data);
  99. me.frm && me.frm.cscript.on_remove_attachment
  100. && me.frm.cscript.on_remove_attachment(me.frm.doc);
  101. me.frm.refresh();
  102. }
  103. });
  104. });
  105. return false;
  106. });
  107. },
  108. new_attachment: function() {
  109. var me = this;
  110. if(!this.dialog) {
  111. this.dialog = new wn.ui.Dialog({
  112. title: wn._('Upload Attachment'),
  113. width: 400
  114. });
  115. $y(this.dialog.body, {margin:'13px'});
  116. this.dialog.make();
  117. }
  118. this.dialog.body.innerHTML = '';
  119. this.dialog.show();
  120. wn.upload.make({
  121. parent: this.dialog.body,
  122. args: {
  123. from_form: 1,
  124. doctype: this.frm.doctype,
  125. docname: this.frm.docname
  126. },
  127. callback: function(fileid, filename) {
  128. me.add_to_file_list(fileid, filename);
  129. me.dialog.hide();
  130. me.refresh();
  131. }
  132. });
  133. },
  134. add_to_file_list: function(fileid, filename) {
  135. var doc = this.frm.doc;
  136. if(doc.file_list) {
  137. var fl = doc.file_list.split('\n');
  138. fl.push(filename + ',' + fileid);
  139. doc.file_list = fl.join('\n');
  140. } else {
  141. doc.file_list = filename + ',' + fileid;
  142. }
  143. },
  144. remove_fileid: function(fileid) {
  145. this.frm.doc.file_list = $.map(this.get_filelist(), function(f) {
  146. if(f.split(',')[1]!=fileid) return f;
  147. }).join('\n');
  148. }
  149. });