Skip to content

Commit

Permalink
Merge branch 'master' of github.com:observing/square into development
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd-Eden committed May 18, 2012
2 parents fac69d3 + 2589088 commit b7ca0a2
Showing 1 changed file with 1 addition and 114 deletions.
115 changes: 1 addition & 114 deletions static/reload.js
Expand Up @@ -7,119 +7,7 @@
(function loader () {
"use strict";

/**
* Generate a cache buster querystring for the given url, if it already has
* a generated cache buster url we are going to replace it with a new one.
*
* @param {String} url
* @returns {String}
* @api public
*/

loader.bust = function buster (url) {
var now = + new Date()
, prefix = '__square__'
, token = prefix + now + '=' + now;

// remove the old token from the url if it exists
url = url.replace(/[&|?]__square__\d+=\d+/g, '');

// add it to the url
return ~url.indexOf('&')
? url + '&' + token
: url + '?' + token;
};

/**
* Array#indexOf polyfill.
*
* @see bit.ly/a5Dxa2
* @param {Array} arr array to search in
* @param {Mixed} o item to search
* @param {Number} i optional start index
* @api public
*/

loader.indexOf = function indexOf (arr, o, i) {
var j = arr.length;
i = i < 0 ? i + j < 0 ? 0 : i + j : i || 0;

// iterate all the things
for (; i < j && arr[i] !== o; i++) {}

return j <= i
? -1
: i;
};

/**
* Generate a array of allowed images for hot reloading.
*
* @type {Array}
* @api private
*/

loader.images = (function images () {
var defaults = ['gif', 'jpg', 'jpeg', 'png']
, navigator = window.navigator;

if (!('mimeTypes' in navigator)) return defaults;

// generate a list of accepted extensions based on the mimeTypes that are
// stored in the browser.
var mimeTypes = Array.prototype.slice.call(navigator.mimeTypes, 0)
, i = mimeTypes.length;

while (i--) {
var type = mimeTypes[i]
, extensions = type.split(',')
, j = extensions.length;

// we only want image types
if (type.type && type.type.indexOf('image') === -1) continue;

// add the extensions to the defaults array if they don't exit there
// already
while (j--) {
if (loader.indexOf(defaults, extensions[j]) === -1) {
defaults.push(extensions[j]);
}
}
}

return defaults;
}());

/**
* Reload the data without refreshing the page if we can.
*
* @param {String} type extension
* @api public
*/

loader.reload = function reload (type) {
switch (type) {
case 'js':
// make sure we reload the page with a `true` to load it again from the
// server.
window.location.reload(true);
break;

case 'css':
loader.reload.styles();
break;

default:
loader.reload.images();
}
};

loader.reload.styles = function styles () {};
loader.reload.images = function images () {};

// Now for the big tricky part, we are going to load in all the dependencies
// of our livereload plugin so we can establish a real time connection with
// the started [square] service.
// find out the path of the reload service
var scripts = document.getElementsByTagName('script')
, i = scripts.length
, base;
Expand Down Expand Up @@ -149,7 +37,6 @@
document.body.appendChild(script);
script.src = base + '/live/socket.io.js';

// a poorman's script onready checker
setTimeout(function has () {
if (!('io' in window)) return setTimeout(has, 250);

Expand Down

0 comments on commit b7ca0a2

Please sign in to comment.