blob: cce278afdb0f2804b33479abb5e39c17a590b22d [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;
67 this._update();
68 };
69 return this._value;
70 },
Nils Diewaldc4c4b832015-05-05 16:00:08 +000071
72
73 // Update dom element
Nils Diewaldf0c4f112015-05-05 12:56:59 +000074 _update : function () {
Nils Diewaldc4c4b832015-05-05 16:00:08 +000075 this._input.value = this._value;
Nils Diewaldf0c4f112015-05-05 12:56:59 +000076 if (this._regex) {
77 this._element.classList.add('regex');
78 }
79 else {
Nils Diewaldc4c4b832015-05-05 16:00:08 +000080 this._element.classList.remove('regex');
Nils Diewaldf0c4f112015-05-05 12:56:59 +000081 };
82 },
83
Nils Diewaldc4c4b832015-05-05 16:00:08 +000084
85 /**
86 * Store the string value.
87 * This method should be overwritten.
88 * The method receives the the value and the regex.
89 */
90 store : function (v,r) {},
91
92
93 focus : function () {
94 this._element.children[0].focus();
95 },
96
97 /**
98 * Get the associated dom element.
99 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000100 element : function () {
101 if (this._element !== undefined)
102 return this._element;
103
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000104 // Create element
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000105 this._element = document.createElement('div');
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000106 var e = this._element;
107 e.setAttribute('tabindex', 0);
108 e.style.outline = 0;
109
110 var cl = e.classList;
111 cl.add('value');
112 if (this.regex() === true)
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000113 cl.add('regex');
114
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000115 // Add input field
116 this._input = e.appendChild(
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000117 document.createElement('input')
118 );
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000119 if (this.value() !== undefined)
120 this._input.value = this.value();
121
Nils Diewald9c125062015-05-05 23:54:17 +0000122 // Add regex button
123 var re = e.appendChild(
124 document.createElement('div')
125 );
126 re.addEventListener(
127 'click',
128 function (e) {
129 this.toggleRegex();
130 e.halt();
131 }.bind(this),
132 true
133 );
134 re.appendChild(document.createTextNode('RE'));
135
136 e.addEventListener(
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000137 'blur',
Nils Diewald9c125062015-05-05 23:54:17 +0000138 function (e) {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000139 this.store(this.value(), this.regex());
Nils Diewald9c125062015-05-05 23:54:17 +0000140 e.halt();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000141 }.bind(this)
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000142 );
143
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000144 this._input.addEventListener(
145 'keypress',
146 function (e) {
147 if (e.keyCode == 13) {
148 this.value(this._input.value);
149 this.store(this.value(), this.regex());
150 return false;
151 };
152 }.bind(this)
153 );
154
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000155 // Add store button
156 /*
157 e.appendChild(
158 document.createElement('div')
159 ).addEventListener('click', function () {
160 this.store(this.value(), this.regex());
161 }.bind(this));
162 */
163 return e;
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000164 }
165});