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/src/scripts/directives/setting.js

140 lines
5.1 KiB
JavaScript
Raw Normal View History

2016-06-01 17:31:48 +02:00
(function () {
'use strict';
2016-06-11 12:52:40 +02:00
angular.module('ariaNg').directive('ngSetting', ['$timeout', 'ariaNgConstants', function ($timeout, ariaNgConstants) {
2016-06-01 17:31:48 +02:00
return {
restrict: 'E',
2016-06-11 12:52:40 +02:00
templateUrl: 'views/setting.html',
2016-06-01 17:31:48 +02:00
require: '?ngModel',
replace: true,
scope: {
option: '=',
ngModel: '=',
placeholder: '=?',
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);
2016-06-02 18:16:27 +02:00
scope.optionStatus = (function () {
var value = 'ready';
return {
getValue: function () {
return value;
},
setReady: function () {
value = 'ready';
},
setPending: function () {
value = 'pending';
},
setSaving: function () {
value = 'pending';
},
setSuccess: function () {
value = 'success';
},
setFailed: function () {
value = 'failed';
},
setError: function () {
value = 'error';
},
getStatusFeedbackStyle: function () {
if (value == 'success') {
return 'has-success';
} else if (value == 'failed') {
return 'has-warning';
} else if (value == 'error') {
return 'has-error';
} else {
return '';
}
},
getStatusIcon: function () {
if (value == 'pending') {
return 'fa-hourglass-start';
} else if (value == 'saving') {
return 'fa-spin fa-pulse fa-spinner';
} else if (value == 'success') {
return 'fa-check';
} else if (value == 'failed') {
return 'fa-exclamation';
} else if (value == 'error') {
return 'fa-times';
} else {
return '';
}
},
isShowStatusIcon: function () {
return this.getStatusIcon() != '';
}
};
})();
2016-06-01 17:31:48 +02:00
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;
2016-06-02 18:16:27 +02:00
scope.optionStatus.setReady();
2016-06-01 17:31:48 +02:00
if (!scope.option || !scope.option.key || scope.option.readonly) {
return;
}
var data = {
key: scope.option.key,
2016-06-02 18:16:27 +02:00
value: optionValue,
optionStatus: scope.optionStatus
2016-06-01 17:31:48 +02:00
};
if (pendingSaveRequest) {
$timeout.cancel(pendingSaveRequest);
}
2016-06-02 18:16:27 +02:00
var invokeChange = function () {
scope.optionStatus.setSaving();
scope.onChangeValue(data);
};
2016-06-01 17:31:48 +02:00
if (scope.onChangeValue) {
if (lazySave) {
2016-06-02 18:16:27 +02:00
scope.optionStatus.setPending();
2016-06-01 17:31:48 +02:00
pendingSaveRequest = $timeout(function () {
2016-06-02 18:16:27 +02:00
invokeChange();
2016-06-01 17:31:48 +02:00
}, options.lazySaveTimeout);
} else {
2016-06-02 18:16:27 +02:00
invokeChange();
2016-06-01 17:31:48 +02:00
}
}
};
2016-06-20 18:22:18 +02:00
if (ngModel) {
scope.$watch(function () {
return ngModel.$viewValue;
}, function (value) {
scope.optionValue = value;
});
}
2016-06-05 09:52:55 +02:00
2016-06-11 12:52:40 +02:00
scope.$watch('option', function () {
element.find('[data-toggle="popover"]').popover();
2016-06-05 09:52:55 +02:00
});
2016-06-01 17:31:48 +02:00
}
};
}]);
})();