blob: 59a0ab5af50f0afb20a1c2a944df70c99dbaacc4 [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
Leo Reppd162b2e2021-06-30 13:51:07 +020025 if (t["onclick"] !== undefined) {
Akron24aa0052020-11-10 11:00:34 +010026 t._el["onclick"] = t.onclick.bind(t);
Leo Reppd162b2e2021-06-30 13:51:07 +020027 }
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 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000039 * Get or set the activity status of the prefix.
40 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000041 active : function (bool) {
Akronc53cfc82020-10-19 11:00:58 +020042 const cl = this.element().classList;
Nils Diewald0e6992a2015-04-14 20:13:52 +000043 if (bool === undefined)
44 return cl.contains("active");
45 else if (bool)
46 cl.add("active");
47 else
48 cl.remove("active");
49 },
50
Akronc53cfc82020-10-19 11:00:58 +020051
Nils Diewald7148c6f2015-05-04 15:07:53 +000052 /**
53 * Check, if a prefix is given or not.
54 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000055 isSet : function () {
56 return this._string.length > 0 ?
57 true : false;
58 },
59
Akronc53cfc82020-10-19 11:00:58 +020060
Nils Diewald7148c6f2015-05-04 15:07:53 +000061 /**
62 * Get or set the prefix string.
63 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000064 value : function (string) {
65 if (arguments.length === 1) {
66 this._string = string;
Nils Diewald7148c6f2015-05-04 15:07:53 +000067 return this._update();
Nils Diewald0e6992a2015-04-14 20:13:52 +000068 };
69 return this._string;
70 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000071
72
73 /**
74 * Add string to prefix.
75 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000076 add : function (string) {
77 this._string += string;
Nils Diewald7148c6f2015-05-04 15:07:53 +000078 return this._update();
Nils Diewald0e6992a2015-04-14 20:13:52 +000079 },
Akronc53cfc82020-10-19 11:00:58 +020080
Nils Diewald7148c6f2015-05-04 15:07:53 +000081
82 /**
83 * Clear prefix
84 */
85 clear : function () {
86 this._string = '';
87 return this._update();
88 },
89
90
91 /**
92 * Action method.
93 * Expected to be overridden.
94 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000095 onclick : function () {},
96
Akronc53cfc82020-10-19 11:00:58 +020097
Nils Diewald7148c6f2015-05-04 15:07:53 +000098 /**
99 * Remove the last character of the string
100 */
101 chop : function () {
Akronc53cfc82020-10-19 11:00:58 +0200102 const t = this;
Nils Diewald7148c6f2015-05-04 15:07:53 +0000103
104 // Prefix is long enough for backspace
Akronc53cfc82020-10-19 11:00:58 +0200105 if (t._string.length > 1) {
106 t._string = t._string.substring(
107 0, t._string.length - 1
Nils Diewald0e6992a2015-04-14 20:13:52 +0000108 );
109 }
110 else {
Akronc53cfc82020-10-19 11:00:58 +0200111 t._string = '';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000112 };
113
Akronc53cfc82020-10-19 11:00:58 +0200114 return t._update();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000115 },
116
Nils Diewald7148c6f2015-05-04 15:07:53 +0000117
118 /**
119 * Get the associated dom element.
120 */
121 element : function () {
Akron24aa0052020-11-10 11:00:34 +0100122 return this._el;
Nils Diewald7148c6f2015-05-04 15:07:53 +0000123 },
124
125
Nils Diewald0e6992a2015-04-14 20:13:52 +0000126 /**
127 * Return menu list.
128 */
129 menu : function () {
130 return this._menu;
131 }
132});