MediaWiki:Gadget-GlossaryGraph.js: Difference between revisions

Jump to navigation Jump to search
AdminIsidore (talk | contribs)
No edit summary
AdminIsidore (talk | contribs)
No edit summary
 
(11 intermediate revisions by the same user not shown)
Line 7: Line 7:
             if (!pageTitle) {
             if (!pageTitle) {
                 $container.text('Error: No page title');
                 $container.text('Error: No page title');
                console.error('No page title found for container');
                 return;
                 return;
             }
             }


             // Use MediaWiki API to get links
             var term = pageTitle.split(':').pop().toLowerCase();
             new mw.Api().get({
             new mw.Api().get({
                 action: 'query',
                 action: 'askargs',
                 prop: 'links',
                 conditions: 'Category:Glossary Entries',
                 titles: pageTitle,
                 printouts: 'Glossary-Term|Glossary-Definition',
                 pllimit: 'max'
                 parameters: 'limit=50',
                format: 'json',
                formatversion: 2
             }).done(function(data) {
             }).done(function(data) {
                 var pages = data.query.pages;
                console.log('SMW API response:', JSON.stringify(data, null, 2));
                 var pageId = Object.keys(pages)[0];
                 var results = data.query.results || {};
                 var links = pages[pageId].links || [];
                 var links = [];
                 for (var page in results) {
                    if (results.hasOwnProperty(page) && page !== pageTitle) {
                        var definitions = results[page].printouts['Glossary-Definition'] || [];
                        if (definitions.length > 0 && definitions.some(function(def) {
                            return def.toLowerCase().indexOf(term) !== -1;
                        })) {
                            links.push({ title: page });
                        }
                    }
                }
 
                 if (links.length === 0) {
                 if (links.length === 0) {
                     $container.text('No connections found.');
                     $container.text('No connections found.');
                    console.log('No connections found for term:', term);
                     return;
                     return;
                 }
                 }


                // Transform API data into nodes and links
                 var nodes = [{ id: pageTitle }];
                 var nodes = [{ id: pageTitle }];
                 var edges = [];
                 var edges = [];
Line 33: Line 47:
                 });
                 });


                // Clear container and create SVG
                 $container.empty();
                 $container.empty();
                 var width = 400, height = 300;
                 var width = 400, height = 300;
Line 40: Line 53:
                     .attr('width', width)
                     .attr('width', width)
                     .attr('height', height)
                     .attr('height', height)
                     .style('background-color', '#000000'); // Black background for IBM style
                     .style('background-color', '#000000');


                // Force simulation
                 var simulation = d3.forceSimulation(nodes)
                 var simulation = d3.forceSimulation(nodes)
                     .force('link', d3.forceLink(edges).id(d => d.id).distance(100))
                     .force('link', d3.forceLink(edges).id(d => d.id).distance(100))
Line 48: Line 60:
                     .force('center', d3.forceCenter(width / 2, height / 2));
                     .force('center', d3.forceCenter(width / 2, height / 2));


                // Draw links
                 var link = svg.append('g')
                 var link = svg.append('g')
                     .selectAll('line')
                     .selectAll('line')
                     .data(edges)
                     .data(edges)
                     .enter().append('line')
                     .enter().append('line')
                     .attr('stroke', '#00FF00') // Green lines
                     .attr('stroke', '#00FF00')
                     .attr('stroke-width', 2);
                     .attr('stroke-width', 2);


                // Draw nodes
                 var node = svg.append('g')
                 var node = svg.append('g')
                     .selectAll('circle')
                     .selectAll('g')
                     .data(nodes)
                     .data(nodes)
                     .enter().append('circle')
                     .enter().append('g')
                    .attr('r', 10)
                    .attr('fill', '#00CC00') // Darker green nodes
                     .call(d3.drag()
                     .call(d3.drag()
                         .on('start', function(event, d) {
                         .on('start', function(event, d) {
Line 79: Line 87:
                         }));
                         }));


                 // Add tooltips
                 node.append('circle')
                    .attr('r', 10)
                    .attr('fill', '#00CC00');
 
                node.append('text')
                    .attr('dx', 12)
                    .attr('dy', '.35em')
                    .text(d => d.id.split(':').pop()) // Show short term name (e.g., "Dictum")
                    .style('fill', '#00FF00')
                    .style('font-family', 'IBMPlexMono, "Courier New", monospace')
                    .style('font-size', '12px');
 
                 node.append('title').text(d => d.id);
                 node.append('title').text(d => d.id);


                // Update positions on tick
                 simulation.on('tick', function() {
                 simulation.on('tick', function() {
                     link.attr('x1', d => d.source.x)
                     link.attr('x1', d => d.source.x)
Line 88: Line 106:
                         .attr('x2', d => d.target.x)
                         .attr('x2', d => d.target.x)
                         .attr('y2', d => d.target.y);
                         .attr('y2', d => d.target.y);
                     node.attr('cx', d => d.x)
                     node.attr('transform', d => `translate(${d.x},${d.y})`);
                        .attr('cy', d => d.y);
                 });
                 });
             }).fail(function(error) {
             }).fail(function(jqXHR, textStatus, errorThrown) {
                 $container.text('Error: API request failed');
                 $container.text('Error: API request failed');
                console.error('SMW API error details:', {
                    status: textStatus,
                    error: errorThrown,
                    responseText: jqXHR.responseText,
                    statusCode: jqXHR.status,
                    url: 'https://www.ooda.wiki/api.php?action=askargs&conditions=Category:Glossary%20Entries&printouts=Glossary-Term|Glossary-Definition&parameters=limit=50&format=json&formatversion=2'
                });
             });
             });
         });
         });
     });
     });
});
});