site.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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);
  6. a.addEventListener('click', function() {
  7. jsonGET('/api/albums/' + alb.id + '/', function (alb) {
  8. startGallery(alb.photos);
  9. });
  10. });
  11. });
  12. }
  13. function setIncoming(albums) {
  14. var dAlbums = document.getElementById('albums');
  15. dAlbums.innerHTML = '';
  16. Array.prototype.forEach.call(albums, function(alb, i){
  17. var a = addAlbumDom(dAlbums, alb);
  18. a.addEventListener('click', function() {
  19. startGallery(alb.photos);
  20. });
  21. });
  22. }
  23. function startGallery(items, index) {
  24. var pswpElement = document.querySelectorAll('.pswp')[0];
  25. var options = {
  26. index: index || 0
  27. };
  28. // Initializes and opens PhotoSwipe
  29. var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
  30. gallery.init();
  31. return gallery;
  32. }
  33. function addAlbumDom(parent, alb) {
  34. var a = appendElement(parent, 'a');
  35. var img = appendElement(a, 'img');
  36. img.src = alb.cover.src;
  37. if (alb.title) {
  38. var fig = appendElement(a, 'figure');
  39. fig.textContent = alb.title;
  40. };
  41. return a;
  42. }
  43. function appendElement(parent, tag, id, className, innerHTML) {
  44. var el = document.createElement(tag);
  45. if (id) el.id = id;
  46. if (className) el.className = className;
  47. if (innerHTML) el.innerHTML = innerHTML;
  48. parent.appendChild(el);
  49. return el;
  50. }
  51. function xdr(url, method, data, callback, errback) {
  52. var req;
  53. if (!callback)
  54. callback = function(){};
  55. if (!errback)
  56. errback = function(){};
  57. if(XMLHttpRequest && ('withCredentials' in (req = new XMLHttpRequest()))) {
  58. req.open(method, url, true);
  59. req.withCredentials = "true";
  60. req.onerror = errback;
  61. req.onreadystatechange = function() {
  62. if (req.readyState === 4) {
  63. if (req.status >= 200 && req.status < 400) {
  64. callback(req.responseText);
  65. } else {
  66. errback(new Error('Response returned with non-OK status'), req);
  67. }
  68. }
  69. };
  70. req.setRequestHeader("Accept", "application/json");
  71. if (data)
  72. req.setRequestHeader("Content-type", "application/json");
  73. req.send(data);
  74. } else if(XDomainRequest) {
  75. var reqUrl = url;
  76. if (url.indexOf('http:') === 0) {
  77. reqUrl = url.substr(5);
  78. }
  79. else if (url.indexOf('https:') === 0) {
  80. reqUrl = url.substr(6);
  81. }
  82. req = new XDomainRequest();
  83. req.open(method, reqUrl);
  84. req.onerror = function(err) { errback(err, req); };
  85. req.onload = function() {
  86. callback(req.responseText);
  87. };
  88. req.ontimeout = function() {};
  89. req.onprogress = function() {};
  90. req.timeout = 0;
  91. req.send(data);
  92. } else {
  93. errback(new Error('CORS not supported'));
  94. }
  95. };
  96. function jsonGET(url, callback, errback){
  97. xdr(url, 'GET', null, function(data){
  98. callback(JSON.parse(data));
  99. }, errback);
  100. }
  101. function jsonPOST(url, data, callback, errback){
  102. xdr(url, 'POST', data, function(data){
  103. callback(JSON.parse(data));
  104. }, errback);
  105. }