#!/usr/bin/env python3 # -*- coding:utf-8 -*- from flexx import flx from ipfsdocs.downloadmanager import DownloadManager from ipfsdocs.common import ( get_from_localstorage, set_to_localstorage, ) class State(flx.JsComponent): page = flx.IntProp(0, settable=True) number_per_page = flx.IntProp(25, settable=True) query = flx.StringProp(settable=True) query_results = flx.ListProp(settable=True) query_limit = flx.IntProp(200, settable=True) query_order = flx.StringProp(settable=True) number_of_elements = flx.IntProp(0, settable=True) number_of_pages = flx.IntProp(0, settable=True) number_of_elements_on_this_page = flx.IntProp(0, settable=True) in_last_page = flx.BoolProp(False, settable=True) in_first_page = flx.BoolProp(False, settable=True) selection = flx.DictProp(settable=True) focused_cid = flx.StringProp(settable=True) active_tags = flx.ListProp(settable=True) events = flx.ListProp(settable=True) tags = flx.DictProp(settable=True) current_event_id = flx.IntProp(-1, settable=True) thumbnail_width = flx.IntProp(100, settable=True) state = flx.EnumProp( ["TODO", "NEXT", "DONE", "DELETE"], "TODO", settable=True ) owner = flx.EnumProp( ('AYLAPOMME', 'KONUBINIX', "AYLA"), "AYLAPOMME", settable=True ) from_date = flx.StringProp(settable=True) to_date = flx.StringProp(settable=True) readonly = flx.BoolProp(False, settable=True) autoplay = flx.BoolProp(False, settable=True) random = flx.BoolProp(False, settable=True) @flx.reaction("active_tags") def save_state(self): set_to_localstorage("active_tags", {"tags": self.active_tags}) @flx.reaction("thumbnail_width") def save_thumbnail_width(self): set_to_localstorage("thumbnail_width", {"width": self.thumbnail_width}) @flx.reaction("query_limit") def save_query_limit(self): if self.query_limit < 3000: set_to_localstorage("query_limit", {"limit": self.query_limit}) @flx.reaction("page") def save_page(self): set_to_localstorage("page", {"page": self.page}) @flx.action def add_active_tag(self, key, value): item = (key, value) if item not in self.active_tags: self._mutate_active_tags([item], "insert", len(self.active_tags)) @flx.action def remove_active_tag(self, index): self._mutate_active_tags(1, "remove", index) def init(self): self.downloadmanager = DownloadManager() self.set_active_tags(get_from_localstorage("active_tags")["tags"] or []) self.set_page(get_from_localstorage("page")["page"] or 0) self.set_thumbnail_width(get_from_localstorage("thumbnail_width")["width"] or 200) self.set_query_limit(get_from_localstorage("query_limit")["limit"] or 200) def get_info(cid): return [ info for info in self.root.state.query_results if info["cid"] == cid ][0] @flx.action def add_to_selection(self, *cids): selection = {} for cid in cids: selection[cid] = True self._mutate_selection(selection, "insert") @flx.reaction("query_results") def select_only_things_in_query_result(self): query_cids = [ info["cid"] for info in self.query_results ] selection = {} for cid in self.selection: if cid in query_cids: selection[cid] = True self.set_selection(selection) @flx.action def remove_from_selection(self, cid): self._mutate_selection( [cid], "remove" ) @flx.reaction("number_of_elements", "number_per_page") def update_number_of_pages(self, *events): if self.number_per_page == 0: return global Math self.set_number_of_pages( Math.ceil(self.number_of_elements / self.number_per_page) ) def next_page(self): if self.page == self.number_of_pages - 1: self.goto_first_page() else: self.set_page(self.page + 1) def previous_page(self): if self.page == 0: self.goto_last_page() else: self.set_page(max(self.page - 1, 0)) def goto_last_page(self): if ( self.number_of_pages != 0 and ( self.page != (self.number_of_pages - 1) ) ): self.set_page(self.number_of_pages - 1) def goto_first_page(self): self.set_page(0) @flx.reaction("number_of_elements", "number_per_page", "page") def update_number_of_elements_on_this_page(self, *events): greater_index = ((self.page + 1) * self.number_per_page) - 1 if greater_index <= self.number_of_elements - 1: number = self.number_per_page else: number = self.number_per_page - (greater_index - (self.number_of_elements - 1)) self.set_number_of_elements_on_this_page(number) @flx.reaction("page", "number_of_pages") def update_in_last_page(self): self.set_in_last_page(self.page == (self.number_of_pages - 1)) @flx.reaction("page") def update_in_first_page(self): self.set_in_first_page(self.page == 0)