|
| 1 | +var domify = require('domify'); |
| 2 | +var Events = require('./events'); |
| 3 | +var slice = Array.prototype.slice; |
| 4 | + |
| 5 | +module.exports = ObjectElement; |
| 6 | + |
| 7 | +function ObjectElement(param) { |
| 8 | + var element, eventsRegistry = {}; |
| 9 | + |
| 10 | + if (typeof param === 'undefined' || param === null) { |
| 11 | + element = document.createElement('div'); |
| 12 | + } else if (typeof param === 'string') { |
| 13 | + element = document.createElement(param); |
| 14 | + } else if (typeof param === 'object') { |
| 15 | + element = param; |
| 16 | + } |
| 17 | + |
| 18 | + Object.defineProperty(this, 'element', { |
| 19 | + get: function () { |
| 20 | + return element; |
| 21 | + }, |
| 22 | + |
| 23 | + set: function (value) { |
| 24 | + element = value; |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + Object.defineProperty(this, 'eventsRegistry', { |
| 29 | + get: function () { |
| 30 | + return eventsRegistry |
| 31 | + } |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Wrap HTMLElement with ObjectElement |
| 37 | + * @param {HTMLElement element} |
| 38 | + * @return {ObjectElement} |
| 39 | + */ |
| 40 | +ObjectElement.wrapElement = function (element) { |
| 41 | + return element.OBJECT_ELEMENT ? element : new ObjectElement(element); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Loop through HTMLElements and wrap each of them with ObjectElement |
| 46 | + * @param {Array elements} |
| 47 | + * @return {Array} |
| 48 | + */ |
| 49 | +ObjectElement.wrapElements = function (elements) { |
| 50 | + elements = slice.call(elements); |
| 51 | + |
| 52 | + return elements.map(function (element, i) { |
| 53 | + return ObjectElement.wrapElement(element); |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | +ObjectElement.createElement = function (tag) { |
| 58 | + tag = tag || 'div'; |
| 59 | + |
| 60 | + return this.wrapElement(document.createElement(tag)); |
| 61 | +} |
| 62 | + |
| 63 | +ObjectElement.prototype = new Events; |
| 64 | + |
| 65 | +ObjectElement.prototype.defineProperty = function (name, defines) { |
| 66 | + Object.defineProperty(this, name, defines); |
| 67 | +} |
| 68 | + |
| 69 | +ObjectElement.prototype.defineProperty('OBJECT_ELEMENT', { |
| 70 | + get: function () { |
| 71 | + return 1; |
| 72 | + } |
| 73 | +}); |
| 74 | + |
| 75 | +/** |
| 76 | + * Shortcut to .element.id |
| 77 | + */ |
| 78 | +ObjectElement.prototype.defineProperty('id', { |
| 79 | + get: function () { |
| 80 | + return this.element.id; |
| 81 | + }, |
| 82 | + |
| 83 | + set: function (value) { |
| 84 | + this.element.id = value; |
| 85 | + } |
| 86 | +}); |
| 87 | + |
| 88 | +/** |
| 89 | + * Shortcut to .element.tagName |
| 90 | + */ |
| 91 | +ObjectElement.prototype.defineProperty('tag', { |
| 92 | + get: function () { |
| 93 | + return this.element.tagName; |
| 94 | + } |
| 95 | +}); |
| 96 | + |
| 97 | +/** |
| 98 | + * Get or set textContent of the element |
| 99 | + */ |
| 100 | +ObjectElement.prototype.defineProperty('text', { |
| 101 | + get: function () { |
| 102 | + return this.element.textContent; |
| 103 | + }, |
| 104 | + |
| 105 | + set: function (value) { |
| 106 | + this.element.textContent = value; |
| 107 | + } |
| 108 | +}); |
| 109 | + |
| 110 | +/** |
| 111 | + * Get or set innerHTML of the element |
| 112 | + */ |
| 113 | +ObjectElement.prototype.defineProperty('html', { |
| 114 | + get: function () { |
| 115 | + return this.element.innerHTML; |
| 116 | + }, |
| 117 | + |
| 118 | + set: function (htmlString) { |
| 119 | + this.element.innerHTML = ''; |
| 120 | + this.element.appendChild(domify(htmlString)); |
| 121 | + } |
| 122 | +}); |
| 123 | + |
| 124 | +/** |
| 125 | + * Call a function on this element |
| 126 | + * @param {Function callback} |
| 127 | + * @return {Null} |
| 128 | + */ |
| 129 | +ObjectElement.prototype.tie = function (callback) { |
| 130 | + callback.call(this, this.element); |
| 131 | +} |
0 commit comments