Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame^] | 1 | /** |
| 2 | * Add string values to the virtual collection |
| 3 | */ |
| 4 | define({ |
| 5 | create : function () { |
| 6 | var regex = false; |
| 7 | var value = ''; |
| 8 | if (arguments.length == 2) { |
| 9 | regex = arguments[1]; |
| 10 | }; |
| 11 | if (arguments.length > 1) { |
| 12 | value = arguments[0]; |
| 13 | }; |
| 14 | return Object.create(this)._init(value, regex); |
| 15 | }, |
| 16 | |
| 17 | _init : function (value, regex) { |
| 18 | this.element(); |
| 19 | this.value(value); |
| 20 | this.regex(regex); |
| 21 | return this; |
| 22 | }, |
| 23 | |
| 24 | regex : function (bool) { |
| 25 | if (arguments.length === 1) { |
| 26 | if (bool) { |
| 27 | this._regex = true; |
| 28 | } |
| 29 | else { |
| 30 | this._regex = false; |
| 31 | }; |
| 32 | this._update(); |
| 33 | }; |
| 34 | return this._regex; |
| 35 | }, |
| 36 | |
| 37 | toggleRegex : function () { |
| 38 | if (this._regex === false) |
| 39 | this.regex(true); |
| 40 | else |
| 41 | this.regex(false); |
| 42 | }, |
| 43 | |
| 44 | value : function (val) { |
| 45 | if (arguments.length === 1) { |
| 46 | this._value = val; |
| 47 | this._update(); |
| 48 | }; |
| 49 | return this._value; |
| 50 | }, |
| 51 | |
| 52 | _update : function () { |
| 53 | this._input.firstChild.data = this._value; |
| 54 | if (this._regex) { |
| 55 | this._element.classList.add('regex'); |
| 56 | } |
| 57 | else { |
| 58 | this._element.classList.add('regex'); |
| 59 | }; |
| 60 | }, |
| 61 | |
| 62 | element : function () { |
| 63 | if (this._element !== undefined) |
| 64 | return this._element; |
| 65 | |
| 66 | this._element = document.createElement('div'); |
| 67 | var cl = this._element.classList; |
| 68 | cl.add('vc-value'); |
| 69 | if (this.regex()) |
| 70 | cl.add('regex'); |
| 71 | |
| 72 | this._input = this._element.appendChild( |
| 73 | document.createElement('input') |
| 74 | ); |
| 75 | this._input.appendChild( |
| 76 | document.createTextNode(this.value()) |
| 77 | ); |
| 78 | |
| 79 | this._element.appendChild( |
| 80 | document.createElement('div') |
| 81 | ).addEventListener('click', function (e) { |
| 82 | this.toggleRegex(); |
| 83 | }.bind(this)); |
| 84 | |
| 85 | var go = this._element.appendChild( |
| 86 | document.createElement('span') |
| 87 | ); |
| 88 | |
| 89 | return this._element; |
| 90 | } |
| 91 | }); |