Created
March 18, 2013 21:07
-
-
Save biilmann/5190820 to your computer and use it in GitHub Desktop.
Random shuffle for image galleries
This file contains 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
<!-- Content with image gallery field called "image_gallery" --> | |
<pop:content> | |
<pop:random:gallery field="image_gallery"> | |
<pop:value resize="fill" width="200"/> | |
<pop:random:gallery> | |
</pop:content> |
This file contains 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
var shuffle = function(images) { | |
var a = [images[0]]; | |
var i = 1; | |
while (images[i]) { | |
var j = Math.floor(Math.random() * (i + 1)); | |
a[i] = a[j]; | |
a[j] = images[i]; | |
i++; | |
} | |
return a; | |
}; | |
exports.gallery = function(options, _, scope) { | |
var gallery = scope[options.field].images; | |
return gallery.length ? shuffle(gallery) : []; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment