| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- function xdr(url, method, data, callback, errback) {
- var req;
- if (!callback)
- callback = function(){};
- if (!errback)
- errback = function(){};
- if(XMLHttpRequest && ('withCredentials' in (req = new XMLHttpRequest()))) {
- req.open(method, url, true);
- req.withCredentials = "true";
- req.onerror = errback;
- req.onreadystatechange = function() {
- if (req.readyState === 4) {
- if (req.status >= 200 && req.status < 400) {
- callback(req.responseText);
- } else {
- errback(new Error('Response returned with non-OK status'), req);
- }
- }
- };
- req.setRequestHeader("Accept", "application/json");
- if (data)
- req.setRequestHeader("Content-type", "application/json");
- req.send(data);
- } else if(XDomainRequest) {
- var reqUrl = url;
- if (url.indexOf('http:') === 0) {
- reqUrl = url.substr(5);
- }
- else if (url.indexOf('https:') === 0) {
- reqUrl = url.substr(6);
- }
- req = new XDomainRequest();
- req.open(method, reqUrl);
- req.onerror = function(err) { errback(err, req); };
- req.onload = function() {
- callback(req.responseText);
- };
- req.ontimeout = function() {};
- req.onprogress = function() {};
- req.timeout = 0;
- req.send(data);
- } else {
- errback(new Error('CORS not supported'));
- }
- };
- function jsonGET(url, callback, errback){
- xdr(url, 'GET', null, function(data){
- callback(JSON.parse(data));
- }, errback);
- }
- function jsonPOST(url, data, callback, errback){
- xdr(url, 'POST', data, function(data){
- callback(JSON.parse(data));
- }, errback);
- }
- function startGallery(items, index) {
- var pswpElement = document.querySelectorAll('.pswp')[0];
- var options = {
- index: index || 0
- };
- // Initializes and opens PhotoSwipe
- var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
- gallery.init();
- return gallery;
- }
|