/** * On-demand JavaScript handler * * Based on http://plugins.jquery.com/files/issues/jquery.ondemand.js_.txt * and heavily modified to integrate with SilverStripe and prototype.js. * Adds capabilities for custom X-Include-CSS and X-Include-JS HTTP headers * to request loading of externals alongside an ajax response. * * Requires jQuery 1.5 ($.Deferred support) * * CAUTION: Relies on customization of the 'beforeSend' callback in jQuery.ajaxSetup() * * @author Ingo Schommer (ingo at silverstripe dot com) * @author Sam Minnee (sam at silverstripe dot com) */ (function($){ $.extend({ // loaded files list - to protect against loading existed file again (by PGA) _ondemand_loaded_list : null, // Added by SRM: Initialise the loaded_list with the scripts included on first load initialiseItemLoadedList : function() { if(this.loaded_list == null) { $this = this; $this.loaded_list = {}; $('script').each(function() { if($(this).attr('src')) $this.loaded_list[ $(this).attr('src') ] = 1; }); $('link[rel="stylesheet"]').each(function() { if($(this).attr('href')) $this.loaded_list[ $(this).attr('href') ] = 1; }); } }, /** * Returns true if the given CSS or JS script has already been loaded */ isItemLoaded : function(scriptUrl) { var self = this; if(this._ondemand_loaded_list == null) { this._ondemand_loaded_list = {}; $('script').each(function() { if($(this).attr('src')) self._ondemand_loaded_list[ $(this).attr('src') ] = 1; }); $('link[rel="stylesheet"]').each(function() { if($(this).attr('href')) self._ondemand_loaded_list[ $(this).attr('href') ] = 1; }); } return (this._ondemand_loaded_list[scriptUrl] != undefined); }, requireCss : function(styleUrl, media){ if(media == null) media = 'all'; // Don't double up on loading scripts if($.isItemLoaded(styleUrl)) return; if(document.createStyleSheet){ var ss = document.createStyleSheet(styleUrl); ss.media = media; } else { var styleTag = document.createElement('link'); $(styleTag).attr({ href : styleUrl, type : 'text/css', media : media, rel : 'stylesheet' }).appendTo($('head').get(0)); } this._ondemand_loaded_list[styleUrl] = 1; }, /** * Process the X-Include-CSS and X-Include-JS headers provided by the Requirements class */ processOnDemandHeaders: function(xml, status, xhr) { var self = this, processDfd = new $.Deferred(); // CSS if(xhr.getResponseHeader && xhr.getResponseHeader('X-Include-CSS')) { var cssIncludes = xhr.getResponseHeader('X-Include-CSS').split(','); for(var i=0;i