diff --git a/tailbone/diffs.py b/tailbone/diffs.py index 595dbfc9..d4031b1f 100644 --- a/tailbone/diffs.py +++ b/tailbone/diffs.py @@ -2,7 +2,7 @@ ################################################################################ # # Rattail -- Retail Software Framework -# Copyright © 2010-2017 Lance Edgar +# Copyright © 2010-2019 Lance Edgar # # This file is part of Rattail. # @@ -35,7 +35,8 @@ class Diff(object): Core diff class. In sore need of documentation. """ - def __init__(self, old_data, new_data, columns=None, fields=None, render_field=None, render_value=None, monospace=False): + def __init__(self, old_data, new_data, columns=None, fields=None, render_field=None, render_value=None, monospace=False, + extra_row_attrs=None): self.old_data = old_data self.new_data = new_data self.columns = columns or ["field name", "old value", "new value"] @@ -43,6 +44,7 @@ class Diff(object): self._render_field = render_field or self.render_field_default self.render_value = render_value or self.render_value_default self.monospace = monospace + self.extra_row_attrs = extra_row_attrs def make_fields(self): return sorted(set(self.old_data) | set(self.new_data), key=lambda x: x.lower()) @@ -61,6 +63,32 @@ class Diff(object): context['diff'] = self return HTML.literal(render(template, context)) + def get_row_attrs(self, field): + """ + Returns a *rendered* set of extra attributes for the ```` element + for the given field. May be an empty string, or a snippet of HTML + attribute syntax, e.g.: + + .. code-highlight:: none + + class="diff" foo="bar" + + If you wish to supply additional attributes, please define + :attr:`extra_row_attrs`, which can be either a static dict, or a + callable returning a dict. + """ + attrs = {} + if self.values_differ(field): + attrs['class'] = 'diff' + + if self.extra_row_attrs: + if callable(self.extra_row_attrs): + attrs.update(self.extra_row_attrs(field, attrs)) + else: + attrs.update(self.extra_row_attrs) + + return HTML.render_attrs(attrs) + def render_field(self, field): return self._render_field(field, self) diff --git a/tailbone/templates/diff.mako b/tailbone/templates/diff.mako index 0985d74d..3e5ec99e 100644 --- a/tailbone/templates/diff.mako +++ b/tailbone/templates/diff.mako @@ -9,7 +9,7 @@ % for field in diff.fields: - + ${diff.render_field(field)} ${diff.render_old_value(field)} ${diff.render_new_value(field)} diff --git a/tailbone/templates/upgrades/view.mako b/tailbone/templates/upgrades/view.mako index 3b5ce206..e14c7140 100644 --- a/tailbone/templates/upgrades/view.mako +++ b/tailbone/templates/upgrades/view.mako @@ -3,6 +3,7 @@ <%def name="extra_javascript()"> ${parent.extra_javascript()} + % if not use_buefy: + % endif <%def name="render_form_buttons()"> @@ -60,5 +62,13 @@ % endif +<%def name="modify_tailbone_form()"> + + + ${parent.body()} diff --git a/tailbone/views/upgrades.py b/tailbone/views/upgrades.py index 78cbe878..324b23f7 100644 --- a/tailbone/views/upgrades.py +++ b/tailbone/views/upgrades.py @@ -240,24 +240,57 @@ class UpgradeView(MasterView): return filename def render_package_diff(self, upgrade, fieldname): + use_buefy = self.get_use_buefy() try: before = self.parse_requirements(upgrade, 'before') after = self.parse_requirements(upgrade, 'after') + + kwargs = {} + if use_buefy: + kwargs['extra_row_attrs'] = self.get_extra_diff_row_attrs diff = self.make_diff(before, after, columns=["package", "old version", "new version"], render_field=self.render_diff_field, render_value=self.render_diff_value, - ) - showing = HTML.tag('div', - "showing: " - + tags.link_to("all", '#', class_='all') - + " / " - + tags.link_to("diffs only", '#', class_='diffs'), - class_='showing') - return showing + diff.render_html() + **kwargs) + + kwargs = {} + if use_buefy: + kwargs['@click'] = "showingPackages = 'all'" + kwargs[':style'] = "{'font-weight': showingPackages == 'all' ? 'bold' : null}" + else: + kwargs['class_'] = 'all' + all_link = tags.link_to("all", '#', **kwargs) + + kwargs = {} + if use_buefy: + kwargs['@click'] = "showingPackages = 'diffs'" + kwargs[':style'] = "{'font-weight': showingPackages == 'diffs' ? 'bold' : null}" + else: + kwargs['class_'] = 'diffs' + diffs_link = tags.link_to("diffs only", '#', **kwargs) + + kwargs = {} + if not use_buefy: + kwargs['class_'] = 'showing' + showing = HTML.tag('div', c=["showing: " + + all_link + + " / " + + diffs_link], + **kwargs) + + return HTML.tag('div', c=[showing + diff.render_html()]) + except: log.debug("failed to render package diff for upgrade: {}".format(upgrade), exc_info=True) - return "(not available for this upgrade)" + return HTML.tag('div', c="(not available for this upgrade)") + + def get_extra_diff_row_attrs(self, field, attrs): + # note, this is only needed/used with Buefy + extra = {} + if attrs.get('class') != 'diff': + extra['v-show'] = "showingPackages == 'all'" + return extra def changelog_link(self, project, url): return tags.link_to(project, url, target='_blank')