Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
color = d3.scale.category10()
force = d3.layout.force().gravity(0.05).charge(lambda d, i: 0 if i else -2000)\
.nodes(nodes).size([w, h])
root = nodes[0]
root.radius = 0
root.fixed = True
force.start()
x = d3.select('#' + self.id)
print(x, self.id)
svg = RawJS('x.append("svg").attr("width", w).attr("height", h)')
x = RawJS(
'svg.selectAll("circle").data(nodes.slice(1)).enter().append("circle")')
x.attr("r", lambda d: d.radius).style("fill", lambda d, i: color(i % 3))
def on_tick(e):
q = d3.geom.quadtree(nodes)
i = 0
n = nodes.length
while i < n-1:
i += 1
q.visit(collide(nodes[i]))
svg.selectAll("circle").attr("cx", lambda d: d.x).attr("cy", lambda d: d.y)
force.on("tick", on_tick)
def on_mousemove():
p1 = d3.mouse(self.node)
# Include basic support for multi-touch
touches = {}
for i in range(e.changedTouches.length):
t = e.changedTouches[i]
if t.target is not e.target:
continue
touches[t.identifier] = (float(t.clientX - offset[0]),
float(t.clientY - offset[1]),
t.force)
else:
# Mouse event
pos = float(e.clientX - offset[0]), float(e.clientY - offset[1])
page_pos = e.pageX, e.pageY
# Fix buttons
if e.buttons:
buttons_mask = RawJS(
"e.buttons.toString(2).split('').reverse().join('')"
)
else:
# libjavascriptcoregtk-3.0-0 version 2.4.11-1 does not define
# e.buttons
buttons_mask = [e.button.toString(2)]
buttons = [i+1 for i in range(5) if buttons_mask[i] == '1']
button = {0: 1, 1: 3, 2: 2, 3: 4, 4: 5}[e.button]
touches = {-1: (pos[0], pos[1], 1)} # key must not clash with real touches
# note: our button has a value as in JS "which"
modifiers = [n for n in ('Alt', 'Shift', 'Ctrl', 'Meta')
if e[n.toLowerCase() + 'Key']]
# Create event dict
return dict(pos=pos, page_pos=page_pos, touches=touches,
button=button, buttons=buttons,
def __create_reaction_ob(self, reaction_func, name, mode, connection_strings):
# Keep ref to the reaction function, see comment in create_action().
# Create function that becomes our "reaction object"
def reaction():
return reaction_func.apply(self, arguments) # arguments == events
# Attach methods to the function object (this gets replaced)
REACTION_METHODS_HOOK # noqa
# Init reaction
that = self
RawJS("Component.prototype._REACTION_COUNT += 1")
reaction._id = RawJS("'r' + Component.prototype._REACTION_COUNT")
reaction._name = name
reaction._mode = mode
reaction._ob1 = lambda : that # no weakref in JS
reaction._init(connection_strings, self)
return reaction
elif callable(connection_strings[-1]):
func = connection_strings[-1]
connection_strings = connection_strings[:-1]
else:
raise TypeError('Component.reaction() requires a callable.')
# Verify connection strings
for i in range(len(connection_strings)):
s = connection_strings[i]
if not (isinstance(s, str) and len(s)):
raise ValueError('Connection string must be nonempty strings.')
# Get function name (Flexx sets __name__ on methods)
name = RawJS("func.__name__ || func.name || 'anonymous'")
# name = name.split(' ')[-1].split('flx_')[-1]
nameparts = RawJS("name.split(' ')")
nameparts = RawJS("nameparts[nameparts.length-1].split('flx_')")
name = nameparts[-1]
mode = 'normal'
return self.__create_reaction_ob(func, name, mode, connection_strings)
def __create_reaction_ob(self, reaction_func, name, mode, connection_strings):
# Keep ref to the reaction function, see comment in create_action().
# Create function that becomes our "reaction object"
def reaction():
return reaction_func.apply(self, arguments) # arguments == events
# Attach methods to the function object (this gets replaced)
REACTION_METHODS_HOOK # noqa
# Init reaction
that = self
RawJS("Component.prototype._REACTION_COUNT += 1")
reaction._id = RawJS("'r' + Component.prototype._REACTION_COUNT")
reaction._name = name
reaction._mode = mode
reaction._ob1 = lambda : that # no weakref in JS
reaction._init(connection_strings, self)
return reaction
def __init__(self, *init_args, **property_values):
RawJS('Component.prototype._COUNT += 1')
self._id = RawJS("this.__name__ + Component.prototype._COUNT")
self._disposed = False
# Init some internal variables
self.__handlers = {} # reactions connecting to this component
self.__pending_events = []
self.__anonymous_reactions = []
self.__initial_mutation = False
# Create actions
for i in range(len(self.__actions__)):
name = self.__actions__[i]
self.__create_action(self[name], name)
# Create emitters
for i in range(len(self.__emitters__)):
name = self.__emitters__[i]
self.__handlers[name] = []
def spin(self, n=1):
RawJS("""
var el = window.document.getElementById('flexx-spinner');