2009-06-18 01:14:40 +02:00
|
|
|
/**
|
|
|
|
* On-demand JavaScript handler
|
2011-03-01 05:39:12 +01:00
|
|
|
*
|
2009-11-21 03:30:51 +01:00
|
|
|
* Based on http://plugins.jquery.com/files/issues/jquery.ondemand.js_.txt
|
2009-11-21 03:31:36 +01:00
|
|
|
* and heavily modified to integrate with SilverStripe and prototype.js.
|
2009-11-21 03:30:51 +01:00
|
|
|
* Adds capabilities for custom X-Include-CSS and X-Include-JS HTTP headers
|
|
|
|
* to request loading of externals alongside an ajax response.
|
2009-11-21 03:22:57 +01:00
|
|
|
*
|
2011-03-01 05:39:12 +01:00
|
|
|
* 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)
|
2009-06-18 01:14:40 +02:00
|
|
|
*/
|
|
|
|
(function($){
|
|
|
|
|
2012-01-06 14:09:43 +01:00
|
|
|
var decodePath = function(str) {
|
2014-02-19 17:57:27 +01:00
|
|
|
return str.replace(/%2C/g,',').replace(/\&/g, '&').replace(/^\s+|\s+$/g, '');
|
2012-01-06 14:09:43 +01:00
|
|
|
};
|
|
|
|
|
2009-06-18 01:14:40 +02:00
|
|
|
$.extend({
|
|
|
|
|
2009-11-21 03:30:51 +01:00
|
|
|
// loaded files list - to protect against loading existed file again (by PGA)
|
2009-11-21 03:31:36 +01:00
|
|
|
_ondemand_loaded_list : null,
|
2012-12-08 12:20:20 +01:00
|
|
|
|
2009-11-21 03:30:44 +01:00
|
|
|
/**
|
|
|
|
* Returns true if the given CSS or JS script has already been loaded
|
|
|
|
*/
|
|
|
|
isItemLoaded : function(scriptUrl) {
|
2015-11-02 05:11:22 +01:00
|
|
|
var self = this, src;
|
2012-06-06 11:50:17 +02:00
|
|
|
if(this._ondemand_loaded_list === null) {
|
2009-11-21 03:31:36 +01:00
|
|
|
this._ondemand_loaded_list = {};
|
|
|
|
$('script').each(function() {
|
2012-06-06 11:50:17 +02:00
|
|
|
src = $(this).attr('src');
|
2015-11-02 05:11:22 +01:00
|
|
|
if(src) self._ondemand_loaded_list[src] = 1;
|
2009-11-21 03:31:36 +01:00
|
|
|
});
|
|
|
|
$('link[rel="stylesheet"]').each(function() {
|
2012-06-06 11:50:17 +02:00
|
|
|
src = $(this).attr('href');
|
2015-11-02 05:11:22 +01:00
|
|
|
if(src) self._ondemand_loaded_list[src] = 1;
|
2009-06-18 01:14:40 +02:00
|
|
|
});
|
|
|
|
}
|
2015-11-02 05:11:22 +01:00
|
|
|
return (this._ondemand_loaded_list[decodePath(scriptUrl)] !== undefined);
|
2009-06-18 01:14:40 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
requireCss : function(styleUrl, media){
|
2012-06-21 06:50:14 +02:00
|
|
|
if(!media) media = 'all';
|
2009-06-18 01:14:40 +02:00
|
|
|
|
2009-11-21 03:30:44 +01:00
|
|
|
// Don't double up on loading scripts
|
2009-11-21 03:31:36 +01:00
|
|
|
if($.isItemLoaded(styleUrl)) return;
|
2009-06-18 01:14:40 +02:00
|
|
|
|
|
|
|
if(document.createStyleSheet){
|
2009-11-21 03:31:36 +01:00
|
|
|
var ss = document.createStyleSheet(styleUrl);
|
2009-06-18 01:14:40 +02:00
|
|
|
ss.media = media;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
var styleTag = document.createElement('link');
|
|
|
|
$(styleTag).attr({
|
2009-11-21 03:31:36 +01:00
|
|
|
href : styleUrl,
|
2009-06-18 01:14:40 +02:00
|
|
|
type : 'text/css',
|
|
|
|
media : media,
|
|
|
|
rel : 'stylesheet'
|
|
|
|
}).appendTo($('head').get(0));
|
|
|
|
}
|
|
|
|
|
2015-11-02 05:11:22 +01:00
|
|
|
this._ondemand_loaded_list[styleUrl] = 1;
|
|
|
|
|
2009-11-21 03:31:36 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the X-Include-CSS and X-Include-JS headers provided by the Requirements class
|
|
|
|
*/
|
2011-03-01 05:39:12 +01:00
|
|
|
processOnDemandHeaders: function(xml, status, xhr) {
|
|
|
|
var self = this, processDfd = new $.Deferred();
|
2009-11-21 03:31:36 +01:00
|
|
|
|
|
|
|
// CSS
|
2011-03-30 03:29:22 +02:00
|
|
|
if(xhr.getResponseHeader && xhr.getResponseHeader('X-Include-CSS')) {
|
2011-03-01 05:39:12 +01:00
|
|
|
var cssIncludes = xhr.getResponseHeader('X-Include-CSS').split(',');
|
2009-11-21 03:31:36 +01:00
|
|
|
for(var i=0;i<cssIncludes.length;i++) {
|
|
|
|
// Syntax: "URL:##:media"
|
|
|
|
if(cssIncludes[i].match(/^(.*):##:(.*)$/)) {
|
2012-01-06 14:09:43 +01:00
|
|
|
$.requireCss(decodePath(RegExp.$1), RegExp.$2);
|
2009-11-21 03:31:36 +01:00
|
|
|
// Syntax: "URL"
|
|
|
|
} else {
|
2012-01-06 14:09:43 +01:00
|
|
|
$.requireCss(decodePath(cssIncludes[i]));
|
2009-11-21 03:31:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-18 01:14:40 +02:00
|
|
|
|
2009-11-21 03:31:36 +01:00
|
|
|
// JavaScript
|
|
|
|
var newJsIncludes = [];
|
2011-03-30 03:29:22 +02:00
|
|
|
if(xhr.getResponseHeader && xhr.getResponseHeader('X-Include-JS')) {
|
2011-03-01 05:39:12 +01:00
|
|
|
var jsIncludes = xhr.getResponseHeader('X-Include-JS').split(',');
|
2009-11-21 03:31:36 +01:00
|
|
|
for(var i=0;i<jsIncludes.length;i++) {
|
2012-01-06 14:09:43 +01:00
|
|
|
var jsIncludePath = decodePath(jsIncludes[i]);
|
|
|
|
if(!$.isItemLoaded(jsIncludePath)) {
|
|
|
|
newJsIncludes.push(jsIncludePath);
|
2009-11-21 03:31:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We make an array of the includes that are actually new, and attach the callback to the last one
|
|
|
|
// They are placed in a queue and will be included in order. This means that the callback will
|
|
|
|
// be able to execute script in the new includes (such as a livequery update)
|
|
|
|
var getScriptQueue = function() {
|
|
|
|
if(newJsIncludes.length) {
|
|
|
|
var newJsInclude = newJsIncludes.shift();
|
|
|
|
// emulates getScript() with addtl. setting
|
|
|
|
$.ajax({
|
|
|
|
dataType: 'script',
|
|
|
|
url: newJsInclude,
|
|
|
|
success: function() {
|
2015-11-02 05:11:22 +01:00
|
|
|
self._ondemand_loaded_list[newJsInclude] = 1;
|
2009-11-21 03:31:36 +01:00
|
|
|
getScriptQueue();
|
|
|
|
},
|
|
|
|
cache: false,
|
|
|
|
// jQuery seems to override the XHR objects if used in async mode
|
|
|
|
async: false
|
|
|
|
});
|
|
|
|
} else {
|
2011-03-01 05:39:12 +01:00
|
|
|
processDfd.resolve(xml, status, xhr);
|
2009-11-21 03:31:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(newJsIncludes.length) {
|
|
|
|
getScriptQueue();
|
|
|
|
} else {
|
|
|
|
// If there aren't any new includes, then we can just call the callbacks ourselves
|
2011-03-01 05:39:12 +01:00
|
|
|
processDfd.resolve(xml, status, xhr);
|
2009-11-21 03:31:36 +01:00
|
|
|
}
|
2011-03-01 05:39:12 +01:00
|
|
|
|
|
|
|
return processDfd.promise();
|
2009-06-18 01:14:40 +02:00
|
|
|
}
|
2011-03-01 05:39:12 +01:00
|
|
|
|
2009-11-21 03:31:36 +01:00
|
|
|
});
|
2009-06-18 01:14:40 +02:00
|
|
|
|
2011-03-01 05:39:12 +01:00
|
|
|
$.ajaxSetup({
|
|
|
|
// beforeSend is the only place to access the XHR object before success handlers are added
|
|
|
|
beforeSend: function(jqXHR, s) {
|
|
|
|
// Avoid recursion in ajax callbacks caused by getScript(), by not parsing
|
|
|
|
// ondemand headers for 'script' datatypes
|
|
|
|
if(s.dataType == 'script') return;
|
|
|
|
|
|
|
|
var dfd = new $.Deferred();
|
|
|
|
|
|
|
|
// Register our own success handler (assumes no handlers are already registered)
|
|
|
|
// 'success' is an alias for 'done', which is executed by the built-in deferred instance in $.ajax()
|
|
|
|
jqXHR.success(function(success, statusText, jXHR) {
|
|
|
|
$.processOnDemandHeaders(success, statusText, jXHR).done(function() {
|
|
|
|
dfd.resolveWith(s.context || this, [success, statusText, jXHR]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Reroute all external success hanlders through our own deferred.
|
|
|
|
// Not overloading fail() as no event can cause the original request to fail.
|
|
|
|
jqXHR.success = function(callback) {
|
|
|
|
dfd.done(callback);
|
2009-11-21 03:30:44 +01:00
|
|
|
}
|
|
|
|
}
|
2011-03-01 05:39:12 +01:00
|
|
|
});
|
|
|
|
|
2009-06-18 01:14:40 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
})(jQuery);
|