app.js 9.1 KB

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