-
Notifications
You must be signed in to change notification settings - Fork 17
/
angular-placeholder-shim.js
67 lines (58 loc) · 1.56 KB
/
angular-placeholder-shim.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
/* angular-placeholder-shim version 0.3.1
* License: MIT.
* Copyright (C) 2013, Uri Shaked.
*/
'use strict';
angular.module('placeholderShim', [])
.directive('placeholder', ['$interpolate', '$timeout', function ($interpolate, $timeout) {
if (jQuery.placeholder.browser_supported()) {
return {};
}
return function (scope, element) {
var config = {
color: '#888',
cls: 'placeholder'
};
var interpolatedPlaceholder = $interpolate(element.attr('placeholder'));
var placeholderText = null;
var overlay = null;
var pendingTimer = null;
function addPlaceholder() {
pendingTimer = $timeout(function () {
element._placeholder_shim(config);
overlay = element.data('placeholder');
pendingTimer = null;
});
}
if (element.is(':visible')) {
addPlaceholder();
}
// The following code accounts for value changes from within the code
// and for dynamic changes in placeholder text
scope.$watch(function () {
if (!overlay && element.is(':visible') && !pendingTimer) {
addPlaceholder();
}
if (overlay && (element.get(0) !== document.activeElement)) {
if (element.val().length) {
overlay.hide();
} else {
overlay.show();
}
}
if (overlay) {
var newText = interpolatedPlaceholder(scope);
if (newText !== placeholderText) {
placeholderText = newText;
overlay.text(placeholderText);
}
}
});
scope.$on('$destroy', function() {
if (pendingTimer) {
$timeout.cancel(pendingTimer);
pendingTimer = null;
}
});
};
}]);