app.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. for (let photo of data.photos) {
  26. let privacy;
  27. let privacyTag = $('<span class="tag-box">');
  28. if (photo.public) {
  29. privacy = 'public';
  30. privacyTag.text('public');
  31. } else if (photo.friend && photo.family) {
  32. privacy = 'friends_family';
  33. privacyTag.text('friends&family');
  34. } else if (photo.friend) {
  35. privacy = 'friends';
  36. privacyTag.text('friends');
  37. } else if (photo.family) {
  38. privacy = 'family';
  39. privacyTag.text('family');
  40. } else {
  41. privacy = 'private';
  42. privacyTag.text('private');
  43. }
  44. let photoTag = $('<div column>').addClass('license-' + photo.license).addClass(privacy);
  45. let ignoreTag = $('<button>').click(function() {
  46. console.log(photo);
  47. });
  48. if (photo.ignore) {
  49. photoTag.addClass('ignored');
  50. ignoreTag.text('ignored');
  51. } else {
  52. ignoreTag.addClass('-bordered').text('ignore');
  53. }
  54. let license = licenses[photo.license];
  55. photoTag.append($('<div class="card-box">').append([
  56. $('<a>', {href: photo.url, target: '_blank'}).append($('<img>', {title: photo.title, src: photo.img})),
  57. $('<div class="card-content">').append([
  58. $('<span>', {class: 'tag-box', title: license.name}).html(license.icon),
  59. '<br>',
  60. privacyTag,
  61. '<br>',
  62. ignoreTag,
  63. ]),
  64. ]));
  65. photosTag.append(photoTag);
  66. }
  67. if (data.path) {
  68. reloadPhotos({}, data.path);
  69. } else {
  70. showLicenseTag.change();
  71. showPrivacyTag.change();
  72. showIgnoredTag.change();
  73. controlsDisabled(false);
  74. }
  75. }).fail(function() {
  76. controlsDisabled(false);
  77. });
  78. }
  79. function showLicense() {
  80. if (showLicenseVal == showLicenseTag.val()) {
  81. console.log(showLicenseVal);
  82. } else {
  83. $.post('/user', {show_license: showLicenseTag.val()}, function() {
  84. showLicenseVal = showLicenseTag.val();
  85. showLicense();
  86. }).fail(function() {
  87. showLicenseTag.val(showLicenseVal);
  88. });
  89. }
  90. }
  91. function showPrivacy() {
  92. if (showPrivacyVal == showPrivacyTag.val()) {
  93. console.log(showPrivacyVal);
  94. } else {
  95. $.post('/user', {show_privacy: showPrivacyTag.val()}, function() {
  96. showPrivacyVal = showPrivacyTag.val();
  97. showPrivacy();
  98. }).fail(function() {
  99. showPrivacyTag.val(showPrivacyVal);
  100. });
  101. }
  102. }
  103. function showIgnored() {
  104. if (showIgnoredVal == showIgnoredTag.val()) {
  105. console.log(showIgnoredVal);
  106. } else {
  107. $.post('/user', {show_ignored: showIgnoredTag.val()}, function() {
  108. showIgnoredVal = showIgnoredTag.val();
  109. showIgnored();
  110. }).fail(function() {
  111. showIgnoredTag.val(showIgnoredVal);
  112. });
  113. }
  114. }
  115. errorTag.dialog({autoOpen: false, modal: true});
  116. $(document).ajaxError(function(event, request, settings, error) {
  117. if (request.responseJSON && request.responseJSON.error) {
  118. errorTag.text(request.responseJSON.error);
  119. } else {
  120. errorTag.empty().append($('<div>').text(request.status + ' ' + error), $('<iframe>', {style: 'height: 100%; width: 100%;', srcdoc: request.responseText}));
  121. }
  122. errorTag.dialog('open');
  123. });
  124. reloadPhotosTag.click(function() {
  125. photosTag.empty().append('<div class="spinner">');
  126. selectLicenseTag.val('').change();
  127. reloadPhotos({reload: true});
  128. });
  129. showLicenseTag.change(showLicense);
  130. showPrivacyTag.change(showPrivacy);
  131. showIgnoredTag.change(showIgnored);
  132. selectLicenseTag.change(function() {
  133. applyLicenseTag.prop('disabled', selectLicenseTag.val() == '');
  134. var license = licenses[selectLicenseTag.val()];
  135. if (license) {
  136. licenseLinkTag.html(license.url ? $('<a>', {href: license.url, target: '_blank'}).html(license.iconname) : license.iconname);
  137. } else {
  138. licenseLinkTag.empty();
  139. }
  140. });
  141. applyLicenseTag.click(function() {
  142. console.log(selectLicenseTag.val());
  143. });
  144. reloadPhotos();
  145. });