|
1 | 1 | (function(global) {
|
2 | 2 | "use strict";
|
3 | 3 |
|
| 4 | + var inNodeJS = false; |
| 5 | + if (typeof process !== 'undefined') { |
| 6 | + inNodeJS = true; |
| 7 | + var request = require('request'); |
| 8 | + } |
| 9 | + |
4 | 10 | if (!Array.prototype.indexOf) {
|
5 | 11 | Array.prototype.indexOf = function (obj, fromIndex) {
|
6 | 12 | if (fromIndex === null) {
|
|
30 | 36 | if(!this || !(this instanceof Tabletop)) {
|
31 | 37 | return new Tabletop(options);
|
32 | 38 | }
|
33 |
| - |
| 39 | + |
34 | 40 | if(typeof(options) === 'string') {
|
35 | 41 | options = { key : options };
|
36 | 42 | }
|
|
79 | 85 | this.models = {};
|
80 | 86 | this.model_names = [];
|
81 | 87 |
|
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 | + } |
83 | 95 |
|
84 | 96 | if(!this.wait) {
|
85 | 97 | this.fetch();
|
|
104 | 116 | if(typeof(callback) !== "undefined") {
|
105 | 117 | this.callback = callback;
|
106 | 118 | }
|
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 | + } |
108 | 133 | },
|
109 | 134 |
|
110 | 135 | /*
|
|
158 | 183 |
|
159 | 184 | document.getElementsByTagName('script')[0].parentNode.appendChild(script);
|
160 | 185 | },
|
| 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 | + }, |
161 | 199 |
|
162 | 200 | /*
|
163 | 201 | Is this a sheet you want to pull?
|
|
212 | 250 | */
|
213 | 251 | loadSheets: function(data) {
|
214 | 252 | var i, ilen;
|
215 |
| - var toInject = []; |
| 253 | + var toLoad = []; |
216 | 254 | this.foundSheetNames = [];
|
217 | 255 |
|
218 | 256 | for(i = 0, ilen = data.feed.entry.length; i < ilen ; i++) {
|
219 | 257 | this.foundSheetNames.push(data.feed.entry[i].title.$t);
|
220 | 258 | // Only pull in desired sheets to reduce loading
|
221 | 259 | if( this.isWanted(data.feed.entry[i].content.$t) ) {
|
222 | 260 | 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); |
225 | 268 | }
|
226 | 269 | }
|
227 | 270 |
|
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); |
231 | 274 | }
|
232 | 275 | },
|
233 | 276 |
|
|
274 | 317 | Tests this.sheetsToLoad just in case a race condition happens to show up
|
275 | 318 | */
|
276 | 319 | 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 | + } |
279 | 323 | },
|
280 | 324 |
|
281 | 325 | log: function(msg) {
|
|
360 | 404 | }
|
361 | 405 | };
|
362 | 406 |
|
363 |
| -})(this); |
| 407 | +})(typeof process === 'undefined' ? this : module.exports); |
0 commit comments