app.js 7.1 KB

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