Skip to content

Commit

Permalink
Merge pull request #2 from LauLamanApps/master
Browse files Browse the repository at this point in the history
version 2.0
  • Loading branch information
suabo committed May 6, 2015
2 parents 7231a75 + e553387 commit a04efe3
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 48 deletions.
4 changes: 2 additions & 2 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Development of Version 1.0.0 by
Marcel Grolms
Development of Version 1.0.0 by Marcel Grolms
Development of Version 2.0.0 by Laurens Laman
51 changes: 5 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,12 @@ jQuery Plugin for Wikipedia

## Description
jQuery Plugin zum einfachen nachladen von Daten aus Wikipedia über die Wikipedia API mittels JSON.
Zum laden muss ein Wikipediaseitentitel übergeben werden. Über den zweiten Parameter können
diverse Einstellungen für die Darstellung getroffen werden.
jQuery plugin for easy download of data from Wikipedia about the Wikipedia API using JSON. To download a Wikipedia page a title needs to be passed. Various settings can be made about the second parameter.

[Demo]http://support.suabo.de/opensource/jquery-plugin-wikipedia

## Usage

1- Include the Javascript resources into you pages <head>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.wikipedia.js"></script>

2- Add a placeholder for the widget to your page

<div id="divRss"></div>

3- Add the Javascript that will populate the placeholder

$('#ContainerToLoad').WikipediaWidget('Page Title', [{ 'showTitle': true,
'maxThumbnails': 3,
'cutFirstInfoTableRows': 4,
'maxInfoTableRows': 8,
'thumbMaxWidth': '180px',
'thumbMaxHeight': '180px'
}]);

## Options

- **showTitle**
Show or hide titlebar. Default: true

- **maxThumbnails**
Set max count of thumbnails to load. Default: 3

- **cutFirstInfoTableRows**
Cut out the first rows of the information table. Default: 4

- **maxInfoTableRows**
Set max count of information table rows. Default: 8

- **thumbMaxWidth**
Set max Thumbnail width. Default: '180px'
Demo: http://support.suabo.de/opensource/jquery-plugin-wikipedia

- **thumbMaxHeight**
Set max Thumbnail height. Default: '180px'
## Documentation
[Version 1.0](https://github.com/LauLamanApps/Wikipedia/blob/master/documentation/version%201.0.md)

## Demo
[Version 2.0](https://github.com/LauLamanApps/Wikipedia/blob/master/documentation/version%202.0.md#version-20)

[Demo]http://support.suabo.de/opensource/jquery-plugin-wikipedia
43 changes: 43 additions & 0 deletions documentation/version 1.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## Usage

1- Include the Javascript resources into you pages <head>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.wikipedia.js"></script>

2- Add a placeholder for the widget to your page

<div id="divRss"></div>

3- Add the Javascript that will populate the placeholder

$('#divRss').WikipediaWidget('Page Title', [{ 'showTitle': true,
'maxThumbnails': 3,
'cutFirstInfoTableRows': 4,
'maxInfoTableRows': 8,
'thumbMaxWidth': '180px',
'thumbMaxHeight': '180px'
}]);

## Options

- **showTitle**
Show or hide titlebar. Default: true

- **maxThumbnails**
Set max count of thumbnails to load. Default: 3

- **cutFirstInfoTableRows**
Cut out the first rows of the information table. Default: 4

- **maxInfoTableRows**
Set max count of information table rows. Default: 8

- **thumbMaxWidth**
Set max Thumbnail width. Default: '180px'

- **thumbMaxHeight**
Set max Thumbnail height. Default: '180px'

## Demo
http://support.suabo.de/opensource/jquery-plugin-wikipedia
56 changes: 56 additions & 0 deletions documentation/version 2.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
## Version 2.0

## Usage

1- Include the Javascript resources into you pages <head>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.wikipedia.2.0.js"></script>

2- Add a placeholder for the widget to your page

<div id="divRss"></div>

3- Add the Javascript that will populate the placeholder

$('#divRss').Wikipedia('Page Title', options, callback);

## Options

- **showTitle**
Show or hide titlebar. Default: `false`
- **showInfo**
Show or hide info. Default: `true`
- **maxThumbnails**
Set max count of thumbnails to load. Default: `0`
- **cutFirstInfoTableRows**
Cut out the first rows of the information table. Default: `0`
- **maxInfoTableRows**
Set max count of information table rows. 0 means all. Default: `0`
- **thumbMaxWidth**
Set max Thumbnail width. Default: `'180px'`
- **thumbMaxHeight**
Set max Thumbnail height. Default: `'180px'`
- **locale**
Locale you want to request the information from. Default: `'en'`
- **removeIPA**
Remove the "[ˈæpəlˌɪŋk]" IPA. Default: `true`
- **descriptionLinks**
Show links in description. Default: `true`
- **redirect**
Redirect when only 1 link is found. Default: `true`
- **elements.loader**
Container the datavis placed in. Default: `'<div class="ajaxLoading">Loading...</div>'`
- **elements.title**
Container the datavis placed in. Default: `['<div class="wikipediaTitle">', '</div>']`
- **elements.description**
Container the datavis placed in. Default: `['<div class="wikipediaDescription">', '</div>']`
- **elements.logo**
Container the datavis placed in. Default: `['<div class="wikipediaLogo">', '</div>']`
- **elements.gallery.parent**
Container the datavis placed in. Default: `['<ul class="wikipediaThumbGallery">', '</ul>']`
- **elements.gallery.child**
Container the datavis placed in. Default: `['<li class="wikipediaThumbnail">', '</li>']`

## Demo
Demo not yet available
190 changes: 190 additions & 0 deletions js/jquery.wikipedia.2.0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
* jQuery Plugin - Wikipedia V2.0
*
* todo: add description here
*
* Autor:
* Version 1.0 : Marcel Grolms - www.suabo.de 2013
* Version 2.0 : Laurens Laman - https://github.com/LauLaman
*
*/
(function($){
//-- declare var in global scope
window.Wikipedia = {};
var Wiki = {
settings: {
showTitle: false,
showInfo: true,
maxThumbnails: 0,
cutFirstInfoTableRows: 0,
maxInfoTableRows: 0,
thumbMaxWidth : '180px',
thumbMaxHeight: '180px',
locale: 'nl',
removeIPA: true,
descriptionLinks: true,
redirect: true,
elements: {
loader : '<div class="ajaxLoading col-lg-12 col-md-12 col-sm-12 col-xs-12">Loading...</div>',
title : ['<div class="wikipediaTitle">', '</div>'],
description : ['<div class="wikipediaDescription col-lg-10 col-md-10 col-sm-10 col-xs-10">', '</div>'],
logo : ['<div class="wikipediaLogo col-lg-2 col-md-2 col-sm-2 col-xs-2">', '</div>'],
gallery: {
parent: ['<ul class="wikipediaThumbGallery">', '</ul>'],
child: ['<li class="wikipediaThumbnail">', '</li>'],
}
}
},
article: {
logo: false,
gallery:[],
info:[]
}
};

Wikipedia = {
strRemove: function(theTarget, theString) {
return $("<div/>").append(
$(theTarget, theString).remove().end()
).html();
},
Get: function(wikipediaPage, options, callback){
//check if pagetitle is set
if(wikipediaPage == undefined) {
console.log('Kein Wikipedia Suchtitel!Keine Abfrage gestartet!');
return;
}
if(options != undefined) {
//we got more options
$.extend(Wiki.settings, options);
}
Wiki.article = {
gallery:[],
info:[]
};

$.getJSON('http://'+Wiki.settings.locale+'.wikipedia.org/w/api.php?action=parse&format=json&callback=?', {page:wikipediaPage, prop:'text|images', uselang: Wiki.settings.locale}, function(parsedata) {
var data = $(parsedata.parse.text["*"]).wrap('<div></div>').parent();
//-- is this a rediredct page?
if(parsedata.parse.text["*"].substring(0,3) == '<p>' || parsedata.parse.text["*"].substring(0,25) == '<div class="redirectMsg">') {
//-- is there only 1 redirection?
if ($(data).find('ul').first().find('li').length==1 && Wiki.settings.redirect) {
var wikipediaPage = $(data).find('a').first().attr('href');
//-- extract page from link
if (parsedata.parse.text["*"].substring(0,25) == '<div class="redirectMsg">') {
wikipediaPage = wikipediaPage.split('title=');
wikipediaPage = wikipediaPage[wikipediaPage.length-1].split('&');
wikipediaPage = wikipediaPage[0];
} else {
wikipediaPage = wikipediaPage.split('/');
wikipediaPage = wikipediaPage[wikipediaPage.length-1];
}
Wikipedia.Get(wikipediaPage, options, callback);
callback = null; //-- prevent callback from firing twice
}

Wiki.article.description = Wikipedia.strRemove("sup", $(data).find('p').first()) + "<br>"+ Wikipedia.strRemove("sup", $(data).find('ul').first());

} else {
Wiki.article.title = $(data).find('.infobox tr:first-child th').text();
if (typeof $(data).find('.infobox .image img').attr('src') !== "undefined") {
Wiki.article.logo = 'http:'+ $(data).find('.infobox .image img').attr('src');
} else {
Wiki.article.logo = false;
}
Wiki.article.ipa = $(data).find('.IPA').text();
Wiki.article.description = $(data).find('p').first();
//-- remove superscript text
Wiki.article.description = Wikipedia.strRemove("sup", Wiki.article.description);
if (Wiki.settings.removeIPA) {
Wiki.article.description = Wikipedia.strRemove(".IPA", Wiki.article.description);
Wiki.article.description = Wiki.article.description.replace('[]', '');
}
if (!Wiki.settings.descriptionLinks) {
Wiki.article.description = $(Wiki.article.description).text();
}
$.each(data.find('.thumb a.image img'), function(index, image){
Wiki.article.gallery.push('http:'+$(image).attr('src'))
});

$.each($(data).find('table.infobox tr'), function(index, element) {
if($(element).has('td')){
Wiki.article.info.push({
name: $(element).find('th').text(),
info: $(element).find('td').text()
});
}
});
}

if (typeof callback == 'function') { // make sure the callback is a function
callback.call(this); // brings the scope to the callback
} else{
return Wiki.article;
}
});
}
};

$.fn.Wikipedia = function(wikipediaPage, options){
var wikiContainer = this;
wikiContainer.html('');
wikiContainer.append(Wiki.settings.elements.loader);
Wikipedia.Get(wikipediaPage, options, function(){
wikiContainer.html('');
if(Wiki.settings.showTitle) {
wikiContainer.append(Wiki.settings.elements.title[0]+Wiki.article.title+Wiki.settings.elements.title[1]);
}

wikiContainer.append(Wiki.settings.elements.description[0]+Wiki.article.description+Wiki.settings.elements.description[1]);
if (Wiki.settings.descriptionLinks) {
$.each($(wikiContainer).find('.wikipediaDescription a'), function(index, element) {
var href = $(element).attr('href');
console.log(element);
if(href.indexOf("#") >= 0){
//-- remove anchor
$(element).parent('li').remove();
} else {
//-- is the link like index.php?title=
if(href.indexOf("php") >= 0){
//-- check if page exist
if ($(element).hasClass('new')){
$(element).replaceWith($(element).text());
} else {
href = href.split('title=');
href = href[href.length-1].split('&');
href = href[0];
}
} else {
href = href.split('/');
href = href[href.length-1];
}
if (href) {
href = 'javascript:$(\''+wikiContainer.selector+'\').Wikipedia(\''+href+'\');';
$(element).attr('href', href);
}
}

});
}

if (Wiki.article.logo && Wiki.article.logo) {
wikiContainer.append(Wiki.settings.elements.logo[0]+'<img src="'+Wiki.article.logo+'">'+Wiki.settings.elements.logo[1]);
}

if (Wiki.settings.maxThumbnails > 0 && Wiki.article.gallery.length > 0) {
var data = '';
$.each(Wiki.article.gallery, function(index, image){
data += Wiki.settings.elements.gallery.child[0]+'<img alt="" src="'+image+'">'+ Wiki.settings.elements.gallery.child[1];
if ((index+1) >= Wiki.settings.maxThumbnails) {
return false;
}
});
$(data).find('img').css({'width': 'auto', 'height': 'auto', 'max-width': Wiki.settings.thumbMaxWidth, 'max-height': Wiki.settings.thumbMaxHeight}).wrap('<li class="wikipediaThumbnail"></li>')
wikiContainer.append(Wiki.settings.elements.gallery.parent[0]+data+Wiki.settings.elements.gallery.parent[1]);
}
});
}


})(jQuery);

0 comments on commit a04efe3

Please sign in to comment.