User:Egon Willighagen/cdkdepict gadget.js

From Wikidata
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/*
 This gadget displays SMILES, CXSMILES, and SMARTS renderings for chemical compounds,
 and chemical compound classes,  where the item contains statements for P233 and
 P2017 for SMILES, P10718 for CXSMILES, and P8533 for SMARTS.

 To add this gadget, put the following line on your common.js User subpage:

 importScript( 'User:Egon_Willighagen/cdkdepict_gadget.js' );

 Inspired by Magnus's SMILES gadget: https://www.wikidata.org/wiki/User:Magnus_Manske/smiles_gadget.js

 NOTE: This script uses the online CDK Depict service, https://cdkdepict.toolforge.org/,
 hosted on Toolforge
 
 If wished, cdkdepict.toolforge.org can be replaced with www.simolecule.com/cdkdepict/
 or cdkdepict.cloud.vhp4safety.nl (by the Dutch VHP4Safety project, https://vhp4safety.nl/)
 
 Information, source code, Docker information about CDK Depict can be found at https://github.com/cdk/depict
*/

function render(prop) {
	$('#'+prop+' div.wikibase-statementview').each ( function ( dummy , element ) {
		smiles_string = $(element).find('div.wikibase-snakview-value').eq(0).text();
		url = 'https://cdkdepict.toolforge.org/depict/bot/svg?smi=' + encodeURIComponent(smiles_string);
		html = '<img src="'+url+'" style="width: 300px; height: 200px;" />'
		$(element).append(html);
	} ) ;
}

function renderWithCIP(prop) {
	$('#'+prop+' div.wikibase-statementview').each ( function ( dummy , element ) {
		smiles_string = $(element).find('div.wikibase-snakview-value').eq(0).text();
		url = 'https://cdkdepict.toolforge.org/depict/bot/svg?annotate=cip&smi=' + encodeURIComponent(smiles_string);
		html = '<img src="'+url+'" style="width: 300px; height: 200px;" />'
		$(element).append(html);
	} ) ;
}

if ( $('#P233, #P2017, #P10718, #P8533').length>0 ) {
	$('canvas.smiles_canvas').remove();
	render('P233');
	renderWithCIP('P2017');
	renderWithCIP('P8533');
	renderWithCIP('P10718');
}