diff --git a/ionic-rating.coffee b/ionic-rating.coffee index da5a913..30739a0 100644 --- a/ionic-rating.coffee +++ b/ionic-rating.coffee @@ -6,7 +6,7 @@ angular.module 'ionic.rating', [] stateOff: null } -.controller 'RatingController', ($scope, $attrs, ratingConfig) -> +.controller 'RatingController', ['$scope', '$attrs', 'ratingConfig', ($scope, $attrs, ratingConfig) -> ngModelCtrl = { $setViewValue: angular.noop } this.init = (ngModelCtrl_) -> @@ -58,7 +58,7 @@ angular.module 'ionic.rating', [] $scope.value = ngModelCtrl.$viewValue return this - +] .directive 'rating', -> return { restrict: 'EA' diff --git a/ionic-rating.js b/ionic-rating.js index e25f199..7e811ff 100644 --- a/ionic-rating.js +++ b/ionic-rating.js @@ -1,73 +1,92 @@ -// Generated by CoffeeScript 1.9.1 +// Generated by CoffeeScript 1.10.0 (function() { angular.module('ionic.rating', []).constant('ratingConfig', { - max: 5 - }).controller('RatingController', function($scope, $attrs, ratingConfig) { - var ngModelCtrl; - ngModelCtrl = { - $setViewValue: angular.noop - }; - this.init = function(ngModelCtrl_) { - var max, ratingStates; - ngModelCtrl = ngModelCtrl_; - ngModelCtrl.$render = this.render; - max = angular.isDefined($attrs.max) ? $scope.$parent.$eval($attrs.max) : ratingConfig.max; - return $scope.range = this.buildTemplateObjects(ngModelCtrl.$modelValue, max); - }; - this.buildTemplateObjects = function(stateValue, max) { - var i, j, len, states = []; - - for (j = 0; j < max; j++) { - if(stateValue > j && stateValue < j+1) - states[j] = 2; - else if(stateValue > j) - states[j] = 1; - else - states[j] = 0; - } - - return states; - }; - $scope.rate = function(value) { - if (!$scope.readonly && value >= 0 && value <= $scope.range.length) { - ngModelCtrl.$setViewValue(value); - return ngModelCtrl.$render(); - } - }; - $scope.onKeydown = function(evt) { - if (/(37|38|39|40)/.test(evt.which)) { - evt.preventDefault(); - evt.stopPropagation(); - return $scope.rate($scope.value + (evt.which === 38 || evt.which === 39 ? { - 1: -1 - } : void 0)); - } - }; - this.render = function() { - return $scope.value = ngModelCtrl.$viewValue; - }; - return this; - }).directive('rating', function($timeout) { + max: 5, + stateOn: null, + stateOff: null + }).controller('RatingController', [ + '$scope', '$attrs', 'ratingConfig', function($scope, $attrs, ratingConfig) { + var ngModelCtrl; + ngModelCtrl = { + $setViewValue: angular.noop + }; + this.init = function(ngModelCtrl_) { + var max, ratingStates; + ngModelCtrl = ngModelCtrl_; + ngModelCtrl.$render = this.render; + this.stateOn = angular.isDefined($attrs.stateOn) ? $scope.$parent.$eval($attrs.stateOn) : ratingConfig.stateOn; + this.stateOff = angular.isDefined($attrs.stateOff) ? $scope.$parent.$eval($attrs.stateOff) : ratingConfig.stateOff; + max = angular.isDefined($attrs.max) ? $scope.$parent.$eval($attrs.max) : ratingConfig.max; + ratingStates = angular.isDefined($attrs.ratingStates) ? $scope.$parent.$eval($attrs.ratingStates) : new Array(max); + return $scope.range = this.buildTemplateObjects(ratingStates); + }; + this.buildTemplateObjects = function(states) { + var i, j, len, ref; + ref = states.length; + for (j = 0, len = ref.length; j < len; j++) { + i = ref[j]; + states[i] = angular.extend({ + index: 1 + }, { + stateOn: this.stateOn, + stateOff: this.stateOff + }, states[i]); + } + return states; + }; + $scope.rate = function(value) { + if (!$scope.readonly && value >= 0 && value <= $scope.range.length) { + ngModelCtrl.$setViewValue(value); + return ngModelCtrl.$render(); + } + }; + $scope.reset = function() { + $scope.value = ngModelCtrl.$viewValue; + return $scope.onLeave(); + }; + $scope.enter = function(value) { + if (!$scope.readonly) { + $scope.value = value; + } + return $scope.onHover({ + value: value + }); + }; + $scope.onKeydown = function(evt) { + if (/(37|38|39|40)/.test(evt.which)) { + evt.preventDefault(); + evt.stopPropagation(); + return $scope.rate($scope.value + (evt.which === 38 || evt.which === 39 ? { + 1: -1 + } : void 0)); + } + }; + this.render = function() { + return $scope.value = ngModelCtrl.$viewValue; + }; + return this; + } + ]).directive('rating', function() { return { restrict: 'EA', require: ['rating', 'ngModel'], scope: { - readonly: '=?' + readonly: '=?', + onHover: '&', + onLeave: '&' }, controller: 'RatingController', - template: '
', + template: '', replace: true, link: function(scope, element, attrs, ctrls) { var ngModelCtrl, ratingCtrl; ratingCtrl = ctrls[0]; ngModelCtrl = ctrls[1]; if (ngModelCtrl) { - $timeout(function(){ return ratingCtrl.init(ngModelCtrl); - }) } } }; }); -}).call(this); \ No newline at end of file +}).call(this); diff --git a/ionic-rating.min.js b/ionic-rating.min.js index 8fd7397..982b47b 100644 --- a/ionic-rating.min.js +++ b/ionic-rating.min.js @@ -1 +1 @@ -(function(){angular.module('ionic.rating',[]).constant('ratingConfig',{max:5}).controller('RatingController',function($scope,$attrs,ratingConfig){var ngModelCtrl;ngModelCtrl={$setViewValue:angular.noop};this.init=function(ngModelCtrl_){var max,ratingStates;ngModelCtrl=ngModelCtrl_;ngModelCtrl.$render=this.render;max=angular.isDefined($attrs.max)?$scope.$parent.$eval($attrs.max):ratingConfig.max;return $scope.range=this.buildTemplateObjects(ngModelCtrl.$modelValue,max)};this.buildTemplateObjects=function(stateValue,max){var i,j,len,states=[];for(j=0;j