[#578] Added _teardown function as well as unit tests to test the functionality

This commit is contained in:
David Seifried 2011-06-23 11:27:49 -04:00
Родитель 9a20736ae8
Коммит c22734eff5
2 изменённых файлов: 76 добавлений и 29 удалений

Просмотреть файл

@ -3,7 +3,7 @@
var wikiCallback; var wikiCallback;
(function (Popcorn) { (function ( Popcorn ) {
/** /**
* Wikipedia popcorn plug-in * Wikipedia popcorn plug-in
@ -41,13 +41,37 @@ var wikiCallback;
website: "annasob.wordpress.com" website: "annasob.wordpress.com"
}, },
options:{ options:{
start : {elem:'input', type:'text', label:'In'}, start: {
end : {elem:'input', type:'text', label:'Out'}, elem: 'input',
lang : {elem:'input', type:'text', label:'Language'}, type: 'text',
src : {elem:'input', type:'text', label:'Src'}, label: 'In'
title : {elem:'input', type:'text', label:'Title'}, },
numberofwords : {elem:'input', type:'text', label:'Num Of Words'}, end: {
target : 'wikipedia-container' elem: 'input',
type: 'text',
label: 'Out'
},
lang: {
elem: 'input',
type: 'text',
label: 'Language'
},
src: {
elem: 'input',
type: 'text',
label: 'Src'
},
title: {
elem: 'input',
type: 'text',
label: 'Title'
},
numberofwords: {
elem: 'input',
type: 'text',
label: 'Num Of Words'
},
target: 'wikipedia-container'
} }
}, },
/** /**
@ -64,33 +88,41 @@ var wikiCallback;
var _text, _guid = Popcorn.guid(); var _text, _guid = Popcorn.guid();
// if the user didn't specify a language default to english // if the user didn't specify a language default to english
if (typeof options.lang === 'undefined') { options.lang ="en"; } if ( typeof options.lang === 'undefined' ) {
options.lang = "en";
}
// if the user didn't specify number of words to use default to 200 // if the user didn't specify number of words to use default to 200
options.numberofwords = options.numberofwords || 200; options.numberofwords = options.numberofwords || 200;
// wiki global callback function with a unique id // wiki global callback function with a unique id
// function gets the needed information from wikipedia // function gets the needed information from wikipedia
// and stores it by appending values to the options object // and stores it by appending values to the options object
window["wikiCallback"+ _guid] = function ( data ) { window[ "wikiCallback" + _guid ] = function ( data ) {
options._link = document.createElement( 'a' ); options._link = document.createElement( 'a' );
options._link.setAttribute( 'href', options.src ); options._link.setAttribute( 'href', options.src );
options._link.setAttribute( 'target', '_blank' ); options._link.setAttribute( 'target', '_blank' );
// add the title of the article to the link // add the title of the article to the link
options._link.innerHTML = options.title || data.parse.displaytitle; options._link.innerHTML = options.title || data.parse.displaytitle;
// get the content of the wiki article // get the content of the wiki article
options._desc = document.createElement( 'p' ); options._desc = document.createElement( 'p' );
// get the article text and remove any special characters // get the article text and remove any special characters
_text = data.parse.text[ "*" ].substr( data.parse.text[ "*" ].indexOf('<p>') ); _text = data.parse.text[ "*" ].substr( data.parse.text[ "*" ].indexOf( '<p>' ) );
_text = _text.replace(/((<(.|\n)+?>)|(\((.*?)\) )|(\[(.*?)\]))/g, ""); _text = _text.replace( /((<(.|\n)+?>)|(\((.*?)\) )|(\[(.*?)\]))/g, "" );
options._desc.innerHTML = _text.substr( 0, options.numberofwords ) + " ..."; options._desc.innerHTML = _text.substr( 0, options.numberofwords ) + " ...";
options._fired = true; options._fired = true;
}; };
if ( options.src ) { if ( options.src ) {
Popcorn.getScript("http://" + options.lang + ".wikipedia.org/w/api.php?action=parse&props=text&page=" + options.src.slice( options.src.lastIndexOf("/")+1) + "&format=json&callback=wikiCallback" + _guid); Popcorn.getScript( "http://" + options.lang + ".wikipedia.org/w/api.php?action=parse&props=text&page="
+ options.src.slice( options.src.lastIndexOf("/")+1) + "&format=json&callback=wikiCallback" + _guid);
} else { } else {
throw ("Wikipedia plugin needs a 'src'"); throw ( "Wikipedia plugin needs a 'src'" );
} }
}, },
@ -135,6 +167,15 @@ var wikiCallback;
document.getElementById( options.target ).removeChild( options._link ); document.getElementById( options.target ).removeChild( options._link );
document.getElementById( options.target ).removeChild( options._desc ); document.getElementById( options.target ).removeChild( options._desc );
} }
},
_teardown: function( options ){
if ( options._added ) {
options._link.parentNode && document.getElementById( options.target ).removeChild( options._link );
options._desc.parentNode && document.getElementById( options.target ).removeChild( options._desc );
delete options.target;
}
} }
}); });

Просмотреть файл

@ -1,24 +1,24 @@
test("Popcorn wikipedia Plugin", function () { test("Popcorn wikipedia Plugin", function () {
var popped = Popcorn("#video"), var popped = Popcorn( "#video" ),
expects = 10, expects = 11,
count = 0, count = 0,
theArticle = document.getElementById('wikidiv'); theArticle = document.getElementById( 'wikidiv' );
expect(expects); expect( expects );
function plus() { function plus() {
if ( ++count===expects) { if ( ++count === expects ) {
start(); start();
} }
} }
stop(); stop();
ok ('wikipedia' in popped, "wikipedia is a mehtod of the popped instance"); ok ( 'wikipedia' in popped, "wikipedia is a mehtod of the popped instance" );
plus(); plus();
equals (theArticle.innerHTML, "", "initially, there is nothing in the wikidiv" ); equals ( theArticle.innerHTML, "", "initially, there is nothing in the wikidiv" );
plus(); plus();
popped.wikipedia({ popped.wikipedia({
@ -38,27 +38,33 @@ test("Popcorn wikipedia Plugin", function () {
.play(); .play();
popped.exec( 2, function() { popped.exec( 2, function() {
ok (theArticle.innerHTML !== "", "wikidiv now contains information" ); ok ( theArticle.innerHTML !== "", "wikidiv now contains information" );
plus(); plus();
equals (theArticle.childElementCount, 2, "wikidiv now contains two child elements" ); equals ( theArticle.childElementCount, 2, "wikidiv now contains two child elements" );
plus(); plus();
equals (theArticle.children[0].innerHTML, "this is an article", "wikidiv has the right title" ); equals ( theArticle.children[ 0 ].innerHTML, "this is an article", "wikidiv has the right title" );
plus(); plus();
ok (theArticle.children[1].innerHTML !=="", "wikidiv has some content" ); ok ( theArticle.children[ 1 ].innerHTML !=="", "wikidiv has some content" );
plus(); plus();
}); });
popped.exec( 3, function() { popped.exec( 3, function() {
equals (theArticle.innerHTML, "", "wikidiv was cleared properly" ); equals ( theArticle.innerHTML, "", "wikidiv was cleared properly" );
plus(); plus();
}); });
popped.exec( 4, function() { popped.exec( 4, function() {
ok (theArticle.innerHTML !== "", "wikidiv now contains information" ); ok ( theArticle.innerHTML !== "", "wikidiv now contains information" );
plus(); plus();
equals (theArticle.childElementCount, 2, "wikidiv now contains two child elements" ); equals ( theArticle.childElementCount, 2, "wikidiv now contains two child elements" );
plus(); plus();
ok (theArticle.children[1].innerHTML !== "", "wikidiv has the right content" ); ok ( theArticle.children[ 1 ].innerHTML !== "", "wikidiv has the right content" );
plus();
});
popped.exec( 6, function() {
popped.pause().removeTrackEvent( popped.data.trackEvents.byStart[ 4 ]._id );
equals ( theArticle.innerHTML, "", "wikidiv is now empty" );
plus(); plus();
}); });
}); });