[124] | 1 | /* |
---|
| 2 | * Timemap.js Copyright 2008 Nick Rabinowitz. |
---|
| 3 | * Licensed under the MIT License (see LICENSE.txt) |
---|
| 4 | */ |
---|
| 5 | |
---|
| 6 | /** |
---|
| 7 | * @fileOverview |
---|
| 8 | * JSON Loaders (JSONP, JSON String) |
---|
| 9 | * |
---|
| 10 | * @author Nick Rabinowitz (www.nickrabinowitz.com) |
---|
| 11 | */ |
---|
| 12 | |
---|
| 13 | // for JSLint |
---|
| 14 | /*global TimeMap */ |
---|
| 15 | |
---|
| 16 | /** |
---|
| 17 | * @class |
---|
| 18 | * JSONP loader class - expects a service that takes a callback function name as |
---|
| 19 | * the last URL parameter. |
---|
| 20 | * |
---|
| 21 | * <p>The jsonp loader assumes that the JSON can be loaded from a url to which a |
---|
| 22 | * callback function name can be appended, e.g. "http://www.test.com/getsomejson.php?callback=" |
---|
| 23 | * The loader then appends a nonce function name which the JSON should include. |
---|
| 24 | * This works for services like Google Spreadsheets, etc., and accepts remote URLs.</p> |
---|
| 25 | * |
---|
| 26 | * @example Usage in TimeMap.init(): |
---|
| 27 | |
---|
| 28 | datasets: [ |
---|
| 29 | { |
---|
| 30 | title: "JSONP Dataset", |
---|
| 31 | type: "jsonp", |
---|
| 32 | options: { |
---|
| 33 | url: "http://www.test.com/getsomejson.php?callback=" |
---|
| 34 | } |
---|
| 35 | } |
---|
| 36 | ] |
---|
| 37 | * |
---|
| 38 | * @constructor |
---|
| 39 | * @param {Object} options All options for the loader:<pre> |
---|
| 40 | * {Array} url URL of JSON service to load, callback name left off |
---|
| 41 | * {Function} preloadFunction Function to call on data before loading |
---|
| 42 | * {Function} transformFunction Function to call on individual items before loading |
---|
| 43 | * </pre> |
---|
| 44 | */ |
---|
| 45 | TimeMap.loaders.jsonp = function(options) { |
---|
| 46 | // get standard functions and document |
---|
| 47 | TimeMap.loaders.mixin(this, options); |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * Function to call on data object before loading |
---|
| 51 | * @name TimeMap.loaders.jsonp#preload |
---|
| 52 | * @function |
---|
| 53 | * @parameter {Object} data Data to preload |
---|
| 54 | * @return {Array} data Array of item data |
---|
| 55 | */ |
---|
| 56 | |
---|
| 57 | /** |
---|
| 58 | * Function to call on a single item data object before loading |
---|
| 59 | * @name TimeMap.loaders.jsonp#transform |
---|
| 60 | * @function |
---|
| 61 | * @parameter {Object} data Data to transform |
---|
| 62 | * @return {Object} data Transformed data for one item |
---|
| 63 | */ |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | * URL to load, with missing callback parameter |
---|
| 67 | * @example "http://www.example.com/myjsonservice?callback=" |
---|
| 68 | * @type String |
---|
| 69 | */ |
---|
| 70 | this.url = options.url; |
---|
| 71 | }; |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * JSONP load function. |
---|
| 75 | * |
---|
| 76 | * @param {TimeMapDataset} dataset Dataset to load data into |
---|
| 77 | * @param {Function} callback Function to call once data is loaded |
---|
| 78 | */ |
---|
| 79 | TimeMap.loaders.jsonp.prototype.load = function(dataset, callback) { |
---|
| 80 | var loader = this; |
---|
| 81 | // get items |
---|
| 82 | TimeMap.loaders.jsonp.read(this.url, function(result) { |
---|
| 83 | // load |
---|
| 84 | var items = loader.preload(result); |
---|
| 85 | dataset.loadItems(items, loader.transform); |
---|
| 86 | // callback |
---|
| 87 | callback(); |
---|
| 88 | }); |
---|
| 89 | }; |
---|
| 90 | |
---|
| 91 | /** |
---|
| 92 | * Static - for naming callback functions |
---|
| 93 | * @type int |
---|
| 94 | */ |
---|
| 95 | TimeMap.loaders.jsonp.counter = 0; |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * Static - reads JSON from a URL, assuming that the service is set up to apply |
---|
| 99 | * a callback function specified in the URL parameters. |
---|
| 100 | * |
---|
| 101 | * @param {String} jsonUrl URL to load, missing the callback function name |
---|
| 102 | * @param {function} f Callback function to apply to returned data |
---|
| 103 | */ |
---|
| 104 | TimeMap.loaders.jsonp.read = function(url, f) { |
---|
| 105 | // Define a unique function name |
---|
| 106 | var callbackName = "_" + TimeMap.loaders.jsonp.counter++; |
---|
| 107 | |
---|
| 108 | TimeMap.loaders.jsonp[callbackName] = function(result) { |
---|
| 109 | // Pass result to user function |
---|
| 110 | f(result); |
---|
| 111 | // Delete the callback function |
---|
| 112 | delete TimeMap.loaders.jsonp[callbackName]; |
---|
| 113 | }; |
---|
| 114 | |
---|
| 115 | // Create a script tag, set its src attribute and add it to the document |
---|
| 116 | // This triggers the HTTP request and submits the query |
---|
| 117 | var script = document.createElement("script"); |
---|
| 118 | script.src = url + "TimeMap.loaders.jsonp." + callbackName; |
---|
| 119 | document.body.appendChild(script); |
---|
| 120 | }; |
---|
| 121 | |
---|
| 122 | /** |
---|
| 123 | * Static - cancel all current JSONP requests |
---|
| 124 | * (note that this doesn't cancel the load, just the callback) |
---|
| 125 | */ |
---|
| 126 | TimeMap.loaders.jsonp.cancelAll = function() { |
---|
| 127 | var namespace = TimeMap.loaders.jsonp; |
---|
| 128 | for (var i in namespace){ |
---|
| 129 | // XXX: this is too cludgy - callback functions need their own namespace |
---|
| 130 | if (i.substr(0,1) == '_'){ |
---|
| 131 | namespace[i] = function(){ |
---|
| 132 | delete namespace[i]; |
---|
| 133 | }; |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | }; |
---|
| 137 | |
---|
| 138 | /** |
---|
| 139 | * @class |
---|
| 140 | * JSON string loader factory - expects a plain JSON array. |
---|
| 141 | * |
---|
| 142 | * <p>The json_string loader assumes an array of items in plain JSON, with no |
---|
| 143 | * callback function - this will require a local URL.</p> |
---|
| 144 | * <p>Note that this loader requires lib/json2.pack.js.</p> |
---|
| 145 | * |
---|
| 146 | * @augments TimeMap.loaders.remote |
---|
| 147 | * |
---|
| 148 | * @requires lib/json2.pack.js |
---|
| 149 | * |
---|
| 150 | * @example Usage in TimeMap.init(): |
---|
| 151 | |
---|
| 152 | datasets: [ |
---|
| 153 | { |
---|
| 154 | title: "JSON String Dataset", |
---|
| 155 | type: "json_string", |
---|
| 156 | options: { |
---|
| 157 | url: "mydata.json" // Must be a local URL |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | ] |
---|
| 161 | * |
---|
| 162 | * @param {Object} options All options for the loader:<pre> |
---|
| 163 | * {Array} url URL of JSON service to load, callback name left off |
---|
| 164 | * {Function} preloadFunction Function to call on data before loading |
---|
| 165 | * {Function} transformFunction Function to call on individual items before loading |
---|
| 166 | * </pre> |
---|
| 167 | * @return {TimeMap.loaders.remote} Remote loader configured for JSON strings |
---|
| 168 | */ |
---|
| 169 | TimeMap.loaders.json_string = function(options) { |
---|
| 170 | var loader = new TimeMap.loaders.remote(options); |
---|
| 171 | |
---|
| 172 | /** |
---|
| 173 | * Parse a JSON string into a JavaScript object, using the json2.js library. |
---|
| 174 | * @name TimeMap.loaders.json_string#parse |
---|
| 175 | * @param {String} json JSON string to parse |
---|
| 176 | * @returns {Object} Parsed JavaScript object |
---|
| 177 | */ |
---|
| 178 | loader.parse = JSON.parse; |
---|
| 179 | |
---|
| 180 | return loader; |
---|
| 181 | }; |
---|
| 182 | |
---|
| 183 | // Probably the default json loader should be json_string, not |
---|
| 184 | // jsonp. I may change this in the future, so I'd encourage you to use |
---|
| 185 | // the specific one you want. |
---|
| 186 | TimeMap.loaders.json = TimeMap.loaders.jsonp; |
---|