site.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. function xdr(url, method, data, callback, errback) {
  2. var req;
  3. if (!callback)
  4. callback = function(){};
  5. if (!errback)
  6. errback = function(){};
  7. if(XMLHttpRequest && ('withCredentials' in (req = new XMLHttpRequest()))) {
  8. req.open(method, url, true);
  9. req.withCredentials = "true";
  10. req.onerror = errback;
  11. req.onreadystatechange = function() {
  12. if (req.readyState === 4) {
  13. if (req.status >= 200 && req.status < 400) {
  14. callback(req.responseText);
  15. } else {
  16. errback(new Error('Response returned with non-OK status'), req);
  17. }
  18. }
  19. };
  20. req.setRequestHeader("Accept", "application/json");
  21. if (data)
  22. req.setRequestHeader("Content-type", "application/json");
  23. req.send(data);
  24. } else if(XDomainRequest) {
  25. var reqUrl = url;
  26. if (url.indexOf('http:') === 0) {
  27. reqUrl = url.substr(5);
  28. }
  29. else if (url.indexOf('https:') === 0) {
  30. reqUrl = url.substr(6);
  31. }
  32. req = new XDomainRequest();
  33. req.open(method, reqUrl);
  34. req.onerror = function(err) { errback(err, req); };
  35. req.onload = function() {
  36. callback(req.responseText);
  37. };
  38. req.ontimeout = function() {};
  39. req.onprogress = function() {};
  40. req.timeout = 0;
  41. req.send(data);
  42. } else {
  43. errback(new Error('CORS not supported'));
  44. }
  45. };
  46. function jsonGET(url, callback, errback){
  47. xdr(url, 'GET', null, function(data){
  48. callback(JSON.parse(data));
  49. }, errback);
  50. }
  51. function jsonPOST(url, data, callback, errback){
  52. xdr(url, 'POST', data, function(data){
  53. callback(JSON.parse(data));
  54. }, errback);
  55. }
  56. function startGallery(items, index) {
  57. var pswpElement = document.querySelectorAll('.pswp')[0];
  58. var options = {
  59. index: index || 0
  60. };
  61. // Initializes and opens PhotoSwipe
  62. var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
  63. gallery.init();
  64. return gallery;
  65. }