| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import hash_literals |
| 4 |
|
| 5 |
simple_vendor_prefixes = { |
| 6 |
'animation': ['webkit', 'moz', 'o'], |
| 7 |
'animation-name': ['webkit', 'moz', 'o'], |
| 8 |
'animation-duration': ['webkit', 'moz', 'o'], |
| 9 |
'animation-timing-function': ['webkit', 'moz', 'o'], |
| 10 |
'animation-delay': ['webkit', 'moz', 'o'], |
| 11 |
'animation-iteration-count': ['webkit', 'moz', 'o'], |
| 12 |
'animation-direction': ['webkit', 'moz', 'o'], |
| 13 |
'animation-fill-mode': ['webkit', 'moz', 'o'], |
| 14 |
'animation-play-state': ['webkit', 'moz', 'o'], |
| 15 |
'hyphens': ['webkit', 'moz', 'ms'], |
| 16 |
'transform': ['webkit', 'ms', 'moz', 'o'], |
| 17 |
'transform-origin': ['webkit', 'ms', 'moz', 'o'], |
| 18 |
'transition': ['webkit', 'moz', 'o'], |
| 19 |
'filter': ['webkit'], |
| 20 |
'user-select': ['webkit', 'moz', 'ms'], |
| 21 |
'break-before': ['webkit-column'], |
| 22 |
'break-after': ['webkit-column'], |
| 23 |
'break-inside': ['webkit-column'], |
| 24 |
'column-gap': ['webkit', 'moz'], |
| 25 |
'column-width': ['webkit', 'moz'], |
| 26 |
'column-rule': ['webkit', 'moz'], |
| 27 |
'column-fill': ['moz'], |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
def set_css(elem, **kw): |
| 32 |
if jstype(elem) is 'string': |
| 33 |
elem = document.querySelector(elem) |
| 34 |
s = elem.style |
| 35 |
if s: |
| 36 |
for prop in kw: |
| 37 |
name, val = str.replace(str.rstrip(prop, '_'), '_', '-'), kw[prop] |
| 38 |
if val is None or val is undefined: |
| 39 |
s.removeProperty(name) |
| 40 |
else: |
| 41 |
s.setProperty(name, val) |
| 42 |
prefixes = simple_vendor_prefixes[name] |
| 43 |
if prefixes: |
| 44 |
for prefix in prefixes: |
| 45 |
if val is None or val is undefined: |
| 46 |
s.removeProperty('-' + prefix + '-' + name) |
| 47 |
else: |
| 48 |
s.setProperty('-' + prefix + '-' + name, val) |
| 49 |
return elem |
| 50 |
|
| 51 |
|
| 52 |
def set_important_css(elem, **kw): |
| 53 |
if jstype(elem) is 'string': |
| 54 |
elem = document.querySelector(elem) |
| 55 |
s = elem.style |
| 56 |
if s: |
| 57 |
for prop in kw: |
| 58 |
name, val = str.replace(str.rstrip(prop, '_'), '_', '-'), kw[prop] |
| 59 |
if val is None or val is undefined: |
| 60 |
s.removeProperty(name) |
| 61 |
else: |
| 62 |
s.setProperty(name, val, 'important') |
| 63 |
prefixes = simple_vendor_prefixes[name] |
| 64 |
if prefixes: |
| 65 |
for prefix in prefixes: |
| 66 |
if val is None or val is undefined: |
| 67 |
s.removeProperty('-' + prefix + '-' + name) |
| 68 |
else: |
| 69 |
s.setProperty('-' + prefix + '-' + name, val, 'important') |
| 70 |
return elem |
| 71 |
|
| 72 |
|
| 73 |
def build_rule(selector, **kw): |
| 74 |
ans = [selector + ' { '] |
| 75 |
for prop in kw: |
| 76 |
name, val = str.replace(str.rstrip(prop, '_'), '_', '-'), kw[prop] |
| 77 |
ans.push(name + ':' + val + ';') |
| 78 |
prefixes = simple_vendor_prefixes[name] |
| 79 |
if prefixes: |
| 80 |
for prefix in prefixes: |
| 81 |
ans.push('-' + prefix + '-' + name + ':' + val + ';') |
| 82 |
ans.push('}') |
| 83 |
return ans.join('\n') + '\n' |
| 84 |
|
| 85 |
|
| 86 |
def clear(): |
| 87 |
for v'var i = 0; i < arguments.length; i++': |
| 88 |
node = arguments[i] |
| 89 |
while node.firstChild: |
| 90 |
node.removeChild(node.firstChild) |
| 91 |
|
| 92 |
|
| 93 |
def remove_all_attributes(): |
| 94 |
for v'var i = 0; i < arguments.length; i++': |
| 95 |
node = arguments[i] |
| 96 |
while node.attributes.length > 0: |
| 97 |
node.removeAttributeNode(node.attributes[0]) |
| 98 |
|
| 99 |
|
| 100 |
def create_keyframes(animation_name, *frames): |
| 101 |
ans = [] |
| 102 |
for prefix in '-webkit-', '-moz-', '-o-', '': |
| 103 |
ans.push('@' + prefix + 'keyframes ' + animation_name + ' {') |
| 104 |
for frame in frames: |
| 105 |
ans.push(frame) |
| 106 |
ans.push('}') |
| 107 |
return ans.join('\n') + '\n' |
| 108 |
|
| 109 |
|
| 110 |
def change_icon_image(icon_element, new_name, tooltip): |
| 111 |
if new_name: |
| 112 |
icon_element.firstChild.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', '#icon-' + new_name) |
| 113 |
else: |
| 114 |
icon_element.firstChild.removeAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href') |
| 115 |
if tooltip?: |
| 116 |
x = icon_element.getElementsByTagNameNS('http://www.w3.org/2000/svg', 'title') |
| 117 |
if not x.length: |
| 118 |
tt = document.createElementNS('http://www.w3.org/2000/svg', 'title') |
| 119 |
icon_element.appendChild(tt) |
| 120 |
x = [tt] |
| 121 |
x[0].textContent = tooltip |
| 122 |
|
| 123 |
|
| 124 |
def svgicon(name, height, width, tooltip): |
| 125 |
ans = document.createElementNS('http://www.w3.org/2000/svg', 'svg') |
| 126 |
ans.setAttribute('style', 'fill: currentColor; height: {}; width: {}; vertical-align: text-top'.format(height ? '2ex', width ? '2ex')) |
| 127 |
u = document.createElementNS('http://www.w3.org/2000/svg', 'use') |
| 128 |
ans.appendChild(u) |
| 129 |
if name: |
| 130 |
ans.firstChild.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', '#icon-' + name) |
| 131 |
if tooltip: |
| 132 |
tt = document.createElementNS('http://www.w3.org/2000/svg', 'title') |
| 133 |
tt.textContent = tooltip |
| 134 |
ans.appendChild(tt) |
| 135 |
return ans |
| 136 |
|
| 137 |
|
| 138 |
def element(elem_id, child_selector): |
| 139 |
ans = document.getElementById(elem_id) if elem_id else document.body |
| 140 |
if child_selector: |
| 141 |
ans = ans.querySelector(child_selector) |
| 142 |
return ans |
| 143 |
|
| 144 |
|
| 145 |
def selector(id_selector, *args): |
| 146 |
ans = '#' + id_selector |
| 147 |
for x in args: |
| 148 |
ans += ' ' + x |
| 149 |
return ans |
| 150 |
|
| 151 |
|
| 152 |
def rule(id_selector, *args, **kw): |
| 153 |
return build_rule(selector(id_selector, *args), **kw) |
| 154 |
|
| 155 |
|
| 156 |
def unique_id(prefix): |
| 157 |
prefix = prefix or 'auto-id' |
| 158 |
unique_id.counts[prefix] = num = (unique_id.counts[prefix] or 0) + 1 |
| 159 |
return prefix + '-' + num |
| 160 |
unique_id.counts = {} |
| 161 |
|
| 162 |
|
| 163 |
def ensure_id(w, prefix): |
| 164 |
ans = w.getAttribute('id') |
| 165 |
if not ans: |
| 166 |
ans = unique_id(prefix) |
| 167 |
w.setAttribute('id', ans) |
| 168 |
return ans |
| 169 |
|
| 170 |
extra_css = [] |
| 171 |
add_extra_css = extra_css.push.bind(extra_css) |
| 172 |
|
| 173 |
|
| 174 |
def get_widget_css(): |
| 175 |
ans = [] |
| 176 |
for func in extra_css: |
| 177 |
ans.push(func()) |
| 178 |
return ans.join('\n') |
| 179 |
|
| 180 |
|
| 181 |
def set_radio_group_value(parent, name, val): |
| 182 |
changed = False |
| 183 |
for inp in parent.querySelectorAll(f'input[name={name}]'): |
| 184 |
inp.checked = inp.value is val |
| 185 |
changed = True |
| 186 |
if not changed: |
| 187 |
raise KeyError(f'No radio group with name={name} found') |