__init__.py (10440B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\scriber\widgets\__init__.py 2 from __future__ import print_function 3 import warnings 4 import scriber 5 COLOR_DEFAULT = 0 6 COLOR_PRIMARY = 1 7 COLOR_INFO = 2 8 COLOR_SUCCESS = 3 9 COLOR_WARNING = 4 10 COLOR_DANGER = 5 11 COLOR_INVERT = 6 12 COLOR_TEXT = 7 13 COLOR_CLASSES = {COLOR_DEFAULT: '', 14 COLOR_PRIMARY: 'primary', 15 COLOR_INFO: 'info', 16 COLOR_SUCCESS: 'success', 17 COLOR_WARNING: 'warning', 18 COLOR_DANGER: 'danger', 19 COLOR_INVERT: 'inverse', 20 COLOR_TEXT: 'link'} 21 SIZE_DEFAULT = 0 22 SIZE_MINI = 1 23 SIZE_SMALL = 2 24 SIZE_LARGE = 3 25 SIZE_CLASSES = {SIZE_DEFAULT: '', 26 SIZE_MINI: 'mini', 27 SIZE_SMALL: 'small', 28 SIZE_LARGE: 'large'} 29 FLOAT_NONE = 0 30 FLOAT_LEFT = 1 31 FLOAT_RIGHT = 2 32 FLOAT_CLASSES = {FLOAT_NONE: '', 33 FLOAT_LEFT: 'pull-left', 34 FLOAT_RIGHT: 'pull-right'} 35 BADGE_DEFAULT = 0 36 BADGE_SUCCESS = 1 37 BADGE_WARNING = 2 38 BADGE_IMPORTANT = 3 39 BADGE_INFO = 4 40 BADGE_INVERT = 5 41 BADGE_CLASSES = {BADGE_DEFAULT: '', 42 BADGE_SUCCESS: 'success', 43 BADGE_WARNING: 'warning', 44 BADGE_IMPORTANT: 'important', 45 BADGE_INFO: 'info', 46 BADGE_INVERT: 'inverse'} 47 48 class Label(object): 49 50 @classmethod 51 def get(cls, text, tooltip = '', badge_type = BADGE_DEFAULT): 52 badge_class = '' 53 if badge_type: 54 badge_class = ' label-%s' % BADGE_CLASSES[badge_type] 55 if tooltip: 56 tooltip = ' data-html="true" title="%s"' % tooltip.replace('"', '"') 57 badge_class = '%s ttip' % badge_class 58 return '<span class="label%s"%s>%s</span>' % (badge_class, tooltip, text) 59 60 @classmethod 61 def green(cls, text, tooltip = ''): 62 return cls.get(text, tooltip, badge_type=BADGE_SUCCESS) 63 64 @classmethod 65 def blue(cls, text, tooltip = ''): 66 return cls.get(text, tooltip, badge_type=BADGE_INFO) 67 68 @classmethod 69 def yellow(cls, text, tooltip = ''): 70 return cls.get(text, tooltip, badge_type=BADGE_WARNING) 71 72 @classmethod 73 def red(cls, text, tooltip = ''): 74 return cls.get(text, tooltip, badge_type=BADGE_IMPORTANT) 75 76 @classmethod 77 def black(cls, text, tooltip = ''): 78 return cls.get(text, tooltip, badge_type=BADGE_INVERT) 79 80 81 class Badge(object): 82 83 @classmethod 84 def get(cls, text, tooltip = '', badge_type = BADGE_DEFAULT): 85 badge_class = '' 86 if badge_type: 87 badge_class = ' badge-%s' % BADGE_CLASSES[badge_type] 88 if tooltip: 89 tooltip = ' data-html="true" title="%s"' % tooltip.replace('"', '"') 90 badge_class = '%s ttip' % badge_class 91 return '<span class="badge%s"%s>%s</span>' % (badge_class, tooltip, text) 92 93 @classmethod 94 def green(cls, text, tooltip = ''): 95 return cls.get(text, tooltip, badge_type=BADGE_SUCCESS) 96 97 @classmethod 98 def blue(cls, text, tooltip = ''): 99 return cls.get(text, tooltip, badge_type=BADGE_INFO) 100 101 @classmethod 102 def yellow(cls, text, tooltip = ''): 103 return cls.get(text, tooltip, badge_type=BADGE_WARNING) 104 105 @classmethod 106 def red(cls, text, tooltip = ''): 107 return cls.get(text, tooltip, badge_type=BADGE_IMPORTANT) 108 109 @classmethod 110 def black(cls, text, tooltip = ''): 111 return cls.get(text, tooltip, badge_type=BADGE_INVERT) 112 113 114 class Icon(object): 115 116 @classmethod 117 def get(cls, icon, white = False): 118 if icon: 119 if white: 120 icon = '%s icon-white' % icon 121 return '<i class="%s"></i> ' % icon 122 return '' 123 124 125 class Style(object): 126 127 def __init__(self, icon = None, color = COLOR_DEFAULT, size = SIZE_DEFAULT, float_to = FLOAT_NONE, **kwargs): 128 self._icon = icon 129 self._color = color 130 self._size = size 131 self._float = float_to 132 for k, v in kwargs.iteritems(): 133 setattr(self, '_%s' % k, v) 134 135 self._color_format = '%s' 136 self._size_format = '%s' 137 self._float_format = '%s' 138 139 @property 140 def icon_class(self): 141 if self._icon: 142 white = '' 143 if self.color: 144 white = ' icon-white' 145 return 'icon-%s%s' % (self._icon, white) 146 return '' 147 148 @property 149 def icon(self): 150 if self._icon: 151 return '<i class="%s"></i> ' % self.icon_class 152 return '' 153 154 @property 155 def float(self): 156 print('######## Style.float was just called!') 157 if self._float: 158 return self._float_format % FLOAT_CLASSES[self._float] 159 return '' 160 161 @property 162 def size(self): 163 if self._size: 164 return self._size_format % SIZE_CLASSES[self._size] 165 return '' 166 167 @property 168 def color(self): 169 if self._color: 170 return self._color_format % COLOR_CLASSES[self._color] 171 return '' 172 173 174 class Widget(object): 175 template = 'widgets/test.html' 176 177 def __init__(self): 178 self.style = None 179 180 def _pre_render(self): 181 pass 182 183 def set_style(self, style): 184 if style: 185 if isinstance(style, dict): 186 self.style = Style(**style) 187 elif isinstance(style, Style): 188 self.style = style 189 else: 190 warnings.warn('Widget.set_style got unexpected parameter type: %s => %r' % (type(style), style)) 191 self.style = Style() 192 else: 193 self.style = Style() 194 195 def render(self): 196 self._pre_render() 197 return scriber.scribe(self.template, self.__dict__) 198 199 200 class LinkItem(object): 201 202 def __init__(self, label, action, icon = None): 203 self.label = label 204 self.action = action 205 self._icon = icon 206 207 @property 208 def icon(self): 209 if self._icon: 210 return Icon.get(self._icon) 211 return '' 212 213 214 class ButtonGroup(Widget): 215 template = 'widgets/button_group.html' 216 217 def __init__(self, label, action, item_list = None, style = None): 218 super(ButtonGroup, self).__init__() 219 self.label = label 220 self.action = action 221 self.set_style(style) 222 self.style._color_format = ' btn-%s' 223 self.style._size_format = ' btn-%s' 224 self.style._float_format = ' %s' 225 if item_list: 226 self.item_list = item_list 227 else: 228 self.item_list = [] 229 230 def add_item(self, label, action, icon = None): 231 self.item_list.append(LinkItem(label, action, icon)) 232 233 def add_divider(self): 234 self.item_list.append(LinkItem('', '', '')) 235 236 def _pre_render(self): 237 for i, item in enumerate(self.item_list): 238 if isinstance(item, (list, tuple)): 239 self.item_list[i] = LinkItem(*item) 240 elif isinstance(item, dict): 241 self.item_list[i] = LinkItem(**item) 242 elif not isinstance(item, LinkItem): 243 self.item_list[i] = LinkItem(item, '') 244 245 if self.action.startswith('/') or self.action.startswith('http'): 246 self.action = "go('%s');" % self.action 247 248 @classmethod 249 def get(cls, label, action, item_list, style): 250 return cls(label=label, action=action, item_list=item_list, style=style).render() 251 252 253 class ModalMessage(Widget): 254 template = 'widgets/modal_message.html' 255 256 def __init__(self, title, body = '', dom_id = '', ok_label = 'Ok'): 257 super(ModalMessage, self).__init__() 258 self.title = title 259 self.body = body 260 self.dom_id = dom_id 261 self.ok_label = ok_label 262 263 def _pre_render(self): 264 if isinstance(self.body, (list, tuple)): 265 self.body = '</p><p>'.join(self.body) 266 267 @classmethod 268 def get(cls, title, body = '', dom_id = '', ok_label = 'Ok'): 269 return cls(title, body, dom_id, ok_label).render() 270 271 272 class QuickTable(Widget): 273 template = 'widgets/quick_table.html' 274 DEFAULT_DOM_CLASSES = ['table', 275 'table-condensed', 276 'table-bordered', 277 'table-striped', 278 'table-hover'] 279 280 def __init__(self, dict_or_list_of_lists, dom_id = None, dom_classes = DEFAULT_DOM_CLASSES, dom_style = None, has_header = None, **kwargs): 281 super(QuickTable, self).__init__() 282 self._dom_id = dom_id 283 self._dom_classes = dom_classes 284 self._dom_style = dom_style 285 self._attr_map = kwargs 286 self.data = dict_or_list_of_lists 287 try: 288 if getattr(getattr(dict_or_list_of_lists, '__class__', None), '__name__', None) == 'DBRow': 289 self.data = {} 290 for key in dict_or_list_of_lists.__keys__: 291 self.data[key] = dict_or_list_of_lists[key] 292 293 except Exception: 294 self.data = dict_or_list_of_lists 295 296 self.attributes = '' 297 self.is_vertical = False 298 self.has_header = has_header 299 self.is_list_of_lists = False 300 301 def _pre_render(self): 302 table_attributes = [] 303 if isinstance(self.data, dict): 304 self.is_vertical = True 305 if self.has_header is None: 306 self.has_header = False 307 elif isinstance(self.data, (list, tuple)): 308 if len(self.data) > 0: 309 if isinstance(self.data[0], (list, tuple)): 310 self.is_list_of_lists = True 311 if self.has_header is None: 312 self.has_header = True 313 else: 314 self.is_list_of_lists = False 315 if self.has_header is None: 316 self.has_header = False 317 if self._dom_id: 318 table_attributes.append('id="%s"' % self._dom_id) 319 if self._dom_classes: 320 if isinstance(self._dom_classes, (list, tuple)): 321 table_attributes.append('class="%s"' % ' '.join(self._dom_classes)) 322 else: 323 table_attributes.append('class="%s"' % self._dom_classes) 324 if self._dom_style: 325 if isinstance(self._dom_style, dict): 326 table_attributes.append('style="%s"' % ';'.join([ '%s:%s' % (k, v) for k, v in self._dom_style ])) 327 else: 328 table_attributes.append('style="%s"' % self._dom_style) 329 if self._attr_map: 330 for k, v in self._attr_map: 331 table_attributes.append('%s="%s"' % (k, v)) 332 333 self.attributes = ' %s' % ' '.join(table_attributes) 334 335 @classmethod 336 def get(cls, dict_or_list_of_lists, dom_id = None, dom_classes = DEFAULT_DOM_CLASSES, dom_style = None, has_header = None, **kwargs): 337 return cls(dict_or_list_of_lists, dom_id, dom_classes, dom_style, has_header, **kwargs).render()