/public/javascripts/capture/Uploader.js

https://github.com/rap1ds/hannotaatio · JavaScript · 132 lines · 77 code · 13 blank · 42 comment · 8 complexity · 73d1c4aa9bdc9c412857bf6894f430de MD5 · raw file

  1. /**
  2. * Uploader is responsible of sending the contents of one Capturer object to
  3. * the API server. There are two different methods of sending the annotation:
  4. * an AJAX based approach and a HTML Form based approach. Both methods use the
  5. * same input, that is an Capturer object.
  6. *
  7. * @constructor
  8. * @param {Preferences} prefs The preferences are used when
  9. * determining upload url and parameters to identify
  10. * different projects.
  11. */
  12. var Uploader = function(prefs) {
  13. this.prefs = prefs;
  14. };
  15. /**
  16. * Creates a new Annotation in the Hannotatio service using AJAX. The Annotation
  17. * consists of several entities that are saved using separate REST requests. One
  18. * Annotation consists of at least the Annotation metadata (including the UUID),
  19. * and the actual HTML content of the page to annotate.
  20. *
  21. * @param {Capturer} capturer a Capturer object.
  22. * @param {function(string)} successCallback Called with the content
  23. * when the upload is complete. Optional.
  24. * @param {function(string)} errorCallback error callback.
  25. */
  26. Uploader.prototype.uploadAjax = function(capturer, successCallback,
  27. errorCallback) {
  28. var annotations_url = this.prefs.api_url + 'annotations';
  29. var uuid = capturer.uuid;
  30. var annotationMetadata = capturer.getAnnotationMetadata();
  31. var htmlContent = capturer.getHtmlContent();
  32. $.ajax({
  33. type: 'POST',
  34. // Make sure that the site is located on the same domain as the url
  35. url: annotations_url,
  36. contentType: 'application/json',
  37. processData: false,
  38. dataType: 'json',
  39. data: HannotaatioJSON.stringify(annotationMetadata),
  40. complete: function(request, status) {
  41. if (request.status == 201) {
  42. $.ajax({
  43. type: 'POST',
  44. // Make sure that the site is located on the same
  45. // domain as the url
  46. url: annotations_url + '/' + uuid + '/capture/page.html',
  47. contentType: 'text/html',
  48. data: htmlContent,
  49. complete: function(request, status) {
  50. if (request.status == 201) {
  51. successCallback(request.status);
  52. } else {
  53. errorCallback(request.status);
  54. }
  55. }
  56. });
  57. }
  58. else {
  59. errorCallback(request.status);
  60. }
  61. }
  62. });
  63. };
  64. /**
  65. * Creates a new Annotation in the Hannotatio service using a HTML Form. See the
  66. * corresponding AJAX function for more details.
  67. *
  68. * @param {Capturer} capturer a Capturer object.
  69. */
  70. Uploader.prototype.uploadForm = function(capturer) {
  71. var annotations_url = this.prefs.api_url + 'annotations/' + '?api_key=' + this.prefs.apiKey;
  72. var uuid = capturer.uuid;
  73. var annotationMetadata = capturer.getAnnotationMetadata();
  74. var htmlContent = capturer.getHtmlContent();
  75. var doctype = capturer.getDoctype();
  76. var content = doctype !== null ? doctype + '\n' + htmlContent : htmlContent;
  77. var capturedImages = capturer.capturedImages;
  78. var formFields = {};
  79. $.each(annotationMetadata.annotation, function(key, value) {
  80. formFields['annotation[' + key + ']'] = value;
  81. });
  82. // Page
  83. formFields['capture[page.html]'] = content;
  84. // Images
  85. if(capturedImages != null) {
  86. $.each(capturedImages, function(index, value) {
  87. var image = value;
  88. formFields['capture_encoding[' + image.newUrl + ']'] = 'base64';
  89. formFields['capture[' + image.newUrl + ']'] = image.data;
  90. });
  91. }
  92. // Create a form holding the data to save.
  93. // Important: Be shure to set the accept-charset to UTF-8
  94. var $form = $('<form action="' + annotations_url +
  95. '" method="post" accept-charset="utf-8" id="apiForm"></form>');
  96. // Populate the form with the form fields to send.
  97. // The key and value attributes are set using function calls rather than
  98. // inlining their contents to the html string used for creating the element.
  99. // When passing the values as function arguments they can contain any
  100. // characters, and we do not have to worry about proper escaping in the html
  101. // text.
  102. var element = null;
  103. $.each(formFields, function(key, value) {
  104. $element = $("<input type='text'/>");
  105. $element.attr('name', key);
  106. $element.val(value);
  107. $form.append($element);
  108. });
  109. $.each(this.prefs.notificationEmails, function(key, value) {
  110. $element = $("<input type='text'/>");
  111. $element.attr('name', 'notification_emails[]');
  112. $element.val(value);
  113. $form.append($element);
  114. });
  115. $form.hide();
  116. $('body').append($form);
  117. $form.submit();
  118. };
  119. // (function(){ /* test coverage for JSCoverage */ })();