From 365f7dbf0c222d04a84eef17d0cadbe393e99fbf Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Tue, 10 Apr 2012 11:29:30 +0200 Subject: [PATCH] Change js inclusion, starting to extract js lib --- pygal/config.py | 20 ++++++++++++++++++-- pygal/js/graph.coffee | 4 ++-- pygal/js/graph.js | 4 ++-- pygal/style.py | 10 ++++++++++ pygal/svg.py | 25 ++++++++++++++----------- 5 files changed, 46 insertions(+), 17 deletions(-) diff --git a/pygal/config.py b/pygal/config.py index 0057f80..6404ba9 100644 --- a/pygal/config.py +++ b/pygal/config.py @@ -21,7 +21,7 @@ Config module with all options """ - +import os from pygal.style import DefaultStyle @@ -43,7 +43,12 @@ class Config(object): #: If set to a filename, this will replace the default css base_css = None #: or default js - base_js = None + included_js = [os.path.join(os.path.dirname(__file__), 'js', 'graph.js')] + external_js = [ + # 'http://code.jquery.com/jquery.min.js', + # 'http://keith-wood.name/js/jquery.svg.js', + # 'http://keith-wood.name/js/jquery.svgdom.js' + ] #: Style holding values injected in css style = DefaultStyle #: Various font sizes @@ -120,3 +125,14 @@ class Config(object): ('%dpx' % getattr(self, name) ) if with_unit else getattr(self, name)) return fs + + def to_dict(self): + config = {} + for attr in dir(self): + if not attr.startswith('__'): + value = getattr(self, attr) + if hasattr(value, 'to_dict'): + config[attr] = value.to_dict() + elif not hasattr(value, '__call__'): + config[attr] = value + return config diff --git a/pygal/js/graph.coffee b/pygal/js/graph.coffee index 464664e..b250d57 100644 --- a/pygal/js/graph.coffee +++ b/pygal/js/graph.coffee @@ -2,8 +2,8 @@ _ = (x) -> document.querySelectorAll(x) __ = (x) -> document.getElementById(x) padding = 5 tooltip_timeout = 0 -tooltip_font_size = parseInt("{{ font_sizes.tooltip }}") -anim_steps = parseInt("{{ animation_steps }}") +tooltip_font_size = @config.tooltip_font_size +anim_steps = @config.animation_steps class Queue constructor: (@delay) -> diff --git a/pygal/js/graph.js b/pygal/js/graph.js index 7ee3c2e..cef934c 100644 --- a/pygal/js/graph.js +++ b/pygal/js/graph.js @@ -15,9 +15,9 @@ tooltip_timeout = 0; - tooltip_font_size = parseInt("{{ font_sizes.tooltip }}"); + tooltip_font_size = this.config.tooltip_font_size; - anim_steps = parseInt("{{ animation_steps }}"); + anim_steps = this.config.animation_steps; Queue = (function() { diff --git a/pygal/style.py b/pygal/style.py index 1f1e1fb..b06a6ec 100644 --- a/pygal/style.py +++ b/pygal/style.py @@ -59,6 +59,16 @@ class Style(object): '}}\n'.format(*tupl)) return '\n'.join(map(color, enumerate(self._colors))) + def to_dict(self): + config = {} + for attr in dir(self): + if not attr.startswith('__'): + value = getattr(self, attr) + if not hasattr(value, '__call__'): + config[attr] = value + return config + + DefaultStyle = Style(opacity_hover='.4', opacity='.8') LightStyle = Style( background='white', diff --git a/pygal/svg.py b/pygal/svg.py index 9c95e83..a3f3323 100644 --- a/pygal/svg.py +++ b/pygal/svg.py @@ -24,6 +24,7 @@ Svg helper from __future__ import division import io import os +import json from lxml import etree from math import cos, sin, pi from pygal.util import template, coord_format @@ -64,16 +65,19 @@ class Svg(object): hidden='y' if self.graph.horizontal else 'x') style.text = templ - def add_script(self, js): + def add_scripts(self): """Add the js to the svg""" - script = self.node(self.defs, 'script', type='text/javascript') - with io.open(js, encoding='utf-8') as f: - templ = template( - f.read(), - font_sizes=self.graph.font_sizes(False), - animation_steps=self.graph.animation_steps - ) - script.text = templ + common_script = self.node(self.defs, 'script', type='text/javascript') + common_script.text = " = ".join( + ("window.config", json.dumps(self.graph.config.to_dict()))) + + for external_js in self.graph.external_js: + self.node( + self.defs, 'script', type='text/javascript', href=external_js) + for included_js in self.graph.included_js: + script = self.node(self.defs, 'script', type='text/javascript') + with io.open(included_js, encoding='utf-8') as f: + script.text = f.read() def node(self, parent=None, tag='g', attrib=None, **extras): """Make a new svg node""" @@ -167,8 +171,7 @@ class Svg(object): """Last things to do before rendering""" self.add_style(self.graph.base_css or os.path.join( os.path.dirname(__file__), 'css', 'graph.css')) - self.add_script(self.graph.base_js or os.path.join( - os.path.dirname(__file__), 'js', 'graph.js')) + self.add_scripts() self.root.set( 'viewBox', '0 0 %d %d' % (self.graph.width, self.graph.height)) if self.graph.explicit_size: