app.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. ignore = !photo.ignore;
  47. ignoreTag.prop('disabled', true)
  48. $.post('/photos', {ignore: ignore, photo: photo.id}, function() {
  49. photo.ignore = ignore;
  50. if (ignore) {
  51. photoTag.addClass('ignored');
  52. ignoreTag.removeClass('-bordered').text('ignored');
  53. } else {
  54. photoTag.removeClass('ignored');
  55. ignoreTag.addClass('-bordered').text('ignore');
  56. }
  57. ignoreTag.prop('disabled', false)
  58. }).fail(function() {
  59. ignoreTag.prop('disabled', false)
  60. });
  61. });
  62. if (photo.ignore) {
  63. photoTag.addClass('ignored');
  64. ignoreTag.text('ignored');
  65. } else {
  66. ignoreTag.addClass('-bordered').text('ignore');
  67. }
  68. let license = licenses[photo.license];
  69. photoTag.append($('<div class="card-box">').append([
  70. $('<a>', {href: photo.url, target: '_blank'}).append($('<img>', {title: photo.title, src: photo.img})),
  71. $('<div class="card-content">').append([
  72. $('<span>', {class: 'tag-box', title: license.name}).html(license.icon),
  73. '<br>',
  74. privacyTag,
  75. '<br>',
  76. ignoreTag,
  77. ]),
  78. ]));
  79. photosTag.append(photoTag);
  80. }
  81. if (data.path) {
  82. reloadPhotos({}, data.path);
  83. } else {
  84. showLicenseTag.change();
  85. showPrivacyTag.change();
  86. showIgnoredTag.change();
  87. controlsDisabled(false);
  88. }
  89. }).fail(function() {
  90. controlsDisabled(false);
  91. });
  92. }
  93. function showLicense() {
  94. if (showLicenseVal == showLicenseTag.val()) {
  95. console.log(showLicenseVal);
  96. } else {
  97. $.post('/user', {show_license: showLicenseTag.val()}, function() {
  98. showLicenseVal = showLicenseTag.val();
  99. showLicense();
  100. }).fail(function() {
  101. showLicenseTag.val(showLicenseVal);
  102. });
  103. }
  104. }
  105. function showPrivacy() {
  106. if (showPrivacyVal == showPrivacyTag.val()) {
  107. console.log(showPrivacyVal);
  108. } else {
  109. $.post('/user', {show_privacy: showPrivacyTag.val()}, function() {
  110. showPrivacyVal = showPrivacyTag.val();
  111. showPrivacy();
  112. }).fail(function() {
  113. showPrivacyTag.val(showPrivacyVal);
  114. });
  115. }
  116. }
  117. function showIgnored() {
  118. if (showIgnoredVal == showIgnoredTag.val()) {
  119. console.log(showIgnoredVal);
  120. } else {
  121. $.post('/user', {show_ignored: showIgnoredTag.val()}, function() {
  122. showIgnoredVal = showIgnoredTag.val();
  123. showIgnored();
  124. }).fail(function() {
  125. showIgnoredTag.val(showIgnoredVal);
  126. });
  127. }
  128. }
  129. errorTag.dialog({autoOpen: false, modal: true});
  130. $(document).ajaxError(function(event, request, settings, error) {
  131. if (request.responseJSON && request.responseJSON.error) {
  132. errorTag.text(request.responseJSON.error);
  133. } else {
  134. errorTag.empty().append($('<div>').text(request.status + ' ' + error), $('<iframe>', {srcdoc: request.responseText}));
  135. }
  136. errorTag.dialog('open');
  137. });
  138. reloadPhotosTag.click(function() {
  139. photosTag.empty().append('<div class="spinner">');
  140. selectLicenseTag.val('').change();
  141. reloadPhotos({reload: true});
  142. });
  143. showLicenseTag.change(showLicense);
  144. showPrivacyTag.change(showPrivacy);
  145. showIgnoredTag.change(showIgnored);
  146. selectLicenseTag.change(function() {
  147. applyLicenseTag.prop('disabled', selectLicenseTag.val() == '');
  148. var license = licenses[selectLicenseTag.val()];
  149. if (license) {
  150. licenseLinkTag.html(license.url ? $('<a>', {href: license.url, target: '_blank'}).html(license.iconname) : license.iconname);
  151. } else {
  152. licenseLinkTag.empty();
  153. }
  154. });
  155. applyLicenseTag.click(function() {
  156. console.log(selectLicenseTag.val());
  157. });
  158. reloadPhotos();
  159. });