app.js 8.3 KB

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