From f6575108911f1952f99eda8e12c667eed31931b9 Mon Sep 17 00:00:00 2001 From: Anna Sobiepanek Date: Wed, 22 Dec 2010 20:49:34 -0500 Subject: [PATCH] fixed as per review [#158 state:peer-review-requested] --- plugins/wikipedia/popcorn.wikipedia.html | 4 +- plugins/wikipedia/popcorn.wikipedia.js | 188 +++++++++++------------ 2 files changed, 88 insertions(+), 104 deletions(-) diff --git a/plugins/wikipedia/popcorn.wikipedia.html b/plugins/wikipedia/popcorn.wikipedia.html index 9c74d2f..8026a92 100644 --- a/plugins/wikipedia/popcorn.wikipedia.html +++ b/plugins/wikipedia/popcorn.wikipedia.html @@ -1,7 +1,7 @@ - Popcorn WebPage Plug-in Demo + Popcorn Wikipedia Plug-in Demo @@ -28,7 +28,7 @@ -

Popcorn WebPage Plug-in Demo

+

Popcorn Wikipedia Plug-in Demo

A Wiki article about Cape Town will appear at 0 seconds and disappear at 10 seconds.

A Wiki article about Sao Paolo will appear at 7 seconds and disappear at 20 seconds.

diff --git a/plugins/wikipedia/popcorn.wikipedia.js b/plugins/wikipedia/popcorn.wikipedia.js index fd273e8..f9794b4 100644 --- a/plugins/wikipedia/popcorn.wikipedia.js +++ b/plugins/wikipedia/popcorn.wikipedia.js @@ -9,11 +9,11 @@ var wikiCallback; * Wikipedia popcorn plug-in * Displays a wikipedia aricle in the target specified by the user by using * new DOM element instead overwriting them - * Options parameter will need a start, end, target, lang, src, and numOfWords. + * Options parameter will need a start, end, target, lang, src, title and numOfWords. * -Start is the time that you want this plug-in to execute * -End is the time that you want this plug-in to stop executing - * -Target is the id of the document element that the iframe needs to be attached to, - * this target element must exist on the DOM + * -Target is the id of the document element that the text from the article needs to be + * attached to, this target element must exist on the DOM * -Lang (optional, defaults to english) is the language in which the article is in. * -Src is the url of the article * -Title (optional) is the title of the article @@ -27,114 +27,98 @@ var wikiCallback; start: 5, // seconds end: 15, // seconds src: 'http://en.wikipedia.org/wiki/Cape_Town', - target: 'webpagediv' + target: 'wikidiv' } ) * */ - Popcorn.plugin( "wikipedia" , (function(){ + Popcorn.plugin( "wikipedia" , { - return { - /** - * @member wikipedia - * The setup function will get all of the needed - * items in place before the start function is called. - * This includes getting data from wikipedia, if the data - * is not received and processed before start is called start - * will not do anything - */ - _setup : function( options ) { - // declare needed variables - // get a guid to use for the global wikicallback function - var _text, _guid = Popcorn.guid(); - - // if the user didn't specify a language default to english - if (typeof options.lang === 'undefined') { options.lang ="en"; } - // if the user didn't specify number of words to use default to 200 - options.numOfWords = options.numOfWords || 200; - // replace the user specified target with the actual DOM element - options.target = document.getElementById( options.target ); - - // wiki global callback function with a unique id - // function gets the needed information from wikipedia - // and stores it by appending values to the options object - window["wikiCallback"+ _guid] = function (data) { - options._link = document.createElement('a'); - options._link.setAttribute('href', options.src); - options._link.setAttribute('target', '_blank'); - // add the title of the article to the link - options._link.innerHTML = data.parse.displaytitle; - // get the content of the wiki article - options._desc = document.createElement('p'); - // get the article text and remove any special characters - _text = data.parse.text["*"].substr(data.parse.text["*"].indexOf('

')); - _text = _text.replace(/((<(.|\n)+?>)|(\((.*?)\) )|(\[(.*?)\]))/g, ""); - options._desc.innerHTML = _text.substr(0, options.numOfWords ) + " ..."; - - options._fired = true; - }; - - // get the wiki article on a separate thread - // call the wikiCallback function above once the script is loaded - //setTimeout(function() { - // getJson("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); - //}, 500); + /** + * @member wikipedia + * The setup function will get all of the needed + * items in place before the start function is called. + * This includes getting data from wikipedia, if the data + * is not received and processed before start is called start + * will not do anything + */ + _setup : function( options ) { + // declare needed variables + // get a guid to use for the global wikicallback function + var _text, _guid = Popcorn.guid(); + + // if the user didn't specify a language default to english + if (typeof options.lang === 'undefined') { options.lang ="en"; } + // if the user didn't specify number of words to use default to 200 + options.numOfWords = options.numOfWords || 200; + // replace the user specified target with the actual DOM element + options.target = document.getElementById( options.target ); + + // wiki global callback function with a unique id + // function gets the needed information from wikipedia + // and stores it by appending values to the options object + window["wikiCallback"+ _guid] = function (data) { + options._link = document.createElement('a'); + options._link.setAttribute('href', options.src); + options._link.setAttribute('target', '_blank'); + // add the title of the article to the link + options._link.innerHTML = data.parse.displaytitle; + // get the content of the wiki article + options._desc = document.createElement('p'); + // get the article text and remove any special characters + _text = data.parse.text["*"].substr(data.parse.text["*"].indexOf('

')); + _text = _text.replace(/((<(.|\n)+?>)|(\((.*?)\) )|(\[(.*?)\]))/g, ""); + options._desc.innerHTML = _text.substr(0, options.numOfWords ) + " ..."; - // add the script to the DOM - /*var getJson = function(url) { - var head = document.getElementsByTagName("head")[0]; - var script = document.createElement("script"); - script.src = url; - head.insertBefore( script, head.firstChild ); - }; */ - var head = document.getElementsByTagName("head")[0]; - 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; + options._fired = true; + }; + + var head = document.getElementsByTagName("head")[0]; + 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; - head.insertBefore( script, head.firstChild ); - }, - /** - * @member wikipedia - * The start function will be executed when the currentTime - * of the video reaches the start time provided by the - * options variable - */ - start: function(event, options){ - // dont do anything if the information didn't come back from wiki - var isReady = function () { - - if ( !options._fired ) { - setTimeout(function () { - isReady(); - }, 13); - } else { + head.insertBefore( script, head.firstChild ); + }, + /** + * @member wikipedia + * The start function will be executed when the currentTime + * of the video reaches the start time provided by the + * options variable + */ + start: function(event, options){ + // dont do anything if the information didn't come back from wiki + var isReady = function () { - if (options._link && options._desc) { - options.target.appendChild(options._link); - options.target.appendChild(options._desc); - options._added = true; - } + if ( !options._fired ) { + setTimeout(function () { + isReady(); + }, 13); + } else { + + if (options._link && options._desc) { + options.target.appendChild(options._link); + options.target.appendChild(options._desc); + options._added = true; } - }; - - isReady(); - }, - /** - * @member wikipedia - * The end function will be executed when the currentTime - * of the video reaches the end time provided by the - * options variable - */ - end: function(event, options){ - // ensure that the data was actually added to the - // DOM before removal - if (options._added) { - options.target.removeChild(options._link); - options.target.removeChild(options._desc); } - } + }; - }; - - })()); + isReady(); + }, + /** + * @member wikipedia + * The end function will be executed when the currentTime + * of the video reaches the end time provided by the + * options variable + */ + end: function(event, options){ + // ensure that the data was actually added to the + // DOM before removal + if (options._added) { + options.target.removeChild(options._link); + options.target.removeChild(options._desc); + } + } + + }); })( Popcorn ); \ No newline at end of file -- 2.43.0