blob: c7a5d673d39aa715fcceeb9412ba07d629b0f84b [file] [log] [blame]
Nils Diewaldf0c4f112015-05-05 12:56:59 +00001/**
2 * Add string values to the virtual collection
3 */
4define({
Nils Diewaldc4c4b832015-05-05 16:00:08 +00005
6 /**
7 * Create new string value helper.
8 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +00009 create : function () {
10 var regex = false;
11 var value = '';
12 if (arguments.length == 2) {
13 regex = arguments[1];
14 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +000015 if (arguments.length >= 1) {
Nils Diewaldf0c4f112015-05-05 12:56:59 +000016 value = arguments[0];
17 };
18 return Object.create(this)._init(value, regex);
19 },
20
Nils Diewaldc4c4b832015-05-05 16:00:08 +000021
22 // Initialize the string value
Nils Diewaldf0c4f112015-05-05 12:56:59 +000023 _init : function (value, regex) {
24 this.element();
25 this.value(value);
26 this.regex(regex);
27 return this;
28 },
29
Nils Diewaldc4c4b832015-05-05 16:00:08 +000030
31 /**
32 * Get or set the regex boolean value.
33 *
34 * @param bool Either true or false
35 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +000036 regex : function (bool) {
37 if (arguments.length === 1) {
38 if (bool) {
39 this._regex = true;
40 }
41 else {
42 this._regex = false;
43 };
44 this._update();
45 };
46 return this._regex;
47 },
48
Nils Diewaldc4c4b832015-05-05 16:00:08 +000049
50 /**
51 * Toggle the regex, make it either true,
52 * if it is false, or make it false, if it is true.
53 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +000054 toggleRegex : function () {
55 if (this._regex === false)
56 this.regex(true);
57 else
58 this.regex(false);
59 },
60
Nils Diewaldc4c4b832015-05-05 16:00:08 +000061 /**
62 * Get or set the string value.
63 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +000064 value : function (val) {
65 if (arguments.length === 1) {
66 this._value = val;
Nils Diewald7991a3f2015-05-19 14:12:37 +000067 this._input.value = val;
Nils Diewaldf0c4f112015-05-05 12:56:59 +000068 this._update();
69 };
70 return this._value;
71 },
Nils Diewaldc4c4b832015-05-05 16:00:08 +000072
73
74 // Update dom element
Nils Diewaldf0c4f112015-05-05 12:56:59 +000075 _update : function () {
Nils Diewald7991a3f2015-05-19 14:12:37 +000076 this._value = this._input.value;
77
Nils Diewaldf0c4f112015-05-05 12:56:59 +000078 if (this._regex) {
79 this._element.classList.add('regex');
80 }
81 else {
Nils Diewaldc4c4b832015-05-05 16:00:08 +000082 this._element.classList.remove('regex');
Nils Diewaldf0c4f112015-05-05 12:56:59 +000083 };
84 },
85
Nils Diewaldc4c4b832015-05-05 16:00:08 +000086
87 /**
88 * Store the string value.
89 * This method should be overwritten.
90 * The method receives the the value and the regex.
91 */
92 store : function (v,r) {},
93
94
95 focus : function () {
96 this._element.children[0].focus();
97 },
98
99 /**
100 * Get the associated dom element.
101 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000102 element : function () {
103 if (this._element !== undefined)
104 return this._element;
105
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000106 // Create element
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000107 this._element = document.createElement('div');
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000108 var e = this._element;
109 e.setAttribute('tabindex', 0);
110 e.style.outline = 0;
111
112 var cl = e.classList;
113 cl.add('value');
114 if (this.regex() === true)
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000115 cl.add('regex');
116
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000117 // Add input field
118 this._input = e.appendChild(
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000119 document.createElement('input')
120 );
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000121 if (this.value() !== undefined)
122 this._input.value = this.value();
123
Nils Diewald9c125062015-05-05 23:54:17 +0000124 // Add regex button
125 var re = e.appendChild(
126 document.createElement('div')
127 );
128 re.addEventListener(
129 'click',
130 function (e) {
131 this.toggleRegex();
132 e.halt();
133 }.bind(this),
134 true
135 );
136 re.appendChild(document.createTextNode('RE'));
137
138 e.addEventListener(
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000139 'blur',
Nils Diewald9c125062015-05-05 23:54:17 +0000140 function (e) {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000141 this.store(this.value(), this.regex());
Nils Diewald9c125062015-05-05 23:54:17 +0000142 e.halt();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000143 }.bind(this)
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000144 );
145
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000146 this._input.addEventListener(
147 'keypress',
148 function (e) {
149 if (e.keyCode == 13) {
150 this.value(this._input.value);
151 this.store(this.value(), this.regex());
152 return false;
153 };
154 }.bind(this)
155 );
156
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000157 // Add store button
158 /*
159 e.appendChild(
160 document.createElement('div')
161 ).addEventListener('click', function () {
162 this.store(this.value(), this.regex());
163 }.bind(this));
164 */
165 return e;
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000166 }
167});