Created
November 7, 2013 01:50
-
-
Save bpholt/7347593 to your computer and use it in GitHub Desktop.
Gallery2 Album to HiDPI <img>s
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function (albumID, $) { | |
| 'use strict'; | |
| $.extend({ | |
| interpolate: function (s, data) { | |
| var variable; | |
| for (variable in data) { | |
| if (data.hasOwnProperty(variable)) { | |
| s = s.replace(new RegExp('{' + variable + '}', 'g'), data[variable]); | |
| } | |
| } | |
| return s; | |
| } | |
| }); | |
| function handleGallery2Response(data) { | |
| var lines = data.split('\n'), | |
| pattern = /^image\.(.+)\.(\d+)$/, | |
| template = '<a href="/gallery2/d/{name}-2/{title}" title="{description}" rel="lightbox[g2image]"><img src="/gallery2/d/{resized.2.name}-2/{title}" srcset="/gallery2/d/{resized.2.name}-2/{title} 1x, /gallery2/d/{resized.1.name}-2/{title} 2x" alt="{caption}" width="{resized.2.width}" height="{resized.2.height}"/></a>', | |
| images = []; | |
| $.each(lines, function () { | |
| var keyVal = this.split('='), | |
| parsedKey, | |
| fieldName, | |
| imageIndex, | |
| image; | |
| if (pattern.test(keyVal[0])) { | |
| parsedKey = pattern.exec(keyVal[0]); | |
| fieldName = parsedKey[1]; | |
| imageIndex = parsedKey[2] - 1; | |
| image = images[imageIndex] || {}; | |
| image[fieldName] = keyVal[1]; | |
| images[imageIndex] = image; | |
| } | |
| }); | |
| $.each(images, function (i) { | |
| if (this['extrafield.Description']) { | |
| this.description = this['extrafield.Description']; | |
| } else { | |
| this.description = this.caption; | |
| } | |
| if (!this['resized.1.name']) { | |
| console.warn($.interpolate('{title} with ID {name} is missing HiDPI resize', this)); | |
| } | |
| if (!this['resized.2.name']) { | |
| console.warn($.interpolate('{title} with ID {name} is missing default resize', this)); | |
| } | |
| images[i] = this; | |
| }); | |
| $.each(images, function () { | |
| console.log($.interpolate(template, this)); | |
| }); | |
| } | |
| $.post('/gallery2/main.php?g2_controller=remote:GalleryRemote', { | |
| 'g2_form[cmd]': 'fetch-album-images', | |
| 'g2_form[protocol_version]': '2.4', | |
| 'g2_form[set_albumName]': albumID, | |
| 'g2_form[albums_too]': 'no', | |
| 'g2_form[random]': 'no', | |
| 'g2_form[extrafields]': 'yes', | |
| 'g2_form[all_sizes]': 'yes' | |
| }, handleGallery2Response); | |
| }(6985, jQuery)); | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gallery2 has a terrible HTTP API. /sigh