app.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* flickrlicense -- A thingy to update Flickr photo licenses
  2. * Copyright (C) 2017 Douglas Thrift
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License as published
  6. * by the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Affero General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. $(function() {
  18. var errorTag = $('#error');
  19. var reloadPhotosTag = $('#reload_photos');
  20. var showLicenseTag = $('#show_license');
  21. var showLicenseVal = showLicenseTag.val();
  22. var showPrivacyTag = $('#show_privacy');
  23. var showPrivacyVal = showPrivacyTag.val();
  24. var showIgnoredTag = $('#show_ignored');
  25. var showIgnoredVal = showIgnoredTag.val();
  26. var photosTag = $('#photos');
  27. var selectLicenseTag = $('#select_license');
  28. var applyLicenseTag = $('#apply_license');
  29. var licenseLinkTag = $('#license_link');
  30. var selectedCountTag = $('#selected_count');
  31. var selectedNounTag = $('#selected_noun');
  32. function controlsDisabled(disabled) {
  33. reloadPhotosTag.prop('disabled', disabled);
  34. showLicenseTag.prop('disabled', disabled);
  35. showPrivacyTag.prop('disabled', disabled);
  36. showIgnoredTag.prop('disabled', disabled);
  37. selectLicenseTag.prop('disabled', disabled);
  38. photosTag.find('.photo button').prop('disabled', disabled);
  39. }
  40. function filterPhotos() {
  41. var count = 0;
  42. photosTag.children('.photo').each(function(index, element) {
  43. var photoTag = $(element);
  44. var show = true;
  45. var ignore = photoTag.hasClass('ignored');
  46. if (showLicenseVal != '' && !photoTag.hasClass('license-' + showLicenseVal)) {
  47. show = false;
  48. }
  49. if (showPrivacyVal != 'all' && !photoTag.hasClass(showPrivacyVal)) {
  50. show = false;
  51. }
  52. if (showIgnoredVal != 'true' && ignore) {
  53. show = false;
  54. }
  55. if (show) {
  56. if (!ignore) {
  57. count++;
  58. }
  59. photoTag.show();
  60. } else {
  61. photoTag.hide();
  62. }
  63. });
  64. selectedCountTag.text(count);
  65. selectedNounTag.text(count == 1 ? "photo" : "photos");
  66. }
  67. function reloadPhotos(params = {}, path = '/photos/1') {
  68. controlsDisabled(true);
  69. $.getJSON(path, params, function(data) {
  70. photosTag.children('.spinner').remove();
  71. for (let photo of data.photos) {
  72. let privacy;
  73. let privacyTag = $('<span class="tag-box">');
  74. if (photo.public) {
  75. privacy = 'public';
  76. privacyTag.text('public');
  77. } else if (photo.friend && photo.family) {
  78. privacy = 'friends_family';
  79. privacyTag.text('friends&family');
  80. } else if (photo.friend) {
  81. privacy = 'friends';
  82. privacyTag.text('friends');
  83. } else if (photo.family) {
  84. privacy = 'family';
  85. privacyTag.text('family');
  86. } else {
  87. privacy = 'private';
  88. privacyTag.text('private');
  89. }
  90. let photoTag = $('<div class="photo" column>').addClass('license-' + photo.license).addClass(privacy).data('photo', photo);
  91. let ignoreTag = $('<button>').click(function() {
  92. ignore = !photo.ignore;
  93. ignoreTag.prop('disabled', true)
  94. $.post('/photos', {ignore: ignore, photo: photo.id}, function() {
  95. photo.ignore = ignore;
  96. if (ignore) {
  97. photoTag.addClass('ignored');
  98. ignoreTag.removeClass('-bordered').text('ignored');
  99. } else {
  100. photoTag.removeClass('ignored');
  101. ignoreTag.addClass('-bordered').text('ignore');
  102. }
  103. filterPhotos();
  104. ignoreTag.prop('disabled', false)
  105. }).fail(function() {
  106. ignoreTag.prop('disabled', false)
  107. });
  108. });
  109. if (photo.ignore) {
  110. photoTag.addClass('ignored');
  111. ignoreTag.text('ignored');
  112. } else {
  113. ignoreTag.addClass('-bordered').text('ignore');
  114. }
  115. let license = licenses[photo.license];
  116. photoTag.append($('<div class="card-box">').append([
  117. $('<a>', {href: photo.url, target: '_blank'}).append($('<img>', {title: photo.title, src: photo.img})),
  118. $('<div class="card-content">').append([
  119. $('<span>', {class: 'license tag-box', title: license.name}).html(license.icon),
  120. '<br>',
  121. privacyTag,
  122. '<br>',
  123. ignoreTag,
  124. ]),
  125. ]));
  126. photosTag.append(photoTag);
  127. }
  128. if (data.path) {
  129. reloadPhotos({}, data.path);
  130. } else {
  131. filterPhotos();
  132. controlsDisabled(false);
  133. }
  134. }).fail(function() {
  135. controlsDisabled(false);
  136. });
  137. }
  138. function showLicense() {
  139. if (showLicenseVal == showLicenseTag.val()) {
  140. filterPhotos();
  141. } else {
  142. $.post('/user', {show_license: showLicenseTag.val()}, function() {
  143. showLicenseVal = showLicenseTag.val();
  144. showLicense();
  145. }).fail(function() {
  146. showLicenseTag.val(showLicenseVal);
  147. });
  148. }
  149. }
  150. function showPrivacy() {
  151. if (showPrivacyVal == showPrivacyTag.val()) {
  152. filterPhotos();
  153. } else {
  154. $.post('/user', {show_privacy: showPrivacyTag.val()}, function() {
  155. showPrivacyVal = showPrivacyTag.val();
  156. showPrivacy();
  157. }).fail(function() {
  158. showPrivacyTag.val(showPrivacyVal);
  159. });
  160. }
  161. }
  162. function showIgnored() {
  163. if (showIgnoredVal == showIgnoredTag.val()) {
  164. filterPhotos();
  165. } else {
  166. $.post('/user', {show_ignored: showIgnoredTag.val()}, function() {
  167. showIgnoredVal = showIgnoredTag.val();
  168. showIgnored();
  169. }).fail(function() {
  170. showIgnoredTag.val(showIgnoredVal);
  171. });
  172. }
  173. }
  174. function applyLicense(license, photos, index = 0) {
  175. if (index >= photos.length) {
  176. applyLicenseTag.prop('disabled', false);
  177. return controlsDisabled(false);
  178. }
  179. applyLicenseTag.prop('disabled', true);
  180. controlsDisabled(true);
  181. var photoTag = $(photos[index]);
  182. var photo = photoTag.data('photo');
  183. if (photo.ignore || photo.license == license) {
  184. applyLicense(license, photos, ++index);
  185. } else {
  186. $.post(photo.path, {license: license}, function() {
  187. photoTag.removeClass('license-' + photo.license).addClass('license-' + license);
  188. photo.license = license;
  189. {
  190. let license = licenses[photo.license];
  191. photoTag.find('.license').attr('title', license.name).html(license.icon);
  192. }
  193. filterPhotos();
  194. applyLicense(license, photos, ++index);
  195. }).fail(function() {
  196. applyLicenseTag.prop('disabled', false);
  197. controlsDisabled(false);
  198. });
  199. }
  200. }
  201. errorTag.dialog({autoOpen: false, modal: true});
  202. $(document).ajaxError(function(event, request, settings, error) {
  203. if (request.responseJSON && request.responseJSON.error) {
  204. errorTag.text(request.responseJSON.error);
  205. } else {
  206. errorTag.empty().append($('<div>').text(request.status + ' ' + error), $('<iframe>', {srcdoc: request.responseText}));
  207. }
  208. errorTag.dialog('open');
  209. });
  210. reloadPhotosTag.click(function() {
  211. photosTag.empty().append('<div class="spinner">');
  212. selectLicenseTag.val('').change();
  213. reloadPhotos({reload: true});
  214. });
  215. showLicenseTag.change(showLicense);
  216. showPrivacyTag.change(showPrivacy);
  217. showIgnoredTag.change(showIgnored);
  218. selectLicenseTag.change(function() {
  219. applyLicenseTag.prop('disabled', selectLicenseTag.val() == '');
  220. var license = licenses[selectLicenseTag.val()];
  221. if (license) {
  222. licenseLinkTag.html(license.url ? $('<a>', {href: license.url, target: '_blank'}).html(license.iconname) : license.iconname);
  223. } else {
  224. licenseLinkTag.empty();
  225. }
  226. });
  227. applyLicenseTag.click(function() {
  228. applyLicense(selectLicenseTag.val(), photosTag.children('.photo:visible'));
  229. });
  230. reloadPhotos();
  231. });