@@ 5338-5415 (lines=78) @@ | ||
5335 | titles : ['one', 'two', 'three', 'four', 'five'] |
|
5336 | }) |
|
5337 | ||
5338 | .controller('UibRatingController', ['$scope', '$attrs', 'uibRatingConfig', function($scope, $attrs, ratingConfig) { |
|
5339 | var ngModelCtrl = { $setViewValue: angular.noop }, |
|
5340 | self = this; |
|
5341 | ||
5342 | this.init = function(ngModelCtrl_) { |
|
5343 | ngModelCtrl = ngModelCtrl_; |
|
5344 | ngModelCtrl.$render = this.render; |
|
5345 | ||
5346 | ngModelCtrl.$formatters.push(function(value) { |
|
5347 | if (angular.isNumber(value) && value << 0 !== value) { |
|
5348 | value = Math.round(value); |
|
5349 | } |
|
5350 | ||
5351 | return value; |
|
5352 | }); |
|
5353 | ||
5354 | this.stateOn = angular.isDefined($attrs.stateOn) ? $scope.$parent.$eval($attrs.stateOn) : ratingConfig.stateOn; |
|
5355 | this.stateOff = angular.isDefined($attrs.stateOff) ? $scope.$parent.$eval($attrs.stateOff) : ratingConfig.stateOff; |
|
5356 | this.enableReset = angular.isDefined($attrs.enableReset) ? |
|
5357 | $scope.$parent.$eval($attrs.enableReset) : ratingConfig.enableReset; |
|
5358 | var tmpTitles = angular.isDefined($attrs.titles) ? $scope.$parent.$eval($attrs.titles) : ratingConfig.titles; |
|
5359 | this.titles = angular.isArray(tmpTitles) && tmpTitles.length > 0 ? |
|
5360 | tmpTitles : ratingConfig.titles; |
|
5361 | ||
5362 | var ratingStates = angular.isDefined($attrs.ratingStates) ? |
|
5363 | $scope.$parent.$eval($attrs.ratingStates) : |
|
5364 | new Array(angular.isDefined($attrs.max) ? $scope.$parent.$eval($attrs.max) : ratingConfig.max); |
|
5365 | $scope.range = this.buildTemplateObjects(ratingStates); |
|
5366 | }; |
|
5367 | ||
5368 | this.buildTemplateObjects = function(states) { |
|
5369 | for (var i = 0, n = states.length; i < n; i++) { |
|
5370 | states[i] = angular.extend({ index: i }, { stateOn: this.stateOn, stateOff: this.stateOff, title: this.getTitle(i) }, states[i]); |
|
5371 | } |
|
5372 | return states; |
|
5373 | }; |
|
5374 | ||
5375 | this.getTitle = function(index) { |
|
5376 | if (index >= this.titles.length) { |
|
5377 | return index + 1; |
|
5378 | } |
|
5379 | ||
5380 | return this.titles[index]; |
|
5381 | }; |
|
5382 | ||
5383 | $scope.rate = function(value) { |
|
5384 | if (!$scope.readonly && value >= 0 && value <= $scope.range.length) { |
|
5385 | var newViewValue = self.enableReset && ngModelCtrl.$viewValue === value ? 0 : value; |
|
5386 | ngModelCtrl.$setViewValue(newViewValue); |
|
5387 | ngModelCtrl.$render(); |
|
5388 | } |
|
5389 | }; |
|
5390 | ||
5391 | $scope.enter = function(value) { |
|
5392 | if (!$scope.readonly) { |
|
5393 | $scope.value = value; |
|
5394 | } |
|
5395 | $scope.onHover({value: value}); |
|
5396 | }; |
|
5397 | ||
5398 | $scope.reset = function() { |
|
5399 | $scope.value = ngModelCtrl.$viewValue; |
|
5400 | $scope.onLeave(); |
|
5401 | }; |
|
5402 | ||
5403 | $scope.onKeydown = function(evt) { |
|
5404 | if (/(37|38|39|40)/.test(evt.which)) { |
|
5405 | evt.preventDefault(); |
|
5406 | evt.stopPropagation(); |
|
5407 | $scope.rate($scope.value + (evt.which === 38 || evt.which === 39 ? 1 : -1)); |
|
5408 | } |
|
5409 | }; |
|
5410 | ||
5411 | this.render = function() { |
|
5412 | $scope.value = ngModelCtrl.$viewValue; |
|
5413 | $scope.title = self.getTitle($scope.value - 1); |
|
5414 | }; |
|
5415 | }]) |
|
5416 | ||
5417 | .directive('uibRating', function() { |
|
5418 | return { |
@@ 5337-5414 (lines=78) @@ | ||
5334 | titles : ['one', 'two', 'three', 'four', 'five'] |
|
5335 | }) |
|
5336 | ||
5337 | .controller('UibRatingController', ['$scope', '$attrs', 'uibRatingConfig', function($scope, $attrs, ratingConfig) { |
|
5338 | var ngModelCtrl = { $setViewValue: angular.noop }, |
|
5339 | self = this; |
|
5340 | ||
5341 | this.init = function(ngModelCtrl_) { |
|
5342 | ngModelCtrl = ngModelCtrl_; |
|
5343 | ngModelCtrl.$render = this.render; |
|
5344 | ||
5345 | ngModelCtrl.$formatters.push(function(value) { |
|
5346 | if (angular.isNumber(value) && value << 0 !== value) { |
|
5347 | value = Math.round(value); |
|
5348 | } |
|
5349 | ||
5350 | return value; |
|
5351 | }); |
|
5352 | ||
5353 | this.stateOn = angular.isDefined($attrs.stateOn) ? $scope.$parent.$eval($attrs.stateOn) : ratingConfig.stateOn; |
|
5354 | this.stateOff = angular.isDefined($attrs.stateOff) ? $scope.$parent.$eval($attrs.stateOff) : ratingConfig.stateOff; |
|
5355 | this.enableReset = angular.isDefined($attrs.enableReset) ? |
|
5356 | $scope.$parent.$eval($attrs.enableReset) : ratingConfig.enableReset; |
|
5357 | var tmpTitles = angular.isDefined($attrs.titles) ? $scope.$parent.$eval($attrs.titles) : ratingConfig.titles; |
|
5358 | this.titles = angular.isArray(tmpTitles) && tmpTitles.length > 0 ? |
|
5359 | tmpTitles : ratingConfig.titles; |
|
5360 | ||
5361 | var ratingStates = angular.isDefined($attrs.ratingStates) ? |
|
5362 | $scope.$parent.$eval($attrs.ratingStates) : |
|
5363 | new Array(angular.isDefined($attrs.max) ? $scope.$parent.$eval($attrs.max) : ratingConfig.max); |
|
5364 | $scope.range = this.buildTemplateObjects(ratingStates); |
|
5365 | }; |
|
5366 | ||
5367 | this.buildTemplateObjects = function(states) { |
|
5368 | for (var i = 0, n = states.length; i < n; i++) { |
|
5369 | states[i] = angular.extend({ index: i }, { stateOn: this.stateOn, stateOff: this.stateOff, title: this.getTitle(i) }, states[i]); |
|
5370 | } |
|
5371 | return states; |
|
5372 | }; |
|
5373 | ||
5374 | this.getTitle = function(index) { |
|
5375 | if (index >= this.titles.length) { |
|
5376 | return index + 1; |
|
5377 | } |
|
5378 | ||
5379 | return this.titles[index]; |
|
5380 | }; |
|
5381 | ||
5382 | $scope.rate = function(value) { |
|
5383 | if (!$scope.readonly && value >= 0 && value <= $scope.range.length) { |
|
5384 | var newViewValue = self.enableReset && ngModelCtrl.$viewValue === value ? 0 : value; |
|
5385 | ngModelCtrl.$setViewValue(newViewValue); |
|
5386 | ngModelCtrl.$render(); |
|
5387 | } |
|
5388 | }; |
|
5389 | ||
5390 | $scope.enter = function(value) { |
|
5391 | if (!$scope.readonly) { |
|
5392 | $scope.value = value; |
|
5393 | } |
|
5394 | $scope.onHover({value: value}); |
|
5395 | }; |
|
5396 | ||
5397 | $scope.reset = function() { |
|
5398 | $scope.value = ngModelCtrl.$viewValue; |
|
5399 | $scope.onLeave(); |
|
5400 | }; |
|
5401 | ||
5402 | $scope.onKeydown = function(evt) { |
|
5403 | if (/(37|38|39|40)/.test(evt.which)) { |
|
5404 | evt.preventDefault(); |
|
5405 | evt.stopPropagation(); |
|
5406 | $scope.rate($scope.value + (evt.which === 38 || evt.which === 39 ? 1 : -1)); |
|
5407 | } |
|
5408 | }; |
|
5409 | ||
5410 | this.render = function() { |
|
5411 | $scope.value = ngModelCtrl.$viewValue; |
|
5412 | $scope.title = self.getTitle($scope.value - 1); |
|
5413 | }; |
|
5414 | }]) |
|
5415 | ||
5416 | .directive('uibRating', function() { |
|
5417 | return { |