blob: f6c6c76433199d5026c99ca2f13d7f57c5842753 [file] [log] [blame]
Akronc53cfc82020-10-19 11:00:58 +02001"use strict";
2
Nils Diewald0e6992a2015-04-14 20:13:52 +00003define({
Nils Diewald7148c6f2015-05-04 15:07:53 +00004
5 /**
6 * Create new prefix object.
7 */
8 create : function () {
Nils Diewald0e6992a2015-04-14 20:13:52 +00009 return Object.create(this)._init();
10 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000011
Akronc53cfc82020-10-19 11:00:58 +020012
Nils Diewald7148c6f2015-05-04 15:07:53 +000013 // Initialize prefix object
Nils Diewald0e6992a2015-04-14 20:13:52 +000014 _init : function () {
Akronc53cfc82020-10-19 11:00:58 +020015 const t = this;
16
17 t._string = '';
Nils Diewald0e6992a2015-04-14 20:13:52 +000018
19 // Add prefix span
Akron24aa0052020-11-10 11:00:34 +010020 t._el = document.createElement('span');
Akronf21bf742021-05-19 13:30:38 +020021 t._el.classList.add('non-item');
Akron24aa0052020-11-10 11:00:34 +010022 t._el.classList.add('pref');
Nils Diewald0e6992a2015-04-14 20:13:52 +000023 // Connect action
24
Akronc53cfc82020-10-19 11:00:58 +020025 if (t["onclick"] !== undefined)
Akron24aa0052020-11-10 11:00:34 +010026 t._el["onclick"] = t.onclick.bind(t);
Nils Diewald0e6992a2015-04-14 20:13:52 +000027
Akronc53cfc82020-10-19 11:00:58 +020028 return t;
Nils Diewald0e6992a2015-04-14 20:13:52 +000029 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000030
Akronc53cfc82020-10-19 11:00:58 +020031
Nils Diewald0e6992a2015-04-14 20:13:52 +000032 _update : function () {
Akron24aa0052020-11-10 11:00:34 +010033 return this._el.innerHTML
Nils Diewald0e6992a2015-04-14 20:13:52 +000034 = this._string;
35 },
36
Akronc53cfc82020-10-19 11:00:58 +020037
Nils Diewald0e6992a2015-04-14 20:13:52 +000038 /**
39 * Upgrade this object to another object,
40 * while private data stays intact.
41 *
42 * @param {Object} An object with properties.
43 */
44 upgradeTo : function (props) {
Akronc53cfc82020-10-19 11:00:58 +020045 for (let prop in props) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000046 this[prop] = props[prop];
47 };
48 return this;
49 },
50
Nils Diewald7148c6f2015-05-04 15:07:53 +000051
52 /**
53 * Get or set the activity status of the prefix.
54 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000055 active : function (bool) {
Akronc53cfc82020-10-19 11:00:58 +020056 const cl = this.element().classList;
Nils Diewald0e6992a2015-04-14 20:13:52 +000057 if (bool === undefined)
58 return cl.contains("active");
59 else if (bool)
60 cl.add("active");
61 else
62 cl.remove("active");
63 },
64
Akronc53cfc82020-10-19 11:00:58 +020065
Nils Diewald7148c6f2015-05-04 15:07:53 +000066 /**
67 * Check, if a prefix is given or not.
68 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000069 isSet : function () {
70 return this._string.length > 0 ?
71 true : false;
72 },
73
Akronc53cfc82020-10-19 11:00:58 +020074
Nils Diewald7148c6f2015-05-04 15:07:53 +000075 /**
76 * Get or set the prefix string.
77 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000078 value : function (string) {
79 if (arguments.length === 1) {
80 this._string = string;
Nils Diewald7148c6f2015-05-04 15:07:53 +000081 return this._update();
Nils Diewald0e6992a2015-04-14 20:13:52 +000082 };
83 return this._string;
84 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000085
86
87 /**
88 * Add string to prefix.
89 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000090 add : function (string) {
91 this._string += string;
Nils Diewald7148c6f2015-05-04 15:07:53 +000092 return this._update();
Nils Diewald0e6992a2015-04-14 20:13:52 +000093 },
Akronc53cfc82020-10-19 11:00:58 +020094
Nils Diewald7148c6f2015-05-04 15:07:53 +000095
96 /**
97 * Clear prefix
98 */
99 clear : function () {
100 this._string = '';
101 return this._update();
102 },
103
104
105 /**
106 * Action method.
107 * Expected to be overridden.
108 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000109 onclick : function () {},
110
Akronc53cfc82020-10-19 11:00:58 +0200111
Nils Diewald7148c6f2015-05-04 15:07:53 +0000112 /**
113 * Remove the last character of the string
114 */
115 chop : function () {
Akronc53cfc82020-10-19 11:00:58 +0200116 const t = this;
Nils Diewald7148c6f2015-05-04 15:07:53 +0000117
118 // Prefix is long enough for backspace
Akronc53cfc82020-10-19 11:00:58 +0200119 if (t._string.length > 1) {
120 t._string = t._string.substring(
121 0, t._string.length - 1
Nils Diewald0e6992a2015-04-14 20:13:52 +0000122 );
123 }
124 else {
Akronc53cfc82020-10-19 11:00:58 +0200125 t._string = '';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000126 };
127
Akronc53cfc82020-10-19 11:00:58 +0200128 return t._update();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000129 },
130
Nils Diewald7148c6f2015-05-04 15:07:53 +0000131
132 /**
133 * Get the associated dom element.
134 */
135 element : function () {
Akron24aa0052020-11-10 11:00:34 +0100136 return this._el;
Nils Diewald7148c6f2015-05-04 15:07:53 +0000137 },
138
139
Nils Diewald0e6992a2015-04-14 20:13:52 +0000140 /**
141 * Return menu list.
142 */
143 menu : function () {
144 return this._menu;
145 }
146});