SmartIT_Extensions/BMC/smart-it-full/scripts/app/common/currency-input-directive.js

76 lines
3.8 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('myitsmApp')
.directive('currencyInput', ['$timeout', 'metadataModel',
function ($timeout, metadataModel) {
return {
restrict: 'E',
templateUrl: 'views/common/currency-input-directive.html',
replace: true,
require: '?^form',
scope: {
// mandatory attributes
fieldName: '@',
model: '=',
// optional attributes
id: '@',
title: '@',
inputDisabled: '=',
required: '='
},
link: function (scope, element, attr) {
var dropdownToggle = element.find('.dropdown-toggle');
scope.state = {
isDropdownOpen: false
};
scope.currencySelected = function (currency) {
scope.selectedCurrency = currency;
scope.model.currencycode = currency.currencycode;
scope.step = Math.pow(10, -currency.precision);
$timeout(function () {
//manual close of dropdown for IE9-IE10
if (scope.state.isDropdownOpen) {
scope.state.isDropdownOpen = false;
}
dropdownToggle.focus();
}, 0);
};
scope.handleKeydown = function ($event) {
// In case this directive is used in $modal dialog, need to intercept the escape key before $modal action blade
// otherwise, hitting escape key in dropdown expand mode will close the $modal action blade instead
if ($event.keyCode === 27) { // escape key
$timeout(function () {
dropdownToggle.trigger('click');
dropdownToggle.focus();
}, 0);
$event.stopPropagation();
}
};
function init() {
metadataModel.getMetadataByType(EntityVO.TYPE_ASSET).then(function (AssetMetadata) {
scope.metadata = AssetMetadata && AssetMetadata.currencyFields && AssetMetadata.currencyFields[scope.fieldName];
if (scope.model) {
// use currencycode from model otherwise use the default from metadata
var defaultCurrency = scope.model.currencycode || (scope.metadata && scope.metadata.currencycode) || 'USD';
scope.currencySelected(_.find(scope.metadata.currencyOptions, { currencycode: defaultCurrency }));
}
// scope.model might not be available during init time, we need fallback to fill it in if it comes later
scope.$watch('model.currencycode', function (newValue) {
if (newValue) {
scope.currencySelected(_.find(scope.metadata.currencyOptions, { currencycode: scope.model.currencycode }));
}
});
if (angular.isDefined(attr.autofocus)) {
$timeout(function () {
dropdownToggle.focus();
}, 300);
}
});
}
init();
}
};
}]);
}());