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

From EAGLE MediaWiki
Jump to navigation Jump to search
 
(47 intermediate revisions by 3 users not shown)
Line 1: Line 1:
function generateButton(type) {
 
  +
// For each translation property, this function returns the translation language code.
var pulsanti = {
 
  +
function langFromProperty(prop) {
'edit':
 
  +
// prop: p1, p2, p13, ...
$( '<a>' )
 
  +
// returns null if the property is not a translation
.attr( {
 
  +
'class': 'wikibase-toolbarbutton perseids-edit',
 
  +
var propToLang = {
'href': '#'
+
'p11': 'en',
} )
+
'p13': 'it',
.before( '|' )
+
'p16': 'el',
.text( 'edit in Perseids' )
+
'p19': 'hu',
.click( function ( event ) {
+
'p14': 'es',
event.preventDefault();
+
'p17': 'ru',
} ),
+
'p20': 'ro',
  +
'p57': 'hr',
  +
'p12': 'de',
  +
'p15': 'fr',
  +
'p18': 'si',
  +
'p61': 'se',
  +
};
  +
  +
if (prop in propToLang)
  +
return propToLang[prop];
  +
else
  +
return null;
  +
}
  +
  +
// Returns the name of the property, e.g. 'p59'
  +
function getAssociatedProperty(elem) {
  +
var theClass = elem.parents('.wb-claim-section').attr('class').split(' ')[1];
  +
return theClass.replace('wb-claim-section-', '');
  +
}
  +
  +
// Returns claim ID
  +
function getAssociatedClaim(elem) {
  +
var claimClass = elem.parents('.wb-statement-claim').find('.wb-claim').attr('class').split(' ')[1];
  +
return claimClass.replace('wb-claim-', '');
  +
}
  +
  +
// These functions generate and attach the buttons.
  +
  +
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
  +
return;
  +
  +
var editUrl = 'http://sosol.perseids.org/sosol/cts_publications/create_from_agent?agent='
  +
+ 'http://www.eagle-network.eu&id=' + itemId + '&filter=' + claimId;
  +
  +
var editButton = $( '<a>' )
  +
.attr( {
  +
'class': 'wikibase-toolbarbutton perseids-edit',
  +
'href': editUrl,
  +
} )
  +
.before( '|' )
  +
.text( 'edit in Perseids' );
 
 
'save':
 
  +
$(this).after(editButton);
$( '<a>' )
 
  +
}
.attr( {
 
  +
'class': 'wikibase-toolbarbutton perseids-save',
 
  +
function makeAddButton() {
'href': '#'
 
  +
} )
 
  +
var itemId = wb.entity.getId();
.before( '|' )
 
  +
var langProp = getAssociatedProperty($(this));
.text( 'save to Perseids' )
 
  +
var langCode = langFromProperty(langProp);
.click( function ( event ) {
 
  +
event.preventDefault();
 
  +
if(!langCode) // only for translation claims
} ),
 
  +
return;
  +
  +
var addUrl = 'http://sosol.perseids.org/sosol/cts_publications/create_from_agent?agent='
  +
+ 'http://www.eagle-network.eu&id=' + itemId + '&lang=' + langCode;
  +
  +
var addButton = $( '<a>' )
  +
.attr( {
  +
'class': 'wikibase-toolbarbutton perseids-add',
  +
'href': addUrl,
  +
} )
  +
.before( '|' )
  +
.text( 'add via Perseids' );
 
 
'add':
 
  +
$(this).after(addButton);
$( '<a>' )
 
  +
}
.attr( {
 
  +
'class': 'wikibase-toolbarbutton perseids-add',
 
  +
function makeAddNewButton() {
'href': '#'
 
  +
} )
 
  +
.before( '|' )
 
.text( 'add via Perseids' )
 
.click( function ( event ) {
 
event.preventDefault();
 
} ),
 
};
 
 
 
return pulsanti[type].clone();
 
  +
var itemId = wb.entity.getId();
  +
  +
var addUrl = 'http://sosol.perseids.org/sosol/cts_publications/create_from_agent?agent='
  +
+ 'http://www.eagle-network.eu&id=' + itemId + '&lang=REPLACE_LANG';
  +
  +
var selectBox = $( '<select id="perseids_lang_select" name="perseids_lang_select" onchange="submit_addnew();" style="display:none;">' +
  +
'<option value="">Select a language</option>' +
  +
'<option value="hr">Croatian</option>' +
  +
'<option value="en">English</option>' +
  +
'<option value="fr">French</option>' +
  +
'<option value="de">German</option>' +
  +
'<option value="el">Greek (Modern)</option>' +
  +
'<option value="he">Hebrew</option>' +
  +
'<option value="hu">Hungarian</option>' +
  +
'<option value="it">Italian</option>' +
  +
'<option value="si">Slovenian</option>' +
  +
'<option value="es">Spanish</option>' +
  +
'<option value="se">Swedish</option>' +
  +
'<option value="ro">Romenian</option>' +
  +
'<option value="ru">Russian</option>' +
  +
'</select>');
  +
var addButton = $( '<a>' )
  +
.attr( {
  +
'class': 'wikibase-toolbarbutton perseids-add',
  +
'href': addUrl,
  +
'id': 'perseids-add-new',
  +
'onclick': 'return selectLanguage();'
  +
} )
  +
.before( '|' )
  +
.text( 'add new via Perseids' )
  +
.after(selectBox);
  +
  +
$(this).after(addButton);
 
}
 
}
   
$( '.wikibase-toolbareditgroup-innoneditmode .wikibase-toolbar .wikibase-toolbarbutton:first-child' )
 
  +
function selectLanguage() {
.after(generateButton('edit'));
 
  +
$("#perseids_lang_select").show();
  +
return false;
  +
}
   
$( '.wb-claim-section .wb-addtoolbar .wb-editsection a.wikibase-toolbarbutton:first-child')
 
  +
function submit_addnew() {
.after(generateButton('add'));
 
  +
var lang = $("#perseids_lang_select").val();
  +
if (lang) {
  +
var addurl = $("#perseids-add-new").attr("href");
  +
$ ("#perseids-add-new").attr("href",addurl.replace('REPLACE_LANG',lang));
  +
window.location.replace($("#perseids-add-new").attr("href"));
  +
} else {
  +
$("#perseids-lang-select").hide();
  +
}
  +
}
  +
  +
function makeSaveButton() {
  +
var langProp = getAssociatedProperty($(this));
  +
var langCode = langFromProperty(langProp);
  +
  +
if(!langCode) // only for translation claims
  +
return;
  +
  +
var saveButton = $( '<a>' )
  +
.attr( {
  +
'class': 'wikibase-toolbarbutton perseids-save',
  +
'href': 'javascript:void(0);'
  +
} )
  +
.before( '|' )
  +
.text( 'save to Perseids' );
  +
  +
$(this).after(saveButton);
  +
}
  +
  +
jQuery(document).ready(function() {
  +
// Only displays the buttons if the item has a Trismegistos ID.
  +
if($('.wb-claims .wb-claim-section-p3').length === 0 && $('.wb-claims .wb-claim-section-p69').length === 0 ) {
  +
return;
  +
}
  +
  +
// Edit buttons
  +
$( '.wb-claimlist .wb-statement-claim .wikibase-toolbareditgroup-innoneditmode .wikibase-toolbar .wikibase-toolbarbutton:first-child' )
  +
.each(makeEditButton);
   
$( '.wb-claimlist wb-addtoolbar wb-editsection .wikibase-toolbar .wikibase-toolbar:first-child a.wikibase-toolbarbutton')
 
  +
// Add buttons
.after(generateButton('add'));
 
  +
$( '.wb-claimlist .wb-claim-section > .wb-addtoolbar .wb-editsection a.wikibase-toolbarbutton:first-child')
  +
.each(makeAddButton);
   
$( '.wikibase-toolbareditgroup-ineditmode .wikibase-toolbar .wikibase-toolbarbutton:first-child')
 
  +
// Add new buttons
.after(generateButton('save'));
 
  +
  +
$( '.wb-claimlist .wikibase-toolbar.wb-addtoolbar > .wb-editsection .wikibase-toolbar').last().each(makeAddNewButton);
   
/* Quando clicco sul pulsante "edit"... */
+
// When I click on "edit"...
$( '.wikibase-toolbareditgroup .wikibase-toolbarbutton:first-child:not(.wikibase-toolbarbutton-disabled)' ).click(function () {
+
$( '.wb-claimlist .wb-statement-claim .wikibase-toolbareditgroup .wikibase-toolbarbutton:first-child:not(.wikibase-toolbarbutton-disabled)' ).click(function () {
/* Genero il pulsante "save to Perseids" */
+
// I make the "save to Perseids" button
$( '.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 on "undo": recreates "edit" button
$( '.wikibase-toolbareditgroup-ineditmode .wikibase-toolbar .wikibase-toolbarbutton:last-child').click(function(){
+
$( '.wb-claimlist .wb-statement-claim .wikibase-toolbareditgroup-ineditmode .wikibase-toolbar .wikibase-toolbarbutton:last-child').click(function(){
$( '.wikibase-toolbareditgroup-innoneditmode .wikibase-toolbar:not(:has(.perseids-edit))' ).append(generateButton('edit'));
+
$( '.wb-claimlist .wb-statement-claim .wikibase-toolbareditgroup-innoneditmode .wikibase-toolbar:not(:has(.perseids-edit)) .wikibase-toolbarbutton:first-child' )
  +
.each(makeEditButton);
  +
});
 
});
 
});
 
});
 
});

Latest revision as of 19:03, 7 August 2015

// For each translation property, this function returns the translation language code.
function langFromProperty(prop) {
	// prop: p1, p2, p13, ...
	// 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;
}

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

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

// These functions generate and attach the buttons.

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
		return;
	
	var editUrl = 'http://sosol.perseids.org/sosol/cts_publications/create_from_agent?agent='
		+ 'http://www.eagle-network.eu&id=' + itemId + '&filter=' + claimId;
	
	var editButton = $( '<a>' )
		.attr( {
			'class': 'wikibase-toolbarbutton perseids-edit',
			'href': editUrl,
		} )
		.before( '|' )
		.text( 'edit in Perseids' );
		
	$(this).after(editButton);
}

function makeAddButton() {
	
	var itemId = wb.entity.getId();
	var langProp = getAssociatedProperty($(this));
	var langCode = langFromProperty(langProp);
	
	if(!langCode) // only for translation claims
		return;
	
	var addUrl = 'http://sosol.perseids.org/sosol/cts_publications/create_from_agent?agent='
		+ 'http://www.eagle-network.eu&id=' + itemId + '&lang=' + langCode;
	
	var addButton = $( '<a>' )
		.attr( {
			'class': 'wikibase-toolbarbutton perseids-add',
			'href': addUrl,
		} )
		.before( '|' )
		.text( 'add via Perseids' );
		
	$(this).after(addButton);
}

function makeAddNewButton() {


	
	var itemId = wb.entity.getId();
	
	var addUrl = 'http://sosol.perseids.org/sosol/cts_publications/create_from_agent?agent='
		+ 'http://www.eagle-network.eu&id=' + itemId + '&lang=REPLACE_LANG';
	
        var selectBox = $( '<select id="perseids_lang_select" name="perseids_lang_select" onchange="submit_addnew();" style="display:none;">' + 
           '<option value="">Select a language</option>' +  
           '<option value="hr">Croatian</option>' +
           '<option value="en">English</option>' + 
           '<option value="fr">French</option>' +  
           '<option value="de">German</option>' + 
           '<option value="el">Greek (Modern)</option>' +
           '<option value="he">Hebrew</option>' +  
           '<option value="hu">Hungarian</option>' +
           '<option value="it">Italian</option>' +
           '<option value="si">Slovenian</option>' +  
           '<option value="es">Spanish</option>' +  
           '<option value="se">Swedish</option>' +
           '<option value="ro">Romenian</option>' + 
           '<option value="ru">Russian</option>' + 
           '</select>');
	var addButton = $( '<a>' )
		.attr( {
			'class': 'wikibase-toolbarbutton perseids-add',
                        'href': addUrl,
                        'id': 'perseids-add-new',
                        'onclick': 'return selectLanguage();'
		} )
		.before( '|' )
		.text( 'add new via Perseids' )
                .after(selectBox);
	
	$(this).after(addButton);       
}

function selectLanguage() {
   $("#perseids_lang_select").show();
   return false;
}

function submit_addnew() {
    var lang = $("#perseids_lang_select").val();
    if (lang) {
        var addurl = $("#perseids-add-new").attr("href");
        $ ("#perseids-add-new").attr("href",addurl.replace('REPLACE_LANG',lang));
        window.location.replace($("#perseids-add-new").attr("href"));
    } else {
      $("#perseids-lang-select").hide();
    }
}

function makeSaveButton() {
	var langProp = getAssociatedProperty($(this));
	var langCode = langFromProperty(langProp);
	
	if(!langCode) // only for translation claims
		return;
	
	var saveButton = $( '<a>' )
		.attr( {
			'class': 'wikibase-toolbarbutton perseids-save',
			'href': 'javascript:void(0);'
		} )
		.before( '|' )
		.text( 'save to Perseids' );
		
	$(this).after(saveButton);
}

jQuery(document).ready(function() {
	// Only displays the buttons if the item has a Trismegistos ID.
	if($('.wb-claims .wb-claim-section-p3').length === 0 && $('.wb-claims .wb-claim-section-p69').length === 0 ) {
		return;
	}
	
	// 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);

        // Add new buttons
       
	$( '.wb-claimlist .wikibase-toolbar.wb-addtoolbar > .wb-editsection .wikibase-toolbar').last().each(makeAddNewButton);

	// When I click on "edit"...
	$( '.wb-claimlist .wb-statement-claim .wikibase-toolbareditgroup .wikibase-toolbarbutton:first-child:not(.wikibase-toolbarbutton-disabled)' ).click(function () {
		// I make the "save to Perseids" button
		$( '.wb-claimlist .wb-statement-claim .wikibase-toolbareditgroup-ineditmode .wikibase-toolbar:not(:has(.perseids-save)) .wikibase-toolbarbutton:first-child')
			.each(makeSaveButton);
	
		// Click on "undo": recreates "edit" button
		$( '.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);
		});
	});
});