forked from culturebase/cbui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.js
539 lines (482 loc) · 15.9 KB
/
input.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/* This file is part of cbui.
* Copyright © 2010-2012 stiftung kulturserver.de ggmbh <[email protected]>
*
* cbui is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* cbui is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with cbui. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* An input widget. Input widgets can be validated, they have a value and they
* can be marked as containing an input error.
*/
jQuery.CbWidget.input = jQuery.CbWidget.widget.extend({
/**
* Create the input widget.
*/
constructor : function(element) {
this.base(element);
var title = this.element().attr('title');
if (title) this.texts.title = title;
/**
* validators for this widget.
*/
this.validators = [];
},
changeLanguage : function(bricks) {
if (this.texts.title) {
this.element().attr('title', bricks[this.texts.title]);
}
this.base(bricks);
},
/**
* Query or set the current value of the field.
* @param val the new value (optional)
* @return the value of the field
*/
value : function(val) {
if (typeof(val) == 'undefined') {
return this.element().val();
} else {
return this.element().val(val);
}
},
/**
* Run all validators on this widget.
* @return true if validation was successful, false otherwise
*/
validate : function() {
var self = this;
var ret = true;
this.valid();
jQuery.each(this.validators, function(i, validator) {
if (!validator.valid(self)) {
self.error();
ret = false;
return false;
} else {
return true;
}
});
return ret;
},
/**
* Certain widgets have defined "finished" states which may be important.
* For example the search box will return a value but an invalid ID if it's
* queries before editing is finished.
*/
editingFinished : function() {return true;},
/**
* callback to announce that this field has an input error. Assigns the
* class '__CbUiInputError' to the element.
*/
handleError : function() {
this.element().addClass("__CbUiInputError");
return this;
},
/**
* clear the error state and remove the CSS class '__CbUiInputError'.
*/
handleValid : function() {
this.element().removeClass("__CbUiInputError");
return this;
},
/**
* Remove the widget and all its validators.
*/
handleDestroy : function() {
jQuery.each(this.validators, function(i, validator) {
validator.destroy();
});
this.base();
}
}, {
init : function() {
/* triggered when the input has been found invalid */
jQuery.CbEvent(this, 'error');
/* triggered when the input has been found valid */
jQuery.CbEvent(this, 'valid');
this.base();
}
});
/**
* Text input widget.
* Text input fields are expected to have a predefined standard value which is
* the ML brick associated with the label registered at position "text" (i.e.
* this.bricks[this.texts.text]).
* Focus handling is centralized for text input widgets.
*/
jQuery.CbWidget.inputText = jQuery.CbWidget.input.extend({
/**
* change the language. The description of the widget is usually shown in
* the input field itself if it hasn't been edited yet. This is reflected
* here. You can override this function or value() to change the behaviour.
*
* For external labels, consider using separate text widgets, though.
*/
changeLanguage : function(bricks) {
var label = this.texts.text;
if (this.value() == '' || this.value() == this.bricks[label]) {
this.value(bricks[label]);
}
this.bricks[label] = bricks[label];
this.base(bricks);
},
reset : function() {
this.value(this.bricks[this.texts.text]);
this.element().removeClass('__CbUiFieldEdited');
this.element().addClass('__CbUiFieldUnedited');
},
/**
* (re-)bind the events associated with this widget. Focus and blur events
* from the widget's elements are passed on to trigger the respective events
* on the widget itself.
*/
bindEvents : function() {
var self = this;
this.element().focus(function() {
self.focus();
}).blur(function() {
self.blur();
}).click(function() {
this.select();
});
return this;
},
/**
* reread the element from the DOM and rebind the events.
*/
refreshElement : function() {
this.base();
this.bindEvents();
return this;
},
/**
* Create the text input widget. Assigns the class __CbUiFieldUnedited to
* its element.
*/
constructor : function(element) {
this.base(element);
/**
* bricks to be used for translation and for comparing with standard value.
*/
this.bricks = {};
var label = this.value();
this.texts.text = label;
this.bricks[label] = label;
this.element().addClass('__CbUiFieldUnedited');
this.bindEvents();
},
/**
* blur handler. Sets the class '__CbUiFieldUnedited' on the element if it's
* empty or contains the standard value.
*/
handleBlur : function() {
if (this.value() == '' || this.value() == this.bricks[this.texts.text]) {
this.reset();
}
return this;
},
/**
* focus handler. Sets the class '__CbUiFieldEdited' on the element.
*/
handleFocus : function() {
this.element().removeClass('__CbUiFieldUnedited');
this.element().addClass('__CbUiFieldEdited');
return this;
}
}, {
init : function() {
jQuery.CbEvent(this, 'focus');
jQuery.CbEvent(this, 'blur');
return this.base();
}
});
/**
* A password entry widget. This is basically a text entry widget which changes
* its type to "password" as soon as you type anything into it. It can optionally
* use strength checking on the password field and open a hint element to explain
* that.
* You need pstrength.js if you want to use the password widget's strength check.
*/
jQuery.CbWidget.password = jQuery.CbWidget.inputText.extend({
/**
* reread the element and the backup elements from the DOM and
* reattach the strength handler.
*/
refreshElement : function() {
this.base();
this.pivot.refreshElement();
this.cycler.refreshElement();
if (this.strength_check) this.cycler.elements.pstrength();
this.pivot.parent.children().first().unbind('blur');
},
/**
* add a strength check. The background color will appear in various shades
* of green and red depending on the strength of the password.
* @param hint_element an optional hint widget to be shown while editing the
* password.
*/
addStrengthCheck : function(hint_element) {
this.strength_check = true;
this.hint_element = hint_element;
this.default_color = this.element().css('background-color');
this.cycler.elements.pstrength();
this.cycler.getShown().css('background-color', this.current_color);
if (this.hint_element !== undefined) this.hint_element.slideDown();
},
removeStrengthCheck : function() {
if (this.hint_element !== undefined) this.hint_element.slideUp();
this.cycler.elements.pstrength('destroy');
this.element().css('background-color', this.default_color);
delete this.hint_element;
this.strength_check = false;
},
/**
* create the password widget.
* This actually replaces the given field with a span containing two input
* fields: one with type 'password' and one with type 'text'. During the life
* time of the widgets those are shown and hidden to conform to the standard
* behaviour of text input widgets.
*/
constructor : function(field) {
this.pivot = new CbElementPivot(field);
/* We can't use clone() here as IE and Opera don't like setting the
* type attribute on existing nodes.
*/
var new_child = jQuery('<input type="text"/>');
var self = this;
jQuery.each(['style', 'class', 'value', 'size', 'maxlength', 'name'], function(i, attrib) {
var val = self.pivot.child.attr(attrib);
/* extra ugly clutch to make FF 4 happy: you can't set maxlength = -1 */
if (typeof(val) != 'undefined' && (attrib != 'maxlength' || val != -1)) {
new_child.attr(attrib, val);
}
});
this.pivot.parent.prepend(new_child);
this.cycler = new CbElementCycler(this.pivot.parent.children());
this.base(this.pivot.parent);
this.pivot.parent.children().first().unbind('blur'); // no need to handle blur event of text field
return this;
},
/**
* focus handler.
* Change the type attribute of the password field. This is complicated because of
* cross browser issues.
*/
handleFocus : function() {
if (this.cycler.getShown().attr('type') != 'password') {
this.cycler.show();
/* you should be able to type a password now */
/* pass the focus and rebind the keypress as we have swapped the
* node */
this.cycler.getShown().focus();
/* The field should have focus so that you can type a password now. */
}
if (this.value() == '' || this.value() == this.bricks[this.texts.text]) {
this.value('');
}
return this.base();
},
/**
* blur handler.
* change the type back to 'text' if the content hasn't been edited.
*/
handleBlur : function() {
if (this.value() == '' || this.value() == this.bricks[this.texts.text]) {
this.cycler.show();
}
return this.base();
},
/**
* get or set the current value for the password
* @return the value of the currently active field
*/
value : function(val) {
if (typeof(val) != 'undefined') {
this.cycler.elements.val(val);
return val;
} else {
return this.cycler.getShown().val();
}
},
/**
* destroy the widget and remove the backup elements.
* @return this
*/
handleDestroy : function() {
this.cycler.destroy();
this.pivot.destroy();
return this.base();
},
/**
* return all elements in the cycler.
*/
element : function() {
return this.cycler.elements;
}
});
/**
* A widget for the "select" input. It translates all its options on
* changeLanguage.
*/
jQuery.CbWidget.select = jQuery.CbWidget.input.extend({
/**
* create the widget and collect the texts to be translated
* @param element the element the widget should attach itself to.
*/
constructor : function(element) {
this.base(element);
var self = this;
this.element().children().each(function() {
var label = jQuery(this).text();
self.texts['value' + jQuery(this).val()] = label;
});
},
/**
* translate all options.
* @param bricks the translations
*/
changeLanguage : function(bricks) {
var self = this;
this.element().children().each(function() {
var label = self.texts['value' + jQuery(this).val()];
jQuery(this).html(bricks[label]);
});
this.base(bricks);
},
addOption : function(value, text, css_class, selected) {
var el = jQuery(document.createElement('option')).val(value).text(text);
if (css_class) el.addClass(css_class);
if (selected) el.attr('selected', 'true');
this.element().append(el);
}
});
/**
* A widget for the "checkbox" input. The pivoting is needed to make the element
* stylable (checkboxes themselves can't be styled in firefox).
*/
jQuery.CbWidget.checkbox = jQuery.CbWidget.input.extend({
constructor : function(element) {
this.pivot = new CbElementPivot(element);
this.checkbox = this.pivot.child;
this.base(this.pivot.parent);
},
handleDestroy : function() {
this.base();
this.pivot.destroy();
},
refreshElement : function() {
this.base();
this.checkbox = jQuery(this.checkbox);
this.pivot.refreshElement();
},
/**
* Query or set the current value of the checkbox.
* An unchecked checkbox always returns false when queried.
* A checked checkbox without attribute "value" set returns true when queried.
* A checked checkbox with value set returns the value when queried.
* When setting the value, only the "checked" state is set according to the
* truthiness of the parameter.
* @param val the new value (optional)
* @return the value of the checkbox
*/
value : function(val) {
if (typeof(val) == 'undefined') {
if (!this.checkbox.attr('checked')) {
return false;
} else if (!this.checkbox.val()) {
return true;
} else {
return this.checkbox.val();
}
} else {
this.checkbox.attr('checked', val);
return this.value();
}
}
});
/**
* a searchbox widget. It invokes autocomplete() on its element's second child
* and has the ID recorded in its element's first child. You can configure it
* using the "options" member. All options are passed on to autocomplete.
* Autocomplete is reinitialized when changing the language. As the text input
* is managed by autocomplete, this is not a real text input widget.
* You'll need autocomplete2/mod.autocomplete.js if you want to use this widget.
*/
jQuery.CbWidget.searchBox = jQuery.CbWidget.inputText.extend({
constructor : function(element) {
this.options = {};
if (jQuery.CbWidgetRegistry.language) {
this.options.language = jQuery.CbWidgetRegistry.language;
}
this.pivot = new CbElementPivot(element, 'div');
this.name_field = this.pivot.child;
var id_field = jQuery(document.createElement('input')).attr('type', 'hidden');
this.base(this.pivot.parent);
var id = 'searchbox' + this.base2ID;
this.options.putIdInto = '#'+ id;
this.pivot.parent.prepend(id_field.attr('id', id));
},
changeLanguage : function(bricks) {
this.base(bricks);
this.options.language = jQuery.CbWidgetRegistry.language;
this.name_field.unbind();
},
handleReady : function(options) {
jQuery.extend(this.options, options);
this.name_field.autoComplete(this.options);
return this.base(options);
},
refreshElement : function() {
this.name_field.unbind();
this.base();
this.name_field = jQuery(this.name_field);
this.name_field.autoComplete(this.options);
this.pivot.refreshElement();
},
editingFinished : function() {
return this.name_field.hasClass('__AC_freetext') || this.name_field.hasClass('__AC_validated');
},
value : function(val) {
if (typeof(val) == 'undefined') {
return this.name_field.val();
} else {
return this.name_field.val(val);
}
},
/**
* get or set the ID associated with the current value of the field
*/
valueId : function(val) {
if (typeof(val) == 'undefined') {
return jQuery(this.options.putIdInto).val();
} else {
return jQuery(this.options.putIdInto).val(val);
}
},
handleDestroy : function() {
this.base();
this.pivot.destroy();
},
handleHide : function() {
this.pivot.parent.find('.__AC_close').click();
this.base();
},
element : function() {
return this.pivot.child;
}
});
/**
* Same as inputText, but with a textarea as base element. The distinction may
* be important for styling.
*/
jQuery.CbWidget.inputTextArea = jQuery.CbWidget.inputText.extend({});