This repository has been archived on 2022-01-02. You can view files and clone it, but cannot push or open issues or pull requests.
AriaNg/app/scripts/directives/setting.js

73 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-06-01 17:31:48 +02:00
(function () {
'use strict';
angular.module("ariaNg").directive('ngSetting', ['$timeout', 'ariaNgConstants', function ($timeout, ariaNgConstants) {
return {
restrict: 'E',
templateUrl: "views/setting.html",
require: '?ngModel',
replace: true,
scope: {
option: '=',
ngModel: '=',
status: '=',
2016-06-01 18:02:10 +02:00
onPendingChangeValue: '&',
2016-06-01 17:31:48 +02:00
onChangeValue: '&'
},
link: function (scope, element, attrs, ngModel) {
var pendingSaveRequest = null;
var options = {
lazySaveTimeout: ariaNgConstants.lazySaveTimeout
};
angular.extend(options, attrs);
scope.getTotalCount = function () {
if (!scope.optionValue && !angular.isString(scope.optionValue)) {
return 0;
}
return scope.optionValue.split(scope.option.split).length;
};
scope.changeValue = function (optionValue, lazySave) {
scope.optionValue = optionValue;
if (!scope.option || !scope.option.key || scope.option.readonly) {
return;
}
var data = {
key: scope.option.key,
value: optionValue
};
2016-06-01 18:02:10 +02:00
if (scope.onPendingChangeValue) {
scope.onPendingChangeValue(data);
2016-06-01 17:31:48 +02:00
}
if (pendingSaveRequest) {
$timeout.cancel(pendingSaveRequest);
}
if (scope.onChangeValue) {
if (lazySave) {
pendingSaveRequest = $timeout(function () {
scope.onChangeValue(data);
}, options.lazySaveTimeout);
} else {
scope.onChangeValue(data);
}
}
};
scope.$watch(function () {
return ngModel.$viewValue;
}, function (value) {
scope.optionValue = value;
});
}
};
}]);
})();