User:Simant/common.js/ListFiles.js

/* __NOWYSIWYG__ ';

// insert table into the document $('#s-table, #s-textarea').remove; $('#results-counter').after(sortingTable);

// Event handling for hover image preview $('.s-table-filename-a').parent .mouseover(function {			var $a = $(this).children('a'),				width = $a.attr('data-width'),				height = $a.attr('data-height'),				url = $a.attr('data-url');			var preview = ' ';			$(document.body).append(preview);			$container.css('opacity', '.75');		}) .mouseout(function {			$container.css('opacity', '1');			$('#s-image-hover-preview').remove;		}); }

// rebuilds the sorting textarea using current 'displayParameters' and 'query' function rebuildDisplayTextarea { var separator = displayParameters.rawtextSeparator;

// Sorting textarea var $textarea = $(' ');

// populate the textarea var str = ''; for (var i = 0, len = query.length; i < len; i++) { var q = query[i]; if (displayParameters.filensprefix) { str += ('File:' + q.name); } else { str += q.name; }			if (displayParameters.timestamp) { str += (separator + q.timestamp); }			if (displayParameters.user) { str += (separator + q.user); }			if (displayParameters.mime) { str += (separator + q.mime); }			if (displayParameters.size) { if (displayParameters.approxsize) { str += (separator + getApproxSize(q.size)); } else { str += (separator + q.size); }			}			if (displayParameters.width) { str += (separator + q.width); }			if (displayParameters.height) { str += (separator + q.height); }			str += '\n'; }		$textarea.css('height', (((query.length + 2) * 10) + 'px')).val(str);

// insert table into the document $('#s-table, #s-textarea').remove; $('#results-counter').after($textarea); }

// function that initializes the entire script on pageload function initListFilesForm { // set container $container = $('#ListFiles-container');

// do nothing if container doesn't exist if ($container.length === 0) { var index = mw.config.get('wgPageName').indexOf('ListFiles'); var wgNamespaceNumber = mw.config.get('wgNamespaceNumber'); if ((index !== -1) && (wgNamespaceNumber === 2 || wgNamespaceNumber === 4)) { $container = $('#mw-content-text'); } else { return; }		}

// remove old stylesheet and interface $('#ListFiles-stylesheet').remove; $container.children.remove; $container.text('');

// create stylesheet var qostyle = '#settings-save,#settings-reset{margin-left:100px;}#settings-save a, #settings-reset a{cursor:pointer;}#qo-table{width:100%;margin-top:30px;}#qo-table input[type="text"],#qo-table div{width:99%;}#qo-table label{font-family:monospace;}#qo-title{font-size:150%;}#qo-table td{padding:5px;width:33%;}#qo-descending{margin-left:10px;}#qo-query{float:right;}#qo-inner-table td{border:0;}';

var dstyle = '#d-table{width:100%;margin-top:30px;}#d-title{font-size:150%;}#d-table td{padding:5px 5px 5px 25px;}.margin-left-20,.d-radio,#d-sorting-property{margin-left:20px;}';

var sstyle = '#results-counter{margin-top:30px;width:100%;text-align:center;font-weight:bold;}#s-table{table-layout:fixed;width:100%;margin-top:30px;word-wrap:break-word;}#s-textarea{width:99%;margin-top:30px;white-space:nowrap;overflow-x:auto;overflow-y:auto;}';

$(document.head).append(('' + '#WikiaMainContent{width:1010px;}#WikiaRail{display:none;}' + qostyle + dstyle + sstyle + ' '));

// initialize private properties: query, baseURL, urlParameters and displayParameters query = []; baseURL = mw.config.get('wgServer') + '/api.php?action=query&list=allimages'; initURLandDisplayParams;

// Dev Info: script-dev-div var devInfo = ' w:c:dev:ListFiles'; if (window.localStorage) { devInfo += (' [Save current settings] '				+ ' [Reset to default settings] '); }		devInfo += ' ';

// Query Options: qo-table var queryOptions = ' ' + ' Query  ' + ' ';

// Display Options: d-table var displayOptions = ' ';

// insert HTML into the document $container.append(devInfo + queryOptions + displayOptions);

// set private properties: jQuery objects for attaching event handlers $aifrom = $('#qo-aifrom'); $aito = $('#qo-aito'); $ascending = $('#qo-ascending'); $descending = $('#qo-descending'); $aiprefix = $('#qo-aiprefix'); $aiminsize = $('#qo-aiminsize'); $aimaxsize = $('#qo-aimaxsize'); $ailimit = $('#qo-ailimit'); $query = $('#qo-query'); // finish setting form state //$('#qo-table, #d-table').makeCollapsible; initSavedFormState; rebuildResults;

// initialize events initQueryEvents; initDisplayEvents; }	// if purgeLocalStorage, deletes urlParameters and displayParameters from // local storage and sets them to their default values. // Otherwise, initializes urlParameters and displayParameters // to values in localStorage. function initURLandDisplayParams(purgeLocalStorage) { var u, d;		if (purgeLocalStorage && window.localStorage) { window.localStorage.removeItem('ListFiles-urlParameters'); window.localStorage.removeItem('ListFiles-displayParameters'); } else if (window.localStorage) { u = window.localStorage.getItem('ListFiles-urlParameters'); d = window.localStorage.getItem('ListFiles-displayParameters'); }		// initialize urlParameters if (u) { urlParameters = JSON.parse(u); } else { urlParameters = { ailimit: 100, aiprop: 'timestamp|user|url|size|mime' };		}		// initialize displayParameters if (d) { displayParameters = JSON.parse(d); } else { displayParameters = { timestamp: 'checked', user: 'checked', mime: 'checked', size: 'checked', width: 'checked', height: 'checked', rawtextSeparator: '\t', sortingDirection: -1, sortingProperty: 'name' };		}	}

// initializes form state from the current urlParameters and displayParameters function initSavedFormState { // Set form to using current urlParameters if (urlParameters.aidir) { $descending.attr('checked', 'checked'); $ascending.removeAttr('checked'); } else { $ascending.attr('checked', 'checked'); $descending.removeAttr('checked'); }		$ailimit.children.removeAttr('selected'); $ailimit.children(('[value="' + urlParameters.ailimit + '"]')).attr('selected', 'selected'); $aifrom.val((urlParameters.aifrom || '')); $aito.val((urlParameters.aito || '')); $aiprefix.val((urlParameters.aiprefix || '')); $aiminsize.val((urlParameters.aiminsize || '')); $aimaxsize.val((urlParameters.aimaxsize || '')); setURL;

// Set form to using current displayParameters $('.d-checkbox').each(function {			var $this = $(this);			var dparam = $this.attr('dparam');			if (displayParameters[dparam]) {				$this.attr('checked', 'checked');			} else {				$this.removeAttr('checked');			}		}); if (displayParameters.sortingDirection === -1) { $('#d-ascending').attr('checked', 'checked'); $('#d-descending').removeAttr('checked'); } else { $('#d-descending').attr('checked', 'checked'); $('#d-ascending').removeAttr('checked'); }		$('#d-sorting-property').children.removeAttr('selected'); $('#d-sorting-property').children(('[value="' + displayParameters.sortingDirection + '"]')).attr('selected', 'selected'); }

// initializes event handlers for Query Options after the form has been constructed function initQueryEvents { // Event handling for settings save link $('#settings-save').click(function {			window.localStorage.setItem('ListFiles-urlParameters', JSON.stringify(urlParameters));			window.localStorage.setItem('ListFiles-displayParameters', JSON.stringify(displayParameters));		});

// Event handling for settings reset link $('#settings-reset').click(function {			initURLandDisplayParams(true);			initSavedFormState;			rebuildResults;		});

// Event handling for aidir radio buttons $ascending.change(function {			$descending.attr('checked', false);			delete urlParameters.aidir;			setURL;		}); $descending.change(function {			$ascending.attr('checked', false);			urlParameters.aidir = "descending";			setURL;		});

// Event handling for aifrom $aifrom.change(function {			var temp = $(this).val;			if (temp.length > 0) {				urlParameters.aifrom = window.encodeURIComponent(temp.replace(/ /g, '_'));			} else {				delete urlParameters.aifrom;			}			setURL;		});

// Event handling for aito $aito.change(function {			var temp = $(this).val;			if (temp.length > 0) {				urlParameters.aito = window.encodeURIComponent(temp.replace(/ /g, '_'));			} else {				delete urlParameters.aito;			}			setURL;		});

// Event handling for aiprefix $aiprefix.change(function {			var temp = $(this).val;			if (temp.length > 0) {				urlParameters.aiprefix = window.encodeURIComponent(temp.replace(/ /g, '_'));			} else {				delete urlParameters.aiprefix;			}			setURL;		});

// Event handling for aiminsize $aiminsize.change(function {			var temp = window.parseInt($(this).val);			if (window.isNaN(temp) || typeof temp !== "number") {				delete urlParameters.aiminsize;			} else {				urlParameters.aiminsize = temp;			}			setURL;		});

// Event handling for aimaxsize $aimaxsize.change(function {			var temp = window.parseInt($(this).val);			if (window.isNaN(temp) || typeof temp !== "number") {				delete urlParameters.aimaxsize;			} else {				urlParameters.aimaxsize = temp;			}			setURL;		});

// Event handling for ailimit dropdown $ailimit.change(function {			var temp = window.parseInt($(this).val);			if (window.isNaN(temp) || typeof temp !== "number") {				delete urlParameters.ailimit;			} else {				urlParameters.ailimit = temp;			}			setURL;		});

// Event handling for query button $query.click(function {			$(this).attr('disabled', 'disabled');			$.getJSON((url + '&format=json'), apiCall);		}); }

// initializes event handlers for Display Options after the form has been constructed function initDisplayEvents { // Event handling for sorting direction radio buttons $('#d-ascending').change(function {			$('#d-descending').attr('checked', false);			displayParameters.sortingDirection = -1;			rebuildResults;		}); $('#d-descending').change(function {			$('#d-ascending').attr('checked', false);			displayParameters.sortingDirection = 1;			rebuildResults;		});

// Event handling for sorting property dropdown $('#d-sorting-property').change(function {			displayParameters.sortingProperty = $(this).children(':selected').attr('value');			rebuildResults;		});

// Event handling for all display checkboxes $('.d-checkbox').change(function {			var $this = $(this);			var dparam = $this.attr('dparam');			if ($this.is(':checked')) {				// action to take if checked				displayParameters[dparam] = 'checked';

// get rawtextSeparator if applicable if (dparam === 'rawtext') { displayParameters.rawtextSeparator = window.prompt('When results are concatenated in raw text, they are separated by a certain string or character. Please choose your separator string. Note: the default separator is a single \'tab\' character.', '\t'); }			} else { // action to take if NOT checked delete displayParameters[dparam]; }			rebuildResults; });	}

// Main form initialization sequence if (mw.config.get('wgAction') !== 'edit') { $(initListFilesForm); }

var wgCanonicalSpecialPageName = mw.config.get('wgCanonicalSpecialPageName'); // Special:ListFiles modification - adds a raw list of filenames uploaded by given user if (wgCanonicalSpecialPageName === "Listfiles") { $(function {			var filenames = "",				count = 0,				$table = $('.listfiles'),				$tr;

// set filenames $table.find('.TablePager_col_img_name').children('a:even').each(function {				filenames += $(this).attr('title') + "\n";				count++;			}); $tr = $('Here is a raw list of the ' + count + ' filename(s) currently shown on this page, provided by the ListFiles</a> script on the Wikia Developers Wiki. <textarea id="filenames-list-textarea" style="width:95%; height:150px;"> '); $tr.find('#filenames-list-textarea').val(filenames);

// insert new table row $('#filenames-list-tr').remove; // prevent duplicates $table.children('tbody').prepend($tr); });	} else if (wgCanonicalSpecialPageName === "Unusedimages" || wgCanonicalSpecialPageName === "UnusedVideos" || wgCanonicalSpecialPageName === "Uncategorizedimages") {	// Special:UnusedFiles, Special:UnusedVideos, Special:UncategorizedImages raw text list		$(function { var filenames = "", count = 0, $container; // set filenames $('.gallerytext > a').each(function {				filenames += this.title + "\n";				count++;			}); $container = $(' Here is a raw list of the ' + count + ' filename(s) currently shown on this page, provided by the ListFiles</a> script on the Wikia Developers Wiki. <textarea id="filesnames-list-textarea" style="width: 95%; height: 150px"> '); $container.children('#filesnames-list-textarea').val(filenames); // insert container $('#filesnames-list-container').remove; // prevent duplicates $('.gallery').before($container); });	} }(jQuery, mediaWiki, window));

/* */