Skip to content

Commit 015d6f5

Browse files
committed
Merge branch 'master' of github.com:jsoma/tabletop
2 parents 839fd05 + 5fd1b0f commit 015d6f5

File tree

4 files changed

+98
-12
lines changed

4 files changed

+98
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
node_modules
12
.DS_Store
23
*.swp

examples/nodejs/test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var Tabletop = require('../../').Tabletop;
2+
3+
var testURL = 'https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0AmYzu_s7QHsmdDNZUzRlYldnWTZCLXdrMXlYQzVxSFE&output=html';
4+
5+
function onLoad(data, tabletop) {
6+
console.log(data);
7+
};
8+
9+
var options = {
10+
key: testURL,
11+
callback: onLoad,
12+
simpleSheet: true
13+
};
14+
15+
Tabletop.init(options);

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "tabletop",
3+
"version": "0.0.0",
4+
"description": "**Tabletop.js** takes a Google Spreadsheet and makes it easily accessible through JavaScript. With zero dependencies!",
5+
"main": "src/tabletop.js",
6+
"directories": {
7+
"example": "examples"
8+
},
9+
"scripts": {
10+
"test": "node examples/nodejs/test.js"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "https://github.com/jsoma/tabletop.git"
15+
},
16+
"author": "",
17+
"license": "BSD",
18+
"readmeFilename": "README.md",
19+
"gitHead": "fdb1c3ee3bc0f7f3be6ab223a56f9f050fe32496",
20+
"dependencies": {
21+
"request": "~2.16.6"
22+
},
23+
"engines": {
24+
"node": ">=0.10.0"
25+
}
26+
}

src/tabletop.js

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
(function(global) {
22
"use strict";
33

4+
var inNodeJS = false;
5+
if (typeof process !== 'undefined') {
6+
inNodeJS = true;
7+
var request = require('request');
8+
}
9+
410
if (!Array.prototype.indexOf) {
511
Array.prototype.indexOf = function (obj, fromIndex) {
612
if (fromIndex === null) {
@@ -30,7 +36,7 @@
3036
if(!this || !(this instanceof Tabletop)) {
3137
return new Tabletop(options);
3238
}
33-
39+
3440
if(typeof(options) === 'string') {
3541
options = { key : options };
3642
}
@@ -79,7 +85,13 @@
7985
this.models = {};
8086
this.model_names = [];
8187

82-
this.base_json_path = "/feeds/worksheets/" + this.key + "/public/basic?alt=json-in-script";
88+
this.base_json_path = "/feeds/worksheets/" + this.key + "/public/basic?alt=";
89+
90+
if (inNodeJS) {
91+
this.base_json_path += 'json';
92+
} else {
93+
this.base_json_path += 'json-in-script';
94+
}
8395

8496
if(!this.wait) {
8597
this.fetch();
@@ -104,7 +116,20 @@
104116
if(typeof(callback) !== "undefined") {
105117
this.callback = callback;
106118
}
107-
this.injectScript(this.base_json_path, this.loadSheets);
119+
this.requestData(this.base_json_path, this.loadSheets);
120+
},
121+
122+
/*
123+
This will call the environment appropriate request method.
124+
125+
In browser it will use JSON-P, in node it will use request()
126+
*/
127+
requestData: function(path, callback) {
128+
if (inNodeJS) {
129+
this.serverSideFetch(path, callback);
130+
} else {
131+
this.injectScript(path, callback);
132+
}
108133
},
109134

110135
/*
@@ -158,6 +183,19 @@
158183

159184
document.getElementsByTagName('script')[0].parentNode.appendChild(script);
160185
},
186+
187+
/*
188+
This will only run if tabletop is being run in node.js
189+
*/
190+
serverSideFetch: function(path, callback) {
191+
var self = this
192+
request({url: this.endpoint + path, json: true}, function(err, resp, body) {
193+
if (err) {
194+
return console.error(err);
195+
}
196+
callback.call(self, body);
197+
});
198+
},
161199

162200
/*
163201
Is this a sheet you want to pull?
@@ -212,22 +250,27 @@
212250
*/
213251
loadSheets: function(data) {
214252
var i, ilen;
215-
var toInject = [];
253+
var toLoad = [];
216254
this.foundSheetNames = [];
217255

218256
for(i = 0, ilen = data.feed.entry.length; i < ilen ; i++) {
219257
this.foundSheetNames.push(data.feed.entry[i].title.$t);
220258
// Only pull in desired sheets to reduce loading
221259
if( this.isWanted(data.feed.entry[i].content.$t) ) {
222260
var sheet_id = data.feed.entry[i].link[3].href.substr( data.feed.entry[i].link[3].href.length - 3, 3);
223-
var json_path = "/feeds/list/" + this.key + "/" + sheet_id + "/public/values?alt=json-in-script&sq=" + this.query;
224-
toInject.push(json_path);
261+
var json_path = "/feeds/list/" + this.key + "/" + sheet_id + "/public/values?sq=" + this.query + '&alt='
262+
if (inNodeJS) {
263+
json_path += 'json';
264+
} else {
265+
json_path += 'json-in-script';
266+
}
267+
toLoad.push(json_path);
225268
}
226269
}
227270

228-
this.sheetsToLoad = toInject.length;
229-
for(i = 0, ilen = toInject.length; i < ilen; i++) {
230-
this.injectScript(toInject[i], this.loadSheet);
271+
this.sheetsToLoad = toLoad.length;
272+
for(i = 0, ilen = toLoad.length; i < ilen; i++) {
273+
this.requestData(toLoad[i], this.loadSheet);
231274
}
232275
},
233276

@@ -274,8 +317,9 @@
274317
Tests this.sheetsToLoad just in case a race condition happens to show up
275318
*/
276319
doCallback: function() {
277-
if(this.sheetsToLoad === 0)
278-
this.callback.apply(this.callbackContext || this, [this.data(), this]);
320+
if(this.sheetsToLoad === 0) {
321+
this.callback.apply(this.callbackContext || this, [this.data(), this]);
322+
}
279323
},
280324

281325
log: function(msg) {
@@ -360,4 +404,4 @@
360404
}
361405
};
362406

363-
})(this);
407+
})(typeof process === 'undefined' ? this : module.exports);

0 commit comments

Comments
 (0)