File indexing completed on 2024-05-12 05:50:06

0001 /*
0002 Slideshow script for ubiquity-slideshow, global to all variations.
0003 
0004 * Interprets parameters passed via location.hash (in #?param1=key?param2 format)
0005 * Creates an animated slideshow inside the #slideshow element.
0006 * Automatically loads a requested locale, based on the default slides.
0007 * Manages slideshow controls, if requested via parameters.
0008 
0009 Assumptions are made about the design of the html document this is inside of.
0010 Please see slides/ubuntu/index.html for an example of this script in use.
0011 
0012 Please include this script last, after any other scripts.
0013 
0014 
0015 Dependencies (please load these first):
0016 link-core/jquery.js
0017 link-core/jquery.cycle.all.js
0018 link-core/signals.js
0019 */
0020 
0021 /* Pass parameters by creating a global SLIDESHOW_OPTIONS object, containing
0022    any options described at <http://jquery.malsup.com/cycle/options.html>
0023    
0024    The default callback for cycle.next also checks an extra autopause parameter,
0025    which will pause the slideshow when it reaches the end (but doesn't stop it)
0026    
0027    Signals: slideshow-loaded
0028             slideshow-started
0029             slide-opening
0030             slide-opened
0031             slide-closing
0032             slide-closed
0033 */
0034 
0035 var slideshow;
0036 
0037 var directory = {};
0038 var extra_directory = {};
0039 
0040 $(document).ready(function() {
0041         function loadExtraSlides() {
0042                 $.ajax({
0043                         type: 'GET',
0044                         url: 'extra/directory.jsonp',
0045                         dataType: 'jsonp',
0046                         jsonpCallback: 'ubiquitySlideshowDirectoryCb',
0047                         success: function(data, status, xhr) {
0048                                 extra_directory = $.extend(extra_directory, data);
0049                         },
0050                         complete: function(xhr, status) {
0051                                 slideshowReady();
0052                         }
0053                 });
0054         }
0055         
0056         $.ajax({
0057                 type: 'GET',
0058                 url: 'directory.jsonp',
0059                 dataType: 'jsonp',
0060                 jsonpCallback: 'ubiquitySlideshowDirectoryCb',
0061                 success: function(data, status, xhr) {
0062                         directory = $.extend(directory, data);
0063                 },
0064                 complete: function(xhr, status) {
0065                         loadExtraSlides();
0066                 }
0067         });
0068 });
0069 
0070 function slideshowReady() {
0071         slideshow = $('#slideshow');
0072         
0073         var slideshow_options = {
0074                 fx:'scrollHorz',
0075                 timeout:45000,
0076                 speed:500,
0077                 nowrap:false,
0078                 autopause:false,
0079                 manualTrump:false,
0080         };
0081         
0082         $.extend(slideshow_options, window.SLIDESHOW_OPTIONS);
0083         
0084         if ( 'rtl' in INSTANCE_OPTIONS )
0085                 $(document.body).addClass('rtl');
0086         
0087         var locale = INSTANCE_OPTIONS['locale'] || 'C';
0088         loadSlides(locale);
0089         
0090         Signals.fire('slideshow-loaded');
0091         
0092         slideshow_options.before = function(curr, next, opts) {
0093                 if ($(next).data('last')) {
0094                         $('#next-slide').addClass('disabled').fadeOut(slideshow_options.speed);
0095                 } else {
0096                         $('#next-slide').removeClass('disabled').fadeIn(slideshow_options.speed);
0097                 }
0098                 
0099                 if ($(next).data('first')) {
0100                         $('#prev-slide').addClass('disabled').fadeOut(slideshow_options.speed);
0101                 } else {
0102                         $('#prev-slide').removeClass('disabled').fadeIn(slideshow_options.speed);
0103                 }
0104                 
0105                 Signals.fire('slide-closing', $(curr));
0106                 Signals.fire('slide-opening', $(next));
0107         }
0108         
0109         slideshow_options.after = function(curr, next, opts) {
0110                 var index = opts.currSlide;
0111                 /* pause at last slide if requested in options */
0112                 if ( index == opts.slideCount - 1 && opts.autopause ) {
0113                         slideshow.cycle('pause'); /* slides can still be advanced manually */
0114                 }
0115                 
0116                 Signals.fire('slide-closed', $(curr));
0117                 Signals.fire('slide-opened', $(next));
0118         }
0119         
0120         var controls = $('#controls');
0121         if ( 'controls' in INSTANCE_OPTIONS ) {
0122                 var debug_controls = $('#debug-controls');
0123                 if (debug_controls.length > 0) {
0124                         debug_controls.show();
0125                         controls = debug_controls;
0126                 }
0127         }
0128         slideshow_options.prev = controls.children('#prev-slide');
0129         slideshow_options.next = controls.children('#next-slide');
0130         
0131         if ( 'slideNumber' in INSTANCE_OPTIONS )
0132                 slideshow_options.startingSlide = INSTANCE_OPTIONS['slideNumber'];
0133         
0134         if (slideshow.children().length > 1) {
0135                 slideshow.cycle(slideshow_options)
0136                 Signals.fire('slideshow-started');
0137         } else {
0138                 $('#prev-slide').addClass('disabled').hide();
0139                 $('#next-slide').addClass('disabled').hide();
0140         }
0141 };
0142 
0143 function getTranslatedFile(locale, file_name, file_category) {
0144         var territory = locale.split(".",1)[0];
0145         var language = territory.split("_",1)[0];
0146         
0147         return tryFileLocale(locale, file_name, file_category) ||
0148                tryFileLocale(territory, file_name, file_category) ||
0149                tryFileLocale(language, file_name, file_category) ||
0150                tryFileLocale('C', file_name, file_category);
0151         
0152         function tryFileLocale(locale, file_name, file_category) {
0153                 if (translationFileExists(extra_directory, locale, file_name, file_category)) {
0154                         // extra_directory can override slides from any locale, including C
0155                         return './extra/'+locale+'/'+file_name;
0156                 } else if (locale == 'C') {
0157                         return './'+file_name;
0158                 } else if (translationFileExists(directory, locale, file_name, file_category)) {
0159                         return './l10n/'+locale+'/'+file_name;
0160                 } else {
0161                         return undefined;
0162                 }
0163         }
0164         
0165         function translationFileExists(directory, locale, file_name, file_category) {
0166                 return locale in directory &&
0167                        file_category in directory[locale] &&
0168                        directory[locale][file_category].indexOf(file_name) >= 0;
0169         }
0170 }
0171 
0172 function loadSlides(locale) {
0173         var selected_slidesets = []
0174         if ( 'slidesets' in INSTANCE_OPTIONS )
0175                 selected_slidesets = INSTANCE_OPTIONS['slidesets'].split(' ');
0176         
0177         function slideIsVisible(slide) {
0178                 var slidesets = $(slide).data('slidesets');
0179                 var is_visible = true;
0180                 if ( slidesets !== undefined ) {
0181                         is_visible = false;
0182                         $.each(slidesets.split(' '), function(index, slideset) {
0183                                 if ($.inArray(slideset, selected_slidesets) >= 0) {
0184                                         is_visible = true;
0185                                 }
0186                         });
0187                 }
0188                 return is_visible;
0189         }
0190         
0191         function translateSlideContents(locale, contents) {
0192                 // Warning: this assumes all images are local.
0193                 // TODO: Only translate images selected by $(img.translatable)
0194                 var images = $('img', contents);
0195                 $.each(images, function(index, image) {
0196                         var image_name = $(image).attr('src');
0197                         var translated_image = getTranslatedFile(locale, image_name, 'media');
0198                         $(image).attr('src', translated_image);
0199                 });
0200         }
0201         
0202         slideshow.children('div').each(function(index, slide) {
0203                 if ( slideIsVisible($(slide)) ) {
0204                         var slide_name = $(slide).children('a').attr('href');
0205                         var translated_slide = getTranslatedFile(locale, slide_name, 'slides');
0206                         
0207                         $.ajax({
0208                                 type: 'GET',
0209                                 url: translated_slide,
0210                                 cache: false,
0211                                 async: false,
0212                                 success: function(data, status, xhr) {
0213                                         $(data).appendTo(slide);
0214                                         translateSlideContents(locale, slide);
0215                                 }
0216                         });
0217                 } else {
0218                         $(slide).detach();
0219                 }
0220         });
0221         
0222         slideshow.children('div').first().data('first', true);
0223         slideshow.children('div').last().data('last', true);
0224 }
0225