Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 1 | define({ |
| 2 | create : function (params) { |
| 3 | return Object.create(this)._init(); |
| 4 | }, |
| 5 | _init : function () { |
| 6 | this._string = ''; |
| 7 | |
| 8 | // Add prefix span |
| 9 | this._element = document.createElement('span'); |
| 10 | this._element.classList.add('pref'); |
| 11 | // Connect action |
| 12 | |
| 13 | if (this["onclick"] !== undefined) |
| 14 | this._element["onclick"] = this.onclick.bind(this); |
| 15 | |
| 16 | return this; |
| 17 | }, |
| 18 | _update : function () { |
| 19 | this._element.innerHTML |
| 20 | = this._string; |
| 21 | }, |
| 22 | |
| 23 | /** |
| 24 | * Upgrade this object to another object, |
| 25 | * while private data stays intact. |
| 26 | * |
| 27 | * @param {Object} An object with properties. |
| 28 | */ |
| 29 | upgradeTo : function (props) { |
| 30 | for (var prop in props) { |
| 31 | this[prop] = props[prop]; |
| 32 | }; |
| 33 | return this; |
| 34 | }, |
| 35 | |
| 36 | active : function (bool) { |
| 37 | var cl = this.element().classList; |
| 38 | if (bool === undefined) |
| 39 | return cl.contains("active"); |
| 40 | else if (bool) |
| 41 | cl.add("active"); |
| 42 | else |
| 43 | cl.remove("active"); |
| 44 | }, |
| 45 | |
| 46 | element : function () { |
| 47 | return this._element; |
| 48 | }, |
| 49 | |
| 50 | isSet : function () { |
| 51 | return this._string.length > 0 ? |
| 52 | true : false; |
| 53 | }, |
| 54 | |
| 55 | value : function (string) { |
| 56 | if (arguments.length === 1) { |
| 57 | this._string = string; |
| 58 | this._update(); |
| 59 | }; |
| 60 | return this._string; |
| 61 | }, |
| 62 | |
| 63 | add : function (string) { |
| 64 | this._string += string; |
| 65 | this._update(); |
| 66 | }, |
| 67 | |
| 68 | onclick : function () {}, |
| 69 | |
| 70 | backspace : function () { |
| 71 | if (this._string.length > 1) { |
| 72 | this._string = this._string.substring( |
| 73 | 0, this._string.length - 1 |
| 74 | ); |
| 75 | } |
| 76 | else { |
| 77 | this._string = ''; |
| 78 | }; |
| 79 | |
| 80 | this._update(); |
| 81 | }, |
| 82 | |
| 83 | /** |
| 84 | * Return menu list. |
| 85 | */ |
| 86 | menu : function () { |
| 87 | return this._menu; |
| 88 | } |
| 89 | }); |