From 4599b6eb3d86ec75b5d6691b09c30e130e9ac58e Mon Sep 17 00:00:00 2001 From: rwldrn Date: Wed, 15 Dec 2010 14:30:35 -0500 Subject: [PATCH] [#220] Popcorn.xhr support needed for parsing data-timeline-sources --- popcorn.js | 56 ++++++++++++++++++++++ test/data/json.js | 1 + test/data/test.js | 1 + test/data/test.txt | 1 + test/data/test.xml | 11 +++++ test/popcorn.unit.js | 112 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 182 insertions(+) create mode 100644 test/data/json.js create mode 100644 test/data/test.js create mode 100644 test/data/test.txt create mode 100644 test/data/test.xml diff --git a/popcorn.js b/popcorn.js index 6268d78..7be5192 100644 --- a/popcorn.js +++ b/popcorn.js @@ -542,6 +542,62 @@ return plugin; }; + + var setup = { + url: '', + data: '', + dataType: '', + success: Popcorn.nop, + type: 'GET', + async: true, + xhr: function() { + return new XMLHttpRequest(); + } + }; + + Popcorn.xhr = function ( options ) { + + var settings = Popcorn.extend( {}, setup, options ); + + settings.ajax = settings.xhr(); + + if ( settings.ajax ) { + + settings.ajax.open( settings.type, settings.url, settings.async ); + settings.ajax.send( null ); + + return Popcorn.xhr.httpData( settings ); + } + }; + + + Popcorn.xhr.httpData = function ( settings ) { + + var data, json = null, + + onreadystatechange = settings.ajax.onreadystatechange = function() { + + if ( settings.ajax.readyState == 4 ) { + + try { + json = JSON.parse(settings.ajax.responseText); + } catch(e) { + //suppress + }; + + data = { + xml: settings.ajax.responseXML, + text: settings.ajax.responseText, + json: json + }; + + settings.success.call( settings.ajax, data ); + + } + }; + return data; + }; + global.Popcorn = Popcorn; diff --git a/test/data/json.js b/test/data/json.js new file mode 100644 index 0000000..fa798a0 --- /dev/null +++ b/test/data/json.js @@ -0,0 +1 @@ +{ "data": {"lang": "en", "length": 25} } \ No newline at end of file diff --git a/test/data/test.js b/test/data/test.js new file mode 100644 index 0000000..fa798a0 --- /dev/null +++ b/test/data/test.js @@ -0,0 +1 @@ +{ "data": {"lang": "en", "length": 25} } \ No newline at end of file diff --git a/test/data/test.txt b/test/data/test.txt new file mode 100644 index 0000000..1f0886a --- /dev/null +++ b/test/data/test.txt @@ -0,0 +1 @@ +This is a text test \ No newline at end of file diff --git a/test/data/test.xml b/test/data/test.xml new file mode 100644 index 0000000..10f6b33 --- /dev/null +++ b/test/data/test.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index 28352c9..46ab0ad 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -662,8 +662,120 @@ test("Protected Names", function () { }; }); }); +Popcorn.xhr.httpData + +module("Popcorn XHR"); +test("Basic", function () { + + expect(2); + + equals( typeof Popcorn.xhr, "function" , "Popcorn.xhr is a provided utility function"); + equals( typeof Popcorn.xhr.httpData, "function" , "Popcorn.xhr.httpData is a provided utility function"); + + +}); + +test("Parsing text", function () { + + var expects = 2, + count = 0; + + function plus() { + if ( ++count === expects ) { + start(); + } + } + + expect(expects); + + stop(); + + Popcorn.xhr({ + url: 'data/test.txt', + success: function( data ) { + + ok(data, "xhr returns data"); + plus(); + + equals( data.text, "This is a text test", "test.txt returns the string 'This is a text test'"); + plus(); + + } + }); +}); + +test("Parsing JSON", function () { + + var expects = 2, + count = 0; + + function plus() { + if ( ++count === expects ) { + start(); + } + } + + expect(expects); + + stop(); + + + var testObj = { "data": {"lang": "en", "length": 25} }; + + Popcorn.xhr({ + url: 'data/test.js', + success: function( data ) { + + ok(data, "xhr returns data"); + plus(); + + + ok( QUnit.equiv(data.json, testObj) , "data.json returns an object of data"); + plus(); + + } + }); + +}); + +test("Parsing XML", function () { + + var expects = 2, + count = 0; + + function plus() { + if ( ++count === expects ) { + start(); + } + } + + expect(expects); + + stop(); + + + Popcorn.xhr({ + url: 'data/test.xml', + success: function( data ) { + + ok(data, "xhr returns data"); + plus(); + + var parser = new DOMParser(), + xml = parser.parseFromString(' ',"text/xml"); + + + equals( data.xml.toString(), xml.toString(), "data.xml returns a document of xml"); + plus(); + + } + }); + +}); + +module("Popcorn Test Runner End"); test("Last Check", function () { // ALWAYS RUN LAST -- 2.43.0