site.js 4.8 KB

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