1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| var utils = { actions: { }, element: { jsonWrapper: function(target) { var jsonParseRule = /^\{"([\w\W])+\}$/; if (jsonParseRule.test(target)) return JSON.parse(target); return (typeof target === 'object' && target !== null) ? JSON.stringify(target) : target; }, elementWrapper: function($element) { return (typeof $element === 'object') ? $element : document.querySelector($element); }, setAttr: function($element, key, value) { $element = this.elementWrapper($element); (key) && $element.setAttribute(key, this.jsonWrapper(value)); return this; }, setCss: function($element, styleKey, styleValue, important) { $element = this.elementWrapper($element); $element.style.setProperty(styleKey, styleValue, important === 'important' ? important : undefined); return this; }, getAttr: function($element, key) { $element = this.elementWrapper($element); return (key) ? this.jsonWrapper($element.getAttribute(key)) : undefined; }, setData: function($element, key, value) { $element = this.elementWrapper($element); (key) && this.setAttr($element, 'data-' + key, value); return this; }, getData: function($element, key) { $element = this.elementWrapper($element); return key ? this.getAttr($element, 'data-' + key) : undefined; }, addClass: function($element, className) { $element = this.elementWrapper($element); var classes = $element.className.split(' '); if (!classes.includes(className)) { classes.push(className); $element.className = classes.join(' '); } return this; }, removeClass: function($element, className) { $element = this.elementWrapper($element); var classes = $element.className.split(' '); var index = classes.indexOf(className); if (index !== -1) { classes.splice(index, 1); $element.className = classes.join(' '); } return this; }, empty: function($element) { $element = this.elementWrapper($element); $element.innerHTML = ''; }, html: function($element, htmlStr) { $element = this.elementWrapper($element); $element.innerHTML = htmlStr; } }, renderX1: function (r, d) { if (d === 'top' || d === 'bottom') return (r.x + r.width / 2 + 'px'); if (d === 'left') return (r.x - 6 + 'px'); if (d === 'right') return (r.x + r.width + 6 + 'px'); if (d === 'bottomleft' || d === 'topleft') return (r.x + 'px'); if (d === 'bottomright' || d === 'topright') return (r.x + r.width + 'px'); }, renderY1: function (r, d) { if (d === 'top') return (r.y - 6 + 'px'); if (d === 'left' || d === 'right') return (r.y + r.height / 2 + 'px'); if (d === 'bottom') return (r.y + r.height + 6 + 'px'); if (d === 'bottomleft' || d === 'bottomright') return (r.y + r.height + 'px'); if (d === 'topleft' || d === 'topright') return (r.y + 'px'); }, renderX2: function (r, d) { if (d === 'top' || d === 'bottom') return (r.x - r.width / 2 + 'px'); if (d === 'left' || d === 'bottomleft' || d === 'topleft') return (r.x - r.width + 'px'); if (d === 'right') return (r.x + 'px'); if (d === 'bottomright' || d === 'topright') return (r.x + r.widht + 'px'); }, renderY2: function (r, d) { if (d === 'top' || d === 'topleft' || d === 'topright') return (r.y - r.height + 'px'); if (d === 'left' || d === 'right') return (r.y - r.height / 2 + 'px'); if (d === 'bottom' || d === 'bottomleft' || d === 'bottomright') return (r.y + 'px'); }, actionDebounce: function(symbol, action, params) { var that = this; var timer = setTimeout(function() { action(params); clearTimeout(timer); delete that.actions[symbol]; }, 300); if (!that.actions[symbol]) { that.actions[symbol] = timer; } else { clearTimeout(that.actions[symbol]); that.actions[symbol] = timer; } } }
|