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

12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. filename = fileinfo[0];
  68. 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 yn = confirm("Are you sure you want to delete the attachment?");
  83. if(!yn) return;
  84. var data = $(this).data("fileid");
  85. wn.call({
  86. method: 'webnotes.widgets.form.utils.remove_attach',
  87. args: {
  88. 'fid': data,
  89. dt: me.frm.doctype,
  90. dn: me.frm.docname
  91. },
  92. callback: function(r,rt) {
  93. me.frm.doc.modified = r.message;
  94. me.remove_fileid(data);
  95. me.frm && me.frm.cscript.on_remove_attachment
  96. && me.frm.cscript.on_remove_attachment(me.frm.doc);
  97. me.frm.refresh();
  98. }
  99. });
  100. return false;
  101. });
  102. },
  103. new_attachment: function() {
  104. if(!this.dialog) {
  105. this.dialog = new wn.ui.Dialog({
  106. title:'Upload Attachment',
  107. width: 400
  108. })
  109. $y(this.dialog.body, {margin:'13px'})
  110. this.dialog.make();
  111. }
  112. this.dialog.body.innerHTML = '';
  113. this.dialog.show();
  114. wn.upload.make({
  115. parent: this.dialog.body,
  116. args: {
  117. from_form: 1,
  118. doctype: this.frm.doctype,
  119. docname: this.frm.docname
  120. },
  121. callback: wn.ui.form.file_upload_done
  122. });
  123. },
  124. remove_fileid: function(fileid) {
  125. this.frm.doc.file_list = $.map(this.get_filelist(), function(f) {
  126. if(f.split(',')[1]!=fileid) return f;
  127. }).join('\n');
  128. }
  129. })
  130. // this function will be called after the upload is done
  131. // from webnotes.utils.file_manager
  132. wn.ui.form.file_upload_done = function(doctype, docname, fileid, filename, at_id, new_timestamp) {
  133. // add to file_list
  134. var doc = locals[doctype][docname];
  135. if(doc.file_list) {
  136. var fl = doc.file_list.split('\n')
  137. fl.push(filename + ',' + fileid)
  138. doc.file_list = fl.join('\n');
  139. }
  140. else
  141. doc.file_list = filename + ',' + fileid;
  142. // update timestamp
  143. doc.modified = new_timestamp;
  144. // update file_list
  145. var frm = wn.views.formview[doctype].frm;
  146. frm.attachments.dialog.hide();
  147. msgprint('File Uploaded Sucessfully.');
  148. frm.refresh();
  149. }