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.

upload.js 4.0 KiB

12 vuotta sitten
13 vuotta sitten
11 vuotta sitten
13 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
13 vuotta sitten
13 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
13 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. // MIT License. See license.txt
  3. // parent, args, callback
  4. wn.upload = {
  5. make: function(opts) {
  6. if(!opts.args) opts.args = {};
  7. var $upload = $('<div class="file-upload">\
  8. <p class="small"><a class="action-attach disabled" href="#"><i class="icon-upload"></i> '
  9. + wn._('Upload a file') + '</a> | <a class="action-link" href="#"><i class="icon-link"></i> '
  10. + wn._('Attach as web link') + '</a></p>\
  11. <div class="action-attach-input">\
  12. <input class="alert alert-info" style="padding: 7px; margin: 7px 0px;" type="file" name="filedata" />\
  13. </div>\
  14. <div class="action-link-input" style="display: none; margin-top: 7px;">\
  15. <input class="form-control" style="max-width: 300px;" type="text" name="file_url" />\
  16. <p class="text-muted">'
  17. + (opts.sample_url || 'e.g. http://example.com/somefile.png') +
  18. '</p>\
  19. </div>\
  20. <button class="btn btn-info btn-upload"><i class="icon-upload"></i> ' +wn._('Upload')
  21. +'</button></div>').appendTo(opts.parent);
  22. $upload.find(".action-link").click(function() {
  23. $upload.find(".action-attach").removeClass("disabled");
  24. $upload.find(".action-link").addClass("disabled");
  25. $upload.find(".action-attach-input").toggle(false);
  26. $upload.find(".action-link-input").toggle(true);
  27. $upload.find(".btn-upload").html('<i class="icon-link"></i> ' +wn._('Set Link'))
  28. return false;
  29. })
  30. $upload.find(".action-attach").click(function() {
  31. $upload.find(".action-link").removeClass("disabled");
  32. $upload.find(".action-attach").addClass("disabled");
  33. $upload.find(".action-link-input").toggle(false);
  34. $upload.find(".action-attach-input").toggle(true);
  35. $upload.find(".btn-upload").html('<i class="icon-upload"></i> ' +wn._('Upload'))
  36. return false;
  37. })
  38. // get the first file
  39. $upload.find(".btn-upload").click(function() {
  40. // convert functions to values
  41. for(key in opts.args) {
  42. if(typeof val==="function")
  43. opt.args[key] = opts.args[key]();
  44. }
  45. // add other inputs in the div as arguments
  46. opts.args.params = {};
  47. $upload.find("input[name]").each(function() {
  48. var key = $(this).attr("name");
  49. var type = $(this).attr("type");
  50. if(key!="filedata" && key!="file_url") {
  51. if(type === "checkbox") {
  52. opts.args.params[key] = $(this).is(":checked");
  53. } else {
  54. opts.args.params[key] = $(this).val();
  55. }
  56. }
  57. })
  58. opts.args.file_url = $upload.find('[name="file_url"]').val();
  59. var fileobj = $upload.find(":file").get(0).files[0];
  60. wn.upload.upload_file(fileobj, opts.args, opts);
  61. })
  62. },
  63. upload_file: function(fileobj, args, opts) {
  64. if(!fileobj && !args.file_url) {
  65. msgprint(wn._("Please attach a file or set a URL"));
  66. return;
  67. }
  68. var dataurl = null;
  69. var _upload_file = function() {
  70. if(opts.on_attach) {
  71. opts.on_attach(args, dataurl)
  72. } else {
  73. var msgbox = msgprint(wn._("Uploading..."));
  74. return wn.call({
  75. "method": "uploadfile",
  76. args: args,
  77. callback: function(r) {
  78. if(!r._server_messages)
  79. msgbox.hide();
  80. if(r.exc) {
  81. // if no onerror, assume callback will handle errors
  82. opts.onerror ? opts.onerror(r) : opts.callback(null, null, r);
  83. return;
  84. }
  85. opts.callback(r.message.fid, r.message.filename, r);
  86. $(document).trigger("upload_complete",
  87. [r.message.fid, r.message.filename]);
  88. }
  89. });
  90. }
  91. }
  92. if(args.file_url) {
  93. _upload_file();
  94. } else {
  95. var freader = new FileReader();
  96. freader.onload = function() {
  97. args.filename = fileobj.name;
  98. if((opts.max_width || opts.max_height) && (/\.(gif|jpg|jpeg|tiff|png)$/i).test(args.filename)) {
  99. wn.utils.resize_image(freader, function(_dataurl) {
  100. dataurl = _dataurl;
  101. args.filedata = _dataurl.split(",")[1];
  102. console.log("resized!")
  103. _upload_file();
  104. })
  105. } else {
  106. dataurl = freader.result;
  107. args.filedata = freader.result.split(",")[1];
  108. _upload_file();
  109. }
  110. };
  111. freader.readAsDataURL(fileobj);
  112. }
  113. }
  114. }