Difference between revisions of "MediaWiki:Perseids-toolbar.js"

From EAGLE MediaWiki
Jump to navigation Jump to search
m
Line 56: Line 56:
 
else
 
else
 
return null;
 
return null;
  +
}
  +
  +
function getAssociatedProperty(elem) {
  +
// Returns the name of the property, e.g. 'p59'
  +
var theClass = elem.parents('.wb-claim-section').attr('class').split(' ')[1];
  +
return theClass.replace('wb-claim-section-', '');
  +
}
  +
  +
function getAssociatedClaim(elem) {
  +
// Returns claim ID
  +
  +
var claimClass = elem.parents('.wb-statement-claim').find('.wb-claim').attr('class').split(' ')[1];
  +
return claimClass.replace('wb-claim-', '');
 
}
 
}
   
 
function makeEditButton() {
 
function makeEditButton() {
 
var itemId = wb.entity.getId();
 
var itemId = wb.entity.getId();
var claimClass = $(this).parents('.wb-statement-claim').find('.wb-claim').attr('class').split(' ')[1];
 
  +
var claimId = getAssociatedClaim($(this));
var claimId = claimClass.replace('wb-claim-', '');
 
 
 
var langClass = $(this).parents('.wb-claim-section').attr('class').split(' ')[1];
 
  +
var langProp = getAssociatedProperty($(this));
var langProp = langClass.replace('wb-claim-section-', '');
 
 
var langCode = langFromProperty(langProp);
 
var langCode = langFromProperty(langProp);
 
 
Line 74: Line 85:
 
 
 
var itemId = wb.entity.getId();
 
var itemId = wb.entity.getId();
var langClass = $(this).parents('.wb-claim-section').attr('class').split(' ')[1];
 
  +
var langProp = getAssociatedProperty($(this));
var langProp = langClass.replace('wb-claim-section-', '');
 
 
var langCode = langFromProperty(langProp);
 
var langCode = langFromProperty(langProp);
 
 
 
if(langCode) // only for translation claims
 
if(langCode) // only for translation claims
 
$(this).after(generateButton('add', itemId, null, langCode));
 
$(this).after(generateButton('add', itemId, null, langCode));
  +
}
  +
  +
function makeSaveButton() {
  +
var langProp = getAssociatedProperty($(this));
  +
var langCode = langFromProperty(langProp);
  +
  +
if(langCode) // only for translation claims
  +
$(this).after(generateButton('save', null, null, langCode));
 
}
 
}
   
Line 89: Line 107:
 
$( '.wb-claimlist .wb-claim-section > .wb-addtoolbar .wb-editsection a.wikibase-toolbarbutton:first-child')
 
$( '.wb-claimlist .wb-claim-section > .wb-addtoolbar .wb-editsection a.wikibase-toolbarbutton:first-child')
 
.each(makeAddButton);
 
.each(makeAddButton);
 
// Save buttons
 
$( '.wb-claimlist .wb-statement-claim .wikibase-toolbareditgroup-ineditmode .wikibase-toolbar .wikibase-toolbarbutton:first-child')
 
.after(generateButton('save'));
 
   
 
// Quando clicco sul pulsante "edit"...
 
// Quando clicco sul pulsante "edit"...
Line 98: Line 112:
 
// Genero il pulsante "save to Perseids"
 
// Genero il pulsante "save to Perseids"
 
$( '.wb-claimlist .wb-statement-claim .wikibase-toolbareditgroup-ineditmode .wikibase-toolbar:not(:has(.perseids-save)) .wikibase-toolbarbutton:first-child')
 
$( '.wb-claimlist .wb-statement-claim .wikibase-toolbareditgroup-ineditmode .wikibase-toolbar:not(:has(.perseids-save)) .wikibase-toolbarbutton:first-child')
.after(generateButton('save'));
 
  +
.each(makeSaveButton);
 
 
 
// Click su "annulla": rigenero il pulsante "edit"
 
// Click su "annulla": rigenero il pulsante "edit"

Revision as of 06:07, 16 September 2014

function generateButton(type, itemId, claimId, lang) {
	if(type == 'edit') {
		var editUrl = 'http://sosol.perseids.org/sosol/cts_publications/create_from_agent?agent='
			+ 'http://www.eagle-network.eu&id=' + itemId + '&filter=' + claimId;
		
		return $( '<a>' )
			.attr( {
				'class': 'wikibase-toolbarbutton perseids-edit',
				'href': editUrl,
			} )
			.before( '|' )
			.text( 'edit in Perseids' );
	} else if (type == 'save') {
		return $( '<a>' )
			.attr( {
				'class': 'wikibase-toolbarbutton perseids-save',
				'href': 'javascript:void(0);'
			} )
			.before( '|' )
			.text( 'save to Perseids' );
	} else if (type == 'add') {
		var addUrl = 'http://sosol.perseids.org/sosol/cts_publications/create_from_agent?agent='
			+ 'http://www.eagle-network.eu&id=' + itemId + '&lang=' + lang;
		
		return $( '<a>' )
			.attr( {
				'class': 'wikibase-toolbarbutton perseids-add',
				'href': addUrl,
			} )
			.before( '|' )
			.text( 'add via Perseids' );
	}
}

function langFromProperty(prop) {
	// prop: p1, p2, p13, ecc.
	// returns null if the property is not a translation

	var propToLang = {
			'p11': 'en',
			'p13': 'it',
			'p16': 'el',
			'p19': 'hu',
			'p14': 'es',
			'p17': 'ru',
			'p20': 'ro',
			'p57': 'hr',
			'p12': 'de',
			'p15': 'fr',
			'p18': 'si',
			'p61': 'se',
		};
	
	if (prop in propToLang)
		return propToLang[prop];
	else
		return null;
}

function getAssociatedProperty(elem) {
	// Returns the name of the property, e.g. 'p59'
	var theClass = elem.parents('.wb-claim-section').attr('class').split(' ')[1];
	return theClass.replace('wb-claim-section-', '');
}

function getAssociatedClaim(elem) {
	// Returns claim ID
	
	var claimClass = elem.parents('.wb-statement-claim').find('.wb-claim').attr('class').split(' ')[1];
	return claimClass.replace('wb-claim-', '');
}

function makeEditButton() {
	var itemId = wb.entity.getId();
	var claimId = getAssociatedClaim($(this));
	
	var langProp = getAssociatedProperty($(this));
	var langCode = langFromProperty(langProp);
	
	if(langCode) // only for translation claims
		$(this).after(generateButton('edit', itemId, claimId, null));
}

function makeAddButton() {
	
	var itemId = wb.entity.getId();
	var langProp = getAssociatedProperty($(this));
	var langCode = langFromProperty(langProp);
	
	if(langCode) // only for translation claims
		$(this).after(generateButton('add', itemId, null, langCode));
}

function makeSaveButton() {
	var langProp = getAssociatedProperty($(this));
	var langCode = langFromProperty(langProp);
	
	if(langCode) // only for translation claims
		$(this).after(generateButton('save', null, null, langCode));
}

// Edit buttons
$( '.wb-claimlist .wb-statement-claim .wikibase-toolbareditgroup-innoneditmode .wikibase-toolbar .wikibase-toolbarbutton:first-child' )
	.each(makeEditButton);

// Add buttons
$( '.wb-claimlist .wb-claim-section > .wb-addtoolbar .wb-editsection a.wikibase-toolbarbutton:first-child')
	.each(makeAddButton);

// Quando clicco sul pulsante "edit"...
$( '.wb-claimlist .wb-statement-claim .wikibase-toolbareditgroup .wikibase-toolbarbutton:first-child:not(.wikibase-toolbarbutton-disabled)' ).click(function () {
	// Genero il pulsante "save to Perseids"
	$( '.wb-claimlist .wb-statement-claim .wikibase-toolbareditgroup-ineditmode .wikibase-toolbar:not(:has(.perseids-save)) .wikibase-toolbarbutton:first-child')
		.each(makeSaveButton);
	
	// Click su "annulla": rigenero il pulsante "edit"
	$( '.wb-claimlist .wb-statement-claim .wikibase-toolbareditgroup-ineditmode .wikibase-toolbar .wikibase-toolbarbutton:last-child').click(function(){
		$( '.wb-claimlist .wb-statement-claim .wikibase-toolbareditgroup-innoneditmode .wikibase-toolbar:not(:has(.perseids-edit)) .wikibase-toolbarbutton:first-child' )
			.each(makeEditButton);
	});
});