| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- function setAlbums(albums) {
- var dAlbums = document.getElementById('albums');
- dAlbums.innerHTML = '';
- Array.prototype.forEach.call(albums, function(alb, i){
- var a = appendElement(dAlbums, 'a');
- var img = appendElement(a, 'img');
- img.src = alb.cover.src;
- if (alb.title) {
- var fig = appendElement(a, 'figure');
- fig.textContent = alb.title;
- };
- a.addEventListener('click', function() {
- startGallery(alb.photos);
- });
- });
- }
- 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;
- }
- function appendElement(parent, tag, id, className, innerHTML) {
- var el = document.createElement(tag);
- if (id) el.id = id;
- if (className) el.className = className;
- if (innerHTML) el.innerHTML = innerHTML;
- parent.appendChild(el);
- return el;
- }
- 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);
- }
|