SmartIT_Extensions/BMC/smart-it-full/scripts/app/common/placeholder-polyfill-direct...

59 lines
2.9 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('myitsmApp')
.directive('placeholder', ['$compile', function ($compile) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
// element must be either input or textarea
if (element[0].nodeName.toLowerCase() !== 'input' && element[0].nodeName.toLowerCase() !== 'textarea') {
return;
}
// ignore for Bootstrap directives
if (attrs.bsDatepicker || attrs.bsTimepicker) {
return;
}
// element must have ngModel
if (!attrs.ngModel) {
return;
}
// element must not support placeholders natively
if (!_.isUndefined(element[0].placeholder)) {
return;
}
var childScope = scope.$new(true, scope);
childScope.inputClass = attrs.class;
childScope.inputPlaceholder = attrs.placeholder;
scope.$watch(attrs.ngModel, function (newValue) {
childScope.inputNgModel = attrs.typeaheadEditable === "false" ? element[0].value : newValue;
});
scope.$watch(attrs.ngHide, function (newValue) {
childScope.inputNgHide = newValue;
});
var placeholderElement = angular.element('<span prevent-click-event class="ie-placeholder {{inputClass}}">{{inputPlaceholder}}</span>');
element[0].parentNode.insertBefore(placeholderElement[0], element[0]);
if (!attrs.ngHide) {
// placeholder will be shown only when input's value is empty
placeholderElement.attr('ng-show', '!inputNgModel');
}
else {
// if ng-hide is present, placeholder will be hidden if input's value is present, or its ng-hide is true
placeholderElement.attr('ng-hide', 'inputNgModel || inputNgHide');
}
function placeholderClickHandler() {
element.focus();
element.click();
}
placeholderElement.on('click', placeholderClickHandler);
scope.$on("$destroy", function () {
console.log("placeholder: unbind events");
placeholderElement.off('click', placeholderClickHandler);
placeholderElement = null;
});
$compile(placeholderElement)(childScope);
}
};
}]);
}());