blob: e513e8532dbdef1aec43e956c67d019528ea8eec [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
Akronc53cfc82020-10-19 11:00:58 +020020 t._element = document.createElement('span');
21 t._element.classList.add('pref');
Nils Diewald0e6992a2015-04-14 20:13:52 +000022 // Connect action
23
Akronc53cfc82020-10-19 11:00:58 +020024 if (t["onclick"] !== undefined)
25 t._element["onclick"] = t.onclick.bind(t);
Nils Diewald0e6992a2015-04-14 20:13:52 +000026
Akronc53cfc82020-10-19 11:00:58 +020027 return t;
Nils Diewald0e6992a2015-04-14 20:13:52 +000028 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000029
Akronc53cfc82020-10-19 11:00:58 +020030
Nils Diewald0e6992a2015-04-14 20:13:52 +000031 _update : function () {
Nils Diewald7148c6f2015-05-04 15:07:53 +000032 return this._element.innerHTML
Nils Diewald0e6992a2015-04-14 20:13:52 +000033 = this._string;
34 },
35
Akronc53cfc82020-10-19 11:00:58 +020036
Nils Diewald0e6992a2015-04-14 20:13:52 +000037 /**
38 * Upgrade this object to another object,
39 * while private data stays intact.
40 *
41 * @param {Object} An object with properties.
42 */
43 upgradeTo : function (props) {
Akronc53cfc82020-10-19 11:00:58 +020044 for (let prop in props) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000045 this[prop] = props[prop];
46 };
47 return this;
48 },
49
Nils Diewald7148c6f2015-05-04 15:07:53 +000050
51 /**
52 * Get or set the activity status of the prefix.
53 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000054 active : function (bool) {
Akronc53cfc82020-10-19 11:00:58 +020055 const cl = this.element().classList;
Nils Diewald0e6992a2015-04-14 20:13:52 +000056 if (bool === undefined)
57 return cl.contains("active");
58 else if (bool)
59 cl.add("active");
60 else
61 cl.remove("active");
62 },
63
Akronc53cfc82020-10-19 11:00:58 +020064
Nils Diewald7148c6f2015-05-04 15:07:53 +000065 /**
66 * Check, if a prefix is given or not.
67 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000068 isSet : function () {
69 return this._string.length > 0 ?
70 true : false;
71 },
72
Akronc53cfc82020-10-19 11:00:58 +020073
Nils Diewald7148c6f2015-05-04 15:07:53 +000074 /**
75 * Get or set the prefix string.
76 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000077 value : function (string) {
78 if (arguments.length === 1) {
79 this._string = string;
Nils Diewald7148c6f2015-05-04 15:07:53 +000080 return this._update();
Nils Diewald0e6992a2015-04-14 20:13:52 +000081 };
82 return this._string;
83 },
Nils Diewald7148c6f2015-05-04 15:07:53 +000084
85
86 /**
87 * Add string to prefix.
88 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000089 add : function (string) {
90 this._string += string;
Nils Diewald7148c6f2015-05-04 15:07:53 +000091 return this._update();
Nils Diewald0e6992a2015-04-14 20:13:52 +000092 },
Akronc53cfc82020-10-19 11:00:58 +020093
Nils Diewald7148c6f2015-05-04 15:07:53 +000094
95 /**
96 * Clear prefix
97 */
98 clear : function () {
99 this._string = '';
100 return this._update();
101 },
102
103
104 /**
105 * Action method.
106 * Expected to be overridden.
107 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000108 onclick : function () {},
109
Akronc53cfc82020-10-19 11:00:58 +0200110
Nils Diewald7148c6f2015-05-04 15:07:53 +0000111 /**
112 * Remove the last character of the string
113 */
114 chop : function () {
Akronc53cfc82020-10-19 11:00:58 +0200115 const t = this;
Nils Diewald7148c6f2015-05-04 15:07:53 +0000116
117 // Prefix is long enough for backspace
Akronc53cfc82020-10-19 11:00:58 +0200118 if (t._string.length > 1) {
119 t._string = t._string.substring(
120 0, t._string.length - 1
Nils Diewald0e6992a2015-04-14 20:13:52 +0000121 );
122 }
123 else {
Akronc53cfc82020-10-19 11:00:58 +0200124 t._string = '';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000125 };
126
Akronc53cfc82020-10-19 11:00:58 +0200127 return t._update();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000128 },
129
Nils Diewald7148c6f2015-05-04 15:07:53 +0000130
131 /**
132 * Get the associated dom element.
133 */
134 element : function () {
135 return this._element;
136 },
137
138
Nils Diewald0e6992a2015-04-14 20:13:52 +0000139 /**
140 * Return menu list.
141 */
142 menu : function () {
143 return this._menu;
144 }
145});