-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhole.js
57 lines (50 loc) · 2.03 KB
/
hole.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
/// <reference path="./jquery.d.ts" />
var hole;
(function (hole) {
var addOptionalTextToLabel = function ($inputElem, $labelElem) {
var required = $inputElem.prop('required');
var originalLabelText = $labelElem.text();
var labelText = required ? originalLabelText : originalLabelText + " (optional)";
$labelElem.text(labelText);
$inputElem.attr({ placeholder: labelText });
};
var addClassToMiniLabelWhenFocused = function ($inputElem, $labelElem) {
var isFocused = $inputElem.focusE().map(true).merge($inputElem.blurE().map(false));
isFocused.onValue(function (isFocused) {
$labelElem.toggleClass('holelib-minilabel-focused', isFocused);
});
};
var hideMiniLabelWhenValueIsEmpty = function ($inputElem, $labelElem) {
var isEmpty = Bacon.$.textFieldValue($inputElem).map(function (val) {
return val.length == 0;
});
isEmpty.onValue(function (fieldIsEmpty) {
$labelElem.toggleClass('holelib-minilabel-hidden', fieldIsEmpty);
});
};
var holeTextInput = function ($container, opts) {
var $inputElem = opts.$inputElem;
var $labelElem = opts.$labelElem.addClass('holelib-minilabel');
addOptionalTextToLabel($inputElem, $labelElem);
addClassToMiniLabelWhenFocused($inputElem, $labelElem);
hideMiniLabelWhenValueIsEmpty($inputElem, $labelElem);
return $container.append($labelElem, $inputElem);
};
function create(opts) {
var $container = $('<div>').addClass('holelib-container');
var $inputElem = opts.$inputElem;
var $labelElem = opts.$labelElem;
$inputElem.addClass('holelib-input');
$labelElem.addClass('holelib-label');
switch (opts.type) {
case 'text':
case 'tel':
case 'email':
case 'textarea':
default:
return holeTextInput($container, opts);
break;
}
}
hole.create = create;
})(hole || (hole = {}));