blob: f9aa593a4415bce1ca3dd9bd0ebac99f8fc0f657 [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
122 this._input.addEventListener(
123 'blur',
124 function () {
125 this.store(this.value(), this.regex());
126 }.bind(this)
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000127 );
128
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000129 this._input.addEventListener(
130 'keypress',
131 function (e) {
132 if (e.keyCode == 13) {
133 this.value(this._input.value);
134 this.store(this.value(), this.regex());
135 return false;
136 };
137 }.bind(this)
138 );
139
140 // Add regex button
141 var re = e.appendChild(
142 document.createElement('div')
143 );
144 re.addEventListener('click', function (e) {
145 this.toggleRegex();
146 e.halt();
147 }.bind(this));
148 re.appendChild(document.createTextNode('RE'));
149
150 // Add store button
151 /*
152 e.appendChild(
153 document.createElement('div')
154 ).addEventListener('click', function () {
155 this.store(this.value(), this.regex());
156 }.bind(this));
157 */
158 return e;
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000159 }
160});