site.js 2.8 KB

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