-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaceholder.js
More file actions
92 lines (74 loc) · 2.57 KB
/
placeholder.js
File metadata and controls
92 lines (74 loc) · 2.57 KB
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
app.directive('myPlaceholder', ['$timeout', function ($timeout) {
'use strict';
return {
restrict: 'A',
require: 'ngModel',
// If using isolated scope when a sibling directive is used events will be fired into orphaned scope
// (i.e. they will not propagate properly).
// The real problem here is that this directive modifies DOM outside it's scope.
//scope: {},
compile: function (element, attrs) {
element.parent().append('<label class="my-placeholder"></label>');
return function (scope) {
function safeApply(scope, fn) {
return (scope.$$phase || scope.$root.$$phase) ? fn() : scope.$apply(fn);
}
var input = element,
label = input.closest('div').find('label.my-placeholder');
function adjustInput() {
safeApply(scope, function () {
// --Use this if regular placeholder is used (except for IE9)
// scope['placeholder' + attrs.ngModel] = (input.val() === '') ? attrs.ngPlaceholder : scope['placeholder' + attrs.ngModel] = '';
// --else
if (evt === undefined && input.val() === '') {
label.removeClass('-selected');
if (label.html() !== attrs.myPlaceholder) {
label.html(attrs.myPlaceholder);
}
} else {
if (!label.hasClass('-selected')) {
label.addClass('-selected');
}
if (attrs.vnPlaceholderShort && attrs.vnPlaceholderShort.length > 0) {
label.html(attrs.vnPlaceholderShort);
}
// --This is needed in case placeholder will slide to right side of the input *****
// divider was calculated empirically (YMMV) - TT :)
//var divider = 2;
//if (label.html().length <= 5) {
// divider = 1.7;
//}
//input.css('padding-right', label.html().length / divider + 'em');
// ********************************************************************************
}
});
}
safeApply(scope, function () {
$timeout(function () {
label.html(attrs.vnPlaceholder);
}, 0);
});
// In case the input have some default value
scope.$watch('attrs.ngModel', function () {
adjustInput();
});
label
.on('click', function () {
input.focus();
});
input
.on('focus keyup change', function (evt) {
adjustInput(evt);
})
.on('blur', function () {
if (input.val() === '') {
label.removeClass('-selected');
if (label.html() !== attrs.myPlaceholder) {
label.html(attrs.myPlaceholder);
}
}
});
};
}
};
}]);