image-chooser-modal.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. IMAGE_CHOOSER_MODAL_ONLOAD_HANDLERS = {
  2. 'chooser': function(modal, jsonData) {
  3. var searchUrl = $('form.image-search', modal.body).attr('action');
  4. /* currentTag stores the tag currently being filtered on, so that we can
  5. preserve this when paginating */
  6. var currentTag;
  7. function ajaxifyLinks (context) {
  8. $('.listing a', context).on('click', function() {
  9. modal.loadUrl(this.href);
  10. return false;
  11. });
  12. $('.pagination a', context).on('click', function() {
  13. var page = this.getAttribute("data-page");
  14. setPage(page);
  15. return false;
  16. });
  17. }
  18. function fetchResults(requestData) {
  19. $.ajax({
  20. url: searchUrl,
  21. data: requestData,
  22. success: function(data, status) {
  23. $('#image-results').html(data);
  24. ajaxifyLinks($('#image-results'));
  25. }
  26. });
  27. }
  28. function search() {
  29. /* Searching causes currentTag to be cleared - otherwise there's
  30. no way to de-select a tag */
  31. currentTag = null;
  32. fetchResults({
  33. q: $('#id_q').val(),
  34. collection_id: $('#collection_chooser_collection_id').val()
  35. });
  36. return false;
  37. }
  38. function setPage(page) {
  39. params = {p: page};
  40. if ($('#id_q').val().length){
  41. params['q'] = $('#id_q').val();
  42. }
  43. if (currentTag) {
  44. params['tag'] = currentTag;
  45. }
  46. params['collection_id'] = $('#collection_chooser_collection_id').val();
  47. fetchResults(params);
  48. return false;
  49. }
  50. ajaxifyLinks(modal.body);
  51. $('form.image-upload', modal.body).on('submit', function() {
  52. var formdata = new FormData(this);
  53. if ($('#id_title', modal.body).val() == '') {
  54. var li = $('#id_title', modal.body).closest('li');
  55. if (!li.hasClass('error')) {
  56. li.addClass('error');
  57. $('#id_title', modal.body).closest('.field-content').append('<p class="error-message"><span>This field is required.</span></p>')
  58. }
  59. setTimeout(cancelSpinner, 500);
  60. } else {
  61. $.ajax({
  62. url: this.action,
  63. data: formdata,
  64. processData: false,
  65. contentType: false,
  66. type: 'POST',
  67. dataType: 'text',
  68. success: function(response){
  69. modal.loadResponseText(response);
  70. },
  71. error: function(response, textStatus, errorThrown) {
  72. message = jsonData['error_message'] + '<br />' + errorThrown + ' - ' + response.status;
  73. $('#upload').append(
  74. '<div class="help-block help-critical">' +
  75. '<strong>' + jsonData['error_label'] + ': </strong>' + message + '</div>');
  76. }
  77. });
  78. }
  79. return false;
  80. });
  81. $('form.image-search', modal.body).on('submit', search);
  82. $('#id_q').on('input', function() {
  83. clearTimeout($.data(this, 'timer'));
  84. var wait = setTimeout(search, 200);
  85. $(this).data('timer', wait);
  86. });
  87. $('#collection_chooser_collection_id').on('change', search);
  88. $('a.suggested-tag').on('click', function() {
  89. currentTag = $(this).text();
  90. $('#id_q').val('');
  91. fetchResults({
  92. 'tag': currentTag,
  93. collection_id: $('#collection_chooser_collection_id').val()
  94. });
  95. return false;
  96. });
  97. function populateTitle(context) {
  98. // Note: There are two inputs with `#id_title` on the page.
  99. // The page title and image title. Select the input inside the modal body.
  100. var fileWidget = $('#id_file', context);
  101. fileWidget.on('change', function () {
  102. var titleWidget = $('#id_title', context);
  103. var title = titleWidget.val();
  104. if (title === '') {
  105. // The file widget value example: `C:\fakepath\image.jpg`
  106. var parts = fileWidget.val().split('\\');
  107. var fileName = parts[parts.length - 1];
  108. titleWidget.val(fileName);
  109. }
  110. });
  111. }
  112. populateTitle(modal.body);
  113. /* Add tag entry interface (with autocompletion) to the tag field of the image upload form */
  114. $('#id_tags', modal.body).tagit({
  115. autocomplete: {source: jsonData['tag_autocomplete_url']}
  116. });
  117. },
  118. 'image_chosen': function(modal, jsonData) {
  119. modal.respond('imageChosen', jsonData['result']);
  120. modal.close();
  121. },
  122. 'select_format': function(modal) {
  123. $('form', modal.body).on('submit', function() {
  124. var formdata = new FormData(this);
  125. $.post(this.action, $(this).serialize(), function(response){
  126. modal.loadResponseText(response);
  127. }, 'text');
  128. return false;
  129. });
  130. }
  131. };