Update tabletop.js to 1.3.3

This commit is contained in:
Jessica Lord 2014-02-24 23:48:47 -08:00
parent 23c034f3ba
commit d22d2f9631

View File

@ -2,26 +2,39 @@
"use strict"; "use strict";
var inNodeJS = false; var inNodeJS = false;
if (typeof process !== 'undefined') { if (typeof module !== 'undefined' && module.exports) {
inNodeJS = true; inNodeJS = true;
var request = require('request'); var request = require('request');
} }
if (!Array.prototype.indexOf) { var supportsCORS = false;
Array.prototype.indexOf = function (obj, fromIndex) { var inLegacyIE = false;
if (fromIndex === null) { try {
fromIndex = 0; var testXHR = new XMLHttpRequest();
} else if (fromIndex < 0) { if (typeof testXHR.withCredentials !== 'undefined') {
fromIndex = Math.max(0, this.length + fromIndex); supportsCORS = true;
} else {
if ("XDomainRequest" in window) {
supportsCORS = true;
inLegacyIE = true;
} }
for (var i = fromIndex, j = this.length; i < j; i++) { }
if (this[i] === obj) { } catch (e) { }
return i;
} // Create a simple indexOf function for support
} // of older browsers. Uses native indexOf if
return -1; // available. Code similar to underscores.
}; // By making a separate function, instead of adding
} // to the prototype, we will not break bad for loops
// in older browsers
var indexOfProto = Array.prototype.indexOf;
var ttIndexOf = function(array, item) {
var i = 0, l = array.length;
if (indexOfProto && array.indexOf === indexOfProto) return array.indexOf(item);
for (; i < l; i++) if (array[i] === item) return i;
return -1;
};
/* /*
Initialize with Tabletop.init( { key: '0AjAPaAU9MeLFdHUxTlJiVVRYNGRJQnRmSnQwTlpoUXc' } ) Initialize with Tabletop.init( { key: '0AjAPaAU9MeLFdHUxTlJiVVRYNGRJQnRmSnQwTlpoUXc' } )
@ -47,18 +60,24 @@
this.simpleSheet = !!options.simpleSheet; this.simpleSheet = !!options.simpleSheet;
this.parseNumbers = !!options.parseNumbers; this.parseNumbers = !!options.parseNumbers;
this.wait = !!options.wait; this.wait = !!options.wait;
this.reverse = !!options.reverse;
this.postProcess = options.postProcess; this.postProcess = options.postProcess;
this.debug = !!options.debug; this.debug = !!options.debug;
this.query = options.query || ''; this.query = options.query || '';
this.orderby = options.orderby;
this.endpoint = options.endpoint || "https://spreadsheets.google.com"; this.endpoint = options.endpoint || "https://spreadsheets.google.com";
this.singleton = !!options.singleton; this.singleton = !!options.singleton;
this.simple_url = !!options.simple_url; this.simple_url = !!options.simple_url;
this.callbackContext = options.callbackContext; this.callbackContext = options.callbackContext;
if(typeof(options.proxy) !== 'undefined') { if(typeof(options.proxy) !== 'undefined') {
this.endpoint = options.proxy; // Remove trailing slash, it will break the app
this.endpoint = options.proxy.replace(/\/$/,'');
this.simple_url = true; this.simple_url = true;
this.singleton = true; this.singleton = true;
// Let's only use CORS (straight JSON request) when
// fetching straight from Google
supportsCORS = false
} }
this.parameterize = options.parameterize || false; this.parameterize = options.parameterize || false;
@ -72,10 +91,16 @@
/* Be friendly about what you accept */ /* Be friendly about what you accept */
if(/key=/.test(this.key)) { if(/key=/.test(this.key)) {
this.log("You passed a key as a URL! Attempting to parse."); this.log("You passed an old Google Docs url as the key! Attempting to parse.");
this.key = this.key.match("key=(.*?)&")[1]; this.key = this.key.match("key=(.*?)&")[1];
} }
if(/pubhtml/.test(this.key)) {
alert("You passed a new Google Spreadsheets url as the key! This won't work yet, you'll need to change back to old Sheets.");
this.key = this.key.match("d\\/(.*?)\\/pubhtml")[1];
console.log(this.key);
}
if(!this.key) { if(!this.key) {
this.log("You need to pass Tabletop a key!"); this.log("You need to pass Tabletop a key!");
return; return;
@ -88,7 +113,7 @@
this.base_json_path = "/feeds/worksheets/" + this.key + "/public/basic?alt="; this.base_json_path = "/feeds/worksheets/" + this.key + "/public/basic?alt=";
if (inNodeJS) { if (inNodeJS || supportsCORS) {
this.base_json_path += 'json'; this.base_json_path += 'json';
} else { } else {
this.base_json_path += 'json-in-script'; this.base_json_path += 'json-in-script';
@ -129,9 +154,35 @@
if (inNodeJS) { if (inNodeJS) {
this.serverSideFetch(path, callback); this.serverSideFetch(path, callback);
} else { } else {
this.injectScript(path, callback); //CORS only works in IE8/9 across the same protocol
//You must have your server on HTTPS to talk to Google, or it'll fall back on injection
var protocol = this.endpoint.split("//").shift() || "http";
if (supportsCORS && (!inLegacyIE || protocol === location.protocol)) {
this.xhrFetch(path, callback);
} else {
this.injectScript(path, callback);
}
} }
}, },
/*
Use Cross-Origin XMLHttpRequest to get the data in browsers that support it.
*/
xhrFetch: function(path, callback) {
//support IE8's separate cross-domain object
var xhr = inLegacyIE ? new XDomainRequest() : new XMLHttpRequest();
xhr.open("GET", this.endpoint + path);
var self = this;
xhr.onload = function() {
try {
var json = JSON.parse(xhr.responseText);
} catch (e) {
console.error(e);
}
callback.call(self, json);
};
xhr.send();
},
/* /*
Insert the URL into the page as a script tag. Once it's loaded the spreadsheet data Insert the URL into the page as a script tag. Once it's loaded the spreadsheet data
@ -207,7 +258,7 @@
if(this.wanted.length === 0) { if(this.wanted.length === 0) {
return true; return true;
} else { } else {
return this.wanted.indexOf(sheetName) !== -1; return (ttIndexOf(this.wanted, sheetName) !== -1);
} }
}, },
@ -236,7 +287,7 @@
Add another sheet to the wanted list Add another sheet to the wanted list
*/ */
addWanted: function(sheet) { addWanted: function(sheet) {
if(this.wanted.indexOf(sheet) === -1) { if(ttIndexOf(this.wanted, sheet) === -1) {
this.wanted.push(sheet); this.wanted.push(sheet);
} }
}, },
@ -260,11 +311,17 @@
if( this.isWanted(data.feed.entry[i].content.$t) ) { if( this.isWanted(data.feed.entry[i].content.$t) ) {
var sheet_id = data.feed.entry[i].link[3].href.substr( data.feed.entry[i].link[3].href.length - 3, 3); var sheet_id = data.feed.entry[i].link[3].href.substr( data.feed.entry[i].link[3].href.length - 3, 3);
var json_path = "/feeds/list/" + this.key + "/" + sheet_id + "/public/values?sq=" + this.query + '&alt=' var json_path = "/feeds/list/" + this.key + "/" + sheet_id + "/public/values?sq=" + this.query + '&alt='
if (inNodeJS) { if (inNodeJS || supportsCORS) {
json_path += 'json'; json_path += 'json';
} else { } else {
json_path += 'json-in-script'; json_path += 'json-in-script';
} }
if(this.orderby) {
json_path += "&orderby=column:" + this.orderby.toLowerCase();
}
if(this.reverse) {
json_path += "&reverse=true";
}
toLoad.push(json_path); toLoad.push(json_path);
} }
} }
@ -304,7 +361,7 @@
postProcess: this.postProcess, postProcess: this.postProcess,
tabletop: this } ); tabletop: this } );
this.models[ model.name ] = model; this.models[ model.name ] = model;
if(this.model_names.indexOf(model.name) === -1) { if(ttIndexOf(this.model_names, model.name) === -1) {
this.model_names.push(model.name); this.model_names.push(model.name);
} }
this.sheetsToLoad--; this.sheetsToLoad--;
@ -411,4 +468,4 @@
global.Tabletop = Tabletop; global.Tabletop = Tabletop;
} }
})(this); })(this);