site.js 4.5 KB

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