Merge branch 'wikiPlugin' into 0.3

This commit is contained in:
Anna Sobiepanek 2011-02-02 16:47:21 -05:00
Родитель 83e286e0c5 70274cee3b
Коммит 33ecf35d89
3 изменённых файлов: 25 добавлений и 21 удалений

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

@ -14,6 +14,7 @@
start: 0, // seconds start: 0, // seconds
end: 10, // seconds end: 10, // seconds
src: 'http://en.wikipedia.org/wiki/Cape_Town', src: 'http://en.wikipedia.org/wiki/Cape_Town',
title: "this is an article",
target: 'wikidiv' target: 'wikidiv'
} ) } )
.wikipedia({ .wikipedia({

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

@ -71,25 +71,25 @@ var wikiCallback;
// 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 = 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;
}; };
var head = document.getElementsByTagName("head")[0]; var head = document.getElementsByTagName( "head" )[0];
var script = document.createElement("script"); var script = document.createElement( "script" );
script.src = "http://"+options.lang+".wikipedia.org/w/api.php?action=parse&props=text&page=" + ( options.title || options.src.slice(options.src.lastIndexOf("/")+1)) + "&format=json&callback=wikiCallback"+ _guid; script.src = "http://" + options.lang + ".wikipedia.org/w/api.php?action=parse&props=text&page=" + ( options.title || options.src.slice( options.src.lastIndexOf("/")+1) ) + "&format=json&callback=wikiCallback" + _guid;
head.insertBefore( script, head.firstChild ); head.insertBefore( script, head.firstChild );
}, },
@ -99,20 +99,20 @@ var wikiCallback;
* of the video reaches the start time provided by the * of the video reaches the start time provided by the
* options variable * options variable
*/ */
start: function(event, options){ start: function( event, options ){
// dont do anything if the information didn't come back from wiki // dont do anything if the information didn't come back from wiki
var isReady = function () { var isReady = function () {
if ( !options._fired ) { if ( !options._fired ) {
setTimeout(function () { setTimeout( function () {
isReady(); isReady();
}, 13); }, 13);
} else { } else {
if (options._link && options._desc) { if ( options._link && options._desc ) {
if ( document.getElementById( options.target ) ) { if ( document.getElementById( options.target ) ) {
document.getElementById( options.target ).appendChild(options._link); document.getElementById( options.target ).appendChild( options._link );
document.getElementById( options.target ).appendChild(options._desc); document.getElementById( options.target ).appendChild( options._desc );
options._added = true; options._added = true;
} }
} }
@ -127,12 +127,12 @@ var wikiCallback;
* of the video reaches the end time provided by the * of the video reaches the end time provided by the
* options variable * options variable
*/ */
end: function(event, options){ end: function( event, options ){
// ensure that the data was actually added to the // ensure that the data was actually added to the
// DOM before removal // DOM before removal
if (options._added) { if ( options._added ) {
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 );
} }
} }

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

@ -1,7 +1,7 @@
test("Popcorn wikipedia Plugin", function () { test("Popcorn wikipedia Plugin", function () {
var popped = Popcorn("#video"), var popped = Popcorn("#video"),
expects = 7, expects = 9,
count = 0, count = 0,
theArticle = document.getElementById('wikidiv'), theArticle = document.getElementById('wikidiv'),
wikiInterval, wikiInterval,
@ -30,6 +30,7 @@ test("Popcorn wikipedia Plugin", function () {
start: 5, // seconds start: 5, // seconds
end: 10, // seconds end: 10, // seconds
src: 'http://en.wikipedia.org/wiki/Cape_Town', src: 'http://en.wikipedia.org/wiki/Cape_Town',
title: "this is an article",
target: 'wikidiv' target: 'wikidiv'
} ) } )
.wikipedia({ .wikipedia({
@ -48,6 +49,8 @@ test("Popcorn wikipedia Plugin", function () {
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.childElement[0].innerHTML, "this is an article", "wikidiv has the right title" );
plus();
equals (theArticle.childElement[1].innerHTML, "Cape Town metropolitan municipality. It is the provincial capital and primate city ...", "wikidiv has the right content" ); equals (theArticle.childElement[1].innerHTML, "Cape Town metropolitan municipality. It is the provincial capital and primate city ...", "wikidiv has the right content" );
plus(); plus();
clearInterval( wikiInterval ); clearInterval( wikiInterval );