Coverage for src/ipyvizzustory/storylib/template.py: 100%

6 statements  

« prev     ^ index     » next       coverage.py v7.5.4, created at 2024-07-10 10:22 +0000

1"""A module for storing the `HTML` templates.""" 

2 

3 

4VIZZU_STORY: str = ( 

5 "https://cdn.jsdelivr.net/npm/vizzu-story@0.7/dist/vizzu-story.min.js" 

6) 

7"""A variable for storing the default url of the `vizzu-story` package.""" 

8 

9DISPLAY_INDENT: str = " " 

10"""A variable for storing the default indent in the `HTML` template.""" 

11 

12DISPLAY_TEMPLATE: str = """ 

13<div> 

14 <vizzu-player id="{id}" {vizzu} {start_slide} controller></vizzu-player> 

15 <script type="module"> 

16 import VizzuPlayer from "{vizzu_story}"; 

17 

18 class IpyvizzuStory {{ 

19 static version = "{version}"; 

20 static analytics = undefined; 

21 

22 static changeAnalyticsTo(analytics) {{ 

23 if (IpyvizzuStory.analytics !== analytics) {{ 

24 console.log("ipyvizzu-story gather usage stats:", analytics); 

25 IpyvizzuStory.analytics = analytics; 

26 }} 

27 if (analytics) {{ 

28 IpyvizzuStory._addHeadScript(); 

29 }} else {{ 

30 IpyvizzuStory._removeScript("ipyvizzu-story-analytics-head"); 

31 }} 

32 }} 

33 

34 static _addHeadScript() {{ 

35 const scriptId = "ipyvizzu-story-analytics-head"; 

36 if (!IpyvizzuStory._isScriptAppended(scriptId)) {{ 

37 const script = document.createElement("script"); 

38 script.defer = true; 

39 script.src = "https://plausible.io/js/script.local.js"; 

40 script.dataset.domain = "usage.ipyvizzu-story.com"; 

41 script.id = scriptId; 

42 document.getElementsByTagName("head")[0].appendChild(script); 

43 }} 

44 }} 

45 

46 static _isScriptAppended(id) {{ 

47 return document.querySelector(`script[id="${{id}}"]`) !== null; 

48 }} 

49 

50 static _removeScript(id) {{ 

51 const script = document.getElementById(id); 

52 if (script) script.remove(); 

53 }} 

54 }} 

55 

56 if (IpyvizzuStory.version !== window.IpyvizzuStory?.version) {{ 

57 window.IpyvizzuStory = IpyvizzuStory; 

58 console.log(`ipyvizzu-story ${{IpyvizzuStory.version}}`); 

59 }} 

60 

61 window.IpyvizzuStory?.changeAnalyticsTo({analytics}); 

62 

63 class Plugins {{ 

64 static _resolveVizzuVersion(vp) {{ 

65 const url = vp.vizzuUrl; 

66 const versionMatch = url.match(/vizzu@([^\\/]+)\\//); 

67 return versionMatch[1]; 

68 }} 

69 

70 static _resolveUrl(plugin, tag) {{ 

71 if (!plugin.includes('/')) {{ 

72 const jsdelivr = "https://cdn.jsdelivr.net/npm/@vizzu"; 

73 return `${{jsdelivr}}/${{plugin}}@${{tag}}/dist/mjs/index.min.js`; 

74 }} 

75 return plugin; 

76 }} 

77 

78 static register(vp, chart, plugins) {{ 

79 const tag = `vizzu-${{Plugins._resolveVizzuVersion(vp)}}`; 

80 const pluginsRegistered = []; 

81 for (const plugin of plugins) {{ 

82 const pluginUrl = Plugins._resolveUrl(plugin.plugin, tag); 

83 const pluginRegistered = import(pluginUrl).then(pluginModule => {{ 

84 const pluginInstance = new pluginModule[plugin.name](plugin.options); 

85 chart.feature(pluginInstance, true); 

86 }}).catch((error) => {{ 

87 console.error('Error importing plugin:', pluginUrl, error) 

88 }}); 

89 pluginsRegistered.push(pluginRegistered); 

90 }} 

91 return Promise.all(pluginsRegistered); 

92 }} 

93 }} 

94 

95 const vp = document.getElementById("{id}"); 

96 vp.initializing.then(chart => {{ 

97 const lib = vp.Vizzu; 

98 

99 // story.set_size() 

100 {chart_size} 

101 

102 // story.add_plugin() 

103 const plugins = []; 

104 {chart_plugins} 

105 Plugins.register(vp, chart, plugins).then(() => {{ 

106 // story.set_feature() 

107 {chart_features} 

108 // story.add_event() 

109 {chart_events} 

110 

111 const vizzuPlayerData = {vizzu_player_data}; 

112 vp.slides = vizzuPlayerData; 

113 }}); 

114 }}); 

115 </script> 

116</div> 

117""" 

118"""A variable for storing the `vizzu-story` `HTML` template."""