site.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. function setAlbums(albums) {
  2. var dAlbums = document.getElementById('albums');
  3. var lastYear = null;
  4. dAlbums.innerHTML = '';
  5. Array.prototype.forEach.call(albums, function(alb, i){
  6. var date = new Date(alb.date*1000),
  7. yyyy = date.getFullYear();
  8. if (yyyy != lastYear) {
  9. appendElement(dAlbums, 'h3', null, 'albums-year', yyyy);
  10. lastYear = yyyy;
  11. };
  12. var a = addAlbumDom(dAlbums, alb, true);
  13. a.setAttribute('data-id', alb.id);
  14. a.setAttribute('title', alb.cover);
  15. a.addEventListener('click', function(e) {
  16. jsonGET('/api/albums/' + alb.id + '/' + (e.ctrlKey ? '?full=1' : ''), function (alb) {
  17. var pswp = startGallery(alb.photos, 0, alb.id);
  18. pswp.listen('setCoverClick', function(item) {
  19. jsonPOST('/api/albums/' + alb.id + '/cover/', JSON.stringify({id: item.id}));
  20. });
  21. pswp.listen('hideClick', function(item) {
  22. var data = JSON.stringify({id: item.id, hidden:item.hidden ? null : 1});
  23. jsonPOST('/api/albums/' + alb.id + '/hide/', data);
  24. });
  25. });
  26. });
  27. });
  28. }
  29. function setIncoming(albums, parent) {
  30. parent.innerHTML = '';
  31. Array.prototype.forEach.call(albums, function(alb, i){
  32. var div = appendElement(parent, 'div', null, 'incoming-album');
  33. var a = addAlbumDom(div, alb, true);
  34. a.addEventListener('click', function(e) {
  35. e.preventDefault();
  36. startGallery(alb.photos, 0, i);
  37. });
  38. var form = appendElement(div, 'form', null, null, '<div><p><label>Title</label><input type="text"/></p><p><label>Path</label><input type="text"/></p><p><input type="submit" value="Import"/></p>');
  39. form[0].value = alb.title;
  40. form[1].value = alb.dest;
  41. form.addEventListener('submit', function (e) {
  42. e.preventDefault();
  43. form[2].disabled = true;
  44. var paths = Array.prototype.map.call(alb.photos, function(p) {return p.path});
  45. var data = JSON.stringify({name: form[0].value,
  46. dest: form[1].value,
  47. paths: paths});
  48. jsonPOST('/api/incoming/import/', data, function(resp) {
  49. alert('Added album ' + resp.id);
  50. div.innerHTML = '';
  51. }, function(err, req) {
  52. alert(err);
  53. form[2].disabled = false;
  54. });
  55. });
  56. });
  57. }
  58. function addAlbumDom(parent, alb, add_count) {
  59. var a = appendElement(parent, 'a');
  60. var img = appendElement(a, 'img');
  61. img.src = alb.cover;
  62. if (alb.title) {
  63. var fig = appendElement(a, 'figure');
  64. fig.textContent = alb.title;
  65. if (add_count)
  66. fig.textContent += ' [' + (alb.count || alb.photos.length) + ']';
  67. };
  68. return a;
  69. }
  70. function startGallery(items, index, uid) {
  71. var pswpElement = document.querySelectorAll('.pswp')[0];
  72. var options = {
  73. preload: [1, 3],
  74. loadingIndicatorDelay: 300,
  75. index: index || 0,
  76. galleryUID: uid || 1
  77. };
  78. // Initializes and opens PhotoSwipe
  79. var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
  80. gallery.init();
  81. return gallery;
  82. }
  83. function appendElement(parent, tag, id, className, innerHTML) {
  84. var el = document.createElement(tag);
  85. if (id) el.id = id;
  86. if (className) el.className = className;
  87. if (innerHTML) el.innerHTML = innerHTML;
  88. parent.appendChild(el);
  89. return el;
  90. }
  91. function xdr(url, method, data, callback, errback) {
  92. var req;
  93. if (!callback)
  94. callback = function(){};
  95. if (!errback)
  96. errback = function(){};
  97. if(XMLHttpRequest && ('withCredentials' in (req = new XMLHttpRequest()))) {
  98. req.open(method, url, true);
  99. req.withCredentials = "true";
  100. req.onerror = errback;
  101. req.onreadystatechange = function() {
  102. if (req.readyState === 4) {
  103. if (req.status >= 200 && req.status < 400) {
  104. callback(req.responseText);
  105. } else {
  106. errback(new Error('Response returned with non-OK status'), req);
  107. }
  108. }
  109. };
  110. req.setRequestHeader("Accept", "application/json");
  111. if (data)
  112. req.setRequestHeader("Content-type", "application/json");
  113. req.send(data);
  114. } else if(XDomainRequest) {
  115. var reqUrl = url;
  116. if (url.indexOf('http:') === 0) {
  117. reqUrl = url.substr(5);
  118. }
  119. else if (url.indexOf('https:') === 0) {
  120. reqUrl = url.substr(6);
  121. }
  122. req = new XDomainRequest();
  123. req.open(method, reqUrl);
  124. req.onerror = function(err) { errback(err, req); };
  125. req.onload = function() {
  126. callback(req.responseText);
  127. };
  128. req.ontimeout = function() {};
  129. req.onprogress = function() {};
  130. req.timeout = 0;
  131. req.send(data);
  132. } else {
  133. errback(new Error('CORS not supported'));
  134. }
  135. };
  136. function jsonGET(url, callback, errback){
  137. xdr(url, 'GET', null, function(data){
  138. callback(JSON.parse(data));
  139. }, errback);
  140. }
  141. function jsonPOST(url, data, callback, errback){
  142. xdr(url, 'POST', data, function(data){
  143. callback(JSON.parse(data));
  144. }, errback);
  145. }