|
| 1 | +var svgNamespace = 'http://www.w3.org/2000/svg'; |
| 2 | + |
| 3 | +// Wrapped to provide tag vs svgTag methods |
| 4 | +var createElement = function(name, attr, children, svg) { |
| 5 | + var tag; |
| 6 | + if (svg) { |
| 7 | + tag = document.createElementNS(svgNamespace, name); |
| 8 | + } else { |
| 9 | + tag = document.createElement(name); |
| 10 | + } |
| 11 | + // We apply attributes after children so that we can set the value of |
| 12 | + // a select tag at render time |
| 13 | + if (children) { |
| 14 | + if (!Array.isArray(children)) { |
| 15 | + children = [ children ]; |
| 16 | + } |
| 17 | + for (var i = 0; i < children.length; i++) { |
| 18 | + var node = children[i]; |
| 19 | + if (typeof node === 'string' || typeof node === 'number') { |
| 20 | + node = document.createTextNode(node); |
| 21 | + } |
| 22 | + if (node) { |
| 23 | + // Allow falsy entries to be ignored |
| 24 | + tag.appendChild(node); |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + if (attr) { |
| 29 | + for (var a in attr) { |
| 30 | + if (a === 'style') { |
| 31 | + for (var s in attr[a]) { |
| 32 | + tag.style[s] = attr[a][s]; |
| 33 | + } |
| 34 | + } else if (a.indexOf('data-') === 0 || svg) { |
| 35 | + tag.setAttribute(a, attr[a]); |
| 36 | + } else { |
| 37 | + tag[a] = attr[a]; |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + return tag; |
| 42 | +}; |
| 43 | + |
| 44 | +var UI = { |
| 45 | + // DOM construction methods |
| 46 | + tag: function(name, attr, children) { |
| 47 | + return createElement(name, attr, children, false); |
| 48 | + }, |
| 49 | + |
| 50 | + svgTag: function(name, attr, children) { |
| 51 | + return createElement(name, attr, children, true); |
| 52 | + }, |
| 53 | + |
| 54 | + createIcon: function(icon) { |
| 55 | + var i = document.createElement('i'); |
| 56 | + i.className = 'icon_' + icon; |
| 57 | + return i; |
| 58 | + }, |
| 59 | + |
| 60 | + createTooltip: function(text, options) { |
| 61 | + var className = 'tooltip_wrap'; |
| 62 | + switch (options.direction) { |
| 63 | + case 'left': |
| 64 | + className += ' to_left'; |
| 65 | + break; |
| 66 | + case 'top-left': |
| 67 | + className += ' to_top_left'; |
| 68 | + break; |
| 69 | + case 'top-right': |
| 70 | + className += ' to_top_right'; |
| 71 | + break; |
| 72 | + } |
| 73 | + return UI.tag('span', { className: className }, [ |
| 74 | + UI.tag('span', { className: 'tip' }, text) |
| 75 | + ]); |
| 76 | + }, |
| 77 | + |
| 78 | + // DOM property methods |
| 79 | + hasAncestor: function(node, ancestor) { |
| 80 | + var cur = node.parentNode; |
| 81 | + while (cur) { |
| 82 | + if (cur === ancestor) { |
| 83 | + return true; |
| 84 | + } |
| 85 | + cur = cur.parentNode; |
| 86 | + } |
| 87 | + return false; |
| 88 | + }, |
| 89 | + |
| 90 | + addClass: function(node, className) { |
| 91 | + var re = new RegExp('\\b' + className + '\\b'); |
| 92 | + if (!node.className.match(re)) { |
| 93 | + node.className += ' ' + className; |
| 94 | + } |
| 95 | + }, |
| 96 | + |
| 97 | + removeClass: function(node, className) { |
| 98 | + var re = new RegExp('\\s*\\b' + className + '\\b', 'g'); |
| 99 | + node.className = node.className.replace(re, ''); |
| 100 | + }, |
| 101 | + |
| 102 | + toggleClass: function(node, className, add) { |
| 103 | + if (add === undefined) { |
| 104 | + add = !UI.hasClass(node, className); |
| 105 | + } |
| 106 | + |
| 107 | + if (add) { |
| 108 | + UI.addClass(node, className); |
| 109 | + } else { |
| 110 | + UI.removeClass(node, className); |
| 111 | + } |
| 112 | + }, |
| 113 | + |
| 114 | + hasClass: function(node, className) { |
| 115 | + var re = new RegExp('\\b' + className + '\\b'); |
| 116 | + return !!node.className.match(re); |
| 117 | + }, |
| 118 | + |
| 119 | + getStyle: function(node, prop) { |
| 120 | + if (node.currentStyle) |
| 121 | + return node.currentStyle[styleProp]; |
| 122 | + if (window.getComputedStyle) |
| 123 | + return document.defaultView.getComputedStyle(node, null).getPropertyValue(prop); |
| 124 | + return ''; |
| 125 | + }, |
| 126 | + |
| 127 | + documentPosition: function(node) { |
| 128 | + var pos = { x: 0, y: 0 }; |
| 129 | + var cur = node; |
| 130 | + while (cur) { |
| 131 | + pos.x += cur.offsetLeft; |
| 132 | + pos.y += cur.offsetTop; |
| 133 | + cur = cur.offsetParent; |
| 134 | + } |
| 135 | + return pos; |
| 136 | + }, |
| 137 | + |
| 138 | + windowPosition: function(node) { |
| 139 | + var pos = UI.documentPosition(node); |
| 140 | + pos.x -= window.scrollX; |
| 141 | + pos.y -= window.scrollY; |
| 142 | + return pos; |
| 143 | + }, |
| 144 | + |
| 145 | + // DOM event methods |
| 146 | + delegate: function(event, parent, selector, cb) { |
| 147 | + if (event === 'focus' || event === 'blur') { |
| 148 | + // Focus and blur don't bubble |
| 149 | + throw 'Focus and blur delegation are not yet supported'; |
| 150 | + } |
| 151 | + var matches = function() { return false; }; |
| 152 | + if (typeof selector === 'string') { |
| 153 | + selector = selector.toUpperCase(); |
| 154 | + matches = function(el) { |
| 155 | + return el.tagName === selector; |
| 156 | + }; |
| 157 | + } else { |
| 158 | + if (selector.className) { |
| 159 | + var re = new RegExp('\\b' + selector.className + '\\b'); |
| 160 | + matches = function(el) { |
| 161 | + return el.className.match(re); |
| 162 | + }; |
| 163 | + } else if (selector.id) { |
| 164 | + matches = function(el) { |
| 165 | + return el.id === selector.id; |
| 166 | + }; |
| 167 | + } |
| 168 | + } |
| 169 | + parent.addEventListener(event, function(e) { |
| 170 | + var node = e.target; |
| 171 | + while (node && node !== document) { |
| 172 | + if (matches(node)) { |
| 173 | + cb.call(node, e); |
| 174 | + } |
| 175 | + node = node.parentNode; |
| 176 | + } |
| 177 | + }); |
| 178 | + }, |
| 179 | + |
| 180 | + // formatting methods |
| 181 | + prettyNumber: function(num) { |
| 182 | + var pre; |
| 183 | + var places = Math.ceil(Math.log(Math.abs(num) + 1) / Math.LN10); |
| 184 | + if (places > 6 && places < 10) { |
| 185 | + pre = (num / 1e6); |
| 186 | + if ((pre|0) === pre || Math.round(num % 1e6 / 1e5) === 0) { |
| 187 | + return (pre|0) + 'M'; |
| 188 | + } else { |
| 189 | + return (pre|0) + '.' + Math.round(num % 1e6 / 1e5) + 'M'; |
| 190 | + } |
| 191 | + } else if (places > 5) { |
| 192 | + pre = (num / 1000); |
| 193 | + if ((pre|0) === pre || Math.round(num % 1000 / 100) === 0) { |
| 194 | + return (pre|0) + 'K'; |
| 195 | + } else { |
| 196 | + return (pre|0) + '.' + Math.round(num % 1000 / 100) + 'K'; |
| 197 | + } |
| 198 | + } else if (places > 3) { |
| 199 | + var post = ((num % 1000)|0); |
| 200 | + if (post < 10) { |
| 201 | + post = '00' + post; |
| 202 | + } else if (post < 100) { |
| 203 | + post = '0' + post; |
| 204 | + } |
| 205 | + return ((num / 1000)|0) + ',' + post; |
| 206 | + } else { |
| 207 | + return (num|0) + ''; |
| 208 | + } |
| 209 | + }, |
| 210 | + |
| 211 | + // animation methods |
| 212 | + Animate: { |
| 213 | + // The show and hide functions require a "transition: 'opacity' delay" |
| 214 | + // CSS class to be present on the element |
| 215 | + show: function(el, delay) { |
| 216 | + if (delay === 'undefined') { |
| 217 | + delay = 0; |
| 218 | + } |
| 219 | + el.style.display = 'block'; |
| 220 | + el.style.opacity = 0; |
| 221 | + setTimeout(function() { |
| 222 | + el.style.opacity = 1; |
| 223 | + }, delay); |
| 224 | + }, |
| 225 | + |
| 226 | + hide: function(el, delay) { |
| 227 | + if (window.getComputedStyle(el).opacity > 0) { |
| 228 | + if (typeof delay === 'undefined') { |
| 229 | + delay = 500; |
| 230 | + } |
| 231 | + el.style.opacity = 0; |
| 232 | + setTimeout(function() { |
| 233 | + el.style.display = 'none'; |
| 234 | + }, delay); |
| 235 | + } |
| 236 | + } |
| 237 | + }, |
| 238 | + |
| 239 | + // Methods inherited by components |
| 240 | + ComponentProto: { |
| 241 | + attach: function(parent) { |
| 242 | + if (this.node) { |
| 243 | + parent.appendChild(this.node); |
| 244 | + return this; |
| 245 | + } |
| 246 | + return null; |
| 247 | + }, |
| 248 | + |
| 249 | + remove: function() { |
| 250 | + if (this.node && this.node.parentNode) { |
| 251 | + this.node.parentNode.removeChild(this.node); |
| 252 | + return this; |
| 253 | + } |
| 254 | + return null; |
| 255 | + } |
| 256 | + } |
| 257 | +}; |
| 258 | + |
| 259 | +module.exports = UI; |
0 commit comments