1#!/usr/local/bin/python3.8
2#
3# Copyright (C) 2009 Aurelio A. Heckert, aurium (a) gmail dot com
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18#
19"""
20This effect adds a feature visible (or usable) only on a SVG enabled web
21browser (like Firefox). This effect sets one or more attributes in the second
22selected element, when a defined event occurs on the first selected element.
23If you want to set more than one attribute, you must separate this with a space,
24and only with aspace.
25"""
26
27# local library
28import inkex
29from inkex.localization import inkex_gettext as _
30import inkwebeffect
31
32class SetAttribute(inkwebeffect.InkWebEffect):
33    """Set a web attribute accross many objects"""
34    def add_arguments(self, pars):
35        pars.add_argument("--tab", help="The selected UI-tab when OK was pressed")
36        pars.add_argument("--att", default="fill", help="Attribute to set.")
37        pars.add_argument("--val", default="red", help="Values to set.")
38        pars.add_argument("--when", default="onclick", help="When it must to set?")
39        pars.add_argument("--from-and-to", dest="from_and_to", default="g-to-one")
40        pars.add_argument("--compatibility", default="append",
41                          help="Compatibility with previews code to this event.")
42
43    def effect(self):
44        self.ensureInkWebSupport()
45
46        if len(self.options.ids) < 2:
47            raise inkex.AbortExtension(_("You must select at least two elements."))
48
49        # All set the last else The first set all
50        split = -1 if self.options.from_and_to == "g-to-one" else 1
51        el_from = list(self.svg.selected.values())[:split]
52        id_to = list(self.svg.selected.ids)[split:]
53
54        ev_code = "InkWeb.setAtt({{el:['{}'], att:'{}', val:'{}'}})".format(
55            "','".join(id_to), self.options.att, self.options.val)
56        for elem in el_from:
57            prev_ev_code = elem.get(self.options.when)
58            if prev_ev_code is None:
59                prev_ev_code = ""
60
61            if self.options.compatibility == 'append':
62                el_ev_code = prev_ev_code + ";\n" + ev_code
63            if self.options.compatibility == 'prepend':
64                el_ev_code = ev_code + ";\n" + prev_ev_code
65            if self.options.compatibility == 'replace':
66                el_ev_code = ev_code
67
68            elem.set(self.options.when, el_ev_code)
69
70if __name__ == '__main__':
71    SetAttribute().run()
72