“MediaWiki:Gadget-ECharts.js”的版本间的差异
来自小鱼君和他的朋友们
([InPageEdit] 没有编辑摘要) |
([InPageEdit] 没有编辑摘要) |
||
第2行: | 第2行: | ||
* @name WikiECharts | * @name WikiECharts | ||
* @desc Provides the function of inserting | * @desc Provides the function of inserting | ||
− | * | + | * ECharts table into the wiki page. |
*/ | */ | ||
!(function(window, $) { | !(function(window, $) { | ||
+ | // @function loadScript | ||
+ | var loadScript = function(url) { | ||
+ | return $.ajax({ | ||
+ | url: url, | ||
+ | dataType: 'script', | ||
+ | cache: true | ||
+ | }) | ||
+ | } | ||
+ | |||
// Get blocks | // Get blocks | ||
var $blocks = $('.ECharts, .Echarts, .echarts') | var $blocks = $('.ECharts, .Echarts, .echarts') | ||
if ($blocks.length > 0) { | if ($blocks.length > 0) { | ||
− | $. | + | // Load dependencies |
− | + | // These are trusted official CDN | |
− | + | $.when( | |
− | + | // Apache ECharts | |
− | + | loadScript('https://unpkg.com/echarts'), | |
+ | // JSON5: https://github.com/json5/json5 | ||
+ | loadScript('https://unpkg.com/json5') | ||
+ | ).then(function() { | ||
$blocks.each(function(index, item) { | $blocks.each(function(index, item) { | ||
var $this = $(item) | var $this = $(item) | ||
第21行: | 第33行: | ||
// Try to parse JSON | // Try to parse JSON | ||
try { | try { | ||
− | option = | + | // Parse JSON with JSON5 { foo: 'bar' } -> { "foo": "bar" } |
+ | option = JSON5.parse(text) | ||
} catch (e) { | } catch (e) { | ||
$this.append( | $this.append( | ||
第47行: | 第60行: | ||
}) | }) | ||
} | } | ||
− | })(window, jQuery) | + | })(window, jQuery); |
2023年5月6日 (六) 17:50的最新版本
/** * @name WikiECharts * @desc Provides the function of inserting * ECharts table into the wiki page. */ !(function(window, $) { // @function loadScript var loadScript = function(url) { return $.ajax({ url: url, dataType: 'script', cache: true }) } // Get blocks var $blocks = $('.ECharts, .Echarts, .echarts') if ($blocks.length > 0) { // Load dependencies // These are trusted official CDN $.when( // Apache ECharts loadScript('https://unpkg.com/echarts'), // JSON5: https://github.com/json5/json5 loadScript('https://unpkg.com/json5') ).then(function() { $blocks.each(function(index, item) { var $this = $(item) var option = {} var text = $this.text() $this.html('') // Try to parse JSON try { // Parse JSON with JSON5 { foo: 'bar' } -> { "foo": "bar" } option = JSON5.parse(text) } catch (e) { $this.append( $('<pre>', { class: 'error', html: 'ECharts options parse error:\n' + e }) ) return console.warn('ECharts options parse error', item, e) } // Init Chart var thisChart = echarts.init($this.get(0)) thisChart.setOption(option) // Resize Chart thisChart.resize() $(window).on('resize', function() { thisChart.resize() }) $('.mw-collapsible-toggle, .pi-section-tab').click(function() { thisChart.resize() }) }) }) } })(window, jQuery);