Created
April 23, 2015 23:15
-
-
Save nagareproject/041184e1fcc2f427f591 to your computer and use it in GitHub Desktop.
List of items that can be ordered
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Encoding: utf-8 | |
from nagare import component, presentation | |
class Item(object): | |
def __init__(self, text): | |
self.text = text | |
@presentation.render_for(Item) | |
def render(self, h, *args): | |
return h.span(self.text) | |
class OrderedItems(object): | |
def __init__(self, items): | |
self.items = items | |
def move_up(self, i): | |
self.items.insert(i, self.items.pop(i-1)) | |
def move_down(self, i): | |
self.items.insert(i+1, self.items.pop(i)) | |
@presentation.render_for(OrderedItems) | |
def render(self, h, *args): | |
for i, item in enumerate(self.items): | |
with h.div: | |
with h.pre(style='display: inline'): | |
if i: | |
h << h.a(u'\N{UPWARDS ARROW}').action(lambda i=i: self.move_up(i)) | |
else: | |
h << ' ' | |
h << ' ' | |
if i != len(self.items)-1: | |
h << h.a(u'\N{DOWNWARDS ARROW}').action(lambda i=i: self.move_down(i)) | |
else: | |
h << ' ' | |
h << ' ' << item | |
return h.root | |
def create_app(): | |
items = [component.Component(Item(c * 5)) for c in ('a', 'b', 'c', 'd', 'e')] | |
return component.Component(OrderedItems(items)) | |
app = create_app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment