blob: abcf66c4c450cab15ab8dcc28da8a7ee203cf130 [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001define({
Nils Diewald7148c6f2015-05-04 15:07:53 +00002
3 /**
4 * Create new prefix object.
5 */
6 create : function () {
Nils Diewald0e6992a2015-04-14 20:13:52 +00007 return Object.create(this)._init();
8 },
Nils Diewald7148c6f2015-05-04 15:07:53 +00009
10 // Initialize prefix object
Nils Diewald0e6992a2015-04-14 20:13:52 +000011 _init : function () {
12 this._string = '';
13
14 // Add prefix span
15 this._element = document.createElement('span');
16 this._element.classList.add('pref');
17 // Connect action
18
19 if (this["onclick"] !== undefined)
20 this._element["onclick"] = this.onclick.bind(this);
21
22 return this;
23 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000024
Nils Diewald0e6992a2015-04-14 20:13:52 +000025 _update : function () {
Nils Diewald7148c6f2015-05-04 15:07:53 +000026 return this._element.innerHTML
Nils Diewald0e6992a2015-04-14 20:13:52 +000027 = this._string;
28 },
29
30 /**
31 * Upgrade this object to another object,
32 * while private data stays intact.
33 *
34 * @param {Object} An object with properties.
35 */
36 upgradeTo : function (props) {
37 for (var prop in props) {
38 this[prop] = props[prop];
39 };
40 return this;
41 },
42
Nils Diewald7148c6f2015-05-04 15:07:53 +000043
44 /**
45 * Get or set the activity status of the prefix.
46 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000047 active : function (bool) {
48 var cl = this.element().classList;
49 if (bool === undefined)
50 return cl.contains("active");
51 else if (bool)
52 cl.add("active");
53 else
54 cl.remove("active");
55 },
56
Nils Diewald7148c6f2015-05-04 15:07:53 +000057 /**
58 * Check, if a prefix is given or not.
59 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000060 isSet : function () {
61 return this._string.length > 0 ?
62 true : false;
63 },
64
Nils Diewald7148c6f2015-05-04 15:07:53 +000065 /**
66 * Get or set the prefix string.
67 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000068 value : function (string) {
69 if (arguments.length === 1) {
70 this._string = string;
Nils Diewald7148c6f2015-05-04 15:07:53 +000071 return this._update();
Nils Diewald0e6992a2015-04-14 20:13:52 +000072 };
73 return this._string;
74 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000075
76
77 /**
78 * Add string to prefix.
79 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000080 add : function (string) {
81 this._string += string;
Nils Diewald7148c6f2015-05-04 15:07:53 +000082 return this._update();
Nils Diewald0e6992a2015-04-14 20:13:52 +000083 },
84
Nils Diewald7148c6f2015-05-04 15:07:53 +000085
86 /**
87 * Clear prefix
88 */
89 clear : function () {
90 this._string = '';
91 return this._update();
92 },
93
94
95 /**
96 * Action method.
97 * Expected to be overridden.
98 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000099 onclick : function () {},
100
Nils Diewald7148c6f2015-05-04 15:07:53 +0000101
102 /**
103 * Remove the last character of the string
104 */
105 chop : function () {
106
107 // Prefix is long enough for backspace
Nils Diewald0e6992a2015-04-14 20:13:52 +0000108 if (this._string.length > 1) {
109 this._string = this._string.substring(
110 0, this._string.length - 1
111 );
112 }
113 else {
114 this._string = '';
115 };
116
Nils Diewald7148c6f2015-05-04 15:07:53 +0000117 return this._update();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000118 },
119
Nils Diewald7148c6f2015-05-04 15:07:53 +0000120
121 /**
122 * Get the associated dom element.
123 */
124 element : function () {
125 return this._element;
126 },
127
128
Nils Diewald0e6992a2015-04-14 20:13:52 +0000129 /**
130 * Return menu list.
131 */
132 menu : function () {
133 return this._menu;
134 }
135});