User:Csokol/MyScript.js

/***************************************************************** Copyright (C) 2011 Francisco Sokol 

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to

Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor Boston, MA  02110-1301, USA.

function SlideshowFactory(linkId, appendToId, articleTitle, articleId) { //attributes this.linkId = linkId; this.appendToId = appendToId; this.articleTitle = articleTitle; this.articleId = articleId; } //methods SlideshowFactory.prototype.activate = function { api = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + "/api.php"; apiUrl = "http://" + api; getData = { action: "query", titles: this.articleTitle, prop: "imageinfo", generator: "images", iiprop: "url|comment", format: "json" }   $('#'+this.linkId).click(function{        $.ajax({ url: apiUrl, dataType: "json", data: getData, success: function(data){ imagesUrls = []; for (i in data.query.pages) { var url = data.query.pages[i].imageinfo[0].url; var comment = data.query.pages[i].imageinfo[0].comment; imagesUrls.push(url); }               globalSlideshow = new Slideshow(imagesUrls); globalSlideshow.startPresentation; }       });    }) }

SlideshowFactory.prototype.showLink = function { $('#'+this.appendToId).append(" Slideshow "); }

function Slideshow(imageUrls) { this.HTML = ""; this.imagesUrls = imageUrls; this.totalImages = imageUrls.length; this.displayedImage = 0; this.imageIdPrefix = "slideshowimg-"; this.slideshowId = "slideshow" ; this.title = "Slideshow" ; this.dialogHeight = $(window).height * 0.8; this.dialogWidth = $(window).width * 0.8; count = 0; for (i in this.imagesUrls) { url = this.imagesUrls[i]; this.HTML += " "; count++; }   this.HTML = "" + this.HTML + " "; this.startPresentation = function { $('body').append(this.HTML); cssImg = { 'display': 'none', 'margin-left' :'auto', 'margin-right':'auto', 'max-width': this.dialogWidth, 'max-height': this.dialogHeight - (31 + 12 + 46), }       $('#slideshow img').css(cssImg); dialogButtons = [ {               text:"Next", click: function { globalSlideshow.nextImg; }           },            {                text:"Previous", click: function { globalSlideshow.previousImg; }           }        ];        dialogOptions = { title: this.title, height: this.dialogHeight, width: this.dialogWidth, buttons: dialogButtons, }       $('#'+this.slideshowId).dialog(dialogOptions); $('#'+this.slideshowId).dialog('open'); $('#'+this.imageIdPrefix+this.displayedImage).css('display', 'block'); }   this.nextImg = function { if (this.displayedImage == this.totalImages - 1) return; $('#'+this.imageIdPrefix+this.displayedImage).css('display', 'none'); this.displayedImage++; $('#'+this.imageIdPrefix+this.displayedImage).css('display', 'block'); }   this.previousImg = function { if (this.displayedImage == 0) return; $('#'+this.imageIdPrefix+this.displayedImage).css('display', 'none'); this.displayedImage--; $('#'+this.imageIdPrefix+this.displayedImage).css('display', 'block'); } }

var slideshowFactory; var globalSlideshow; mw.loader.load( ['jquery.ui.dialog'] ); $(document).ready(function {   articleId = mw.config.get('wgArticleId');    //if its not a special page    if (articleId!= 0) {        title = mw.config.get('wgTitle');        slideshowFactory = new SlideshowFactory('slideshow_link', 'p-namespaces ul', title, articleId);        slideshowFactory.showLink;        slideshowFactory.activate;    } });