app.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. $(function() {
  2. var errorTag = $('#error');
  3. var reloadPhotosTag = $('#reload_photos');
  4. var showLicenseTag = $('#show_license');
  5. var showLicenseVal = showLicenseTag.val();
  6. var showPrivacyTag = $('#show_privacy');
  7. var showPrivacyVal = showPrivacyTag.val();
  8. var showIgnoredTag = $('#show_ignored');
  9. var showIgnoredVal = showIgnoredTag.val();
  10. var photosTag = $('#photos');
  11. var selectLicenseTag = $('#select_license');
  12. var applyLicenseTag = $('#apply_license');
  13. var licenseLinkTag = $('#license_link');
  14. function controlsDisabled(disabled) {
  15. reloadPhotosTag.prop('disabled', disabled);
  16. showLicenseTag.prop('disabled', disabled);
  17. showPrivacyTag.prop('disabled', disabled);
  18. showIgnoredTag.prop('disabled', disabled);
  19. selectLicenseTag.prop('disabled', disabled)
  20. }
  21. function reloadPhotos(params = {}, path = '/photos/1') {
  22. controlsDisabled(true);
  23. $.getJSON(path, params, function(data) {
  24. photosTag.children('.spinner').remove();
  25. console.log(data);
  26. if (data.path) {
  27. reloadPhotos({}, data.path);
  28. } else {
  29. showLicenseTag.change();
  30. showPrivacyTag.change();
  31. showIgnoredTag.change();
  32. controlsDisabled(false);
  33. }
  34. }).fail(function() {
  35. controlsDisabled(false);
  36. });
  37. }
  38. function showLicense() {
  39. if (showLicenseVal == showLicenseTag.val()) {
  40. console.log(showLicenseVal);
  41. } else {
  42. $.post('/user', {show_license: showLicenseTag.val()}, function() {
  43. showLicenseVal = showLicenseTag.val();
  44. showLicense();
  45. }).fail(function() {
  46. showLicenseTag.val(showLicenseVal);
  47. });
  48. }
  49. }
  50. function showPrivacy() {
  51. if (showPrivacyVal == showPrivacyTag.val()) {
  52. console.log(showPrivacyVal);
  53. } else {
  54. $.post('/user', {show_privacy: showPrivacyTag.val()}, function() {
  55. showPrivacyVal = showPrivacyTag.val();
  56. showPrivacy();
  57. }).fail(function() {
  58. showPrivacyTag.val(showPrivacyVal);
  59. });
  60. }
  61. }
  62. function showIgnored() {
  63. if (showIgnoredVal == showIgnoredTag.val()) {
  64. console.log(showIgnoredVal);
  65. } else {
  66. $.post('/user', {show_ignored: showIgnoredTag.val()}, function() {
  67. showIgnoredVal = showIgnoredTag.val();
  68. showIgnored();
  69. }).fail(function() {
  70. showIgnoredTag.val(showIgnoredVal);
  71. });
  72. }
  73. }
  74. errorTag.dialog({autoOpen: false, modal: true});
  75. $(document).ajaxError(function(event, request, settings, error) {
  76. if (request.responseJSON && request.responseJSON.error) {
  77. errorTag.text(request.responseJSON.error);
  78. } else {
  79. errorTag.empty().append($('<div>').text(request.status + ' ' + error), $('<iframe>', {style: 'height: 100%; width: 100%;', srcdoc: request.responseText}));
  80. }
  81. errorTag.dialog('open');
  82. });
  83. reloadPhotosTag.click(function() {
  84. photosTag.empty().append('<div class="spinner">');
  85. selectLicenseTag.val('').change();
  86. reloadPhotos({reload: true});
  87. });
  88. showLicenseTag.change(showLicense);
  89. showPrivacyTag.change(showPrivacy);
  90. showIgnoredTag.change(showIgnored);
  91. selectLicenseTag.change(function() {
  92. applyLicenseTag.prop('disabled', selectLicenseTag.val() == '');
  93. licenseLinkTag.empty();
  94. var license = licenses[selectLicenseTag.val()];
  95. if (license) {
  96. licenseLinkTag.append(license.url ? $('<a>', {href: license.url, target: '_blank'}).text(license.name) : license.name);
  97. }
  98. });
  99. applyLicenseTag.click(function() {
  100. console.log(selectLicenseTag.val());
  101. });
  102. reloadPhotos();
  103. });